@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.
@@ -0,0 +1,253 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import type { CompactionCandidate } from "./candidate.ts";
3
+ import {
4
+ containsSymbol,
5
+ emptyFootprint,
6
+ productionFacts,
7
+ type BatchFootprint,
8
+ type FileProbe,
9
+ type ResolvedProduct,
10
+ type ResolvedReference,
11
+ } from "./observations.ts";
12
+ import {
13
+ acceptedRoute,
14
+ buildRuleInput,
15
+ resolveCandidates,
16
+ route,
17
+ type RuleInput,
18
+ type RuleVerdict,
19
+ } from "./rules.ts";
20
+
21
+ /** The four facts the legacy rows read. */
22
+ interface LegacyFacts {
23
+ readonly writes: boolean;
24
+ readonly reads: boolean;
25
+ readonly dependent: boolean;
26
+ readonly planDocument: boolean;
27
+ }
28
+
29
+ /** Build an input that realizes the facts, with no declarations. */
30
+ const legacyInput = (facts: LegacyFacts): RuleInput => ({
31
+ references: [],
32
+ products: [],
33
+ candidatePaths: facts.writes ? ["src/missing.ts"] : facts.reads ? ["src/produced.ts"] : [],
34
+ knownText: facts.reads ? "src/produced.ts" : "",
35
+ producedText: facts.reads ? "src/produced.ts" : "",
36
+ dependent: facts.dependent,
37
+ planDocument: facts.planDocument,
38
+ });
39
+
40
+ /**
41
+ * The frozen pre-revert rule table, kept here with its old rule ids so the
42
+ * parity test proves the rename changed no verdict. Do not edit this to match
43
+ * the new table; that would make the test circular.
44
+ */
45
+ const frozenRoute = (facts: LegacyFacts): { verdict: RuleVerdict; rule: string } => {
46
+ if (facts.writes) return { verdict: "keep", rule: "writes-missing-document" };
47
+ if (facts.reads && !facts.dependent) {
48
+ return { verdict: "compact", rule: "reads-produced-document" };
49
+ }
50
+ if (!facts.reads && !facts.dependent && !facts.planDocument) {
51
+ return { verdict: "compact", rule: "nothing-in-play" };
52
+ }
53
+ if (facts.dependent && facts.planDocument) {
54
+ return { verdict: "abstain", rule: "document-and-dependency" };
55
+ }
56
+ if (facts.dependent) return { verdict: "abstain", rule: "dependent-successor" };
57
+ return { verdict: "abstain", rule: "plan-document" };
58
+ };
59
+
60
+ /** The two renamed compact rows; every other id is unchanged. */
61
+ const RENAMED: Readonly<Record<string, string>> = {
62
+ "writes-missing-document": "names-unknown-path",
63
+ "reads-produced-document": "names-batch-output",
64
+ "nothing-in-play": "no-visible-link",
65
+ };
66
+
67
+ const reference = (
68
+ state: ResolvedReference["state"],
69
+ value: ResolvedReference["reference"] = { kind: "path", path: "src/a.ts" },
70
+ ): ResolvedReference => ({ reference: value, state });
71
+
72
+ const product = (state: ResolvedProduct["state"], path = "src/out.ts"): ResolvedProduct => ({
73
+ product: { path },
74
+ state,
75
+ });
76
+
77
+ const declaredInput = (
78
+ references: readonly ResolvedReference[],
79
+ products: readonly ResolvedProduct[] = [],
80
+ ): RuleInput => ({
81
+ references,
82
+ products,
83
+ candidatePaths: [],
84
+ knownText: "",
85
+ producedText: "",
86
+ dependent: false,
87
+ planDocument: false,
88
+ });
89
+
90
+ it("the legacy rows reproduce the frozen table on all sixteen fact combinations", () => {
91
+ for (const writes of [false, true]) {
92
+ for (const reads of [false, true]) {
93
+ for (const dependent of [false, true]) {
94
+ for (const planDocument of [false, true]) {
95
+ const facts = { writes, reads, dependent, planDocument };
96
+ const frozen = frozenRoute(facts);
97
+ const renamed = { verdict: frozen.verdict, rule: RENAMED[frozen.rule] ?? frozen.rule };
98
+ assert.deepStrictEqual(route(legacyInput(facts)), renamed, JSON.stringify(facts));
99
+ }
100
+ }
101
+ }
102
+ }
103
+ });
104
+
105
+ it("a lost reference keeps the context before any other row", () => {
106
+ const decision = route(
107
+ declaredInput([reference("lost"), reference("readable", { kind: "path", path: "src/b.ts" })]),
108
+ );
109
+ assert.deepStrictEqual(decision, { verdict: "keep", rule: "reference-is-lost" });
110
+ });
111
+
112
+ it("an unwritten plan product keeps the context; a non-plan product does not", () => {
113
+ assert.deepStrictEqual(route(declaredInput([], [product("missing", ".qrspi/plans/router.md")])), {
114
+ verdict: "keep",
115
+ rule: "product-is-unwritten-plan",
116
+ });
117
+ assert.deepStrictEqual(route(declaredInput([], [product("missing", "src/out.ts")])), {
118
+ verdict: "abstain",
119
+ rule: "declarations-unresolved",
120
+ });
121
+ });
122
+
123
+ it("a declared decision routes to the classifier", () => {
124
+ const decision = route(
125
+ declaredInput([reference("unresolved", { kind: "decision", topic: "storage shape" })]),
126
+ );
127
+ assert.deepStrictEqual(decision, { verdict: "abstain", rule: "decision-is-declared" });
128
+ });
129
+
130
+ it("a drifted reference routes to the classifier", () => {
131
+ assert.deepStrictEqual(route(declaredInput([reference("drifted")])), {
132
+ verdict: "abstain",
133
+ rule: "reference-has-drifted",
134
+ });
135
+ });
136
+
137
+ it("all readable references compact", () => {
138
+ assert.deepStrictEqual(route(declaredInput([reference("readable"), reference("readable")])), {
139
+ verdict: "compact",
140
+ rule: "reference-is-readable",
141
+ });
142
+ });
143
+
144
+ it("an unresolved reference routes to the classifier, never to a legacy compact row", () => {
145
+ const decision = route(declaredInput([reference("unresolved")]));
146
+ assert.deepStrictEqual(decision, { verdict: "abstain", rule: "reference-is-unresolved" });
147
+ });
148
+
149
+ it("a declaration without a verdict does not fall through to no-visible-link", () => {
150
+ const decision = route(declaredInput([], [product("exists", "docs/plan.md")]));
151
+ assert.deepStrictEqual(decision, { verdict: "abstain", rule: "declarations-unresolved" });
152
+ });
153
+
154
+ it("acceptedRoute lets only the gated rows decide", () => {
155
+ assert.deepStrictEqual(
156
+ acceptedRoute(
157
+ legacyInput({
158
+ writes: false,
159
+ reads: false,
160
+ dependent: false,
161
+ planDocument: false,
162
+ }),
163
+ ),
164
+ { verdict: "compact", rule: "no-visible-link" },
165
+ );
166
+ assert.deepStrictEqual(
167
+ acceptedRoute(
168
+ legacyInput({
169
+ writes: true,
170
+ reads: false,
171
+ dependent: false,
172
+ planDocument: false,
173
+ }),
174
+ ),
175
+ { verdict: "abstain", rule: "names-unknown-path" },
176
+ );
177
+ assert.deepStrictEqual(acceptedRoute(declaredInput([reference("readable")])), {
178
+ verdict: "abstain",
179
+ rule: "reference-is-readable",
180
+ });
181
+ });
182
+
183
+ // ---------------------------------------------------------------------------
184
+ // resolveCandidates and buildRuleInput
185
+ // ---------------------------------------------------------------------------
186
+
187
+ const candidateOf = (overrides: Partial<CompactionCandidate>): CompactionCandidate => ({
188
+ class: "ready-queue",
189
+ listName: "Work",
190
+ id: 2,
191
+ text: "second step",
192
+ completed: [],
193
+ ...overrides,
194
+ });
195
+
196
+ const probeOf = (files: Readonly<Record<string, string>>): FileProbe => ({
197
+ read: (path) => files[path],
198
+ findSymbol: (symbol) =>
199
+ Object.entries(files)
200
+ .filter(([, text]) => containsSymbol(text, symbol))
201
+ .map(([path]) => path),
202
+ });
203
+
204
+ const footprintWith = (overrides: Partial<BatchFootprint>): BatchFootprint => ({
205
+ ...emptyFootprint(),
206
+ ...overrides,
207
+ });
208
+
209
+ it("resolveCandidates abstains every reference without a footprint", () => {
210
+ const candidate = candidateOf({ refs: [{ kind: "path", path: "src/a.ts" }] });
211
+ const probe = probeOf({ "src/a.ts": "export {};" });
212
+ assert.deepStrictEqual(resolveCandidates([candidate], emptyFootprint(), probe)[0]?.references, [
213
+ { reference: { kind: "path", path: "src/a.ts" }, state: "unresolved" },
214
+ ]);
215
+ assert.deepStrictEqual(
216
+ resolveCandidates([candidate], footprintWith({ read: ["src/a.ts"] }), probe)[0]?.references,
217
+ [{ reference: { kind: "path", path: "src/a.ts" }, state: "readable" }],
218
+ );
219
+ });
220
+
221
+ it("resolveCandidates checks a product against disk without a footprint", () => {
222
+ const candidate = candidateOf({ produces: [{ path: "docs/plan.md" }] });
223
+ assert.deepStrictEqual(
224
+ resolveCandidates([candidate], emptyFootprint(), probeOf({ "docs/plan.md": "# Plan" }))[0]
225
+ ?.products,
226
+ [{ product: { path: "docs/plan.md" }, state: "exists" }],
227
+ );
228
+ });
229
+
230
+ it("a candidate that names a batch output routes to names-batch-output", () => {
231
+ const candidate = candidateOf({ text: "wire src/router.ts" });
232
+ const footprint = footprintWith({ wrote: [{ path: "src/router.ts", spans: [] }] });
233
+ const facts = resolveCandidates([candidate], footprint, probeOf({}))[0]!;
234
+ const production = productionFacts([], footprint, candidate.text);
235
+ assert.deepStrictEqual(acceptedRoute(buildRuleInput(facts, production)), {
236
+ verdict: "compact",
237
+ rule: "names-batch-output",
238
+ });
239
+ });
240
+
241
+ it("a restored readable reference routes to reference-is-unresolved", () => {
242
+ const candidate = candidateOf({ refs: [{ kind: "path", path: "src/a.ts" }] });
243
+ const facts = resolveCandidates(
244
+ [candidate],
245
+ emptyFootprint(),
246
+ probeOf({ "src/a.ts": "export {};" }),
247
+ )[0]!;
248
+ const production = productionFacts([], emptyFootprint(), candidate.text);
249
+ assert.deepStrictEqual(acceptedRoute(buildRuleInput(facts, production)), {
250
+ verdict: "abstain",
251
+ rule: "reference-is-unresolved",
252
+ });
253
+ });
@@ -0,0 +1,188 @@
1
+ /**
2
+ * The reliance rule table.
3
+ *
4
+ * Every row is named for the observation it tests; the verdict is separate.
5
+ * The declared rows fire only when the item carries declarations, and they come
6
+ * first. The legacy rows reproduce the pre-revert rules exactly, under honest
7
+ * names, so the corpus parity test can show the rename changed nothing.
8
+ *
9
+ * `ACCEPTED_RULES` is the product policy. A row that has not cleared the
10
+ * two-pass gate reports its raw verdict and abstains, so an unmeasured row can
11
+ * never compact on its own.
12
+ */
13
+ import type { CompactionCandidate } from "./candidate.ts";
14
+ import {
15
+ QRSPI_PLAN_DOCUMENT,
16
+ productionFacts,
17
+ resolveProducts,
18
+ resolveReferences,
19
+ type BatchFootprint,
20
+ type FileProbe,
21
+ type ResolvedProduct,
22
+ type ResolvedReference,
23
+ } from "./observations.ts";
24
+
25
+ /** What the rule layer decides for one candidate. */
26
+ export type RuleVerdict = "keep" | "compact" | "abstain";
27
+
28
+ /** Everything the rows read. Declarations may be empty; the legacy facts may not. */
29
+ export interface RuleInput {
30
+ /** Declared references, already resolved against the disk. */
31
+ readonly references: readonly ResolvedReference[];
32
+ /** Declared products, already resolved against the disk. */
33
+ readonly products: readonly ResolvedProduct[];
34
+ /** Every file path the candidate names. */
35
+ readonly candidatePaths: readonly string[];
36
+ /** The batch texts plus the work record, joined. A document named here was named. */
37
+ readonly knownText: string;
38
+ /** The `write` and `edit` call paths, joined. A path named here was produced. */
39
+ readonly producedText: string;
40
+ /** The candidate is a `dependent-successor`. */
41
+ readonly dependent: boolean;
42
+ /** The batch or the record names a plan, design, or outline document. */
43
+ readonly planDocument: boolean;
44
+ }
45
+
46
+ export interface RuleDecision {
47
+ readonly verdict: RuleVerdict;
48
+ /** The rule id, stable across the report and the product. */
49
+ readonly rule: string;
50
+ }
51
+
52
+ const hasDeclaration = (input: RuleInput): boolean =>
53
+ input.references.length > 0 || input.products.length > 0;
54
+
55
+ /** The declaration facts one candidate resolves to. */
56
+ export interface CandidateFacts {
57
+ readonly references: readonly ResolvedReference[];
58
+ readonly products: readonly ResolvedProduct[];
59
+ /** The candidate is a `dependent-successor`. */
60
+ readonly dependent: boolean;
61
+ }
62
+
63
+ /**
64
+ * Decide one candidate from the observations.
65
+ *
66
+ * The order matters. A lost reference keeps the context before anything else.
67
+ * An unwritten plan product keeps it next, because the document it will write
68
+ * needs the upstream decisions. A declared decision abstains: it is a semantic
69
+ * claim, and the classifier is the judge. A drifted or unresolved declaration
70
+ * abstains rather than compact on half a fact. Only then do the legacy rows
71
+ * run, and they run exactly as the pre-revert table did.
72
+ */
73
+ export const route = (input: RuleInput): RuleDecision => {
74
+ // --- Declared rows: they fire only when the item carries declarations. ---
75
+ if (input.references.some((entry) => entry.state === "lost")) {
76
+ return { verdict: "keep", rule: "reference-is-lost" };
77
+ }
78
+ if (
79
+ input.products.some(
80
+ (entry) => entry.state === "missing" && QRSPI_PLAN_DOCUMENT.test(entry.product.path),
81
+ )
82
+ ) {
83
+ return { verdict: "keep", rule: "product-is-unwritten-plan" };
84
+ }
85
+ if (input.references.some((entry) => entry.reference.kind === "decision")) {
86
+ return { verdict: "abstain", rule: "decision-is-declared" };
87
+ }
88
+ if (input.references.some((entry) => entry.state === "drifted")) {
89
+ return { verdict: "abstain", rule: "reference-has-drifted" };
90
+ }
91
+ if (
92
+ input.references.length > 0 &&
93
+ input.references.every((entry) => entry.state === "readable")
94
+ ) {
95
+ return { verdict: "compact", rule: "reference-is-readable" };
96
+ }
97
+ if (input.references.some((entry) => entry.state === "unresolved")) {
98
+ return { verdict: "abstain", rule: "reference-is-unresolved" };
99
+ }
100
+ if (hasDeclaration(input)) {
101
+ // A declaration exists and no row decided it. Never compact on a
102
+ // half-resolved declaration.
103
+ return { verdict: "abstain", rule: "declarations-unresolved" };
104
+ }
105
+
106
+ // --- Legacy rows: the frozen pre-revert rules, under honest names. ---
107
+ const writes = input.candidatePaths.some((path) => !input.knownText.includes(path));
108
+ const reads = input.candidatePaths.some((path) => input.producedText.includes(path));
109
+ if (writes) return { verdict: "keep", rule: "names-unknown-path" };
110
+ if (reads && !input.dependent) return { verdict: "compact", rule: "names-batch-output" };
111
+ if (!reads && !input.dependent && !input.planDocument) {
112
+ return { verdict: "compact", rule: "no-visible-link" };
113
+ }
114
+ if (input.dependent && input.planDocument) {
115
+ return { verdict: "abstain", rule: "document-and-dependency" };
116
+ }
117
+ if (input.dependent) return { verdict: "abstain", rule: "dependent-successor" };
118
+ return { verdict: "abstain", rule: "plan-document" };
119
+ };
120
+
121
+ /**
122
+ * The rows that cleared the gate and may decide without a model call. The
123
+ * measured production gate: `names-batch-output` agrees with 96.4% of pass 1
124
+ * and 100.0% of pass 2, and `no-visible-link` with 96.0% of pass 2 and 98.2%
125
+ * of pass 3. `bun run eval:promote` prints the table and rewrites this block;
126
+ * every declared row is observe-only until it clears the same gate on a corpus
127
+ * that carries declarations.
128
+ */
129
+ export const ACCEPTED_RULES: ReadonlySet<string> = new Set([
130
+ "names-batch-output",
131
+ "no-visible-link",
132
+ ]);
133
+
134
+ /**
135
+ * The product policy: a rule that cleared the gate keeps its verdict; every
136
+ * other row abstains and keeps its name for the report.
137
+ */
138
+ export const acceptedRoute = (input: RuleInput): RuleDecision => {
139
+ const decision = route(input);
140
+ return ACCEPTED_RULES.has(decision.rule) ? decision : { verdict: "abstain", rule: decision.rule };
141
+ };
142
+
143
+ /** True when the batch recorded any work at all. */
144
+ export const hasFootprint = (footprint: BatchFootprint): boolean =>
145
+ footprint.wrote.length > 0 ||
146
+ footprint.read.length > 0 ||
147
+ footprint.deleted.length > 0 ||
148
+ footprint.commands.length > 0;
149
+
150
+ /**
151
+ * Resolve each candidate's declarations against the footprint and the disk.
152
+ *
153
+ * Without a footprint there is nothing to resolve against: a session restore
154
+ * leaves the recorder empty, so every declared reference abstains and the
155
+ * classifier decides. Declared products still check disk existence, because a
156
+ * product is a claim about a file, not about the batch.
157
+ */
158
+ export const resolveCandidates = (
159
+ candidates: readonly CompactionCandidate[],
160
+ footprint: BatchFootprint,
161
+ probe: FileProbe,
162
+ ): readonly CandidateFacts[] => {
163
+ const recorded = hasFootprint(footprint);
164
+ return candidates.map((candidate) => {
165
+ const references = candidate.refs ?? [];
166
+ return {
167
+ references: recorded
168
+ ? resolveReferences(references, footprint, probe)
169
+ : references.map((reference) => ({ reference, state: "unresolved" as const })),
170
+ products: resolveProducts(candidate.produces ?? [], probe),
171
+ dependent: candidate.class === "dependent-successor",
172
+ };
173
+ });
174
+ };
175
+
176
+ /** Fold the resolved declarations and the production facts into a rule input. */
177
+ export const buildRuleInput = (
178
+ facts: CandidateFacts,
179
+ production: ReturnType<typeof productionFacts>,
180
+ ): RuleInput => ({
181
+ references: facts.references,
182
+ products: facts.products,
183
+ candidatePaths: production.candidatePaths,
184
+ knownText: production.knownText,
185
+ producedText: production.producedText,
186
+ dependent: facts.dependent,
187
+ planDocument: production.planDocument,
188
+ });
@@ -7,15 +7,20 @@ import {
7
7
  CLASSIFIER_KEY,
8
8
  DEFAULT_MAX_CANDIDATES,
9
9
  ENABLED_KEY,
10
- KEEP_CONTEXT_MIN_CONFIDENCE_KEY,
11
- KEEP_CONTEXT_THRESHOLD_KEY,
10
+ MIN_ANSWER_CONFIDENCE_KEY,
11
+ NEEDS_CONTEXT_PROBABILITY_THRESHOLD_KEY,
12
+ LEGACY_KEEP_CONTEXT_MIN_CONFIDENCE_KEY,
13
+ LEGACY_KEEP_CONTEXT_THRESHOLD_KEY,
12
14
  MAX_CANDIDATES_KEY,
13
15
  SMART_COMPACTION_KEY,
14
16
  SmartCompactionSettingsService,
15
17
  parseClassifierIdentity,
16
18
  type SmartCompactionSettings,
17
19
  } from "./settings.ts";
