@maheidem/pi-delegate 0.1.1 → 0.1.2
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/application.ts +39 -1
- package/package.json +1 -1
- package/run-store.ts +20 -7
- package/types.ts +11 -1
package/application.ts
CHANGED
|
@@ -29,8 +29,10 @@ import type {
|
|
|
29
29
|
RunInspection,
|
|
30
30
|
RunMetadataV1,
|
|
31
31
|
RunnerOutcome,
|
|
32
|
+
TranscriptRecordV1,
|
|
32
33
|
SessionEntryLike,
|
|
33
34
|
} from "./types.ts";
|
|
35
|
+
import { decodeTranscriptRecord } from "./types.ts";
|
|
34
36
|
import { normalizeConfig, saveConfig, type DelegateConfigV1 } from "./config.ts";
|
|
35
37
|
import { EMPTY_USAGE } from "./types.ts";
|
|
36
38
|
import { isRoleName, resolveRole, rolePromptExists, DELEGATE_ROLES } from "./roles.ts";
|
|
@@ -295,7 +297,7 @@ export class DelegateApplicationImpl implements DelegateApplication {
|
|
|
295
297
|
if (!metadata) throw Object.assign(new Error(`Unknown run id '${id}'.`), { code: "E_RUN_NOT_FOUND" });
|
|
296
298
|
const inspection: RunInspection = { metadata };
|
|
297
299
|
if (includeTranscript) {
|
|
298
|
-
inspection.transcriptPreview =
|
|
300
|
+
inspection.transcriptPreview = transcriptPreview(metadata.transcriptPath, 12000);
|
|
299
301
|
inspection.stderrPreview = boundedPreview(metadata.stderrPath, 4000);
|
|
300
302
|
}
|
|
301
303
|
return inspection;
|
|
@@ -460,6 +462,42 @@ function isTerminal(state: string): boolean {
|
|
|
460
462
|
return ["succeeded", "failed", "cancelled", "timed_out_idle", "timed_out_hard", "crashed"].includes(state);
|
|
461
463
|
}
|
|
462
464
|
|
|
465
|
+
/**
|
|
466
|
+
* Read a transcript envelope back as readable text: decode each record
|
|
467
|
+
* (UTF-8 `raw`, or base64 fallback for non-UTF-8 evidence), bounded.
|
|
468
|
+
*/
|
|
469
|
+
function transcriptPreview(filePath: string, maxChars: number): string {
|
|
470
|
+
try {
|
|
471
|
+
const buf = fs.readFileSync(filePath);
|
|
472
|
+
if (buf.length > 200_000) {
|
|
473
|
+
// Bound the work on huge transcripts: decode the newest records.
|
|
474
|
+
const lines = buf.toString("utf8").split("\n");
|
|
475
|
+
const tail = lines.slice(-Math.max(1, Math.floor(200_000 / 200))).filter((l) => l.trim());
|
|
476
|
+
return decodeTranscriptLines(tail, maxChars, tail.length < lines.filter((l) => l.trim()).length);
|
|
477
|
+
}
|
|
478
|
+
const lines = buf.toString("utf8").split("\n").filter((l) => l.trim());
|
|
479
|
+
return decodeTranscriptLines(lines, maxChars, false);
|
|
480
|
+
} catch {
|
|
481
|
+
return "";
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function decodeTranscriptLines(lines: string[], maxChars: number, hadMore: boolean): string {
|
|
486
|
+
const decoded = lines
|
|
487
|
+
.map((line) => {
|
|
488
|
+
try {
|
|
489
|
+
const rec = JSON.parse(line) as TranscriptRecordV1;
|
|
490
|
+
return decodeTranscriptRecord(rec);
|
|
491
|
+
} catch {
|
|
492
|
+
return line; // non-envelope line kept verbatim
|
|
493
|
+
}
|
|
494
|
+
})
|
|
495
|
+
.filter((t) => t.length > 0);
|
|
496
|
+
let text = decoded.join("\n");
|
|
497
|
+
if (text.length > maxChars) text = `${text.slice(0, maxChars)}\n… [truncated]`;
|
|
498
|
+
return hadMore ? `… [earlier records omitted]\n${text}` : text;
|
|
499
|
+
}
|
|
500
|
+
|
|
463
501
|
function boundedPreview(filePath: string, maxBytes: number): string {
|
|
464
502
|
try {
|
|
465
503
|
const buf = fs.readFileSync(filePath);
|
package/package.json
CHANGED
package/run-store.ts
CHANGED
|
@@ -128,13 +128,26 @@ export function appendTranscriptRecord(
|
|
|
128
128
|
receivedAt: string,
|
|
129
129
|
rawBytes: Buffer,
|
|
130
130
|
): void {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
131
|
+
let record: TranscriptRecordV1;
|
|
132
|
+
try {
|
|
133
|
+
// Normal case: the child emits valid UTF-8 JSON — keep it readable.
|
|
134
|
+
record = {
|
|
135
|
+
schemaVersion: 1,
|
|
136
|
+
sequence,
|
|
137
|
+
receivedAt,
|
|
138
|
+
stream: "stdout",
|
|
139
|
+
raw: new TextDecoder("utf-8", { fatal: true }).decode(rawBytes),
|
|
140
|
+
};
|
|
141
|
+
} catch {
|
|
142
|
+
// Non-UTF-8 evidence is preserved verbatim, never dropped.
|
|
143
|
+
record = {
|
|
144
|
+
schemaVersion: 1,
|
|
145
|
+
sequence,
|
|
146
|
+
receivedAt,
|
|
147
|
+
stream: "stdout",
|
|
148
|
+
rawBase64: rawBytes.toString("base64"),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
138
151
|
const line = JSON.stringify(record) + "\n";
|
|
139
152
|
const fd = (stdoutStream as unknown as { fd: number | null }).fd;
|
|
140
153
|
if (typeof fd === "number") {
|
package/types.ts
CHANGED
|
@@ -195,7 +195,17 @@ export interface TranscriptRecordV1 {
|
|
|
195
195
|
sequence: number;
|
|
196
196
|
receivedAt: string;
|
|
197
197
|
stream: "stdout";
|
|
198
|
-
|
|
198
|
+
/** UTF-8 text of the raw record (the normal case — readable). */
|
|
199
|
+
raw?: string;
|
|
200
|
+
/** Base64 fallback when the bytes are not valid UTF-8. */
|
|
201
|
+
rawBase64?: string;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Decode a captured transcript record back to its original text. */
|
|
205
|
+
export function decodeTranscriptRecord(record: TranscriptRecordV1): string {
|
|
206
|
+
if (typeof record.raw === "string") return record.raw;
|
|
207
|
+
if (typeof record.rawBase64 === "string") return Buffer.from(record.rawBase64, "base64").toString("utf8");
|
|
208
|
+
return "";
|
|
199
209
|
}
|
|
200
210
|
|
|
201
211
|
export interface DelegatePaths {
|