@superblocksteam/vite-plugin-file-sync 2.0.179 → 2.0.180-next.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-tool-executor/claude-file-tools.d.ts +10 -72
- package/dist/agent-tool-executor/claude-file-tools.d.ts.map +1 -1
- package/dist/agent-tool-executor/claude-sandbox.d.ts +1 -15
- package/dist/agent-tool-executor/claude-sandbox.d.ts.map +1 -1
- package/dist/agent-tool-executor/claude-shell.d.ts +5 -37
- package/dist/agent-tool-executor/claude-shell.d.ts.map +1 -1
- package/dist/agent-tool-executor/claude-tool-server.js +2 -2
- package/dist/agent-tool-executor/claude-tool-server.js.map +1 -1
- package/dist/agent-tool-executor/codex-exec-server.d.ts.map +1 -1
- package/dist/agent-tool-executor/codex-exec-server.js +6 -4
- package/dist/agent-tool-executor/codex-exec-server.js.map +1 -1
- package/dist/agent-tool-executor/codex-execution-policy.js +2 -2
- package/dist/agent-tool-executor/codex-execution-policy.js.map +1 -1
- package/dist/agent-tool-executor/native-draft-service.d.ts +5 -2
- package/dist/agent-tool-executor/native-draft-service.d.ts.map +1 -1
- package/dist/agent-tool-executor/native-tool-environment.js +1 -1
- package/dist/agent-tool-executor/native-tool-environment.js.map +1 -1
- package/dist/ai-service/agent/prompts/build-base-system-prompt.d.ts +2 -0
- package/dist/ai-service/agent/prompts/build-base-system-prompt.d.ts.map +1 -1
- package/dist/ai-service/agent/prompts/build-base-system-prompt.js +9 -2
- package/dist/ai-service/agent/prompts/build-base-system-prompt.js.map +1 -1
- package/dist/ai-service/agent/tools/branching/branch-tools.d.ts +2 -2
- package/dist/ai-service/agent/tools/branching/branch-tools.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/branching/branch-tools.js +1 -1
- package/dist/ai-service/agent/tools/branching/branch-tools.js.map +1 -1
- package/dist/ai-service/agent/tools/build-copy-utils.d.ts +13 -44
- package/dist/ai-service/agent/tools/build-copy-utils.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-debug.d.ts +1 -1
- package/dist/ai-service/agent/tools/build-debug.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-delete-file.d.ts +1 -0
- package/dist/ai-service/agent/tools/build-delete-file.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-delete-file.js +3 -0
- package/dist/ai-service/agent/tools/build-delete-file.js.map +1 -1
- package/dist/ai-service/agent/tools/build-edit-file.d.ts +2 -0
- package/dist/ai-service/agent/tools/build-edit-file.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-edit-file.js +7 -0
- package/dist/ai-service/agent/tools/build-edit-file.js.map +1 -1
- package/dist/ai-service/agent/tools/build-install-packages.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-install-packages.js +5 -1
- package/dist/ai-service/agent/tools/build-install-packages.js.map +1 -1
- package/dist/ai-service/agent/tools/build-list-files.d.ts +1 -1
- package/dist/ai-service/agent/tools/build-multi-edit-file.d.ts +1 -0
- package/dist/ai-service/agent/tools/build-multi-edit-file.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-multi-edit-file.js +3 -0
- package/dist/ai-service/agent/tools/build-multi-edit-file.js.map +1 -1
- package/dist/ai-service/agent/tools/build-navigate-preview.js +2 -2
- package/dist/ai-service/agent/tools/build-navigate-preview.js.map +1 -1
- package/dist/ai-service/agent/tools/build-write-file.d.ts +1 -0
- package/dist/ai-service/agent/tools/build-write-file.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-write-file.js +3 -0
- package/dist/ai-service/agent/tools/build-write-file.js.map +1 -1
- package/dist/ai-service/agent/tools/clear-error-cache.d.ts +1 -1
- package/dist/ai-service/agent/tools/databases/dev-database-tasks.d.ts +39 -185
- package/dist/ai-service/agent/tools/databases/dev-database-tasks.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/databases/dev-database-tasks.js +35 -13
- package/dist/ai-service/agent/tools/databases/dev-database-tasks.js.map +1 -1
- package/dist/ai-service/agent/tools/databases/dev-database-tools.d.ts +2 -2
- package/dist/ai-service/agent/tools/get-security-findings.d.ts +1 -1
- package/dist/ai-service/agent/tools/integrations/execute-request.d.ts +47 -342
- package/dist/ai-service/agent/tools/integrations/execute-request.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/integrations/execute-request.js +4 -4
- package/dist/ai-service/agent/tools/integrations/execute-request.js.map +1 -1
- package/dist/ai-service/agent/tools/integrations/integration-setup.d.ts +2 -2
- package/dist/ai-service/agent/tools/integrations/integration-setup.js +3 -3
- package/dist/ai-service/agent/tools/integrations/integration-setup.js.map +1 -1
- package/dist/ai-service/agent/tools/project-agent-feedback.d.ts +17 -0
- package/dist/ai-service/agent/tools/project-agent-feedback.d.ts.map +1 -0
- package/dist/ai-service/agent/tools/project-agent-feedback.js +87 -0
- package/dist/ai-service/agent/tools/project-agent-feedback.js.map +1 -0
- package/dist/ai-service/agent/tools/report-security-findings.d.ts +25 -112
- package/dist/ai-service/agent/tools/report-security-findings.d.ts.map +1 -1
- package/dist/ai-service/agent/tools.d.ts.map +1 -1
- package/dist/ai-service/agent/tools.js +1 -0
- package/dist/ai-service/agent/tools.js.map +1 -1
- package/dist/ai-service/agent/tools2/tools/bash.d.ts +2 -2
- package/dist/ai-service/agent/tools2/tools/end-test-run.d.ts +2 -2
- package/dist/ai-service/agent/tools2/tools/list-tasks.d.ts +1 -1
- package/dist/ai-service/agent/tools2/types.d.ts +1 -1
- package/dist/ai-service/agent/tools2/types.d.ts.map +1 -1
- package/dist/ai-service/agent/tools2/types.js.map +1 -1
- package/dist/ai-service/app-interface/invalid-lockfile-error.d.ts +11 -0
- package/dist/ai-service/app-interface/invalid-lockfile-error.d.ts.map +1 -0
- package/dist/ai-service/app-interface/invalid-lockfile-error.js +27 -0
- package/dist/ai-service/app-interface/invalid-lockfile-error.js.map +1 -0
- package/dist/ai-service/app-interface/npm-lockfile-sources.d.ts +13 -0
- package/dist/ai-service/app-interface/npm-lockfile-sources.d.ts.map +1 -0
- package/dist/ai-service/app-interface/npm-lockfile-sources.js +129 -0
- package/dist/ai-service/app-interface/npm-lockfile-sources.js.map +1 -0
- package/dist/ai-service/app-interface/npm-registry.d.ts +20 -10
- package/dist/ai-service/app-interface/npm-registry.d.ts.map +1 -1
- package/dist/ai-service/app-interface/npm-registry.js +109 -73
- package/dist/ai-service/app-interface/npm-registry.js.map +1 -1
- package/dist/ai-service/app-interface/registry-lockfile-preparer.d.ts +46 -0
- package/dist/ai-service/app-interface/registry-lockfile-preparer.d.ts.map +1 -0
- package/dist/ai-service/app-interface/registry-lockfile-preparer.js +88 -0
- package/dist/ai-service/app-interface/registry-lockfile-preparer.js.map +1 -0
- package/dist/ai-service/app-interface/shell.d.ts +14 -4
- package/dist/ai-service/app-interface/shell.d.ts.map +1 -1
- package/dist/ai-service/app-interface/shell.js +45 -30
- package/dist/ai-service/app-interface/shell.js.map +1 -1
- package/dist/ai-service/builder-binding-error.d.ts +5 -0
- package/dist/ai-service/builder-binding-error.d.ts.map +1 -0
- package/dist/ai-service/builder-binding-error.js +8 -0
- package/dist/ai-service/builder-binding-error.js.map +1 -0
- package/dist/ai-service/builder-conversation.d.ts +44 -3
- package/dist/ai-service/builder-conversation.d.ts.map +1 -1
- package/dist/ai-service/builder-conversation.js +353 -47
- package/dist/ai-service/builder-conversation.js.map +1 -1
- package/dist/ai-service/builder-core-prompt.d.ts +1 -1
- package/dist/ai-service/builder-core-prompt.d.ts.map +1 -1
- package/dist/ai-service/builder-core-prompt.js +7 -12
- package/dist/ai-service/builder-core-prompt.js.map +1 -1
- package/dist/ai-service/builder-runtime-auth.d.ts.map +1 -1
- package/dist/ai-service/builder-runtime-auth.js +6 -2
- package/dist/ai-service/builder-runtime-auth.js.map +1 -1
- package/dist/ai-service/builder-runtime.d.ts +103 -237
- package/dist/ai-service/builder-runtime.d.ts.map +1 -1
- package/dist/ai-service/builder-runtime.js +367 -61
- package/dist/ai-service/builder-runtime.js.map +1 -1
- package/dist/ai-service/builder-session-store.d.ts +1 -7
- package/dist/ai-service/builder-session-store.d.ts.map +1 -1
- package/dist/ai-service/builder-session-store.js +1 -1
- package/dist/ai-service/builder-session-store.js.map +1 -1
- package/dist/ai-service/builder-tool-callbacks.d.ts +5 -0
- package/dist/ai-service/builder-tool-callbacks.d.ts.map +1 -1
- package/dist/ai-service/builder-tool-callbacks.js +5 -2
- package/dist/ai-service/builder-tool-callbacks.js.map +1 -1
- package/dist/ai-service/builder-tools.d.ts +26 -4
- package/dist/ai-service/builder-tools.d.ts.map +1 -1
- package/dist/ai-service/builder-tools.js +156 -29
- package/dist/ai-service/builder-tools.js.map +1 -1
- package/dist/ai-service/builder-workspace.d.ts +1 -0
- package/dist/ai-service/builder-workspace.d.ts.map +1 -1
- package/dist/ai-service/builder-workspace.js +15 -6
- package/dist/ai-service/builder-workspace.js.map +1 -1
- package/dist/ai-service/chat/chat-session-store.d.ts +2 -2
- package/dist/ai-service/chat/chat-session-store.d.ts.map +1 -1
- package/dist/ai-service/chat/chat-session-store.js +25 -8
- package/dist/ai-service/chat/chat-session-store.js.map +1 -1
- package/dist/ai-service/demo-agents/load-agents.d.ts +5 -2
- package/dist/ai-service/demo-agents/load-agents.d.ts.map +1 -1
- package/dist/ai-service/demo-agents/load-agents.js +43 -49
- package/dist/ai-service/demo-agents/load-agents.js.map +1 -1
- package/dist/ai-service/demo-agents/run-agent.js +2 -2
- package/dist/ai-service/demo-agents/run-agent.js.map +1 -1
- package/dist/ai-service/demo-agents/system-prompt.d.ts +6 -5
- package/dist/ai-service/demo-agents/system-prompt.d.ts.map +1 -1
- package/dist/ai-service/demo-agents/system-prompt.js +18 -26
- package/dist/ai-service/demo-agents/system-prompt.js.map +1 -1
- package/dist/ai-service/demo-agents/types.d.ts +1 -56
- package/dist/ai-service/demo-agents/types.d.ts.map +1 -1
- package/dist/ai-service/demo-agents/types.js +0 -7
- package/dist/ai-service/demo-agents/types.js.map +1 -1
- package/dist/ai-service/dev-database-file-sync.d.ts +2 -1
- package/dist/ai-service/dev-database-file-sync.d.ts.map +1 -1
- package/dist/ai-service/dev-database-file-sync.js.map +1 -1
- package/dist/ai-service/features.d.ts +20 -8
- package/dist/ai-service/features.d.ts.map +1 -1
- package/dist/ai-service/index.d.ts +100 -7
- package/dist/ai-service/index.d.ts.map +1 -1
- package/dist/ai-service/index.js +549 -108
- package/dist/ai-service/index.js.map +1 -1
- package/dist/ai-service/judge/tools/playwright-action.d.ts +5 -5
- package/dist/ai-service/judge/tools/playwright-action.d.ts.map +1 -1
- package/dist/ai-service/judge/tools/playwright-action.js +4 -1
- package/dist/ai-service/judge/tools/playwright-action.js.map +1 -1
- package/dist/ai-service/llm/context-v2/storage/event-types.d.ts +45 -110
- package/dist/ai-service/llm/context-v2/storage/event-types.d.ts.map +1 -1
- package/dist/ai-service/llm/context-v2/storage/event-types.js +3 -3
- package/dist/ai-service/llm/context-v2/storage/event-types.js.map +1 -1
- package/dist/ai-service/mcp/adapter/json-schema-to-zod.d.ts.map +1 -1
- package/dist/ai-service/mcp/adapter/json-schema-to-zod.js.map +1 -1
- package/dist/ai-service/mcp/embedded-playwright-mcp-server.d.ts +2 -0
- package/dist/ai-service/mcp/embedded-playwright-mcp-server.d.ts.map +1 -1
- package/dist/ai-service/mcp/embedded-playwright-mcp-server.js +28 -0
- package/dist/ai-service/mcp/embedded-playwright-mcp-server.js.map +1 -1
- package/dist/ai-service/mcp/playwright-server.d.ts +2 -0
- package/dist/ai-service/mcp/playwright-server.d.ts.map +1 -1
- package/dist/ai-service/mcp/playwright-server.js +1 -0
- package/dist/ai-service/mcp/playwright-server.js.map +1 -1
- package/dist/ai-service/migration-prompt-gating.d.ts +2 -0
- package/dist/ai-service/migration-prompt-gating.d.ts.map +1 -1
- package/dist/ai-service/migration-prompt-gating.js +3 -0
- package/dist/ai-service/migration-prompt-gating.js.map +1 -1
- package/dist/ai-service/prompt-builder-service/types.d.ts +3 -1
- package/dist/ai-service/prompt-builder-service/types.d.ts.map +1 -1
- package/dist/ai-service/security/vulnerability-triage.d.ts +12 -30
- package/dist/ai-service/security/vulnerability-triage.d.ts.map +1 -1
- package/dist/ai-service/skills/system/_registry.generated.d.ts.map +1 -1
- package/dist/ai-service/skills/system/_registry.generated.js +6 -0
- package/dist/ai-service/skills/system/_registry.generated.js.map +1 -1
- package/dist/ai-service/skills/system/project-agents/references/errors.generated.d.ts +2 -0
- package/dist/ai-service/skills/system/project-agents/references/errors.generated.d.ts.map +1 -0
- package/dist/ai-service/skills/system/project-agents/references/errors.generated.js +25 -0
- package/dist/ai-service/skills/system/project-agents/references/errors.generated.js.map +1 -0
- package/dist/ai-service/skills/system/project-agents/references/fields.generated.d.ts +2 -0
- package/dist/ai-service/skills/system/project-agents/references/fields.generated.d.ts.map +1 -0
- package/dist/ai-service/skills/system/project-agents/references/fields.generated.js +55 -0
- package/dist/ai-service/skills/system/project-agents/references/fields.generated.js.map +1 -0
- package/dist/ai-service/skills/system/project-agents/skill.generated.d.ts +2 -0
- package/dist/ai-service/skills/system/project-agents/skill.generated.d.ts.map +1 -0
- package/dist/ai-service/skills/system/project-agents/skill.generated.js +73 -0
- package/dist/ai-service/skills/system/project-agents/skill.generated.js.map +1 -0
- package/dist/ai-service/state-machine/handlers/llm-generating.d.ts.map +1 -1
- package/dist/ai-service/state-machine/handlers/llm-generating.js +10 -7
- package/dist/ai-service/state-machine/handlers/llm-generating.js.map +1 -1
- package/dist/ai-service/state-machine/helpers/stable-peer.d.ts +2 -0
- package/dist/ai-service/state-machine/helpers/stable-peer.d.ts.map +1 -1
- package/dist/ai-service/state-machine/helpers/stable-peer.js +9 -2
- package/dist/ai-service/state-machine/helpers/stable-peer.js.map +1 -1
- package/dist/ai-service/tasks/task-store.d.ts.map +1 -1
- package/dist/ai-service/tasks/task-store.js +1 -1
- package/dist/ai-service/tasks/task-store.js.map +1 -1
- package/dist/ai-service/tasks/types.d.ts +5 -1
- package/dist/ai-service/tasks/types.d.ts.map +1 -1
- package/dist/ai-service/util/inference-jwt.d.ts +5 -0
- package/dist/ai-service/util/inference-jwt.d.ts.map +1 -1
- package/dist/ai-service/util/inference-jwt.js +17 -0
- package/dist/ai-service/util/inference-jwt.js.map +1 -1
- package/dist/ai-service/workspace-mirror.d.ts +2 -0
- package/dist/ai-service/workspace-mirror.d.ts.map +1 -1
- package/dist/ai-service/workspace-mirror.js +21 -0
- package/dist/ai-service/workspace-mirror.js.map +1 -1
- package/dist/ai-service/workspace-package-registry.d.ts +3 -0
- package/dist/ai-service/workspace-package-registry.d.ts.map +1 -0
- package/dist/ai-service/workspace-package-registry.js +87 -0
- package/dist/ai-service/workspace-package-registry.js.map +1 -0
- package/dist/ai-service/workspace-package-state.d.ts +12 -0
- package/dist/ai-service/workspace-package-state.d.ts.map +1 -0
- package/dist/ai-service/workspace-package-state.js +47 -0
- package/dist/ai-service/workspace-package-state.js.map +1 -0
- package/dist/ai-service/workspace-packages.d.ts +234 -0
- package/dist/ai-service/workspace-packages.d.ts.map +1 -0
- package/dist/ai-service/workspace-packages.js +466 -0
- package/dist/ai-service/workspace-packages.js.map +1 -0
- package/dist/dev-server-ws-smoke-client.d.ts.map +1 -1
- package/dist/dev-server-ws-smoke-client.js +1 -0
- package/dist/dev-server-ws-smoke-client.js.map +1 -1
- package/dist/file-sync-vite-plugin.d.ts +3 -1
- package/dist/file-sync-vite-plugin.d.ts.map +1 -1
- package/dist/file-sync-vite-plugin.js +19 -11
- package/dist/file-sync-vite-plugin.js.map +1 -1
- package/dist/file-system-manager.d.ts.map +1 -1
- package/dist/file-system-manager.js +11 -13
- package/dist/file-system-manager.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/lock-service/index.d.ts +36 -0
- package/dist/lock-service/index.d.ts.map +1 -1
- package/dist/lock-service/index.js +59 -0
- package/dist/lock-service/index.js.map +1 -1
- package/dist/plugin-options.d.ts +9 -1
- package/dist/plugin-options.d.ts.map +1 -1
- package/dist/plugin-options.js.map +1 -1
- package/dist/policy-gate-runner.d.ts +1 -9
- package/dist/policy-gate-runner.d.ts.map +1 -1
- package/dist/project-agents.d.ts +31 -0
- package/dist/project-agents.d.ts.map +1 -0
- package/dist/project-agents.js +50 -0
- package/dist/project-agents.js.map +1 -0
- package/dist/project-manifest.d.ts +8 -108
- package/dist/project-manifest.d.ts.map +1 -1
- package/dist/project-manifest.js +28 -115
- package/dist/project-manifest.js.map +1 -1
- package/dist/server-rpc/index.d.ts +6 -0
- package/dist/server-rpc/index.d.ts.map +1 -1
- package/dist/server-rpc/index.js +13 -1
- package/dist/server-rpc/index.js.map +1 -1
- package/dist/socket-manager.d.ts +3 -1
- package/dist/socket-manager.d.ts.map +1 -1
- package/dist/socket-manager.js +115 -65
- package/dist/socket-manager.js.map +1 -1
- package/dist/sync-service/draft-mutation.d.ts +6 -2
- package/dist/sync-service/draft-mutation.d.ts.map +1 -1
- package/dist/sync-service/draft-mutation.js +20 -33
- package/dist/sync-service/draft-mutation.js.map +1 -1
- package/dist/sync-service/index.d.ts +6 -6
- package/dist/sync-service/index.d.ts.map +1 -1
- package/dist/sync-service/index.js +29 -61
- package/dist/sync-service/index.js.map +1 -1
- package/dist/workspace-sync-methods.d.ts +5 -20
- package/dist/workspace-sync-methods.d.ts.map +1 -1
- package/package.json +17 -13
- package/dist/sync-service/native-draft-session.d.ts +0 -3
- package/dist/sync-service/native-draft-session.d.ts.map +0 -1
- package/dist/sync-service/native-draft-session.js +0 -4
- package/dist/sync-service/native-draft-session.js.map +0 -1
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
/** Runs native builder turns and coordinates their editor callbacks and recovery. */
|
|
1
2
|
import { randomBytes } from "node:crypto";
|
|
2
|
-
import { context, trace } from "@opentelemetry/api";
|
|
3
|
+
import { context, metrics, trace } from "@opentelemetry/api";
|
|
3
4
|
import { asSchema, tool } from "ai";
|
|
4
5
|
import { z } from "zod";
|
|
6
|
+
import { NATIVE_BUILDER_PROVIDERS, } from "@superblocksteam/library-shared/types";
|
|
5
7
|
import { NativeBuilderModel, } from "@superblocksteam/shared";
|
|
6
8
|
import { BUILDER_RUNTIME_ENDPOINT_HEADER, BUILDER_RUNTIME_INVOCATIONS_PATH, BUILDER_RUNTIME_SESSION_HEADER, BUILDER_RUNTIME_SIGN_GRANT_HEADER, BUILDER_RUNTIME_SIGN_PATH, WORKSPACE_INGRESS_PORT, builderRuntimeEndpointHeaderSchema, deriveWorkspaceEndpointToken, deriveWorkspaceToken, workspaceEndpointSchema, } from "@superblocksteam/workspace-sync/control";
|
|
7
|
-
import { getErrorMeta } from "../util/logger.js";
|
|
9
|
+
import { getErrorMeta, getLogger } from "../util/logger.js";
|
|
8
10
|
import { getToolOutput } from "./agent/tool-message-utils.js";
|
|
9
11
|
import { movesEditor } from "./agent/tools/branching/branch-tools.js";
|
|
10
12
|
import { BuilderFailureDrain, } from "./builder-failure-drain.js";
|
|
@@ -18,6 +20,76 @@ const INFERENCE_AUTH_CALLS = new Set([
|
|
|
18
20
|
"__runtime_redact",
|
|
19
21
|
"__runtime_inference_token",
|
|
20
22
|
]);
|
|
23
|
+
/** Runtime control calls, which never wait on a stream event. */
|
|
24
|
+
const RUNTIME_CONTROL_CALLS = new Set([
|
|
25
|
+
"__runtime_redact",
|
|
26
|
+
"__runtime_skills_token",
|
|
27
|
+
"__runtime_inference_token",
|
|
28
|
+
]);
|
|
29
|
+
/** Error codes with which a runtime declines a `turn.attach`. */
|
|
30
|
+
const ATTACH_REFUSALS = new Set([
|
|
31
|
+
"RUN_NOT_FOUND",
|
|
32
|
+
"RUN_ENDED",
|
|
33
|
+
"ATTACH_GAP",
|
|
34
|
+
"UNSUPPORTED_INVOCATION",
|
|
35
|
+
]);
|
|
36
|
+
const MAX_REATTACH_DELAY_MS = 4_000;
|
|
37
|
+
class AttachRefusedError extends Error {
|
|
38
|
+
code;
|
|
39
|
+
constructor(code) {
|
|
40
|
+
super(`The builder runtime refused to reattach: ${code}`);
|
|
41
|
+
this.code = code;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
class AttachTimeoutError extends Error {
|
|
45
|
+
constructor() {
|
|
46
|
+
super("Reattaching to the builder runtime timed out");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
class StreamEndedError extends Error {
|
|
50
|
+
constructor() {
|
|
51
|
+
super("Native builder stream ended without a finish event");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** Whether `error` means the connection carrying a runtime stream was lost. */
|
|
55
|
+
function isTransportLoss(error) {
|
|
56
|
+
let current = error;
|
|
57
|
+
for (let depth = 0; current instanceof Error && depth < 5; depth++) {
|
|
58
|
+
const code = "code" in current ? current.code : undefined;
|
|
59
|
+
if (
|
|
60
|
+
// A proxy or a draining server can end the body cleanly mid-turn.
|
|
61
|
+
current instanceof StreamEndedError ||
|
|
62
|
+
current.message === "terminated" ||
|
|
63
|
+
current.message === "fetch failed" ||
|
|
64
|
+
current.message.includes("other side closed") ||
|
|
65
|
+
code === "UND_ERR_SOCKET" ||
|
|
66
|
+
code === "ECONNRESET" ||
|
|
67
|
+
code === "ECONNREFUSED")
|
|
68
|
+
return true;
|
|
69
|
+
current = current.cause;
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
function parseSeq(value) {
|
|
74
|
+
if (!/^[1-9][0-9]{0,15}$/.test(value))
|
|
75
|
+
return undefined;
|
|
76
|
+
const seq = Number(value);
|
|
77
|
+
return Number.isSafeInteger(seq) ? seq : undefined;
|
|
78
|
+
}
|
|
79
|
+
function delay(ms, signal) {
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
signal.throwIfAborted();
|
|
82
|
+
const onAbort = () => {
|
|
83
|
+
clearTimeout(timer);
|
|
84
|
+
reject(signal.reason);
|
|
85
|
+
};
|
|
86
|
+
const timer = setTimeout(() => {
|
|
87
|
+
signal.removeEventListener("abort", onAbort);
|
|
88
|
+
resolve();
|
|
89
|
+
}, ms);
|
|
90
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
91
|
+
});
|
|
92
|
+
}
|
|
21
93
|
const executionDescriptorSchema = z
|
|
22
94
|
.object({
|
|
23
95
|
token: z.string().min(32).max(4096),
|
|
@@ -67,6 +139,8 @@ const workspaceFinishSchema = z.object({
|
|
|
67
139
|
dbfsHash: z.string().min(1).optional(),
|
|
68
140
|
rehydrate: z.enum(["fresh", "resumed", "replayed", "reloaded"]).optional(),
|
|
69
141
|
filesChanged: z.boolean().optional(),
|
|
142
|
+
/** The AI checkpoint the VM's turn end created; absent when it created none. */
|
|
143
|
+
commitId: z.string().min(1).optional(),
|
|
70
144
|
/**
|
|
71
145
|
* Per-VM-boot credential the AI process presents as an ai-service peer on
|
|
72
146
|
* the VM's `/sb-dev-updates`; `workspace.open` returns it.
|
|
@@ -93,7 +167,7 @@ const runtimeFailureReasonSchema = z.enum([
|
|
|
93
167
|
]);
|
|
94
168
|
const eventSchema = z.discriminatedUnion("type", [
|
|
95
169
|
z.object({ type: z.literal("session"), sessionId: z.string().min(1) }),
|
|
96
|
-
z.object({ type: z.literal("callback-ready"), id: z.
|
|
170
|
+
z.object({ type: z.literal("callback-ready"), id: z.guid() }),
|
|
97
171
|
z.object({ type: z.literal("text-delta"), text: z.string() }),
|
|
98
172
|
z.object({
|
|
99
173
|
type: z.literal("plan"),
|
|
@@ -104,7 +178,7 @@ const eventSchema = z.discriminatedUnion("type", [
|
|
|
104
178
|
type: z.literal("tool-call"),
|
|
105
179
|
toolCallId: z.string(),
|
|
106
180
|
toolName: z.string(),
|
|
107
|
-
input: z.unknown(),
|
|
181
|
+
input: z.unknown().optional(),
|
|
108
182
|
// The sub-agent call this call was made inside. Lenient: grouping is a
|
|
109
183
|
// convenience and must not fail the stream parse.
|
|
110
184
|
parentToolCallId: z.string().optional().catch(undefined),
|
|
@@ -113,7 +187,7 @@ const eventSchema = z.discriminatedUnion("type", [
|
|
|
113
187
|
type: z.literal("tool-result"),
|
|
114
188
|
toolCallId: z.string(),
|
|
115
189
|
toolName: z.string(),
|
|
116
|
-
output: z.unknown(),
|
|
190
|
+
output: z.unknown().optional(),
|
|
117
191
|
isError: z.boolean().optional(),
|
|
118
192
|
}),
|
|
119
193
|
// Workspace turn-end data rides on `finish` rather than a new event type:
|
|
@@ -122,6 +196,8 @@ const eventSchema = z.discriminatedUnion("type", [
|
|
|
122
196
|
type: z.literal("finish"),
|
|
123
197
|
usage: usageSchema,
|
|
124
198
|
workspace: workspaceFinishSchema.optional(),
|
|
199
|
+
/** On `turn.cancel`, whether a running turn was stopped. */
|
|
200
|
+
cancelled: z.boolean().optional(),
|
|
125
201
|
}),
|
|
126
202
|
z.object({
|
|
127
203
|
type: z.literal("error"),
|
|
@@ -166,7 +242,7 @@ export class BuilderRuntimeError extends Error {
|
|
|
166
242
|
return new BuilderRuntimeError(this.code, this.reason, this.beforeTurn, this.runSubmitted, false, outcome);
|
|
167
243
|
}
|
|
168
244
|
/** Logs the safe failure classification and its invocation-attempt context. */
|
|
169
|
-
static log(error, logger, attempt) {
|
|
245
|
+
static log(error, logger, attempt, context) {
|
|
170
246
|
const code = error instanceof BuilderRuntimeError
|
|
171
247
|
? z
|
|
172
248
|
.enum([
|
|
@@ -185,6 +261,7 @@ export class BuilderRuntimeError extends Error {
|
|
|
185
261
|
? runtimeFailureReasonSchema.catch("unknown").parse(error.reason)
|
|
186
262
|
: undefined;
|
|
187
263
|
logger.warn("Native builder operation failed", {
|
|
264
|
+
...context,
|
|
188
265
|
...getErrorMeta(error),
|
|
189
266
|
...(code && { "failure.code": code.success ? code.data : "unknown" }),
|
|
190
267
|
...(reason && { "failure.reason": reason }),
|
|
@@ -263,6 +340,21 @@ export class BuilderRuntime {
|
|
|
263
340
|
});
|
|
264
341
|
}
|
|
265
342
|
toolHost = new AgentCoreToolHost();
|
|
343
|
+
/**
|
|
344
|
+
* How long a cancelled workspace turn's stream stays open for the finish
|
|
345
|
+
* naming its checkpoint before it is closed as for any other turn.
|
|
346
|
+
*/
|
|
347
|
+
cancelFinishTimeoutMs = 30_000;
|
|
348
|
+
reattachOutcomes = metrics
|
|
349
|
+
.getMeter("superblocks.ai_service")
|
|
350
|
+
.createCounter("superblocks.ai_service.builder_reattach", {
|
|
351
|
+
description: "Native builder turns whose lost stream was reattached or given up",
|
|
352
|
+
unit: "1",
|
|
353
|
+
});
|
|
354
|
+
/** How long after a turn's stream is lost it may be reattached. */
|
|
355
|
+
reattachBudgetMs = 15_000;
|
|
356
|
+
/** The first pause before reattaching; it doubles per attempt. */
|
|
357
|
+
reattachDelayMs = 250;
|
|
266
358
|
secrets = new NativeSecretRedactor();
|
|
267
359
|
get model() {
|
|
268
360
|
return this.selectedModel ?? NativeBuilderModel.resolve(this.harness);
|
|
@@ -381,8 +473,9 @@ export class BuilderRuntime {
|
|
|
381
473
|
}),
|
|
382
474
|
});
|
|
383
475
|
}
|
|
476
|
+
/** Resolves provider routing from the shared native harness contract. */
|
|
384
477
|
providerFor(_model) {
|
|
385
|
-
return this.harness
|
|
478
|
+
return NATIVE_BUILDER_PROVIDERS[this.harness];
|
|
386
479
|
}
|
|
387
480
|
async invoke(options) {
|
|
388
481
|
if (Object.keys(options.tools).some((name) => name.startsWith("__runtime_")))
|
|
@@ -420,10 +513,9 @@ export class BuilderRuntime {
|
|
|
420
513
|
runId: options.runId,
|
|
421
514
|
concurrentTools: new Set([
|
|
422
515
|
...(options.concurrentTools ?? []),
|
|
423
|
-
|
|
424
|
-
"__runtime_skills_token",
|
|
425
|
-
"__runtime_inference_token",
|
|
516
|
+
...RUNTIME_CONTROL_CALLS,
|
|
426
517
|
]),
|
|
518
|
+
unorderedTools: RUNTIME_CONTROL_CALLS,
|
|
427
519
|
tools: {
|
|
428
520
|
...options.tools,
|
|
429
521
|
...options.callbackTools,
|
|
@@ -530,7 +622,21 @@ export class BuilderRuntime {
|
|
|
530
622
|
});
|
|
531
623
|
let mustRevoke = false;
|
|
532
624
|
let failure;
|
|
533
|
-
|
|
625
|
+
// The runtime's stream. A cancel closes it, except that a workspace
|
|
626
|
+
// turn's end commits its tree even when cancelled and its finish names
|
|
627
|
+
// that checkpoint: once its stream is open, a cancel stops the turn with
|
|
628
|
+
// `turn.cancel` and keeps reading. Closing the stream is the fallback
|
|
629
|
+
// when that call fails or the finish does not come in time.
|
|
630
|
+
const stream = new AbortController();
|
|
631
|
+
let streaming = false;
|
|
632
|
+
options.signal.addEventListener("abort", () => {
|
|
633
|
+
const close = () => stream.abort(options.signal.reason);
|
|
634
|
+
if (!options.workspaceLocal || !streaming)
|
|
635
|
+
return close();
|
|
636
|
+
const timer = setTimeout(close, this.cancelFinishTimeoutMs);
|
|
637
|
+
stream.signal.addEventListener("abort", () => clearTimeout(timer));
|
|
638
|
+
void this.cancelTurn(options, stream.signal).then((cancelled) => cancelled || close());
|
|
639
|
+
}, { once: true, signal: stream.signal });
|
|
534
640
|
try {
|
|
535
641
|
options.signal.throwIfAborted();
|
|
536
642
|
mustRevoke = !!execution;
|
|
@@ -576,31 +682,42 @@ export class BuilderRuntime {
|
|
|
576
682
|
payload,
|
|
577
683
|
session: options.workspaceSessionKey ? "workspace" : "chat",
|
|
578
684
|
access: options.access,
|
|
579
|
-
signal:
|
|
685
|
+
signal: stream.signal,
|
|
580
686
|
});
|
|
581
|
-
|
|
582
|
-
await this.
|
|
687
|
+
streaming = true;
|
|
688
|
+
await this.followTurn(body, stream.signal, {
|
|
583
689
|
...options,
|
|
584
690
|
onEvent: (event) => {
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
691
|
+
// After an inference or Skills auth failure, drop events but still
|
|
692
|
+
// deliver the finish naming the checkpoint the workspace turn
|
|
693
|
+
// committed.
|
|
694
|
+
if ((inferenceFailure || skillsFailure) &&
|
|
695
|
+
!(event.type === "finish" && event.workspace))
|
|
696
|
+
return;
|
|
589
697
|
return options.onEvent(event);
|
|
590
698
|
},
|
|
591
|
-
}, callbacks, callbackResults
|
|
699
|
+
}, callbacks, callbackResults,
|
|
700
|
+
// A workspace turn still owes the finish naming its committed checkpoint.
|
|
701
|
+
() => !!options.workspaceSessionKey ||
|
|
702
|
+
(!inferenceFailure && !skillsFailure));
|
|
592
703
|
if (inferenceFailure)
|
|
593
704
|
throw inferenceFailure;
|
|
594
705
|
if (skillsFailure)
|
|
595
706
|
throw skillsFailure;
|
|
707
|
+
options.signal.throwIfAborted();
|
|
596
708
|
}
|
|
597
709
|
catch (error) {
|
|
598
710
|
failure = { error: inferenceFailure ?? skillsFailure ?? error };
|
|
599
711
|
}
|
|
600
712
|
finally {
|
|
601
|
-
//
|
|
602
|
-
|
|
603
|
-
|
|
713
|
+
// Ends the cancel's fallback timer and any cancel still in flight.
|
|
714
|
+
stream.abort();
|
|
715
|
+
// A cancelled turn whose stream closed without its own `turn.cancel`
|
|
716
|
+
// still runs in the VM, since AgentCore keeps an invocation going after
|
|
717
|
+
// its caller hangs up. Before the tools close, so the VM stops instead
|
|
718
|
+
// of failing its next callback.
|
|
719
|
+
if (streaming && options.signal.aborted && !options.workspaceLocal)
|
|
720
|
+
await this.cancelTurn(options, AbortSignal.timeout(5_000), false);
|
|
604
721
|
closeTools();
|
|
605
722
|
callbackResults.clear();
|
|
606
723
|
}
|
|
@@ -615,30 +732,6 @@ export class BuilderRuntime {
|
|
|
615
732
|
if (failure)
|
|
616
733
|
throw failure.error;
|
|
617
734
|
}
|
|
618
|
-
/**
|
|
619
|
-
* Tells the VM to stop run `runId`. AgentCore keeps an invocation running
|
|
620
|
-
* after its caller hangs up, so aborting our stream alone leaves the turn
|
|
621
|
-
* going. Best effort: a runtime without `turn.cancel` answers 422.
|
|
622
|
-
*/
|
|
623
|
-
async cancelTurn(options) {
|
|
624
|
-
try {
|
|
625
|
-
const body = await this.invokeRaw({
|
|
626
|
-
payload: JSON.stringify({
|
|
627
|
-
kind: "turn.cancel",
|
|
628
|
-
provider: this.providerFor(this.model),
|
|
629
|
-
runId: options.runId,
|
|
630
|
-
actorId: options.actorId,
|
|
631
|
-
}),
|
|
632
|
-
session: options.workspaceSessionKey ? "workspace" : "chat",
|
|
633
|
-
access: options.access,
|
|
634
|
-
signal: AbortSignal.timeout(5_000),
|
|
635
|
-
});
|
|
636
|
-
await body[Symbol.asyncIterator]().return?.();
|
|
637
|
-
}
|
|
638
|
-
catch {
|
|
639
|
-
// The turn still ends when the VM's next callback finds the run closed.
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
735
|
/**
|
|
643
736
|
* The workspace edge's base URL (no path) and the token the VM presents to
|
|
644
737
|
* it and accepts from it, for one workspace session. The edge is the
|
|
@@ -779,6 +872,14 @@ export class BuilderRuntime {
|
|
|
779
872
|
* Throws when the stream ends without one or a frame is too large.
|
|
780
873
|
*/
|
|
781
874
|
static async *readEvents(body, signal) {
|
|
875
|
+
for await (const { event } of BuilderRuntime.readFrames(body, signal))
|
|
876
|
+
yield event;
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
* {@link readEvents} with each frame's `id:` sequence number. A frame
|
|
880
|
+
* without a valid id has none.
|
|
881
|
+
*/
|
|
882
|
+
static async *readFrames(body, signal) {
|
|
782
883
|
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
783
884
|
let buffer = "";
|
|
784
885
|
const iterator = body[Symbol.asyncIterator]();
|
|
@@ -801,47 +902,252 @@ export class BuilderRuntime {
|
|
|
801
902
|
throw new Error("Native builder event exceeds size limit");
|
|
802
903
|
const frame = buffer.slice(0, boundary.index);
|
|
803
904
|
buffer = buffer.slice(boundary.index + boundary[0].length);
|
|
804
|
-
const
|
|
805
|
-
|
|
905
|
+
const lines = frame.split(/\r?\n/);
|
|
906
|
+
const data = lines
|
|
806
907
|
.filter((line) => line.startsWith("data:"))
|
|
807
908
|
.map((line) => line.slice(5).trimStart())
|
|
808
909
|
.join("\n");
|
|
809
910
|
if (!data)
|
|
810
911
|
continue;
|
|
811
912
|
const event = eventSchema.parse(JSON.parse(data));
|
|
812
|
-
|
|
913
|
+
const id = lines.findLast((line) => line.startsWith("id:"));
|
|
914
|
+
const seq = id === undefined ? undefined : parseSeq(id.slice(3).trim());
|
|
915
|
+
yield seq === undefined ? { event } : { event, seq };
|
|
813
916
|
if (event.type === "finish")
|
|
814
917
|
return;
|
|
815
918
|
}
|
|
816
919
|
if (buffer.length > 8_388_608)
|
|
817
920
|
throw new Error("Native builder event exceeds size limit");
|
|
818
921
|
}
|
|
819
|
-
throw new
|
|
922
|
+
throw new StreamEndedError();
|
|
820
923
|
}
|
|
821
924
|
finally {
|
|
822
925
|
signal.removeEventListener("abort", onAbort);
|
|
823
926
|
void iterator.return?.().catch(() => { });
|
|
824
927
|
}
|
|
825
928
|
}
|
|
826
|
-
|
|
929
|
+
/**
|
|
930
|
+
* Asks the runtime to stop the turn `options.runId` without closing its
|
|
931
|
+
* stream. AgentCore keeps an invocation running after its caller hangs up,
|
|
932
|
+
* so closing the stream alone leaves the turn going. False when it could
|
|
933
|
+
* not: an older runtime (which answers 422), or no such turn running.
|
|
934
|
+
* Without `awaitReply` it only sends the cancel and reports true.
|
|
935
|
+
*/
|
|
936
|
+
async cancelTurn(options, signal, awaitReply = true) {
|
|
937
|
+
try {
|
|
938
|
+
const body = await this.invokeRaw({
|
|
939
|
+
payload: JSON.stringify({
|
|
940
|
+
kind: "turn.cancel",
|
|
941
|
+
provider: this.providerFor(this.model),
|
|
942
|
+
runId: options.runId,
|
|
943
|
+
actorId: options.actorId,
|
|
944
|
+
}),
|
|
945
|
+
session: options.workspaceSessionKey ? "workspace" : "chat",
|
|
946
|
+
access: options.access,
|
|
947
|
+
signal,
|
|
948
|
+
});
|
|
949
|
+
if (!awaitReply) {
|
|
950
|
+
await body[Symbol.asyncIterator]().return?.();
|
|
951
|
+
return true;
|
|
952
|
+
}
|
|
953
|
+
for await (const event of BuilderRuntime.readEvents(body, signal))
|
|
954
|
+
if (event.type === "finish")
|
|
955
|
+
return event.cancelled === true;
|
|
956
|
+
return false;
|
|
957
|
+
}
|
|
958
|
+
catch {
|
|
959
|
+
return false;
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* Consumes a turn's stream. When the connection carrying it is lost, the
|
|
964
|
+
* turn is still running in the VM, so this reattaches with `turn.attach`
|
|
965
|
+
* and resumes after the last event it delivered. Each loss may be
|
|
966
|
+
* reattached for {@link reattachBudgetMs}; an attach stream that delivered
|
|
967
|
+
* new events or stayed open that long counts as reconnected, so a later
|
|
968
|
+
* loss gets a new budget. A refused or exhausted reattach fails with the
|
|
969
|
+
* first transport error.
|
|
970
|
+
*/
|
|
971
|
+
async followTurn(first, signal, options, callbacks, callbackResults, canReattach) {
|
|
972
|
+
const cursor = { seq: 0 };
|
|
973
|
+
let body = first;
|
|
974
|
+
let lost;
|
|
975
|
+
let pause = this.reattachDelayMs;
|
|
976
|
+
let attempts = 0;
|
|
977
|
+
let losses = 0;
|
|
978
|
+
// Once per turn whose stream was lost.
|
|
979
|
+
const settle = (outcome, cause) => {
|
|
980
|
+
trace.getSpan(options.traceContext ?? context.active())?.setAttributes({
|
|
981
|
+
"builder.reattach.attempts": attempts,
|
|
982
|
+
"builder.reattach.episodes": losses,
|
|
983
|
+
"builder.reattach.outcome": outcome,
|
|
984
|
+
});
|
|
985
|
+
this.reattachOutcomes.add(1, { "reattach.outcome": outcome });
|
|
986
|
+
const attributes = {
|
|
987
|
+
"builder.run_id": options.runId,
|
|
988
|
+
"builder.reattach.after_seq": cursor.seq,
|
|
989
|
+
"builder.reattach.attempts": attempts,
|
|
990
|
+
"builder.reattach.episodes": losses,
|
|
991
|
+
"builder.reattach.outcome": outcome,
|
|
992
|
+
};
|
|
993
|
+
if (outcome === "recovered") {
|
|
994
|
+
getLogger().info("Native builder turn reattached", attributes);
|
|
995
|
+
return;
|
|
996
|
+
}
|
|
997
|
+
const status = BuilderRuntimeHttpError.statusOf(cause);
|
|
998
|
+
getLogger().warn("Native builder turn could not be reattached", {
|
|
999
|
+
...attributes,
|
|
1000
|
+
...(cause !== undefined && getErrorMeta(cause)),
|
|
1001
|
+
...(cause instanceof AttachRefusedError && {
|
|
1002
|
+
"builder.reattach.refusal": cause.code,
|
|
1003
|
+
}),
|
|
1004
|
+
...(status !== undefined && { "builder.reattach.status": status }),
|
|
1005
|
+
});
|
|
1006
|
+
};
|
|
1007
|
+
for (;;) {
|
|
1008
|
+
let opened = false;
|
|
1009
|
+
let openedAt = 0;
|
|
1010
|
+
const seqBefore = cursor.seq;
|
|
1011
|
+
try {
|
|
1012
|
+
if (!body) {
|
|
1013
|
+
if (!lost)
|
|
1014
|
+
throw new Error("Native builder has no stream to read");
|
|
1015
|
+
attempts++;
|
|
1016
|
+
body = await this.attachTurn(options, cursor.seq, signal, lost.until - performance.now());
|
|
1017
|
+
opened = true;
|
|
1018
|
+
openedAt = performance.now();
|
|
1019
|
+
}
|
|
1020
|
+
await this.consume(body, signal, options, callbacks, callbackResults, cursor, lost !== undefined);
|
|
1021
|
+
if (lost)
|
|
1022
|
+
settle("recovered");
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
catch (error) {
|
|
1026
|
+
body = undefined;
|
|
1027
|
+
if (signal.aborted || options.signal.aborted) {
|
|
1028
|
+
if (lost)
|
|
1029
|
+
settle("cancelled", error);
|
|
1030
|
+
throw error;
|
|
1031
|
+
}
|
|
1032
|
+
const transient = isTransportLoss(error) ||
|
|
1033
|
+
error instanceof AttachTimeoutError ||
|
|
1034
|
+
(BuilderRuntimeHttpError.statusOf(error) ?? 0) >= 500;
|
|
1035
|
+
// An attach that drops at once is the same loss, so its retries back off.
|
|
1036
|
+
let newLoss = opened &&
|
|
1037
|
+
(cursor.seq > seqBefore ||
|
|
1038
|
+
performance.now() - openedAt >= this.reattachBudgetMs);
|
|
1039
|
+
if (!lost) {
|
|
1040
|
+
if (!isTransportLoss(error) || !canReattach())
|
|
1041
|
+
throw error;
|
|
1042
|
+
lost = { error, until: 0 };
|
|
1043
|
+
newLoss = true;
|
|
1044
|
+
}
|
|
1045
|
+
else if (error instanceof AttachRefusedError ||
|
|
1046
|
+
(!opened && !transient)) {
|
|
1047
|
+
settle("refused", error);
|
|
1048
|
+
throw lost.error;
|
|
1049
|
+
}
|
|
1050
|
+
else if (!transient) {
|
|
1051
|
+
// The reattached turn itself failed.
|
|
1052
|
+
settle("failed", error);
|
|
1053
|
+
throw error;
|
|
1054
|
+
}
|
|
1055
|
+
if (newLoss) {
|
|
1056
|
+
losses++;
|
|
1057
|
+
lost.until = performance.now() + this.reattachBudgetMs;
|
|
1058
|
+
pause = this.reattachDelayMs;
|
|
1059
|
+
getLogger().warn("Native builder turn stream lost; reattaching", {
|
|
1060
|
+
...getErrorMeta(error),
|
|
1061
|
+
"builder.run_id": options.runId,
|
|
1062
|
+
"builder.reattach.after_seq": cursor.seq,
|
|
1063
|
+
"builder.reattach.episodes": losses,
|
|
1064
|
+
});
|
|
1065
|
+
}
|
|
1066
|
+
if (performance.now() + pause >= lost.until) {
|
|
1067
|
+
settle("exhausted", error);
|
|
1068
|
+
throw lost.error;
|
|
1069
|
+
}
|
|
1070
|
+
try {
|
|
1071
|
+
await delay(pause, AbortSignal.any([signal, options.signal]));
|
|
1072
|
+
}
|
|
1073
|
+
catch (aborted) {
|
|
1074
|
+
settle("cancelled", aborted);
|
|
1075
|
+
throw aborted;
|
|
1076
|
+
}
|
|
1077
|
+
pause = Math.min(pause * 2, MAX_REATTACH_DELAY_MS);
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* Opens a `turn.attach` stream replaying the run's events after `afterSeq`.
|
|
1083
|
+
* `timeoutMs` bounds only the request, not the stream it opens.
|
|
1084
|
+
*/
|
|
1085
|
+
async attachTurn(options, afterSeq, signal, timeoutMs) {
|
|
1086
|
+
const request = new AbortController();
|
|
1087
|
+
const timer = setTimeout(() => request.abort(new AttachTimeoutError()), Math.max(timeoutMs, 0));
|
|
1088
|
+
try {
|
|
1089
|
+
return await this.invokeRaw({
|
|
1090
|
+
payload: JSON.stringify({
|
|
1091
|
+
kind: "turn.attach",
|
|
1092
|
+
provider: this.providerFor(this.model),
|
|
1093
|
+
runId: options.runId,
|
|
1094
|
+
actorId: options.actorId,
|
|
1095
|
+
afterSeq,
|
|
1096
|
+
}),
|
|
1097
|
+
session: options.workspaceSessionKey ? "workspace" : "chat",
|
|
1098
|
+
access: options.access,
|
|
1099
|
+
signal: AbortSignal.any([signal, request.signal]),
|
|
1100
|
+
});
|
|
1101
|
+
}
|
|
1102
|
+
finally {
|
|
1103
|
+
clearTimeout(timer);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
async consume(body, signal, options, callbacks, callbackResults, cursor, attaching) {
|
|
827
1107
|
let failure;
|
|
828
|
-
const cleanup = new BuilderFailureDrain(
|
|
1108
|
+
const cleanup = new BuilderFailureDrain(signal);
|
|
829
1109
|
try {
|
|
830
|
-
for await (const event of BuilderRuntime.
|
|
831
|
-
if (
|
|
1110
|
+
for await (const { event, seq } of BuilderRuntime.readFrames(body, cleanup.signal)) {
|
|
1111
|
+
if (seq !== undefined) {
|
|
1112
|
+
if (seq <= cursor.seq)
|
|
1113
|
+
continue;
|
|
1114
|
+
cursor.seq = seq;
|
|
1115
|
+
}
|
|
1116
|
+
if (attaching &&
|
|
1117
|
+
event.type === "error" &&
|
|
1118
|
+
event.code &&
|
|
1119
|
+
ATTACH_REFUSALS.has(event.code))
|
|
1120
|
+
throw new AttachRefusedError(event.code);
|
|
1121
|
+
if (failure || options.signal.aborted) {
|
|
1122
|
+
// A finish confirms the runtime released its run before another
|
|
1123
|
+
// invocation.
|
|
1124
|
+
if (failure && event.type === "finish")
|
|
1125
|
+
cleanup.finish();
|
|
1126
|
+
// A workspace turn's end commits even when the turn failed or was
|
|
1127
|
+
// cancelled; its finish names that checkpoint.
|
|
1128
|
+
if (event.type === "finish" && event.workspace)
|
|
1129
|
+
await options.onEvent(event);
|
|
1130
|
+
continue;
|
|
1131
|
+
}
|
|
1132
|
+
if (event.type === "error") {
|
|
832
1133
|
failure = new BuilderRuntimeError(event.code ?? "INVOCATION_FAILED", event.reason, event.beforeTurn === true, event.runSubmitted === true);
|
|
833
1134
|
cleanup.start();
|
|
834
|
-
}
|
|
835
|
-
// A finish confirms the runtime released its run before another invocation.
|
|
836
|
-
if (failure && event.type === "finish")
|
|
837
|
-
cleanup.finish();
|
|
838
|
-
if (failure)
|
|
839
1135
|
continue;
|
|
840
|
-
|
|
1136
|
+
}
|
|
1137
|
+
try {
|
|
1138
|
+
await this.forwardEvent(event, options, callbacks, callbackResults);
|
|
1139
|
+
}
|
|
1140
|
+
catch (error) {
|
|
1141
|
+
// A cancel stopped this event; a workspace turn's stream is still
|
|
1142
|
+
// read for its finish, and any other stream is closed.
|
|
1143
|
+
if (!options.signal.aborted)
|
|
1144
|
+
throw error;
|
|
1145
|
+
}
|
|
841
1146
|
}
|
|
842
1147
|
}
|
|
843
1148
|
catch (error) {
|
|
844
|
-
|
|
1149
|
+
// The stream after an error matters only for that finish.
|
|
1150
|
+
if (!failure || signal.aborted)
|
|
845
1151
|
throw error;
|
|
846
1152
|
throw failure.withUnconfirmedCleanup(cleanup.outcome);
|
|
847
1153
|
}
|