18
- import { DEFAULT_KEEP_CONTEXT_MIN_CONFIDENCE, DEFAULT_KEEP_CONTEXT_THRESHOLD } from "./classifier.ts";
20
+ import {
21
+ DEFAULT_MIN_ANSWER_CONFIDENCE,
22
+ DEFAULT_NEEDS_CONTEXT_PROBABILITY_THRESHOLD,
23
+ } from "./classifier.ts";
19
24
 
20
25
  const TEST_PATH = "/tmp/tracker-settings-test/tui.json";
21
26
  const FIXTURE_PATH = fileURLToPath(
@@ -61,11 +66,7 @@ function makeMemFs(init: Record<string, string> = {}): MemFs {
61
66
  return { fs, files, writes };
62
67
  }
63
68
 
64
- const load = (): Effect.Effect<
65
- SmartCompactionSettings,
66
- never,
67
- SmartCompactionSettingsService
68
- > =>
69
+ const load = (): Effect.Effect<SmartCompactionSettings, never, SmartCompactionSettingsService> =>
69
70
  Effect.gen(function* () {
70
71
  const service = yield* SmartCompactionSettingsService;
71
72
  return yield* service.load;
@@ -83,7 +84,10 @@ it.effect("load returns the fail-open defaults when the file is missing", () =>
83
84
  const settings = yield* runWithMem(mem, load());
84
85
  assert.strictEqual(settings.enabled, true);
85
86
  assert.strictEqual(Option.isNone(settings.chosen), true);
86
- assert.strictEqual(settings.keepContextThreshold, DEFAULT_KEEP_CONTEXT_THRESHOLD);
87
+ assert.strictEqual(
88
+ settings.needsContextProbabilityThreshold,
89
+ DEFAULT_NEEDS_CONTEXT_PROBABILITY_THRESHOLD,
90
+ );
87
91
  }),
88
92
  );
89
93
 
@@ -93,7 +97,10 @@ it.effect("load returns the fail-open defaults when the file is not JSON", () =>
93
97
  const settings = yield* runWithMem(mem, load());
94
98
  assert.strictEqual(settings.enabled, true);
95
99
  assert.strictEqual(Option.isNone(settings.chosen), true);
96
- assert.strictEqual(settings.keepContextThreshold, DEFAULT_KEEP_CONTEXT_THRESHOLD);
100
+ assert.strictEqual(
101
+ settings.needsContextProbabilityThreshold,
102
+ DEFAULT_NEEDS_CONTEXT_PROBABILITY_THRESHOLD,
103
+ );
97
104
  }),
