@ftrdotdev/pi-tracker 0.6.0 → 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 +109 -60
- package/package.json +8 -3
- package/src/compaction/candidate.test.ts +9 -6
- package/src/compaction/candidate.ts +32 -2
- package/src/compaction/classifier.test.ts +67 -60
- package/src/compaction/classifier.ts +24 -22
- package/src/compaction/completion.test.ts +127 -49
- package/src/compaction/completion.ts +1 -3
- package/src/compaction/decision-record.test.ts +91 -0
- package/src/compaction/decision-record.ts +101 -0
- package/src/compaction/digest.test.ts +104 -9
- package/src/compaction/digest.ts +96 -9
- 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/rules.test.ts +253 -0
- package/src/compaction/rules.ts +188 -0
- package/src/compaction/settings.test.ts +105 -49
- package/src/compaction/settings.ts +53 -31
- package/src/compaction/settle.test.ts +370 -0
- package/src/compaction/settle.ts +109 -53
- package/src/core/deps.test.ts +1 -1
- package/src/core/domain.test.ts +42 -9
- package/src/core/domain.ts +73 -8
- package/src/core/persistence.test.ts +7 -7
- package/src/core/store.test.ts +116 -50
- package/src/core/store.ts +92 -31
- package/src/index.test.ts +172 -68
- package/src/index.ts +148 -46
- package/src/presentation/tool-metadata.test.ts +263 -48
- package/src/presentation/tool-metadata.ts +325 -102
- package/src/presentation/ui.test.ts +60 -6
- package/src/presentation/ui.ts +33 -15
|
@@ -63,18 +63,19 @@ export const PREFERRED_CLASSIFIERS: readonly ClassifierIdentity[] = [
|
|
|
63
63
|
];
|
|
64
64
|
|
|
65
65
|
/**
|
|
66
|
-
* The default
|
|
67
|
-
* the prior context. A user overrides it with
|
|
68
|
-
* `smartCompaction.
|
|
69
|
-
*
|
|
66
|
+
* The default `P(needs-context)` at or above which the candidate is judged to
|
|
67
|
+
* need the prior context. A user overrides it with
|
|
68
|
+
* `smartCompaction.needsContextProbabilityThreshold`. Raise it to compact more
|
|
69
|
+
* often.
|
|
70
70
|
*/
|
|
71
|
-
export const
|
|
71
|
+
export const DEFAULT_NEEDS_CONTEXT_PROBABILITY_THRESHOLD = 0.5;
|
|
72
72
|
|
|
73
73
|
/**
|
|
74
|
-
* The default
|
|
75
|
-
* is kept. A user overrides it with
|
|
74
|
+
* The default floor on the answer's self-reported `confidence`. A lower answer
|
|
75
|
+
* is distrusted and the context is kept. A user overrides it with
|
|
76
|
+
* `smartCompaction.minAnswerConfidence`. Raise it to keep context more often.
|
|
76
77
|
*/
|
|
77
|
-
export const
|
|
78
|
+
export const DEFAULT_MIN_ANSWER_CONFIDENCE = 0.5;
|
|
78
79
|
|
|
79
80
|
/** The `choice` label that means the candidate needs the completed context. */
|
|
80
81
|
export const NEEDS_CONTEXT_LABEL = "needs-context";
|
|
@@ -104,28 +105,29 @@ export const resolveClassifier = (
|
|
|
104
105
|
};
|
|
105
106
|
|
|
106
107
|
/**
|
|
107
|
-
* The verdict over every judged candidate. Keep context when any candidate
|
|
108
|
-
* needs
|
|
109
|
-
*
|
|
110
|
-
*
|
|
108
|
+
* The verdict over every judged candidate. Keep context when any candidate's
|
|
109
|
+
* `P(needs-context)` reaches `needsContextProbabilityThreshold`, or when an
|
|
110
|
+
* answer is below the `minAnswerConfidence` floor. Compact only when every
|
|
111
|
+
* answer is above the floor and self-contained. A non-stop or malformed answer,
|
|
112
|
+
* or an empty question set, keeps the context.
|
|
111
113
|
*/
|
|
112
114
|
export const classifyVerdict = (
|
|
113
115
|
result: ClassifierResult,
|
|
114
116
|
questionKeys: readonly string[],
|
|
115
|
-
|
|
116
|
-
|
|
117
|
+
needsContextProbabilityThreshold: number,
|
|
118
|
+
minAnswerConfidence: number,
|
|
117
119
|
): ClassificationVerdict => {
|
|
118
120
|
if (result.stopReason !== "stop") return CLASSIFICATION_VERDICT.disabled;
|
|
119
121
|
if (questionKeys.length === 0) return CLASSIFICATION_VERDICT.disabled;
|
|
120
122
|
for (const questionKey of questionKeys) {
|
|
121
123
|
const answer = result.answers[questionKey];
|
|
122
124
|
if (answer === undefined || answer.type !== "choice") return CLASSIFICATION_VERDICT.disabled;
|
|
123
|
-
if ((answer.probabilities[NEEDS_CONTEXT_LABEL] ?? 0) >=
|
|
125
|
+
if ((answer.probabilities[NEEDS_CONTEXT_LABEL] ?? 0) >= needsContextProbabilityThreshold) {
|
|
124
126
|
return CLASSIFICATION_VERDICT.keep;
|
|
125
127
|
}
|
|
126
128
|
// A provider that omits or mangles confidence keeps the context.
|
|
127
129
|
const confidence = Number.isFinite(answer.confidence) ? answer.confidence : 0;
|
|
128
|
-
if (confidence <
|
|
130
|
+
if (confidence < minAnswerConfidence) return CLASSIFICATION_VERDICT.keep;
|
|
129
131
|
}
|
|
130
132
|
return CLASSIFICATION_VERDICT.compact;
|
|
131
133
|
};
|
|
@@ -138,8 +140,8 @@ export class ClassifierGateway extends Context.Service<
|
|
|
138
140
|
readonly registry: ClassifierRegistry;
|
|
139
141
|
readonly enabled: boolean;
|
|
140
142
|
readonly chosen: Option.Option<ClassifierIdentity>;
|
|
141
|
-
readonly
|
|
142
|
-
readonly
|
|
143
|
+
readonly needsContextProbabilityThreshold: number;
|
|
144
|
+
readonly minAnswerConfidence: number;
|
|
143
145
|
readonly digest: JsonObject;
|
|
144
146
|
readonly questions: readonly CompactionQuestion[];
|
|
145
147
|
readonly signal?: AbortSignal | undefined;
|
|
@@ -151,8 +153,8 @@ export class ClassifierGateway extends Context.Service<
|
|
|
151
153
|
readonly registry: ClassifierRegistry;
|
|
152
154
|
readonly enabled: boolean;
|
|
153
155
|
readonly chosen: Option.Option<ClassifierIdentity>;
|
|
154
|
-
readonly
|
|
155
|
-
readonly
|
|
156
|
+
readonly needsContextProbabilityThreshold: number;
|
|
157
|
+
readonly minAnswerConfidence: number;
|
|
156
158
|
readonly digest: JsonObject;
|
|
157
159
|
readonly questions: readonly CompactionQuestion[];
|
|
158
160
|
readonly signal?: AbortSignal | undefined;
|
|
@@ -187,8 +189,8 @@ export class ClassifierGateway extends Context.Service<
|
|
|
187
189
|
: classifyVerdict(
|
|
188
190
|
result.value,
|
|
189
191
|
input.questions.map((question) => question.key),
|
|
190
|
-
input.
|
|
191
|
-
input.
|
|
192
|
+
input.needsContextProbabilityThreshold,
|
|
193
|
+
input.minAnswerConfidence,
|
|
192
194
|
);
|
|
193
195
|
});
|
|
194
196
|
return ClassifierGateway.of({ verdict });
|
|
@@ -14,7 +14,13 @@ const list = (id: number, name: string, items: readonly ItemSpec[]): TodoList =>
|
|
|
14
14
|
id,
|
|
15
15
|
name,
|
|
16
16
|
items: items.map(
|
|
17
|
-
(item) =>
|
|
17
|
+
(item) =>
|
|
18
|
+
new TodoItem({
|
|
19
|
+
id: item.id,
|
|
20
|
+
title: `item ${item.id}`,
|
|
21
|
+
done: item.done,
|
|
22
|
+
deps: [...(item.deps ?? [])],
|
|
23
|
+
}),
|
|
18
24
|
),
|
|
19
25
|
nextItemId: items.reduce((max, item) => Math.max(max, item.id), 0) + 1,
|
|
20
26
|
});
|
|
@@ -30,8 +36,24 @@ const state = (
|
|
|
30
36
|
// ---------------------------------------------------------------------------
|
|
31
37
|
|
|
32
38
|
it("completedInActiveList returns every item that transitioned from open to done, in list order", () => {
|
|
33
|
-
const before = state(
|
|
34
|
-
|
|
39
|
+
const before = state(
|
|
40
|
+
[
|
|
41
|
+
list(1, "Work", [
|
|
42
|
+
{ id: 1, done: false },
|
|
43
|
+
{ id: 3, done: false },
|
|
44
|
+
]),
|
|
45
|
+
],
|
|
46
|
+
1,
|
|
47
|
+
);
|
|
48
|
+
const after = state(
|
|
49
|
+
[
|
|
50
|
+
list(1, "Work", [
|
|
51
|
+
{ id: 1, done: true },
|
|
52
|
+
{ id: 3, done: true },
|
|
53
|
+
]),
|
|
54
|
+
],
|
|
55
|
+
1,
|
|
56
|
+
);
|
|
35
57
|
assert.deepStrictEqual(completedInActiveList(before, after), [1, 3]);
|
|
36
58
|
});
|
|
37
59
|
|
|
@@ -71,9 +93,33 @@ it.effect("CompletionObserver record folds successive mutations into one batch",
|
|
|
71
93
|
withObserver(
|
|
72
94
|
Effect.gen(function* () {
|
|
73
95
|
const observer = yield* CompletionObserver;
|
|
74
|
-
const s0 = state(
|
|
75
|
-
|
|
76
|
-
|
|
96
|
+
const s0 = state(
|
|
97
|
+
[
|
|
98
|
+
list(1, "Work", [
|
|
99
|
+
{ id: 1, done: false },
|
|
100
|
+
{ id: 3, done: false },
|
|
101
|
+
]),
|
|
102
|
+
],
|
|
103
|
+
1,
|
|
104
|
+
);
|
|
105
|
+
const s1 = state(
|
|
106
|
+
[
|
|
107
|
+
list(1, "Work", [
|
|
108
|
+
{ id: 1, done: true },
|
|
109
|
+
{ id: 3, done: false },
|
|
110
|
+
]),
|
|
111
|
+
],
|
|
112
|
+
1,
|
|
113
|
+
);
|
|
114
|
+
const s2 = state(
|
|
115
|
+
[
|
|
116
|
+
list(1, "Work", [
|
|
117
|
+
{ id: 1, done: true },
|
|
118
|
+
{ id: 3, done: true },
|
|
119
|
+
]),
|
|
120
|
+
],
|
|
121
|
+
1,
|
|
122
|
+
);
|
|
77
123
|
yield* observer.record(s0, s1);
|
|
78
124
|
yield* observer.record(s1, s2);
|
|
79
125
|
const batch = Option.getOrThrow(yield* observer.consume);
|
|
@@ -86,23 +132,33 @@ it.effect("CompletionObserver record folds successive mutations into one batch",
|
|
|
86
132
|
),
|
|
87
133
|
);
|
|
88
134
|
|
|
89
|
-
it.effect(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
135
|
+
it.effect(
|
|
136
|
+
"CompletionObserver record refreshes the snapshot when a later mutation completes nothing",
|
|
137
|
+
() =>
|
|
138
|
+
withObserver(
|
|
139
|
+
Effect.gen(function* () {
|
|
140
|
+
const observer = yield* CompletionObserver;
|
|
141
|
+
const s0 = state([list(1, "Work", [{ id: 1, done: false }])], 1);
|
|
142
|
+
const s1 = state([list(1, "Work", [{ id: 1, done: true }])], 1);
|
|
143
|
+
const s2 = state(
|
|
144
|
+
[
|
|
145
|
+
list(1, "Work", [
|
|
146
|
+
{ id: 1, done: true },
|
|
147
|
+
{ id: 2, done: false },
|
|
148
|
+
]),
|
|
149
|
+
],
|
|
150
|
+
1,
|
|
151
|
+
);
|
|
152
|
+
yield* observer.record(s0, s1);
|
|
153
|
+
yield* observer.record(s1, s2);
|
|
154
|
+
const batch = Option.getOrThrow(yield* observer.consume);
|
|
155
|
+
assert.deepStrictEqual(batch.completedItemIds, [1]);
|
|
156
|
+
assert.deepStrictEqual(
|
|
157
|
+
batch.list.items.map((item) => item.id),
|
|
158
|
+
[1, 2],
|
|
159
|
+
);
|
|
160
|
+
}),
|
|
161
|
+
),
|
|
106
162
|
);
|
|
107
163
|
|
|
108
164
|
it.effect("CompletionObserver record drops a completed item that was reopened before settle", () =>
|
|
@@ -133,34 +189,56 @@ it.effect("CompletionObserver record drops a completed item that was removed bef
|
|
|
133
189
|
),
|
|
134
190
|
);
|
|
135
191
|
|
|
136
|
-
it.effect(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
192
|
+
it.effect(
|
|
193
|
+
"CompletionObserver record clears the batch when the active list changes without a completion",
|
|
194
|
+
() =>
|
|
195
|
+
withObserver(
|
|
196
|
+
Effect.gen(function* () {
|
|
197
|
+
const observer = yield* CompletionObserver;
|
|
198
|
+
const s0 = state(
|
|
199
|
+
[list(1, "Work", [{ id: 1, done: false }]), list(2, "Other", [{ id: 1, done: false }])],
|
|
200
|
+
1,
|
|
201
|
+
);
|
|
202
|
+
const s1 = state(
|
|
203
|
+
[list(1, "Work", [{ id: 1, done: true }]), list(2, "Other", [{ id: 1, done: false }])],
|
|
204
|
+
1,
|
|
205
|
+
);
|
|
206
|
+
const s2 = state(
|
|
207
|
+
[list(1, "Work", [{ id: 1, done: true }]), list(2, "Other", [{ id: 1, done: false }])],
|
|
208
|
+
2,
|
|
209
|
+
);
|
|
210
|
+
yield* observer.record(s0, s1);
|
|
211
|
+
yield* observer.record(s1, s2);
|
|
212
|
+
assert.strictEqual(Option.isNone(yield* observer.consume), true);
|
|
213
|
+
}),
|
|
214
|
+
),
|
|
148
215
|
);
|
|
149
216
|
|
|
150
|
-
it.effect(
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
217
|
+
it.effect(
|
|
218
|
+
"CompletionObserver record starts a fresh batch when a completion arrives in a different active list",
|
|
219
|
+
() =>
|
|
220
|
+
withObserver(
|
|
221
|
+
Effect.gen(function* () {
|
|
222
|
+
const observer = yield* CompletionObserver;
|
|
223
|
+
const s0 = state(
|
|
224
|
+
[list(1, "Work", [{ id: 1, done: false }]), list(2, "Other", [{ id: 1, done: false }])],
|
|
225
|
+
1,
|
|
226
|
+
);
|
|
227
|
+
const s1 = state(
|
|
228
|
+
[list(1, "Work", [{ id: 1, done: true }]), list(2, "Other", [{ id: 1, done: false }])],
|
|
229
|
+
1,
|
|
230
|
+
);
|
|
231
|
+
const s2 = state(
|
|
232
|
+
[list(1, "Work", [{ id: 1, done: true }]), list(2, "Other", [{ id: 1, done: true }])],
|
|
233
|
+
2,
|
|
234
|
+
);
|
|
235
|
+
yield* observer.record(s0, s1);
|
|
236
|
+
yield* observer.record(s1, s2);
|
|
237
|
+
const batch = Option.getOrThrow(yield* observer.consume);
|
|
238
|
+
assert.deepStrictEqual(batch.completedItemIds, [1]);
|
|
239
|
+
assert.strictEqual(batch.list.name, "Other");
|
|
240
|
+
}),
|
|
241
|
+
),
|
|
164
242
|
);
|
|
165
243
|
|
|
166
244
|
it.effect("CompletionObserver consume clears the record", () =>
|
|
@@ -63,9 +63,7 @@ const mergeLiveBatch = (
|
|
|
63
63
|
const completedItemIds = list.items
|
|
64
64
|
.filter((item) => item.done && tracked.has(item.id))
|
|
65
65
|
.map((item) => item.id);
|
|
66
|
-
return completedItemIds.length === 0
|
|
67
|
-
? Option.none()
|
|
68
|
-
: Option.some({ list, completedItemIds });
|
|
66
|
+
return completedItemIds.length === 0 ? Option.none() : Option.some({ list, completedItemIds });
|
|
69
67
|
};
|
|
70
68
|
|
|
71
69
|
export class CompletionObserver extends Context.Service<
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import {
|
|
4
|
+
DECISION_CUSTOM_TYPE,
|
|
5
|
+
buildDecisionRecord,
|
|
6
|
+
decodeDecisionRecord,
|
|
7
|
+
encodeDecisionRecord,
|
|
8
|
+
type DecisionRecord,
|
|
9
|
+
} from "./decision-record.ts";
|
|
10
|
+
import type { ResolvedProduct, ResolvedReference } from "./observations.ts";
|
|
11
|
+
|
|
12
|
+
const reference = (
|
|
13
|
+
value: ResolvedReference["reference"],
|
|
14
|
+
state: ResolvedReference["state"],
|
|
15
|
+
): ResolvedReference => ({ reference: value, state });
|
|
16
|
+
|
|
17
|
+
const product = (path: string, state: ResolvedProduct["state"]): ResolvedProduct => ({
|
|
18
|
+
product: { path },
|
|
19
|
+
state,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const input = (overrides: {
|
|
23
|
+
readonly references?: readonly ResolvedReference[];
|
|
24
|
+
readonly products?: readonly ResolvedProduct[];
|
|
25
|
+
}): Parameters<typeof buildDecisionRecord>[0] => ({
|
|
26
|
+
rule: "reference-is-readable",
|
|
27
|
+
verdict: "compact",
|
|
28
|
+
candidateRef: "Work:2",
|
|
29
|
+
references: overrides.references ?? [],
|
|
30
|
+
products: overrides.products ?? [],
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("the custom-entry type is the reliance-decision carrier", () => {
|
|
34
|
+
assert.strictEqual(DECISION_CUSTOM_TYPE, "tracker/reliance-decision");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("a decision records the fired row and the resolved states", () => {
|
|
38
|
+
const record = buildDecisionRecord(
|
|
39
|
+
input({
|
|
40
|
+
references: [reference({ kind: "path", path: "src/a.ts", symbol: "route" }, "readable")],
|
|
41
|
+
products: [product("docs/plan.md", "missing")],
|
|
42
|
+
}),
|
|
43
|
+
)!;
|
|
44
|
+
assert.deepStrictEqual(record, {
|
|
45
|
+
rule: "reference-is-readable",
|
|
46
|
+
verdict: "compact",
|
|
47
|
+
candidateRef: "Work:2",
|
|
48
|
+
references: [{ kind: "path", path: "src/a.ts", symbol: "route", state: "readable" }],
|
|
49
|
+
products: [{ path: "docs/plan.md", state: "missing" }],
|
|
50
|
+
});
|
|
51
|
+
assert.notInclude(JSON.stringify(record), "transcript");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("a decision with no declarations records no decision entry", () => {
|
|
55
|
+
assert.isUndefined(buildDecisionRecord(input({})));
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("a decision reference rides as a topic, not as an archived claim", () => {
|
|
59
|
+
const record = buildDecisionRecord(
|
|
60
|
+
input({ references: [reference({ kind: "decision", topic: "storage shape" }, "unresolved")] }),
|
|
61
|
+
)!;
|
|
62
|
+
assert.deepStrictEqual(record.references, [
|
|
63
|
+
{ kind: "decision", topic: "storage shape", state: "unresolved" },
|
|
64
|
+
]);
|
|
65
|
+
assert.notInclude(JSON.stringify(record), "archived");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("a record round-trips through the codec", () => {
|
|
69
|
+
const record = buildDecisionRecord(
|
|
70
|
+
input({ references: [reference({ kind: "path", path: "src/a.ts" }, "drifted")] }),
|
|
71
|
+
)!;
|
|
72
|
+
const decoded = Effect.runSync(decodeDecisionRecord(encodeDecisionRecord(record)));
|
|
73
|
+
assert.deepStrictEqual(decoded as DecisionRecord, record);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("a record caps references at eight and products at four", () => {
|
|
77
|
+
const references = Array.from({ length: 10 }, (_value, index) =>
|
|
78
|
+
reference({ kind: "path", path: `src/f${index}.ts` }, "readable"),
|
|
79
|
+
);
|
|
80
|
+
const products = Array.from({ length: 6 }, (_value, index) =>
|
|
81
|
+
product(`src/p${index}.ts`, "exists"),
|
|
82
|
+
);
|
|
83
|
+
const record = buildDecisionRecord(input({ references, products }))!;
|
|
84
|
+
assert.strictEqual(record.references.length, 8);
|
|
85
|
+
assert.strictEqual(record.products.length, 4);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("a malformed record fails to decode", () => {
|
|
89
|
+
const exit = Effect.runSync(Effect.exit(decodeDecisionRecord({ rule: 42 })));
|
|
90
|
+
assert.strictEqual(exit._tag, "Failure");
|
|
91
|
+
});
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { REFERENCE_CAP, PRODUCT_CAP } from "./digest.ts";
|
|
3
|
+
import type { ResolvedProduct, ResolvedReference } from "./observations.ts";
|
|
4
|
+
import type { RuleVerdict } from "./rules.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The live decision record.
|
|
8
|
+
*
|
|
9
|
+
* One compact custom session entry per settle decision, appended only when the
|
|
10
|
+
* candidate declared a reference or a product. It carries the raw rule row,
|
|
11
|
+
* the raw verdict, the candidate reference, and the resolved states, all
|
|
12
|
+
* bounded by the digest's caps. Nothing else from the decision enters: no
|
|
13
|
+
* transcript text, no batch prose, no item body beyond the reference.
|
|
14
|
+
*
|
|
15
|
+
* The eval harvest reads this entry back into its case, so a declared row's
|
|
16
|
+
* real coverage can be measured without a second classifier call.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** The custom-entry type the bridge appends for one settle decision. */
|
|
20
|
+
export const DECISION_CUSTOM_TYPE = "tracker/reliance-decision";
|
|
21
|
+
|
|
22
|
+
/** Every declared reference state the record may carry. */
|
|
23
|
+
const referenceState = Schema.Literals(["readable", "lost", "drifted", "unresolved"]);
|
|
24
|
+
|
|
25
|
+
/** Every declared product state the record may carry. */
|
|
26
|
+
const productState = Schema.Literals(["exists", "missing"]);
|
|
27
|
+
|
|
28
|
+
/** One resolved reference, flattened to the fields its kind owns. */
|
|
29
|
+
const recordReference = Schema.Struct({
|
|
30
|
+
kind: Schema.Literals(["path", "decision"]),
|
|
31
|
+
path: Schema.optional(Schema.String),
|
|
32
|
+
symbol: Schema.optional(Schema.String),
|
|
33
|
+
span: Schema.optional(Schema.String),
|
|
34
|
+
topic: Schema.optional(Schema.String),
|
|
35
|
+
state: referenceState,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
/** One resolved product. */
|
|
39
|
+
const recordProduct = Schema.Struct({
|
|
40
|
+
path: Schema.String,
|
|
41
|
+
state: productState,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/** The stored shape of one decision record. */
|
|
45
|
+
export const decisionRecordSchema = Schema.Struct({
|
|
46
|
+
rule: Schema.String,
|
|
47
|
+
verdict: Schema.Literals(["keep", "compact", "abstain"]),
|
|
48
|
+
candidateRef: Schema.String,
|
|
49
|
+
references: Schema.Array(recordReference),
|
|
50
|
+
products: Schema.Array(recordProduct),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export type DecisionRecord = Schema.Schema.Type<typeof decisionRecordSchema>;
|
|
54
|
+
|
|
55
|
+
const referenceOf = (entry: ResolvedReference): DecisionRecord["references"][number] =>
|
|
56
|
+
entry.reference.kind === "decision"
|
|
57
|
+
? { kind: "decision", topic: entry.reference.topic, state: entry.state }
|
|
58
|
+
: {
|
|
59
|
+
kind: "path",
|
|
60
|
+
path: entry.reference.path,
|
|
61
|
+
...(entry.reference.symbol === undefined ? {} : { symbol: entry.reference.symbol }),
|
|
62
|
+
...(entry.reference.span === undefined ? {} : { span: entry.reference.span }),
|
|
63
|
+
state: entry.state,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Build the record for one decision, or undefined when the candidate declared
|
|
68
|
+
* nothing. The caps match the digest, so the entry stays compact.
|
|
69
|
+
*/
|
|
70
|
+
export const buildDecisionRecord = (input: {
|
|
71
|
+
readonly rule: string;
|
|
72
|
+
readonly verdict: RuleVerdict;
|
|
73
|
+
readonly candidateRef: string;
|
|
74
|
+
readonly references: readonly ResolvedReference[];
|
|
75
|
+
readonly products: readonly ResolvedProduct[];
|
|
76
|
+
}): DecisionRecord | undefined => {
|
|
77
|
+
if (input.references.length === 0 && input.products.length === 0) return undefined;
|
|
78
|
+
return {
|
|
79
|
+
rule: input.rule,
|
|
80
|
+
verdict: input.verdict,
|
|
81
|
+
candidateRef: input.candidateRef,
|
|
82
|
+
references: input.references.slice(0, REFERENCE_CAP).map(referenceOf),
|
|
83
|
+
products: input.products.slice(0, PRODUCT_CAP).map((entry) => ({
|
|
84
|
+
path: entry.product.path,
|
|
85
|
+
state: entry.state,
|
|
86
|
+
})),
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/** Encode a record into its plain JSON shape, throwing on a malformed value. */
|
|
91
|
+
export const encodeDecisionRecord = (record: DecisionRecord): unknown =>
|
|
92
|
+
Schema.encodeSync(decisionRecordSchema)(record);
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Decode an untrusted session entry into a record. Unknown extra keys are
|
|
96
|
+
* ignored, so a newer writer does not break an older reader.
|
|
97
|
+
*/
|
|
98
|
+
export const decodeDecisionRecord = (
|
|
99
|
+
snapshot: unknown,
|
|
100
|
+
): Effect.Effect<DecisionRecord, Schema.SchemaError> =>
|
|
101
|
+
Schema.decodeUnknownEffect(decisionRecordSchema, { onExcessProperty: "ignore" })(snapshot);
|
|
@@ -2,15 +2,30 @@ import { assert, it } from "@effect/vitest";
|
|
|
2
2
|
import { NEEDS_CONTEXT_LABEL, NEEDS_NONE_LABEL } from "./classifier.ts";
|
|
3
3
|
import type { CompactionCandidate } from "./candidate.ts";
|
|
4
4
|
import {
|
|
5
|
+
DESCRIPTION_LIMIT,
|
|
6
|
+
PRODUCT_CAP,
|
|
5
7
|
QUESTION_KEY_PREFIX,
|
|
8
|
+
REFERENCE_CAP,
|
|
6
9
|
buildDigests,
|
|
7
10
|
compactQuestions,
|
|
8
11
|
questionKey,
|
|
9
12
|
} from "./digest.ts";
|
|
13
|
+
import type { ResolvedProduct, ResolvedReference } from "./observations.ts";
|
|
10
14
|
|
|
11
15
|
interface DigestShape {
|
|
12
|
-
candidates: Array<{
|
|
13
|
-
|
|
16
|
+
candidates: Array<{
|
|
17
|
+
ref: string;
|
|
18
|
+
text: string;
|
|
19
|
+
description?: string;
|
|
20
|
+
class: string;
|
|
21
|
+
relationship: string;
|
|
22
|
+
references: Array<Record<string, unknown>>;
|
|
23
|
+
products: Array<Record<string, unknown>>;
|
|
24
|
+
referenceCut?: string;
|
|
25
|
+
productCut?: string;
|
|
26
|
+
}>;
|
|
27
|
+
completed: Array<{ ref: string; text: string; description?: string }>;
|
|
28
|
+
survival: string;
|
|
14
29
|
}
|
|
15
30
|
|
|
16
31
|
const dependent: CompactionCandidate = {
|
|
@@ -29,24 +44,104 @@ const readyQueue: CompactionCandidate = {
|
|
|
29
44
|
completed: [{ ref: "Work:1", text: "first" }],
|
|
30
45
|
};
|
|
31
46
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
47
|
+
const noFacts: readonly { references: []; products: [] }[] = [];
|
|
48
|
+
|
|
49
|
+
it("buildDigests carries only the candidates, the completed batch, and the survival instruction", () => {
|
|
50
|
+
const digest = buildDigests([dependent], noFacts);
|
|
51
|
+
assert.deepStrictEqual(Object.keys(digest).sort(), ["candidates", "completed", "survival"]);
|
|
35
52
|
});
|
|
36
53
|
|
|
37
54
|
it("buildDigests lists one entry per candidate with its class and relationship", () => {
|
|
38
|
-
const digest = buildDigests([dependent, readyQueue]) as unknown as DigestShape;
|
|
55
|
+
const digest = buildDigests([dependent, readyQueue], noFacts) as unknown as DigestShape;
|
|
39
56
|
assert.deepStrictEqual(digest.candidates, [
|
|
40
|
-
{
|
|
41
|
-
|
|
57
|
+
{
|
|
58
|
+
ref: "Work:2",
|
|
59
|
+
text: "second",
|
|
60
|
+
class: "dependent-successor",
|
|
61
|
+
relationship: "depends on Work:1",
|
|
62
|
+
references: [],
|
|
63
|
+
products: [],
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
ref: "Work:3",
|
|
67
|
+
text: "third",
|
|
68
|
+
class: "ready-queue",
|
|
69
|
+
relationship: "independent of Work:1",
|
|
70
|
+
references: [],
|
|
71
|
+
products: [],
|
|
72
|
+
},
|
|
42
73
|
]);
|
|
43
74
|
});
|
|
44
75
|
|
|
45
76
|
it("buildDigests carries the completed batch once as prior work", () => {
|
|
46
|
-
const digest = buildDigests([dependent, readyQueue]) as unknown as DigestShape;
|
|
77
|
+
const digest = buildDigests([dependent, readyQueue], noFacts) as unknown as DigestShape;
|
|
47
78
|
assert.deepStrictEqual(digest.completed, [{ ref: "Work:1", text: "first" }]);
|
|
48
79
|
});
|
|
49
80
|
|
|
81
|
+
// ---------------------------------------------------------------------------
|
|
82
|
+
// Slice 6: descriptions, declarations, and the survival instruction
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
it("buildDigests carries a long description as its first paragraph with a named cut", () => {
|
|
86
|
+
const first = "a".repeat(DESCRIPTION_LIMIT + 50);
|
|
87
|
+
const candidate: CompactionCandidate = {
|
|
88
|
+
...readyQueue,
|
|
89
|
+
description: `${first}\n\nsecond paragraph`,
|
|
90
|
+
};
|
|
91
|
+
const digest = buildDigests([candidate], noFacts) as unknown as DigestShape;
|
|
92
|
+
const description = digest.candidates[0]!.description!;
|
|
93
|
+
assert.strictEqual(description, `${"a".repeat(DESCRIPTION_LIMIT)}… [50 more characters]`);
|
|
94
|
+
assert.notInclude(description, "second paragraph");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("buildDigests carries a description that is already one short paragraph unchanged", () => {
|
|
98
|
+
const candidate: CompactionCandidate = { ...readyQueue, description: "one line" };
|
|
99
|
+
const digest = buildDigests([candidate], noFacts) as unknown as DigestShape;
|
|
100
|
+
assert.strictEqual(digest.candidates[0]!.description, "one line");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("buildDigests caps references at eight and products at four with a marker", () => {
|
|
104
|
+
const refs = Array.from({ length: 10 }, (_value, index) => ({
|
|
105
|
+
kind: "path" as const,
|
|
106
|
+
path: `src/f${index}.ts`,
|
|
107
|
+
}));
|
|
108
|
+
const produces = Array.from({ length: 6 }, (_value, index) => ({ path: `src/p${index}.ts` }));
|
|
109
|
+
const references: ResolvedReference[] = refs.map((reference) => ({
|
|
110
|
+
reference,
|
|
111
|
+
state: "readable",
|
|
112
|
+
}));
|
|
113
|
+
const products: ResolvedProduct[] = produces.map((product) => ({ product, state: "exists" }));
|
|
114
|
+
const digest = buildDigests(
|
|
115
|
+
[{ ...readyQueue, refs, produces }],
|
|
116
|
+
[{ references, products }],
|
|
117
|
+
) as unknown as DigestShape;
|
|
118
|
+
const candidate = digest.candidates[0]!;
|
|
119
|
+
assert.strictEqual(candidate.references.length, REFERENCE_CAP);
|
|
120
|
+
assert.strictEqual(candidate.products.length, PRODUCT_CAP);
|
|
121
|
+
assert.strictEqual(candidate.referenceCut, "… [2 more references declared]");
|
|
122
|
+
assert.strictEqual(candidate.productCut, "… [2 more products declared]");
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("buildDigests carries a declared decision as a fact and never asserts it is archived", () => {
|
|
126
|
+
const refs = [{ kind: "decision" as const, topic: "storage shape" }];
|
|
127
|
+
const digest = buildDigests(
|
|
128
|
+
[{ ...readyQueue, refs }],
|
|
129
|
+
[{ references: [{ reference: refs[0]!, state: "unresolved" }], products: [] }],
|
|
130
|
+
) as unknown as DigestShape;
|
|
131
|
+
assert.deepStrictEqual(digest.candidates[0]!.references, [
|
|
132
|
+
{ kind: "decision", topic: "storage shape", state: "unresolved" },
|
|
133
|
+
]);
|
|
134
|
+
assert.notInclude(JSON.stringify(digest), "archived");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("buildDigests carries the survival instruction and no transcript text", () => {
|
|
138
|
+
const digest = buildDigests([dependent], noFacts) as unknown as DigestShape;
|
|
139
|
+
assert.include(digest.survival, "transcript");
|
|
140
|
+
assert.include(digest.survival, "description");
|
|
141
|
+
assert.notInclude(JSON.stringify(digest), "assistant said");
|
|
142
|
+
assert.notInclude(JSON.stringify(digest), "tool-call");
|
|
143
|
+
});
|
|
144
|
+
|
|
50
145
|
it("compactQuestions keys each question by its candidate reference and index", () => {
|
|
51
146
|
const questions = compactQuestions([dependent, readyQueue]);
|
|
52
147
|
assert.deepStrictEqual(
|