@stigmer/runner 3.0.8-dev.20260613085218 → 3.0.9-dev.20260615145121

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.
Files changed (106) hide show
  1. package/dist/.build-fingerprint +1 -1
  2. package/dist/activities/execute-cursor/hook-script.d.ts +23 -12
  3. package/dist/activities/execute-cursor/hook-script.js +85 -51
  4. package/dist/activities/execute-cursor/hook-script.js.map +1 -1
  5. package/dist/activities/execute-cursor/index.js +220 -79
  6. package/dist/activities/execute-cursor/index.js.map +1 -1
  7. package/dist/activities/execute-cursor/message-translator.d.ts +51 -9
  8. package/dist/activities/execute-cursor/message-translator.js +146 -20
  9. package/dist/activities/execute-cursor/message-translator.js.map +1 -1
  10. package/dist/activities/execute-cursor/persist-decision.d.ts +42 -0
  11. package/dist/activities/execute-cursor/persist-decision.js +30 -0
  12. package/dist/activities/execute-cursor/persist-decision.js.map +1 -0
  13. package/dist/activities/execute-cursor/prompt-builder.d.ts +25 -0
  14. package/dist/activities/execute-cursor/prompt-builder.js +54 -0
  15. package/dist/activities/execute-cursor/prompt-builder.js.map +1 -1
  16. package/dist/activities/execute-cursor/workspace-setup.d.ts +8 -2
  17. package/dist/activities/execute-cursor/workspace-setup.js +62 -30
  18. package/dist/activities/execute-cursor/workspace-setup.js.map +1 -1
  19. package/dist/activities/execute-deep-agent/index.js +15 -5
  20. package/dist/activities/execute-deep-agent/index.js.map +1 -1
  21. package/dist/activities/execute-deep-agent/status-builder-shared.d.ts +0 -1
  22. package/dist/activities/execute-deep-agent/status-builder-shared.js +32 -8
  23. package/dist/activities/execute-deep-agent/status-builder-shared.js.map +1 -1
  24. package/dist/activities/execute-deep-agent/status-builder.js +4 -5
  25. package/dist/activities/execute-deep-agent/status-builder.js.map +1 -1
  26. package/dist/activities/execute-deep-agent/streaming-v3.js +4 -5
  27. package/dist/activities/execute-deep-agent/streaming-v3.js.map +1 -1
  28. package/dist/activities/execute-deep-agent/streaming.d.ts +9 -1
  29. package/dist/activities/execute-deep-agent/streaming.js +4 -5
  30. package/dist/activities/execute-deep-agent/streaming.js.map +1 -1
  31. package/dist/activities/execute-deep-agent/subagent-tracker.js +4 -5
  32. package/dist/activities/execute-deep-agent/subagent-tracker.js.map +1 -1
  33. package/dist/activities/execute-deep-agent/v3-status-builder.js +6 -5
  34. package/dist/activities/execute-deep-agent/v3-status-builder.js.map +1 -1
  35. package/dist/config.d.ts +21 -0
  36. package/dist/config.js +12 -0
  37. package/dist/config.js.map +1 -1
  38. package/dist/in-flight.d.ts +35 -0
  39. package/dist/in-flight.js +61 -0
  40. package/dist/in-flight.js.map +1 -0
  41. package/dist/runner-manager.d.ts +2 -0
  42. package/dist/runner-manager.js +90 -29
  43. package/dist/runner-manager.js.map +1 -1
  44. package/dist/runner.d.ts +2 -0
  45. package/dist/runner.js +2 -0
  46. package/dist/runner.js.map +1 -1
  47. package/dist/shared/grpc-retry.d.ts +9 -20
  48. package/dist/shared/grpc-retry.js +9 -52
  49. package/dist/shared/grpc-retry.js.map +1 -1
  50. package/dist/shared/stall-watchdog.d.ts +68 -0
  51. package/dist/shared/stall-watchdog.js +102 -0
  52. package/dist/shared/stall-watchdog.js.map +1 -0
  53. package/dist/shared/status-offload.d.ts +84 -0
  54. package/dist/shared/status-offload.js +292 -0
  55. package/dist/shared/status-offload.js.map +1 -0
  56. package/dist/shared/status.d.ts +34 -3
  57. package/dist/shared/status.js +102 -9
  58. package/dist/shared/status.js.map +1 -1
  59. package/dist/{activities/execute-deep-agent → shared}/streaming-scheduler.d.ts +4 -0
  60. package/dist/{activities/execute-deep-agent → shared}/streaming-scheduler.js +4 -0
  61. package/dist/shared/streaming-scheduler.js.map +1 -0
  62. package/package.json +2 -2
  63. package/src/__tests__/config.test.ts +8 -0
  64. package/src/__tests__/in-flight.test.ts +84 -0
  65. package/src/activities/__tests__/classify-tool-approvals.test.ts +1 -0
  66. package/src/activities/__tests__/discover-mcp-server.test.ts +1 -0
  67. package/src/activities/execute-cursor/__tests__/build-prompt.test.ts +74 -0
  68. package/src/activities/execute-cursor/__tests__/hook-script.test.ts +90 -15
  69. package/src/activities/execute-cursor/__tests__/message-translator.test.ts +124 -12
  70. package/src/activities/execute-cursor/__tests__/persist-decision.test.ts +99 -0
  71. package/src/activities/execute-cursor/__tests__/tool-result-image.test.ts +244 -0
  72. package/src/activities/execute-cursor/__tests__/workspace-setup.test.ts +53 -4
  73. package/src/activities/execute-cursor/hook-script.ts +85 -51
  74. package/src/activities/execute-cursor/index.ts +187 -38
  75. package/src/activities/execute-cursor/message-translator.ts +146 -20
  76. package/src/activities/execute-cursor/persist-decision.ts +54 -0
  77. package/src/activities/execute-cursor/prompt-builder.ts +59 -0
  78. package/src/activities/execute-cursor/workspace-setup.ts +76 -44
  79. package/src/activities/execute-deep-agent/__tests__/index.test.ts +1 -0
  80. package/src/activities/execute-deep-agent/__tests__/status-builder-shared.test.ts +66 -0
  81. package/src/activities/execute-deep-agent/__tests__/status-builder.test.ts +6 -3
  82. package/src/activities/execute-deep-agent/__tests__/streaming-v3.test.ts +70 -0
  83. package/src/activities/execute-deep-agent/index.ts +17 -5
  84. package/src/activities/execute-deep-agent/status-builder-shared.ts +27 -5
  85. package/src/activities/execute-deep-agent/status-builder.ts +3 -5
  86. package/src/activities/execute-deep-agent/streaming-v3.ts +5 -5
  87. package/src/activities/execute-deep-agent/streaming.ts +14 -5
  88. package/src/activities/execute-deep-agent/subagent-tracker.ts +4 -5
  89. package/src/activities/execute-deep-agent/v3-status-builder.ts +5 -5
  90. package/src/config.ts +27 -0
  91. package/src/in-flight.ts +71 -0
  92. package/src/runner-manager.ts +127 -33
  93. package/src/runner.ts +6 -0
  94. package/src/shared/__tests__/artifact-storage.test.ts +1 -0
  95. package/src/shared/__tests__/grpc-retry-extended.test.ts +6 -144
  96. package/src/shared/__tests__/grpc-retry.test.ts +5 -134
  97. package/src/shared/__tests__/stall-watchdog.test.ts +193 -0
  98. package/src/shared/__tests__/status-offload.test.ts +256 -0
  99. package/src/shared/__tests__/status.test.ts +199 -0
  100. package/src/shared/grpc-retry.ts +9 -72
  101. package/src/shared/stall-watchdog.ts +122 -0
  102. package/src/shared/status-offload.ts +342 -0
  103. package/src/shared/status.ts +142 -8
  104. package/src/{activities/execute-deep-agent → shared}/streaming-scheduler.ts +4 -0
  105. package/dist/activities/execute-deep-agent/streaming-scheduler.js.map +0 -1
  106. /package/src/{activities/execute-deep-agent → shared}/__tests__/streaming-scheduler.test.ts +0 -0
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Progress-based stall watchdog.
3
+ *
4
+ * Long-running agent turns are kept alive against Temporal by a periodic
5
+ * keep-alive heartbeat (see {@link ./heartbeat.ts}). That heartbeat proves the
6
+ * runner *process* is alive, but it says nothing about whether the agent is
7
+ * making *progress*: a turn that wedges inside the harness stream loop (a tool
8
+ * call that never returns, a model connection that silently dies) keeps
9
+ * heartbeating forever and never times out. The execution then sits at
10
+ * EXECUTION_IN_PROGRESS indefinitely.
11
+ *
12
+ * This watchdog closes that gap. It is an *out-of-band* timer: callers report
13
+ * progress via {@link StallWatchdog.recordActivity} on every stream event AND
14
+ * every token delta, and the watchdog fires `onStall` once if no progress is
15
+ * reported for `stallMs`. "Out-of-band" is the deliberate correctness property
16
+ * — the check runs on its own timer rather than inside the consumer's loop, so
17
+ * it catches a true "no new events for N minutes" hang. An in-band check that
18
+ * only runs when the loop advances cannot fire while the loop is blocked
19
+ * awaiting the next event, which is exactly the wedge we need to detect.
20
+ *
21
+ * This module is the convergence target for stall detection across harnesses:
22
+ * the Cursor harness wires it here, and the native deep-agent harness has its
23
+ * own (harness-local, in-band) check that should later converge onto this one.
24
+ */
25
+ /**
26
+ * Canonical default stall window. Harnesses may override with a larger value
27
+ * when their tool calls routinely run longer without emitting stream activity.
28
+ */
29
+ export declare const DEFAULT_STALL_TIMEOUT_MS = 120000;
30
+ /**
31
+ * Thrown / reported when an agent stream makes no progress for longer than the
32
+ * configured stall window. Carries the observed idle duration so callers can
33
+ * build an actionable, recognizable error message.
34
+ */
35
+ export declare class StallTimeoutError extends Error {
36
+ readonly stalledMs: number;
37
+ constructor(stalledMs: number, detail?: string);
38
+ }
39
+ /**
40
+ * Recognizable prefix on every stall-induced failure message. Single source of
41
+ * truth so callers (and any future UI/log keying) match on one constant.
42
+ */
43
+ export declare const STALL_ERROR_PREFIX = "[StallTimeoutError]";
44
+ /**
45
+ * Build the user-facing failure text for a stall. Keeps the canonical wording
46
+ * (prefix + actionable "Retry or resume.") next to the error it describes.
47
+ */
48
+ export declare function formatStallFailure(error: StallTimeoutError): string;
49
+ export interface StallWatchdog {
50
+ /** Reset the idle timer. Call on every stream event AND every token delta. */
51
+ recordActivity(): void;
52
+ /** Disarm the watchdog. Idempotent; safe to call in a `finally`. */
53
+ stop(): void;
54
+ }
55
+ /**
56
+ * Start an out-of-band stall watchdog.
57
+ *
58
+ * @param stallMs Idle window after which `onStall` fires.
59
+ * @param onStall Invoked at most once with the observed idle duration (ms)
60
+ * when `Date.now() - lastActivityAt` exceeds `stallMs`. The
61
+ * watchdog disarms itself before invoking, so `onStall` runs
62
+ * exactly once even if its handler is slow.
63
+ *
64
+ * The poll interval is `stallMs / 4` capped at 15s: frequent enough to detect a
65
+ * stall promptly without busy-looping, and bounded so a large `stallMs` still
66
+ * polls on a sane cadence.
67
+ */
68
+ export declare function startStallWatchdog(stallMs: number, onStall: (idleMs: number) => void): StallWatchdog;
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Progress-based stall watchdog.
3
+ *
4
+ * Long-running agent turns are kept alive against Temporal by a periodic
5
+ * keep-alive heartbeat (see {@link ./heartbeat.ts}). That heartbeat proves the
6
+ * runner *process* is alive, but it says nothing about whether the agent is
7
+ * making *progress*: a turn that wedges inside the harness stream loop (a tool
8
+ * call that never returns, a model connection that silently dies) keeps
9
+ * heartbeating forever and never times out. The execution then sits at
10
+ * EXECUTION_IN_PROGRESS indefinitely.
11
+ *
12
+ * This watchdog closes that gap. It is an *out-of-band* timer: callers report
13
+ * progress via {@link StallWatchdog.recordActivity} on every stream event AND
14
+ * every token delta, and the watchdog fires `onStall` once if no progress is
15
+ * reported for `stallMs`. "Out-of-band" is the deliberate correctness property
16
+ * — the check runs on its own timer rather than inside the consumer's loop, so
17
+ * it catches a true "no new events for N minutes" hang. An in-band check that
18
+ * only runs when the loop advances cannot fire while the loop is blocked
19
+ * awaiting the next event, which is exactly the wedge we need to detect.
20
+ *
21
+ * This module is the convergence target for stall detection across harnesses:
22
+ * the Cursor harness wires it here, and the native deep-agent harness has its
23
+ * own (harness-local, in-band) check that should later converge onto this one.
24
+ */
25
+ /**
26
+ * Canonical default stall window. Harnesses may override with a larger value
27
+ * when their tool calls routinely run longer without emitting stream activity.
28
+ */
29
+ export const DEFAULT_STALL_TIMEOUT_MS = 120_000;
30
+ /**
31
+ * Thrown / reported when an agent stream makes no progress for longer than the
32
+ * configured stall window. Carries the observed idle duration so callers can
33
+ * build an actionable, recognizable error message.
34
+ */
35
+ export class StallTimeoutError extends Error {
36
+ stalledMs;
37
+ constructor(stalledMs, detail) {
38
+ super(`Agent stream stalled: no activity for ${Math.round(stalledMs / 1000)}s` +
39
+ (detail ? ` (${detail})` : ""));
40
+ this.stalledMs = stalledMs;
41
+ this.name = "StallTimeoutError";
42
+ }
43
+ }
44
+ /**
45
+ * Recognizable prefix on every stall-induced failure message. Single source of
46
+ * truth so callers (and any future UI/log keying) match on one constant.
47
+ */
48
+ export const STALL_ERROR_PREFIX = "[StallTimeoutError]";
49
+ /**
50
+ * Build the user-facing failure text for a stall. Keeps the canonical wording
51
+ * (prefix + actionable "Retry or resume.") next to the error it describes.
52
+ */
53
+ export function formatStallFailure(error) {
54
+ return `${STALL_ERROR_PREFIX} ${error.message}. Retry or resume.`;
55
+ }
56
+ /**
57
+ * Start an out-of-band stall watchdog.
58
+ *
59
+ * @param stallMs Idle window after which `onStall` fires.
60
+ * @param onStall Invoked at most once with the observed idle duration (ms)
61
+ * when `Date.now() - lastActivityAt` exceeds `stallMs`. The
62
+ * watchdog disarms itself before invoking, so `onStall` runs
63
+ * exactly once even if its handler is slow.
64
+ *
65
+ * The poll interval is `stallMs / 4` capped at 15s: frequent enough to detect a
66
+ * stall promptly without busy-looping, and bounded so a large `stallMs` still
67
+ * polls on a sane cadence.
68
+ */
69
+ export function startStallWatchdog(stallMs, onStall) {
70
+ let lastActivityAt = Date.now();
71
+ let fired = false;
72
+ let timer;
73
+ const tickMs = Math.min(Math.max(Math.floor(stallMs / 4), 1), 15_000);
74
+ const disarm = () => {
75
+ if (timer !== undefined) {
76
+ clearInterval(timer);
77
+ timer = undefined;
78
+ }
79
+ };
80
+ timer = setInterval(() => {
81
+ if (fired)
82
+ return;
83
+ const idleMs = Date.now() - lastActivityAt;
84
+ if (idleMs >= stallMs) {
85
+ fired = true;
86
+ disarm();
87
+ onStall(idleMs);
88
+ }
89
+ }, tickMs);
90
+ // Do not keep the event loop alive solely for the watchdog.
91
+ timer.unref?.();
92
+ return {
93
+ recordActivity() {
94
+ lastActivityAt = Date.now();
95
+ },
96
+ stop() {
97
+ fired = true;
98
+ disarm();
99
+ },
100
+ };
101
+ }
102
+ //# sourceMappingURL=stall-watchdog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stall-watchdog.js","sourceRoot":"","sources":["../../src/shared/stall-watchdog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH;;;GAGG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,OAAO,CAAC;AAEhD;;;;GAIG;AACH,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAExB;IADlB,YACkB,SAAiB,EACjC,MAAe;QAEf,KAAK,CACH,yCAAyC,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG;YACtE,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,MAAM,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CACjC,CAAC;QANc,cAAS,GAAT,SAAS,CAAQ;QAOjC,IA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C;QACD,IAAI;YACF,KAAK,GAAG,IAAI,CAAC;YACb,MAAM,EAAE,CAAC;QACX,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Size-bounding guard for the persisted AgentExecutionStatus payload.
3
+ *
4
+ * Tool outputs (an MCP screenshot's base64 image, a giant accessibility-tree
5
+ * dump, a multi-MB shell log, a huge file write) are stored inline in
6
+ * `ToolCall.result`/`args_preview`, which live in `status.messages` and are
7
+ * re-serialized whole on every `persistStatus` -> `updateStatus` gRPC call.
8
+ * Left unchecked, a single large result pushes the message past the server's
9
+ * 4 MiB gRPC receive cap; the call fails with `resource_exhausted`, progress
10
+ * stops persisting, and the live UI freezes mid-execution.
11
+ *
12
+ * This module enforces, at the single persist boundary, that the payload stays
13
+ * under the limit:
14
+ *
15
+ * 1. offloadOversizedToolOutputs — per-tool-call, SIZE-DRIVEN (not keyed on
16
+ * tool type): any result over the byte threshold is spilled to artifact
17
+ * storage and replaced with a short head plus a typed ToolCallOutputRef.
18
+ * Images are uploaded as their decoded bytes (so the UI can render an
19
+ * <img>); other large output is uploaded as text with a preview head.
20
+ * Idempotent and content-hash-deduped so the throttled, repeated persists
21
+ * (and result re-inflation by mergeToolCallEvent) upload each blob once.
22
+ *
23
+ * 2. enforceStatusSizeLimit — an aggregate, type-agnostic backstop that runs
24
+ * even when no artifact storage is available: if the encoded status still
25
+ * exceeds a soft cap (comfortably under 4 MiB), it elides the largest
26
+ * remaining inline fields in place until the payload fits.
27
+ *
28
+ * Both operate ONLY on what is persisted/streamed; the agent's working context
29
+ * is managed by the harness/SDK separately, so reasoning is unaffected.
30
+ */
31
+ import type { AgentExecutionStatus } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/api_pb";
32
+ import type { ArtifactStorage } from "./artifact-storage.js";
33
+ /**
34
+ * A single tool output (result or args_preview) larger than this many bytes is
35
+ * offloaded to artifact storage instead of inlined into the persisted status.
36
+ * 256 KiB is generous for ordinary tool output yet far below the 4 MiB cap, so
37
+ * even a handful of large-but-sub-threshold results cannot aggregate past it.
38
+ */
39
+ export declare const INLINE_TOOL_OUTPUT_MAX_BYTES: number;
40
+ /** Head of an offloaded text result kept inline for an at-a-glance preview. */
41
+ export declare const TEXT_PREVIEW_HEAD_CHARS = 4000;
42
+ /**
43
+ * Soft cap for the whole encoded status. Kept comfortably under the 4 MiB gRPC
44
+ * receive limit so the aggregate backstop trims before the server ever rejects.
45
+ */
46
+ export declare const STATUS_PAYLOAD_SOFT_LIMIT_BYTES: number;
47
+ /**
48
+ * Tighter cap used only after the server has already rejected a payload as too
49
+ * large, to maximize the chance the retry succeeds.
50
+ */
51
+ export declare const STATUS_PAYLOAD_HARD_LIMIT_BYTES: number;
52
+ export interface ToolOutputOffloadContext {
53
+ readonly artifactStorage: ArtifactStorage;
54
+ readonly executionId: string;
55
+ /** Override the per-result byte threshold (tests use a small value). */
56
+ readonly maxInlineBytes?: number;
57
+ }
58
+ interface ImagePayload {
59
+ readonly base64: string;
60
+ readonly mimeType: string;
61
+ }
62
+ /**
63
+ * Best-effort extraction of an inline base64 image from a tool result string.
64
+ * Handles a raw data URL, MCP image content blocks ({type:"image", data,
65
+ * mimeType}) and OpenAI-style image_url blocks. Returns null for non-image or
66
+ * unparseable results so the caller falls back to text offload.
67
+ */
68
+ export declare function detectImagePayload(result: string): ImagePayload | null;
69
+ /**
70
+ * Offload every oversized tool result in the status to artifact storage,
71
+ * replacing the inline value with a short head + ToolCallOutputRef. Per-tool
72
+ * failures fall back to an inline truncation (a bounded result beats a failed
73
+ * persist) and never throw, so a storage hiccup cannot fail the execution.
74
+ */
75
+ export declare function offloadOversizedToolOutputs(status: AgentExecutionStatus, ctx: ToolOutputOffloadContext): Promise<void>;
76
+ /**
77
+ * Aggregate, type-agnostic backstop. If the encoded status exceeds
78
+ * `softLimitBytes`, elide the largest inline tool fields (and, as a last
79
+ * resort, message content) in place until it fits. Returns true if anything
80
+ * was elided. This guarantees a bounded payload even without artifact storage
81
+ * (e.g. offload disabled) or when many medium results sum past the limit.
82
+ */
83
+ export declare function enforceStatusSizeLimit(status: AgentExecutionStatus, softLimitBytes?: number): boolean;
84
+ export {};
@@ -0,0 +1,292 @@
1
+ /**
2
+ * Size-bounding guard for the persisted AgentExecutionStatus payload.
3
+ *
4
+ * Tool outputs (an MCP screenshot's base64 image, a giant accessibility-tree
5
+ * dump, a multi-MB shell log, a huge file write) are stored inline in
6
+ * `ToolCall.result`/`args_preview`, which live in `status.messages` and are
7
+ * re-serialized whole on every `persistStatus` -> `updateStatus` gRPC call.
8
+ * Left unchecked, a single large result pushes the message past the server's
9
+ * 4 MiB gRPC receive cap; the call fails with `resource_exhausted`, progress
10
+ * stops persisting, and the live UI freezes mid-execution.
11
+ *
12
+ * This module enforces, at the single persist boundary, that the payload stays
13
+ * under the limit:
14
+ *
15
+ * 1. offloadOversizedToolOutputs — per-tool-call, SIZE-DRIVEN (not keyed on
16
+ * tool type): any result over the byte threshold is spilled to artifact
17
+ * storage and replaced with a short head plus a typed ToolCallOutputRef.
18
+ * Images are uploaded as their decoded bytes (so the UI can render an
19
+ * <img>); other large output is uploaded as text with a preview head.
20
+ * Idempotent and content-hash-deduped so the throttled, repeated persists
21
+ * (and result re-inflation by mergeToolCallEvent) upload each blob once.
22
+ *
23
+ * 2. enforceStatusSizeLimit — an aggregate, type-agnostic backstop that runs
24
+ * even when no artifact storage is available: if the encoded status still
25
+ * exceeds a soft cap (comfortably under 4 MiB), it elides the largest
26
+ * remaining inline fields in place until the payload fits.
27
+ *
28
+ * Both operate ONLY on what is persisted/streamed; the agent's working context
29
+ * is managed by the harness/SDK separately, so reasoning is unaffected.
30
+ */
31
+ import { createHash } from "node:crypto";
32
+ import { create, toBinary } from "@bufbuild/protobuf";
33
+ import { AgentExecutionStatusSchema, } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/api_pb";
34
+ import { ToolCallOutputRefSchema } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/message_pb";
35
+ /**
36
+ * A single tool output (result or args_preview) larger than this many bytes is
37
+ * offloaded to artifact storage instead of inlined into the persisted status.
38
+ * 256 KiB is generous for ordinary tool output yet far below the 4 MiB cap, so
39
+ * even a handful of large-but-sub-threshold results cannot aggregate past it.
40
+ */
41
+ export const INLINE_TOOL_OUTPUT_MAX_BYTES = 256 * 1024;
42
+ /** Head of an offloaded text result kept inline for an at-a-glance preview. */
43
+ export const TEXT_PREVIEW_HEAD_CHARS = 4_000;
44
+ /**
45
+ * Soft cap for the whole encoded status. Kept comfortably under the 4 MiB gRPC
46
+ * receive limit so the aggregate backstop trims before the server ever rejects.
47
+ */
48
+ export const STATUS_PAYLOAD_SOFT_LIMIT_BYTES = 3 * 1024 * 1024;
49
+ /**
50
+ * Tighter cap used only after the server has already rejected a payload as too
51
+ * large, to maximize the chance the retry succeeds.
52
+ */
53
+ export const STATUS_PAYLOAD_HARD_LIMIT_BYTES = 2 * 1024 * 1024;
54
+ /** Marker left in place of an aggregate-elided inline field. */
55
+ const ELISION_MARKER = "[output elided to keep status under the size limit]";
56
+ /** Below this size an inline field is not worth eliding (the marker is ~50B). */
57
+ const ELISION_MIN_BYTES = 1_024;
58
+ function byteLen(s) {
59
+ return s ? Buffer.byteLength(s, "utf8") : 0;
60
+ }
61
+ function sha256(s) {
62
+ return createHash("sha256").update(s, "utf8").digest("hex");
63
+ }
64
+ function headChars(s, n) {
65
+ return s.length <= n ? s : s.slice(0, n);
66
+ }
67
+ function formatBytes(n) {
68
+ if (n >= 1024 * 1024)
69
+ return `${(n / (1024 * 1024)).toFixed(1)} MB`;
70
+ if (n >= 1024)
71
+ return `${(n / 1024).toFixed(1)} KB`;
72
+ return `${n} B`;
73
+ }
74
+ function extFromMime(mimeType) {
75
+ switch (mimeType) {
76
+ case "image/png": return "png";
77
+ case "image/jpeg": return "jpg";
78
+ case "image/gif": return "gif";
79
+ case "image/webp": return "webp";
80
+ case "image/svg+xml": return "svg";
81
+ default: return "bin";
82
+ }
83
+ }
84
+ function matchDataUrl(s) {
85
+ const m = s.match(/^data:(image\/[a-zA-Z0-9.+-]+);base64,([\s\S]+)$/);
86
+ if (!m)
87
+ return null;
88
+ return { mimeType: m[1], base64: m[2].replace(/\s+/g, "") };
89
+ }
90
+ function contentBlocks(parsed) {
91
+ if (Array.isArray(parsed))
92
+ return parsed;
93
+ if (parsed && typeof parsed === "object") {
94
+ const obj = parsed;
95
+ if (Array.isArray(obj.content))
96
+ return obj.content;
97
+ // Defensive: a serialized LangChain ToolMessage envelope nests its blocks
98
+ // under kwargs.content. The deep-agent extractor already normalizes image
99
+ // results to a top-level array (status-builder-shared.ts), so this branch is
100
+ // insurance against future shape drift — not the primary path.
101
+ const kwargs = obj.kwargs;
102
+ if (kwargs && typeof kwargs === "object" && Array.isArray(kwargs.content)) {
103
+ return kwargs.content;
104
+ }
105
+ }
106
+ return [];
107
+ }
108
+ /**
109
+ * Best-effort extraction of an inline base64 image from a tool result string.
110
+ * Handles a raw data URL, MCP image content blocks ({type:"image", data,
111
+ * mimeType}) and OpenAI-style image_url blocks. Returns null for non-image or
112
+ * unparseable results so the caller falls back to text offload.
113
+ */
114
+ export function detectImagePayload(result) {
115
+ const direct = matchDataUrl(result.trim());
116
+ if (direct)
117
+ return direct;
118
+ let parsed;
119
+ try {
120
+ parsed = JSON.parse(result);
121
+ }
122
+ catch {
123
+ return null;
124
+ }
125
+ for (const block of contentBlocks(parsed)) {
126
+ if (!block || typeof block !== "object")
127
+ continue;
128
+ const obj = block;
129
+ const type = typeof obj.type === "string" ? obj.type : "";
130
+ if (type !== "image" && type !== "image_url")
131
+ continue;
132
+ const raw = typeof obj.data === "string" ? obj.data :
133
+ typeof obj.image === "string" ? obj.image :
134
+ undefined;
135
+ if (raw) {
136
+ const asUrl = matchDataUrl(raw);
137
+ if (asUrl)
138
+ return asUrl;
139
+ const mimeType = typeof obj.mimeType === "string" ? obj.mimeType :
140
+ typeof obj.mime_type === "string" ? obj.mime_type :
141
+ "image/png";
142
+ return { mimeType, base64: raw.replace(/\s+/g, "") };
143
+ }
144
+ const imageUrl = obj.image_url;
145
+ if (imageUrl && typeof imageUrl === "object") {
146
+ const url = imageUrl.url;
147
+ if (typeof url === "string") {
148
+ const asUrl = matchDataUrl(url);
149
+ if (asUrl)
150
+ return asUrl;
151
+ }
152
+ }
153
+ }
154
+ return null;
155
+ }
156
+ function collapsedResultFor(ref) {
157
+ if (ref.isImage) {
158
+ return `[image output — ${formatBytes(Number(ref.sizeBytes))}, view inline]`;
159
+ }
160
+ const tail = `\n\n[output truncated — ${formatBytes(Number(ref.sizeBytes))} total; view full output]`;
161
+ return ref.truncatedPreview + tail;
162
+ }
163
+ async function maybeOffloadToolCall(tc, ctx, maxBytes) {
164
+ const result = tc.result;
165
+ if (!result)
166
+ return;
167
+ const hash = sha256(result);
168
+ // Idempotent: the same content was already offloaded on a prior persist (or
169
+ // mergeToolCallEvent re-inflated `result` with identical bytes). Re-collapse
170
+ // the inline copy without re-uploading.
171
+ if (tc.outputRef && tc.outputRef.contentHash === hash) {
172
+ tc.result = collapsedResultFor(tc.outputRef);
173
+ return;
174
+ }
175
+ // Images are offloaded regardless of size, BEFORE the size gate below: a
176
+ // `ToolCallOutputRef` is the only path the UI has to render an image inline,
177
+ // so even a sub-threshold screenshot must be lifted out of `result` into a
178
+ // renderable ref. Non-image text only offloads when it exceeds maxBytes.
179
+ const image = detectImagePayload(result);
180
+ if (image) {
181
+ const bytes = Buffer.from(image.base64, "base64");
182
+ const key = `artifacts/${ctx.executionId}/toolcalls/${tc.id}.${extFromMime(image.mimeType)}`;
183
+ await ctx.artifactStorage.upload(key, bytes, image.mimeType);
184
+ const downloadUrl = await ctx.artifactStorage.getDownloadUrl(key);
185
+ tc.outputRef = create(ToolCallOutputRefSchema, {
186
+ storageKey: key,
187
+ downloadUrl,
188
+ sizeBytes: BigInt(bytes.length),
189
+ contentHash: hash,
190
+ mimeType: image.mimeType,
191
+ isImage: true,
192
+ truncatedPreview: "",
193
+ });
194
+ tc.result = collapsedResultFor(tc.outputRef);
195
+ return;
196
+ }
197
+ // Non-image text below the inline budget stays inline; only oversized text is
198
+ // spilled (with a head preview) to keep the persisted payload bounded.
199
+ if (byteLen(result) <= maxBytes)
200
+ return;
201
+ const content = Buffer.from(result, "utf8");
202
+ const key = `artifacts/${ctx.executionId}/toolcalls/${tc.id}.txt`;
203
+ await ctx.artifactStorage.upload(key, content, "text/plain");
204
+ const downloadUrl = await ctx.artifactStorage.getDownloadUrl(key);
205
+ tc.outputRef = create(ToolCallOutputRefSchema, {
206
+ storageKey: key,
207
+ downloadUrl,
208
+ sizeBytes: BigInt(content.length),
209
+ contentHash: hash,
210
+ mimeType: "text/plain",
211
+ isImage: false,
212
+ truncatedPreview: headChars(result, TEXT_PREVIEW_HEAD_CHARS),
213
+ });
214
+ tc.result = collapsedResultFor(tc.outputRef);
215
+ }
216
+ /**
217
+ * Offload every oversized tool result in the status to artifact storage,
218
+ * replacing the inline value with a short head + ToolCallOutputRef. Per-tool
219
+ * failures fall back to an inline truncation (a bounded result beats a failed
220
+ * persist) and never throw, so a storage hiccup cannot fail the execution.
221
+ */
222
+ export async function offloadOversizedToolOutputs(status, ctx) {
223
+ const maxBytes = ctx.maxInlineBytes ?? INLINE_TOOL_OUTPUT_MAX_BYTES;
224
+ for (const msg of status.messages) {
225
+ for (const tc of msg.toolCalls) {
226
+ try {
227
+ await maybeOffloadToolCall(tc, ctx, maxBytes);
228
+ }
229
+ catch (err) {
230
+ const original = tc.result ?? "";
231
+ tc.result =
232
+ headChars(original, TEXT_PREVIEW_HEAD_CHARS) +
233
+ `\n\n[output truncated — offload failed: ${err instanceof Error ? err.message : String(err)}]`;
234
+ console.warn(`[status-offload] execution=${ctx.executionId} tool=${tc.name} ` +
235
+ `offload failed (non-fatal); truncated inline`);
236
+ }
237
+ }
238
+ }
239
+ }
240
+ function encodedSize(status) {
241
+ return toBinary(AgentExecutionStatusSchema, status).length;
242
+ }
243
+ /**
244
+ * Aggregate, type-agnostic backstop. If the encoded status exceeds
245
+ * `softLimitBytes`, elide the largest inline tool fields (and, as a last
246
+ * resort, message content) in place until it fits. Returns true if anything
247
+ * was elided. This guarantees a bounded payload even without artifact storage
248
+ * (e.g. offload disabled) or when many medium results sum past the limit.
249
+ */
250
+ export function enforceStatusSizeLimit(status, softLimitBytes = STATUS_PAYLOAD_SOFT_LIMIT_BYTES) {
251
+ if (encodedSize(status) <= softLimitBytes)
252
+ return false;
253
+ const toolCalls = [];
254
+ for (const msg of status.messages) {
255
+ for (const tc of msg.toolCalls)
256
+ toolCalls.push(tc);
257
+ }
258
+ // Largest inline footprint first so we shed the most bytes per elision.
259
+ toolCalls.sort((a, b) => byteLen(b.result) + byteLen(b.argsPreview) -
260
+ (byteLen(a.result) + byteLen(a.argsPreview)));
261
+ let elidedAny = false;
262
+ for (const tc of toolCalls) {
263
+ if (encodedSize(status) <= softLimitBytes)
264
+ return elidedAny;
265
+ if (byteLen(tc.result) > ELISION_MIN_BYTES) {
266
+ tc.result = ELISION_MARKER;
267
+ elidedAny = true;
268
+ }
269
+ if (byteLen(tc.argsPreview) > ELISION_MIN_BYTES) {
270
+ tc.argsPreview = ELISION_MARKER;
271
+ elidedAny = true;
272
+ }
273
+ if (tc.args !== undefined) {
274
+ tc.args = undefined;
275
+ elidedAny = true;
276
+ }
277
+ }
278
+ // Last resort: oversized message content (e.g. a huge AI response).
279
+ if (encodedSize(status) > softLimitBytes) {
280
+ const byContent = [...status.messages].sort((a, b) => byteLen(b.content) - byteLen(a.content));
281
+ for (const msg of byContent) {
282
+ if (encodedSize(status) <= softLimitBytes)
283
+ break;
284
+ if (byteLen(msg.content) > ELISION_MIN_BYTES) {
285
+ msg.content = headChars(msg.content, TEXT_PREVIEW_HEAD_CHARS) + `\n\n${ELISION_MARKER}`;
286
+ elidedAny = true;
287
+ }
288
+ }
289
+ }
290
+ return elidedAny;
291
+ }
292
+ //# sourceMappingURL=status-offload.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"status-offload.js","sourceRo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AAC;gBAC7C,GAAG,CAAC,OAAO,GAAG,SAAS,CAAC,GAAG,CAAC,OAAO,EAAE,uBAAuB,CAAC,GAAG,OAAO,cAAc,EAAE,CAAC;gBACxF,SAAS,GAAG,IAAI,CAAC;YACnB,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
@@ -10,13 +10,44 @@
10
10
  import type { AgentExecutionStatus } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/api_pb";