98
105
  );
99
106
 
@@ -103,7 +110,10 @@ it.effect("load returns the fail-open defaults when the section has the wrong sh
103
110
  const settings = yield* runWithMem(mem, load());
104
111
  assert.strictEqual(settings.enabled, true);
105
112
  assert.strictEqual(Option.isNone(settings.chosen), true);
106
- assert.strictEqual(settings.keepContextThreshold, DEFAULT_KEEP_CONTEXT_THRESHOLD);
113
+ assert.strictEqual(
114
+ settings.needsContextProbabilityThreshold,
115
+ DEFAULT_NEEDS_CONTEXT_PROBABILITY_THRESHOLD,
116
+ );
107
117
  }),
108
118
  );
109
119
 
@@ -120,59 +130,103 @@ it.effect("load ignores an unrecognized enabled value and keeps a valid chosen c
120
130
  provider: "typesafe",
121
131
  modelId: "jev-latest",
122
132
  });
123
- assert.strictEqual(settings.keepContextThreshold, DEFAULT_KEEP_CONTEXT_THRESHOLD);
133
+ assert.strictEqual(
134
+ settings.needsContextProbabilityThreshold,
135
+ DEFAULT_NEEDS_CONTEXT_PROBABILITY_THRESHOLD,
136
+ );
124
137
  }),
