@ftrdotdev/pi-tracker 0.5.2 → 0.7.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 +136 -46
- package/package.json +8 -2
- package/src/compaction/candidate.test.ts +191 -0
- package/src/compaction/candidate.ts +105 -0
- package/src/compaction/classifier.test.ts +422 -0
- package/src/compaction/classifier.ts +198 -0
- package/src/compaction/completion.test.ts +257 -0
- package/src/compaction/completion.ts +94 -0
- package/src/compaction/decision-record.test.ts +91 -0
- package/src/compaction/decision-record.ts +101 -0
- package/src/compaction/digest.test.ts +185 -0
- package/src/compaction/digest.ts +158 -0
- package/src/compaction/footprint.test.ts +135 -0
- package/src/compaction/footprint.ts +387 -0
- package/src/compaction/observations.test.ts +199 -0
- package/src/compaction/observations.ts +348 -0
- package/src/compaction/pointer.test.ts +26 -0
- package/src/compaction/pointer.ts +15 -0
- package/src/compaction/rules.test.ts +253 -0
- package/src/compaction/rules.ts +188 -0
- package/src/compaction/settings.test.ts +315 -0
- package/src/compaction/settings.ts +185 -0
- package/src/compaction/settle.test.ts +370 -0
- package/src/compaction/settle.ts +150 -0
- package/src/{deps.test.ts → core/deps.test.ts} +1 -1
- package/src/{domain.test.ts → core/domain.test.ts} +42 -9
- package/src/{domain.ts → core/domain.ts} +73 -8
- package/src/{persistence.test.ts → core/persistence.test.ts} +7 -7
- package/src/{store.test.ts → core/store.test.ts} +116 -50
- package/src/{store.ts → core/store.ts} +92 -31
- package/src/index.test.ts +728 -84
- package/src/index.ts +237 -49
- package/src/{tool-metadata.test.ts → presentation/tool-metadata.test.ts} +264 -49
- package/src/presentation/tool-metadata.ts +675 -0
- package/src/{ui.test.ts → presentation/ui.test.ts} +128 -9
- package/src/{ui.ts → presentation/ui.ts} +123 -30
- package/src/tool-metadata.ts +0 -452
- /package/src/{deps.ts → core/deps.ts} +0 -0
- /package/src/{persistence.ts → core/persistence.ts} +0 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { Effect, Layer } from "effect";
|
|
3
|
+
import type { ClassifierApi, ClassifierModel, ClassifierResult } from "@earendil-works/pi-ai";
|
|
4
|
+
import { NEEDS_CONTEXT_LABEL, NEEDS_NONE_LABEL } from "./classifier.ts";
|
|
5
|
+
import { ClassifierGateway, type ClassifierRegistry } from "./classifier.ts";
|
|
6
|
+
import type { CompactionCandidate } from "./candidate.ts";
|
|
7
|
+
import { CompletionObserver } from "./completion.ts";
|
|
8
|
+
import { FootprintRecorder } from "./footprint.ts";
|
|
9
|
+
import {
|
|
10
|
+
containsSymbol,
|
|
11
|
+
emptyFootprint,
|
|
12
|
+
productionFacts,
|
|
13
|
+
type BatchFootprint,
|
|
14
|
+
type FileProbe,
|
|
15
|
+
} from "./observations.ts";
|
|
16
|
+
import { acceptedRoute, buildRuleInput, resolveCandidates } from "./rules.ts";
|
|
17
|
+
import { SettleDecider, type SettleDecision } from "./settle.ts";
|
|
18
|
+
import { SmartCompactionSettingsService, defaultSmartCompactionSettings } from "./settings.ts";
|
|
19
|
+
import { TodoItem, TodoList, TrackerState } from "../core/domain.ts";
|
|
20
|
+
|
|
21
|
+
const model: ClassifierModel<ClassifierApi> = {
|
|
22
|
+
id: "@cf/cloudflare/clef-flash",
|
|
23
|
+
name: "clef-flash",
|
|
24
|
+
api: "typesafe-system-one",
|
|
25
|
+
provider: "cloudflare-workers-ai",
|
|
26
|
+
baseUrl: "https://example.invalid",
|
|
27
|
+
input: ["text"],
|
|
28
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
29
|
+
type: "classifier",
|
|
30
|
+
contextWindow: 8192,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** A probe over a path-to-text map. `findSymbol` scans the same map. */
|
|
34
|
+
const probeOf = (files: Readonly<Record<string, string>>): FileProbe => ({
|
|
35
|
+
read: (path) => files[path],
|
|
36
|
+
findSymbol: (symbol) =>
|
|
37
|
+
Object.entries(files)
|
|
38
|
+
.filter(([, text]) => containsSymbol(text, symbol))
|
|
39
|
+
.map(([path]) => path),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
interface RegistrySpy {
|
|
43
|
+
readonly registry: ClassifierRegistry;
|
|
44
|
+
readonly calls: () => number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A registry that answers every question with the given `needs-context` mass. */
|
|
48
|
+
const registrySpy = (needsMass: number): RegistrySpy => {
|
|
49
|
+
let calls = 0;
|
|
50
|
+
return {
|
|
51
|
+
calls: () => calls,
|
|
52
|
+
registry: {
|
|
53
|
+
getAvailableOfType: async () => [model],
|
|
54
|
+
classify: async (_model, context) => {
|
|
55
|
+
calls += 1;
|
|
56
|
+
const answers: ClassifierResult["answers"] = {};
|
|
57
|
+
for (const key of Object.keys(context.questions)) {
|
|
58
|
+
answers[key] = {
|
|
59
|
+
type: "choice",
|
|
60
|
+
choice: needsMass >= 0.5 ? NEEDS_CONTEXT_LABEL : NEEDS_NONE_LABEL,
|
|
61
|
+
probabilities: { [NEEDS_CONTEXT_LABEL]: needsMass, [NEEDS_NONE_LABEL]: 1 - needsMass },
|
|
62
|
+
confidence: 1,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
api: "typesafe-system-one",
|
|
67
|
+
provider: "typesafe",
|
|
68
|
+
model: "jev-latest",
|
|
69
|
+
answers,
|
|
70
|
+
stopReason: "stop",
|
|
71
|
+
timestamp: 0,
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const settingsLayer = (
|
|
79
|
+
overrides: Partial<ReturnType<typeof defaultSmartCompactionSettings>> = {},
|
|
80
|
+
): Layer.Layer<SmartCompactionSettingsService> =>
|
|
81
|
+
Layer.succeed(
|
|
82
|
+
SmartCompactionSettingsService,
|
|
83
|
+
SmartCompactionSettingsService.of({
|
|
84
|
+
path: "/tmp/smart-compaction.json",
|
|
85
|
+
load: Effect.succeed({ ...defaultSmartCompactionSettings(), ...overrides }),
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const footprintLayer = (footprint: BatchFootprint): Layer.Layer<FootprintRecorder> =>
|
|
90
|
+
Layer.succeed(
|
|
91
|
+
FootprintRecorder,
|
|
92
|
+
FootprintRecorder.of({
|
|
93
|
+
capture: () => Effect.void,
|
|
94
|
+
snapshot: Effect.succeed(footprint),
|
|
95
|
+
consume: Effect.succeed(footprint),
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const item = (spec: {
|
|
100
|
+
readonly id: number;
|
|
101
|
+
readonly title: string;
|
|
102
|
+
readonly description?: string;
|
|
103
|
+
readonly done: boolean;
|
|
104
|
+
readonly refs?: TodoItem["refs"];
|
|
105
|
+
readonly produces?: TodoItem["produces"];
|
|
106
|
+
}): TodoItem =>
|
|
107
|
+
new TodoItem({
|
|
108
|
+
id: spec.id,
|
|
109
|
+
title: spec.title,
|
|
110
|
+
description: spec.description ?? "",
|
|
111
|
+
done: spec.done,
|
|
112
|
+
deps: [],
|
|
113
|
+
refs: [...(spec.refs ?? [])],
|
|
114
|
+
produces: [...(spec.produces ?? [])],
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const stateOf = (first: TodoItem, candidate: TodoItem, firstDone: boolean): TrackerState =>
|
|
118
|
+
new TrackerState({
|
|
119
|
+
lists: [
|
|
120
|
+
new TodoList({
|
|
121
|
+
id: 1,
|
|
122
|
+
name: "Work",
|
|
123
|
+
items: [
|
|
124
|
+
new TodoItem({ ...first, done: firstDone }),
|
|
125
|
+
new TodoItem({ ...candidate, done: false }),
|
|
126
|
+
],
|
|
127
|
+
nextItemId: 3,
|
|
128
|
+
}),
|
|
129
|
+
],
|
|
130
|
+
activeListId: 1,
|
|
131
|
+
nextListId: 2,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Record one completion (item 1) and decide. `candidate` is the ready item the
|
|
136
|
+
* rule table and the classifier judge.
|
|
137
|
+
*/
|
|
138
|
+
const decide = (input: {
|
|
139
|
+
readonly footprint: BatchFootprint;
|
|
140
|
+
readonly probe: FileProbe;
|
|
141
|
+
readonly spy: RegistrySpy;
|
|
142
|
+
readonly candidate: TodoItem;
|
|
143
|
+
}): Effect.Effect<SettleDecision> => {
|
|
144
|
+
const first = item({
|
|
145
|
+
id: 1,
|
|
146
|
+
title: "first slice",
|
|
147
|
+
description: "the completed work",
|
|
148
|
+
done: true,
|
|
149
|
+
});
|
|
150
|
+
const before = stateOf(first, input.candidate, false);
|
|
151
|
+
const after = stateOf(first, input.candidate, true);
|
|
152
|
+
return Effect.gen(function* () {
|
|
153
|
+
const observer = yield* CompletionObserver;
|
|
154
|
+
yield* observer.record(before, after);
|
|
155
|
+
const decider = yield* SettleDecider;
|
|
156
|
+
return yield* decider.decide({ registry: input.spy.registry, outcome: "completed" });
|
|
157
|
+
}).pipe(
|
|
158
|
+
Effect.provide(
|
|
159
|
+
SettleDecider.layer(input.probe).pipe(
|
|
160
|
+
Layer.provideMerge(
|
|
161
|
+
Layer.mergeAll(
|
|
162
|
+
CompletionObserver.layer,
|
|
163
|
+
ClassifierGateway.layer,
|
|
164
|
+
footprintLayer(input.footprint),
|
|
165
|
+
settingsLayer(),
|
|
166
|
+
),
|
|
167
|
+
),
|
|
168
|
+
),
|
|
169
|
+
),
|
|
170
|
+
);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const candidate = (overrides: Partial<TodoItem> = {}): TodoItem =>
|
|
174
|
+
item({ id: 2, title: "second step", done: false, ...overrides });
|
|
175
|
+
|
|
176
|
+
const wrote = (path: string): BatchFootprint => ({
|
|
177
|
+
...emptyFootprint(),
|
|
178
|
+
wrote: [{ path, spans: [] }],
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
/** The decision compacts the head candidate; the pointer is a long resume text. */
|
|
182
|
+
const assertCompacts = (decision: SettleDecision): void => {
|
|
183
|
+
if (decision.kind !== "compact") assert.fail(`expected compact, got ${decision.kind}`);
|
|
184
|
+
assert.include(decision.pointer, "Work:2");
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// ---------------------------------------------------------------------------
|
|
188
|
+
// The accepted rows decide without the classifier
|
|
189
|
+
// ---------------------------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
it.effect("no declarations and no named batch output compact without a classifier call", () =>
|
|
192
|
+
Effect.gen(function* () {
|
|
193
|
+
const spy = registrySpy(0.1);
|
|
194
|
+
const decision = yield* decide({
|
|
195
|
+
footprint: emptyFootprint(),
|
|
196
|
+
probe: probeOf({}),
|
|
197
|
+
spy,
|
|
198
|
+
candidate: candidate(),
|
|
199
|
+
});
|
|
200
|
+
assertCompacts(decision);
|
|
201
|
+
assert.strictEqual(spy.calls(), 0);
|
|
202
|
+
// No declarations: no decision record is written.
|
|
203
|
+
assert.isUndefined(decision.record);
|
|
204
|
+
}),
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
it.effect("a candidate that names a batch output compacts", () =>
|
|
208
|
+
Effect.gen(function* () {
|
|
209
|
+
const spy = registrySpy(0.1);
|
|
210
|
+
const decision = yield* decide({
|
|
211
|
+
footprint: wrote("src/router.ts"),
|
|
212
|
+
probe: probeOf({}),
|
|
213
|
+
spy,
|
|
214
|
+
candidate: candidate({ title: "wire src/router.ts into the app" }),
|
|
215
|
+
});
|
|
216
|
+
assertCompacts(decision);
|
|
217
|
+
assert.strictEqual(spy.calls(), 0);
|
|
218
|
+
}),
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
// ---------------------------------------------------------------------------
|
|
222
|
+
// Observe-only rows keep the classifier in the loop
|
|
223
|
+
// ---------------------------------------------------------------------------
|
|
224
|
+
|
|
225
|
+
it.effect("a readable declared reference reports but does not compact", () =>
|
|
226
|
+
Effect.gen(function* () {
|
|
227
|
+
const spy = registrySpy(0.9);
|
|
228
|
+
const decision = yield* decide({
|
|
229
|
+
footprint: { ...emptyFootprint(), read: ["src/a.ts"] },
|
|
230
|
+
probe: probeOf({ "src/a.ts": "export {};" }),
|
|
231
|
+
spy,
|
|
232
|
+
candidate: candidate({ refs: [{ kind: "path", path: "src/a.ts" }] }),
|
|
233
|
+
});
|
|
234
|
+
assert.strictEqual(spy.calls(), 1);
|
|
235
|
+
assert.strictEqual(decision.kind, "keep");
|
|
236
|
+
// The declared decision carries the raw row and the resolved state.
|
|
237
|
+
assert.strictEqual(decision.record?.rule, "reference-is-readable");
|
|
238
|
+
assert.deepStrictEqual(decision.record?.references, [
|
|
239
|
+
{ kind: "path", path: "src/a.ts", state: "readable" },
|
|
240
|
+
]);
|
|
241
|
+
}),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
it.effect("a lost declared reference is reported and the classifier decides", () =>
|
|
245
|
+
Effect.gen(function* () {
|
|
246
|
+
const spy = registrySpy(0.9);
|
|
247
|
+
const decision = yield* decide({
|
|
248
|
+
footprint: { ...emptyFootprint(), deleted: [{ path: "src/a.ts" }] },
|
|
249
|
+
probe: probeOf({}),
|
|
250
|
+
spy,
|
|
251
|
+
candidate: candidate({ refs: [{ kind: "path", path: "src/a.ts" }] }),
|
|
252
|
+
});
|
|
253
|
+
assert.strictEqual(spy.calls(), 1);
|
|
254
|
+
assert.strictEqual(decision.kind, "keep");
|
|
255
|
+
}),
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
it.effect("after a restore, a declared reference abstains and the classifier decides", () =>
|
|
259
|
+
Effect.gen(function* () {
|
|
260
|
+
const spy = registrySpy(0.9);
|
|
261
|
+
const decision = yield* decide({
|
|
262
|
+
footprint: emptyFootprint(),
|
|
263
|
+
probe: probeOf({ "src/a.ts": "export {};" }),
|
|
264
|
+
spy,
|
|
265
|
+
candidate: candidate({ refs: [{ kind: "path", path: "src/a.ts" }] }),
|
|
266
|
+
});
|
|
267
|
+
assert.strictEqual(spy.calls(), 1);
|
|
268
|
+
assert.strictEqual(decision.kind, "keep");
|
|
269
|
+
}),
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
it.effect("after a restore, a declared product resolves by disk existence", () =>
|
|
273
|
+
Effect.gen(function* () {
|
|
274
|
+
const spy = registrySpy(0.9);
|
|
275
|
+
const decision = yield* decide({
|
|
276
|
+
footprint: emptyFootprint(),
|
|
277
|
+
probe: probeOf({ "docs/plan.md": "# Plan" }),
|
|
278
|
+
spy,
|
|
279
|
+
candidate: candidate({ produces: [{ path: "docs/plan.md" }] }),
|
|
280
|
+
});
|
|
281
|
+
assert.strictEqual(spy.calls(), 1);
|
|
282
|
+
assert.strictEqual(decision.kind, "keep");
|
|
283
|
+
}),
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
it.effect("a declared symbol found in two workspace files abstains", () =>
|
|
287
|
+
Effect.gen(function* () {
|
|
288
|
+
const spy = registrySpy(0.9);
|
|
289
|
+
const decision = yield* decide({
|
|
290
|
+
footprint: { ...emptyFootprint(), read: ["src/a.ts"] },
|
|
291
|
+
probe: probeOf({ "src/a.ts": "nothing", "src/b.ts": "route()", "src/c.ts": "route()" }),
|
|
292
|
+
spy,
|
|
293
|
+
candidate: candidate({ refs: [{ kind: "path", path: "src/a.ts", symbol: "route" }] }),
|
|
294
|
+
});
|
|
295
|
+
assert.strictEqual(spy.calls(), 1);
|
|
296
|
+
assert.strictEqual(decision.kind, "keep");
|
|
297
|
+
}),
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
it.effect("a row outside the accepted set keeps the classifier in the loop", () =>
|
|
301
|
+
Effect.gen(function* () {
|
|
302
|
+
const spy = registrySpy(0.1);
|
|
303
|
+
const decision = yield* decide({
|
|
304
|
+
footprint: { ...emptyFootprint(), read: ["src/a.ts"] },
|
|
305
|
+
probe: probeOf({ "src/a.ts": "export {};" }),
|
|
306
|
+
spy,
|
|
307
|
+
candidate: candidate({ refs: [{ kind: "path", path: "src/a.ts" }] }),
|
|
308
|
+
});
|
|
309
|
+
assert.strictEqual(spy.calls(), 1);
|
|
310
|
+
assertCompacts(decision);
|
|
311
|
+
}),
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
it.effect("a disabled setting keeps the context without a classifier call", () =>
|
|
315
|
+
Effect.gen(function* () {
|
|
316
|
+
const spy = registrySpy(0.1);
|
|
317
|
+
const first = item({
|
|
318
|
+
id: 1,
|
|
319
|
+
title: "first slice",
|
|
320
|
+
description: "the completed work",
|
|
321
|
+
done: true,
|
|
322
|
+
});
|
|
323
|
+
const before = stateOf(first, candidate(), false);
|
|
324
|
+
const after = stateOf(first, candidate(), true);
|
|
325
|
+
const decision = yield* Effect.gen(function* () {
|
|
326
|
+
const observer = yield* CompletionObserver;
|
|
327
|
+
yield* observer.record(before, after);
|
|
328
|
+
const decider = yield* SettleDecider;
|
|
329
|
+
return yield* decider.decide({ registry: spy.registry, outcome: "completed" });
|
|
330
|
+
}).pipe(
|
|
331
|
+
Effect.provide(
|
|
332
|
+
SettleDecider.layer(probeOf({})).pipe(
|
|
333
|
+
Layer.provideMerge(
|
|
334
|
+
Layer.mergeAll(
|
|
335
|
+
CompletionObserver.layer,
|
|
336
|
+
ClassifierGateway.layer,
|
|
337
|
+
footprintLayer(emptyFootprint()),
|
|
338
|
+
settingsLayer({ enabled: false }),
|
|
339
|
+
),
|
|
340
|
+
),
|
|
341
|
+
),
|
|
342
|
+
),
|
|
343
|
+
);
|
|
344
|
+
assert.strictEqual(spy.calls(), 0);
|
|
345
|
+
assert.strictEqual(decision.kind, "keep");
|
|
346
|
+
}),
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
// The product state must really be `exists`, so the restore test cannot pass
|
|
350
|
+
// by accident; `resolveCandidates` reads the disk for it.
|
|
351
|
+
it("a product that exists on disk resolves to exists and grants no compact row", () => {
|
|
352
|
+
const candidate: CompactionCandidate = {
|
|
353
|
+
class: "ready-queue",
|
|
354
|
+
listName: "Work",
|
|
355
|
+
id: 2,
|
|
356
|
+
text: "second step",
|
|
357
|
+
completed: [],
|
|
358
|
+
produces: [{ path: "docs/plan.md" }],
|
|
359
|
+
};
|
|
360
|
+
const facts = resolveCandidates(
|
|
361
|
+
[candidate],
|
|
362
|
+
emptyFootprint(),
|
|
363
|
+
probeOf({ "docs/plan.md": "# Plan" }),
|
|
364
|
+
)[0]!;
|
|
365
|
+
assert.deepStrictEqual(facts.products, [{ product: { path: "docs/plan.md" }, state: "exists" }]);
|
|
366
|
+
assert.deepStrictEqual(
|
|
367
|
+
acceptedRoute(buildRuleInput(facts, productionFacts([], emptyFootprint(), candidate.text))),
|
|
368
|
+
{ verdict: "abstain", rule: "declarations-unresolved" },
|
|
369
|
+
);
|
|
370
|
+
});
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { Context, Effect, Layer, Option } from "effect";
|
|
2
|
+
import { formatItemRef } from "../core/deps.ts";
|
|
3
|
+
import { ClassifierGateway, type ClassifierRegistry } from "./classifier.ts";
|
|
4
|
+
import { selectCandidates } from "./candidate.ts";
|
|
5
|
+
import { CompletionObserver } from "./completion.ts";
|
|
6
|
+
import { buildDigests, compactQuestions } from "./digest.ts";
|
|
7
|
+
import { buildDecisionRecord, type DecisionRecord } from "./decision-record.ts";
|
|
8
|
+
import { FootprintRecorder } from "./footprint.ts";
|
|
9
|
+
import { productionFacts, type FileProbe } from "./observations.ts";
|
|
10
|
+
import { resumePointer } from "./pointer.ts";
|
|
11
|
+
import { acceptedRoute, buildRuleInput, resolveCandidates, route } from "./rules.ts";
|
|
12
|
+
import { SmartCompactionSettingsService } from "./settings.ts";
|
|
13
|
+
|
|
14
|
+
/** How the run that just reached the settle boundary ended. */
|
|
15
|
+
export type SettleOutcome = "completed" | "aborted" | "error";
|
|
16
|
+
|
|
17
|
+
const SETTLE_DECISION = {
|
|
18
|
+
keep: { kind: "keep" } as { readonly kind: "keep" },
|
|
19
|
+
compact: (pointer: string): { readonly kind: "compact"; readonly pointer: string } => ({
|
|
20
|
+
kind: "compact",
|
|
21
|
+
pointer,
|
|
22
|
+
}),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** What the bridge should do with the run's context. */
|
|
26
|
+
export type SettleDecision =
|
|
27
|
+
| { readonly kind: "keep"; readonly record?: DecisionRecord }
|
|
28
|
+
| {
|
|
29
|
+
readonly kind: "compact";
|
|
30
|
+
readonly pointer: string;
|
|
31
|
+
readonly record?: DecisionRecord;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The settle decision. It consumes the completion record first, so every
|
|
36
|
+
* terminal path clears it and one completion set yields at most one decision,
|
|
37
|
+
* then requires a completed run before it classifies anything. It judges the
|
|
38
|
+
* whole ready frontier and never fails: every failure path keeps the context.
|
|
39
|
+
*
|
|
40
|
+
* The reliance rule table runs before the classifier. A row in `ACCEPTED_RULES`
|
|
41
|
+
* decides on its own. Every other row reports its name and hands the frontier
|
|
42
|
+
* to the classifier, so an unmeasured row can never compact alone.
|
|
43
|
+
*/
|
|
44
|
+
export class SettleDecider extends Context.Service<
|
|
45
|
+
SettleDecider,
|
|
46
|
+
{
|
|
47
|
+
readonly decide: (input: {
|
|
48
|
+
readonly registry: ClassifierRegistry;
|
|
49
|
+
readonly outcome: SettleOutcome;
|
|
50
|
+
readonly signal?: AbortSignal | undefined;
|
|
51
|
+
}) => Effect.Effect<SettleDecision>;
|
|
52
|
+
}
|
|
53
|
+
>()("tracker/SettleDecider") {
|
|
54
|
+
static readonly layer = (
|
|
55
|
+
probe: FileProbe,
|
|
56
|
+
): Layer.Layer<
|
|
57
|
+
SettleDecider,
|
|
58
|
+
never,
|
|
59
|
+
CompletionObserver | SmartCompactionSettingsService | ClassifierGateway | FootprintRecorder
|
|
60
|
+
> =>
|
|
61
|
+
Layer.effect(
|
|
62
|
+
SettleDecider,
|
|
63
|
+
Effect.gen(function* () {
|
|
64
|
+
const pending = yield* CompletionObserver;
|
|
65
|
+
const settings = yield* SmartCompactionSettingsService;
|
|
66
|
+
const gateway = yield* ClassifierGateway;
|
|
67
|
+
const recorder = yield* FootprintRecorder;
|
|
68
|
+
|
|
69
|
+
const decide = Effect.fn("SettleDecider.decide")(
|
|
70
|
+
function* (input: {
|
|
71
|
+
readonly registry: ClassifierRegistry;
|
|
72
|
+
readonly outcome: SettleOutcome;
|
|
73
|
+
readonly signal?: AbortSignal | undefined;
|
|
74
|
+
}) {
|
|
75
|
+
// Consume first: nothing below may run twice for one completion, and
|
|
76
|
+
// every path from here clears both records. The footprint is
|
|
77
|
+
// in-memory, so it goes with the completion that consumed it.
|
|
78
|
+
const recorded = yield* pending.consume;
|
|
79
|
+
const footprint = yield* recorder.consume;
|
|
80
|
+
if (Option.isNone(recorded)) return SETTLE_DECISION.keep;
|
|
81
|
+
// An aborted or errored run compacts nothing and produces no pointer.
|
|
82
|
+
if (input.outcome !== "completed") return SETTLE_DECISION.keep;
|
|
83
|
+
const values = yield* settings.load;
|
|
84
|
+
// A disabled feature compacts nothing, whatever the rules say.
|
|
85
|
+
if (!values.enabled) return SETTLE_DECISION.keep;
|
|
86
|
+
// The whole ready frontier is judged, capped by the setting. The
|
|
87
|
+
// first entry is the pointer target; the array is empty when nothing
|
|
88
|
+
// is ready.
|
|
89
|
+
const candidates = selectCandidates(recorded.value, values.maxCandidates);
|
|
90
|
+
if (candidates.length === 0) return SETTLE_DECISION.keep;
|
|
91
|
+
|
|
92
|
+
// The rule table reads the pointer candidate's declarations and the
|
|
93
|
+
// batch's production facts. An accepted row decides the whole
|
|
94
|
+
// frontier; every other row reports and asks the classifier.
|
|
95
|
+
const head = candidates[0]!;
|
|
96
|
+
const facts = resolveCandidates(candidates, footprint, probe);
|
|
97
|
+
const batchTexts = head.completed.flatMap((item) =>
|
|
98
|
+
item.description === undefined ? [item.text] : [item.text, item.description],
|
|
99
|
+
);
|
|
100
|
+
const ruleInput = buildRuleInput(
|
|
101
|
+
facts[0]!,
|
|
102
|
+
productionFacts(batchTexts, footprint, head.text),
|
|
103
|
+
);
|
|
104
|
+
const raw = route(ruleInput);
|
|
105
|
+
const routed = acceptedRoute(ruleInput);
|
|
106
|
+
// The record names the raw row, so a declared row's live coverage is
|
|
107
|
+
// measurable without a second model call. It exists only when the
|
|
108
|
+
// candidate declared something.
|
|
109
|
+
const record = buildDecisionRecord({
|
|
110
|
+
rule: raw.rule,
|
|
111
|
+
verdict: raw.verdict,
|
|
112
|
+
candidateRef: formatItemRef(head.listName, head.id),
|
|
113
|
+
references: facts[0]!.references,
|
|
114
|
+
products: facts[0]!.products,
|
|
115
|
+
});
|
|
116
|
+
const withRecord = (decision: SettleDecision): SettleDecision =>
|
|
117
|
+
record === undefined ? decision : { ...decision, record };
|
|
118
|
+
if (routed.verdict === "keep") return withRecord(SETTLE_DECISION.keep);
|
|
119
|
+
if (routed.verdict === "compact") {
|
|
120
|
+
return withRecord(SETTLE_DECISION.compact(resumePointer(head)));
|
|
121
|
+
}
|
|
122
|
+
yield* Effect.logInfo(
|
|
123
|
+
`tracker: smart-compaction rule ${routed.rule} is observe-only for ${formatItemRef(head.listName, head.id)}; asking the classifier`,
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const verdict = yield* gateway.verdict({
|
|
127
|
+
registry: input.registry,
|
|
128
|
+
enabled: values.enabled,
|
|
129
|
+
chosen: values.chosen,
|
|
130
|
+
needsContextProbabilityThreshold: values.needsContextProbabilityThreshold,
|
|
131
|
+
minAnswerConfidence: values.minAnswerConfidence,
|
|
132
|
+
digest: buildDigests(candidates, facts),
|
|
133
|
+
questions: compactQuestions(candidates),
|
|
134
|
+
signal: input.signal,
|
|
135
|
+
});
|
|
136
|
+
return verdict.kind === "compact"
|
|
137
|
+
? withRecord(SETTLE_DECISION.compact(resumePointer(head)))
|
|
138
|
+
: withRecord(SETTLE_DECISION.keep);
|
|
139
|
+
},
|
|
140
|
+
Effect.catchCause(() =>
|
|
141
|
+
Effect.logWarning("tracker: smart-compaction decision failed, keeping context").pipe(
|
|
142
|
+
Effect.as(SETTLE_DECISION.keep),
|
|
143
|
+
),
|
|
144
|
+
),
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
return SettleDecider.of({ decide });
|
|
148
|
+
}),
|
|
149
|
+
);
|
|
150
|
+
}
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
decodeStateEffect,
|
|
5
5
|
emptyState,
|
|
6
6
|
encodeState,
|
|
7
|
+
migrateState,
|
|
7
8
|
TodoItem,
|
|
8
9
|
TodoList,
|
|
9
10
|
TrackerState,
|
|
@@ -29,15 +30,15 @@ const sampleState = (): TrackerState =>
|
|
|
29
30
|
name: "Work",
|
|
30
31
|
nextItemId: 3,
|
|
31
32
|
items: [
|
|
32
|
-
new TodoItem({ id: 1,
|
|
33
|
-
new TodoItem({ id: 2,
|
|
33
|
+
new TodoItem({ id: 1, title: "write plan", done: true }),
|
|
34
|
+
new TodoItem({ id: 2, title: "implement tracker", done: false }),
|
|
34
35
|
],
|
|
35
36
|
}),
|
|
36
37
|
new TodoList({
|
|
37
38
|
id: 2,
|
|
38
39
|
name: "Home",
|
|
39
40
|
nextItemId: 2,
|
|
40
|
-
items: [new TodoItem({ id: 1,
|
|
41
|
+
items: [new TodoItem({ id: 1, title: "water plants", done: false })],
|
|
41
42
|
}),
|
|
42
43
|
],
|
|
43
44
|
activeListId: 1,
|
|
@@ -73,8 +74,8 @@ it("item ids and deps round-trip", () => {
|
|
|
73
74
|
name: "Work",
|
|
74
75
|
nextItemId: 4,
|
|
75
76
|
items: [
|
|
76
|
-
new TodoItem({ id: 1,
|
|
77
|
-
new TodoItem({ id: 3,
|
|
77
|
+
new TodoItem({ id: 1, title: "a", done: true }),
|
|
78
|
+
new TodoItem({ id: 3, title: "b", done: false, deps: ["Work:1"] }),
|
|
78
79
|
],
|
|
79
80
|
}),
|
|
80
81
|
],
|
|
@@ -118,8 +119,8 @@ it("decoded values are real class instances", () => {
|
|
|
118
119
|
it("constructors validate their input", () => {
|
|
119
120
|
// Invalid types / missing fields throw at construction time. (The invalid
|
|
120
121
|
// inputs are cast because the constructor signature is already typed.)
|
|
121
|
-
assert.throws(() => new TodoItem({
|
|
122
|
-
assert.throws(() => new TodoItem({
|
|
122
|
+
assert.throws(() => new TodoItem({ title: 42, done: false } as never));
|
|
123
|
+
assert.throws(() => new TodoItem({ title: "x" } as never));
|
|
123
124
|
assert.throws(() => new TrackerState({} as never));
|
|
124
125
|
});
|
|
125
126
|
|
|
@@ -157,7 +158,7 @@ it("rejects a snapshot with the wrong field type", () => {
|
|
|
157
158
|
|
|
158
159
|
it("rejects a snapshot with an invalid item field", () => {
|
|
159
160
|
const bad = {
|
|
160
|
-
lists: [{ id: 1, name: "Work", items: [{
|
|
161
|
+
lists: [{ id: 1, name: "Work", items: [{ title: "x", done: "yes" }] }],
|
|
161
162
|
activeListId: null,
|
|
162
163
|
nextListId: 2,
|
|
163
164
|
};
|
|
@@ -176,7 +177,39 @@ it("drops a legacy string item id so old sessions stay loadable", () => {
|
|
|
176
177
|
nextItemId: 9,
|
|
177
178
|
};
|
|
178
179
|
const decoded = decodeOrThrow(legacy);
|
|
179
|
-
assert.deepStrictEqual(decoded.lists[0]?.items, [new TodoItem({
|
|
180
|
+
assert.deepStrictEqual(decoded.lists[0]?.items, [new TodoItem({ title: "x", done: false })]);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("splits a legacy text item into a title and a description", () => {
|
|
184
|
+
const legacy = {
|
|
185
|
+
lists: [
|
|
186
|
+
{
|
|
187
|
+
id: 1,
|
|
188
|
+
name: "Work",
|
|
189
|
+
items: [{ id: 1, text: "Short title\nMore detail", done: false, deps: [] }],
|
|
190
|
+
},
|
|
191
|
+
],
|
|
192
|
+
activeListId: null,
|
|
193
|
+
nextListId: 2,
|
|
194
|
+
};
|
|
195
|
+
const migrated = migrateState(decodeOrThrow(legacy));
|
|
196
|
+
const item = migrated.lists[0]!.items[0]!;
|
|
197
|
+
assert.strictEqual(item.title, "Short title");
|
|
198
|
+
assert.strictEqual(item.description, "More detail");
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("a single-line legacy text item becomes a title with an empty description", () => {
|
|
202
|
+
const legacy = {
|
|
203
|
+
lists: [
|
|
204
|
+
{ id: 1, name: "Work", items: [{ id: 1, text: "Just a title", done: false, deps: [] }] },
|
|
205
|
+
],
|
|
206
|
+
activeListId: null,
|
|
207
|
+
nextListId: 2,
|
|
208
|
+
};
|
|
209
|
+
const migrated = migrateState(decodeOrThrow(legacy));
|
|
210
|
+
const item = migrated.lists[0]!.items[0]!;
|
|
211
|
+
assert.strictEqual(item.title, "Just a title");
|
|
212
|
+
assert.strictEqual(item.description, "");
|
|
180
213
|
});
|
|
181
214
|
|
|
182
215
|
it("enforces Int list ids (fractional ids are rejected)", () => {
|