@agent-native/core 0.24.9 → 0.25.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/dist/agent/run-manager.d.ts.map +1 -1
- package/dist/agent/run-manager.js +26 -13
- package/dist/agent/run-manager.js.map +1 -1
- package/dist/client/agent-chat-adapter.d.ts.map +1 -1
- package/dist/client/agent-chat-adapter.js +51 -6
- package/dist/client/agent-chat-adapter.js.map +1 -1
- package/dist/client/composer/ComposerPlusMenu.d.ts.map +1 -1
- package/dist/client/composer/ComposerPlusMenu.js +138 -5
- package/dist/client/composer/ComposerPlusMenu.js.map +1 -1
- package/dist/client/resources/ResourcesPanel.d.ts.map +1 -1
- package/dist/client/resources/ResourcesPanel.js +124 -8
- package/dist/client/resources/ResourcesPanel.js.map +1 -1
- package/dist/server/onboarding-html.js +1 -1
- package/dist/server/onboarding-html.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run-manager.d.ts","sourceRoot":"","sources":["../../src/agent/run-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAqBtE,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,6EAA6E;IAC7E,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,QAAQ,EAAE,CAAC;IACnB,MAAM,EAAE,SAAS,CAAC;IAClB,WAAW,EAAE,GAAG,CAAC,CAAC,KAAK,EAAE,QAAQ,KAAK,IAAI,CAAC,CAAC;IAC5C,KAAK,EAAE,eAAe,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB;AAQD;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,kCAAkC,QAAS,CAAC;AAEzD;;;;;;;GAOG;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AW,KAChB,OAAO,CAAC,IAAI,CAAC,EAClB,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,SAAS,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EACrD,OAAO,CAAC,EAAE,eAAe,GACxB,SAAS,CAuWX;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,GACd,cAAc,CAAC,UAAU,CAAC,GAAG,IAAI,CAOnC;AA8PD,wEAAwE;AACxE,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAOxE;AAED;;;;;;;;;GASG;AACH,wBAAsB,0BAA0B,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC;IAC1E,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B,GAAG,IAAI,CAAC,CAyER;AAkBD,sBAAsB;AACtB,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAEtD;AAED,gDAAgD;AAChD,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,MAAe,GAAG,OAAO,CAQxE"}
|
|
@@ -347,24 +347,37 @@ export function startRun(runId, threadId, runFn, onComplete, options) {
|
|
|
347
347
|
// immediately, so emitting it earlier recreates the final-message
|
|
348
348
|
// race this manager is meant to avoid.
|
|
349
349
|
if (finalStatus === "completed" || finalStatus === "errored") {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
350
|
+
// Choose the terminal event payload (done / the stashed terminal /
|
|
351
|
+
// a synthesized error). NOTE: the `seq` carried by
|
|
352
|
+
// `pendingTerminalEvent` was captured by `send()` at stash time as
|
|
353
|
+
// `run.events.length` and is NOT authoritative — if the runFn emitted
|
|
354
|
+
// any more events before it actually stopped on the abort signal,
|
|
355
|
+
// those events were pushed and reused that same seq. Persisting the
|
|
356
|
+
// terminal event with the stale seq would collide with an
|
|
357
|
+
// already-persisted streaming event and get silently dropped by
|
|
358
|
+
// insertRunEvent's `ON CONFLICT (run_id, seq) DO NOTHING`, so the
|
|
359
|
+
// client would never see the terminal/continuation signal. We always
|
|
360
|
+
// re-stamp the seq at emit time (max-seq+1) just below.
|
|
361
|
+
const terminalEvent = finalStatus === "completed"
|
|
362
|
+
? (pendingTerminalEvent?.event ?? { type: "done" })
|
|
355
363
|
: pendingTerminalEvent?.event.type === "error"
|
|
356
|
-
? pendingTerminalEvent
|
|
364
|
+
? pendingTerminalEvent.event
|
|
357
365
|
: {
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
? "Agent response could not be saved."
|
|
363
|
-
: "Agent run ended unexpectedly",
|
|
364
|
-
},
|
|
366
|
+
type: "error",
|
|
367
|
+
error: completionError
|
|
368
|
+
? "Agent response could not be saved."
|
|
369
|
+
: "Agent run ended unexpectedly",
|
|
365
370
|
};
|
|
366
371
|
const last = run.events[run.events.length - 1];
|
|
367
372
|
if (!last || !isTerminalRunEvent(last.event)) {
|
|
373
|
+
// Assign the seq at EMIT time, not at stash time. `run.events` is a
|
|
374
|
+
// contiguous 0-based log, so `run.events.length` is the next free
|
|
375
|
+
// seq and can never collide with an event that was pushed after the
|
|
376
|
+
// terminal event was stashed.
|
|
377
|
+
const terminal = {
|
|
378
|
+
seq: run.events.length,
|
|
379
|
+
event: terminalEvent,
|
|
380
|
+
};
|
|
368
381
|
try {
|
|
369
382
|
await emitRunEvent(terminal, { surfacePersistenceError: true });
|
|
370
383
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run-manager.js","sourceRoot":"","sources":["../../src/agent/run-manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAA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AAM,UAAU,CAAC,KAAK,CAAC,CAAC;QACpC,IAAI,CAAC,GAAG;YAAE,OAAO,IAAI,CAAC;QACtB,sEAAsE;QACtE,oDAAoD;QACpD,MAAM,QAAQ,GAAG,MAAM,cAAc,CAAC,GAAG,CAAC,QAAQ,EAAE;YAClD,eAAe,EAAE,IAAI;SACtB,CAAC,CAAC;QACH,IAAI,QAAQ,IAAI,QAAQ,CAAC,EAAE,KAAK,KAAK;YAAE,OAAO,QAAQ,CAAC,cAAc,CAAC;QACtE,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,sBAAsB;AACtB,MAAM,UAAU,MAAM,CAAC,KAAa;IAClC,OAAO,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC;AACvC,CAAC;AAED,gDAAgD;AAChD,MAAM,UAAU,QAAQ,CAAC,KAAa,EAAE,SAAiB,MAAM;IAC7D,MAAM,GAAG,GAAG,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAClC,IAAI,GAAG,EAAE,CAAC;QACR,gBAAgB,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAChC,CAAC;IACD,mEAAmE;IACnE,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IAC9C,OAAO,CAAC,CAAC,GAAG,CAAC;AACf,CAAC","sourcesContent":["import type { AgentChatEvent, RunEvent, RunStatus } from \"./types.js\";\nimport { EngineError } from \"./engine/types.js\";\nimport { captureError } from \"../server/capture-error.js\";\nimport {\n insertRun,\n insertRunEvent,\n updateRunStatus,\n markRunAborted,\n getRunAbortState,\n getRunEventsSince,\n getRunById,\n getRunByThread,\n cleanupOldRuns,\n updateRunHeartbeat,\n bumpRunProgress,\n reapIfStale,\n ensureTerminalRunEvent,\n setRunError,\n STALE_RUN_ERROR_EVENT,\n} from \"./run-store.js\";\n\nexport interface ActiveRun {\n runId: string;\n threadId: string;\n /** Logical-turn identity (see StartRunOptions.turnId). Defaults to runId. */\n turnId: string;\n events: RunEvent[];\n status: RunStatus;\n subscribers: Set<(event: RunEvent) => void>;\n abort: AbortController;\n abortReason?: string;\n startedAt: number;\n}\n\nconst activeRuns = new Map<string, ActiveRun>();\nconst threadToRun = new Map<string, string>();\n\n/** How long to keep completed runs in memory before cleanup (5 min) */\nconst CLEANUP_DELAY_MS = 5 * 60 * 1000;\n\n/**\n * Default run chunk budget for hosted/serverless deploys.\n *\n * This MUST fire before the two upstream hard walls that otherwise kill a run\n * mid-turn with no chance to hand off:\n * 1. The Builder model gateway hard-caps a single model call at 45s\n * (builder-engine.ts MAX_BUILDER_GATEWAY_TIMEOUT_MS) — not raisable.\n * 2. Serverless functions are hard-killed around 60-65s (the heartbeat then\n * reaps the row as a stale_run).\n * Production data showed every cutoff landing in the 44-70s window with ZERO\n * auto_continue events ever emitted — i.e. the old 45s default raced the 45s\n * gateway and lost, and per-template overrides (e.g. 240_000) pushed it past\n * BOTH walls so it could never fire. 40s leaves ~5s of headroom under the\n * gateway wall to abort, persist the partial turn, write the terminal event,\n * and emit a clean auto_continue so the client resumes seamlessly.\n */\nexport const DEFAULT_HOSTED_RUN_SOFT_TIMEOUT_MS = 40_000;\n\n/**\n * Hard ceiling for the hosted soft timeout. On a hosted runtime the\n * auto_continue soft timeout can never usefully exceed this — the gateway\n * (45s) / function (~60s) walls kill the run first, so a larger configured or\n * env value just guarantees the cutoff is a hard error instead of a graceful\n * hand-off. Any resolved value above this is clamped down when hosted. Local\n * dev (non-hosted) is left alone so long-running local turns aren't chunked.\n */\nexport const HOSTED_SOFT_TIMEOUT_CEILING_MS = 40_000;\n\n/** Default SQL retention for completed run event logs (24 hours). */\nexport const DEFAULT_COMPLETED_RUN_RETENTION_MS = 24 * 60 * 60 * 1000;\n\n/**\n * Default SQL retention for errored/aborted run event logs (7 days). Kept\n * longer than completed runs so cut-off / failed chats survive for pattern\n * analysis (listErroredRuns) — these are rare and small, and they are exactly\n * the runs we need to study to keep hardening reliability.\n */\nexport const DEFAULT_ERRORED_RUN_RETENTION_MS = 7 * 24 * 60 * 60 * 1000;\n\n/**\n * How recently a terminal run must have started for `/runs/active` to surface\n * it. Reconnect after this window won't replay the run — typical real-world\n * disconnects resolve in seconds, so 10 minutes is generous while keeping us\n * from resurrecting ancient turns when the user reopens an old thread.\n */\nexport const TERMINAL_RUN_RECONNECT_WINDOW_MS = 10 * 60 * 1000;\n\nexport interface StartRunOptions {\n /** Optional internal run chunk budget. When reached, the framework emits an\n * auto-continuation signal instead of a user-facing timeout. Leave unset for\n * no framework-imposed run timeout. */\n softTimeoutMs?: number;\n /** Opt into the hosted/serverless default chunk budget. Only callers with\n * automatic continuation support should enable this. */\n useHostedSoftTimeoutDefault?: boolean;\n /** Stable identity for the logical assistant turn this run belongs to. A\n * turn may span several continuation runs (each chunk is its own run); they\n * share one `turnId` so the durable assistant message can be folded across\n * them instead of dropped per-run. Defaults to the runId (turn == run). */\n turnId?: string;\n}\n\nexport interface ResolveRunSoftTimeoutOptions {\n useHostedDefault?: boolean;\n}\n\nfunction isHostedRuntime(): boolean {\n if (\n process.env.NETLIFY &&\n process.env.NETLIFY !== \"false\" &&\n process.env.NETLIFY_LOCAL !== \"true\"\n ) {\n return true;\n }\n if (\n process.env.AWS_LAMBDA_FUNCTION_NAME &&\n process.env.NETLIFY_LOCAL !== \"true\"\n ) {\n return true;\n }\n return Boolean(\n process.env.CF_PAGES ||\n process.env.VERCEL ||\n process.env.VERCEL_ENV ||\n process.env.RENDER ||\n process.env.FLY_APP_NAME ||\n process.env.K_SERVICE,\n );\n}\n\nexport function resolveRunSoftTimeoutMs(\n overrideMs?: number,\n options?: ResolveRunSoftTimeoutOptions,\n): number {\n const hosted = isHostedRuntime();\n // A configured/env soft timeout that exceeds the upstream walls can never\n // actually fire (the gateway/function kills the run first), so clamp it down\n // on hosted runtimes. This is what makes auto_continue reach the client\n // instead of the run dying as builder_gateway_timeout / stale_run. `0` means\n // \"disabled\" and is never clamped up.\n const clampHosted = (ms: number): number =>\n hosted && ms > HOSTED_SOFT_TIMEOUT_CEILING_MS\n ? HOSTED_SOFT_TIMEOUT_CEILING_MS\n : ms;\n\n if (typeof overrideMs === \"number\" && Number.isFinite(overrideMs)) {\n return clampHosted(Math.max(0, overrideMs));\n }\n const envValue = process.env.AGENT_RUN_SOFT_TIMEOUT_MS;\n if (envValue !== undefined) {\n const raw = Number(envValue);\n if (Number.isFinite(raw) && raw >= 0) return clampHosted(raw);\n }\n return options?.useHostedDefault && hosted\n ? DEFAULT_HOSTED_RUN_SOFT_TIMEOUT_MS\n : 0;\n}\n\nexport function resolveCompletedRunRetentionMs(): number {\n const envValue = process.env.AGENT_RUN_RETENTION_MS;\n if (envValue !== undefined) {\n const raw = Number(envValue);\n if (Number.isFinite(raw) && raw >= 0) return raw;\n }\n return DEFAULT_COMPLETED_RUN_RETENTION_MS;\n}\n\nexport function resolveErroredRunRetentionMs(): number {\n const envValue = process.env.AGENT_ERRORED_RUN_RETENTION_MS;\n if (envValue !== undefined) {\n const raw = Number(envValue);\n if (Number.isFinite(raw) && raw >= 0) return raw;\n }\n return DEFAULT_ERRORED_RUN_RETENTION_MS;\n}\n\nfunction isTerminalRunEvent(event: AgentChatEvent): boolean {\n return (\n event.type === \"done\" ||\n event.type === \"error\" ||\n event.type === \"missing_api_key\" ||\n event.type === \"loop_limit\" ||\n event.type === \"auto_continue\"\n );\n}\n\nfunction abortInMemoryRun(run: ActiveRun, reason: string = \"user\") {\n run.abortReason = reason;\n run.status = \"aborted\";\n if (threadToRun.get(run.threadId) === run.runId) {\n threadToRun.delete(run.threadId);\n }\n run.abort.abort(reason);\n for (const subscriber of run.subscribers) {\n try {\n subscriber({ seq: run.events.length, event: { type: \"done\" } });\n } catch {\n // ignore — subscriber is being removed below\n }\n }\n run.subscribers.clear();\n}\n\n/**\n * Start a new agent run in the background.\n * `runFn` receives a `send` callback and an `AbortSignal`.\n * The run continues even if all SSE subscribers disconnect.\n *\n * Events are persisted to SQL for cross-isolate access (Cloudflare Workers).\n */\nexport function startRun(\n runId: string,\n threadId: string,\n runFn: (\n send: (event: AgentChatEvent) => void,\n signal: AbortSignal,\n ) => Promise<void>,\n onComplete?: (run: ActiveRun) => void | Promise<void>,\n options?: StartRunOptions,\n): ActiveRun {\n // If there's already a run for this thread, abort it\n const existingRunId = threadToRun.get(threadId);\n if (existingRunId) {\n abortRun(existingRunId);\n }\n\n const abort = new AbortController();\n let softTimedOut = false;\n const run: ActiveRun = {\n runId,\n threadId,\n turnId: options?.turnId ?? runId,\n events: [],\n status: \"running\",\n subscribers: new Set(),\n abort,\n startedAt: Date.now(),\n };\n\n activeRuns.set(runId, run);\n threadToRun.set(threadId, runId);\n\n // Persist run to SQL without blocking the response. Keep the promise so\n // final status cannot race ahead of a slow initial INSERT and then get\n // overwritten by a late row stuck at status='running'.\n const insertRunPromise = insertRun(runId, threadId, options?.turnId).catch(\n () => {},\n );\n\n // Throttle the durable progress timestamp to at most once per second so\n // a chatty token-by-token stream doesn't translate into one DB write per\n // chunk. The stuck-detector threshold is on the order of tens of seconds,\n // so 1s resolution is plenty.\n let lastProgressBumpAt = 0;\n const bumpProgressIfDue = () => {\n const now = Date.now();\n if (now - lastProgressBumpAt < 1000) return;\n lastProgressBumpAt = now;\n bumpRunProgress(runId).catch(() => {});\n };\n\n // Periodic SQL abort check interval (for cross-isolate abort on Workers)\n let lastAbortCheck = Date.now() - 3000;\n const checkSqlAbort = () => {\n const now = Date.now();\n if (now - lastAbortCheck < 3000) return;\n lastAbortCheck = now;\n getRunAbortState(runId)\n .then((state) => {\n if (state.aborted && !abort.signal.aborted) {\n abortInMemoryRun(run, state.reason ?? \"user\");\n }\n })\n .catch(() => {});\n };\n\n // Heartbeat: bump heartbeat_at every 1.5s so watchers can detect a dead\n // producer (process crash, HMR restart, isolate eviction) quickly and\n // reap the row. Paired with RUN_STALE_MS (6s) — 4x the interval to\n // tolerate transient DB slowness without false positives.\n const heartbeatTimer: ReturnType<typeof setInterval> = setInterval(() => {\n updateRunHeartbeat(runId).catch(() => {});\n checkSqlAbort();\n }, 1500);\n const softTimeoutMs = resolveRunSoftTimeoutMs(options?.softTimeoutMs, {\n useHostedDefault: options?.useHostedSoftTimeoutDefault === true,\n });\n const softTimeoutTimer =\n softTimeoutMs > 0\n ? setTimeout(() => {\n if (run.status !== \"running\" || abort.signal.aborted) return;\n softTimedOut = true;\n send({\n type: \"auto_continue\",\n reason: \"run_timeout\",\n });\n abort.abort();\n }, softTimeoutMs)\n : null;\n let pendingTerminalEvent: RunEvent | null = null;\n\n const captureRunError = (error: unknown, phase: \"run\" | \"completion\") => {\n captureError(error, {\n route: \"/_agent-native/agent-chat\",\n tags: {\n source: \"agent-run-manager\",\n phase,\n runStatus: run.status,\n softTimedOut: softTimedOut ? \"true\" : \"false\",\n abortReason: run.abortReason,\n errorCode: error instanceof EngineError ? error.errorCode : undefined,\n },\n extra: {\n runId,\n threadId,\n eventCount: run.events.length,\n startedAt: run.startedAt,\n softTimeoutMs,\n },\n contexts: {\n agentRun: {\n runId,\n threadId,\n status: run.status,\n phase,\n eventCount: run.events.length,\n startedAt: run.startedAt,\n softTimeoutMs,\n softTimedOut,\n abortReason: run.abortReason,\n },\n },\n });\n };\n\n const emitRunEvent = (\n runEvent: RunEvent,\n options?: { surfacePersistenceError?: boolean },\n ): Promise<void> => {\n run.events.push(runEvent);\n\n // Notify in-memory subscribers (same isolate, fast path)\n for (const subscriber of run.subscribers) {\n try {\n subscriber(runEvent);\n } catch {\n run.subscribers.delete(subscriber);\n }\n }\n\n // Bump the durable progress timestamp. Distinct from the heartbeat:\n // heartbeat = \"process is up\", progress = \"real work is happening.\" The\n // gap between them is what the client-side stuck-detector reads to tell\n // a hung run from a healthy one.\n bumpProgressIfDue();\n\n // Persist event to SQL. Ordinary streaming events are fire-and-forget, but\n // terminal events are awaited before final status is persisted so reconnects\n // never observe status='errored' without the actual terminal error payload.\n const persistence = insertRunEvent(\n runId,\n runEvent.seq,\n JSON.stringify(runEvent.event),\n );\n if (!options?.surfacePersistenceError) {\n persistence.catch(() => {});\n }\n\n checkSqlAbort();\n return persistence;\n };\n\n const send = (event: AgentChatEvent) => {\n if (run.status === \"aborted\" && abort.signal.aborted) return;\n\n const runEvent: RunEvent = { seq: run.events.length, event };\n if (isTerminalRunEvent(event)) {\n pendingTerminalEvent = runEvent;\n return;\n }\n\n emitRunEvent(runEvent);\n };\n\n // Run in background — intentionally detached from any HTTP connection\n const runPromise = runFn(send, abort.signal)\n .then(() => {\n if (abort.signal.aborted) {\n run.status = softTimedOut ? \"completed\" : \"aborted\";\n return;\n }\n run.status = \"completed\";\n })\n .catch((err) => {\n // Don't surface abort errors — the run was intentionally stopped\n if (abort.signal.aborted) {\n run.status = softTimedOut ? \"completed\" : \"aborted\";\n return;\n }\n run.status = \"errored\";\n captureRunError(err, \"run\");\n send({\n type: \"error\",\n error: err?.message ?? \"Unknown error\",\n ...(err instanceof EngineError && err.errorCode\n ? { errorCode: err.errorCode }\n : {}),\n ...(err instanceof EngineError && err.upgradeUrl\n ? { upgradeUrl: err.upgradeUrl }\n : {}),\n });\n })\n .finally(async () => {\n // Ordering matters here — this is the atomic-complete boundary.\n // Invariant: once agent_runs.status flips to \"completed\"/\"errored\"\n // in SQL, thread_data for this turn is already durable. This lets\n // reconnecting clients trust the simple rule \"status != running →\n // fetch thread_data\" without polling/retrying for a race window\n // where onComplete was still pending.\n\n // 1. Await the completion callback (thread_data save). Heartbeat is\n // still ticking so the run doesn't look stale to any concurrent\n // /runs/active check while we wait for SQL writes to land.\n let completionError: unknown = null;\n let terminalPersistenceError: unknown = null;\n if (\n onComplete &&\n !(run.status === \"aborted\" && run.abortReason === \"no_progress\")\n ) {\n try {\n const completionRun: ActiveRun = pendingTerminalEvent\n ? { ...run, events: [...run.events, pendingTerminalEvent] }\n : run;\n await onComplete(completionRun);\n } catch (err) {\n completionError = err;\n captureRunError(err, \"completion\");\n console.error(\n \"[run-manager] onComplete callback error:\",\n err instanceof Error ? err.message : err,\n );\n }\n }\n\n // 2. Compute final status. If the completion callback threw, we'd\n // rather mark the run errored than claim success with incomplete\n // thread_data.\n const finalStatus =\n run.status === \"aborted\"\n ? \"aborted\"\n : run.status === \"errored\" || completionError\n ? \"errored\"\n : \"completed\";\n\n // 3. Emit the terminal event only after thread_data is durable. Live\n // SSE clients close on this event and usually fetch thread_data\n // immediately, so emitting it earlier recreates the final-message\n // race this manager is meant to avoid.\n if (finalStatus === \"completed\" || finalStatus === \"errored\") {\n const terminal: RunEvent =\n finalStatus === \"completed\"\n ? (pendingTerminalEvent ?? {\n seq: run.events.length,\n event: { type: \"done\" },\n })\n : pendingTerminalEvent?.event.type === \"error\"\n ? pendingTerminalEvent\n : {\n seq: pendingTerminalEvent?.seq ?? run.events.length,\n event: {\n type: \"error\",\n error: completionError\n ? \"Agent response could not be saved.\"\n : \"Agent run ended unexpectedly\",\n },\n };\n const last = run.events[run.events.length - 1];\n if (!last || !isTerminalRunEvent(last.event)) {\n try {\n await emitRunEvent(terminal, { surfacePersistenceError: true });\n } catch (err) {\n terminalPersistenceError = err;\n captureRunError(err, \"completion\");\n console.error(\n \"[run-manager] terminal event persistence error:\",\n err instanceof Error ? err.message : err,\n );\n }\n }\n }\n for (const subscriber of run.subscribers) {\n run.subscribers.delete(subscriber);\n }\n\n // 4. Stop the heartbeat — all liveness writes are done.\n clearInterval(heartbeatTimer);\n if (softTimeoutTimer) clearTimeout(softTimeoutTimer);\n\n // 5. Persist final status to SQL.\n try {\n await insertRunPromise;\n if (!terminalPersistenceError) {\n await updateRunStatus(runId, finalStatus);\n }\n } catch {\n // Best-effort — reapIfStale will eventually clean this up via\n // the heartbeat-stale path.\n }\n\n // 5b. Record terminal failure classification for errored runs so\n // cut-off / failed chats are queryable for pattern analysis. Read\n // the actual error event the run emitted (errorCode + message) so\n // diagnostics reflect the real cause (builder_gateway_timeout,\n // stale_run, context_length_exceeded, completion_error, …).\n if (finalStatus === \"errored\") {\n let errorCode: string | undefined;\n let errorDetail: string | undefined;\n for (let i = run.events.length - 1; i >= 0; i--) {\n const ev = run.events[i].event as {\n type: string;\n error?: string;\n errorCode?: string;\n details?: string;\n };\n if (ev.type === \"error\") {\n errorCode = ev.errorCode;\n errorDetail = ev.error ?? ev.details;\n break;\n }\n }\n if (completionError && !errorCode) {\n errorCode = \"completion_error\";\n errorDetail =\n errorDetail ??\n (completionError instanceof Error\n ? completionError.message\n : String(completionError));\n }\n await setRunError(runId, errorCode ?? \"unknown\", errorDetail);\n }\n\n // 6. Schedule in-memory cleanup + opportunistic old-run pruning.\n setTimeout(() => {\n activeRuns.delete(runId);\n if (threadToRun.get(threadId) === runId) {\n threadToRun.delete(threadId);\n }\n }, CLEANUP_DELAY_MS);\n cleanupOldRuns(\n resolveCompletedRunRetentionMs(),\n resolveErroredRunRetentionMs(),\n ).catch(() => {});\n });\n\n // On Cloudflare Workers, keep the isolate alive for this run\n try {\n const cfCtx = globalThis.__cf_ctx;\n if (cfCtx?.waitUntil) {\n cfCtx.waitUntil(runPromise);\n }\n } catch {\n // Not on Workers — ignore\n }\n\n return run;\n}\n\n/**\n * Subscribe to a run's events starting from `fromSeq`.\n * Returns a ReadableStream that replays buffered events then live-tails.\n * Cancelling the stream only unsubscribes — does NOT abort the agent.\n *\n * Falls back to SQL polling when the run is not in local memory\n * (cross-isolate reconnection on Workers).\n */\nexport function subscribeToRun(\n runId: string,\n fromSeq: number,\n): ReadableStream<Uint8Array> | null {\n const run = activeRuns.get(runId);\n if (run) {\n return subscribeInMemory(run, fromSeq);\n }\n // Not in local memory — try SQL (cross-isolate path)\n return subscribeFromSQL(runId, fromSeq);\n}\n\n/** In-memory subscription (same isolate, fast path) */\nfunction subscribeInMemory(\n run: ActiveRun,\n fromSeq: number,\n): ReadableStream<Uint8Array> {\n const encoder = new TextEncoder();\n let subscriberRef: ((event: RunEvent) => void) | null = null;\n let pingTimer: ReturnType<typeof setInterval> | null = null;\n\n return new ReadableStream({\n start(controller) {\n const ping = () => {\n try {\n controller.enqueue(encoder.encode(`: ping ${Date.now()}\\n\\n`));\n } catch {\n if (subscriberRef) run.subscribers.delete(subscriberRef);\n if (pingTimer) clearInterval(pingTimer);\n }\n };\n ping();\n pingTimer = setInterval(ping, 10_000);\n\n // Replay buffered events from fromSeq\n for (let i = fromSeq; i < run.events.length; i++) {\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({ ...run.events[i].event, seq: run.events[i].seq })}\\n\\n`,\n ),\n );\n } catch {\n return;\n }\n }\n\n // If run is already done, close immediately\n if (run.status !== \"running\") {\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n return;\n }\n\n // Subscribe to live events\n subscriberRef = (event: RunEvent) => {\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({ ...event.event, seq: event.seq })}\\n\\n`,\n ),\n );\n // Close stream after terminal events\n if (isTerminalRunEvent(event.event)) {\n run.subscribers.delete(subscriberRef!);\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n }\n } catch {\n run.subscribers.delete(subscriberRef!);\n }\n };\n\n run.subscribers.add(subscriberRef);\n },\n cancel() {\n // Only unsubscribe — do NOT abort the agent run\n if (subscriberRef) run.subscribers.delete(subscriberRef);\n if (pingTimer) clearInterval(pingTimer);\n },\n });\n}\n\n/** SQL-based subscription (cross-isolate, polling) */\nfunction subscribeFromSQL(\n runId: string,\n fromSeq: number,\n): ReadableStream<Uint8Array> | null {\n const encoder = new TextEncoder();\n let cancelled = false;\n let pollTimer: ReturnType<typeof setTimeout> | null = null;\n let pingTimer: ReturnType<typeof setInterval> | null = null;\n\n return new ReadableStream({\n async start(controller) {\n let lastSeq = fromSeq;\n const ping = () => {\n try {\n controller.enqueue(encoder.encode(`: ping ${Date.now()}\\n\\n`));\n } catch {\n cancelled = true;\n if (pingTimer) clearInterval(pingTimer);\n }\n };\n ping();\n pingTimer = setInterval(ping, 10_000);\n\n const poll = async () => {\n if (cancelled) return;\n try {\n // Read new events from SQL\n const events = await getRunEventsSince(runId, lastSeq);\n for (const { seq, eventData } of events) {\n let parsed: any;\n try {\n parsed = JSON.parse(eventData);\n } catch {\n continue;\n }\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({ ...parsed, seq })}\\n\\n`,\n ),\n );\n } catch {\n cancelled = true;\n return;\n }\n lastSeq = seq + 1;\n\n // Close on terminal events\n if (isTerminalRunEvent(parsed)) {\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n return;\n }\n }\n\n // Check if run completed (no terminal event but status changed)\n if (events.length === 0) {\n // Opportunistically reap a stale producer before trusting SQL's\n // \"running\" status — otherwise a crashed server leaves us polling\n // forever.\n await reapIfStale(runId).catch(() => {});\n const run = await getRunById(runId);\n if (!run || run.status !== \"running\") {\n // Run ended — do one final event read, then close\n const finalEvents = await getRunEventsSince(runId, lastSeq);\n for (const { seq, eventData } of finalEvents) {\n let parsed: any;\n try {\n parsed = JSON.parse(eventData);\n } catch {\n continue;\n }\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({ ...parsed, seq })}\\n\\n`,\n ),\n );\n } catch {\n cancelled = true;\n return;\n }\n lastSeq = seq + 1;\n if (isTerminalRunEvent(parsed)) {\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n return;\n }\n }\n if (run?.status === \"aborted\") {\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({ type: \"done\", seq: lastSeq })}\\n\\n`,\n ),\n );\n } catch {\n cancelled = true;\n return;\n }\n } else if (run?.status === \"completed\") {\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({ type: \"done\", seq: lastSeq })}\\n\\n`,\n ),\n );\n } catch {\n cancelled = true;\n return;\n }\n } else if (run?.status === \"errored\") {\n // The run row was flipped to `errored` but no terminal event\n // was ever persisted — almost always means a reaper's silent\n // `appendTerminalRunEvent(...).catch(() => {})` swallowed a\n // transient DB error, so the user-facing situation is the\n // same as a stale-run reap. Send the friendly event AND try\n // to persist it so future reconnects replay it from SQL\n // rather than regenerating it (the user used to see a bare\n // \"run_terminal_event_missing\" debug string here).\n await ensureTerminalRunEvent(\n runId,\n STALE_RUN_ERROR_EVENT,\n ).catch(() => {});\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({\n ...STALE_RUN_ERROR_EVENT,\n seq: lastSeq,\n })}\\n\\n`,\n ),\n );\n } catch {\n cancelled = true;\n return;\n }\n }\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n return;\n }\n }\n\n // Schedule next poll\n if (!cancelled) {\n pollTimer = setTimeout(poll, 500);\n }\n } catch {\n // SQL error — close stream\n try {\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n } catch {}\n }\n };\n\n // Verify run exists before starting poll\n try {\n const run = await getRunById(runId);\n if (!run) {\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n return;\n }\n } catch {\n controller.close();\n return;\n }\n\n await poll();\n },\n cancel() {\n cancelled = true;\n if (pollTimer) clearTimeout(pollTimer);\n if (pingTimer) clearInterval(pingTimer);\n },\n });\n}\n\n/** Get the active run for a thread (if any) — checks memory then SQL */\nexport function getActiveRunForThread(threadId: string): ActiveRun | null {\n const runId = threadToRun.get(threadId);\n if (runId) {\n const run = activeRuns.get(runId);\n if (run) return run;\n }\n return null;\n}\n\n/**\n * Async version that also checks SQL — for cross-isolate access.\n * Used by the /runs/active endpoint.\n *\n * Returns `heartbeatAt` so the client can independently decide a run is\n * dead even before the server-side stale reap has fired. Returns\n * `lastProgressAt` so the client-side stuck-detector can show a\n * user-visible \"this chat looks stuck\" affordance when a run is alive\n * (heartbeating) but not actually emitting events.\n */\nexport async function getActiveRunForThreadAsync(threadId: string): Promise<{\n runId: string;\n threadId: string;\n status: string;\n heartbeatAt: number;\n lastProgressAt: number | null;\n} | null> {\n // Check memory first — return both running AND recently-completed runs\n // that still have events in memory. This allows sub-agent tabs to replay\n // the full conversation from completed runs via SSE.\n const memRun = getActiveRunForThread(threadId);\n if (memRun && (memRun.status === \"running\" || memRun.events.length > 0)) {\n return {\n runId: memRun.runId,\n threadId: memRun.threadId,\n status: memRun.status,\n // In-memory means this isolate is the producer. By definition, the\n // heartbeat is fresh as of \"now\" — the client can trust this.\n heartbeatAt: Date.now(),\n // For an in-memory run we don't have a separate \"last event emit\"\n // timestamp tracked in JS — the SQL bump is throttled per-second.\n // Read it back from SQL on demand. For the common case the SQL row\n // is well under 1s old; if it isn't, the stuck-detector will pick\n // it up on the next poll cycle.\n lastProgressAt: await fetchLastProgressAt(memRun.runId),\n };\n }\n // Fall back to SQL — also surface recently terminated runs so the client\n // can reconnect and replay synthesized done/error events instead of\n // retrying the original POST. Without this, a POST that fails after the\n // server already accepted (and finished) the run would re-execute the\n // turn and double-apply mutations: the in-memory branch above already\n // returns terminal runs whose events are still buffered, but the SQL\n // path is the only authority once memory has been evicted.\n try {\n const sqlRun = await getRunByThread(threadId, { includeTerminal: true });\n if (!sqlRun) return null;\n if (sqlRun.status === \"running\") {\n // If the producer is dead (no recent heartbeat), reap before the\n // client can see a stale \"running\" status and enter a reconnect\n // loop it can never exit.\n const reaped = await reapIfStale(sqlRun.id).catch(() => false);\n if (reaped) return null;\n return {\n runId: sqlRun.id,\n threadId: sqlRun.threadId,\n status: sqlRun.status,\n heartbeatAt: sqlRun.heartbeatAt ?? sqlRun.startedAt,\n lastProgressAt: sqlRun.lastProgressAt,\n };\n }\n if (sqlRun.status === \"completed\" || sqlRun.status === \"errored\") {\n // Cap how far back we'll surface terminal runs as \"active\". The goal\n // is to catch the recently-completed-but-reconnecting case, not to\n // resurrect ancient turns when the user reopens an old thread.\n //\n // Measure age from the run's terminal timestamp, not its start. A\n // long-running task that ran 11 minutes and completed five seconds\n // ago should still be reachable — the client's disconnect happened\n // around completion, so completion time is what matters for the\n // \"is the user still here waiting?\" question. Fall back to the last\n // heartbeat (older deployments may have unset completed_at) and\n // finally to startedAt for ancient rows.\n const referenceAt =\n sqlRun.completedAt ?? sqlRun.heartbeatAt ?? sqlRun.startedAt;\n const terminalAge = Date.now() - referenceAt;\n if (terminalAge > TERMINAL_RUN_RECONNECT_WINDOW_MS) return null;\n return {\n runId: sqlRun.id,\n threadId: sqlRun.threadId,\n status: sqlRun.status,\n heartbeatAt: sqlRun.heartbeatAt ?? sqlRun.startedAt,\n lastProgressAt: sqlRun.lastProgressAt,\n };\n }\n } catch {\n // SQL error — fall through\n }\n return null;\n}\n\nasync function fetchLastProgressAt(runId: string): Promise<number | null> {\n try {\n const run = await getRunById(runId);\n if (!run) return null;\n // `getRunById` returns a narrow projection today; ask for the row via\n // the thread lookup which carries last_progress_at.\n const byThread = await getRunByThread(run.threadId, {\n includeTerminal: true,\n });\n if (byThread && byThread.id === runId) return byThread.lastProgressAt;\n return null;\n } catch {\n return null;\n }\n}\n\n/** Get a run by ID */\nexport function getRun(runId: string): ActiveRun | null {\n return activeRuns.get(runId) ?? null;\n}\n\n/** Explicitly abort a run (e.g. Stop button) */\nexport function abortRun(runId: string, reason: string = \"user\"): boolean {\n const run = activeRuns.get(runId);\n if (run) {\n abortInMemoryRun(run, reason);\n }\n // Also mark as aborted in SQL (for cross-isolate abort on Workers)\n markRunAborted(runId, reason).catch(() => {});\n return !!run;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"run-manager.js","sourceRoot":"","sources":["../../src/agent/run-manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EACL,SAAS,EACT,cAAc,EACd,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd,cAAc,EACd,kBAAkB,EAClB,eAAe,EACf,WAAW,EACX,sBAAsB,EACtB,WAAW,EACX,qBAAqB,GACtB,MAAM,gBAAgB,CAAC;AAexB,MAAM,UAAU,GAAG,IAAI,GAAG,EAAqB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GAAG,CAAC,QAAQ,EAAE;YAClD,eAAe,EAAE,IAAI;SACtB,CAAC,CAAC;QACH,IAAI,QAAQ,IAAI,QAAQ,CAAC,EAAE,KAAK,KAAK;YAAE,OAAO,QAAQ,CAAC,cAAc,CAAC;QACtE,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,sBAAsB;AACtB,MAAM,UAAU,MAAM,CAAC,KAAa;IAClC,OAAO,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC;AACvC,CAAC;AAED,gDAAgD;AAChD,MAAM,UAAU,QAAQ,CAAC,KAAa,EAAE,SAAiB,MAAM;IAC7D,MAAM,GAAG,GAAG,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAClC,IAAI,GAAG,EAAE,CAAC;QACR,gBAAgB,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAChC,CAAC;IACD,mEAAmE;IACnE,cAAc,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IAC9C,OAAO,CAAC,CAAC,GAAG,CAAC;AACf,CAAC","sourcesContent":["import type { AgentChatEvent, RunEvent, RunStatus } from \"./types.js\";\nimport { EngineError } from \"./engine/types.js\";\nimport { captureError } from \"../server/capture-error.js\";\nimport {\n insertRun,\n insertRunEvent,\n updateRunStatus,\n markRunAborted,\n getRunAbortState,\n getRunEventsSince,\n getRunById,\n getRunByThread,\n cleanupOldRuns,\n updateRunHeartbeat,\n bumpRunProgress,\n reapIfStale,\n ensureTerminalRunEvent,\n setRunError,\n STALE_RUN_ERROR_EVENT,\n} from \"./run-store.js\";\n\nexport interface ActiveRun {\n runId: string;\n threadId: string;\n /** Logical-turn identity (see StartRunOptions.turnId). Defaults to runId. */\n turnId: string;\n events: RunEvent[];\n status: RunStatus;\n subscribers: Set<(event: RunEvent) => void>;\n abort: AbortController;\n abortReason?: string;\n startedAt: number;\n}\n\nconst activeRuns = new Map<string, ActiveRun>();\nconst threadToRun = new Map<string, string>();\n\n/** How long to keep completed runs in memory before cleanup (5 min) */\nconst CLEANUP_DELAY_MS = 5 * 60 * 1000;\n\n/**\n * Default run chunk budget for hosted/serverless deploys.\n *\n * This MUST fire before the two upstream hard walls that otherwise kill a run\n * mid-turn with no chance to hand off:\n * 1. The Builder model gateway hard-caps a single model call at 45s\n * (builder-engine.ts MAX_BUILDER_GATEWAY_TIMEOUT_MS) — not raisable.\n * 2. Serverless functions are hard-killed around 60-65s (the heartbeat then\n * reaps the row as a stale_run).\n * Production data showed every cutoff landing in the 44-70s window with ZERO\n * auto_continue events ever emitted — i.e. the old 45s default raced the 45s\n * gateway and lost, and per-template overrides (e.g. 240_000) pushed it past\n * BOTH walls so it could never fire. 40s leaves ~5s of headroom under the\n * gateway wall to abort, persist the partial turn, write the terminal event,\n * and emit a clean auto_continue so the client resumes seamlessly.\n */\nexport const DEFAULT_HOSTED_RUN_SOFT_TIMEOUT_MS = 40_000;\n\n/**\n * Hard ceiling for the hosted soft timeout. On a hosted runtime the\n * auto_continue soft timeout can never usefully exceed this — the gateway\n * (45s) / function (~60s) walls kill the run first, so a larger configured or\n * env value just guarantees the cutoff is a hard error instead of a graceful\n * hand-off. Any resolved value above this is clamped down when hosted. Local\n * dev (non-hosted) is left alone so long-running local turns aren't chunked.\n */\nexport const HOSTED_SOFT_TIMEOUT_CEILING_MS = 40_000;\n\n/** Default SQL retention for completed run event logs (24 hours). */\nexport const DEFAULT_COMPLETED_RUN_RETENTION_MS = 24 * 60 * 60 * 1000;\n\n/**\n * Default SQL retention for errored/aborted run event logs (7 days). Kept\n * longer than completed runs so cut-off / failed chats survive for pattern\n * analysis (listErroredRuns) — these are rare and small, and they are exactly\n * the runs we need to study to keep hardening reliability.\n */\nexport const DEFAULT_ERRORED_RUN_RETENTION_MS = 7 * 24 * 60 * 60 * 1000;\n\n/**\n * How recently a terminal run must have started for `/runs/active` to surface\n * it. Reconnect after this window won't replay the run — typical real-world\n * disconnects resolve in seconds, so 10 minutes is generous while keeping us\n * from resurrecting ancient turns when the user reopens an old thread.\n */\nexport const TERMINAL_RUN_RECONNECT_WINDOW_MS = 10 * 60 * 1000;\n\nexport interface StartRunOptions {\n /** Optional internal run chunk budget. When reached, the framework emits an\n * auto-continuation signal instead of a user-facing timeout. Leave unset for\n * no framework-imposed run timeout. */\n softTimeoutMs?: number;\n /** Opt into the hosted/serverless default chunk budget. Only callers with\n * automatic continuation support should enable this. */\n useHostedSoftTimeoutDefault?: boolean;\n /** Stable identity for the logical assistant turn this run belongs to. A\n * turn may span several continuation runs (each chunk is its own run); they\n * share one `turnId` so the durable assistant message can be folded across\n * them instead of dropped per-run. Defaults to the runId (turn == run). */\n turnId?: string;\n}\n\nexport interface ResolveRunSoftTimeoutOptions {\n useHostedDefault?: boolean;\n}\n\nfunction isHostedRuntime(): boolean {\n if (\n process.env.NETLIFY &&\n process.env.NETLIFY !== \"false\" &&\n process.env.NETLIFY_LOCAL !== \"true\"\n ) {\n return true;\n }\n if (\n process.env.AWS_LAMBDA_FUNCTION_NAME &&\n process.env.NETLIFY_LOCAL !== \"true\"\n ) {\n return true;\n }\n return Boolean(\n process.env.CF_PAGES ||\n process.env.VERCEL ||\n process.env.VERCEL_ENV ||\n process.env.RENDER ||\n process.env.FLY_APP_NAME ||\n process.env.K_SERVICE,\n );\n}\n\nexport function resolveRunSoftTimeoutMs(\n overrideMs?: number,\n options?: ResolveRunSoftTimeoutOptions,\n): number {\n const hosted = isHostedRuntime();\n // A configured/env soft timeout that exceeds the upstream walls can never\n // actually fire (the gateway/function kills the run first), so clamp it down\n // on hosted runtimes. This is what makes auto_continue reach the client\n // instead of the run dying as builder_gateway_timeout / stale_run. `0` means\n // \"disabled\" and is never clamped up.\n const clampHosted = (ms: number): number =>\n hosted && ms > HOSTED_SOFT_TIMEOUT_CEILING_MS\n ? HOSTED_SOFT_TIMEOUT_CEILING_MS\n : ms;\n\n if (typeof overrideMs === \"number\" && Number.isFinite(overrideMs)) {\n return clampHosted(Math.max(0, overrideMs));\n }\n const envValue = process.env.AGENT_RUN_SOFT_TIMEOUT_MS;\n if (envValue !== undefined) {\n const raw = Number(envValue);\n if (Number.isFinite(raw) && raw >= 0) return clampHosted(raw);\n }\n return options?.useHostedDefault && hosted\n ? DEFAULT_HOSTED_RUN_SOFT_TIMEOUT_MS\n : 0;\n}\n\nexport function resolveCompletedRunRetentionMs(): number {\n const envValue = process.env.AGENT_RUN_RETENTION_MS;\n if (envValue !== undefined) {\n const raw = Number(envValue);\n if (Number.isFinite(raw) && raw >= 0) return raw;\n }\n return DEFAULT_COMPLETED_RUN_RETENTION_MS;\n}\n\nexport function resolveErroredRunRetentionMs(): number {\n const envValue = process.env.AGENT_ERRORED_RUN_RETENTION_MS;\n if (envValue !== undefined) {\n const raw = Number(envValue);\n if (Number.isFinite(raw) && raw >= 0) return raw;\n }\n return DEFAULT_ERRORED_RUN_RETENTION_MS;\n}\n\nfunction isTerminalRunEvent(event: AgentChatEvent): boolean {\n return (\n event.type === \"done\" ||\n event.type === \"error\" ||\n event.type === \"missing_api_key\" ||\n event.type === \"loop_limit\" ||\n event.type === \"auto_continue\"\n );\n}\n\nfunction abortInMemoryRun(run: ActiveRun, reason: string = \"user\") {\n run.abortReason = reason;\n run.status = \"aborted\";\n if (threadToRun.get(run.threadId) === run.runId) {\n threadToRun.delete(run.threadId);\n }\n run.abort.abort(reason);\n for (const subscriber of run.subscribers) {\n try {\n subscriber({ seq: run.events.length, event: { type: \"done\" } });\n } catch {\n // ignore — subscriber is being removed below\n }\n }\n run.subscribers.clear();\n}\n\n/**\n * Start a new agent run in the background.\n * `runFn` receives a `send` callback and an `AbortSignal`.\n * The run continues even if all SSE subscribers disconnect.\n *\n * Events are persisted to SQL for cross-isolate access (Cloudflare Workers).\n */\nexport function startRun(\n runId: string,\n threadId: string,\n runFn: (\n send: (event: AgentChatEvent) => void,\n signal: AbortSignal,\n ) => Promise<void>,\n onComplete?: (run: ActiveRun) => void | Promise<void>,\n options?: StartRunOptions,\n): ActiveRun {\n // If there's already a run for this thread, abort it\n const existingRunId = threadToRun.get(threadId);\n if (existingRunId) {\n abortRun(existingRunId);\n }\n\n const abort = new AbortController();\n let softTimedOut = false;\n const run: ActiveRun = {\n runId,\n threadId,\n turnId: options?.turnId ?? runId,\n events: [],\n status: \"running\",\n subscribers: new Set(),\n abort,\n startedAt: Date.now(),\n };\n\n activeRuns.set(runId, run);\n threadToRun.set(threadId, runId);\n\n // Persist run to SQL without blocking the response. Keep the promise so\n // final status cannot race ahead of a slow initial INSERT and then get\n // overwritten by a late row stuck at status='running'.\n const insertRunPromise = insertRun(runId, threadId, options?.turnId).catch(\n () => {},\n );\n\n // Throttle the durable progress timestamp to at most once per second so\n // a chatty token-by-token stream doesn't translate into one DB write per\n // chunk. The stuck-detector threshold is on the order of tens of seconds,\n // so 1s resolution is plenty.\n let lastProgressBumpAt = 0;\n const bumpProgressIfDue = () => {\n const now = Date.now();\n if (now - lastProgressBumpAt < 1000) return;\n lastProgressBumpAt = now;\n bumpRunProgress(runId).catch(() => {});\n };\n\n // Periodic SQL abort check interval (for cross-isolate abort on Workers)\n let lastAbortCheck = Date.now() - 3000;\n const checkSqlAbort = () => {\n const now = Date.now();\n if (now - lastAbortCheck < 3000) return;\n lastAbortCheck = now;\n getRunAbortState(runId)\n .then((state) => {\n if (state.aborted && !abort.signal.aborted) {\n abortInMemoryRun(run, state.reason ?? \"user\");\n }\n })\n .catch(() => {});\n };\n\n // Heartbeat: bump heartbeat_at every 1.5s so watchers can detect a dead\n // producer (process crash, HMR restart, isolate eviction) quickly and\n // reap the row. Paired with RUN_STALE_MS (6s) — 4x the interval to\n // tolerate transient DB slowness without false positives.\n const heartbeatTimer: ReturnType<typeof setInterval> = setInterval(() => {\n updateRunHeartbeat(runId).catch(() => {});\n checkSqlAbort();\n }, 1500);\n const softTimeoutMs = resolveRunSoftTimeoutMs(options?.softTimeoutMs, {\n useHostedDefault: options?.useHostedSoftTimeoutDefault === true,\n });\n const softTimeoutTimer =\n softTimeoutMs > 0\n ? setTimeout(() => {\n if (run.status !== \"running\" || abort.signal.aborted) return;\n softTimedOut = true;\n send({\n type: \"auto_continue\",\n reason: \"run_timeout\",\n });\n abort.abort();\n }, softTimeoutMs)\n : null;\n let pendingTerminalEvent: RunEvent | null = null;\n\n const captureRunError = (error: unknown, phase: \"run\" | \"completion\") => {\n captureError(error, {\n route: \"/_agent-native/agent-chat\",\n tags: {\n source: \"agent-run-manager\",\n phase,\n runStatus: run.status,\n softTimedOut: softTimedOut ? \"true\" : \"false\",\n abortReason: run.abortReason,\n errorCode: error instanceof EngineError ? error.errorCode : undefined,\n },\n extra: {\n runId,\n threadId,\n eventCount: run.events.length,\n startedAt: run.startedAt,\n softTimeoutMs,\n },\n contexts: {\n agentRun: {\n runId,\n threadId,\n status: run.status,\n phase,\n eventCount: run.events.length,\n startedAt: run.startedAt,\n softTimeoutMs,\n softTimedOut,\n abortReason: run.abortReason,\n },\n },\n });\n };\n\n const emitRunEvent = (\n runEvent: RunEvent,\n options?: { surfacePersistenceError?: boolean },\n ): Promise<void> => {\n run.events.push(runEvent);\n\n // Notify in-memory subscribers (same isolate, fast path)\n for (const subscriber of run.subscribers) {\n try {\n subscriber(runEvent);\n } catch {\n run.subscribers.delete(subscriber);\n }\n }\n\n // Bump the durable progress timestamp. Distinct from the heartbeat:\n // heartbeat = \"process is up\", progress = \"real work is happening.\" The\n // gap between them is what the client-side stuck-detector reads to tell\n // a hung run from a healthy one.\n bumpProgressIfDue();\n\n // Persist event to SQL. Ordinary streaming events are fire-and-forget, but\n // terminal events are awaited before final status is persisted so reconnects\n // never observe status='errored' without the actual terminal error payload.\n const persistence = insertRunEvent(\n runId,\n runEvent.seq,\n JSON.stringify(runEvent.event),\n );\n if (!options?.surfacePersistenceError) {\n persistence.catch(() => {});\n }\n\n checkSqlAbort();\n return persistence;\n };\n\n const send = (event: AgentChatEvent) => {\n if (run.status === \"aborted\" && abort.signal.aborted) return;\n\n const runEvent: RunEvent = { seq: run.events.length, event };\n if (isTerminalRunEvent(event)) {\n pendingTerminalEvent = runEvent;\n return;\n }\n\n emitRunEvent(runEvent);\n };\n\n // Run in background — intentionally detached from any HTTP connection\n const runPromise = runFn(send, abort.signal)\n .then(() => {\n if (abort.signal.aborted) {\n run.status = softTimedOut ? \"completed\" : \"aborted\";\n return;\n }\n run.status = \"completed\";\n })\n .catch((err) => {\n // Don't surface abort errors — the run was intentionally stopped\n if (abort.signal.aborted) {\n run.status = softTimedOut ? \"completed\" : \"aborted\";\n return;\n }\n run.status = \"errored\";\n captureRunError(err, \"run\");\n send({\n type: \"error\",\n error: err?.message ?? \"Unknown error\",\n ...(err instanceof EngineError && err.errorCode\n ? { errorCode: err.errorCode }\n : {}),\n ...(err instanceof EngineError && err.upgradeUrl\n ? { upgradeUrl: err.upgradeUrl }\n : {}),\n });\n })\n .finally(async () => {\n // Ordering matters here — this is the atomic-complete boundary.\n // Invariant: once agent_runs.status flips to \"completed\"/\"errored\"\n // in SQL, thread_data for this turn is already durable. This lets\n // reconnecting clients trust the simple rule \"status != running →\n // fetch thread_data\" without polling/retrying for a race window\n // where onComplete was still pending.\n\n // 1. Await the completion callback (thread_data save). Heartbeat is\n // still ticking so the run doesn't look stale to any concurrent\n // /runs/active check while we wait for SQL writes to land.\n let completionError: unknown = null;\n let terminalPersistenceError: unknown = null;\n if (\n onComplete &&\n !(run.status === \"aborted\" && run.abortReason === \"no_progress\")\n ) {\n try {\n const completionRun: ActiveRun = pendingTerminalEvent\n ? { ...run, events: [...run.events, pendingTerminalEvent] }\n : run;\n await onComplete(completionRun);\n } catch (err) {\n completionError = err;\n captureRunError(err, \"completion\");\n console.error(\n \"[run-manager] onComplete callback error:\",\n err instanceof Error ? err.message : err,\n );\n }\n }\n\n // 2. Compute final status. If the completion callback threw, we'd\n // rather mark the run errored than claim success with incomplete\n // thread_data.\n const finalStatus =\n run.status === \"aborted\"\n ? \"aborted\"\n : run.status === \"errored\" || completionError\n ? \"errored\"\n : \"completed\";\n\n // 3. Emit the terminal event only after thread_data is durable. Live\n // SSE clients close on this event and usually fetch thread_data\n // immediately, so emitting it earlier recreates the final-message\n // race this manager is meant to avoid.\n if (finalStatus === \"completed\" || finalStatus === \"errored\") {\n // Choose the terminal event payload (done / the stashed terminal /\n // a synthesized error). NOTE: the `seq` carried by\n // `pendingTerminalEvent` was captured by `send()` at stash time as\n // `run.events.length` and is NOT authoritative — if the runFn emitted\n // any more events before it actually stopped on the abort signal,\n // those events were pushed and reused that same seq. Persisting the\n // terminal event with the stale seq would collide with an\n // already-persisted streaming event and get silently dropped by\n // insertRunEvent's `ON CONFLICT (run_id, seq) DO NOTHING`, so the\n // client would never see the terminal/continuation signal. We always\n // re-stamp the seq at emit time (max-seq+1) just below.\n const terminalEvent: AgentChatEvent =\n finalStatus === \"completed\"\n ? (pendingTerminalEvent?.event ?? { type: \"done\" })\n : pendingTerminalEvent?.event.type === \"error\"\n ? pendingTerminalEvent.event\n : {\n type: \"error\",\n error: completionError\n ? \"Agent response could not be saved.\"\n : \"Agent run ended unexpectedly\",\n };\n const last = run.events[run.events.length - 1];\n if (!last || !isTerminalRunEvent(last.event)) {\n // Assign the seq at EMIT time, not at stash time. `run.events` is a\n // contiguous 0-based log, so `run.events.length` is the next free\n // seq and can never collide with an event that was pushed after the\n // terminal event was stashed.\n const terminal: RunEvent = {\n seq: run.events.length,\n event: terminalEvent,\n };\n try {\n await emitRunEvent(terminal, { surfacePersistenceError: true });\n } catch (err) {\n terminalPersistenceError = err;\n captureRunError(err, \"completion\");\n console.error(\n \"[run-manager] terminal event persistence error:\",\n err instanceof Error ? err.message : err,\n );\n }\n }\n }\n for (const subscriber of run.subscribers) {\n run.subscribers.delete(subscriber);\n }\n\n // 4. Stop the heartbeat — all liveness writes are done.\n clearInterval(heartbeatTimer);\n if (softTimeoutTimer) clearTimeout(softTimeoutTimer);\n\n // 5. Persist final status to SQL.\n try {\n await insertRunPromise;\n if (!terminalPersistenceError) {\n await updateRunStatus(runId, finalStatus);\n }\n } catch {\n // Best-effort — reapIfStale will eventually clean this up via\n // the heartbeat-stale path.\n }\n\n // 5b. Record terminal failure classification for errored runs so\n // cut-off / failed chats are queryable for pattern analysis. Read\n // the actual error event the run emitted (errorCode + message) so\n // diagnostics reflect the real cause (builder_gateway_timeout,\n // stale_run, context_length_exceeded, completion_error, …).\n if (finalStatus === \"errored\") {\n let errorCode: string | undefined;\n let errorDetail: string | undefined;\n for (let i = run.events.length - 1; i >= 0; i--) {\n const ev = run.events[i].event as {\n type: string;\n error?: string;\n errorCode?: string;\n details?: string;\n };\n if (ev.type === \"error\") {\n errorCode = ev.errorCode;\n errorDetail = ev.error ?? ev.details;\n break;\n }\n }\n if (completionError && !errorCode) {\n errorCode = \"completion_error\";\n errorDetail =\n errorDetail ??\n (completionError instanceof Error\n ? completionError.message\n : String(completionError));\n }\n await setRunError(runId, errorCode ?? \"unknown\", errorDetail);\n }\n\n // 6. Schedule in-memory cleanup + opportunistic old-run pruning.\n setTimeout(() => {\n activeRuns.delete(runId);\n if (threadToRun.get(threadId) === runId) {\n threadToRun.delete(threadId);\n }\n }, CLEANUP_DELAY_MS);\n cleanupOldRuns(\n resolveCompletedRunRetentionMs(),\n resolveErroredRunRetentionMs(),\n ).catch(() => {});\n });\n\n // On Cloudflare Workers, keep the isolate alive for this run\n try {\n const cfCtx = globalThis.__cf_ctx;\n if (cfCtx?.waitUntil) {\n cfCtx.waitUntil(runPromise);\n }\n } catch {\n // Not on Workers — ignore\n }\n\n return run;\n}\n\n/**\n * Subscribe to a run's events starting from `fromSeq`.\n * Returns a ReadableStream that replays buffered events then live-tails.\n * Cancelling the stream only unsubscribes — does NOT abort the agent.\n *\n * Falls back to SQL polling when the run is not in local memory\n * (cross-isolate reconnection on Workers).\n */\nexport function subscribeToRun(\n runId: string,\n fromSeq: number,\n): ReadableStream<Uint8Array> | null {\n const run = activeRuns.get(runId);\n if (run) {\n return subscribeInMemory(run, fromSeq);\n }\n // Not in local memory — try SQL (cross-isolate path)\n return subscribeFromSQL(runId, fromSeq);\n}\n\n/** In-memory subscription (same isolate, fast path) */\nfunction subscribeInMemory(\n run: ActiveRun,\n fromSeq: number,\n): ReadableStream<Uint8Array> {\n const encoder = new TextEncoder();\n let subscriberRef: ((event: RunEvent) => void) | null = null;\n let pingTimer: ReturnType<typeof setInterval> | null = null;\n\n return new ReadableStream({\n start(controller) {\n const ping = () => {\n try {\n controller.enqueue(encoder.encode(`: ping ${Date.now()}\\n\\n`));\n } catch {\n if (subscriberRef) run.subscribers.delete(subscriberRef);\n if (pingTimer) clearInterval(pingTimer);\n }\n };\n ping();\n pingTimer = setInterval(ping, 10_000);\n\n // Replay buffered events from fromSeq\n for (let i = fromSeq; i < run.events.length; i++) {\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({ ...run.events[i].event, seq: run.events[i].seq })}\\n\\n`,\n ),\n );\n } catch {\n return;\n }\n }\n\n // If run is already done, close immediately\n if (run.status !== \"running\") {\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n return;\n }\n\n // Subscribe to live events\n subscriberRef = (event: RunEvent) => {\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({ ...event.event, seq: event.seq })}\\n\\n`,\n ),\n );\n // Close stream after terminal events\n if (isTerminalRunEvent(event.event)) {\n run.subscribers.delete(subscriberRef!);\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n }\n } catch {\n run.subscribers.delete(subscriberRef!);\n }\n };\n\n run.subscribers.add(subscriberRef);\n },\n cancel() {\n // Only unsubscribe — do NOT abort the agent run\n if (subscriberRef) run.subscribers.delete(subscriberRef);\n if (pingTimer) clearInterval(pingTimer);\n },\n });\n}\n\n/** SQL-based subscription (cross-isolate, polling) */\nfunction subscribeFromSQL(\n runId: string,\n fromSeq: number,\n): ReadableStream<Uint8Array> | null {\n const encoder = new TextEncoder();\n let cancelled = false;\n let pollTimer: ReturnType<typeof setTimeout> | null = null;\n let pingTimer: ReturnType<typeof setInterval> | null = null;\n\n return new ReadableStream({\n async start(controller) {\n let lastSeq = fromSeq;\n const ping = () => {\n try {\n controller.enqueue(encoder.encode(`: ping ${Date.now()}\\n\\n`));\n } catch {\n cancelled = true;\n if (pingTimer) clearInterval(pingTimer);\n }\n };\n ping();\n pingTimer = setInterval(ping, 10_000);\n\n const poll = async () => {\n if (cancelled) return;\n try {\n // Read new events from SQL\n const events = await getRunEventsSince(runId, lastSeq);\n for (const { seq, eventData } of events) {\n let parsed: any;\n try {\n parsed = JSON.parse(eventData);\n } catch {\n continue;\n }\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({ ...parsed, seq })}\\n\\n`,\n ),\n );\n } catch {\n cancelled = true;\n return;\n }\n lastSeq = seq + 1;\n\n // Close on terminal events\n if (isTerminalRunEvent(parsed)) {\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n return;\n }\n }\n\n // Check if run completed (no terminal event but status changed)\n if (events.length === 0) {\n // Opportunistically reap a stale producer before trusting SQL's\n // \"running\" status — otherwise a crashed server leaves us polling\n // forever.\n await reapIfStale(runId).catch(() => {});\n const run = await getRunById(runId);\n if (!run || run.status !== \"running\") {\n // Run ended — do one final event read, then close\n const finalEvents = await getRunEventsSince(runId, lastSeq);\n for (const { seq, eventData } of finalEvents) {\n let parsed: any;\n try {\n parsed = JSON.parse(eventData);\n } catch {\n continue;\n }\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({ ...parsed, seq })}\\n\\n`,\n ),\n );\n } catch {\n cancelled = true;\n return;\n }\n lastSeq = seq + 1;\n if (isTerminalRunEvent(parsed)) {\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n return;\n }\n }\n if (run?.status === \"aborted\") {\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({ type: \"done\", seq: lastSeq })}\\n\\n`,\n ),\n );\n } catch {\n cancelled = true;\n return;\n }\n } else if (run?.status === \"completed\") {\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({ type: \"done\", seq: lastSeq })}\\n\\n`,\n ),\n );\n } catch {\n cancelled = true;\n return;\n }\n } else if (run?.status === \"errored\") {\n // The run row was flipped to `errored` but no terminal event\n // was ever persisted — almost always means a reaper's silent\n // `appendTerminalRunEvent(...).catch(() => {})` swallowed a\n // transient DB error, so the user-facing situation is the\n // same as a stale-run reap. Send the friendly event AND try\n // to persist it so future reconnects replay it from SQL\n // rather than regenerating it (the user used to see a bare\n // \"run_terminal_event_missing\" debug string here).\n await ensureTerminalRunEvent(\n runId,\n STALE_RUN_ERROR_EVENT,\n ).catch(() => {});\n try {\n controller.enqueue(\n encoder.encode(\n `data: ${JSON.stringify({\n ...STALE_RUN_ERROR_EVENT,\n seq: lastSeq,\n })}\\n\\n`,\n ),\n );\n } catch {\n cancelled = true;\n return;\n }\n }\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n return;\n }\n }\n\n // Schedule next poll\n if (!cancelled) {\n pollTimer = setTimeout(poll, 500);\n }\n } catch {\n // SQL error — close stream\n try {\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n } catch {}\n }\n };\n\n // Verify run exists before starting poll\n try {\n const run = await getRunById(runId);\n if (!run) {\n if (pingTimer) clearInterval(pingTimer);\n controller.close();\n return;\n }\n } catch {\n controller.close();\n return;\n }\n\n await poll();\n },\n cancel() {\n cancelled = true;\n if (pollTimer) clearTimeout(pollTimer);\n if (pingTimer) clearInterval(pingTimer);\n },\n });\n}\n\n/** Get the active run for a thread (if any) — checks memory then SQL */\nexport function getActiveRunForThread(threadId: string): ActiveRun | null {\n const runId = threadToRun.get(threadId);\n if (runId) {\n const run = activeRuns.get(runId);\n if (run) return run;\n }\n return null;\n}\n\n/**\n * Async version that also checks SQL — for cross-isolate access.\n * Used by the /runs/active endpoint.\n *\n * Returns `heartbeatAt` so the client can independently decide a run is\n * dead even before the server-side stale reap has fired. Returns\n * `lastProgressAt` so the client-side stuck-detector can show a\n * user-visible \"this chat looks stuck\" affordance when a run is alive\n * (heartbeating) but not actually emitting events.\n */\nexport async function getActiveRunForThreadAsync(threadId: string): Promise<{\n runId: string;\n threadId: string;\n status: string;\n heartbeatAt: number;\n lastProgressAt: number | null;\n} | null> {\n // Check memory first — return both running AND recently-completed runs\n // that still have events in memory. This allows sub-agent tabs to replay\n // the full conversation from completed runs via SSE.\n const memRun = getActiveRunForThread(threadId);\n if (memRun && (memRun.status === \"running\" || memRun.events.length > 0)) {\n return {\n runId: memRun.runId,\n threadId: memRun.threadId,\n status: memRun.status,\n // In-memory means this isolate is the producer. By definition, the\n // heartbeat is fresh as of \"now\" — the client can trust this.\n heartbeatAt: Date.now(),\n // For an in-memory run we don't have a separate \"last event emit\"\n // timestamp tracked in JS — the SQL bump is throttled per-second.\n // Read it back from SQL on demand. For the common case the SQL row\n // is well under 1s old; if it isn't, the stuck-detector will pick\n // it up on the next poll cycle.\n lastProgressAt: await fetchLastProgressAt(memRun.runId),\n };\n }\n // Fall back to SQL — also surface recently terminated runs so the client\n // can reconnect and replay synthesized done/error events instead of\n // retrying the original POST. Without this, a POST that fails after the\n // server already accepted (and finished) the run would re-execute the\n // turn and double-apply mutations: the in-memory branch above already\n // returns terminal runs whose events are still buffered, but the SQL\n // path is the only authority once memory has been evicted.\n try {\n const sqlRun = await getRunByThread(threadId, { includeTerminal: true });\n if (!sqlRun) return null;\n if (sqlRun.status === \"running\") {\n // If the producer is dead (no recent heartbeat), reap before the\n // client can see a stale \"running\" status and enter a reconnect\n // loop it can never exit.\n const reaped = await reapIfStale(sqlRun.id).catch(() => false);\n if (reaped) return null;\n return {\n runId: sqlRun.id,\n threadId: sqlRun.threadId,\n status: sqlRun.status,\n heartbeatAt: sqlRun.heartbeatAt ?? sqlRun.startedAt,\n lastProgressAt: sqlRun.lastProgressAt,\n };\n }\n if (sqlRun.status === \"completed\" || sqlRun.status === \"errored\") {\n // Cap how far back we'll surface terminal runs as \"active\". The goal\n // is to catch the recently-completed-but-reconnecting case, not to\n // resurrect ancient turns when the user reopens an old thread.\n //\n // Measure age from the run's terminal timestamp, not its start. A\n // long-running task that ran 11 minutes and completed five seconds\n // ago should still be reachable — the client's disconnect happened\n // around completion, so completion time is what matters for the\n // \"is the user still here waiting?\" question. Fall back to the last\n // heartbeat (older deployments may have unset completed_at) and\n // finally to startedAt for ancient rows.\n const referenceAt =\n sqlRun.completedAt ?? sqlRun.heartbeatAt ?? sqlRun.startedAt;\n const terminalAge = Date.now() - referenceAt;\n if (terminalAge > TERMINAL_RUN_RECONNECT_WINDOW_MS) return null;\n return {\n runId: sqlRun.id,\n threadId: sqlRun.threadId,\n status: sqlRun.status,\n heartbeatAt: sqlRun.heartbeatAt ?? sqlRun.startedAt,\n lastProgressAt: sqlRun.lastProgressAt,\n };\n }\n } catch {\n // SQL error — fall through\n }\n return null;\n}\n\nasync function fetchLastProgressAt(runId: string): Promise<number | null> {\n try {\n const run = await getRunById(runId);\n if (!run) return null;\n // `getRunById` returns a narrow projection today; ask for the row via\n // the thread lookup which carries last_progress_at.\n const byThread = await getRunByThread(run.threadId, {\n includeTerminal: true,\n });\n if (byThread && byThread.id === runId) return byThread.lastProgressAt;\n return null;\n } catch {\n return null;\n }\n}\n\n/** Get a run by ID */\nexport function getRun(runId: string): ActiveRun | null {\n return activeRuns.get(runId) ?? null;\n}\n\n/** Explicitly abort a run (e.g. Stop button) */\nexport function abortRun(runId: string, reason: string = \"user\"): boolean {\n const run = activeRuns.get(runId);\n if (run) {\n abortInMemoryRun(run, reason);\n }\n // Also mark as aborted in SQL (for cross-isolate abort on Workers)\n markRunAborted(runId, reason).catch(() => {});\n return !!run;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-chat-adapter.d.ts","sourceRoot":"","sources":["../../src/client/agent-chat-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAsB,MAAM,qBAAqB,CAAC;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oBAAoB,CAAC;CAChC,GAAG,gBAAgB,CAqgCnB"}
|
|
@@ -30,6 +30,16 @@ const MAX_HISTORY_TOTAL_CHARS = 64_000;
|
|
|
30
30
|
const MAX_HISTORY_MESSAGE_CHARS = 12_000;
|
|
31
31
|
const MAX_HISTORY_TOOL_ARGS_CHARS = 8_000;
|
|
32
32
|
const MAX_HISTORY_TOOL_RESULT_CHARS = 12_000;
|
|
33
|
+
// Tools whose entire input IS the artifact being built (extension HTML, etc.).
|
|
34
|
+
// Lossy-truncating these to a `{ __agentNativeTruncated }` placeholder strands
|
|
35
|
+
// the resumed agent — it can no longer refine the artifact because it sees a
|
|
36
|
+
// fake input. Keep the real input for these on continuation, only collapsing
|
|
37
|
+
// inputs that are far larger than any realistic extension payload.
|
|
38
|
+
const LARGE_INPUT_TOOL_NAMES = new Set([
|
|
39
|
+
"create-extension",
|
|
40
|
+
"update-extension",
|
|
41
|
+
]);
|
|
42
|
+
const MAX_HISTORY_LARGE_TOOL_ARGS_CHARS = 200_000;
|
|
33
43
|
const STARTUP_RESPONSE_TIMEOUT_MS = 45_000;
|
|
34
44
|
function normalizeMentions(text) {
|
|
35
45
|
return text.replace(/@\[([^\]|]+)\|[^\]]+\]/g, "@$1");
|
|
@@ -72,18 +82,25 @@ function messageTextFromContent(content) {
|
|
|
72
82
|
.map((p) => normalizeMentions(p.text))
|
|
73
83
|
.join("\n"), MAX_HISTORY_MESSAGE_CHARS, "Message");
|
|
74
84
|
}
|
|
75
|
-
function truncateToolArgsForHistory(args) {
|
|
85
|
+
function truncateToolArgsForHistory(args, toolName) {
|
|
76
86
|
if (!args || typeof args !== "object" || Array.isArray(args))
|
|
77
87
|
return {};
|
|
88
|
+
// Large-input tools (e.g. create-extension/update-extension) carry the
|
|
89
|
+
// artifact itself as their input. Preserve the real input on continuation so
|
|
90
|
+
// the resumed agent can keep refining it; only collapse inputs that exceed a
|
|
91
|
+
// far larger ceiling than any realistic payload.
|
|
92
|
+
const cap = toolName && LARGE_INPUT_TOOL_NAMES.has(toolName)
|
|
93
|
+
? MAX_HISTORY_LARGE_TOOL_ARGS_CHARS
|
|
94
|
+
: MAX_HISTORY_TOOL_ARGS_CHARS;
|
|
78
95
|
try {
|
|
79
96
|
const json = JSON.stringify(args);
|
|
80
|
-
if (json.length <=
|
|
97
|
+
if (json.length <= cap) {
|
|
81
98
|
return args;
|
|
82
99
|
}
|
|
83
100
|
return {
|
|
84
101
|
__agentNativeTruncated: true,
|
|
85
102
|
note: "Tool input was too large to resend in chat history. Use the current app/resource state as the source of truth if exact content is needed.",
|
|
86
|
-
preview: truncateForHistory(json,
|
|
103
|
+
preview: truncateForHistory(json, cap, "Tool input"),
|
|
87
104
|
};
|
|
88
105
|
}
|
|
89
106
|
catch {
|
|
@@ -299,7 +316,7 @@ function contentToStructuredMessages(content, nextToolCallId, options) {
|
|
|
299
316
|
toolCallId,
|
|
300
317
|
toolName: part.toolName,
|
|
301
318
|
args: truncate
|
|
302
|
-
? truncateToolArgsForHistory(part.args ?? {})
|
|
319
|
+
? truncateToolArgsForHistory(part.args ?? {}, part.toolName)
|
|
303
320
|
: (part.args ?? {}),
|
|
304
321
|
});
|
|
305
322
|
if (part.result !== undefined) {
|
|
@@ -432,6 +449,16 @@ function hasContinuationProgress(content) {
|
|
|
432
449
|
? part.text.trim().length > 0
|
|
433
450
|
: part.result !== undefined);
|
|
434
451
|
}
|
|
452
|
+
/**
|
|
453
|
+
* True when an action was streamed but never returned a result yet — i.e. a
|
|
454
|
+
* `tool_start` with no matching `tool_done`. The server is still executing it,
|
|
455
|
+
* so a run_timeout that fires in this window is NOT a stall: the agent was
|
|
456
|
+
* actively working and `foldAssistantTurn` persisted the in-flight call. We
|
|
457
|
+
* must not count this against the stalled/empty continuation budgets.
|
|
458
|
+
*/
|
|
459
|
+
function hasInFlightToolCall(content) {
|
|
460
|
+
return content.some((part) => part.type === "tool-call" && part.result === undefined);
|
|
461
|
+
}
|
|
435
462
|
function lastActivityTool(trail) {
|
|
436
463
|
for (let i = trail.length - 1; i >= 0; i--) {
|
|
437
464
|
const tool = trail[i]?.tool?.trim();
|
|
@@ -1000,7 +1027,21 @@ export function createAgentChatAdapter(options) {
|
|
|
1000
1027
|
const isTransient = signal.reason !== "loop_limit";
|
|
1001
1028
|
const visibleContent = visibleContentForContinuation();
|
|
1002
1029
|
const currentPartialHistory = contentToContinuationHistory(visibleContent);
|
|
1003
|
-
|
|
1030
|
+
// Real, content-weight progress: streamed text or a completed tool
|
|
1031
|
+
// result. Used to reset the stalled/empty counters so trivial
|
|
1032
|
+
// whitespace-only output cannot keep the run alive indefinitely.
|
|
1033
|
+
const madeContentProgress = hasContinuationProgress(visibleContent);
|
|
1034
|
+
// An action was streamed but has not returned yet (a tool_start with
|
|
1035
|
+
// no tool_done), or the activity trail shows the server was working
|
|
1036
|
+
// on a tool. A run_timeout that fires in this window means the agent
|
|
1037
|
+
// was actively making progress — the server's foldAssistantTurn
|
|
1038
|
+
// persisted the in-flight call — so it must NOT count against the
|
|
1039
|
+
// stalled/empty continuation budgets.
|
|
1040
|
+
const hasInFlightTool = hasInFlightToolCall(visibleContent) ||
|
|
1041
|
+
Boolean(lastActivityTool(signal.activityTrail));
|
|
1042
|
+
// Either real output or an actively-running tool counts as progress
|
|
1043
|
+
// for the stalled/empty caps.
|
|
1044
|
+
const madeProgress = madeContentProgress || hasInFlightTool;
|
|
1004
1045
|
const madeVisibleProgress = visibleContent.length > 0;
|
|
1005
1046
|
const madeDurableToolProgress = visibleContent.some((part) => part.type === "tool-call" && part.result !== undefined);
|
|
1006
1047
|
if (signal.reason === "loop_limit") {
|
|
@@ -1016,7 +1057,11 @@ export function createAgentChatAdapter(options) {
|
|
|
1016
1057
|
!madeActivityProgress) {
|
|
1017
1058
|
return { ok: false, resetVisibleContent: false };
|
|
1018
1059
|
}
|
|
1019
|
-
|
|
1060
|
+
// Reset the empty-continuation counter on real progress — streamed
|
|
1061
|
+
// text/completed tool OR an in-flight tool the server is running —
|
|
1062
|
+
// not merely on a non-zero part count, which whitespace-only or
|
|
1063
|
+
// unresolved-only output would falsely satisfy.
|
|
1064
|
+
if (!madeProgress) {
|
|
1020
1065
|
emptyTransientContinuationAttempts += 1;
|
|
1021
1066
|
if (emptyTransientContinuationAttempts >
|
|
1022
1067
|
MAX_EMPTY_TRANSIENT_CONTINUATIONS) {
|