Repository navigation
fix: clamp derived state to the last written chapter on delete - #292
Merged
Merged
Conversation
Character profiles, world facts and sync markers advance by chapter number but do
not roll back when chapters are deleted, so facts kept pointing at chapters that
no longer existed and were injected into later prompts.
- `clamp_derived_state_after_delete`: after a chapter delete, drop character facts
and world-setting claims whose chapters are gone, clear out-of-range
`current_state` (and reset its marker so re-analysis of earlier chapters can
rebuild it), and clamp the sync markers.
- The cutoff is the last chapter **with content**, not the last chapter row:
batch generation creates empty chapter shells, and using their numbers kept
facts from deleted chapters legal.
- `find_stale_derived_state` + `GET /project/{id}/consistency`: read-only report of
facts/markers that still reference missing chapters, so this class of drift is
detectable instead of silent.
- 1-N context rendering skips state whose source chapter is after the chapter
being written.
Verified on the affected project: 118 world-setting claims from deleted chapters
were pruned, 0 stale references remain.
Refs #288
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #288.
Problem
Character profiles, world-setting facts and sync markers advance by chapter number but never roll back when chapters are deleted. On a real project, after removing chapters 58–70:
state_updated_chapter/profile_synced_through_chapterstill said 62;profile_factsstill cited chapters 58/59/61/62;Change
app/services/derived_state.py, called afterDELETE /chapters/{id}):current_stateand reset its marker toNULL(setting it to the cutoff would make the update service skip re-analysis of earlier chapters — the anti-regression check ischapter < state_updated_chapter);profile_synced_through_chapterandworld_profile_synced_through_chapter.GET /project/{id}/consistencyreports stale facts/markers (read-only, truncated list + total), so this drift is detectable rather than silent.Verification
Tests cover: clamping facts/state/markers, world-claim pruning (evidence kept when partly in range, claims without evidence preserved), idempotency, "clean data produces no UPDATE", and the read-time skip for both directions.
Independent review
Reviewed by a different model. One MAJOR was raised — world-setting facts were only watermark-clamped while their content stayed — and is addressed here (pruning + re-render avoided on purpose: re-rendering the four text columns from facts would rewrite existing content, verified by length comparison). Reviewer MINORs also handled: marker reset to
NULL, unparseable facts counted in the report, endpoint list truncated to 20 with a total, and the no-op write path now has a test.Not done here (deliberately): soft delete and an archive/trash table. If chapters should be recoverable, that is a separate change with its own read-path audit.
Desensitized per repository policy: no source text, titles or character names are included.