125
138
  );
126
139
 
127
- it.effect("load reads a valid keep-context threshold from the section", () =>
140
+ it.effect("load reads a valid needs-context probability threshold from the section", () =>
128
141
  Effect.gen(function* () {
129
142
  const mem = makeMemFs({
130
- [TEST_PATH]: JSON.stringify({ smartCompaction: { keepContextThreshold: 0.72 } }),
143
+ [TEST_PATH]: JSON.stringify({ smartCompaction: { needsContextProbabilityThreshold: 0.72 } }),
131
144
  });
132
145
  const settings = yield* runWithMem(mem, load());
133
- assert.strictEqual(settings.keepContextThreshold, 0.72);
146
+ assert.strictEqual(settings.needsContextProbabilityThreshold, 0.72);
134
147
  }),
135
148
  );
136
149
 
137
- it.effect("load falls back alone when a threshold is outside the open unit interval", () =>
150
+ it.effect(
151
+ "load falls back alone when the probability threshold is outside the open unit interval",
152
+ () =>
153
+ Effect.gen(function* () {
154
+ for (const invalid of [0, 1, -0.1, 1.5, "0.5"]) {
155
+ const mem = makeMemFs({
156
+ [TEST_PATH]: JSON.stringify({
157
+ smartCompaction: { enabled: false, needsContextProbabilityThreshold: invalid },
158
+ }),
159
+ });
160
+ const settings = yield* runWithMem(mem, load());
161
+ assert.strictEqual(
162
+ settings.needsContextProbabilityThreshold,
163
+ DEFAULT_NEEDS_CONTEXT_PROBABILITY_THRESHOLD,
164
+ );
165
+ // The bad value must not discard the rest of the section.
166
+ assert.strictEqual(settings.enabled, false);
167
+ }
168
+ }),
169
+ );
170
+
171
+ it.effect("load reads a valid min answer confidence from the section", () =>
138
172
  Effect.gen(function* () {
139
- for (const invalid of [0, 1, -0.1, 1.5, "0.5"]) {
140
- const mem = makeMemFs({
141
- [TEST_PATH]: JSON.stringify({
142
- smartCompaction: { enabled: false, keepContextThreshold: invalid },
143
- }),
144
- });
145
- const settings = yield* runWithMem(mem, load());
146
- assert.strictEqual(settings.keepContextThreshold, DEFAULT_KEEP_CONTEXT_THRESHOLD);
147
- // The bad value must not discard the rest of the section.
148
- assert.strictEqual(settings.enabled, false);
149
- }
173
+ const mem = makeMemFs({
174
+ [TEST_PATH]: JSON.stringify({ smartCompaction: { minAnswerConfidence: 0.72 } }),
175
+ });
176
+ const settings = yield* runWithMem(mem, load());
177
+ assert.strictEqual(settings.minAnswerConfidence, 0.72);
150
178
  }),
151
179
  );
152
180
 
153
- it.effect("load reads a valid keep-context min confidence from the section", () =>
181
+ it.effect(
182
+ "load falls back alone when the min answer confidence is outside the open unit interval",
183
+ () =>
184
+ Effect.gen(function* () {
185
+ for (const invalid of [0, 1, -0.1, 1.5, "0.5"]) {
186
+ const mem = makeMemFs({
187
+ [TEST_PATH]: JSON.stringify({
188
+ smartCompaction: { enabled: false, minAnswerConfidence: invalid },
189
+ }),
190
+ });
191
+ const settings = yield* runWithMem(mem, load());
192
+ assert.strictEqual(settings.minAnswerConfidence, DEFAULT_MIN_ANSWER_CONFIDENCE);
193
+ // The bad value must not discard the rest of the section.
194
+ assert.strictEqual(settings.enabled, false);
195
+ }
196
+ }),
197
+ );
198
+
199
+ it.effect("load reads the legacy probability keys when the current keys are absent", () =>
154
200
  Effect.gen(function* () {
155
201
  const mem = makeMemFs({
156
- [TEST_PATH]: JSON.stringify({ smartCompaction: { keepContextMinConfidence: 0.72 } }),
202
+ [TEST_PATH]: JSON.stringify({
203
+ smartCompaction: {
204
+ [LEGACY_KEEP_CONTEXT_THRESHOLD_KEY]: 0.72,
205
+ [LEGACY_KEEP_CONTEXT_MIN_CONFIDENCE_KEY]: 0.31,
206
+ },
207
+ }),
157
208
  });
158
209
  const settings = yield* runWithMem(mem, load());
159
- assert.strictEqual(settings.keepContextMinConfidence, 0.72);
210
+ assert.strictEqual(settings.needsContextProbabilityThreshold, 0.72);
211
+ assert.strictEqual(settings.minAnswerConfidence, 0.31);
160
212
  }),
161
213
  );
162
214
 
163
- it.effect("load falls back alone when the min confidence is outside the open unit interval", () =>
215
+ it.effect("load prefers the current probability keys over their legacy aliases", () =>
164
216
  Effect.gen(function* () {
165
- for (const invalid of [0, 1, -0.1, 1.5, "0.5"]) {
166
- const mem = makeMemFs({
167
- [TEST_PATH]: JSON.stringify({
168
- smartCompaction: { enabled: false, keepContextMinConfidence: invalid },
169
- }),
170
- });
171
- const settings = yield* runWithMem(mem, load());
172
- assert.strictEqual(settings.keepContextMinConfidence, DEFAULT_KEEP_CONTEXT_MIN_CONFIDENCE);
173
- // The bad value must not discard the rest of the section.
174
- assert.strictEqual(settings.enabled, false);
175
- }
217
+ const mem = makeMemFs({
218
+ [TEST_PATH]: JSON.stringify({
219
+ smartCompaction: {
220
+ [NEEDS_CONTEXT_PROBABILITY_THRESHOLD_KEY]: 0.72,
221
+ [LEGACY_KEEP_CONTEXT_THRESHOLD_KEY]: 0.31,
222
+ [MIN_ANSWER_CONFIDENCE_KEY]: 0.41,
223
+ [LEGACY_KEEP_CONTEXT_MIN_CONFIDENCE_KEY]: 0.21,
224
+ },
225
+ }),
226
+ });
227
+ const settings = yield* runWithMem(mem, load());
228
+ assert.strictEqual(settings.needsContextProbabilityThreshold, 0.72);
229
+ assert.strictEqual(settings.minAnswerConfidence, 0.41);
176
230
  }),
177
231
  );
178
232
 
@@ -210,13 +264,13 @@ it.effect("load resolves every value from the shared contract fixture", () =>
210
264
  provider: "typesafe",
211
265
  modelId: "jev-latest",
212
266
  });
213
- assert.strictEqual(settings.keepContextThreshold, 0.5);
214
- assert.strictEqual(settings.keepContextMinConfidence, 0.5);
267
+ assert.strictEqual(settings.needsContextProbabilityThreshold, 0.5);
268
+ assert.strictEqual(settings.minAnswerConfidence, 0.5);
215
269
  assert.strictEqual(settings.maxCandidates, 8);
216
270
  }),
217
271
  );
