@oisin-ee/pi-dogsled 0.34.1 → 0.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -4
- package/package.json +1 -1
- package/pi/activity.ts +159 -36
- package/pi/observation.ts +15 -2
- package/pi/todo/README.md +16 -2
- package/pi/todo/src/branch-state.ts +223 -0
- package/pi/todo/src/delta-fixture.ts +141 -0
- package/pi/todo/src/delta-review-fixture.ts +63 -0
- package/pi/todo/src/delta-sequence-fixture.ts +59 -0
- package/pi/todo/src/delta-validation.ts +78 -0
- package/pi/todo/src/delta-worker-fixture.ts +102 -0
- package/pi/todo/src/delta.ts +152 -0
- package/pi/todo/src/generated/todo_core.d.ts +9 -0
- package/pi/todo/src/generated/todo_core.js +24 -0
- package/pi/todo/src/generated/todo_core_bg.wasm +0 -0
- package/pi/todo/src/generated/todo_core_bg.wasm.d.ts +1 -0
- package/pi/todo/src/history.ts +54 -0
- package/pi/todo/src/old-lifecycle-fixture.ts +106 -0
- package/pi/todo/src/old-provider-fixture.ts +290 -0
- package/pi/todo/src/old-reminder-fixture.ts +31 -0
- package/pi/todo/src/old-state-fixture.ts +106 -0
- package/pi/todo/src/old-tool-fixture.ts +61 -0
- package/pi/todo/src/order-index.ts +109 -0
- package/pi/todo/src/provenance.ts +54 -0
- package/pi/todo/src/provider.ts +34 -33
- package/pi/todo/src/reminder.ts +67 -17
- package/pi/todo/src/seal-reference-fixture.ts +59 -0
- package/pi/todo/src/snapshot-reference-fixture.ts +88 -0
- package/pi/todo/src/state.ts +88 -92
- package/pi/todo/src/tool.ts +4 -3
- package/pi/todo/src/wasm.ts +1 -0
- package/plugins/dogsled/skills/dogsled/references/commands.md +18 -0
- package/plugins/dogsled/skills/dogsled/references/land.md +1 -1
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { DELTA_ENTRY, STATE_ENTRY, indexed, leafTask } from "./delta.ts";
|
|
2
|
+
import type { Immutable, ListIndex, TodoDelta } from "./delta.ts";
|
|
3
|
+
import type { Subtask, TodoList } from "./wasm.ts";
|
|
4
|
+
import { deltaData, snapshotData } from "./delta-validation.ts";
|
|
5
|
+
import { History } from "./history.ts";
|
|
6
|
+
import { OrderIndex } from "./order-index.ts";
|
|
7
|
+
import { Provenance } from "./provenance.ts";
|
|
8
|
+
import { parse } from "./wasm.ts";
|
|
9
|
+
|
|
10
|
+
interface BranchEntry {
|
|
11
|
+
readonly type: string;
|
|
12
|
+
readonly customType?: string;
|
|
13
|
+
readonly data?: unknown;
|
|
14
|
+
readonly id?: string;
|
|
15
|
+
}
|
|
16
|
+
interface ListFrame {
|
|
17
|
+
readonly changed: readonly Immutable<Subtask>[];
|
|
18
|
+
readonly entry: BranchEntry;
|
|
19
|
+
readonly fenced: boolean;
|
|
20
|
+
readonly progress: boolean;
|
|
21
|
+
readonly list: TodoList;
|
|
22
|
+
readonly snapshot: TodoList;
|
|
23
|
+
}
|
|
24
|
+
interface TaskRecord {
|
|
25
|
+
readonly revision: number;
|
|
26
|
+
readonly task: Immutable<Subtask>;
|
|
27
|
+
}
|
|
28
|
+
interface ListVersion {
|
|
29
|
+
readonly hasBase: boolean;
|
|
30
|
+
readonly orders: Readonly<OrderIndex>;
|
|
31
|
+
readonly revision: number;
|
|
32
|
+
readonly size: number;
|
|
33
|
+
readonly tasks: Readonly<History<string, TaskRecord | undefined>>;
|
|
34
|
+
}
|
|
35
|
+
const step = 1,
|
|
36
|
+
empty = 0,
|
|
37
|
+
required = (
|
|
38
|
+
version: Readonly<ListVersion>,
|
|
39
|
+
unitId: string,
|
|
40
|
+
provenance: Readonly<Provenance>,
|
|
41
|
+
): Immutable<Subtask> => {
|
|
42
|
+
const record = version.tasks.get(unitId, version.revision);
|
|
43
|
+
if (record === undefined) {
|
|
44
|
+
throw new Error("INVALID_TODO_DELTA: order names a missing task");
|
|
45
|
+
}
|
|
46
|
+
const task = structuredClone(record.task);
|
|
47
|
+
if (record.revision < version.revision && task.unit !== undefined) {
|
|
48
|
+
provenance.resolve(task.unit, "");
|
|
49
|
+
}
|
|
50
|
+
return task;
|
|
51
|
+
},
|
|
52
|
+
materialize = (version: Readonly<ListVersion>, provenance: Readonly<Provenance>): TodoList => {
|
|
53
|
+
if (!version.hasBase) {
|
|
54
|
+
throw new Error("INVALID_TODO_DELTA: no stable-ID base snapshot");
|
|
55
|
+
}
|
|
56
|
+
const list = parse({
|
|
57
|
+
tasks: version.orders.ids(null, version.revision).map((unitId) =>
|
|
58
|
+
Object.assign({}, required(version, unitId, provenance), {
|
|
59
|
+
subtasks: version.orders
|
|
60
|
+
.ids(unitId, version.revision)
|
|
61
|
+
.map((childId) => required(version, childId, provenance)),
|
|
62
|
+
}),
|
|
63
|
+
),
|
|
64
|
+
});
|
|
65
|
+
if (
|
|
66
|
+
list.tasks.reduce((count, root) => count + step + (root.subtasks?.length ?? empty), empty) !==
|
|
67
|
+
version.size
|
|
68
|
+
) {
|
|
69
|
+
throw new Error("INVALID_TODO_DELTA: tasks missing from order");
|
|
70
|
+
}
|
|
71
|
+
return list;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/** Owns one linear replay, retaining changed values rather than full-list copies. */
|
|
75
|
+
class BranchState {
|
|
76
|
+
readonly #provenance = new Provenance();
|
|
77
|
+
#tasks = new History<string, TaskRecord | undefined>();
|
|
78
|
+
#orders = new OrderIndex();
|
|
79
|
+
#known = new Set<string>();
|
|
80
|
+
#revision = empty;
|
|
81
|
+
#hasBase = false;
|
|
82
|
+
#fenced = false;
|
|
83
|
+
#latest: ListFrame | undefined = undefined;
|
|
84
|
+
|
|
85
|
+
#record(task: Immutable<Subtask>, entry: BranchEntry): void {
|
|
86
|
+
if (task.unit !== undefined) {
|
|
87
|
+
this.#provenance.resolve(structuredClone(task.unit), entry.id ?? "");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#index(index: Readonly<ListIndex>): void {
|
|
92
|
+
for (const [unitId, task] of index.tasks) {
|
|
93
|
+
this.#tasks.set(unitId, this.#revision, { revision: this.#revision, task });
|
|
94
|
+
}
|
|
95
|
+
for (const [parent, ids] of index.orders) {
|
|
96
|
+
let after: string | null = null;
|
|
97
|
+
for (const unitId of ids) {
|
|
98
|
+
this.#orders.apply({ after, id: unitId, parent }, this.#revision);
|
|
99
|
+
after = unitId;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
#replace(index: Readonly<ListIndex> | undefined): void {
|
|
105
|
+
this.#hasBase = index !== undefined;
|
|
106
|
+
this.#known = new Set(index?.tasks.keys());
|
|
107
|
+
this.#tasks = new History();
|
|
108
|
+
this.#orders = new OrderIndex();
|
|
109
|
+
if (index !== undefined) {
|
|
110
|
+
this.#index(index);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
#full(entry: BranchEntry, data: unknown): ListFrame {
|
|
115
|
+
const { fenced, list } = snapshotData(data);
|
|
116
|
+
if (this.#fenced && !fenced) {
|
|
117
|
+
throw new Error("INVALID_TODO_DELTA: unfenced snapshot after delta history");
|
|
118
|
+
}
|
|
119
|
+
const progress = JSON.stringify(this.#latest?.list ?? { tasks: [] }) !== JSON.stringify(list),
|
|
120
|
+
index = indexed(list),
|
|
121
|
+
changed = list.tasks.flatMap((root) => [leafTask(root), ...(root.subtasks ?? [])]),
|
|
122
|
+
provenance = this.#provenance;
|
|
123
|
+
this.#fenced ||= fenced;
|
|
124
|
+
this.#replace(index);
|
|
125
|
+
for (const task of changed) {
|
|
126
|
+
this.#record(task, entry);
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
changed,
|
|
130
|
+
entry,
|
|
131
|
+
fenced: this.#fenced,
|
|
132
|
+
list,
|
|
133
|
+
progress,
|
|
134
|
+
get snapshot(): TodoList {
|
|
135
|
+
return provenance.list(list);
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
#changed(delta: TodoDelta, entry: BranchEntry): boolean {
|
|
141
|
+
let progress = false;
|
|
142
|
+
for (const change of delta.changed) {
|
|
143
|
+
progress ||=
|
|
144
|
+
JSON.stringify(this.#tasks.get(change.id, this.#revision)?.task) !==
|
|
145
|
+
JSON.stringify(change.task);
|
|
146
|
+
this.#known.add(change.id);
|
|
147
|
+
this.#tasks.set(change.id, this.#revision, { revision: this.#revision, task: change.task });
|
|
148
|
+
this.#record(change.task, entry);
|
|
149
|
+
}
|
|
150
|
+
return progress;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
#removed(ids: readonly string[]): boolean {
|
|
154
|
+
let progress = false;
|
|
155
|
+
for (const unitId of ids) {
|
|
156
|
+
progress = this.#known.delete(unitId) || progress;
|
|
157
|
+
this.#tasks.set(unitId, this.#revision, undefined);
|
|
158
|
+
this.#orders.apply(unitId, this.#revision);
|
|
159
|
+
}
|
|
160
|
+
return progress;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
#apply(delta: TodoDelta, entry: BranchEntry): boolean {
|
|
164
|
+
const removed = this.#removed(delta.removed);
|
|
165
|
+
let progress = this.#changed(delta, entry) || removed;
|
|
166
|
+
for (const order of delta.orders) {
|
|
167
|
+
if (!this.#known.has(order.id) || (order.parent !== null && !this.#known.has(order.parent))) {
|
|
168
|
+
throw new Error("INVALID_TODO_DELTA: order names a missing task");
|
|
169
|
+
}
|
|
170
|
+
progress = this.#orders.apply(order, this.#revision) || progress;
|
|
171
|
+
}
|
|
172
|
+
return progress;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
#delta(entry: BranchEntry, data: unknown): ListFrame {
|
|
176
|
+
const delta = deltaData(data);
|
|
177
|
+
if (!this.#hasBase) {
|
|
178
|
+
throw new Error("INVALID_TODO_DELTA: no stable-ID base snapshot");
|
|
179
|
+
}
|
|
180
|
+
const progress = this.#apply(delta, entry),
|
|
181
|
+
version: ListVersion = {
|
|
182
|
+
hasBase: this.#hasBase,
|
|
183
|
+
orders: this.#orders,
|
|
184
|
+
revision: this.#revision,
|
|
185
|
+
size: this.#known.size,
|
|
186
|
+
tasks: this.#tasks,
|
|
187
|
+
},
|
|
188
|
+
provenance = this.#provenance;
|
|
189
|
+
let list: TodoList | undefined = undefined;
|
|
190
|
+
return {
|
|
191
|
+
changed: delta.changed.map((change) => change.task),
|
|
192
|
+
entry,
|
|
193
|
+
fenced: this.#fenced,
|
|
194
|
+
get list(): TodoList {
|
|
195
|
+
list ??= materialize(version, provenance);
|
|
196
|
+
return list;
|
|
197
|
+
},
|
|
198
|
+
progress,
|
|
199
|
+
get snapshot(): TodoList {
|
|
200
|
+
return provenance.list(this.list);
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Full snapshots replace the index. Deltas touch only changed tasks and links.
|
|
206
|
+
public read(entry: BranchEntry): ListFrame | undefined {
|
|
207
|
+
if (entry.type !== "custom") {
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
if (entry.customType === STATE_ENTRY || entry.customType === DELTA_ENTRY) {
|
|
211
|
+
this.#revision += step;
|
|
212
|
+
this.#latest =
|
|
213
|
+
entry.customType === STATE_ENTRY
|
|
214
|
+
? this.#full(entry, entry.data)
|
|
215
|
+
: this.#delta(entry, entry.data);
|
|
216
|
+
return this.#latest;
|
|
217
|
+
}
|
|
218
|
+
return undefined;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export { BranchState };
|
|
223
|
+
export type { BranchEntry, ListFrame };
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { ReminderFact, TodoList } from "./wasm.ts";
|
|
2
|
+
import { STATE_ENTRY, branchList, branchSnapshot, latestSnapshot, stateChange } from "./state.ts";
|
|
3
|
+
import { apply, completionReminder, parse } from "./wasm.ts";
|
|
4
|
+
import { contract, first, firstId, fixture, last } from "./contract-fixture.ts";
|
|
5
|
+
import {
|
|
6
|
+
referenceFacts,
|
|
7
|
+
referenceLatest,
|
|
8
|
+
referenceSnapshot,
|
|
9
|
+
} from "./snapshot-reference-fixture.ts";
|
|
10
|
+
import { reminder, reminderFacts } from "./reminder.ts";
|
|
11
|
+
import type { Immutable } from "./delta.ts";
|
|
12
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
13
|
+
import type { TodoParams } from "./tool.ts";
|
|
14
|
+
|
|
15
|
+
interface SnapshotPair {
|
|
16
|
+
readonly actual: TodoList;
|
|
17
|
+
readonly expected: TodoList;
|
|
18
|
+
}
|
|
19
|
+
const snapshotLists = (facts: readonly Immutable<ReminderFact>[]): unknown[] => {
|
|
20
|
+
const lists: unknown[] = [];
|
|
21
|
+
for (const fact of facts) {
|
|
22
|
+
if (fact.kind === "snapshot") {
|
|
23
|
+
lists.push(fact.list);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return lists;
|
|
27
|
+
},
|
|
28
|
+
deltaCount = (branch: readonly SessionEntry[]): number =>
|
|
29
|
+
branch.filter((item) => item.type === "custom" && item.customType === "todo-delta").length,
|
|
30
|
+
taskAt = (list: TodoList, index = first): NonNullable<TodoList["tasks"][number]> => {
|
|
31
|
+
const task = list.tasks.at(index);
|
|
32
|
+
if (task === undefined) {
|
|
33
|
+
throw new Error("Missing fixture root");
|
|
34
|
+
}
|
|
35
|
+
return task;
|
|
36
|
+
},
|
|
37
|
+
storedData = (branch: readonly SessionEntry[]): { data: unknown; customType: string } => {
|
|
38
|
+
const stored = branch.at(last);
|
|
39
|
+
if (stored?.type !== "custom") {
|
|
40
|
+
throw new Error("Missing custom state entry");
|
|
41
|
+
}
|
|
42
|
+
return { customType: stored.customType, data: stored.data };
|
|
43
|
+
},
|
|
44
|
+
taskCount = 192,
|
|
45
|
+
entry = (id: string, customType: string, data: unknown): SessionEntry => ({
|
|
46
|
+
customType,
|
|
47
|
+
data,
|
|
48
|
+
id,
|
|
49
|
+
parentId: null,
|
|
50
|
+
timestamp: "2026-11-10T00:00:00Z",
|
|
51
|
+
type: "custom",
|
|
52
|
+
}),
|
|
53
|
+
initial = (count: number): TodoList =>
|
|
54
|
+
apply(
|
|
55
|
+
{ tasks: [] },
|
|
56
|
+
{
|
|
57
|
+
items: Array.from({ length: count }, (_unused, index) => ({
|
|
58
|
+
content: `Task ${index}`,
|
|
59
|
+
contract: contract(),
|
|
60
|
+
})),
|
|
61
|
+
op: "init",
|
|
62
|
+
},
|
|
63
|
+
{ mutationId: "initial", sessionId: "test-session" },
|
|
64
|
+
).list,
|
|
65
|
+
statusSizes = (count: number): { delta: number; full: number; type: string } => {
|
|
66
|
+
const caller = fixture(),
|
|
67
|
+
before = caller.call({
|
|
68
|
+
items: Array.from({ length: count }, (_unused, index) => ({
|
|
69
|
+
content: `Task ${index}`,
|
|
70
|
+
contract: contract(),
|
|
71
|
+
})),
|
|
72
|
+
op: "init",
|
|
73
|
+
}),
|
|
74
|
+
after = caller.call({ op: "start", task: firstId(before) }),
|
|
75
|
+
change = storedData(caller.entries),
|
|
76
|
+
encoder = new TextEncoder();
|
|
77
|
+
return {
|
|
78
|
+
delta: encoder.encode(JSON.stringify(change.data)).length,
|
|
79
|
+
full: encoder.encode(JSON.stringify(after)).length,
|
|
80
|
+
type: change.customType,
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/** Paired old and new writers, with a provenance observation at each mutation. */
|
|
85
|
+
class Replay {
|
|
86
|
+
public readonly branch: SessionEntry[] = [];
|
|
87
|
+
public readonly full: SessionEntry[] = [];
|
|
88
|
+
readonly #observed: SnapshotPair[] = [];
|
|
89
|
+
|
|
90
|
+
public save(list: TodoList, forceFull = false): void {
|
|
91
|
+
const id = `entry-${this.full.length}`,
|
|
92
|
+
change = forceFull
|
|
93
|
+
? { customType: STATE_ENTRY, data: list }
|
|
94
|
+
: stateChange(branchSnapshot(this.branch), list),
|
|
95
|
+
parentId = this.full.at(last)?.id ?? null,
|
|
96
|
+
changedData = structuredClone(change.data),
|
|
97
|
+
fullData = structuredClone(list),
|
|
98
|
+
deltaEntry = Object.assign(entry(id, change.customType, changedData), { parentId }),
|
|
99
|
+
fullEntry = Object.assign(entry(id, STATE_ENTRY, fullData), { parentId });
|
|
100
|
+
this.branch.push(deltaEntry);
|
|
101
|
+
this.full.push(fullEntry);
|
|
102
|
+
this.#observed.push({
|
|
103
|
+
actual: branchSnapshot(this.branch),
|
|
104
|
+
expected: referenceSnapshot(this.full),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public call(params: TodoParams): void {
|
|
109
|
+
const context = { mutationId: `change-${this.full.length}`, sessionId: "test-session" },
|
|
110
|
+
expected = apply(referenceSnapshot(this.full), params, context),
|
|
111
|
+
actual = apply(branchSnapshot(this.branch), params, context);
|
|
112
|
+
this.#observed.push({ actual: actual.list, expected: expected.list });
|
|
113
|
+
this.save(actual.list);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public compare(): { actual: unknown; expected: unknown } {
|
|
117
|
+
const facts = referenceFacts(this.full);
|
|
118
|
+
return {
|
|
119
|
+
actual: {
|
|
120
|
+
derived: branchSnapshot(this.branch),
|
|
121
|
+
facts: completionReminder({ facts: reminderFacts(this.branch, []) }),
|
|
122
|
+
frames: [...branchList(this.branch)].map((frame) => frame.list),
|
|
123
|
+
full: branchSnapshot(this.full),
|
|
124
|
+
latest: latestSnapshot(this.branch),
|
|
125
|
+
observed: this.#observed.map((pair) => pair.actual),
|
|
126
|
+
reminder: reminder(this.branch, []),
|
|
127
|
+
},
|
|
128
|
+
expected: {
|
|
129
|
+
derived: referenceSnapshot(this.full),
|
|
130
|
+
facts: completionReminder({ facts }),
|
|
131
|
+
frames: snapshotLists(facts),
|
|
132
|
+
full: referenceSnapshot(this.full),
|
|
133
|
+
latest: parse(referenceLatest(this.full)),
|
|
134
|
+
observed: this.#observed.map((pair) => pair.expected),
|
|
135
|
+
reminder: completionReminder({ facts }),
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export { Replay, deltaCount, entry, initial, statusSizes, storedData, taskAt, taskCount };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { DELTA_ENTRY, STATE_ENTRY, stateChange } from "./state.ts";
|
|
2
|
+
import type { ExtensionContext, SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { entry, initial, taskAt } from "./delta-fixture.ts";
|
|
4
|
+
import { TaskProvider } from "./old-provider-fixture.ts";
|
|
5
|
+
import { leafTask } from "./delta.ts";
|
|
6
|
+
import { runtimeFixture } from "./runtime-fixture.ts";
|
|
7
|
+
|
|
8
|
+
const count = 100,
|
|
9
|
+
changes = 20,
|
|
10
|
+
first = 0,
|
|
11
|
+
step = 1,
|
|
12
|
+
replayBranch = (): SessionEntry[] => {
|
|
13
|
+
const list = initial(count),
|
|
14
|
+
branch = [entry("base", STATE_ENTRY, list)];
|
|
15
|
+
let current = list;
|
|
16
|
+
for (let index = first; index < changes; index += step) {
|
|
17
|
+
const next = structuredClone(current);
|
|
18
|
+
taskAt(next).description = `Change ${index}`;
|
|
19
|
+
const change = stateChange(current, next);
|
|
20
|
+
branch.push(entry(`change-${index}`, change.customType, change.data));
|
|
21
|
+
current = next;
|
|
22
|
+
}
|
|
23
|
+
return branch;
|
|
24
|
+
},
|
|
25
|
+
intermediateBranch = (changed: unknown): SessionEntry[] => {
|
|
26
|
+
const list = initial(count),
|
|
27
|
+
task = leafTask(taskAt(list)),
|
|
28
|
+
id = task.unit?.reference.unitId,
|
|
29
|
+
delta = (value: unknown): unknown => ({
|
|
30
|
+
changed: [{ id, task: value }],
|
|
31
|
+
orders: [],
|
|
32
|
+
removed: [],
|
|
33
|
+
version: step,
|
|
34
|
+
});
|
|
35
|
+
return [
|
|
36
|
+
entry("base", STATE_ENTRY, list),
|
|
37
|
+
entry("bad", DELTA_ENTRY, delta(changed)),
|
|
38
|
+
entry("fixed", DELTA_ENTRY, delta(task)),
|
|
39
|
+
];
|
|
40
|
+
},
|
|
41
|
+
oldRuntime = async (): Promise<{
|
|
42
|
+
runtime: Awaited<ReturnType<typeof runtimeFixture>>;
|
|
43
|
+
provider: TaskProvider;
|
|
44
|
+
context: ExtensionContext;
|
|
45
|
+
}> => {
|
|
46
|
+
const captured: { provider?: TaskProvider; context?: ExtensionContext } = {},
|
|
47
|
+
runtime = await runtimeFixture((pi) => {
|
|
48
|
+
captured.provider = new TaskProvider(pi);
|
|
49
|
+
pi.on("session_start", (_event, ctx) => {
|
|
50
|
+
captured.context = ctx;
|
|
51
|
+
captured.provider?.context(ctx);
|
|
52
|
+
});
|
|
53
|
+
}),
|
|
54
|
+
{ provider, context } = captured;
|
|
55
|
+
if (provider === undefined || context === undefined) {
|
|
56
|
+
runtime.session.dispose();
|
|
57
|
+
throw new Error("Missing retained provider context");
|
|
58
|
+
}
|
|
59
|
+
return { context, provider, runtime };
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export { count, changes, intermediateBranch, oldRuntime, replayBranch };
|
|
63
|
+
export { show as oldShow } from "./old-lifecycle-fixture.ts";
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Replay, initial, taskCount } from "./delta-fixture.ts";
|
|
2
|
+
import { REMINDER_ENTRY } from "./reminder.ts";
|
|
3
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { branchSnapshot } from "./state.ts";
|
|
5
|
+
|
|
6
|
+
const changes = 60,
|
|
7
|
+
rounds = 20,
|
|
8
|
+
first = 0,
|
|
9
|
+
step = 1,
|
|
10
|
+
reminderInterval = 5,
|
|
11
|
+
userInterval = 7,
|
|
12
|
+
boundaryEntries = (index: number): SessionEntry[] => {
|
|
13
|
+
const entries: SessionEntry[] = [];
|
|
14
|
+
if (index % reminderInterval === first) {
|
|
15
|
+
entries.push({
|
|
16
|
+
content: "Earlier reminder",
|
|
17
|
+
customType: REMINDER_ENTRY,
|
|
18
|
+
display: false,
|
|
19
|
+
id: `reminder-${index}`,
|
|
20
|
+
parentId: null,
|
|
21
|
+
timestamp: "2026-11-10T00:00:00Z",
|
|
22
|
+
type: "custom_message",
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
if (index % userInterval === first) {
|
|
26
|
+
entries.push({
|
|
27
|
+
id: `user-${index}`,
|
|
28
|
+
message: { content: "Continue", role: "user", timestamp: first },
|
|
29
|
+
parentId: null,
|
|
30
|
+
timestamp: "2026-11-10T00:00:00Z",
|
|
31
|
+
type: "message",
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return entries;
|
|
35
|
+
},
|
|
36
|
+
changeRound = (
|
|
37
|
+
replay: { readonly branch: readonly SessionEntry[]; readonly call: Replay["call"] },
|
|
38
|
+
index: number,
|
|
39
|
+
): void => {
|
|
40
|
+
const id = branchSnapshot(replay.branch).tasks.at(index)?.unit?.reference.unitId;
|
|
41
|
+
if (id === undefined) {
|
|
42
|
+
throw new Error("Missing scripted task");
|
|
43
|
+
}
|
|
44
|
+
replay.call({ description: `Revision ${index}`, op: "update", task: id });
|
|
45
|
+
replay.call({ op: "start", task: id });
|
|
46
|
+
replay.call({ evidence: "Relevant unit checks pass", op: "done", task: id });
|
|
47
|
+
},
|
|
48
|
+
sequence = (): Replay => {
|
|
49
|
+
const replay = new Replay();
|
|
50
|
+
replay.save(initial(taskCount), true);
|
|
51
|
+
for (let index = first; index < rounds; index += step) {
|
|
52
|
+
changeRound(replay, index);
|
|
53
|
+
replay.branch.push(...boundaryEntries(index));
|
|
54
|
+
replay.full.push(...boundaryEntries(index));
|
|
55
|
+
}
|
|
56
|
+
return replay;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export { changes, sequence };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Task, TodoList } from "./wasm.ts";
|
|
2
|
+
import type { TaskChange, TodoDelta } from "./delta.ts";
|
|
3
|
+
import { deltaVersion, leafTask } from "./delta.ts";
|
|
4
|
+
import { parse, validateTask } from "./wasm.ts";
|
|
5
|
+
import type { OrderChange } from "./order-index.ts";
|
|
6
|
+
|
|
7
|
+
const record = (data: unknown): data is Readonly<Record<string, unknown>> =>
|
|
8
|
+
typeof data === "object" && data !== null,
|
|
9
|
+
nullableId = (data: unknown): data is string | null => data === null || typeof data === "string",
|
|
10
|
+
taskChange = (data: unknown): data is TaskChange =>
|
|
11
|
+
record(data) &&
|
|
12
|
+
typeof data["id"] === "string" &&
|
|
13
|
+
record(data["task"]) &&
|
|
14
|
+
!("subtasks" in data["task"]) &&
|
|
15
|
+
Object.keys(data).every((key) => key === "id" || key === "task"),
|
|
16
|
+
orderChange = (data: unknown): data is OrderChange =>
|
|
17
|
+
record(data) &&
|
|
18
|
+
typeof data["id"] === "string" &&
|
|
19
|
+
nullableId(data["parent"]) &&
|
|
20
|
+
nullableId(data["after"]) &&
|
|
21
|
+
Object.keys(data).every((key) => ["id", "parent", "after"].includes(key)),
|
|
22
|
+
envelope = (data: unknown): data is TodoDelta =>
|
|
23
|
+
record(data) &&
|
|
24
|
+
data["version"] === deltaVersion &&
|
|
25
|
+
Object.keys(data).every((key) => ["version", "changed", "orders", "removed"].includes(key)) &&
|
|
26
|
+
Array.isArray(data["changed"]) &&
|
|
27
|
+
data["changed"].every((change: unknown) => taskChange(change)) &&
|
|
28
|
+
Array.isArray(data["removed"]) &&
|
|
29
|
+
data["removed"].every((unitId: unknown) => typeof unitId === "string") &&
|
|
30
|
+
Array.isArray(data["orders"]) &&
|
|
31
|
+
data["orders"].every((order: unknown) => orderChange(order)),
|
|
32
|
+
changedTask = (data: unknown): Task => {
|
|
33
|
+
try {
|
|
34
|
+
return validateTask(data);
|
|
35
|
+
} catch (error: unknown) {
|
|
36
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
37
|
+
throw new Error(`INVALID_TODO_DELTA: ${message}`, { cause: error });
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
deltaData = (data: unknown): TodoDelta => {
|
|
41
|
+
if (!envelope(data)) {
|
|
42
|
+
throw new Error("INVALID_TODO_DELTA: unsupported or malformed delta");
|
|
43
|
+
}
|
|
44
|
+
const removed = new Set(data.removed);
|
|
45
|
+
if (
|
|
46
|
+
new Set(data.changed.map((change) => change.id)).size !== data.changed.length ||
|
|
47
|
+
removed.size !== data.removed.length ||
|
|
48
|
+
data.changed.some((change) => removed.has(change.id))
|
|
49
|
+
) {
|
|
50
|
+
throw new Error("INVALID_TODO_DELTA: repeated or conflicting task changes");
|
|
51
|
+
}
|
|
52
|
+
const changed = data.changed.map((change): TaskChange => {
|
|
53
|
+
const task = changedTask(change.task);
|
|
54
|
+
if (change.id !== task.unit?.reference.unitId) {
|
|
55
|
+
throw new Error("INVALID_TODO_DELTA: changed task identity differs");
|
|
56
|
+
}
|
|
57
|
+
return { id: change.id, task: leafTask(task) };
|
|
58
|
+
});
|
|
59
|
+
return Object.assign({}, data, { changed });
|
|
60
|
+
},
|
|
61
|
+
snapshotData = (data: unknown): { readonly fenced: boolean; readonly list: TodoList } => {
|
|
62
|
+
const fenced = record(data) && "format" in data;
|
|
63
|
+
if (fenced && data["format"] !== "todo-delta/1") {
|
|
64
|
+
throw new Error("INVALID_TODO_DELTA: unsupported snapshot format");
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
fenced,
|
|
68
|
+
list: parse(
|
|
69
|
+
fenced
|
|
70
|
+
? Object.fromEntries(
|
|
71
|
+
Object.entries(data).filter(([key]: readonly [string, unknown]) => key !== "format"),
|
|
72
|
+
)
|
|
73
|
+
: data,
|
|
74
|
+
),
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export { deltaData, snapshotData };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { DELTA_ENTRY, branchSnapshot } from "./state.ts";
|
|
2
|
+
import { contract, first, firstId, last } from "./contract-fixture.ts";
|
|
3
|
+
import type { RuntimeFixture } from "./runtime-fixture.ts";
|
|
4
|
+
import { apply } from "./wasm.ts";
|
|
5
|
+
import { importFixture } from "./import-fixture.ts";
|
|
6
|
+
import { leafTask } from "./delta.ts";
|
|
7
|
+
import { runtimeFixture } from "./runtime-fixture.ts";
|
|
8
|
+
import { value } from "./provider-fixture.ts";
|
|
9
|
+
|
|
10
|
+
const workers = 100,
|
|
11
|
+
workerDrafts = (): { content: string; contract: ReturnType<typeof contract> }[] =>
|
|
12
|
+
Array.from({ length: workers }, (_unused, index) => ({
|
|
13
|
+
content: `Worker ${index}`,
|
|
14
|
+
contract: Object.assign(contract(), {
|
|
15
|
+
assignment: { kind: "worker" as const, profile: "general" },
|
|
16
|
+
}),
|
|
17
|
+
})),
|
|
18
|
+
claimScenario = async (): Promise<{ actual: unknown; origin: string }> => {
|
|
19
|
+
const runtime = await runtimeFixture();
|
|
20
|
+
try {
|
|
21
|
+
await runtime.todo({ items: workerDrafts(), op: "init" });
|
|
22
|
+
const unitId = firstId(branchSnapshot(runtime.manager.getBranch())),
|
|
23
|
+
provider = runtime.provider(),
|
|
24
|
+
inspected = value(provider.inspect(unitId)),
|
|
25
|
+
claimed = value(
|
|
26
|
+
provider.claim({
|
|
27
|
+
execution: { executor: "dogsled", ownerId: "owner", requestId: "delta-claim" },
|
|
28
|
+
expected: inspected.ref,
|
|
29
|
+
}),
|
|
30
|
+
),
|
|
31
|
+
claimEntry = runtime.manager.getBranch().at(last),
|
|
32
|
+
settled = provider.settle({
|
|
33
|
+
binding: claimed.binding,
|
|
34
|
+
eventId: "delta-returned",
|
|
35
|
+
result: {
|
|
36
|
+
evidence: {
|
|
37
|
+
execution: claimed.binding.execution,
|
|
38
|
+
resultId: "reply",
|
|
39
|
+
summary: "Worker result",
|
|
40
|
+
},
|
|
41
|
+
kind: "returned",
|
|
42
|
+
},
|
|
43
|
+
version: 1,
|
|
44
|
+
}),
|
|
45
|
+
settleEntry = runtime.manager.getBranch().at(last),
|
|
46
|
+
task = branchSnapshot(runtime.manager.getBranch()).tasks.at(first);
|
|
47
|
+
return {
|
|
48
|
+
actual: {
|
|
49
|
+
claimEntry,
|
|
50
|
+
evidence: task?.unit?.evidence?.length,
|
|
51
|
+
origin: task?.unit?.reference.contractEntryId,
|
|
52
|
+
settleEntry,
|
|
53
|
+
settled,
|
|
54
|
+
task,
|
|
55
|
+
},
|
|
56
|
+
origin: inspected.ref.contractEntryId,
|
|
57
|
+
};
|
|
58
|
+
} finally {
|
|
59
|
+
runtime.session.dispose();
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
rewind = (
|
|
63
|
+
runtime: Readonly<Pick<RuntimeFixture, "provider">> & {
|
|
64
|
+
readonly manager: Readonly<Pick<RuntimeFixture["manager"], "branch" | "resetLeaf">>;
|
|
65
|
+
},
|
|
66
|
+
origin: string,
|
|
67
|
+
): { active: unknown; imported: unknown } => {
|
|
68
|
+
runtime.manager.branch(origin);
|
|
69
|
+
const active = runtime.provider().active();
|
|
70
|
+
runtime.manager.resetLeaf();
|
|
71
|
+
return { active, imported: runtime.provider().imported() };
|
|
72
|
+
},
|
|
73
|
+
sealScenario = async (): Promise<{ receipt: unknown; rewound: unknown; sealId: string }> => {
|
|
74
|
+
const fixture = await importFixture(),
|
|
75
|
+
runtime = await runtimeFixture(undefined, false, fixture),
|
|
76
|
+
{ manager } = runtime,
|
|
77
|
+
origin = manager.getLeafId();
|
|
78
|
+
try {
|
|
79
|
+
const sealed = apply(
|
|
80
|
+
branchSnapshot(manager.getBranch()),
|
|
81
|
+
{ evidence: "Reviewed delta seal", op: "done", task: fixture.origin.id },
|
|
82
|
+
{ mutationId: "delta-seal", sessionId: manager.getSessionId() },
|
|
83
|
+
).list,
|
|
84
|
+
root = sealed.tasks.at(first);
|
|
85
|
+
if (root === undefined || origin === null) {
|
|
86
|
+
throw new Error("Missing imported root");
|
|
87
|
+
}
|
|
88
|
+
const sealId = manager.appendCustomEntry(DELTA_ENTRY, {
|
|
89
|
+
changed: [{ id: fixture.origin.id, task: leafTask(root) }],
|
|
90
|
+
orders: [],
|
|
91
|
+
removed: [],
|
|
92
|
+
version: 1,
|
|
93
|
+
}),
|
|
94
|
+
receipt = value(runtime.provider().imported());
|
|
95
|
+
return { receipt, rewound: rewind(runtime, origin), sealId };
|
|
96
|
+
} finally {
|
|
97
|
+
runtime.session.dispose();
|
|
98
|
+
await fixture.close();
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export { claimScenario, sealScenario };
|