Closes M4. The review's three findings are fixed, the durability rule the specification always implied is now enforced, and M3's and M4's browser behaviour has been verified in a real browser for the first time. B-1 -- the Save Point list was an N+1 that loaded whole Action rows, narration included, to answer "does a row exist here". It is now one bulk two-column coordinate query plus one lineage: 53 SELECTs for 25 Save Points became 5, and the count no longer grows with the list. The clause is an OR of exact (branch, depth) pairs rather than two IN lists, because the cross product would report a Save Point resolved on the strength of another one's depth existing on this one's branch. A test builds exactly that trap. B-2 -- reclassified during closeout from "missing warning" to a behaviour defect, and fixed as one. STORY-BRANCH-SEMANTICS §19 says a named checkpoint remains until explicitly deleted, and §28 already required future cleanup to retain checkpoint-referenced paths; a cascade that silently removed Save Points with a branch violated both, and a warning would only have documented the violation. A branch a Save Point names can no longer be deleted. The request is refused with the offending Save Points named, the user deletes them explicitly -- which deletes no story -- and the branch then goes. The scope is the subtree, because deleting a branch takes its descendants. Both delete controls disable and explain. Recorded as a new §19.1; models.py, TECHNICAL-DESIGN §8.8 and DATA-MODEL §8 had all recorded the cascade as the rule and now record the refusal. An earlier pass in this same closeout had kept the cascade and added a warning. That was the wrong fix and its tests were replaced rather than left standing, since they pinned the defect. B-3 -- the D11/L03 automation never left one process, so it could not distinguish durable state from a live Python object. It now spawns real server processes, kills the first, and reads the campaign back with the second. C-5 -- creating a Save Point takes the campaign's turn lock. "Save where I am" has to name one committed position, and the head is what a turn in flight is about to move. Rename and Delete deliberately do not take it. The architecture is untouched: a Save Point is still name + note + (branch, depth), and restore is still coordinate -> head.move_to_node -> head.move_to -> attempts.restore_state. No second restore path, no state copied into a checkpoint, no fork on restore. Browser verification -- the first in this project, and it covers both milestones. Firefox 154.0.1 through geckodriver over the W3C WebDriver protocol, driving the rendered DOM: 47/47 checks, twice, on independent databases, no console errors. M3's Undo/Redo enable states, transcript movement, Retry and the take pager, divergence retiring Redo; M4's whole Save Point lifecycle, both confirmations, and the new branch-delete refusal including its recovery. No dependency was added: the WebDriver client is stdlib HTTP. No application defect was found by the browser. Four failures occurred, all in the harness -- a wrong SPA route, a wait comparing transcript length when the empty-story placeholder is longer than the first turn, a fixture deleting the branch it was reading, and a reload assertion that sampled once instead of waiting. The last was checked against the app before being called a harness bug. Tests: 698 backend pass (was 680), 60 M4, 94 M3 history, 66 export/ migrations, 93 security/local-only. Frontend lint and build clean, Docker build clean, loopback binding unchanged. No assertion weakened, no skip added. Planning: STORY-BRANCH-SEMANTICS §19.1 is the only behavioural change and it strengthens §19. V1-ACCEPTANCE-TESTS records D11-D14, I04, L03 and the E-series, keeping automated, live-runtime and browser evidence distinct, and weakens no pass condition. DATA-MODEL records the coordinate with the retry measurement that settles it. BROWSER-UX-SPEC rules for Moment over Turn. BUILD-MILESTONES marks M4 COMPLETE, closes M3's browser condition, and lists what M5 inherits. VERSION adds v2.6. No new ADR: ADR 005 already decides that history is preserved rather than overwritten, and §19.1 is that decision applied to checkpoint-referenced history. M4 is closed. M5 may now be briefed; it has not been started. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PWU4gTfLYY6Qq9U7aa9Qw2
152 lines
6.4 KiB
JavaScript
152 lines
6.4 KiB
JavaScript
// The story tree, as numbers something can be drawn from.
|
|
//
|
|
// `GET /adventures/{id}/branches` answers two numbers per branch — `fork_depth`,
|
|
// where a line leaves its parent, and `depth`, where it currently ends — which
|
|
// is deliberately enough to draw the whole shape without walking a single node.
|
|
// Both readers of that shape live behind this file: the list in the Branches
|
|
// panel and the map overlay. They order and label a branch the same way because
|
|
// they order and label it *here*, so a branch cannot appear in one and not the
|
|
// other, or be called two different things by the two of them.
|
|
|
|
// Derived, never stored: a generated name in the column would go stale the
|
|
// moment a branch before it is deleted. A fork depth is a coordinate, so it
|
|
// says the same thing whatever else is thrown away.
|
|
export function branchLabel(branch) {
|
|
if (branch.name) return branch.name
|
|
if (branch.parent_branch_id === null) return 'The first telling'
|
|
return `Fork at moment ${branch.fork_depth + 1}`
|
|
}
|
|
|
|
// Parents before children, each child under the branch it left.
|
|
export function orderBranches(branches) {
|
|
const kids = new Map()
|
|
for (const b of branches) {
|
|
const key = b.parent_branch_id
|
|
if (!kids.has(key)) kids.set(key, [])
|
|
kids.get(key).push(b)
|
|
}
|
|
const out = []
|
|
const walk = (parentId, indent) => {
|
|
for (const b of kids.get(parentId) || []) {
|
|
out.push({ branch: b, indent })
|
|
walk(b.id, indent + 1)
|
|
}
|
|
}
|
|
// Anything whose parent is missing would otherwise never be walked. That
|
|
// cannot happen through the API, but a list that silently drops a branch is
|
|
// the one bug this panel exists to make impossible to have.
|
|
walk(null, 0)
|
|
const seen = new Set(out.map((row) => row.branch.id))
|
|
for (const b of branches) if (!seen.has(b.id)) out.push({ branch: b, indent: 0 })
|
|
return out
|
|
}
|
|
|
|
// The branches the head is standing on: itself, and everything it borrows from.
|
|
//
|
|
// This is the client's copy of the server's delete rule — `parent_branch_id`
|
|
// cascades, so deleting an ancestor of the head takes the head with it and
|
|
// leaves `head_branch_id` pointing at a row that is gone. The server refuses
|
|
// exactly this set; computing it here only means the button can say so before
|
|
// it is pressed. The server stays the authority.
|
|
export function headLineage(branches) {
|
|
const byId = new Map(branches.map((b) => [b.id, b]))
|
|
const out = new Set()
|
|
let cur = branches.find((b) => b.is_head)
|
|
// The guard is against a cycle, which the schema forbids and a walk should
|
|
// still never hang on.
|
|
while (cur && !out.has(cur.id)) {
|
|
out.add(cur.id)
|
|
cur = cur.parent_branch_id === null ? null : byId.get(cur.parent_branch_id)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// How many Save Points would go if this branch were deleted.
|
|
//
|
|
// Deleting a branch deletes everything forked from it, and a Save Point names a
|
|
// position on a line, so the Save Points on the whole doomed subtree go too.
|
|
// The count is summed over that subtree rather than over the one branch — a
|
|
// warning that said "1 Save Point" while three disappeared would be worse than
|
|
// no warning at all.
|
|
//
|
|
// Computed here because the panel already holds every branch and its parent,
|
|
// so the answer costs a walk rather than an endpoint. The server remains the
|
|
// authority on what is actually deleted; this only lets the button say so
|
|
// before it is pressed, the same division `headLineage` already uses.
|
|
export function savePointsUnder(branches, rootId) {
|
|
const children = new Map()
|
|
for (const b of branches) {
|
|
const key = b.parent_branch_id
|
|
if (!children.has(key)) children.set(key, [])
|
|
children.get(key).push(b)
|
|
}
|
|
const byId = new Map(branches.map((b) => [b.id, b]))
|
|
const seen = new Set()
|
|
const stack = [rootId]
|
|
let total = 0
|
|
while (stack.length) {
|
|
const id = stack.pop()
|
|
// The guard is against a cycle, which the schema forbids and a walk should
|
|
// still never hang on.
|
|
if (seen.has(id)) continue
|
|
seen.add(id)
|
|
total += byId.get(id)?.save_points ?? 0
|
|
for (const child of children.get(id) ?? []) stack.push(child.id)
|
|
}
|
|
return total
|
|
}
|
|
|
|
// ---------- Map geometry ----------
|
|
|
|
export const ROW_H = 64 // one branch, name above the lane and meta below
|
|
export const PAD = { top: 44, right: 26, bottom: 20, left: 24 }
|
|
export const CORNER = 11 // radius of the elbow a fork turns through
|
|
|
|
// Place every branch on its own horizontal lane, in tree order.
|
|
//
|
|
// A lane runs from where its branch left its parent to where its branch ends,
|
|
// so the horizontal axis is the story's own clock: two branches at the same x
|
|
// are at the same moment, and the length of a lane is how much of the story it
|
|
// covers. Nothing here is measured in pixels — the component owns the mapping
|
|
// from a moment to an x, because only it knows how wide it ended up.
|
|
export function layoutTree(branches) {
|
|
const rows = orderBranches(branches)
|
|
const rowOf = new Map(rows.map((row, i) => [row.branch.id, i]))
|
|
const lanes = rows.map(({ branch }, row) => ({
|
|
branch,
|
|
row,
|
|
// The first telling starts where the story does; every other line starts
|
|
// where it walked away from another one.
|
|
from: branch.parent_branch_id === null ? 0 : (branch.fork_depth ?? 0),
|
|
to: branch.depth,
|
|
parentRow: branch.parent_branch_id === null
|
|
? null
|
|
: (rowOf.has(branch.parent_branch_id) ? rowOf.get(branch.parent_branch_id) : null),
|
|
}))
|
|
return {
|
|
lanes,
|
|
// The whole map is scaled to the longest path, so a short branch reads as
|
|
// short. `|| 1` keeps a one-moment story from dividing by zero.
|
|
maxDepth: Math.max(0, ...branches.map((b) => b.depth)),
|
|
height: PAD.top + rows.length * ROW_H + PAD.bottom,
|
|
}
|
|
}
|
|
|
|
// Round tick marks for the moment axis: about `count` of them, landing on
|
|
// numbers a person would have chosen (1, 2, 5, 10, 25, 50 …) rather than on
|
|
// whatever `maxDepth / 6` happens to be.
|
|
export function momentTicks(maxDepth, count = 6) {
|
|
if (maxDepth <= 0) return [0]
|
|
const raw = maxDepth / count
|
|
const magnitude = 10 ** Math.floor(Math.log10(raw))
|
|
const step = [1, 2, 2.5, 5, 10].map((m) => m * magnitude).find((s) => s >= raw) || magnitude * 10
|
|
const out = []
|
|
for (let d = 0; d <= maxDepth; d += step) out.push(Math.round(d))
|
|
// The last moment is worth naming, but not on top of the tick before it —
|
|
// at a narrow width `26` and `27` printed as `2627`.
|
|
const last = out[out.length - 1]
|
|
if (maxDepth - last > step * 0.6) out.push(maxDepth)
|
|
else if (last !== maxDepth) out[out.length - 1] = maxDepth
|
|
return out
|
|
}
|