218
272
 
219
- it.effect("load reads the enabled, classifier, and threshold keys the contract names", () =>
273
+ it.effect("load reads the enabled, classifier, and probability keys the contract names", () =>
220
274
  Effect.gen(function* () {
221
275
  const parsed = JSON.parse(yield* readFixture) as {
222
276
  smartCompaction: Record<string, unknown>;
@@ -224,13 +278,15 @@ it.effect("load reads the enabled, classifier, and threshold keys the contract n
224
278
  assert.strictEqual(SMART_COMPACTION_KEY in parsed, true);
225
279
  assert.strictEqual(ENABLED_KEY in parsed.smartCompaction, true);
226
280
  assert.strictEqual(CLASSIFIER_KEY in parsed.smartCompaction, true);
227
- assert.strictEqual(KEEP_CONTEXT_THRESHOLD_KEY in parsed.smartCompaction, true);
228
- assert.strictEqual(KEEP_CONTEXT_MIN_CONFIDENCE_KEY in parsed.smartCompaction, true);
281
+ assert.strictEqual(NEEDS_CONTEXT_PROBABILITY_THRESHOLD_KEY in parsed.smartCompaction, true);
282
+ assert.strictEqual(MIN_ANSWER_CONFIDENCE_KEY in parsed.smartCompaction, true);
283
+ assert.strictEqual(LEGACY_KEEP_CONTEXT_THRESHOLD_KEY in parsed.smartCompaction, false);
284
+ assert.strictEqual(LEGACY_KEEP_CONTEXT_MIN_CONFIDENCE_KEY in parsed.smartCompaction, false);
229
285
  assert.strictEqual(MAX_CANDIDATES_KEY in parsed.smartCompaction, true);
230
286
  assert.strictEqual(parsed.smartCompaction[ENABLED_KEY], true);
231
287
  assert.strictEqual(parsed.smartCompaction[CLASSIFIER_KEY], "typesafe/jev-latest");
232
- assert.strictEqual(parsed.smartCompaction[KEEP_CONTEXT_THRESHOLD_KEY], 0.5);
233
- assert.strictEqual(parsed.smartCompaction[KEEP_CONTEXT_MIN_CONFIDENCE_KEY], 0.5);
288
+ assert.strictEqual(parsed.smartCompaction[NEEDS_CONTEXT_PROBABILITY_THRESHOLD_KEY], 0.5);
289
+ assert.strictEqual(parsed.smartCompaction[MIN_ANSWER_CONFIDENCE_KEY], 0.5);
234
290
  assert.strictEqual(parsed.smartCompaction[MAX_CANDIDATES_KEY], 8);
235
291
  }),
236
292
  );