11
11
  import { ExecutionControlSignal } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/enum_pb";
12
12
  import type { StigmerClient } from "../client/stigmer-client.js";
13
+ import { type ToolOutputOffloadContext } from "./status-offload.js";
14
+ import { type RetryOptions } from "./grpc-retry.js";
13
15
  export declare function utcTimestamp(): string;
16
+ /** Options controlling how a status payload is bounded and persisted. */
17
+ export interface PersistStatusOptions {
18
+ /**
19
+ * When supplied, oversized tool outputs (a screenshot's base64, a multi-MB
20
+ * dump) are spilled to artifact storage before persisting — the only path
21
+ * that populates a renderable `ToolCallOutputRef`. When omitted, the
22
+ * aggregate size backstop still runs, so the payload is bounded regardless.
23
+ */
24
+ readonly offload?: ToolOutputOffloadContext;
25
+ /**
26
+ * Transient-error backoff policy. Omitted fields fall back to
27
+ * {@link DEFAULT_PERSIST_RETRY}; an omitted `retry` uses it entirely.
28
+ */
29
+ readonly retry?: RetryOptions;
30
+ }
14
31
  /**
15
32
  * Persist execution status via gRPC. Returns the control signal from the
16
- * backend (e.g. STOP to abort the execution). Errors are logged and
17
- * swallowed — a failed status update should not crash the activity.
33
+ * backend (e.g. STOP to abort the execution).
34
+ *
35
+ * This is the single, unforgeable chokepoint for status persistence across
36
+ * every harness (streaming and terminal, native and Cursor). Routing all
37
+ * persistence through here guarantees three invariants that must never be
38
+ * skipped by an individual call site:
39
+ *
40
+ * 1. Size bounding — when `offload` is supplied, oversized tool outputs are
41
+ * spilled to artifact storage first (the path that makes screenshots
42
+ * render); an aggregate backstop then runs unconditionally so callers
43
+ * without storage are still protected from the 4 MiB gRPC cap.
44
+ * 2. Transient resilience — `Unavailable`/`DeadlineExceeded` back off and
45
+ * retry (default three attempts); terminal errors never retry.
46
+ * 3. Oversize recovery — a `resource_exhausted` rejection is never silently
47
+ * swallowed: we hard-elide and retry once, so an oversized payload can
48
+ * never freeze persistence invisibly (the bug that stalled the UI).
18
49
  */
19
- export declare function persistStatus(client: StigmerClient, executionId: string, status: AgentExecutionStatus): Promise<ExecutionControlSignal>;
50
+ export declare function persistStatus(client: StigmerClient, executionId: string, status: AgentExecutionStatus, options?: PersistStatusOptions): Promise<ExecutionControlSignal>;
20
51
  /**
21
52
  * Report a setup progress phase (e.g. "Resolving MCP servers") so the
22
53
  * frontend can display a spinner with context.