@ftrdotdev/pi-tracker 0.6.0 → 0.8.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 +183 -60
- package/package.json +23 -11
- 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 +209 -68
- package/src/index.ts +170 -48
- package/src/presentation/tool-metadata.test.ts +263 -48
- package/src/presentation/tool-metadata.ts +325 -102
- package/src/presentation/ui.test.ts +122 -7
- package/src/presentation/ui.ts +81 -18
- package/src/presentation/vim-settings.test.ts +98 -0
- package/src/presentation/vim-settings.ts +59 -0
|
@@ -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(
|
package/src/compaction/digest.ts
CHANGED
|
@@ -2,16 +2,30 @@ import type { JsonObject } from "@earendil-works/pi-ai";
|
|
|
2
2
|
import { formatItemRef } from "../core/deps.ts";
|
|
3
3
|
import { NEEDS_CONTEXT_LABEL, NEEDS_NONE_LABEL, type CompactionQuestion } from "./classifier.ts";
|
|
4
4
|
import type { CompactionCandidate } from "./candidate.ts";
|
|
5
|
+
import type { DeclaredReference, ProductState, ReferenceState } from "./observations.ts";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
|
-
* The bounded classifier input: every ready candidate, each with its
|
|
8
|
-
* its relationship to the completed batch,
|
|
9
|
-
*
|
|
8
|
+
* The bounded classifier input: every ready candidate, each with its title,
|
|
9
|
+
* its description, its class, its relationship to the completed batch, and its
|
|
10
|
+
* resolved declarations, plus the batch itself as referenced prior work. One
|
|
11
|
+
* survival instruction states what the item may still reach after compaction.
|
|
12
|
+
* The transcript is never read.
|
|
10
13
|
*/
|
|
11
14
|
|
|
12
15
|
/** Shared prefix of the per-candidate classifier question key. */
|
|
13
16
|
export const QUESTION_KEY_PREFIX = "needsCompletedContext";
|
|
14
17
|
|
|
18
|
+
/** The most references one candidate's digest carries. */
|
|
19
|
+
export const REFERENCE_CAP = 8;
|
|
20
|
+
/** The most products one candidate's digest carries. */
|
|
21
|
+
export const PRODUCT_CAP = 4;
|
|
22
|
+
/** Characters of a description's first paragraph the digest keeps. */
|
|
23
|
+
export const DESCRIPTION_LIMIT = 400;
|
|
24
|
+
|
|
25
|
+
/** What survives compaction, stated once for the whole call. */
|
|
26
|
+
export const SURVIVAL_INSTRUCTION =
|
|
27
|
+
"After compaction, each item's title and description survive, and a file that exists on disk survives. The transcript does not survive, so ask only whether the item needs a detail that its own title, its description, or an existing file does not carry.";
|
|
28
|
+
|
|
15
29
|
/**
|
|
16
30
|
* A safe, unique question id for one candidate. Cloudflare Workers AI rejects
|
|
17
31
|
* any id that does not match `^[A-Za-z0-9_.-]{1,100}$`, so the item reference
|
|
@@ -25,17 +39,90 @@ const relationshipOf = (candidate: CompactionCandidate): string =>
|
|
|
25
39
|
? `depends on ${candidate.completed[0]?.ref ?? "the completed work"}`
|
|
26
40
|
: `independent of ${candidate.completed.map((item) => item.ref).join(", ")}`;
|
|
27
41
|
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
42
|
+
/** The first paragraph of a text, then the cap with a marker naming the cut. */
|
|
43
|
+
const descriptionOf = (text: string): string => {
|
|
44
|
+
const paragraph = text.split(/\n\s*\n/)[0]?.trim() ?? "";
|
|
45
|
+
return paragraph.length <= DESCRIPTION_LIMIT
|
|
46
|
+
? paragraph
|
|
47
|
+
: `${paragraph.slice(0, DESCRIPTION_LIMIT)}… [${paragraph.length - DESCRIPTION_LIMIT} more characters]`;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** One declared reference with the state it resolved to. */
|
|
51
|
+
const referenceOf = (reference: DeclaredReference, state: ReferenceState): JsonObject =>
|
|
52
|
+
reference.kind === "decision"
|
|
53
|
+
? { kind: "decision", topic: reference.topic, state }
|
|
54
|
+
: {
|
|
55
|
+
kind: "path",
|
|
56
|
+
path: reference.path,
|
|
57
|
+
...(reference.symbol === undefined ? {} : { symbol: reference.symbol }),
|
|
58
|
+
...(reference.span === undefined ? {} : { span: reference.span }),
|
|
59
|
+
state,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** A bounded candidate state: title, description, relationship, declarations. */
|
|
63
|
+
const candidateOf = (
|
|
64
|
+
candidate: CompactionCandidate,
|
|
65
|
+
facts: {
|
|
66
|
+
readonly references: readonly {
|
|
67
|
+
readonly reference: DeclaredReference;
|
|
68
|
+
readonly state: ReferenceState;
|
|
69
|
+
}[];
|
|
70
|
+
readonly products: readonly {
|
|
71
|
+
readonly product: { readonly path: string };
|
|
72
|
+
readonly state: ProductState;
|
|
73
|
+
}[];
|
|
74
|
+
},
|
|
75
|
+
): JsonObject => {
|
|
76
|
+
const references = facts.references.slice(0, REFERENCE_CAP);
|
|
77
|
+
const products = facts.products.slice(0, PRODUCT_CAP);
|
|
78
|
+
const droppedReferences = facts.references.length - references.length;
|
|
79
|
+
const droppedProducts = facts.products.length - products.length;
|
|
80
|
+
return {
|
|
33
81
|
ref: formatItemRef(candidate.listName, candidate.id),
|
|
34
82
|
text: candidate.text,
|
|
83
|
+
...(candidate.description === undefined || candidate.description.length === 0
|
|
84
|
+
? {}
|
|
85
|
+
: { description: descriptionOf(candidate.description) }),
|
|
35
86
|
class: candidate.class,
|
|
36
87
|
relationship: relationshipOf(candidate),
|
|
88
|
+
references: references.map((entry) => referenceOf(entry.reference, entry.state)),
|
|
89
|
+
products: products.map((entry) => ({ path: entry.product.path, state: entry.state })),
|
|
90
|
+
...(droppedReferences === 0
|
|
91
|
+
? {}
|
|
92
|
+
: { referenceCut: `… [${droppedReferences} more references declared]` }),
|
|
93
|
+
...(droppedProducts === 0
|
|
94
|
+
? {}
|
|
95
|
+
: { productCut: `… [${droppedProducts} more products declared]` }),
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* One entry per candidate plus the completed batch once, plus the survival
|
|
101
|
+
* instruction. The state is facts only: a title, a description, a class, a
|
|
102
|
+
* relationship, resolved references, resolved products, and their cut markers.
|
|
103
|
+
*/
|
|
104
|
+
export const buildDigests = (
|
|
105
|
+
candidates: readonly CompactionCandidate[],
|
|
106
|
+
facts: readonly {
|
|
107
|
+
readonly references: readonly {
|
|
108
|
+
readonly reference: DeclaredReference;
|
|
109
|
+
readonly state: ReferenceState;
|
|
110
|
+
}[];
|
|
111
|
+
readonly products: readonly {
|
|
112
|
+
readonly product: { readonly path: string };
|
|
113
|
+
readonly state: ProductState;
|
|
114
|
+
}[];
|
|
115
|
+
}[] = [],
|
|
116
|
+
): JsonObject => ({
|
|
117
|
+
candidates: candidates.map((candidate, index) =>
|
|
118
|
+
candidateOf(candidate, facts[index] ?? { references: [], products: [] }),
|
|
119
|
+
),
|
|
120
|
+
completed: (candidates[0]?.completed ?? []).map((item) => ({
|
|
121
|
+
ref: item.ref,
|
|
122
|
+
text: item.text,
|
|
123
|
+
...(item.description === undefined ? {} : { description: item.description }),
|
|
37
124
|
})),
|
|
38
|
-
|
|
125
|
+
survival: SURVIVAL_INSTRUCTION,
|
|
39
126
|
});
|
|
40
127
|
|
|
41
128
|
/**
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { fingerprintOf, type FileProbe } from "./observations.ts";
|
|
4
|
+
import { makeFootprintRecorder, newSideSpans, pendingOf, type ToolEvent } from "./footprint.ts";
|
|
5
|
+
|
|
6
|
+
/** A probe over a path-to-text map that records every read path. */
|
|
7
|
+
const countingProbe = (
|
|
8
|
+
files: Readonly<Record<string, string>>,
|
|
9
|
+
): { readonly probe: FileProbe; readonly reads: string[] } => {
|
|
10
|
+
const reads: string[] = [];
|
|
11
|
+
return {
|
|
12
|
+
reads,
|
|
13
|
+
probe: {
|
|
14
|
+
read: (path) => {
|
|
15
|
+
reads.push(path);
|
|
16
|
+
return files[path];
|
|
17
|
+
},
|
|
18
|
+
findSymbol: () => [],
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const write = (path: string, patch: string): ToolEvent => ({
|
|
24
|
+
phase: "end",
|
|
25
|
+
toolName: "write",
|
|
26
|
+
args: { path },
|
|
27
|
+
details: { patch },
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const paths = (entries: readonly { readonly path: string }[]): readonly string[] =>
|
|
31
|
+
entries.map((entry) => entry.path);
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Projection
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
it("capture projects a write call into a path and its spans", () => {
|
|
38
|
+
const pending = pendingOf({
|
|
39
|
+
phase: "end",
|
|
40
|
+
toolName: "write",
|
|
41
|
+
args: { path: "src/a.ts" },
|
|
42
|
+
details: { patch: "@@ -1,2 +3,4 @@\n context" },
|
|
43
|
+
});
|
|
44
|
+
assert.deepStrictEqual(pending, { kind: "write", path: "src/a.ts", spans: ["3-6"] });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("newSideSpans reads the new-side range of each hunk and caps at four", () => {
|
|
48
|
+
assert.deepStrictEqual(
|
|
49
|
+
newSideSpans({ patch: "@@ -1,0 +1,1 @@\n@@ -4,2 +8 @@\n@@ -9 +20,3 @@" }),
|
|
50
|
+
["1", "8", "20-22"],
|
|
51
|
+
);
|
|
52
|
+
assert.deepStrictEqual(newSideSpans({ firstChangedLine: 12 }), ["12"]);
|
|
53
|
+
assert.deepStrictEqual(newSideSpans({}), []);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
// The recorder
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
it.effect("capture never reads the disk", () =>
|
|
61
|
+
Effect.gen(function* () {
|
|
62
|
+
const files = Object.fromEntries(
|
|
63
|
+
Array.from({ length: 10 }, (_value, index) => [`src/f${index}.ts`, "one\ntwo"]),
|
|
64
|
+
);
|
|
65
|
+
const { probe, reads } = countingProbe(files);
|
|
66
|
+
const parts = yield* makeFootprintRecorder(probe);
|
|
67
|
+
for (let index = 0; index < 10; index += 1) {
|
|
68
|
+
yield* parts.recorder.capture(write(`src/f${index}.ts`, "@@ -1,0 +1,2 @@"));
|
|
69
|
+
}
|
|
70
|
+
assert.strictEqual(reads.length, 0);
|
|
71
|
+
// The snapshot is the drain: only now does the probe see a read.
|
|
72
|
+
yield* parts.recorder.snapshot;
|
|
73
|
+
assert.strictEqual(reads.length, 10);
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
it.effect("repeated writes to one path coalesce into one derivation read", () =>
|
|
78
|
+
Effect.gen(function* () {
|
|
79
|
+
const { probe, reads } = countingProbe({ "src/a.ts": "1\n2\n3\n4\n5\n6\n7\n8" });
|
|
80
|
+
const parts = yield* makeFootprintRecorder(probe);
|
|
81
|
+
yield* parts.recorder.capture(write("src/a.ts", "@@ -1,1 +1,2 @@"));
|
|
82
|
+
yield* parts.recorder.capture(write("src/a.ts", "@@ -1,1 +3,2 @@"));
|
|
83
|
+
yield* parts.recorder.capture(write("src/a.ts", "@@ -1,1 +5,2 @@"));
|
|
84
|
+
const footprint = yield* parts.recorder.snapshot;
|
|
85
|
+
assert.deepStrictEqual(reads, ["src/a.ts"]);
|
|
86
|
+
assert.deepStrictEqual(footprint.wrote, [
|
|
87
|
+
{ path: "src/a.ts", spans: ["5-6"], fingerprint: fingerprintOf("5\n6") },
|
|
88
|
+
]);
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
it.effect("parallel events for two paths derive independently", () =>
|
|
93
|
+
Effect.gen(function* () {
|
|
94
|
+
const { probe } = countingProbe({
|
|
95
|
+
"src/a.ts": "1\n2\n3\n4\n5\n6",
|
|
96
|
+
"src/b.ts": "x\n1\n2",
|
|
97
|
+
});
|
|
98
|
+
const parts = yield* makeFootprintRecorder(probe);
|
|
99
|
+
yield* parts.recorder.capture(write("src/a.ts", "@@ -1,2 +3,4 @@"));
|
|
100
|
+
yield* parts.recorder.capture(write("src/b.ts", "@@ -1,1 +1,2 @@"));
|
|
101
|
+
const footprint = yield* parts.recorder.snapshot;
|
|
102
|
+
assert.deepStrictEqual(paths(footprint.wrote), ["src/a.ts", "src/b.ts"]);
|
|
103
|
+
assert.deepStrictEqual(footprint.wrote[0]?.spans, ["3-6"]);
|
|
104
|
+
assert.deepStrictEqual(footprint.wrote[1]?.spans, ["1-2"]);
|
|
105
|
+
assert.strictEqual(footprint.wrote[0]?.fingerprint, fingerprintOf("3\n4\n5\n6"));
|
|
106
|
+
assert.strictEqual(footprint.wrote[1]?.fingerprint, fingerprintOf("x\n1"));
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
it.effect("a snapshot returns only ready facts and clears on consume", () =>
|
|
111
|
+
Effect.gen(function* () {
|
|
112
|
+
const { probe } = countingProbe({ "src/a.ts": "1\n2\n3\n4\n5\n6" });
|
|
113
|
+
// `src/b.ts` cannot be derived: the probe throws, which leaves its fact out
|
|
114
|
+
// of the snapshot. The resolver abstains on the missing path.
|
|
115
|
+
const failing: FileProbe = {
|
|
116
|
+
read: (path) => {
|
|
117
|
+
if (path === "src/b.ts") throw new Error("not ready");
|
|
118
|
+
return probe.read(path);
|
|
119
|
+
},
|
|
120
|
+
findSymbol: () => [],
|
|
121
|
+
};
|
|
122
|
+
const parts = yield* makeFootprintRecorder(failing);
|
|
123
|
+
yield* parts.recorder.capture(write("src/a.ts", "@@ -1,1 +1,2 @@"));
|
|
124
|
+
yield* parts.recorder.capture(write("src/b.ts", "@@ -1,1 +1,2 @@"));
|
|
125
|
+
|
|
126
|
+
const first = yield* parts.recorder.snapshot;
|
|
127
|
+
assert.deepStrictEqual(paths(first.wrote), ["src/a.ts"]);
|
|
128
|
+
|
|
129
|
+
const consumed = yield* parts.recorder.consume;
|
|
130
|
+
assert.deepStrictEqual(paths(consumed.wrote), ["src/a.ts"]);
|
|
131
|
+
|
|
132
|
+
const second = yield* parts.recorder.snapshot;
|
|
133
|
+
assert.deepStrictEqual(second.wrote, []);
|
|
134
|
+
}),
|
|
135
|
+
);
|