@ftrdotdev/pi-tracker 0.7.0 → 0.9.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.
@@ -106,7 +106,7 @@ export class SmartCompactionSettingsService extends Context.Service<
106
106
  /** Never fails: every read, parse, or shape problem yields the defaults. */
107
107
  readonly load: Effect.Effect<SmartCompactionSettings>;
108
108
  }
109
- >()("tracker/SmartCompactionSettings") {
109
+ >()("@ftrdotdev/pi-tracker/compaction/settings/SmartCompactionSettingsService") {
110
110
  static make(
111
111
  path: string,
112
112
  ): Layer.Layer<SmartCompactionSettingsService, never, FileSystem.FileSystem> {
@@ -1,3 +1,6 @@
1
+ // Each test is its own entry point: it builds a fresh layer for one scenario and
2
+ // runs its effects inside it, so providing there is the pattern the rule allows.
3
+ /* oxlint-disable effecttsgo/strict-effect-provide */
1
4
  import { assert, it } from "@effect/vitest";
2
5
  import { Effect, Layer } from "effect";
3
6
  import type { ClassifierApi, ClassifierModel, ClassifierResult } from "@earendil-works/pi-ai";
@@ -5,7 +8,7 @@ import { NEEDS_CONTEXT_LABEL, NEEDS_NONE_LABEL } from "./classifier.ts";
5
8
  import { ClassifierGateway, type ClassifierRegistry } from "./classifier.ts";
6
9
  import type { CompactionCandidate } from "./candidate.ts";
7
10
  import { CompletionObserver } from "./completion.ts";
8
- import { FootprintRecorder } from "./footprint.ts";
11
+ import { FootprintRecorder, type ToolEvent } from "./footprint.ts";
9
12
  import {
10
13
  containsSymbol,
11
14
  emptyFootprint,
@@ -90,7 +93,10 @@ const footprintLayer = (footprint: BatchFootprint): Layer.Layer<FootprintRecorde
90
93
  Layer.succeed(
91
94
  FootprintRecorder,
92
95
  FootprintRecorder.of({
93
- capture: () => Effect.void,
96
+ // Untraced: a test stub needs no span.
97
+ capture: Effect.fnUntraced(function* (_event: ToolEvent): Effect.fn.Return<void> {
98
+ yield* Effect.void;
99
+ }),
94
100
  snapshot: Effect.succeed(footprint),
95
101
  consume: Effect.succeed(footprint),
96
102
  }),
@@ -104,7 +110,7 @@ const item = (spec: {
104
110
  readonly refs?: TodoItem["refs"];
105
111
  readonly produces?: TodoItem["produces"];
106
112
  }): TodoItem =>
107
- new TodoItem({
113
+ TodoItem.make({
108
114
  id: spec.id,
109
115
  title: spec.title,
110
116
  description: spec.description ?? "",
@@ -114,16 +120,26 @@ const item = (spec: {
114
120
  produces: [...(spec.produces ?? [])],
115
121
  });
116
122
 
123
+ /** A copy of `item` with only `done` changed. Fields stay explicit so the class
124
+ * prototype survives; spreading the instance would drop it. */
125
+ const withDone = (item: TodoItem, done: boolean): TodoItem =>
126
+ TodoItem.make({
127
+ id: item.id,
128
+ title: item.title,
129
+ description: item.description,
130
+ refs: item.refs,
131
+ produces: item.produces,
132
+ deps: item.deps,
133
+ done,
134
+ });
135
+
117
136
  const stateOf = (first: TodoItem, candidate: TodoItem, firstDone: boolean): TrackerState =>
118
- new TrackerState({
137
+ TrackerState.make({
119
138
  lists: [
120
- new TodoList({
139
+ TodoList.make({
121
140
  id: 1,
122
141
  name: "Work",
123
- items: [
124
- new TodoItem({ ...first, done: firstDone }),
125
- new TodoItem({ ...candidate, done: false }),
126
- ],
142
+ items: [withDone(first, firstDone), withDone(candidate, false)],
127
143
  nextItemId: 3,
128
144
  }),
129
145
  ],
@@ -140,6 +156,7 @@ const decide = (input: {
140
156
  readonly probe: FileProbe;
141
157
  readonly spy: RegistrySpy;
142
158
  readonly candidate: TodoItem;
159
+ readonly signal?: AbortSignal | undefined;
143
160
  }): Effect.Effect<SettleDecision> => {
144
161
  const first = item({
145
162
  id: 1,
@@ -153,7 +170,11 @@ const decide = (input: {
153
170
  const observer = yield* CompletionObserver;
154
171
  yield* observer.record(before, after);
155
172
  const decider = yield* SettleDecider;
156
- return yield* decider.decide({ registry: input.spy.registry, outcome: "completed" });
173
+ return yield* decider.decide({
174
+ registry: input.spy.registry,
175
+ outcome: "completed",
176
+ ...(input.signal === undefined ? {} : { signal: input.signal }),
177
+ });
157
178
  }).pipe(
158
179
  Effect.provide(
159
180
  SettleDecider.layer(input.probe).pipe(
@@ -218,6 +239,25 @@ it.effect("a candidate that names a batch output compacts", () =>
218
239
  }),
219
240
  );
220
241
 
242
+ it.effect("the classifier decides a lost reference while the run signal is live", () => {
243
+ // Constructed outside the Effect program so the run signal is a plain value.
244
+ const signal = new AbortController().signal;
245
+ return Effect.gen(function* () {
246
+ const spy = registrySpy(0.1);
247
+ const decision = yield* decide({
248
+ footprint: { ...emptyFootprint(), deleted: [{ path: "src/a.ts" }] },
249
+ probe: probeOf({}),
250
+ spy,
251
+ signal,
252
+ candidate: candidate({ refs: [{ kind: "path", path: "src/a.ts" }] }),
253
+ });
254
+ // A live signal is not an abort: the gateway must still answer, and the
255
+ // answer must reach the decider. Before the fix this path always kept.
256
+ assert.strictEqual(spy.calls(), 1);
257
+ assertCompacts(decision);
258
+ });
259
+ });
260
+
221
261
  // ---------------------------------------------------------------------------
222
262
  // Observe-only rows keep the classifier in the loop
223
263
  // ---------------------------------------------------------------------------
@@ -50,7 +50,7 @@ export class SettleDecider extends Context.Service<
50
50
  readonly signal?: AbortSignal | undefined;
51
51
  }) => Effect.Effect<SettleDecision>;
52
52
  }
53
- >()("tracker/SettleDecider") {
53
+ >()("@ftrdotdev/pi-tracker/compaction/settle/SettleDecider") {
54
54
  static readonly layer = (
55
55
  probe: FileProbe,
56
56
  ): Layer.Layer<
@@ -119,10 +119,10 @@ export class SettleDecider extends Context.Service<
119
119
  if (routed.verdict === "compact") {
120
120
  return withRecord(SETTLE_DECISION.compact(resumePointer(head)));
121
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
-
122
+ // An observe-only row is reported through the decision record, not
123
+ // the console: Effect's default logger writes to stdout, which pi's
124
+ // TUI redirects to stderr, and the differential renderer never
125
+ // repaints those rows, so a log line would stick in the editor area.
126
126
  const verdict = yield* gateway.verdict({
127
127
  registry: input.registry,
128
128
  enabled: values.enabled,
@@ -1,14 +1,24 @@
1
1
  import { assert, it } from "@effect/vitest";
2
+ import { PROPERTY_RUNS } from "@ftrdotdev/pi-test-kit";
3
+ import {
4
+ acyclicListArbitrary,
5
+ cyclicListArbitrary,
6
+ graphListArbitrary,
7
+ refusedReferenceArbitrary,
8
+ referenceArbitrary,
9
+ unlinkedListArbitrary,
10
+ } from "../../test-support/generators.ts";
2
11
  import { TodoItem, TodoList } from "./domain.ts";
3
12
  import {
4
13
  blockersOf,
5
14
  cyclePath,
6
15
  dependentsOf,
7
16
  firstReadyIndex,
17
+ formatItemRef,
8
18
  orderedItems,
9
19
  parseItemRef,
10
20
  readiness,
11
- unsatisfiedDeps,
21
+ resolveRef,
12
22
  } from "./deps.ts";
13
23
 
14
24
  /**
@@ -21,18 +31,17 @@ const listWith = (
21
31
  done: readonly number[] = [],
22
32
  name = "Work",
23
33
  ): TodoList =>
24
- new TodoList({
34
+ TodoList.make({
25
35
  id: 1,
26
36
  name,
27
37
  nextItemId: deps.length + 1,
28
- items: deps.map(
29
- (itemDeps, index) =>
30
- new TodoItem({
31
- id: index + 1,
32
- title: `item ${index + 1}`,
33
- done: done.includes(index),
34
- deps: itemDeps,
35
- }),
38
+ items: deps.map((itemDeps, index) =>
39
+ TodoItem.make({
40
+ id: index + 1,
41
+ title: `item ${index + 1}`,
42
+ done: done.includes(index),
43
+ deps: itemDeps,
44
+ }),
36
45
  ),
37
46
  });
38
47
 
@@ -46,9 +55,26 @@ it("parseItemRef returns null for a malformed reference", () => {
46
55
  }
47
56
  });
48
57
 
49
- it("parseItemRef splits on the last colon so list names may contain colons", () => {
50
- assert.deepStrictEqual(parseItemRef("My:List:3"), { name: "My:List", id: 3 });
51
- });
58
+ it.prop(
59
+ "an item reference round-trips through format and parse, whatever the list name",
60
+ { reference: referenceArbitrary },
61
+ ({ reference }) => {
62
+ assert.deepStrictEqual(parseItemRef(formatItemRef(reference.name, reference.id)), {
63
+ name: reference.name,
64
+ id: reference.id,
65
+ });
66
+ },
67
+ { arbitrary: { runs: PROPERTY_RUNS } },
68
+ );
69
+
70
+ it.prop(
71
+ "parseItemRef refuses a reference whose id is not a positive integer",
72
+ { reference: refusedReferenceArbitrary },
73
+ ({ reference }) => {
74
+ assert.strictEqual(parseItemRef(`${reference.name}:${reference.idText}`), null);
75
+ },
76
+ { arbitrary: { runs: PROPERTY_RUNS } },
77
+ );
52
78
 
53
79
  it("cyclePath returns null for a candidate that keeps the graph acyclic", () => {
54
80
  // Item 2 depends on item 1, so adding "item 3 depends on item 2" is fine.
@@ -107,26 +133,58 @@ it("cyclePath ignores a dependency naming another list", () => {
107
133
  // Readiness (derived on read, never stored)
108
134
  // --------------------------------------------------------------------------
109
135
 
110
- it("an item with no dependencies is ready when open", () => {
111
- const entries = readiness(listWith([[], []]));
112
-
113
- assert.deepStrictEqual(
114
- entries.map((entry) => entry.ready),
115
- [true, true],
116
- );
117
- assert.deepStrictEqual(
118
- entries.map((entry) => entry.blockers),
119
- [[], []],
120
- );
121
- });
122
-
123
- it("a dependency blocks its dependent until it is done", () => {
124
- const entries = readiness(listWith([[], ["Work:1"]]));
136
+ /** The dependency refs of one item whose target is absent or not done. */
137
+ const openDeps = (list: TodoList, index: number): readonly string[] =>
138
+ (list.items[index]?.deps ?? []).filter((ref) => {
139
+ const target = resolveRef(list, ref);
140
+ return target === undefined || !(list.items[target]?.done ?? false);
141
+ });
125
142
 
126
- assert.strictEqual(entries[0]?.ready, true);
127
- assert.strictEqual(entries[1]?.ready, false);
128
- assert.deepStrictEqual(entries[1]?.blockers, ["Work:1"]);
129
- });
143
+ it.prop(
144
+ "readiness marks an item ready exactly when it is open and every dependency is done",
145
+ { list: graphListArbitrary },
146
+ ({ list }) => {
147
+ const entries = readiness(list);
148
+ for (const [index, item] of list.items.entries()) {
149
+ assert.strictEqual(entries[index]!.ready, !item.done && openDeps(list, index).length === 0);
150
+ }
151
+ },
152
+ { arbitrary: { runs: PROPERTY_RUNS } },
153
+ );
154
+
155
+ it.prop(
156
+ "readiness reports exactly the open dependencies as blockers",
157
+ { list: graphListArbitrary },
158
+ ({ list }) => {
159
+ const entries = readiness(list);
160
+ for (const index of list.items.keys()) {
161
+ assert.deepStrictEqual(entries[index]!.blockers, openDeps(list, index));
162
+ }
163
+ },
164
+ { arbitrary: { runs: PROPERTY_RUNS } },
165
+ );
166
+
167
+ it.prop(
168
+ "blockersOf reports nothing for a done item",
169
+ { list: graphListArbitrary },
170
+ ({ list }) => {
171
+ for (const [index, item] of list.items.entries()) {
172
+ if (item.done) assert.deepStrictEqual(blockersOf(list, index), []);
173
+ }
174
+ },
175
+ { arbitrary: { runs: PROPERTY_RUNS } },
176
+ );
177
+
178
+ it.prop(
179
+ "firstReadyIndex is the first ready index, or undefined when nothing is ready",
180
+ { list: graphListArbitrary },
181
+ ({ list }) => {
182
+ const entries = readiness(list);
183
+ const expected = list.items.findIndex((_, index) => entries[index]!.ready);
184
+ assert.strictEqual(firstReadyIndex(list), expected === -1 ? undefined : expected);
185
+ },
186
+ { arbitrary: { runs: PROPERTY_RUNS } },
187
+ );
130
188
 
131
189
  it("a done item reports the dependencies that are still open", () => {
132
190
  // The state a reopen creates: item 2 is done while its prerequisite is open.
@@ -143,56 +201,6 @@ it("a done item reports the dependencies that are still open", () => {
143
201
  assert.deepStrictEqual(satisfied[1]?.blockers, []);
144
202
  });
145
203
 
146
- it("blockersOf reports nothing for a done item, so completion is not gated on it", () => {
147
- // The completion gate reads `blockersOf`. A done item must not be refused a
148
- // repeat done mark, so the gate view stays empty while the reading view
149
- // (`unsatisfiedDeps`, which `readiness` uses) keeps the fact.
150
- const list = listWith([[], ["Work:1"]], [1]);
151
-
152
- assert.deepStrictEqual(blockersOf(list, 1), []);
153
- assert.deepStrictEqual(unsatisfiedDeps(list, 1), ["Work:1"]);
154
- assert.deepStrictEqual(unsatisfiedDeps(listWith([[], ["Work:1"]], []), 1), ["Work:1"]);
155
- assert.deepStrictEqual(unsatisfiedDeps(listWith([[], ["Work:1"]], [0, 1]), 1), []);
156
- });
157
-
158
- it("readiness follows a chain", () => {
159
- // Item 3 depends on item 2, which depends on item 1.
160
- const chain = listWith([[], ["Work:1"], ["Work:2"]]);
161
- assert.deepStrictEqual(
162
- readiness(chain).map((entry) => entry.ready),
163
- [true, false, false],
164
- );
165
-
166
- // Only the middle link is unblocked by completing item 1.
167
- const progressed = listWith([[], [], ["Work:2"]], [0]);
168
- assert.deepStrictEqual(
169
- readiness(progressed).map((entry) => entry.blockers),
170
- [[], [], ["Work:2"]],
171
- );
172
- });
173
-
174
- it("a diamond dependency unblocks only when both branches are done", () => {
175
- const branches = [[], [], ["Work:1", "Work:2"]];
176
-
177
- assert.deepStrictEqual(readiness(listWith(branches))[2]?.blockers, ["Work:1", "Work:2"]);
178
- assert.deepStrictEqual(readiness(listWith(branches, [0]))[2]?.blockers, ["Work:2"]);
179
-
180
- const joined = readiness(listWith(branches, [0, 1]))[2];
181
- assert.deepStrictEqual(joined?.blockers, []);
182
- assert.strictEqual(joined?.ready, true);
183
- });
184
-
185
- it("firstReadyIndex returns the earliest ready item in list order", () => {
186
- // Item 1 is done, item 2 is blocked by item 3, item 3 is ready.
187
- assert.strictEqual(firstReadyIndex(listWith([[], ["Work:3"], []], [0])), 2);
188
- // Item 1 is blocked by item 2, which is ready.
189
- assert.strictEqual(firstReadyIndex(listWith([["Work:2"], []])), 1);
190
- });
191
-
192
- it("firstReadyIndex is undefined when nothing is ready", () => {
193
- assert.strictEqual(firstReadyIndex(listWith([[], []], [0, 1])), undefined);
194
- });
195
-
196
204
  it("a dangling reference leaves its dependent blocked", () => {
197
205
  const entry = readiness(listWith([[], ["Work:9"]]))[1];
198
206
 
@@ -217,25 +225,44 @@ it("dependentsOf lists the items that depend on a given item", () => {
217
225
  const idsOf = (items: readonly { readonly id?: number }[]): number[] =>
218
226
  items.map((item) => item.id ?? 0);
219
227
 
220
- it("orderedItems returns the stored order when nothing has dependencies", () => {
221
- const list = listWith([[], [], []]);
222
-
223
- assert.deepStrictEqual(idsOf(orderedItems(list)), [1, 2, 3]);
224
- });
225
-
226
- it("orderedItems puts a dependency before its dependent", () => {
227
- // Item 1 waits for item 2.
228
- const list = listWith([["Work:2"], []]);
229
-
230
- assert.deepStrictEqual(idsOf(orderedItems(list)), [2, 1]);
231
- });
232
-
233
- it("orderedItems follows a chain", () => {
234
- // Item 3 -> item 2 -> item 1.
235
- const list = listWith([[], ["Work:1"], ["Work:2"]]);
236
-
237
- assert.deepStrictEqual(idsOf(orderedItems(list)), [1, 2, 3]);
238
- });
228
+ it.prop(
229
+ "orderedItems is a permutation of the stored items that places every dependency first",
230
+ { list: acyclicListArbitrary },
231
+ ({ list }) => {
232
+ const ordered = orderedItems(list);
233
+ const compare = (a: number, b: number): number => a - b;
234
+ assert.deepStrictEqual(idsOf(ordered).toSorted(compare), idsOf(list.items).toSorted(compare));
235
+ const position = new Map(ordered.map((item, index) => [item.id, index]));
236
+ for (const item of ordered) {
237
+ for (const ref of item.deps ?? []) {
238
+ const dependency = parseItemRef(ref);
239
+ assert.notStrictEqual(dependency, null);
240
+ assert(position.get(dependency!.id)! < position.get(item.id)!);
241
+ }
242
+ }
243
+ },
244
+ { arbitrary: { runs: PROPERTY_RUNS } },
245
+ );
246
+
247
+ it.prop(
248
+ "orderedItems returns the stored order when no item declares a dependency",
249
+ { list: unlinkedListArbitrary },
250
+ ({ list }) => {
251
+ assert.deepStrictEqual(idsOf(orderedItems(list)), idsOf(list.items));
252
+ },
253
+ { arbitrary: { runs: PROPERTY_RUNS } },
254
+ );
255
+
256
+ it.prop(
257
+ "orderedItems keeps every item when the graph has a cycle",
258
+ { list: cyclicListArbitrary },
259
+ ({ list }) => {
260
+ const ordered = orderedItems(list);
261
+ assert.strictEqual(ordered.length, list.items.length);
262
+ assert.deepStrictEqual(idsOf(ordered), idsOf(list.items));
263
+ },
264
+ { arbitrary: { runs: PROPERTY_RUNS } },
265
+ );
239
266
 
240
267
  it("orderedItems keeps the stored order among items that are ready together", () => {
241
268
  // Item 2 waits for item 1. Item 3 is ready from the start, so it keeps its
@@ -244,11 +271,3 @@ it("orderedItems keeps the stored order among items that are ready together", ()
244
271
 
245
272
  assert.deepStrictEqual(idsOf(orderedItems(list)), [1, 3, 2]);
246
273
  });
247
-
248
- it("orderedItems keeps every item when the graph has a cycle", () => {
249
- // Only a hand-edited snapshot can produce a cycle; rendering must not throw
250
- // or drop items, so the stored order stands in.
251
- const list = listWith([["Work:2"], ["Work:1"]]);
252
-
253
- assert.deepStrictEqual(idsOf(orderedItems(list)), [1, 2]);
254
- });
package/src/core/deps.ts CHANGED
@@ -178,7 +178,7 @@ export const unsatisfiedDeps = (list: DependencyList, index: number): readonly s
178
178
  * fact for a done row.
179
179
  */
180
180
  export const blockersOf = (list: DependencyList, index: number): readonly string[] =>
181
- list.items[index]?.done === true ? [] : unsatisfiedDeps(list, index);
181
+ (list.items[index]?.done ?? false) ? [] : unsatisfiedDeps(list, index);
182
182
 
183
183
  /** Derived readiness of one item. `ready` means "open and unblocked". */
184
184
  export interface ItemReadiness {
@@ -246,5 +246,6 @@ export const orderedItems = <T extends DependencyItem>(
246
246
  });
247
247
  });
248
248
  if (!Graph.isAcyclic(graph)) return list.items;
249
- return [...Graph.indices(Graph.topo(graph))].map((index) => list.items[index]!);
249
+ const order = Graph.topo(graph);
250
+ return [...Graph.indices(order)].map((index) => list.items[index]!);
250
251
  };