Keep persistent publish membership indexed through conflict checks
of shared·codex-capacity-chair··Not checked
Version 132 · Files at version 133
8 files, 116 lines added, 14 removed.
--- a/src/engine/state-source.ts
+++ b/src/engine/state-source.ts
@@ -139,6 +139,40 @@
142+/** Immutable indexed subtraction retains neither operand's enumerated values. */ 143+export class StableDifferenceSet<T> extends StableSet<T> { 144+ readonly left: StableSet<T>; 145+ readonly right: StableSet<T>; 146+ constructor(left: StableSet<T>, right: StableSet<T>) { 151+ get retainedBytes(): number { 152+ return 32 + this.left.retainedBytes + this.right.retainedBytes; 154+ has(value: T): boolean { 155+ return this.left.has(value) && !this.right.has(value); 157+ *values(): SetIterator<T> { 158+ for (const value of this.left) if (!this.right.has(value)) yield value; 162+/** Preserve indexed history; only the caller's operation-sized overlay is copied. */ 163+export function unionOverlay<T>(base: ReadonlySet<T>, overlay: Iterable<T>): ReadonlySet<T> { 164+ return base instanceof StableSet 165+ ? new StableUnionSet(base, new StableOverlaySet(overlay)) 166+ : new Set([...base, ...overlay]); 169+/** Both indexed operands must be immutable for the lifetime of the result. */ 170+export function differenceSet<T>(left: ReadonlySet<T>, right: ReadonlySet<T>): ReadonlySet<T> { 171+ return left instanceof StableSet && right instanceof StableSet 172+ ? new StableDifferenceSet(left, right) 173+ : new Set([...left].filter((value) => !right.has(value))); 142176/** An immutable snapshot for the operation's small overlay, not its history. */ 143177export class StableOverlaySet<T> extends StableSet<T> { 144178 readonly #values: Set<T>; --- /dev/null
+++ b/src/engine/test/stable-set-algebra.test.ts
@@ -0,0 +1,37 @@
1+import assert from "node:assert/strict"; 2+import { test } from "node:test"; 3+import { differenceSet, StableOverlaySet, StableSet, unionOverlay } from "../state-source.ts"; 5+test("indexed union and subtraction preserve order without enumerating history at construction or lookup", () => { 6+ class Range extends StableSet<number> { 9+ has(value: number): boolean { 10+ return Number.isInteger(value) && value >= 0 && value < 100000; 12+ *values(): SetIterator<number> { 13+ for (let n = 0; n < 100000; n++) { 19+ const history = new Range(); 20+ const overlay = new Set([99999, 100000]); 21+ const union = unionOverlay(history, overlay); 22+ const result = differenceSet(union, new StableOverlaySet([0, 1, 99999])); 24+ assert.ok(result instanceof StableSet); 25+ assert.equal(result.has(100000), true); 26+ assert.equal(result.has(99999), false); 27+ assert.equal(result.has(100001), false, "the operation overlay is an immutable snapshot"); 28+ assert.equal(history.visited, 0); 29+ const iterator = result.values(); 30+ assert.equal(iterator.next().value, 2); 31+ assert.equal(history.visited, 3, "the first value does not materialize the remaining history"); 33+ assert.ok(result.retainedBytes < 512); 34+ assert.deepEqual([...differenceSet(new StableOverlaySet([3, 1, 2]), new StableOverlaySet([1]))], [3, 2]); 35+ assert.deepEqual([...unionOverlay(new StableOverlaySet([3, 1]), [1, 2])], [3, 1, 2]); 36+ assert.deepEqual([...differenceSet(new Set([3, 1, 2]), new Set([1]))], [3, 2]); --- a/worker/project/check-clash.ts
+++ b/worker/project/check-clash.ts
@@ -2,6 +2,7 @@
22// CheckClashHost; today the host is Project. 44import type { AtomId, View } from "../../src/engine/index.ts"; 5+import { unionOverlay } from "../../src/engine/state-source.ts"; 56import { ProjectError } from "./project-error.ts"; 67import { configProblem, PROJECT_FILES } from "./project-helpers.ts"; 78import type { Space } from "./spaces.ts"; @@ -10,6 +11,7 @@
1011/** What the spelling clash check needs from a project. */ 1112export interface CheckClashHost { 14+ setOf(space: Space): ReadonlySet<AtomId>; 1315 view(set: ReadonlySet<AtomId>): View; 1517 touchedFiles(ids: Iterable<AtomId>, views: readonly View[]): Set<string>; @@ -29,8 +31,10 @@
3133 // Publishes queued ahead (#39) land first, so they count as shared. 32− view = host.view(new Set([...host.shared().own, ...ahead, ...changes]));
33− now = ahead.size === 0 ? host.sharedView() : host.view(new Set([...host.shared().own, ...ahead]));
34+ const base = host.setOf(host.shared()); 35+ const queued = unionOverlay(base, ahead); 36+ view = host.view(unionOverlay(queued, changes)); 37+ now = ahead.size === 0 ? host.sharedView() : host.view(queued); 3539 if ((e as { code?: string }).code === "not-closed") return; // stranded by a drop: the publish says so itself --- a/worker/project/express-moves.ts
+++ b/worker/project/express-moves.ts
@@ -3,6 +3,7 @@
33// atoms so the lines keep their ids. Writes go through ExpressMovesHost; today the host is Project. 55import type { AtomId, AttrSet, EditInput, Graph, LineId, MoveInput, View } from "../../src/engine/index.ts"; 6+import { differenceSet, StableOverlaySet } from "../../src/engine/state-source.ts"; 67import { PLAIN_LINE } from "./collisions.ts"; 78import type { AgentState, Entry, Meta, Mode } from "./project-types.ts"; @@ -58,7 +59,7 @@
5859 const g = host.graph; 5960 const mine = new Set(draft); 6061 const after = host.agentView(agent); 61− const before = host.view(new Set([...after.set].filter((id) => !mine.has(id))));
62+ const before = host.view(differenceSet(after.set, new StableOverlaySet(draft))); 6263 const touched = new Set<string>(); 6364 for (const id of draft) for (const file of g.filesOf(id)) touched.add(file); --- a/worker/project/news.ts
+++ b/worker/project/news.ts
@@ -2,6 +2,7 @@
22// once, and who hears it after an edit, publish or land. Writes (the news entry) stay on Project. 44import { type AtomId, type Graph, isStartNode, parseKey, type View } from "../../src/engine/index.ts"; 5+import { unionOverlay } from "../../src/engine/state-source.ts"; 56import { type Origin, type Pair, pairsIn, touchedFiles } from "./collisions.ts"; 67import { type DefinitionCollision, signature } from "./definition-rule.ts"; 78import { definitionsIn } from "./definitions.ts"; @@ -192,7 +193,7 @@
192193 readonly graph: Graph; 193194 readonly agents: ReadonlyMap<string, AgentState>; 194195 draft(agent: AgentState): AtomId[]; 195− newerFor(agent: AgentState): Set<AtomId>;
196+ newerFor(agent: AgentState): ReadonlySet<AtomId>; 196197 spaceOf(agent: AgentState): Space; 197198 setOf(space: Space): ReadonlySet<AtomId>; 198199 viewAt(space: Space, version: number): View; @@ -253,7 +254,7 @@
253254 if (draft.length === 0) return; 254255 const newer = host.newerFor(agent); 255256 if (newer.size === 0) return; 256− const lines = pairsIn(host.graph, draft, newer, new Set([...host.setOf(host.spaceOf(agent)), ...draft]));
257+ const lines = pairsIn(host.graph, draft, newer, unionOverlay(host.setOf(host.spaceOf(agent)), draft)); 259260 [...lineNews(host, agent, lines), ...definitionNews(host.definitionCollisions(agent, newer))], @@ -281,7 +282,7 @@
281282 if (agent === publisher || agent.space !== space.name) continue; 282283 const draft = host.draft(agent); 283284 if (draft.length === 0) continue; 284− const lines = pairsIn(host.graph, draft, just, new Set([...host.setOf(space), ...draft]));
285+ const lines = pairsIn(host.graph, draft, just, unionOverlay(host.setOf(space), draft)); 285286 const defs = changedDefinitions 286287 ? host.definitionCollisions(agent, host.newerFor(agent)).filter((d) => just.has(d.by)) --- a/worker/project/project.ts
+++ b/worker/project/project.ts
@@ -15,6 +15,7 @@
1515import { type Atom, type AtomId, EngineError, Graph, type LineId, type View } from "../../src/engine/index.ts"; 1819 type GraphStateStorage, @@ -2184,6 +2185,7 @@
21842185 get #checkClashHost(): CheckClashHost { 21852186 this.#checkClashes ??= { 21862187 shared: () => this.#shared, 2188+ setOf: (space) => this.#setOf(space), 21872189 view: (set) => this.#view(set), 21882190 sharedView: () => this.#sharedView(), 21892191 touchedFiles: (ids, views) => this.#touchedFiles(ids, views), @@ -2545,11 +2547,10 @@
25472549 /** The space's atoms published after the agent's base, by others. */ 2548− #newerFor(agent: AgentState): Set<AtomId> {
2550+ #newerFor(agent: AgentState): ReadonlySet<AtomId> { 25492551 const space = this.#spaceOf(agent); 25502552 const base = this.#at(space, agent.base); 2551− const mine = new Set(agent.mine);
2552− return new Set([...this.#setOf(space)].filter((id) => !base.has(id) && !mine.has(id)));
2553+ return differenceSet(differenceSet(this.#setOf(space), base), new StableOverlaySet(agent.mine)); 25552556 /** The files whose text in the agent's view differs from `before`, and its open collisions. */ @@ -2687,8 +2688,7 @@
26872688 const draft = new Set(this.#draft(agent)); 26882689 if (draft.size === 0 || newer.size === 0) return []; 26892690 const space = this.#spaceOf(agent); 2690− const mine = new Set(agent.mine);
2691− const base = this.#view(new Set([...this.#at(space, agent.base)].filter((id) => !mine.has(id))));
2691+ const base = this.#view(differenceSet(this.#at(space, agent.base), new StableOverlaySet(agent.mine))); 26922692 return this.#definitionRule.collisions( 26942694 this.#agentView(agent), --- a/worker/project/publish.ts
+++ b/worker/project/publish.ts
@@ -2,6 +2,7 @@
22// the host is Project. Ingest calls publishDraft, the inner step. 44import { type AtomId, buildAtom, type Hunk, joinLines, type View } from "../../src/engine/index.ts"; 5+import { differenceSet, unionOverlay } from "../../src/engine/state-source.ts"; 56import type { Pair } from "./collisions.ts"; 67import type { DefinitionCollision } from "./definition-rule.ts"; 78import { keptOf, lostLines, settleLines } from "./markers.ts"; @@ -59,7 +60,7 @@
6061 view(set: ReadonlySet<AtomId>): View; 6162 requireClosed(tie: { deps: readonly AtomId[] }, union: ReadonlySet<AtomId>): void; 62− newerFor(agent: AgentState): Set<AtomId>;
63+ newerFor(agent: AgentState): ReadonlySet<AtomId>; 6364 currentChange(agent: AgentState, time: number): number | undefined; 6465 apply(entry: Entry): void; 6566 newsForOthers(publisher: AgentState, space: Space, version: number, entries: Entry[]): void; @@ -126,7 +127,7 @@
126127 const draft = host.draft(agent); 127128 if (draft.length === 0) return { ok: true, version: last(space), changes: 0 }; 128129 const now = host.setOf(space); 129− const union = new Set([...now, ...draft]);
130+ const union = unionOverlay(now, draft); 130131 const newer = host.newerFor(agent); 131132 const collisions = [ 132133 ...host.pairs(draft, newer, union).map((p) => host.collision(agent, p)), @@ -143,7 +144,7 @@
143144 // (#37: twenty goals twice, shared red for three versions). So 144145 // that is refused unless the agent says to publish as it is (asIs); then, 145146 // like any other seen collision, publishing settles it. 146− const seen = host.pairs(draft, new Set([...now].filter((id) => !newer.has(id))), union);
147+ const seen = host.pairs(draft, differenceSet(now, newer), union); 148149 const untouched = seen 149150 .map((p) => host.collision(agent, p)) --- a/worker/project/test/project-writer-storage.test.ts
+++ b/worker/project/test/project-writer-storage.test.ts
@@ -1,5 +1,6 @@
11import assert from "node:assert/strict"; 22import { test } from "node:test"; 3+import { Graph } from "../../../src/engine/graph.ts"; 34import { StableSet, storedArray } from "../../../src/engine/state-source.ts"; 45import { SqlState } from "../../../src/engine/test/storage-helper.ts"; 56import { MemoryStore, Project } from "../project.ts"; @@ -31,6 +32,29 @@
35+test("persistent publish keeps membership indexed through union and conflict checks", (t) => { 36+ const { storage, entries, store } = setup(); 38+ let project = new Project(store, { writerStorage: storage }); 39+ storage.transaction(() => project.init({ "a.txt": "one\n", "b.txt": "two\n" }, 0)); 40+ storage.transaction(() => project.record("writer", { "a.txt": "ONE\n" }, 1)); 41+ storage.transaction(() => project.record("other", { "b.txt": "TWO\n" }, 2)); 42+ storage.transaction(() => project.publish("other", "Other file", 3)); 43+ project = Project.openWriter(store, storage, entries.length); 44+ t.mock.method(Graph.prototype, "closureOf", () => { 45+ throw new Error("materialized membership closure is forbidden"); 47+ const result = storage.transaction(() => project.publish("writer", "Merge without history copies", 4)); 48+ assert.equal(result.ok, true); 49+ assert.deepEqual(Project.openWriter(store, storage, entries.length).files(), { 3458test("actor fence reads neither enumerate other agents nor project their files", (t) => { 3559 const { storage, entries, store } = setup(); 3660 let project = new Project(store, { writerStorage: storage });