story-tracker-common
marginalia/libs/story-tracker-common is what the plugins that follow the story share: the Knowledge Tracker and the State Tracker (which uses
the present characters reading, the payload insertion and UiAccess so far; the processor comes with its extraction). Read Libraries for plugins first for how a library is embedded and what that implies (a copy per
plugin, no shared state, parameters instead of constants).
The idea
A tracker keeps a state per book (saved in the book's attributes, so it travels with backups) and reads the parts of the active branch in order, from a start point; a cursor says how far it got. Everything it takes from a part remembers that part (its uuid), so the story can change under it: a swipe, another branch, a regenerate or a reprocess take back what came from the parts that are no longer read, and the cursor goes back to the last part both branches share. Only the parts that are settled are read (the last part may still be redrafted).
flowchart LR
B[StoryBranch.read] -->|List of Part| P[BranchProcessor]
S[(state of the tracker<br/>implements TrackerProgress)] <--> P
P -->|read part| S
P -->|pending parts| M[the tracker's model step]Packages
Using it in a tracker
- The plugin's state implements
TrackerProgress(with Gson, the fieldsstartMessage,cursor,processedare plain fields of the state). - A service loads the state and the branch (
StoryBranch.read), callsBranchProcessorunder a lock per book and saves the state. - The model step reads
BranchProcessor.pending(...), asks the model and commits: move the cursor over the run first (BranchProcessor.processwith the end of the run as the settled count), then apply the changes, so the alignment does not take them back (the Knowledge Tracker'sRunCommit).
Checkpoints
The per-entry undo of the log cannot move the cursor back, and it only reverts items still as the run left them. A checkpoint is the whole state of
the tracker in the book (as the tracker serializes it with Gson for the book's attributes, cursor and processed parts included) as a run found it,
taken when the run commits and keyed by the uuid of the cursor's part, TrackerCheckpoints.START when the state had read nothing. Restoring it undoes
that run: the run's parts are after the cursor again. (A checkpoint taken after its run would be the state the tracker is already in, the newest one
would undo nothing and the first run could never be undone.) TrackerCheckpoints (one per tracker, named after it, KEEP = 20 per book):
-
take(files, state, cursor, kind, added, retracted)stores{"part", "state"}gzipped and drops the oldest over the limit. It never throws (a failure is logged; the run stays committed); anullcursor isSTART.KindisRUN,TIDY,READ(without the model) orRESTORE. -
The metadata is in the file name,
<tracker>.checkpoint.<kind>.<added>.<retracted>.<part uuid>.before.json.gz, solist(newest first) opens no file and ignores the other files of the book. Files without.before(earlier 1.9.0 builds) hold the state after their run,Checkpoint.beforeisfalsefor them.onBranchkeeps those whose part is on the active branch, with its position (0 forSTART, on every branch). -
restore(files, id, branch, current, currentCursor, restorer):GONE(no such checkpoint, or the file is unreadable or does not name the part),NOT_ON_BRANCH, else it storescurrentas aRESTOREcheckpoint (so the return can be undone), hands the stored state to the tracker'sRestorerand returnsRESTORED. The tracker holds its lock of the book around it. The overload withoutcurrentkeeps nothing.
CheckpointFiles is where they live: ResourceCheckpointFiles keeps them as resources of the current user linked to the book (ResourceService.link
with Manuscript and the book's id, listed with findPageOfGroupForUser). Nothing in the book names them, so a book save stays small and
BackupResources.collect (which follows the uuids in the book's JSON) never puts them into a backup or copy; the user sees them in Resources.
Tests use an in-memory implementation.
The tracker side: the commits of its runs copy the state right after loading it (before(state): its JSON and cursor, a Before record) and call
take with that copy after the save, when the counts are known, inside the lock (the Knowledge Tracker in ExtractionService.commit/commitTidy
and in KnowledgeProcessService.process when the book reads without the model; the State Tracker in StateExtractionService.commit/commitTidy).
The restore reads the checkpoint's state, lets it keep the current setup (KnowledgeState.keepSetupOf: settings and start point; StateBook.keepSetupOf:
also the categories, adding back those the values use, and the settings of the clock while the marks come from the checkpoint), aligns it with the
branch and saves it (KnowledgeProcessService.restore, StateProcessService.restore).
A run with the model
sequenceDiagram
participant V as the plugin's view
participant R as TrackerRuns / TrackerRun
participant S as the plugin's Steps
participant M as ModelCall
V->>R: start(book, guided, ...)
loop N parts per request, until nothing waits
R->>S: propose(N, live)
S->>M: ask(system, user)
M-->>S: answer + reasoning
S-->>R: Proposal (checked changes)
alt guided and there are changes
R-->>V: phase REVIEW (ProgressPanel shows "Review changes")
V->>R: apply() or cancel()
end
R->>S: commit(proposal)
end
R->>S: tidyDue? proposeTidy / commitTidyThe plugin's Steps is the only place that knows its state: the Knowledge Tracker's KnowledgeSteps reads what the characters learned (or, without
the model, who has met whom), the State Tracker's StateSteps what changed in their state. Every step runs on the worker thread in the request context
of the user who started the run (or who applied the review).