@criogaid/pi-hashline-edit 0.5.0 → 0.5.1
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 +18 -10
- package/package.json +1 -1
- package/src/core/text.test.ts +30 -1
- package/src/core/text.ts +8 -3
- package/src/integration/grep-rg.test.ts +212 -93
- package/src/integration/large-text.test.ts +3 -5
- package/src/pi/action-fusion.test.ts +141 -5
- package/src/pi/action-fusion.ts +36 -3
- package/src/pi/anchor-format.test.ts +46 -0
- package/src/pi/anchor-format.ts +21 -2
- package/src/pi/diagnostic-buffer.test.ts +58 -0
- package/src/pi/diagnostic-buffer.ts +59 -0
- package/src/pi/e2e-scenarios.test.ts +1 -0
- package/src/pi/edit-tool.ts +1 -1
- package/src/pi/fusion-card.test.ts +26 -0
- package/src/pi/fusion-card.ts +21 -6
- package/src/pi/grep-output.ts +26 -28
- package/src/pi/grep-render.ts +8 -11
- package/src/pi/grep-scope.ts +6 -31
- package/src/pi/grep-search.ts +62 -49
- package/src/pi/grep-tool.ts +25 -53
- package/src/pi/grep.test.ts +292 -109
- package/src/pi/pi.test.ts +1 -1
- package/src/pi/read-tool.ts +1 -1
- package/src/pi/registration.test.ts +6 -2
- package/src/pi/render.test.ts +127 -1
- package/src/pi/render.ts +28 -5
- package/src/pi/replace-tool.ts +1 -1
- package/src/pi/rg-process.ts +8 -5
- package/src/pi/rg-text-view.ts +8 -5
- package/src/pi/text-stream.test.ts +153 -0
- package/src/pi/text-stream.ts +33 -13
- package/src/pi/write-tool.ts +2 -0
|
@@ -28,7 +28,12 @@ async function tempDir(): Promise<string> {
|
|
|
28
28
|
return mkdtemp(join(tmpdir(), "hashline-action-fusion-"));
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
// Only the context fields consumed by Fusion and Pi Bash are needed by these tests.
|
|
32
|
+
const ctx = (cwd: string) =>
|
|
33
|
+
({
|
|
34
|
+
cwd,
|
|
35
|
+
sessionManager: { getSessionId: () => "test", getSessionFile: () => undefined },
|
|
36
|
+
}) as ExtensionContext;
|
|
32
37
|
|
|
33
38
|
type Fusion = Parameters<typeof makeWriteOverride>[1];
|
|
34
39
|
const mutationFactories = [
|
|
@@ -239,10 +244,7 @@ test("default runner executes a real local command", async () => {
|
|
|
239
244
|
});
|
|
240
245
|
},
|
|
241
246
|
signal: undefined,
|
|
242
|
-
ctx:
|
|
243
|
-
...ctx(dir),
|
|
244
|
-
sessionManager: { getSessionId: () => "test", getSessionFile: () => undefined },
|
|
245
|
-
} as any,
|
|
247
|
+
ctx: ctx(dir),
|
|
246
248
|
});
|
|
247
249
|
const output = result.content
|
|
248
250
|
.filter((block) => block.type === "text")
|
|
@@ -651,3 +653,137 @@ test("Fusion uses commit facts when details omit or contradict publication and r
|
|
|
651
653
|
await rm(dir, { recursive: true, force: true });
|
|
652
654
|
}
|
|
653
655
|
});
|
|
656
|
+
|
|
657
|
+
test("timed command progress ticks during silence, preserves output, and stops at every outcome", async (t) => {
|
|
658
|
+
for (const outcome of ["succeeded", "failed", "timeout", "cancelled"] as const) {
|
|
659
|
+
await t.test(outcome, async (t) => {
|
|
660
|
+
const dir = await tempDir();
|
|
661
|
+
t.mock.timers.enable({ apis: ["Date", "setInterval"], now: 0 });
|
|
662
|
+
const started = Promise.withResolvers<void>();
|
|
663
|
+
const finished = Promise.withResolvers<string>();
|
|
664
|
+
const controller = new AbortController();
|
|
665
|
+
const events: ActionFusionProgress[] = [];
|
|
666
|
+
const updates: unknown[] = [];
|
|
667
|
+
try {
|
|
668
|
+
const fusion = createActionFusionExecutor(
|
|
669
|
+
async (_id, input, _signal, _ctx, onUpdate) => {
|
|
670
|
+
assert.equal(input.timeout, 10);
|
|
671
|
+
started.resolve();
|
|
672
|
+
onUpdate?.({ content: [{ type: "text", text: "latest output" }], details: undefined });
|
|
673
|
+
return finished.promise;
|
|
674
|
+
},
|
|
675
|
+
(event) => events.push(event),
|
|
676
|
+
);
|
|
677
|
+
const execution = fusion({
|
|
678
|
+
toolCallId: outcome,
|
|
679
|
+
absolutePath: join(dir, "target.txt"),
|
|
680
|
+
thenRun: { command: "check", timeout: 10 },
|
|
681
|
+
mutate: async () => {
|
|
682
|
+
// Mutation time must not consume the command's timeout.
|
|
683
|
+
t.mock.timers.tick(5_000);
|
|
684
|
+
await writeFile(join(dir, "target.txt"), "saved\n");
|
|
685
|
+
return publishedMutation("saved\n", { content: [], details: undefined });
|
|
686
|
+
},
|
|
687
|
+
signal: controller.signal,
|
|
688
|
+
ctx: ctx(dir),
|
|
689
|
+
onUpdate: (update) => updates.push(update),
|
|
690
|
+
});
|
|
691
|
+
await started.promise;
|
|
692
|
+
assert.equal(events[0].timing, undefined);
|
|
693
|
+
assert.deepEqual(events.at(-1)?.timing, { timeoutSeconds: 10, remainingSeconds: 10 });
|
|
694
|
+
const updateCount = updates.length;
|
|
695
|
+
t.mock.timers.tick(2_000);
|
|
696
|
+
assert.equal(updates.length, updateCount + 2);
|
|
697
|
+
assert.equal(events.at(-1)?.timing?.remainingSeconds, 8);
|
|
698
|
+
assert.equal(events.at(-1)?.output, "latest output");
|
|
699
|
+
if (outcome === "succeeded") finished.resolve("complete");
|
|
700
|
+
else {
|
|
701
|
+
if (outcome === "cancelled") controller.abort();
|
|
702
|
+
finished.reject(
|
|
703
|
+
new Error(
|
|
704
|
+
outcome === "timeout" ? "Command timed out" : "command stopped\nunderlying cause",
|
|
705
|
+
),
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
await execution;
|
|
709
|
+
assert.equal(events.at(-1)?.command, outcome);
|
|
710
|
+
const terminalCount = updates.length;
|
|
711
|
+
t.mock.timers.tick(20_000);
|
|
712
|
+
assert.equal(updates.length, terminalCount);
|
|
713
|
+
} finally {
|
|
714
|
+
finished.resolve("");
|
|
715
|
+
await rm(dir, { recursive: true, force: true });
|
|
716
|
+
}
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
test("commands without an explicit timeout do not start countdown updates", async (t) => {
|
|
722
|
+
const dir = await tempDir();
|
|
723
|
+
t.mock.timers.enable({ apis: ["Date", "setInterval"], now: 0 });
|
|
724
|
+
const started = Promise.withResolvers<void>();
|
|
725
|
+
const finished = Promise.withResolvers<string>();
|
|
726
|
+
const events: ActionFusionProgress[] = [];
|
|
727
|
+
try {
|
|
728
|
+
const fusion = createActionFusionExecutor(
|
|
729
|
+
async (_id, input) => {
|
|
730
|
+
assert.equal(input.timeout, undefined);
|
|
731
|
+
started.resolve();
|
|
732
|
+
return finished.promise;
|
|
733
|
+
},
|
|
734
|
+
(event) => events.push(event),
|
|
735
|
+
);
|
|
736
|
+
const execution = fusion({
|
|
737
|
+
toolCallId: "untimed",
|
|
738
|
+
absolutePath: join(dir, "target.txt"),
|
|
739
|
+
thenRun: { command: "check" },
|
|
740
|
+
mutate: async () => {
|
|
741
|
+
await writeFile(join(dir, "target.txt"), "saved\n");
|
|
742
|
+
return publishedMutation("saved\n", { content: [], details: undefined });
|
|
743
|
+
},
|
|
744
|
+
signal: undefined,
|
|
745
|
+
ctx: ctx(dir),
|
|
746
|
+
});
|
|
747
|
+
await started.promise;
|
|
748
|
+
const count = events.length;
|
|
749
|
+
t.mock.timers.tick(20_000);
|
|
750
|
+
assert.equal(events.length, count);
|
|
751
|
+
assert.equal(events.at(-1)?.timing, undefined);
|
|
752
|
+
finished.resolve("complete");
|
|
753
|
+
await execution;
|
|
754
|
+
} finally {
|
|
755
|
+
finished.resolve("");
|
|
756
|
+
await rm(dir, { recursive: true, force: true });
|
|
757
|
+
}
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
test("Pi Bash timeout returns the cause to the model while keeping the published file", async () => {
|
|
761
|
+
const dir = await tempDir();
|
|
762
|
+
try {
|
|
763
|
+
await writeFile(join(dir, "slow.mjs"), "setTimeout(() => {}, 10_000);\n");
|
|
764
|
+
const events: ActionFusionProgress[] = [];
|
|
765
|
+
const fusion = createActionFusionExecutor(undefined, (event) => events.push(event));
|
|
766
|
+
const tool = makeWriteOverride(dir, fusion);
|
|
767
|
+
const result = await callTool(
|
|
768
|
+
tool,
|
|
769
|
+
{
|
|
770
|
+
path: "saved.txt",
|
|
771
|
+
content: "published\n",
|
|
772
|
+
then_run: { command: "node slow.mjs", timeout: 0.2 },
|
|
773
|
+
},
|
|
774
|
+
{ ctx: ctx(dir) },
|
|
775
|
+
);
|
|
776
|
+
assert.equal(await readFile(join(dir, "saved.txt"), "utf8"), "published\n");
|
|
777
|
+
assert.equal(result.details.actionFusion.command, "timeout");
|
|
778
|
+
assert.equal(result.details.actionFusion.publication, "PUBLISHED");
|
|
779
|
+
const text = result.content
|
|
780
|
+
.filter((block: { type: string }) => block.type === "text")
|
|
781
|
+
.map((block: { text: string }) => block.text)
|
|
782
|
+
.join("\n");
|
|
783
|
+
assert.match(text, /timed out after 0\.2 seconds/);
|
|
784
|
+
assert.match(text, /File changes.*saved/);
|
|
785
|
+
assert.equal(events.at(-1)?.command, "timeout");
|
|
786
|
+
} finally {
|
|
787
|
+
await rm(dir, { recursive: true, force: true });
|
|
788
|
+
}
|
|
789
|
+
});
|
package/src/pi/action-fusion.ts
CHANGED
|
@@ -29,6 +29,8 @@ import { commitFreshness, finalizeMutation, type MutationOutcome } from "./mutat
|
|
|
29
29
|
import { errorMessage } from "../core/errors.ts";
|
|
30
30
|
import { OPERATION_ABORTED, throwIfCancelled } from "./error-text.ts";
|
|
31
31
|
|
|
32
|
+
const MILLISECONDS_PER_SECOND = 1_000;
|
|
33
|
+
|
|
32
34
|
export const THEN_RUN_SUCCEEDED = "[then_run:succeeded]";
|
|
33
35
|
export const THEN_RUN_FAILED = "[then_run:failed]";
|
|
34
36
|
export const THEN_RUN_SKIPPED = "[then_run:skipped]";
|
|
@@ -70,6 +72,8 @@ export interface ActionFusionProgress extends Omit<ActionFusionDetails, "command
|
|
|
70
72
|
reason?: string;
|
|
71
73
|
/** True only after mutation execution and result generation both succeed. */
|
|
72
74
|
mutationCompleted: boolean;
|
|
75
|
+
/** Present once a command with an explicit timeout starts; refreshed during silent execution. */
|
|
76
|
+
timing?: { readonly timeoutSeconds: number; readonly remainingSeconds: number };
|
|
73
77
|
}
|
|
74
78
|
|
|
75
79
|
type ProgressReporter = (progress: ActionFusionProgress, ctx: ExtensionContext) => void;
|
|
@@ -290,6 +294,7 @@ export function createActionFusionExecutor(
|
|
|
290
294
|
}): Promise<MutationResult<TDetails>> {
|
|
291
295
|
let completedMutation: MutationOutcome<TDetails> | undefined;
|
|
292
296
|
let progressFailure: string | undefined;
|
|
297
|
+
let commandTiming: { readonly timeoutSeconds: number; readonly deadlineMs: number } | undefined;
|
|
293
298
|
const report = (
|
|
294
299
|
command: ActionFusionProgress["command"],
|
|
295
300
|
publication: PublicationStatus,
|
|
@@ -308,6 +313,17 @@ export function createActionFusionExecutor(
|
|
|
308
313
|
output,
|
|
309
314
|
...(reason ? { reason } : {}),
|
|
310
315
|
mutationCompleted: completedMutation !== undefined,
|
|
316
|
+
...(commandTiming
|
|
317
|
+
? {
|
|
318
|
+
timing: {
|
|
319
|
+
timeoutSeconds: commandTiming.timeoutSeconds,
|
|
320
|
+
remainingSeconds: Math.max(
|
|
321
|
+
0,
|
|
322
|
+
Math.ceil((commandTiming.deadlineMs - Date.now()) / MILLISECONDS_PER_SECOND),
|
|
323
|
+
),
|
|
324
|
+
},
|
|
325
|
+
}
|
|
326
|
+
: {}),
|
|
311
327
|
};
|
|
312
328
|
// Display callbacks are observers: their failures must not change publication or command execution.
|
|
313
329
|
for (const notify of [
|
|
@@ -400,20 +416,37 @@ export function createActionFusionExecutor(
|
|
|
400
416
|
);
|
|
401
417
|
}
|
|
402
418
|
|
|
419
|
+
if (thenRun.timeout !== undefined) {
|
|
420
|
+
commandTiming = {
|
|
421
|
+
timeoutSeconds: thenRun.timeout,
|
|
422
|
+
deadlineMs: Date.now() + thenRun.timeout * MILLISECONDS_PER_SECOND,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
403
425
|
report("running", publication, "unchanged");
|
|
404
|
-
let output
|
|
426
|
+
let output = "";
|
|
405
427
|
let commandError: unknown;
|
|
428
|
+
// The executor owns the heartbeat lifetime; Pi Bash owns timeout and process cleanup.
|
|
429
|
+
const heartbeat =
|
|
430
|
+
commandTiming && (onProgress || onUpdate)
|
|
431
|
+
? setInterval(
|
|
432
|
+
() => report("running", publication, "unknown", output),
|
|
433
|
+
MILLISECONDS_PER_SECOND,
|
|
434
|
+
)
|
|
435
|
+
: undefined;
|
|
436
|
+
heartbeat?.unref();
|
|
406
437
|
try {
|
|
407
438
|
output = await commandRunner(toolCallId, thenRun, signal, ctx, (partial) => {
|
|
408
|
-
|
|
439
|
+
output = partial.content
|
|
409
440
|
.filter((block) => block.type === "text")
|
|
410
441
|
.map((block) => block.text)
|
|
411
442
|
.join("\n");
|
|
412
|
-
report("running", publication, "unknown",
|
|
443
|
+
report("running", publication, "unknown", output);
|
|
413
444
|
});
|
|
414
445
|
} catch (error) {
|
|
415
446
|
commandError = error;
|
|
416
447
|
output = "";
|
|
448
|
+
} finally {
|
|
449
|
+
if (heartbeat) clearInterval(heartbeat);
|
|
417
450
|
}
|
|
418
451
|
const freshness = await readFreshness(absolutePath, baseline);
|
|
419
452
|
if (commandError !== undefined) {
|
|
@@ -6,6 +6,8 @@ import {
|
|
|
6
6
|
createAnchorFormatter,
|
|
7
7
|
parseAnchorToken,
|
|
8
8
|
parseHashline,
|
|
9
|
+
parseDisplayRow,
|
|
10
|
+
plainRow,
|
|
9
11
|
} from "./anchor-format.ts";
|
|
10
12
|
|
|
11
13
|
test("anchor formatter binds the supplied hash length and hashes undisplayed content", () => {
|
|
@@ -60,3 +62,47 @@ test("display rows accept supported hash lengths and preserve nested anchor-like
|
|
|
60
62
|
assert.equal(parseHashline(row), null);
|
|
61
63
|
}
|
|
62
64
|
});
|
|
65
|
+
|
|
66
|
+
test("plain rows expose carriage returns without creating editable anchors", () => {
|
|
67
|
+
assert.equal(plainRow(12, "a\rb│12#ABCD│nested"), "12│a␍b│12#ABCD│nested");
|
|
68
|
+
assert.equal(plainRow(1, ""), "1│");
|
|
69
|
+
assert.equal(parseHashline(plainRow(12, "content")), null);
|
|
70
|
+
assert.equal(parseAnchorToken("12"), undefined);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("display row parsing accepts plain and anchored rows without changing their content", () => {
|
|
74
|
+
for (const hashLen of [2, 4, 6, 8]) {
|
|
75
|
+
const anchored = createAnchorFormatter(hashLen).row(12, " nested 7#ABCD│content");
|
|
76
|
+
assert.deepEqual(parseDisplayRow(anchored), {
|
|
77
|
+
lineNo: "12",
|
|
78
|
+
content: " nested 7#ABCD│content",
|
|
79
|
+
});
|
|
80
|
+
assert.deepEqual(parseDisplayRow(anchored), parseHashline(anchored));
|
|
81
|
+
}
|
|
82
|
+
assert.deepEqual(parseDisplayRow("123│ �│7#ABCD│nested"), {
|
|
83
|
+
lineNo: "123",
|
|
84
|
+
content: " �│7#ABCD│nested",
|
|
85
|
+
});
|
|
86
|
+
assert.deepEqual(parseDisplayRow(plainRow(9, "a\rb")), { lineNo: "9", content: "a␍b" });
|
|
87
|
+
assert.deepEqual(parseDisplayRow("1│"), { lineNo: "1", content: "" });
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("display row parsing rejects headers, notices, and invalid line references", () => {
|
|
91
|
+
for (const row of [
|
|
92
|
+
"",
|
|
93
|
+
"file.txt · 2 matches",
|
|
94
|
+
"[Invalid UTF-8: plain preview]",
|
|
95
|
+
"No matches found",
|
|
96
|
+
"0│text",
|
|
97
|
+
"01│text",
|
|
98
|
+
"-1│text",
|
|
99
|
+
" 1│text",
|
|
100
|
+
"1:text",
|
|
101
|
+
"0#ABCD│text",
|
|
102
|
+
"01#ABCD│text",
|
|
103
|
+
"1#A│text",
|
|
104
|
+
"1#IIII│text",
|
|
105
|
+
]) {
|
|
106
|
+
assert.equal(parseDisplayRow(row), null, row);
|
|
107
|
+
}
|
|
108
|
+
});
|
package/src/pi/anchor-format.ts
CHANGED
|
@@ -9,8 +9,12 @@ export function anchorPattern(hashLen: number): string {
|
|
|
9
9
|
return `^${anchorSource(`{${hashLen}}`)}$`;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
/** Separates a row's line reference from its displayed content. */
|
|
13
|
+
const ROW_SEPARATOR = "│";
|
|
12
14
|
const ANCHOR_TOKEN = new RegExp(`^${anchorSource("+")}$`);
|
|
13
|
-
const HASHLINE_ROW = new RegExp(
|
|
15
|
+
const HASHLINE_ROW = new RegExp(
|
|
16
|
+
`^${anchorSource(`{${HASH_LEN_MIN},${HASH_LEN_MAX}}`)}${ROW_SEPARATOR}(.*)$`,
|
|
17
|
+
);
|
|
14
18
|
|
|
15
19
|
/** Split a `LINE#HASH` token of any hash length; undefined when the shape is wrong. */
|
|
16
20
|
export function parseAnchorToken(value: string): { line: number; hash: string } | undefined {
|
|
@@ -31,6 +35,16 @@ export function parseHashline(line: string): HashlineRow | null {
|
|
|
31
35
|
return match ? { lineNo: match[1], content: match[3] } : null;
|
|
32
36
|
}
|
|
33
37
|
|
|
38
|
+
const PLAIN_ROW = new RegExp(`^([1-9][0-9]*)${ROW_SEPARATOR}(.*)$`);
|
|
39
|
+
|
|
40
|
+
/** Parse an anchored row or a non-editable `LINE│content` row; null for other text. */
|
|
41
|
+
export function parseDisplayRow(line: string): HashlineRow | null {
|
|
42
|
+
const anchored = parseHashline(line);
|
|
43
|
+
if (anchored) return anchored;
|
|
44
|
+
const match = PLAIN_ROW.exec(line);
|
|
45
|
+
return match ? { lineNo: match[1], content: match[2] } : null;
|
|
46
|
+
}
|
|
47
|
+
|
|
34
48
|
/** Anchor serialization bound to a caller-supplied hash length. */
|
|
35
49
|
export interface AnchorFormatter {
|
|
36
50
|
reference(line: number, hash: string): string;
|
|
@@ -43,6 +57,11 @@ export function displayCarriageReturns(text: string): string {
|
|
|
43
57
|
return text.replace(/\r/g, "␍");
|
|
44
58
|
}
|
|
45
59
|
|
|
60
|
+
/** Non-editable `LINE│content` row for text that cannot carry an anchor. */
|
|
61
|
+
export function plainRow(line: number, displayContent: string): string {
|
|
62
|
+
return `${line}${ROW_SEPARATOR}${displayCarriageReturns(displayContent)}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
46
65
|
export function createAnchorFormatter(hashLen: number): AnchorFormatter {
|
|
47
66
|
const reference = (line: number, hash: string) => `${line}#${hash}`;
|
|
48
67
|
const token = (line: number, content: string) =>
|
|
@@ -51,6 +70,6 @@ export function createAnchorFormatter(hashLen: number): AnchorFormatter {
|
|
|
51
70
|
token,
|
|
52
71
|
reference,
|
|
53
72
|
row: (line, content, displayContent = content) =>
|
|
54
|
-
`${token(line, content)}
|
|
73
|
+
`${token(line, content)}${ROW_SEPARATOR}${displayCarriageReturns(displayContent)}`,
|
|
55
74
|
};
|
|
56
75
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { DiagnosticBuffer } from "./diagnostic-buffer.ts";
|
|
4
|
+
import { MAX_SEARCH_DIAGNOSTIC_BYTES } from "./budgets.ts";
|
|
5
|
+
import { formatSearchWarnings } from "./grep-output.ts";
|
|
6
|
+
import { MAX_RG_STDERR_BYTES, runText } from "./rg-process.ts";
|
|
7
|
+
|
|
8
|
+
const diagnostic = (size: number) =>
|
|
9
|
+
`regex parse error:\n${"intermediate context 中🙂\n".repeat(size)}error: unclosed group\n`;
|
|
10
|
+
|
|
11
|
+
test("bounded diagnostics retain opening context and the final cause across streamed chunks", () => {
|
|
12
|
+
const source = diagnostic(MAX_SEARCH_DIAGNOSTIC_BYTES);
|
|
13
|
+
const buffer = new DiagnosticBuffer(MAX_SEARCH_DIAGNOSTIC_BYTES);
|
|
14
|
+
const chunkChars = 113;
|
|
15
|
+
for (let offset = 0; offset < source.length; offset += chunkChars) {
|
|
16
|
+
buffer.append(source.slice(offset, offset + chunkChars));
|
|
17
|
+
}
|
|
18
|
+
const result = buffer.toString();
|
|
19
|
+
assert.match(result, /^regex parse error:/);
|
|
20
|
+
assert.match(result, /error: unclosed group\n$/);
|
|
21
|
+
assert.match(result, /omitted/i);
|
|
22
|
+
assert.ok(Buffer.byteLength(result) <= MAX_SEARCH_DIAGNOSTIC_BYTES);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("diagnostics within the budget are unchanged", () => {
|
|
26
|
+
const buffer = new DiagnosticBuffer(MAX_SEARCH_DIAGNOSTIC_BYTES);
|
|
27
|
+
const source = "path: 权限不足🙂\nretry with another path\n";
|
|
28
|
+
for (const point of source) buffer.append(point);
|
|
29
|
+
assert.equal(buffer.toString(), source);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("search warning aggregation retains the final cause and labels truncation", () => {
|
|
33
|
+
const result = formatSearchWarnings([diagnostic(MAX_SEARCH_DIAGNOSTIC_BYTES)]);
|
|
34
|
+
assert.match(result, /Search incomplete/);
|
|
35
|
+
assert.match(result, /regex parse error:/);
|
|
36
|
+
assert.match(result, /error: unclosed group/);
|
|
37
|
+
assert.match(result, /omitted/i);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("stderr collection preserves UTF-8 and reports overflow without losing the final cause", async () => {
|
|
41
|
+
for (const source of ["权限不足🙂\n", diagnostic(MAX_RG_STDERR_BYTES)]) {
|
|
42
|
+
const result = await runText(
|
|
43
|
+
process.execPath,
|
|
44
|
+
["--input-type=module", "--eval", "process.stdin.pipe(process.stderr)"],
|
|
45
|
+
Buffer.from(source),
|
|
46
|
+
AbortSignal.timeout(10000),
|
|
47
|
+
);
|
|
48
|
+
if (Buffer.byteLength(source) <= MAX_RG_STDERR_BYTES) {
|
|
49
|
+
assert.equal(result.stderr, source);
|
|
50
|
+
} else {
|
|
51
|
+
assert.match(result.stderr, /^regex parse error:/);
|
|
52
|
+
assert.match(result.stderr, /error: unclosed group\n$/);
|
|
53
|
+
assert.match(result.stderr, /omitted/i);
|
|
54
|
+
assert.doesNotMatch(result.stderr, /�/);
|
|
55
|
+
assert.ok(Buffer.byteLength(result.stderr) <= MAX_RG_STDERR_BYTES);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded diagnostic text shared by ripgrep stderr, batch aggregation, and
|
|
3
|
+
* model-facing search warnings. Retain the opening context and final cause;
|
|
4
|
+
* mark omitted text instead of silently dropping diagnostics.
|
|
5
|
+
*/
|
|
6
|
+
import { truncateHead, truncateTail } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { formatKiB } from "./budgets.ts";
|
|
8
|
+
|
|
9
|
+
export class DiagnosticBuffer {
|
|
10
|
+
private head = "";
|
|
11
|
+
private tail = "";
|
|
12
|
+
private truncated = false;
|
|
13
|
+
private readonly notice: string;
|
|
14
|
+
private readonly headBytes: number;
|
|
15
|
+
private readonly tailBytes: number;
|
|
16
|
+
|
|
17
|
+
private readonly maxBytes: number;
|
|
18
|
+
|
|
19
|
+
constructor(maxBytes: number) {
|
|
20
|
+
this.maxBytes = maxBytes;
|
|
21
|
+
this.notice = `\n[Diagnostics truncated (${formatKiB(maxBytes)} limit); middle omitted.]\n`;
|
|
22
|
+
const contentBytes = maxBytes - Buffer.byteLength(this.notice);
|
|
23
|
+
this.headBytes = Math.floor(contentBytes / 2);
|
|
24
|
+
this.tailBytes = contentBytes - this.headBytes;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
append(text: string): void {
|
|
28
|
+
if (!this.truncated) {
|
|
29
|
+
const combined = this.head + text;
|
|
30
|
+
if (Buffer.byteLength(combined) <= this.maxBytes) {
|
|
31
|
+
this.head = combined;
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this.head = truncateHead(combined, {
|
|
35
|
+
maxBytes: this.headBytes,
|
|
36
|
+
maxLines: Number.MAX_SAFE_INTEGER,
|
|
37
|
+
}).content;
|
|
38
|
+
this.tail = this.truncateTail(combined);
|
|
39
|
+
this.truncated = true;
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
this.tail = this.truncateTail(this.tail + text);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
toString(): string {
|
|
46
|
+
return this.truncated ? this.head + this.notice + this.tail : this.head;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private truncateTail(text: string): string {
|
|
50
|
+
// Pi's truncated previews omit the final newline; streaming must retain it
|
|
51
|
+
// so the next chunk cannot merge two diagnostic lines.
|
|
52
|
+
const newline = text.endsWith("\n") ? "\n" : "";
|
|
53
|
+
const preview = truncateTail(text, {
|
|
54
|
+
maxBytes: this.tailBytes - newline.length,
|
|
55
|
+
maxLines: Number.MAX_SAFE_INTEGER,
|
|
56
|
+
});
|
|
57
|
+
return preview.content + (preview.truncated ? newline : "");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -70,6 +70,7 @@ test("E2E Dev D1: Search-and-edit single function without separate read call", a
|
|
|
70
70
|
const grepResult = await call(grep, {
|
|
71
71
|
path: "calc.ts",
|
|
72
72
|
pattern: "calculateDiscount",
|
|
73
|
+
literal: true,
|
|
73
74
|
context: 3,
|
|
74
75
|
});
|
|
75
76
|
const grepText = grepResult.content[0].text;
|
package/src/pi/edit-tool.ts
CHANGED
|
@@ -265,7 +265,7 @@ export function makeEditOverride(
|
|
|
265
265
|
"Edit file lines by LINE#HASH anchors checked against the current file. Returns fresh anchors for changed lines. On anchor failure, shows current context and recovery candidates; nothing is retried automatically.",
|
|
266
266
|
promptSnippet: "Edit file lines using verified anchors",
|
|
267
267
|
promptGuidelines: [
|
|
268
|
-
"Batch
|
|
268
|
+
"Batch all edits to one file in a single edit call; all its anchors are checked against one snapshot.",
|
|
269
269
|
"Reuse anchors while their line number and content are unchanged; inserts and deletes shift later lines, so use the edit's Updated anchors or re-read shifted lines.",
|
|
270
270
|
"On edit anchor failure, inspect the recovery candidates before retrying or re-reading.",
|
|
271
271
|
...(fusion ? ACTION_FUSION_GUIDELINES : []),
|
|
@@ -139,3 +139,29 @@ test("skips and cancellations stay neutral and restore their own reason", () =>
|
|
|
139
139
|
}
|
|
140
140
|
}
|
|
141
141
|
});
|
|
142
|
+
|
|
143
|
+
test("timed command cards show remaining seconds only while actively running", () => {
|
|
144
|
+
const h = harness();
|
|
145
|
+
h.report(waiting);
|
|
146
|
+
const card = h.card(h.entries[0]);
|
|
147
|
+
assert.doesNotMatch(card.render(160).join("\n"), /remaining/);
|
|
148
|
+
const running: ActionFusionProgress = {
|
|
149
|
+
...waiting,
|
|
150
|
+
command: "running",
|
|
151
|
+
publication: "PUBLISHED",
|
|
152
|
+
mutationCompleted: true,
|
|
153
|
+
timing: { timeoutSeconds: 10, remainingSeconds: 10 },
|
|
154
|
+
};
|
|
155
|
+
h.report(running);
|
|
156
|
+
assert.match(card.render(160).join("\n"), /10s remaining/);
|
|
157
|
+
h.report({ ...running, timing: { timeoutSeconds: 10, remainingSeconds: 7 } });
|
|
158
|
+
assert.match(card.render(160).join("\n"), /7s remaining/);
|
|
159
|
+
assert.equal(h.entries.length, 1);
|
|
160
|
+
h.report({ ...running, timing: { timeoutSeconds: 10, remainingSeconds: 0 } });
|
|
161
|
+
assert.match(card.render(160).join("\n"), /0s remaining/);
|
|
162
|
+
h.report({ ...running, command: "timeout", output: "Command timed out after 10 seconds" });
|
|
163
|
+
assert.doesNotMatch(card.render(160).join("\n"), /remaining/);
|
|
164
|
+
const restored = harness(h.entries);
|
|
165
|
+
restored.restore();
|
|
166
|
+
assert.doesNotMatch(restored.card(h.entries[0]).render(160).join("\n"), /remaining/);
|
|
167
|
+
});
|
package/src/pi/fusion-card.ts
CHANGED
|
@@ -22,7 +22,7 @@ const RESULT_TYPE = "hashline-then-run-result";
|
|
|
22
22
|
|
|
23
23
|
type CommandCardData = Pick<
|
|
24
24
|
ActionFusionProgress,
|
|
25
|
-
"toolCallId" | "commandText" | "command" | "output" | "reason"
|
|
25
|
+
"toolCallId" | "commandText" | "command" | "output" | "reason" | "timing"
|
|
26
26
|
>;
|
|
27
27
|
|
|
28
28
|
function commandCardData({
|
|
@@ -31,8 +31,16 @@ function commandCardData({
|
|
|
31
31
|
command,
|
|
32
32
|
output,
|
|
33
33
|
reason,
|
|
34
|
+
timing,
|
|
34
35
|
}: ActionFusionProgress): CommandCardData {
|
|
35
|
-
return {
|
|
36
|
+
return {
|
|
37
|
+
toolCallId,
|
|
38
|
+
commandText,
|
|
39
|
+
command,
|
|
40
|
+
output,
|
|
41
|
+
...(reason ? { reason } : {}),
|
|
42
|
+
...(timing ? { timing } : {}),
|
|
43
|
+
};
|
|
36
44
|
}
|
|
37
45
|
|
|
38
46
|
/** Render one durable transcript card per fused command without adding model context. */
|
|
@@ -82,6 +90,10 @@ export function registerFusionCards(pi: ExtensionAPI) {
|
|
|
82
90
|
const pending = current.command === "waiting" || current.command === "running";
|
|
83
91
|
const interrupted = pending && !active.has(current.toolCallId);
|
|
84
92
|
const status = interrupted ? "interrupted (final status unknown)" : current.command;
|
|
93
|
+
const countdown =
|
|
94
|
+
current.command === "running" && !interrupted && current.timing
|
|
95
|
+
? ` · ${current.timing.remainingSeconds}s remaining`
|
|
96
|
+
: "";
|
|
85
97
|
const failed = current.command === "failed" || current.command === "timeout";
|
|
86
98
|
const color =
|
|
87
99
|
pending || current.command === "cancelled"
|
|
@@ -101,10 +113,13 @@ export function registerFusionCards(pi: ExtensionAPI) {
|
|
|
101
113
|
: "customMessageBg";
|
|
102
114
|
box.setBgFn((line) => theme.bg(background, line));
|
|
103
115
|
statusText.setText(
|
|
104
|
-
`${theme.fg("toolTitle", theme.bold("then_run"))} · ${theme.fg(color, status)}`,
|
|
116
|
+
`${theme.fg("toolTitle", theme.bold("then_run"))} · ${theme.fg(color, status + countdown)}`,
|
|
105
117
|
);
|
|
106
118
|
const context = {
|
|
107
|
-
args: {
|
|
119
|
+
args: {
|
|
120
|
+
command: current.commandText,
|
|
121
|
+
...(current.timing ? { timeout: current.timing.timeoutSeconds } : {}),
|
|
122
|
+
},
|
|
108
123
|
toolCallId: current.toolCallId,
|
|
109
124
|
cwd: currentCwd,
|
|
110
125
|
state: rendererState,
|
|
@@ -186,10 +201,10 @@ export function withMutationStatus<TParams extends TSchema, TDetails>(
|
|
|
186
201
|
| { actionFusion?: { mutationCompleted?: boolean; freshness?: string } }
|
|
187
202
|
| undefined;
|
|
188
203
|
const isPartial = options.isPartial && details?.actionFusion?.mutationCompleted !== true;
|
|
189
|
-
//
|
|
204
|
+
// Pi serializes fused failures into one diagnostic; retain it when the card expands.
|
|
190
205
|
const fusedError = context.isError && (context.args as { then_run?: unknown })?.then_run;
|
|
191
206
|
shell.result = fusedError
|
|
192
|
-
? renderToolError(result, theme)
|
|
207
|
+
? renderToolError(result, theme, options.expanded)
|
|
193
208
|
: tool.renderResult!(result, { ...options, isPartial }, theme, {
|
|
194
209
|
...context,
|
|
195
210
|
isPartial,
|