{"version":133,"agent":"codex-capacity-chair","message":"Keep persistent publish membership indexed through conflict checks","time":1791842960035,"timeIso":"2026-10-12T22:09:20.035Z","changes":8,"change":1284,"files":["src/engine/state-source.ts","src/engine/test/stable-set-algebra.test.ts","worker/project/check-clash.ts","worker/project/express-moves.ts","worker/project/news.ts","worker/project/project.ts","worker/project/publish.ts","worker/project/test/project-writer-storage.test.ts"],"draft":null,"diff":[{"path":"src/engine/state-source.ts","added":34,"removed":0,"diff":"--- a/src/engine/state-source.ts\n+++ b/src/engine/state-source.ts\n@@ -139,6 +139,40 @@\n \t}\n }\n \n+/** Immutable indexed subtraction retains neither operand's enumerated values. */\n+export class StableDifferenceSet<T> extends StableSet<T> {\n+\treadonly left: StableSet<T>;\n+\treadonly right: StableSet<T>;\n+\tconstructor(left: StableSet<T>, right: StableSet<T>) {\n+\t\tsuper();\n+\t\tthis.left = left;\n+\t\tthis.right = right;\n+\t}\n+\tget retainedBytes(): number {\n+\t\treturn 32 + this.left.retainedBytes + this.right.retainedBytes;\n+\t}\n+\thas(value: T): boolean {\n+\t\treturn this.left.has(value) && !this.right.has(value);\n+\t}\n+\t*values(): SetIterator<T> {\n+\t\tfor (const value of this.left) if (!this.right.has(value)) yield value;\n+\t}\n+}\n+\n+/** Preserve indexed history; only the caller's operation-sized overlay is copied. */\n+export function unionOverlay<T>(base: ReadonlySet<T>, overlay: Iterable<T>): ReadonlySet<T> {\n+\treturn base instanceof StableSet\n+\t\t? new StableUnionSet(base, new StableOverlaySet(overlay))\n+\t\t: new Set([...base, ...overlay]);\n+}\n+\n+/** Both indexed operands must be immutable for the lifetime of the result. */\n+export function differenceSet<T>(left: ReadonlySet<T>, right: ReadonlySet<T>): ReadonlySet<T> {\n+\treturn left instanceof StableSet && right instanceof StableSet\n+\t\t? new StableDifferenceSet(left, right)\n+\t\t: new Set([...left].filter((value) => !right.has(value)));\n+}\n+\n /** An immutable snapshot for the operation's small overlay, not its history. */\n export class StableOverlaySet<T> extends StableSet<T> {\n \treadonly #values: Set<T>;"},{"path":"src/engine/test/stable-set-algebra.test.ts","added":37,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/stable-set-algebra.test.ts\n@@ -0,0 +1,37 @@\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { differenceSet, StableOverlaySet, StableSet, unionOverlay } from \"../state-source.ts\";\n+\n+test(\"indexed union and subtraction preserve order without enumerating history at construction or lookup\", () => {\n+\tclass Range extends StableSet<number> {\n+\t\tretainedBytes = 32;\n+\t\tvisited = 0;\n+\t\thas(value: number): boolean {\n+\t\t\treturn Number.isInteger(value) && value >= 0 && value < 100000;\n+\t\t}\n+\t\t*values(): SetIterator<number> {\n+\t\t\tfor (let n = 0; n < 100000; n++) {\n+\t\t\t\tthis.visited++;\n+\t\t\t\tyield n;\n+\t\t\t}\n+\t\t}\n+\t}\n+\tconst history = new Range();\n+\tconst overlay = new Set([99999, 100000]);\n+\tconst union = unionOverlay(history, overlay);\n+\tconst result = differenceSet(union, new StableOverlaySet([0, 1, 99999]));\n+\toverlay.add(100001);\n+\tassert.ok(result instanceof StableSet);\n+\tassert.equal(result.has(100000), true);\n+\tassert.equal(result.has(99999), false);\n+\tassert.equal(result.has(100001), false, \"the operation overlay is an immutable snapshot\");\n+\tassert.equal(history.visited, 0);\n+\tconst iterator = result.values();\n+\tassert.equal(iterator.next().value, 2);\n+\tassert.equal(history.visited, 3, \"the first value does not materialize the remaining history\");\n+\titerator.return?.();\n+\tassert.ok(result.retainedBytes < 512);\n+\tassert.deepEqual([...differenceSet(new StableOverlaySet([3, 1, 2]), new StableOverlaySet([1]))], [3, 2]);\n+\tassert.deepEqual([...unionOverlay(new StableOverlaySet([3, 1]), [1, 2])], [3, 1, 2]);\n+\tassert.deepEqual([...differenceSet(new Set([3, 1, 2]), new Set([1]))], [3, 2]);\n+});"},{"path":"worker/project/check-clash.ts","added":6,"removed":2,"diff":"--- a/worker/project/check-clash.ts\n+++ b/worker/project/check-clash.ts\n@@ -2,6 +2,7 @@\n // CheckClashHost; today the host is Project.\n \n import type { AtomId, View } from \"../../src/engine/index.ts\";\n+import { unionOverlay } from \"../../src/engine/state-source.ts\";\n import { ProjectError } from \"./project-error.ts\";\n import { configProblem, PROJECT_FILES } from \"./project-helpers.ts\";\n import type { Space } from \"./spaces.ts\";\n@@ -10,6 +11,7 @@\n /** What the spelling clash check needs from a project. */\n export interface CheckClashHost {\n \tshared(): Space;\n+\tsetOf(space: Space): ReadonlySet<AtomId>;\n \tview(set: ReadonlySet<AtomId>): View;\n \tsharedView(): View;\n \ttouchedFiles(ids: Iterable<AtomId>, views: readonly View[]): Set<string>;\n@@ -29,8 +31,10 @@\n \tlet now: View;\n \ttry {\n \t\t// Publishes queued ahead (#39) land first, so they count as shared.\n-\t\tview = host.view(new Set([...host.shared().own, ...ahead, ...changes]));\n-\t\tnow = ahead.size === 0 ? host.sharedView() : host.view(new Set([...host.shared().own, ...ahead]));\n+\t\tconst base = host.setOf(host.shared());\n+\t\tconst queued = unionOverlay(base, ahead);\n+\t\tview = host.view(unionOverlay(queued, changes));\n+\t\tnow = ahead.size === 0 ? host.sharedView() : host.view(queued);\n \t} catch (e) {\n \t\tif ((e as { code?: string }).code === \"not-closed\") return; // stranded by a drop: the publish says so itself\n \t\tthrow e;"},{"path":"worker/project/express-moves.ts","added":2,"removed":1,"diff":"--- a/worker/project/express-moves.ts\n+++ b/worker/project/express-moves.ts\n@@ -3,6 +3,7 @@\n // atoms so the lines keep their ids. Writes go through ExpressMovesHost; today the host is Project.\n \n import type { AtomId, AttrSet, EditInput, Graph, LineId, MoveInput, View } from \"../../src/engine/index.ts\";\n+import { differenceSet, StableOverlaySet } from \"../../src/engine/state-source.ts\";\n import { PLAIN_LINE } from \"./collisions.ts\";\n import type { AgentState, Entry, Meta, Mode } from \"./project-types.ts\";\n \n@@ -58,7 +59,7 @@\n \tconst g = host.graph;\n \tconst mine = new Set(draft);\n \tconst after = host.agentView(agent);\n-\tconst before = host.view(new Set([...after.set].filter((id) => !mine.has(id))));\n+\tconst before = host.view(differenceSet(after.set, new StableOverlaySet(draft)));\n \tconst touched = new Set<string>();\n \tfor (const id of draft) for (const file of g.filesOf(id)) touched.add(file);\n "},{"path":"worker/project/news.ts","added":4,"removed":3,"diff":"--- a/worker/project/news.ts\n+++ b/worker/project/news.ts\n@@ -2,6 +2,7 @@\n // once, and who hears it after an edit, publish or land. Writes (the news entry) stay on Project.\n \n import { type AtomId, type Graph, isStartNode, parseKey, type View } from \"../../src/engine/index.ts\";\n+import { unionOverlay } from \"../../src/engine/state-source.ts\";\n import { type Origin, type Pair, pairsIn, touchedFiles } from \"./collisions.ts\";\n import { type DefinitionCollision, signature } from \"./definition-rule.ts\";\n import { definitionsIn } from \"./definitions.ts\";\n@@ -192,7 +193,7 @@\n \treadonly graph: Graph;\n \treadonly agents: ReadonlyMap<string, AgentState>;\n \tdraft(agent: AgentState): AtomId[];\n-\tnewerFor(agent: AgentState): Set<AtomId>;\n+\tnewerFor(agent: AgentState): ReadonlySet<AtomId>;\n \tspaceOf(agent: AgentState): Space;\n \tsetOf(space: Space): ReadonlySet<AtomId>;\n \tviewAt(space: Space, version: number): View;\n@@ -253,7 +254,7 @@\n \tif (draft.length === 0) return;\n \tconst newer = host.newerFor(agent);\n \tif (newer.size === 0) return;\n-\tconst lines = pairsIn(host.graph, draft, newer, new Set([...host.setOf(host.spaceOf(agent)), ...draft]));\n+\tconst lines = pairsIn(host.graph, draft, newer, unionOverlay(host.setOf(host.spaceOf(agent)), draft));\n \thost.queueNews(\n \t\tagent,\n \t\t[...lineNews(host, agent, lines), ...definitionNews(host.definitionCollisions(agent, newer))],\n@@ -281,7 +282,7 @@\n \t\tif (agent === publisher || agent.space !== space.name) continue;\n \t\tconst draft = host.draft(agent);\n \t\tif (draft.length === 0) continue;\n-\t\tconst lines = pairsIn(host.graph, draft, just, new Set([...host.setOf(space), ...draft]));\n+\t\tconst lines = pairsIn(host.graph, draft, just, unionOverlay(host.setOf(space), draft));\n \t\tconst defs = changedDefinitions\n \t\t\t? host.definitionCollisions(agent, host.newerFor(agent)).filter((d) => just.has(d.by))\n \t\t\t: [];"},{"path":"worker/project/project.ts","added":5,"removed":5,"diff":"--- a/worker/project/project.ts\n+++ b/worker/project/project.ts\n@@ -15,6 +15,7 @@\n import { type Atom, type AtomId, EngineError, Graph, type LineId, type View } from \"../../src/engine/index.ts\";\n import {\n \tCachedStateStorage,\n+\tdifferenceSet,\n \ttype GraphStateStorage,\n \tStableOverlaySet,\n \tStableSet,\n@@ -2184,6 +2185,7 @@\n \tget #checkClashHost(): CheckClashHost {\n \t\tthis.#checkClashes ??= {\n \t\t\tshared: () => this.#shared,\n+\t\t\tsetOf: (space) => this.#setOf(space),\n \t\t\tview: (set) => this.#view(set),\n \t\t\tsharedView: () => this.#sharedView(),\n \t\t\ttouchedFiles: (ids, views) => this.#touchedFiles(ids, views),\n@@ -2545,11 +2547,10 @@\n \t}\n \n \t/** The space's atoms published after the agent's base, by others. */\n-\t#newerFor(agent: AgentState): Set<AtomId> {\n+\t#newerFor(agent: AgentState): ReadonlySet<AtomId> {\n \t\tconst space = this.#spaceOf(agent);\n \t\tconst base = this.#at(space, agent.base);\n-\t\tconst mine = new Set(agent.mine);\n-\t\treturn new Set([...this.#setOf(space)].filter((id) => !base.has(id) && !mine.has(id)));\n+\t\treturn differenceSet(differenceSet(this.#setOf(space), base), new StableOverlaySet(agent.mine));\n \t}\n \n \t/** The files whose text in the agent's view differs from `before`, and its open collisions. */\n@@ -2687,8 +2688,7 @@\n \t\tconst draft = new Set(this.#draft(agent));\n \t\tif (draft.size === 0 || newer.size === 0) return [];\n \t\tconst space = this.#spaceOf(agent);\n-\t\tconst mine = new Set(agent.mine);\n-\t\tconst base = this.#view(new Set([...this.#at(space, agent.base)].filter((id) => !mine.has(id))));\n+\t\tconst base = this.#view(differenceSet(this.#at(space, agent.base), new StableOverlaySet(agent.mine)));\n \t\treturn this.#definitionRule.collisions(\n \t\t\tdraft,\n \t\t\tthis.#agentView(agent),"},{"path":"worker/project/publish.ts","added":4,"removed":3,"diff":"--- a/worker/project/publish.ts\n+++ b/worker/project/publish.ts\n@@ -2,6 +2,7 @@\n // the host is Project. Ingest calls publishDraft, the inner step.\n \n import { type AtomId, buildAtom, type Hunk, joinLines, type View } from \"../../src/engine/index.ts\";\n+import { differenceSet, unionOverlay } from \"../../src/engine/state-source.ts\";\n import type { Pair } from \"./collisions.ts\";\n import type { DefinitionCollision } from \"./definition-rule.ts\";\n import { keptOf, lostLines, settleLines } from \"./markers.ts\";\n@@ -59,7 +60,7 @@\n \t): PublishAnswer;\n \tview(set: ReadonlySet<AtomId>): View;\n \trequireClosed(tie: { deps: readonly AtomId[] }, union: ReadonlySet<AtomId>): void;\n-\tnewerFor(agent: AgentState): Set<AtomId>;\n+\tnewerFor(agent: AgentState): ReadonlySet<AtomId>;\n \tcurrentChange(agent: AgentState, time: number): number | undefined;\n \tapply(entry: Entry): void;\n \tnewsForOthers(publisher: AgentState, space: Space, version: number, entries: Entry[]): void;\n@@ -126,7 +127,7 @@\n \tconst draft = host.draft(agent);\n \tif (draft.length === 0) return { ok: true, version: last(space), changes: 0 };\n \tconst now = host.setOf(space);\n-\tconst union = new Set([...now, ...draft]);\n+\tconst union = unionOverlay(now, draft);\n \tconst newer = host.newerFor(agent);\n \tconst collisions = [\n \t\t...host.pairs(draft, newer, union).map((p) => host.collision(agent, p)),\n@@ -143,7 +144,7 @@\n \t// (#37: twenty goals twice, shared red for three versions). So\n \t// that is refused unless the agent says to publish as it is (asIs); then,\n \t// like any other seen collision, publishing settles it.\n-\tconst seen = host.pairs(draft, new Set([...now].filter((id) => !newer.has(id))), union);\n+\tconst seen = host.pairs(draft, differenceSet(now, newer), union);\n \tif (!asIs) {\n \t\tconst untouched = seen\n \t\t\t.map((p) => host.collision(agent, p))"},{"path":"worker/project/test/project-writer-storage.test.ts","added":24,"removed":0,"diff":"--- a/worker/project/test/project-writer-storage.test.ts\n+++ b/worker/project/test/project-writer-storage.test.ts\n@@ -1,5 +1,6 @@\n import assert from \"node:assert/strict\";\n import { test } from \"node:test\";\n+import { Graph } from \"../../../src/engine/graph.ts\";\n import { StableSet, storedArray } from \"../../../src/engine/state-source.ts\";\n import { SqlState } from \"../../../src/engine/test/storage-helper.ts\";\n import { MemoryStore, Project } from \"../project.ts\";\n@@ -31,6 +32,29 @@\n \t};\n }\n \n+test(\"persistent publish keeps membership indexed through union and conflict checks\", (t) => {\n+\tconst { storage, entries, store } = setup();\n+\ttry {\n+\t\tlet project = new Project(store, { writerStorage: storage });\n+\t\tstorage.transaction(() => project.init({ \"a.txt\": \"one\\n\", \"b.txt\": \"two\\n\" }, 0));\n+\t\tstorage.transaction(() => project.record(\"writer\", { \"a.txt\": \"ONE\\n\" }, 1));\n+\t\tstorage.transaction(() => project.record(\"other\", { \"b.txt\": \"TWO\\n\" }, 2));\n+\t\tstorage.transaction(() => project.publish(\"other\", \"Other file\", 3));\n+\t\tproject = Project.openWriter(store, storage, entries.length);\n+\t\tt.mock.method(Graph.prototype, \"closureOf\", () => {\n+\t\t\tthrow new Error(\"materialized membership closure is forbidden\");\n+\t\t});\n+\t\tconst result = storage.transaction(() => project.publish(\"writer\", \"Merge without history copies\", 4));\n+\t\tassert.equal(result.ok, true);\n+\t\tassert.deepEqual(Project.openWriter(store, storage, entries.length).files(), {\n+\t\t\t\"a.txt\": \"ONE\\n\",\n+\t\t\t\"b.txt\": \"TWO\\n\",\n+\t\t});\n+\t} finally {\n+\t\tstorage.db.close();\n+\t}\n+});\n+\n test(\"actor fence reads neither enumerate other agents nor project their files\", (t) => {\n \tconst { storage, entries, store } = setup();\n \tlet project = new Project(store, { writerStorage: storage });"}],"ci":null}