@app-vitals/agent-runtime 1.100.0 → 1.101.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/agent-extension.ts +0 -7
- package/agent-features-ref.ts +2 -4
- package/agent-features-ref.unit.test.ts +2 -5
- package/boot.ts +1 -39
- package/cron-handler.ts +0 -31
- package/cron-handler.unit.test.ts +37 -51
- package/package.json +3 -3
- package/transcript-uploader.ts +0 -169
- package/transcript-uploader.unit.test.ts +0 -257
package/agent-extension.ts
CHANGED
|
@@ -25,7 +25,6 @@ import type { createConfig } from "./config.ts";
|
|
|
25
25
|
import type { createContextStampReader } from "./context-stamp.ts";
|
|
26
26
|
import type { CronRunReporter } from "./cron-run-reporter.ts";
|
|
27
27
|
import type { createFileSessionStore } from "./sessions.ts";
|
|
28
|
-
import type { TranscriptUploader } from "./transcript-uploader.ts";
|
|
29
28
|
|
|
30
29
|
/** Minimal structural view of a cron job, as the seam needs it. */
|
|
31
30
|
export interface ExtensionCronJob {
|
|
@@ -58,12 +57,6 @@ export interface AgentExtensionContext {
|
|
|
58
57
|
cronRunReporter: CronRunReporter | undefined;
|
|
59
58
|
contextStamp: ReturnType<typeof createContextStampReader>;
|
|
60
59
|
sentryClient: ErrorCapturingClient | undefined;
|
|
61
|
-
/**
|
|
62
|
-
* Redacted transcript upload for extension-driven runs (the coding loop).
|
|
63
|
-
* A no-op unless the synced `features.loopTranscriptUpload` is true.
|
|
64
|
-
* Absent when the runtime has no admin API url / agent id.
|
|
65
|
-
*/
|
|
66
|
-
transcriptUploader?: TranscriptUploader;
|
|
67
60
|
}
|
|
68
61
|
|
|
69
62
|
export interface AgentExtension<TBundle = unknown> {
|
package/agent-features-ref.ts
CHANGED
|
@@ -2,15 +2,13 @@
|
|
|
2
2
|
* runtime/agent-features-ref.ts
|
|
3
3
|
*
|
|
4
4
|
* A tiny mutable box holding the `features` object from the most recently
|
|
5
|
-
* synced config bundle
|
|
6
|
-
*
|
|
5
|
+
* synced config bundle, so consumers read a live view without closing over
|
|
6
|
+
* one syncConfig() tick.
|
|
7
7
|
* Absent until the first successful sync (and when talking to an older admin
|
|
8
8
|
* that does not send `features`) — consumers treat that as "all off".
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
export interface AgentFeatures {
|
|
12
|
-
transcriptUpload?: boolean;
|
|
13
|
-
loopTranscriptUpload?: boolean;
|
|
14
12
|
repoAccessCheck?: boolean;
|
|
15
13
|
}
|
|
16
14
|
|
|
@@ -10,11 +10,8 @@ describe("createAgentFeaturesRef", () => {
|
|
|
10
10
|
it("is undefined until set, then reflects the latest features", () => {
|
|
11
11
|
const ref = createAgentFeaturesRef();
|
|
12
12
|
expect(ref.get()).toBeUndefined();
|
|
13
|
-
ref.set({
|
|
14
|
-
expect(ref.get()).toEqual({
|
|
15
|
-
transcriptUpload: true,
|
|
16
|
-
repoAccessCheck: false,
|
|
17
|
-
});
|
|
13
|
+
ref.set({ repoAccessCheck: true });
|
|
14
|
+
expect(ref.get()).toEqual({ repoAccessCheck: true });
|
|
18
15
|
ref.set(undefined);
|
|
19
16
|
expect(ref.get()).toBeUndefined();
|
|
20
17
|
});
|
package/boot.ts
CHANGED
|
@@ -17,7 +17,6 @@
|
|
|
17
17
|
* 8. Graceful SIGTERM/SIGINT shutdown
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
-
import { readdir, readFile } from "node:fs/promises";
|
|
21
20
|
import { join } from "node:path";
|
|
22
21
|
import { initSentry } from "@app-vitals/agent-lib/sentry";
|
|
23
22
|
import * as Sentry from "@sentry/bun";
|
|
@@ -119,10 +118,6 @@ import {
|
|
|
119
118
|
buildProductionDeps as buildSyncConfigCloneDeps,
|
|
120
119
|
syncClonedRepos,
|
|
121
120
|
} from "./sync-config-clone.ts";
|
|
122
|
-
import {
|
|
123
|
-
createTranscriptUploader,
|
|
124
|
-
type TranscriptUploaderDeps,
|
|
125
|
-
} from "./transcript-uploader.ts";
|
|
126
121
|
import { resolveDisplayName, resolveUserEmail } from "./users.ts";
|
|
127
122
|
import {
|
|
128
123
|
buildVoiceConfig,
|
|
@@ -309,25 +304,6 @@ export async function startAgent(
|
|
|
309
304
|
claudeCodeVersion: () => claudeVersionState.version,
|
|
310
305
|
});
|
|
311
306
|
|
|
312
|
-
// COS-43.1: redacted transcript upload deps, shared by the cron handler and
|
|
313
|
-
// the extension capability (they differ only in the gating feature flag).
|
|
314
|
-
const transcriptUploadDeps: TranscriptUploaderDeps | undefined =
|
|
315
|
-
config.shipwright.apiUrl && config.shipwright.agentId
|
|
316
|
-
? {
|
|
317
|
-
fs: {
|
|
318
|
-
readFile: (path) => readFile(path, "utf8"),
|
|
319
|
-
readdir: (path) => readdir(path),
|
|
320
|
-
},
|
|
321
|
-
fetch,
|
|
322
|
-
clock: SystemClock(),
|
|
323
|
-
env: process.env,
|
|
324
|
-
log: (line) => console.warn(line),
|
|
325
|
-
apiUrl: config.shipwright.apiUrl,
|
|
326
|
-
agentId: config.shipwright.agentId,
|
|
327
|
-
getFeatures: () => agentFeaturesRef.get(),
|
|
328
|
-
}
|
|
329
|
-
: undefined;
|
|
330
|
-
|
|
331
307
|
const cronDeps: CronHandlerDeps = {
|
|
332
308
|
// SLK-1.1: a getter, not a plain field — Slack may start after boot (see
|
|
333
309
|
// slack-startup.ts's startSlackIfPossible()), so cron dispatch must read
|
|
@@ -364,11 +340,6 @@ export async function startAgent(
|
|
|
364
340
|
// GOR-1.3: cron spawns carry the default installation's org as the gh hint.
|
|
365
341
|
getDefaultOwner: () =>
|
|
366
342
|
defaultOwnerOf(githubInstallationsManagerRef.get()?.getState?.()),
|
|
367
|
-
// COS-43.1: redacted transcript upload after completeRun; gated by the
|
|
368
|
-
// synced features.transcriptUpload flag, failures swallowed.
|
|
369
|
-
...(transcriptUploadDeps
|
|
370
|
-
? { transcriptUploader: createTranscriptUploader(transcriptUploadDeps) }
|
|
371
|
-
: {}),
|
|
372
343
|
};
|
|
373
344
|
|
|
374
345
|
// ARD-3.1: the specialty extension seam (agent-extension.ts). The consumer
|
|
@@ -383,15 +354,6 @@ export async function startAgent(
|
|
|
383
354
|
cronRunReporter,
|
|
384
355
|
contextStamp,
|
|
385
356
|
sentryClient: process.env.SENTRY_DSN ? Sentry : undefined,
|
|
386
|
-
// Same uploader (redaction, size cap, retention), gated by the loop flag.
|
|
387
|
-
...(transcriptUploadDeps
|
|
388
|
-
? {
|
|
389
|
-
transcriptUploader: createTranscriptUploader({
|
|
390
|
-
...transcriptUploadDeps,
|
|
391
|
-
featureFlag: "loopTranscriptUpload",
|
|
392
|
-
}),
|
|
393
|
-
}
|
|
394
|
-
: {}),
|
|
395
357
|
};
|
|
396
358
|
const extension = composeExtensions(
|
|
397
359
|
extensions.map((createExtension) => createExtension(extensionContext)),
|
|
@@ -659,7 +621,7 @@ export async function startAgent(
|
|
|
659
621
|
// runtime's worktree reaper.
|
|
660
622
|
cleanupAfterDaysRef.set(bundle.cleanupAfterDays);
|
|
661
623
|
|
|
662
|
-
// Sync the feature flags (
|
|
624
|
+
// Sync the feature flags (repoAccessCheck etc.).
|
|
663
625
|
agentFeaturesRef.set(bundle.features);
|
|
664
626
|
} catch (err) {
|
|
665
627
|
if (
|
package/cron-handler.ts
CHANGED
|
@@ -33,7 +33,6 @@ import { parseMarkers } from "./markers.ts";
|
|
|
33
33
|
import type { RunTelemetry } from "./run-telemetry.ts";
|
|
34
34
|
import { dispatchMarkers, type SynthesizeSpeechFn } from "./slack.ts";
|
|
35
35
|
import { buildSpawnEnv } from "./spawn-env.ts";
|
|
36
|
-
import type { TranscriptUploader } from "./transcript-uploader.ts";
|
|
37
36
|
import type { VoiceConfig } from "./voice.ts";
|
|
38
37
|
|
|
39
38
|
/**
|
|
@@ -217,11 +216,6 @@ export interface CronHandlerDeps {
|
|
|
217
216
|
* spawning ~15 real bun child processes per test run (CPS-1.1).
|
|
218
217
|
*/
|
|
219
218
|
spawner?: typeof Bun.spawn;
|
|
220
|
-
/**
|
|
221
|
-
* Uploads the run's redacted session transcript once completeRun has been
|
|
222
|
-
* recorded (COS-43.1). Feature-gated inside the uploader; absent → no-op.
|
|
223
|
-
*/
|
|
224
|
-
transcriptUploader?: TranscriptUploader;
|
|
225
219
|
}
|
|
226
220
|
|
|
227
221
|
export async function handleCronRequest(
|
|
@@ -252,7 +246,6 @@ export async function handleCronRequest(
|
|
|
252
246
|
contextStamp,
|
|
253
247
|
getDefaultOwner,
|
|
254
248
|
spawner = Bun.spawn,
|
|
255
|
-
transcriptUploader,
|
|
256
249
|
} = deps;
|
|
257
250
|
|
|
258
251
|
const startedAt = clock.now();
|
|
@@ -278,24 +271,6 @@ export async function handleCronRequest(
|
|
|
278
271
|
)
|
|
279
272
|
: null;
|
|
280
273
|
|
|
281
|
-
// Best-effort redacted transcript upload (COS-43.1). Runs only after the
|
|
282
|
-
// run is recorded and can never fail or delay-fail the dispatch.
|
|
283
|
-
const uploadTranscript = async (sid: string | undefined): Promise<void> => {
|
|
284
|
-
if (!transcriptUploader || !runId) return;
|
|
285
|
-
try {
|
|
286
|
-
await transcriptUploader.upload({
|
|
287
|
-
cronId: jobId,
|
|
288
|
-
runId,
|
|
289
|
-
sessionId: sid,
|
|
290
|
-
cwd: workspace ?? process.cwd(),
|
|
291
|
-
});
|
|
292
|
-
} catch (err) {
|
|
293
|
-
console.warn(
|
|
294
|
-
`[agent:cron] transcript upload failed for run ${runId}: ${String(err)} — swallowing`,
|
|
295
|
-
);
|
|
296
|
-
}
|
|
297
|
-
};
|
|
298
|
-
|
|
299
274
|
if (req.preCheck) {
|
|
300
275
|
const isRelative =
|
|
301
276
|
req.preCheck.startsWith("./") || req.preCheck.startsWith("../");
|
|
@@ -618,7 +593,6 @@ export async function handleCronRequest(
|
|
|
618
593
|
...tokenPayload,
|
|
619
594
|
sessionId: failureSessionId,
|
|
620
595
|
});
|
|
621
|
-
await uploadTranscript(failureSessionId);
|
|
622
596
|
markCronRunFailureReported(err);
|
|
623
597
|
throw err;
|
|
624
598
|
}
|
|
@@ -637,7 +611,6 @@ export async function handleCronRequest(
|
|
|
637
611
|
...buildTokenPayload(usage, modelUsage, extras(telemetry)),
|
|
638
612
|
sessionId,
|
|
639
613
|
});
|
|
640
|
-
await uploadTranscript(sessionId);
|
|
641
614
|
return;
|
|
642
615
|
}
|
|
643
616
|
const isDmOnly = !!user && !channel;
|
|
@@ -649,7 +622,6 @@ export async function handleCronRequest(
|
|
|
649
622
|
...buildTokenPayload(usage, modelUsage, extras(telemetry)),
|
|
650
623
|
sessionId,
|
|
651
624
|
});
|
|
652
|
-
await uploadTranscript(sessionId);
|
|
653
625
|
return;
|
|
654
626
|
}
|
|
655
627
|
|
|
@@ -664,7 +636,6 @@ export async function handleCronRequest(
|
|
|
664
636
|
...buildTokenPayload(usage, modelUsage, extras(telemetry)),
|
|
665
637
|
sessionId,
|
|
666
638
|
});
|
|
667
|
-
await uploadTranscript(sessionId);
|
|
668
639
|
return;
|
|
669
640
|
}
|
|
670
641
|
|
|
@@ -692,7 +663,6 @@ export async function handleCronRequest(
|
|
|
692
663
|
...buildTokenPayload(usage, modelUsage, extras(telemetry)),
|
|
693
664
|
sessionId,
|
|
694
665
|
});
|
|
695
|
-
await uploadTranscript(sessionId);
|
|
696
666
|
|
|
697
667
|
// The run is already recorded as "completed" above — any error from here
|
|
698
668
|
// on (postMessage, dispatchMarkers) must be tagged before rethrowing so
|
|
@@ -734,7 +704,6 @@ export async function handleCronRequest(
|
|
|
734
704
|
...buildTokenPayload(usage, modelUsage, extras(telemetry)),
|
|
735
705
|
sessionId,
|
|
736
706
|
});
|
|
737
|
-
await uploadTranscript(sessionId);
|
|
738
707
|
|
|
739
708
|
// The run is already recorded as "completed" above — any error from here
|
|
740
709
|
// on (conversations.open, postMessage, dispatchMarkers) must be tagged
|
|
@@ -32,6 +32,7 @@ import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
|
32
32
|
import { tmpdir } from "node:os";
|
|
33
33
|
import { join } from "node:path";
|
|
34
34
|
import type { WebClient } from "@slack/web-api";
|
|
35
|
+
import { type AgentFeatures, agentFeaturesRef } from "./agent-features-ref.ts";
|
|
35
36
|
import {
|
|
36
37
|
ClaudeRunError,
|
|
37
38
|
type ClaudeRunResult,
|
|
@@ -1204,60 +1205,45 @@ describe("handleCronRequest — commandVariant (COS-42.1)", () => {
|
|
|
1204
1205
|
});
|
|
1205
1206
|
});
|
|
1206
1207
|
|
|
1207
|
-
// ─── transcript
|
|
1208
|
+
// ─── legacy transcript feature flags ─────────────────────────────────────────
|
|
1208
1209
|
|
|
1209
|
-
describe("handleCronRequest — transcript
|
|
1210
|
-
|
|
1211
|
-
return {
|
|
1212
|
-
async createRun() {
|
|
1213
|
-
return "run-tx-1";
|
|
1214
|
-
},
|
|
1215
|
-
async completeRun() {
|
|
1216
|
-
order.push("completeRun");
|
|
1217
|
-
},
|
|
1218
|
-
async skipRun() {},
|
|
1219
|
-
async recordProgress() {},
|
|
1220
|
-
async recordSessionId() {},
|
|
1221
|
-
};
|
|
1222
|
-
}
|
|
1223
|
-
|
|
1224
|
-
test("uploads after completeRun with the run's cron, run, session and cwd", async () => {
|
|
1210
|
+
describe("handleCronRequest — legacy transcript feature flags", () => {
|
|
1211
|
+
test("a config bundle still carrying the old flags triggers no upload and no error", async () => {
|
|
1225
1212
|
const order: string[] = [];
|
|
1226
|
-
const
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1213
|
+
const fetchCalls: unknown[] = [];
|
|
1214
|
+
const realFetch = globalThis.fetch;
|
|
1215
|
+
globalThis.fetch = ((...args: unknown[]) => {
|
|
1216
|
+
fetchCalls.push(args);
|
|
1217
|
+
return Promise.resolve(new Response("{}"));
|
|
1218
|
+
}) as typeof fetch;
|
|
1219
|
+
// Admin (until 2.3) still emits these keys; the runtime must ignore them.
|
|
1220
|
+
agentFeaturesRef.set({
|
|
1221
|
+
transcriptUpload: true,
|
|
1222
|
+
loopTranscriptUpload: true,
|
|
1223
|
+
} as AgentFeatures);
|
|
1224
|
+
try {
|
|
1225
|
+
const reporter: CronRunReporter = {
|
|
1226
|
+
async createRun() {
|
|
1227
|
+
return "run-legacy-1";
|
|
1238
1228
|
},
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
expect(order).toEqual(["completeRun", "upload"]);
|
|
1242
|
-
expect(calls).toEqual([
|
|
1243
|
-
{ cronId: "j-tx", runId: "run-tx-1", sessionId: "sess-1", cwd: "/ws" },
|
|
1244
|
-
]);
|
|
1245
|
-
});
|
|
1246
|
-
|
|
1247
|
-
test("an uploader rejection never fails the run", async () => {
|
|
1248
|
-
await expect(
|
|
1249
|
-
handleCronRequest(
|
|
1250
|
-
{ jobId: "j-tx2", prompt: "hello", silent: true },
|
|
1251
|
-
{
|
|
1252
|
-
...deps,
|
|
1253
|
-
cronRunReporter: recordingReporter([]),
|
|
1254
|
-
transcriptUploader: {
|
|
1255
|
-
async upload() {
|
|
1256
|
-
throw new Error("boom");
|
|
1257
|
-
},
|
|
1258
|
-
},
|
|
1229
|
+
async completeRun() {
|
|
1230
|
+
order.push("completeRun");
|
|
1259
1231
|
},
|
|
1260
|
-
|
|
1261
|
-
|
|
1232
|
+
async skipRun() {},
|
|
1233
|
+
async recordProgress() {},
|
|
1234
|
+
async recordSessionId() {},
|
|
1235
|
+
};
|
|
1236
|
+
await expect(
|
|
1237
|
+
handleCronRequest(
|
|
1238
|
+
{ jobId: "j-legacy", prompt: "hello", silent: true },
|
|
1239
|
+
{ ...deps, cronRunReporter: reporter },
|
|
1240
|
+
),
|
|
1241
|
+
).resolves.toBeUndefined();
|
|
1242
|
+
expect(order).toEqual(["completeRun"]);
|
|
1243
|
+
expect(fetchCalls).toEqual([]);
|
|
1244
|
+
} finally {
|
|
1245
|
+
agentFeaturesRef.set(undefined);
|
|
1246
|
+
globalThis.fetch = realFetch;
|
|
1247
|
+
}
|
|
1262
1248
|
});
|
|
1263
1249
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-vitals/agent-runtime",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.101.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/app-vitals/agent.git",
|
|
@@ -41,8 +41,8 @@
|
|
|
41
41
|
"./workspace-paths": "./workspace-paths.ts"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@app-vitals/agent-admin": "1.
|
|
45
|
-
"@app-vitals/agent-lib": "1.
|
|
44
|
+
"@app-vitals/agent-admin": "1.101.1",
|
|
45
|
+
"@app-vitals/agent-lib": "1.101.1",
|
|
46
46
|
"@octokit/auth-app": "^8.2.0",
|
|
47
47
|
"@sentry/bun": "^10.63.0",
|
|
48
48
|
"@slack/bolt": "^5.0.0",
|
package/transcript-uploader.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* runtime/transcript-uploader.ts
|
|
3
|
-
* Uploads a redacted copy of a cron run's Claude session transcript to the
|
|
4
|
-
* admin API (COS-43.1) once the run is recorded.
|
|
5
|
-
*
|
|
6
|
-
* Gated by the synced config bundle's `features.transcriptUpload` (the admin
|
|
7
|
-
* flag SHIPWRIGHT_TRANSCRIPT_API_ENABLED), or `features.loopTranscriptUpload`
|
|
8
|
-
* for the extension-facing loop uploader. Redaction happens HERE, before the
|
|
9
|
-
* transcript leaves the pod: live SECRET_ENV_VARS values plus well-known token
|
|
10
|
-
* shapes (lib/redact.ts). The body is tail-truncated to 1 MiB on whole lines
|
|
11
|
-
* with a marker line first. Every failure logs one line (never the body) and
|
|
12
|
-
* is swallowed — an upload problem must never fail or alter a run.
|
|
13
|
-
*
|
|
14
|
-
* All I/O is injected ({ fs, fetch, clock, env, log }) so it is unit-testable.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { join } from "node:path";
|
|
18
|
-
import { redact } from "@app-vitals/agent-lib/redact";
|
|
19
|
-
import type { Clock } from "./clock.ts";
|
|
20
|
-
|
|
21
|
-
/** Upload cap; the admin route 413s above it. */
|
|
22
|
-
export const MAX_TRANSCRIPT_BYTES = 1024 * 1024;
|
|
23
|
-
|
|
24
|
-
/** Bytes reserved for the truncation marker line. */
|
|
25
|
-
const MARKER_RESERVE = 256;
|
|
26
|
-
|
|
27
|
-
export interface TranscriptFs {
|
|
28
|
-
readFile(path: string): Promise<string>;
|
|
29
|
-
readdir(path: string): Promise<string[]>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface TranscriptFeatures {
|
|
33
|
-
transcriptUpload?: boolean;
|
|
34
|
-
loopTranscriptUpload?: boolean;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface TranscriptUploaderDeps {
|
|
38
|
-
fs: TranscriptFs;
|
|
39
|
-
fetch: typeof fetch;
|
|
40
|
-
clock: Clock;
|
|
41
|
-
env: Readonly<Record<string, string | undefined>>;
|
|
42
|
-
/** One-line logger; never receives transcript content. */
|
|
43
|
-
log: (line: string) => void;
|
|
44
|
-
apiUrl: string;
|
|
45
|
-
agentId: string;
|
|
46
|
-
/** The last synced `features` from the config bundle (absent before sync). */
|
|
47
|
-
getFeatures: () => TranscriptFeatures | undefined;
|
|
48
|
-
/**
|
|
49
|
-
* Which synced feature flag gates uploads. Defaults to `transcriptUpload`
|
|
50
|
-
* (regular cron runs); the loop capability uses `loopTranscriptUpload`.
|
|
51
|
-
*/
|
|
52
|
-
featureFlag?: keyof TranscriptFeatures;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export interface TranscriptUploadArgs {
|
|
56
|
-
cronId: string;
|
|
57
|
-
runId: string;
|
|
58
|
-
sessionId: string | undefined;
|
|
59
|
-
/** The cwd the run's Claude process was spawned in. */
|
|
60
|
-
cwd: string;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export interface TranscriptUploader {
|
|
64
|
-
upload(args: TranscriptUploadArgs): Promise<void>;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** Claude Code's project-dir naming: every "/" and "." becomes "-". */
|
|
68
|
-
export function encodeCwd(cwd: string): string {
|
|
69
|
-
return cwd.replace(/[/.]/g, "-");
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function readTranscript(
|
|
73
|
-
deps: TranscriptUploaderDeps,
|
|
74
|
-
cwd: string,
|
|
75
|
-
sessionId: string,
|
|
76
|
-
): Promise<string> {
|
|
77
|
-
const home = deps.env.AGENT_HOME ?? "";
|
|
78
|
-
const projects = join(home, "dot-claude", "projects");
|
|
79
|
-
const primary = join(projects, encodeCwd(cwd), `${sessionId}.jsonl`);
|
|
80
|
-
try {
|
|
81
|
-
return await deps.fs.readFile(primary);
|
|
82
|
-
} catch {
|
|
83
|
-
// Fall back to scanning every project dir for the session file.
|
|
84
|
-
}
|
|
85
|
-
for (const dir of await deps.fs.readdir(projects)) {
|
|
86
|
-
try {
|
|
87
|
-
return await deps.fs.readFile(join(projects, dir, `${sessionId}.jsonl`));
|
|
88
|
-
} catch {
|
|
89
|
-
// not in this project dir
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
throw new Error("transcript file not found");
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** Keeps whole trailing lines within the cap, prefixed by a marker line. */
|
|
96
|
-
function tailTruncate(text: string): { body: string; truncated: boolean } {
|
|
97
|
-
if (Buffer.byteLength(text) <= MAX_TRANSCRIPT_BYTES) {
|
|
98
|
-
return { body: text, truncated: false };
|
|
99
|
-
}
|
|
100
|
-
const lines = text.split("\n");
|
|
101
|
-
if (lines[lines.length - 1] === "") lines.pop();
|
|
102
|
-
const budget = MAX_TRANSCRIPT_BYTES - MARKER_RESERVE;
|
|
103
|
-
let used = 0;
|
|
104
|
-
let keepFrom = lines.length;
|
|
105
|
-
while (keepFrom > 0) {
|
|
106
|
-
const size = Buffer.byteLength(lines[keepFrom - 1] as string) + 1;
|
|
107
|
-
if (used + size > budget) break;
|
|
108
|
-
used += size;
|
|
109
|
-
keepFrom--;
|
|
110
|
-
}
|
|
111
|
-
const marker = JSON.stringify({
|
|
112
|
-
type: "transcript-truncated",
|
|
113
|
-
note: "earlier lines dropped to fit the 1 MiB transcript cap",
|
|
114
|
-
droppedLines: keepFrom,
|
|
115
|
-
keptLines: lines.length - keepFrom,
|
|
116
|
-
});
|
|
117
|
-
const kept = lines.slice(keepFrom);
|
|
118
|
-
return {
|
|
119
|
-
body: `${marker}\n${kept.length ? `${kept.join("\n")}\n` : ""}`,
|
|
120
|
-
truncated: true,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function createTranscriptUploader(
|
|
125
|
-
deps: TranscriptUploaderDeps,
|
|
126
|
-
): TranscriptUploader {
|
|
127
|
-
return {
|
|
128
|
-
async upload({ cronId, runId, sessionId, cwd }) {
|
|
129
|
-
if (
|
|
130
|
-
deps.getFeatures()?.[deps.featureFlag ?? "transcriptUpload"] !== true
|
|
131
|
-
) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
if (!sessionId) return;
|
|
135
|
-
const apiKey = deps.env.SHIPWRIGHT_AGENT_API_KEY;
|
|
136
|
-
if (!apiKey) return;
|
|
137
|
-
const started = deps.clock.now().getTime();
|
|
138
|
-
try {
|
|
139
|
-
const raw = await readTranscript(deps, cwd, sessionId);
|
|
140
|
-
const { text, count } = redact(raw, deps.env);
|
|
141
|
-
const { body, truncated } = tailTruncate(text);
|
|
142
|
-
const url = `${deps.apiUrl}/agents/${deps.agentId}/crons/${cronId}/runs/${runId}/transcript`;
|
|
143
|
-
const res = await deps.fetch(url, {
|
|
144
|
-
method: "PUT",
|
|
145
|
-
headers: {
|
|
146
|
-
"Content-Type": "application/json",
|
|
147
|
-
Authorization: `Bearer ${apiKey}`,
|
|
148
|
-
},
|
|
149
|
-
body: JSON.stringify({
|
|
150
|
-
sessionId,
|
|
151
|
-
body,
|
|
152
|
-
truncated,
|
|
153
|
-
redactions: count,
|
|
154
|
-
}),
|
|
155
|
-
});
|
|
156
|
-
if (!res.ok) {
|
|
157
|
-
deps.log(
|
|
158
|
-
`[transcript-upload] run ${runId} upload returned ${res.status} — skipped`,
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
} catch (err) {
|
|
162
|
-
const ms = deps.clock.now().getTime() - started;
|
|
163
|
-
deps.log(
|
|
164
|
-
`[transcript-upload] run ${runId} failed after ${ms}ms: ${err instanceof Error ? err.message : String(err)} — skipped`,
|
|
165
|
-
);
|
|
166
|
-
}
|
|
167
|
-
},
|
|
168
|
-
};
|
|
169
|
-
}
|
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* runtime/transcript-uploader.unit.test.ts
|
|
3
|
-
* COS-43.1: path resolution + fallback, truncation marker, feature off,
|
|
4
|
-
* redaction before upload, errors swallowed. All deps injected.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { describe, expect, it } from "bun:test";
|
|
8
|
-
import {
|
|
9
|
-
createTranscriptUploader,
|
|
10
|
-
MAX_TRANSCRIPT_BYTES,
|
|
11
|
-
type TranscriptFs,
|
|
12
|
-
} from "./transcript-uploader.ts";
|
|
13
|
-
|
|
14
|
-
const HOME = "/data/agent-home";
|
|
15
|
-
const CWD = "/data/agent-home/workspace";
|
|
16
|
-
const ENCODED = "-data-agent-home-workspace";
|
|
17
|
-
const SESSION = "sess-123";
|
|
18
|
-
const PRIMARY = `${HOME}/dot-claude/projects/${ENCODED}/${SESSION}.jsonl`;
|
|
19
|
-
|
|
20
|
-
function memFs(files: Record<string, string>): TranscriptFs {
|
|
21
|
-
return {
|
|
22
|
-
async readFile(path) {
|
|
23
|
-
const f = files[path];
|
|
24
|
-
if (f === undefined) throw new Error(`ENOENT ${path}`);
|
|
25
|
-
return f;
|
|
26
|
-
},
|
|
27
|
-
async readdir(path) {
|
|
28
|
-
const prefix = `${path}/`;
|
|
29
|
-
return [
|
|
30
|
-
...new Set(
|
|
31
|
-
Object.keys(files)
|
|
32
|
-
.filter((p) => p.startsWith(prefix))
|
|
33
|
-
.map((p) => p.slice(prefix.length).split("/")[0] as string),
|
|
34
|
-
),
|
|
35
|
-
];
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
interface Call {
|
|
41
|
-
url: string;
|
|
42
|
-
init: RequestInit;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function setup(
|
|
46
|
-
files: Record<string, string>,
|
|
47
|
-
opts: {
|
|
48
|
-
features?:
|
|
49
|
-
| { transcriptUpload?: boolean; loopTranscriptUpload?: boolean }
|
|
50
|
-
| undefined;
|
|
51
|
-
featureFlag?: "transcriptUpload" | "loopTranscriptUpload";
|
|
52
|
-
status?: number;
|
|
53
|
-
throwFetch?: boolean;
|
|
54
|
-
env?: Record<string, string>;
|
|
55
|
-
} = {},
|
|
56
|
-
) {
|
|
57
|
-
const calls: Call[] = [];
|
|
58
|
-
const logs: string[] = [];
|
|
59
|
-
const features =
|
|
60
|
-
"features" in opts ? opts.features : { transcriptUpload: true };
|
|
61
|
-
const uploader = createTranscriptUploader({
|
|
62
|
-
fs: memFs(files),
|
|
63
|
-
fetch: (async (url: string, init: RequestInit) => {
|
|
64
|
-
calls.push({ url, init });
|
|
65
|
-
if (opts.throwFetch) throw new Error("network down");
|
|
66
|
-
return new Response("{}", { status: opts.status ?? 200 });
|
|
67
|
-
}) as unknown as typeof fetch,
|
|
68
|
-
clock: { now: () => new Date("2026-10-26T00:00:00Z") },
|
|
69
|
-
env: {
|
|
70
|
-
AGENT_HOME: HOME,
|
|
71
|
-
SHIPWRIGHT_AGENT_API_KEY: "agent-key-xyz",
|
|
72
|
-
...opts.env,
|
|
73
|
-
},
|
|
74
|
-
log: (l) => logs.push(l),
|
|
75
|
-
apiUrl: "https://admin.example",
|
|
76
|
-
agentId: "agent-1",
|
|
77
|
-
getFeatures: () => features,
|
|
78
|
-
featureFlag: opts.featureFlag,
|
|
79
|
-
});
|
|
80
|
-
const run = (sessionId: string | null = SESSION) =>
|
|
81
|
-
uploader.upload({
|
|
82
|
-
cronId: "cron-1",
|
|
83
|
-
runId: "run-1",
|
|
84
|
-
sessionId: sessionId ?? undefined,
|
|
85
|
-
cwd: CWD,
|
|
86
|
-
});
|
|
87
|
-
return { calls, logs, run };
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
describe("transcript uploader", () => {
|
|
91
|
-
it("PUTs the redacted transcript to the run's transcript path", async () => {
|
|
92
|
-
const { calls, run } = setup({ [PRIMARY]: '{"a":1}\n{"b":2}\n' });
|
|
93
|
-
await run();
|
|
94
|
-
expect(calls).toHaveLength(1);
|
|
95
|
-
expect(calls[0]?.url).toBe(
|
|
96
|
-
"https://admin.example/agents/agent-1/crons/cron-1/runs/run-1/transcript",
|
|
97
|
-
);
|
|
98
|
-
const init = calls[0]?.init as RequestInit;
|
|
99
|
-
expect(init.method).toBe("PUT");
|
|
100
|
-
expect((init.headers as Record<string, string>).Authorization).toBe(
|
|
101
|
-
"Bearer agent-key-xyz",
|
|
102
|
-
);
|
|
103
|
-
expect(JSON.parse(init.body as string)).toEqual({
|
|
104
|
-
sessionId: SESSION,
|
|
105
|
-
body: '{"a":1}\n{"b":2}\n',
|
|
106
|
-
truncated: false,
|
|
107
|
-
redactions: 0,
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it("falls back to projects/*/<sessionId>.jsonl when the encoded cwd misses", async () => {
|
|
112
|
-
const other = `${HOME}/dot-claude/projects/-somewhere-else/${SESSION}.jsonl`;
|
|
113
|
-
const { calls, run } = setup({
|
|
114
|
-
[other]: "fallback\n",
|
|
115
|
-
[`${HOME}/dot-claude/projects/-x/other.jsonl`]: "nope\n",
|
|
116
|
-
});
|
|
117
|
-
await run();
|
|
118
|
-
expect(calls).toHaveLength(1);
|
|
119
|
-
expect(JSON.parse(calls[0]?.init.body as string).body).toBe("fallback\n");
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it("encodes both / and . in the cwd", async () => {
|
|
123
|
-
const dotted = "/data/agent.home/work";
|
|
124
|
-
const p = `${HOME}/dot-claude/projects/-data-agent-home-work/${SESSION}.jsonl`;
|
|
125
|
-
const calls: string[] = [];
|
|
126
|
-
const up = createTranscriptUploader({
|
|
127
|
-
fs: memFs({ [p]: "ok\n" }),
|
|
128
|
-
fetch: (async (_u: string, init: RequestInit) => {
|
|
129
|
-
calls.push(init.body as string);
|
|
130
|
-
return new Response("{}");
|
|
131
|
-
}) as unknown as typeof fetch,
|
|
132
|
-
clock: { now: () => new Date() },
|
|
133
|
-
env: { AGENT_HOME: HOME, SHIPWRIGHT_AGENT_API_KEY: "k" },
|
|
134
|
-
log: () => {},
|
|
135
|
-
apiUrl: "https://a",
|
|
136
|
-
agentId: "a1",
|
|
137
|
-
getFeatures: () => ({ transcriptUpload: true }),
|
|
138
|
-
});
|
|
139
|
-
await up.upload({
|
|
140
|
-
cronId: "c",
|
|
141
|
-
runId: "r",
|
|
142
|
-
sessionId: SESSION,
|
|
143
|
-
cwd: dotted,
|
|
144
|
-
});
|
|
145
|
-
expect(calls).toHaveLength(1);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it("redacts secrets and reports the count", async () => {
|
|
149
|
-
const tok = `ghp_${"A1b2C3d4E5".repeat(3)}`;
|
|
150
|
-
const { calls, run } = setup({ [PRIMARY]: `line ${tok}\n` });
|
|
151
|
-
await run();
|
|
152
|
-
const body = JSON.parse(calls[0]?.init.body as string);
|
|
153
|
-
expect(body.body).not.toContain(tok);
|
|
154
|
-
expect(body.redactions).toBe(1);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it("tail-truncates to 1 MiB keeping whole lines with a marker first", async () => {
|
|
158
|
-
const line = `${JSON.stringify({ pad: "x".repeat(1000) })}\n`;
|
|
159
|
-
const count = Math.ceil((MAX_TRANSCRIPT_BYTES * 1.5) / line.length);
|
|
160
|
-
const lines = Array.from({ length: count }, (_, i) =>
|
|
161
|
-
JSON.stringify({ i, pad: "x".repeat(1000) }),
|
|
162
|
-
);
|
|
163
|
-
const { calls, run } = setup({ [PRIMARY]: `${lines.join("\n")}\n` });
|
|
164
|
-
await run();
|
|
165
|
-
const body = JSON.parse(calls[0]?.init.body as string);
|
|
166
|
-
expect(body.truncated).toBe(true);
|
|
167
|
-
expect(Buffer.byteLength(body.body)).toBeLessThanOrEqual(
|
|
168
|
-
MAX_TRANSCRIPT_BYTES,
|
|
169
|
-
);
|
|
170
|
-
const out = (body.body as string).split("\n").filter(Boolean);
|
|
171
|
-
expect(JSON.parse(out[0] as string).type).toBe("transcript-truncated");
|
|
172
|
-
// The kept tail is whole lines ending at the final input line.
|
|
173
|
-
expect(JSON.parse(out[out.length - 1] as string).i).toBe(count - 1);
|
|
174
|
-
for (const l of out) JSON.parse(l);
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
it("does nothing when the feature is off, absent, or sessionId unknown", async () => {
|
|
178
|
-
for (const features of [{ transcriptUpload: false }, undefined]) {
|
|
179
|
-
const { calls, run } = setup({ [PRIMARY]: "x\n" }, { features });
|
|
180
|
-
await run();
|
|
181
|
-
expect(calls).toHaveLength(0);
|
|
182
|
-
}
|
|
183
|
-
const { calls, run } = setup({ [PRIMARY]: "x\n" });
|
|
184
|
-
await run(null);
|
|
185
|
-
expect(calls).toHaveLength(0);
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
it("loop uploader never uploads for an agent not on the allowlist", async () => {
|
|
189
|
-
for (const features of [
|
|
190
|
-
{ transcriptUpload: true },
|
|
191
|
-
{ transcriptUpload: true, loopTranscriptUpload: false },
|
|
192
|
-
undefined,
|
|
193
|
-
]) {
|
|
194
|
-
const { calls, run } = setup(
|
|
195
|
-
{ [PRIMARY]: "x\n" },
|
|
196
|
-
{ features, featureFlag: "loopTranscriptUpload" },
|
|
197
|
-
);
|
|
198
|
-
await run();
|
|
199
|
-
expect(calls).toHaveLength(0);
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
it("loop uploader uploads when loopTranscriptUpload is true, regardless of transcriptUpload", async () => {
|
|
204
|
-
const { calls, run } = setup(
|
|
205
|
-
{ [PRIMARY]: "x\n" },
|
|
206
|
-
{
|
|
207
|
-
features: { transcriptUpload: false, loopTranscriptUpload: true },
|
|
208
|
-
featureFlag: "loopTranscriptUpload",
|
|
209
|
-
},
|
|
210
|
-
);
|
|
211
|
-
await run();
|
|
212
|
-
expect(calls).toHaveLength(1);
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
it("regular uploader ignores loopTranscriptUpload", async () => {
|
|
216
|
-
const { calls, run } = setup(
|
|
217
|
-
{ [PRIMARY]: "x\n" },
|
|
218
|
-
{ features: { loopTranscriptUpload: true } },
|
|
219
|
-
);
|
|
220
|
-
await run();
|
|
221
|
-
expect(calls).toHaveLength(0);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it("does nothing without an API key", async () => {
|
|
225
|
-
const { calls, run } = setup(
|
|
226
|
-
{ [PRIMARY]: "x\n" },
|
|
227
|
-
{ env: { SHIPWRIGHT_AGENT_API_KEY: "" } },
|
|
228
|
-
);
|
|
229
|
-
await run();
|
|
230
|
-
expect(calls).toHaveLength(0);
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
it("swallows a missing file, a fetch failure and a non-2xx with one log line each", async () => {
|
|
234
|
-
const missing = setup({});
|
|
235
|
-
await missing.run();
|
|
236
|
-
expect(missing.logs).toHaveLength(1);
|
|
237
|
-
expect(missing.calls).toHaveLength(0);
|
|
238
|
-
|
|
239
|
-
const thrown = setup({ [PRIMARY]: "x\n" }, { throwFetch: true });
|
|
240
|
-
await thrown.run();
|
|
241
|
-
expect(thrown.logs).toHaveLength(1);
|
|
242
|
-
|
|
243
|
-
const bad = setup({ [PRIMARY]: "x\n" }, { status: 500 });
|
|
244
|
-
await bad.run();
|
|
245
|
-
expect(bad.logs).toHaveLength(1);
|
|
246
|
-
expect(bad.logs[0]).toContain("500");
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
it("never logs the transcript body", async () => {
|
|
250
|
-
const { logs, run } = setup(
|
|
251
|
-
{ [PRIMARY]: "TOP-SECRET-BODY\n" },
|
|
252
|
-
{ status: 500 },
|
|
253
|
-
);
|
|
254
|
-
await run();
|
|
255
|
-
expect(logs.join("\n")).not.toContain("TOP-SECRET-BODY");
|
|
256
|
-
});
|
|
257
|
-
});
|