pi-web-ui 0.116.0 → 0.118.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/CHANGELOG.md +83 -3
- package/README.md +69 -138
- package/README.zh-CN.md +70 -179
- package/README.zh-TW.md +3 -1
- package/bin/device-hub-cli.mjs +163 -36
- package/bin/frp-manager.mjs +4 -80
- package/bin/hub-upgrade.mjs +561 -0
- package/bin/pi-device-hub.mjs +0 -1
- package/bin/pi-web-ui.mjs +184 -504
- package/deploy/nginx-subpath.conf +0 -33
- package/deploy/pi-web-ui.service +0 -20
- package/dist/server/agent-formatters.js +4 -38
- package/dist/server/agent-service-daemon.js +203 -371
- package/dist/server/agent-service.js +1281 -3030
- package/dist/server/approval-audit.js +2 -23
- package/dist/server/approval-default-rules.js +0 -6
- package/dist/server/approval-rules.js +7 -105
- package/dist/server/ask-user-question-tool.js +1 -12
- package/dist/server/atomic-file.js +33 -5
- package/dist/server/attachment-store.js +0 -36
- package/dist/server/attachments.js +3 -112
- package/dist/server/auth-cookie.js +0 -21
- package/dist/server/bash-tool.js +1 -32
- package/dist/server/bg-servers.js +0 -96
- package/dist/server/browser-page-tool.js +11 -62
- package/dist/server/builtin-modules.js +7 -0
- package/dist/server/canvas/canvas-agent-tool.js +58 -47
- package/dist/server/canvas/canvas-cards.js +0 -14
- package/dist/server/canvas/canvas-dispatch.js +7 -4
- package/dist/server/canvas/canvas-orchestrator.js +167 -181
- package/dist/server/canvas/canvas-runtime-messages.js +30 -0
- package/dist/server/canvas/canvas-scheduled-task.js +0 -3
- package/dist/server/canvas/canvas-scheduler.js +5 -7
- package/dist/server/canvas/canvas-store.js +24 -35
- package/dist/server/canvas/canvas-validation.js +17 -14
- package/dist/server/canvas/canvas-workspace.js +42 -114
- package/dist/server/canvas/pstack-skills-data.js +0 -5
- package/dist/server/claim-files-tool.js +1 -15
- package/dist/server/claim-store.js +5 -49
- package/dist/server/client-id-utils.js +0 -16
- package/dist/server/client-state.js +36 -214
- package/dist/server/communicate-tool.js +0 -3
- package/dist/server/compact-context-tool.js +0 -30
- package/dist/server/compacted-history.js +0 -35
- package/dist/server/compaction-instructions.js +0 -13
- package/dist/server/compaction-markers.js +38 -92
- package/dist/server/compaction-state.js +0 -21
- package/dist/server/composer-drafts.js +0 -34
- package/dist/server/context-budget-runner.js +0 -11
- package/dist/server/context-budget.js +1 -70
- package/dist/server/context-evidence.js +0 -14
- package/dist/server/context-usage.js +0 -12
- package/dist/server/control-socket.js +4 -48
- package/dist/server/conversation-communication.js +0 -10
- package/dist/server/conversation-identity.js +0 -1
- package/dist/server/conversation-read-tool.js +0 -58
- package/dist/server/conversation-tool-names.js +0 -1
- package/dist/server/conversation-tool.js +0 -1
- package/dist/server/conversation-touches.js +4 -75
- package/dist/server/dangling-tools.js +142 -68
- package/dist/server/delegate-mode.js +0 -36
- package/dist/server/delegate-task.js +2 -28
- package/dist/server/device-center/approval-summary.js +0 -1
- package/dist/server/device-center/bot-catalog.js +0 -3
- package/dist/server/device-center/bot-execution.js +48 -9
- package/dist/server/device-center/bot-presets.js +0 -1
- package/dist/server/device-center/bots.js +0 -1
- package/dist/server/device-center/collab-service.js +27 -11
- package/dist/server/device-center/collab-tool.js +393 -27
- package/dist/server/device-center/connector.js +128 -48
- package/dist/server/device-center/delivery-authority.js +95 -0
- package/dist/server/device-center/delivery-subject.js +29 -0
- package/dist/server/device-center/desktop-linux.js +3 -15
- package/dist/server/device-center/desktop-native.js +1 -10
- package/dist/server/device-center/desktop-page.js +2 -3
- package/dist/server/device-center/desktop-setup.js +1 -3
- package/dist/server/device-center/desktop.js +1 -7
- package/dist/server/device-center/device-integration.js +452 -208
- package/dist/server/device-center/device-lan-access.js +403 -0
- package/dist/server/device-center/device-lan.js +484 -0
- package/dist/server/device-center/device-portals.js +0 -3
- package/dist/server/device-center/execution-failure.js +0 -2
- package/dist/server/device-center/execution-guides.js +15 -6
- package/dist/server/device-center/execution-instructions.js +0 -1
- package/dist/server/device-center/execution-settings.js +0 -1
- package/dist/server/device-center/execution-system-prompt.js +0 -3
- package/dist/server/device-center/execution-workspace.js +1 -4
- package/dist/server/device-center/executor.js +46 -31
- package/dist/server/device-center/file-sync-paths.js +0 -5
- package/dist/server/device-center/file-sync.js +0 -10
- package/dist/server/device-center/group-context-delta.js +0 -8
- package/dist/server/device-center/group-context-message.js +0 -14
- package/dist/server/device-center/group-context.js +0 -19
- package/dist/server/device-center/group-events.js +0 -2
- package/dist/server/device-center/group-plan-guard.js +1 -35
- package/dist/server/device-center/group-router-runtime.js +182 -0
- package/dist/server/device-center/group-router-worker.js +178 -0
- package/dist/server/device-center/group-router.js +359 -0
- package/dist/server/device-center/hub-auth.js +24 -2
- package/dist/server/device-center/hub-conversations.js +13 -15
- package/dist/server/device-center/hub-deliveries.js +119 -15
- package/dist/server/device-center/hub-integration.js +647 -334
- package/dist/server/device-center/hub-locks.js +1 -5
- package/dist/server/device-center/hub-mobile.js +147 -0
- package/dist/server/device-center/hub-orchestration.js +400 -0
- package/dist/server/device-center/hub-page.js +28 -9
- package/dist/server/device-center/hub-setup.js +23 -7
- package/dist/server/device-center/hub-transaction-log.js +175 -0
- package/dist/server/device-center/hub-upgrade.js +162 -0
- package/dist/server/device-center/hub-webhooks.js +1 -3
- package/dist/server/device-center/hub.js +946 -404
- package/dist/server/device-center/human-interaction.js +1 -4
- package/dist/server/device-center/integration-errors.js +0 -2
- package/dist/server/device-center/integration-verification.js +111 -0
- package/dist/server/device-center/local-hub.js +6 -4
- package/dist/server/device-center/local-publication.js +416 -0
- package/dist/server/device-center/local-verification-job.js +95 -0
- package/dist/server/device-center/local-verification.js +273 -0
- package/dist/server/device-center/local.js +341 -18
- package/dist/server/device-center/main.js +18 -2
- package/dist/server/device-center/mobile-notification-forwarder.js +292 -0
- package/dist/server/device-center/native-desktop/macos.js +0 -1
- package/dist/server/device-center/native-desktop/windows.js +0 -1
- package/dist/server/device-center/partition-log.js +52 -46
- package/dist/server/device-center/path-portal.js +42 -12
- package/dist/server/device-center/publication-journal.js +305 -0
- package/dist/server/device-center/publish-intent.js +374 -0
- package/dist/server/device-center/relay.js +217 -52
- package/dist/server/device-center/report-upload.js +0 -1
- package/dist/server/device-center/runtime.js +419 -52
- package/dist/server/device-center/store.js +223 -65
- package/dist/server/device-center/task-context-budget.js +0 -3
- package/dist/server/device-center/task-error.js +0 -1
- package/dist/server/device-center/task-journal.js +6 -23
- package/dist/server/device-center/task-outcome.js +68 -0
- package/dist/server/device-center/task-plan.js +0 -1
- package/dist/server/device-center/task-progress.js +0 -16
- package/dist/server/device-center/task-recovery.js +569 -227
- package/dist/server/device-center/task-report-outbox.js +0 -4
- package/dist/server/device-center/task-report.js +2 -10
- package/dist/server/device-center/task-scheduler.js +1 -35
- package/dist/server/device-center/task-settlement.js +335 -0
- package/dist/server/device-center/task-state.js +0 -12
- package/dist/server/device-center/task-workspace.js +393 -0
- package/dist/server/device-center/transfer-stream.js +5 -45
- package/dist/server/device-center/transfers.js +2 -10
- package/dist/server/device-center/verification-limits.js +9 -0
- package/dist/server/device-center/work-contract.js +923 -0
- package/dist/server/device-center/work-items.js +317 -0
- package/dist/server/device-center/workspaces.js +0 -1
- package/dist/server/dispatch-domain-handlers.js +202 -29
- package/dist/server/dispatch-plugin-handlers.js +2 -19
- package/dist/server/dsh/dsh-agent-service.js +78 -601
- package/dist/server/dsh/dsh-client.js +7 -111
- package/dist/server/dsh/dsh-market-service.js +4 -25
- package/dist/server/dsh/dsh-serialize.js +0 -49
- package/dist/server/dsh/dsh-sessions.js +0 -37
- package/dist/server/dsh/dsh-usage.js +0 -37
- package/dist/server/dsh/preset-clones.js +1 -65
- package/dist/server/dsh/runtime/custom-prompt.mjs +0 -14
- package/dist/server/dsh/runtime/goal-rpc.mjs +7 -176
- package/dist/server/dsh/runtime/launcher.mjs +2 -53
- package/dist/server/dsh/runtime/override.patch.yml +0 -29
- package/dist/server/dsh/runtime/runtime-root.mjs +0 -54
- package/dist/server/durable/durable-agent-service.js +5 -23
- package/dist/server/durable/durable-driver.js +12 -73
- package/dist/server/durable/durable-event-projector.js +1 -50
- package/dist/server/durable/durable-loader.js +0 -12
- package/dist/server/durable/durable-models.js +11 -80
- package/dist/server/durable/durable-storage.js +1 -17
- package/dist/server/durable/durable-tools.js +2 -20
- package/dist/server/durable/index.js +0 -3
- package/dist/server/edit-soft-tool.js +2 -81
- package/dist/server/engine-capabilities.js +0 -13
- package/dist/server/engine-catalog.js +74 -0
- package/dist/server/engine-cli.js +89 -0
- package/dist/server/engine-probe.js +35 -217
- package/dist/server/ensure-bash.js +2 -28
- package/dist/server/eval-tool.js +1 -50
- package/dist/server/evidence-tool-hooks.js +0 -2
- package/dist/server/file-archives.js +0 -10
- package/dist/server/file-save.js +1 -12
- package/dist/server/file-transfer-routes.js +0 -11
- package/dist/server/files-service.js +18 -271
- package/dist/server/global-loader.js +9 -89
- package/dist/server/goal-evidence.js +1 -35
- package/dist/server/goal-review-gate.js +0 -58
- package/dist/server/goal-service.js +19 -422
- package/dist/server/hashline-engine.js +8 -112
- package/dist/server/host-guard.js +0 -24
- package/dist/server/host-metrics.js +0 -6
- package/dist/server/http-proxy.js +1 -7
- package/dist/server/i18n.js +5 -72
- package/dist/server/icon-svg.js +0 -15
- package/dist/server/index.js +199 -677
- package/dist/server/json-utils.js +0 -12
- package/dist/server/launch-origin.js +0 -44
- package/dist/server/left-panel-running.js +0 -9
- package/dist/server/load-timing.js +1 -4
- package/dist/server/load-tools-tool.js +0 -20
- package/dist/server/locales.js +1 -46
- package/dist/server/lsp-tool.js +13 -126
- package/dist/server/managed.js +0 -48
- package/dist/server/marker-service.js +1 -22
- package/dist/server/markers/builtins/action.js +0 -19
- package/dist/server/markers/builtins/notify.js +1 -9
- package/dist/server/markers/builtins/rename.js +0 -13
- package/dist/server/markers/index.js +0 -3
- package/dist/server/markers/marker.js +0 -13
- package/dist/server/markers/registry.js +0 -3
- package/dist/server/markers/store.js +1 -12
- package/dist/server/market-service.js +7 -43
- package/dist/server/mcp-agent-tool.js +0 -10
- package/dist/server/mcp-bridge.js +6 -138
- package/dist/server/mcp-hot-reload.js +0 -25
- package/dist/server/mcp-manager.js +2 -44
- package/dist/server/mcp-oauth-login.js +0 -2
- package/dist/server/mcp-oauth-ui.js +1 -4
- package/dist/server/mcp-oauth.js +5 -79
- package/dist/server/mcp-project-runtime.js +0 -4
- package/dist/server/message-deletion.js +0 -2
- package/dist/server/message-entry-resolver.js +0 -8
- package/dist/server/message-window.js +0 -71
- package/dist/server/mobile-notification-events.js +16 -0
- package/dist/server/mobile-origin.js +79 -0
- package/dist/server/mobile-push.js +523 -0
- package/dist/server/model-admin.js +19 -358
- package/dist/server/model-enrich.js +0 -19
- package/dist/server/model-resolver.js +2 -61
- package/dist/server/native-cli-discovery.js +115 -0
- package/dist/server/native-cli-model.js +71 -0
- package/dist/server/native-cli-references.js +214 -0
- package/dist/server/native-cli-remote-cleanup.js +58 -0
- package/dist/server/native-cli-remote-files.js +282 -0
- package/dist/server/native-cli-routes.js +68 -0
- package/dist/server/native-cli-runtime.js +1 -0
- package/dist/server/native-cli-session-hooks.js +170 -0
- package/dist/server/native-cli-session.js +585 -0
- package/dist/server/native-cli-state.js +82 -0
- package/dist/server/native-cli.js +210 -0
- package/dist/server/net-guard.js +9 -71
- package/dist/server/office-parse.js +0 -50
- package/dist/server/orphan-manager.js +0 -18
- package/dist/server/patch-bash-process.js +0 -4
- package/dist/server/patch-executable-lookup.js +0 -77
- package/dist/server/patch-node-pty.js +6 -45
- package/dist/server/patch-remote-catalog.js +4 -34
- package/dist/server/patch-tool.js +0 -15
- package/dist/server/payload-limits.js +0 -5
- package/dist/server/pending-question.js +0 -5
- package/dist/server/permission-preset.js +4 -33
- package/dist/server/persona-chain.js +0 -26
- package/dist/server/pkg-info.js +1 -17
- package/dist/server/plan-manager.js +2 -49
- package/dist/server/plan-mode.js +0 -97
- package/dist/server/plan-update-tool.js +1 -15
- package/dist/server/plugin-api-catalog.js +6 -4
- package/dist/server/plugin-assets.js +1 -35
- package/dist/server/plugin-bundled.js +68 -0
- package/dist/server/plugin-catalog-sync.js +0 -37
- package/dist/server/plugin-catalog.js +13 -67
- package/dist/server/plugin-chat-attachments.js +0 -1
- package/dist/server/plugin-conversation-view.js +0 -20
- package/dist/server/plugin-dom.js +1 -23
- package/dist/server/plugin-facilities.js +4 -115
- package/dist/server/plugin-grants.js +2 -90
- package/dist/server/plugin-install-spec.js +0 -43
- package/dist/server/plugin-installer.js +34 -75
- package/dist/server/plugin-llm.js +4 -38
- package/dist/server/plugin-manifest-validate.js +0 -31
- package/dist/server/plugin-permissions.js +1 -24
- package/dist/server/plugin-project.js +7 -129
- package/dist/server/plugin-removal.js +1 -8
- package/dist/server/plugin-schedule.js +1 -38
- package/dist/server/plugin-secrets.js +3 -16
- package/dist/server/plugin-tool-guard.js +1 -28
- package/dist/server/plugin-ui-slots.js +0 -4
- package/dist/server/plugin-updater.js +15 -90
- package/dist/server/plugins.js +71 -619
- package/dist/server/present-files-tool.js +0 -57
- package/dist/server/preset-fields.js +24 -61
- package/dist/server/preset-plugin-dependencies.js +0 -2
- package/dist/server/preset-share.js +4 -172
- package/dist/server/process-utils.js +4 -80
- package/dist/server/prompt-assembler.js +2 -30
- package/dist/server/prompt-base-store.js +0 -47
- package/dist/server/prompt-composer.js +21 -98
- package/dist/server/prompt-templates.js +0 -2
- package/dist/server/protocol-version.js +1 -11
- package/dist/server/protocol.js +0 -5
- package/dist/server/queue-modes.js +0 -3
- package/dist/server/queue-utils.js +0 -40
- package/dist/server/quick-ask-context.js +384 -0
- package/dist/server/quick-ask-settings.js +61 -0
- package/dist/server/quick-ask.js +260 -0
- package/dist/server/read-tool.js +0 -65
- package/dist/server/reasoning-registry.js +1 -29
- package/dist/server/remote-cli-persistence.js +373 -0
- package/dist/server/remote-native-cli.js +78 -0
- package/dist/server/remote-ssh-service.js +481 -190
- package/dist/server/request-context.js +0 -9
- package/dist/server/resolve-global-sdk.js +55 -31
- package/dist/server/sandbox-data.js +0 -1
- package/dist/server/sandbox-engines.js +0 -1
- package/dist/server/sandbox-runtime-gate.js +0 -3
- package/dist/server/sandbox-service.js +0 -41
- package/dist/server/sandbox-tool.js +0 -20
- package/dist/server/schedule-agent-tool.js +2 -31
- package/dist/server/scheduler-tasks.js +32 -56
- package/dist/server/scm-commitmsg.js +0 -53
- package/dist/server/scm.js +3 -48
- package/dist/server/sdk-builtin-extensions.js +2 -29
- package/dist/server/sdk-origin.js +0 -45
- package/dist/server/serialize.js +2 -92
- package/dist/server/server-config.js +0 -11
- package/dist/server/session-branch.js +1 -87
- package/dist/server/session-list-scan.js +215 -0
- package/dist/server/session-open.js +69 -0
- package/dist/server/session-parser.js +4 -54
- package/dist/server/session-scan.js +81 -45
- package/dist/server/session-search.js +0 -56
- package/dist/server/settings-service.js +19 -126
- package/dist/server/setup-config.js +0 -2
- package/dist/server/setup-page.js +0 -9
- package/dist/server/skill-content-filler.js +0 -17
- package/dist/server/skill-discovery.js +5 -92
- package/dist/server/skill-maintenance.js +1 -28
- package/dist/server/skill-tool.js +0 -30
- package/dist/server/slash-commands.js +5 -50
- package/dist/server/soft-cap.js +3 -40
- package/dist/server/startup-banner.js +0 -8
- package/dist/server/subagent-handoff.js +0 -11
- package/dist/server/subagent-helpers.js +2 -17
- package/dist/server/subagent-notify.js +0 -19
- package/dist/server/subagent-security-gate.js +0 -50
- package/dist/server/subagent-task.js +0 -9
- package/dist/server/subagent-templates-hant.js +0 -10
- package/dist/server/subagent-templates.js +3 -103
- package/dist/server/subagents.js +1 -87
- package/dist/server/system-screenshot-windows.js +259 -0
- package/dist/server/system-screenshot.js +77 -0
- package/dist/server/tabs.js +3 -48
- package/dist/server/terminals.js +559 -586
- package/dist/server/text-quote.js +0 -12
- package/dist/server/text-sniff.js +2 -52
- package/dist/server/themes.js +2 -41
- package/dist/server/tool-approval.js +0 -47
- package/dist/server/tool-guards.js +0 -90
- package/dist/server/tool-host-factories.js +1 -16
- package/dist/server/tool-info.js +0 -13
- package/dist/server/tool-manager.js +1 -229
- package/dist/server/tool-overrides.js +1 -46
- package/dist/server/tool-prompt-overrides.js +1 -30
- package/dist/server/tool-prompts.js +0 -14
- package/dist/server/tool-signature.js +0 -25
- package/dist/server/transcript-prepare.js +55 -0
- package/dist/server/tts-routes.js +58 -0
- package/dist/server/tts-service.js +106 -0
- package/dist/server/tts-system.js +160 -0
- package/dist/server/turn-timing.js +0 -1
- package/dist/server/unauthorized-page.js +0 -8
- package/dist/server/universal/driver-base.js +2 -31
- package/dist/server/universal/drivers/claude-code.js +98 -194
- package/dist/server/universal/drivers/codex.js +12 -35
- package/dist/server/universal/drivers/mock-driver.js +1 -11
- package/dist/server/universal/index.js +0 -3
- package/dist/server/universal/session-store.js +4 -21
- package/dist/server/universal/universal-attachments.js +2 -36
- package/dist/server/universal/universal-service.js +5 -24
- package/dist/server/universal/universal-session.js +30 -148
- package/dist/server/universal/universal-types.js +0 -3
- package/dist/server/update-check.js +6 -182
- package/dist/server/uploads.js +4 -43
- package/dist/server/virtual-tui.js +2 -27
- package/dist/server/vision-bridge.js +0 -27
- package/dist/server/wait-subscription-scan.js +13 -115
- package/dist/server/webui-context.js +2 -87
- package/dist/server/workspace-handle.js +373 -0
- package/dist/server/workspace-snapshot.js +7 -113
- package/dist/server/worktree-service.js +2 -53
- package/dist/server/worktree-tool.js +0 -11
- package/dist/server/ws-client-id.js +0 -18
- package/dist/server/ws-pending-queue.js +0 -7
- package/dist/server/ws-unknown-types.js +0 -17
- package/docs/README.md +24 -17
- package/docs/architecture-plugins.md +55 -6
- package/docs/device-center.md +57 -23
- package/docs/device-desktop.md +3 -3
- package/docs/group-orchestration.md +142 -0
- package/docs/hub-upgrade-release-testing.md +46 -0
- package/extensions/webui.ts +3 -74
- package/package.json +9 -6
- package/plugin-sdk/README.md +22 -8
- package/plugin-sdk/client-utils.mjs +0 -7
- package/plugin-sdk/client.d.ts +26 -0
- package/plugin-sdk/client.mjs +52 -0
- package/plugin-sdk/import-check.mjs +4 -51
- package/plugin-sdk/index.d.ts +128 -117
- package/plugin-sdk/index.mjs +2 -687
- package/plugin-sdk/package.json +15 -4
- package/plugin-sdk/ssh-host-trust.mjs +0 -12
- package/plugin-sdk/testing.d.ts +9 -0
- package/plugin-sdk/testing.mjs +511 -0
- package/plugins/catalog.json +52 -4
- package/plugins/superagent/README.md +110 -0
- package/plugins/superagent/client/entry.mjs +21 -0
- package/plugins/superagent/index.mjs +8 -0
- package/plugins/superagent/main.mjs +218 -0
- package/plugins/superagent/manifest.json +15 -0
- package/plugins/superagent/server.mjs +572 -0
- package/plugins/superagent/style.css +167 -0
- package/themes/aetheris.css +1 -31
- package/themes/ayu-light.css +0 -1
- package/themes/bases/flow-base.css +2 -83
- package/themes/bases/obsidian-base.css +2 -37
- package/themes/bases/statusbar-flat-base.css +0 -1
- package/themes/bases/zhujian-base.css +0 -63
- package/themes/bases/zhupi-base.css +0 -64
- package/themes/catppuccin-latte.css +0 -1
- package/themes/catppuccin.css +0 -1
- package/themes/claude-code-dark.css +0 -1
- package/themes/codex.css +0 -1
- package/themes/dark-teal.css +22 -54
- package/themes/everforest-light.css +0 -1
- package/themes/flow-cristallo.css +160 -0
- package/themes/flow-ink.css +160 -0
- package/themes/flow-light.css +1 -10
- package/themes/flow-obsidian.css +160 -0
- package/themes/flow-zurich.css +160 -0
- package/themes/flow.css +1 -9
- package/themes/geist.css +0 -1
- package/themes/gruvbox-light.css +0 -1
- package/themes/kanagawa-lotus.css +0 -1
- package/themes/md-preview.css +1 -2
- package/themes/mist.css +0 -1
- package/themes/nord.css +0 -1
- package/themes/one-dark.css +0 -1
- package/themes/paper.css +0 -1
- package/themes/rose-pine-dawn.css +0 -1
- package/themes/sakura.css +0 -1
- package/themes/solarized-light.css +0 -1
- package/themes/tokyo-night.css +0 -1
- package/themes/translucent.css +4 -35
- package/themes/transparent.css +4 -41
- package/themes/vs-code-dark.css +0 -1
- package/themes/wallpaper-flow.css +3 -17
- package/themes/wallpaper-obsidian.css +1 -4
- package/themes/white.css +0 -1
- package/web/dist/.build-assets.json +1 -1
- package/web/dist/assets/{BgTasksModal-BDoOpj-F.js → BgTasksModal-D-x7kA63.js} +1 -1
- package/web/dist/assets/CanvasStudio-CAWcs9tm.js +1579 -0
- package/web/dist/assets/DeviceCenter-Dc0whRcu.js +3 -0
- package/web/dist/assets/DeviceExecutionSettings-BoX7-0h7.js +1 -0
- package/web/dist/assets/DeviceGroups-C_fZMNeK.js +1 -0
- package/web/dist/assets/{Dropdown-C4LIhshN.js → Dropdown-CTgVLUbp.js} +1 -1
- package/web/dist/assets/FilePreview-C6W0PvaK.js +2 -0
- package/web/dist/assets/GlobalSearchModal-HR9LSspC.js +1 -0
- package/web/dist/assets/GroupConversation-C81YjF2C.js +61 -0
- package/web/dist/assets/GroupWorkItems-BIvZtr10.js +1 -0
- package/web/dist/assets/{HintTip-c6d0TJoh.js → HintTip-Da6qc566.js} +1 -1
- package/web/dist/assets/ModelConfigModal-DhSyuIuV.js +2 -0
- package/web/dist/assets/NativeCliPanel-CU1rZmyu.js +5 -0
- package/web/dist/assets/{PiSetupModal-BmMrrgB9.js → PiSetupModal-DZKY5Wiw.js} +1 -1
- package/web/dist/assets/ProviderOAuthControls-CIL85nA7.js +1 -0
- package/web/dist/assets/SCMPanel-CuEgzAlO.js +2 -0
- package/web/dist/assets/SettingsModal-Cdu5h29p.js +33 -0
- package/web/dist/assets/TermXterm-Bt5WKAxP.js +3 -0
- package/web/dist/assets/TerminalPanel-BCIBV8se.js +1 -0
- package/web/dist/assets/TuiOverlayModal-3NDd9T97.js +1 -0
- package/web/dist/assets/app-globals-CZjFtI4F.js +129 -0
- package/web/dist/assets/canvas-client-WkhM55mR.js +1 -0
- package/web/dist/assets/composer-bridge-CImnjDHq.js +1 -0
- package/web/dist/assets/desktop-B8NZ3KW-.js +1 -0
- package/web/dist/assets/index-CQc3VrWa.css +1 -0
- package/web/dist/assets/index-D_AGu4Zv.js +404 -0
- package/web/dist/assets/plugin-host-uqZGpvCs.js +1 -0
- package/web/dist/assets/{remote-ssh-client-CfYSMEZg.js → remote-ssh-client-Cdf-cTUe.js} +1 -1
- package/web/dist/assets/theme-DGL7hLUs.js +1 -0
- package/web/dist/index.html +12 -11
- package/web/dist/sw.js +15 -61
- package/web/public/sw.js +14 -60
- package/dist/gh-comment-781.md +0 -5
- package/dist/server/device-center/local-page.js +0 -17
- package/dist/server/device-center/smoke-preview.js +0 -3
- package/dist/server/durable/durable-capabilities.js +0 -11
- package/dist/server/markers/builtins/services.js +0 -123
- package/dist/server/markers/builtins/todo.js +0 -189
- package/dist/server/markers-list-tool.js +0 -34
- package/dist/server/orchestrator.js +0 -109
- package/dist/server/patch-turn-end-boundary.js +0 -77
- package/dist/server/system-prompt-soul.js +0 -41
- package/dist/server/univer-service.js +0 -448
- package/dist/server/universal/drivers/dsh-driver.js +0 -379
- package/dist/test-artifact.md +0 -9
- package/dist/test-preview-modal.md +0 -11
- package/dist/test-report.html +0 -20
- package/web/dist/assets/BgTasksModal-C0i3O1FT.js +0 -1
- package/web/dist/assets/CanvasStudio-B8thymBN.js +0 -1579
- package/web/dist/assets/CanvasStudio-zSxyIx1R.js +0 -1579
- package/web/dist/assets/DeviceCenter-DfAHKcid.js +0 -3
- package/web/dist/assets/DeviceCenter-ydvOTlzE.js +0 -3
- package/web/dist/assets/DeviceExecutionSettings-q5Ik8oXg.js +0 -1
- package/web/dist/assets/DeviceGroups-CI14_SRb.js +0 -1
- package/web/dist/assets/DeviceGroups-DvsB5IEU.js +0 -1
- package/web/dist/assets/FilePreview-BgGongmv.js +0 -2
- package/web/dist/assets/FilePreview-D4NrhF-U.js +0 -2
- package/web/dist/assets/GlobalSearchModal-BWgU8yPd.js +0 -1
- package/web/dist/assets/GlobalSearchModal-F7fNimKk.js +0 -1
- package/web/dist/assets/GroupConversation-Dh71Pdb-.js +0 -17
- package/web/dist/assets/GroupConversation-ZK3nRmNm.js +0 -17
- package/web/dist/assets/ModelConfigModal-YVEgyy34.js +0 -2
- package/web/dist/assets/ModelConfigModal-vWtg3srp.js +0 -2
- package/web/dist/assets/ProviderOAuthControls-BGSBIKD7.js +0 -1
- package/web/dist/assets/SCMPanel-B8_EDxNr.js +0 -2
- package/web/dist/assets/SCMPanel-BU58qk3T.js +0 -2
- package/web/dist/assets/SettingsModal-CMUybq3J.js +0 -32
- package/web/dist/assets/SettingsModal-OBmGTh-5.js +0 -32
- package/web/dist/assets/TerminalPanel-0CYvD72B.js +0 -3
- package/web/dist/assets/TerminalPanel-DDavKvFr.js +0 -3
- package/web/dist/assets/TuiOverlayModal-BsA-QTvN.js +0 -1
- package/web/dist/assets/TuiOverlayModal-DzoVQBuG.js +0 -1
- package/web/dist/assets/app-globals-CZxzGvIo.js +0 -123
- package/web/dist/assets/canvas-client-CUnXlQYX.js +0 -1
- package/web/dist/assets/desktop-aAKpvKJV.js +0 -1
- package/web/dist/assets/index-C6DwztqE.css +0 -1
- package/web/dist/assets/index-CuN802Sj.js +0 -93
- package/web/dist/assets/index-EH5Fw9fa.js +0 -93
- package/web/dist/assets/plugin-host-BRWib9DF.js +0 -1
- package/web/dist/assets/theme-D2l_mLNo.js +0 -1
|
@@ -1,172 +1,359 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { readFileSync } from "node:fs";
|
|
1
|
+
import { readFileSync, unlinkSync } from "node:fs";
|
|
3
2
|
import { join } from "node:path";
|
|
4
3
|
import { writeAtomicSync } from "../atomic-file.js";
|
|
5
|
-
import { deviceTaskFailure } from "./task-error.js";
|
|
6
|
-
import { boundTaskReport } from "./task-report.js";
|
|
7
4
|
import { parseReportMetadata } from "./report-metadata.js";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
import { TASK_REPORT_BYTES } from "./task-report.js";
|
|
6
|
+
import { computeOutcomeFingerprint, computeLegacyOutcomeFingerprint, normalizeTaskOutcome, TASK_OUTCOME_ERROR_BYTES, } from "./task-outcome.js";
|
|
7
|
+
import { validateDeviceArtifact } from "./work-contract.js";
|
|
8
|
+
export { computeOutcomeFingerprint, normalizeTaskOutcome };
|
|
9
|
+
export const TASK_QUARANTINE_LIMITS = { maxEntries: 128, maxTotalBytes: 2 * 1024 * 1024 };
|
|
10
|
+
export const TASK_RECOVERY_STATE_MAX_BYTES = 8 * 1024 * 1024;
|
|
14
11
|
export const TASK_RECOVERY_DEFAULTS = {
|
|
15
|
-
maxInactivityMs: 20 * 60 * 1000,
|
|
16
|
-
maxDurationMs: 0,
|
|
17
|
-
renewIntervalMs: 15 * 1000,
|
|
18
|
-
recoveryWindowMs: 60 * 1000,
|
|
12
|
+
maxInactivityMs: 20 * 60 * 1000,
|
|
13
|
+
maxDurationMs: 0,
|
|
14
|
+
renewIntervalMs: 15 * 1000,
|
|
15
|
+
recoveryWindowMs: 60 * 1000,
|
|
19
16
|
};
|
|
20
17
|
export const TASK_OUTBOX_LIMITS = {
|
|
21
18
|
maxEntries: 128,
|
|
22
|
-
maxTotalBytes: 2 * 1024 * 1024,
|
|
19
|
+
maxTotalBytes: 2 * 1024 * 1024,
|
|
23
20
|
};
|
|
24
|
-
|
|
25
|
-
*
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
export const TASK_ARTIFACT_WORST_CASE_BYTES = 256 * 1024;
|
|
22
|
+
export const TASK_OUTBOX_RESERVATION_BYTES = 6 * (TASK_REPORT_BYTES + TASK_OUTCOME_ERROR_BYTES) + 16 * 1024 + TASK_ARTIFACT_WORST_CASE_BYTES;
|
|
23
|
+
function validateOutboxItem(item, deviceId) {
|
|
24
|
+
if (!item ||
|
|
25
|
+
typeof item !== "object" ||
|
|
26
|
+
typeof item.id !== "string" ||
|
|
27
|
+
!item.id ||
|
|
28
|
+
item.id.length > 257 ||
|
|
29
|
+
typeof item.taskId !== "string" ||
|
|
30
|
+
!item.taskId ||
|
|
31
|
+
item.taskId.length > 128 ||
|
|
32
|
+
typeof item.attemptId !== "string" ||
|
|
33
|
+
!item.attemptId ||
|
|
34
|
+
item.attemptId.length > 128 ||
|
|
35
|
+
typeof item.botId !== "string" ||
|
|
36
|
+
!item.botId ||
|
|
37
|
+
item.botId.length > 128 ||
|
|
38
|
+
item.deviceId !== deviceId ||
|
|
39
|
+
!["completed", "failed", "interrupted"].includes(item.status) ||
|
|
40
|
+
!Number.isSafeInteger(item.createdAt) ||
|
|
41
|
+
item.createdAt <= 0 ||
|
|
42
|
+
!Number.isSafeInteger(item.updatedAt) ||
|
|
43
|
+
!Number.isSafeInteger(item.attempts)) {
|
|
44
|
+
throw new Error("Invalid outbox item schema");
|
|
45
|
+
}
|
|
46
|
+
const entry = item;
|
|
47
|
+
if (entry.id !== `${entry.taskId}:${entry.attemptId}` ||
|
|
48
|
+
(entry.report !== undefined && typeof entry.report !== "string") ||
|
|
49
|
+
(entry.error !== undefined && typeof entry.error !== "string"))
|
|
50
|
+
throw new Error("Invalid outbox identity or content");
|
|
51
|
+
if (entry.reportMetadata !== undefined) {
|
|
52
|
+
entry.reportMetadata = parseReportMetadata(entry.reportMetadata);
|
|
53
|
+
}
|
|
54
|
+
if (entry.artifact !== undefined) {
|
|
55
|
+
const res = validateDeviceArtifact(entry.artifact);
|
|
56
|
+
if (!res.valid)
|
|
57
|
+
throw new Error(`Invalid outbox artifact: ${res.reason}`);
|
|
58
|
+
}
|
|
59
|
+
if (entry.leaseToken !== undefined && (typeof entry.leaseToken !== "string" || entry.leaseToken.length > 128)) {
|
|
60
|
+
throw new Error("Invalid outbox leaseToken");
|
|
61
|
+
}
|
|
62
|
+
if (entry.leaseGeneration !== undefined &&
|
|
63
|
+
(!Number.isSafeInteger(entry.leaseGeneration) || entry.leaseGeneration < 1)) {
|
|
64
|
+
throw new Error("Invalid outbox leaseGeneration");
|
|
65
|
+
}
|
|
66
|
+
if (entry.hubUrl !== undefined && (typeof entry.hubUrl !== "string" || entry.hubUrl.length > 512)) {
|
|
67
|
+
throw new Error("Invalid outbox hubUrl");
|
|
68
|
+
}
|
|
69
|
+
const hasProofOrArtifact = entry.artifact !== undefined || entry.executionCompleted !== undefined || entry.artifactProduced !== undefined;
|
|
70
|
+
const fingerprint = computeOutcomeFingerprint(entry.taskId, entry.attemptId, entry.status, entry.report, entry.error, entry.reportMetadata, entry.artifact?.digest, entry.executionCompleted, entry.artifactProduced);
|
|
71
|
+
if (hasProofOrArtifact) {
|
|
72
|
+
if (entry.fingerprint !== undefined && entry.fingerprint !== fingerprint) {
|
|
73
|
+
throw new Error("Invalid outbox outcome fingerprint (proof bound artifact requires canonical fingerprint)");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
const legacyFingerprint = computeLegacyOutcomeFingerprint(entry.taskId, entry.attemptId, entry.status, entry.report, entry.error, entry.reportMetadata);
|
|
78
|
+
if (entry.fingerprint !== undefined &&
|
|
79
|
+
entry.fingerprint !== fingerprint &&
|
|
80
|
+
entry.fingerprint !== legacyFingerprint) {
|
|
81
|
+
throw new Error("Invalid outbox outcome fingerprint");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
entry.fingerprint ??= fingerprint;
|
|
85
|
+
return entry;
|
|
86
|
+
}
|
|
87
|
+
function validateLeaseItem(item, deviceId) {
|
|
88
|
+
if (!item ||
|
|
89
|
+
typeof item !== "object" ||
|
|
90
|
+
typeof item.taskId !== "string" ||
|
|
91
|
+
!item.taskId ||
|
|
92
|
+
item.taskId.length > 128 ||
|
|
93
|
+
typeof item.attemptId !== "string" ||
|
|
94
|
+
!item.attemptId ||
|
|
95
|
+
item.attemptId.length > 128 ||
|
|
96
|
+
typeof item.botId !== "string" ||
|
|
97
|
+
!item.botId ||
|
|
98
|
+
item.botId.length > 128 ||
|
|
99
|
+
typeof item.roomId !== "string" ||
|
|
100
|
+
!item.roomId ||
|
|
101
|
+
item.roomId.length > 128 ||
|
|
102
|
+
typeof item.leaseToken !== "string" ||
|
|
103
|
+
!item.leaseToken ||
|
|
104
|
+
item.leaseToken.length > 128 ||
|
|
105
|
+
!Number.isSafeInteger(item.leaseGeneration) ||
|
|
106
|
+
item.deviceId !== deviceId ||
|
|
107
|
+
!Number.isSafeInteger(item.startedAt) ||
|
|
108
|
+
item.startedAt <= 0 ||
|
|
109
|
+
!Number.isSafeInteger(item.lastActivityAt) ||
|
|
110
|
+
!Number.isSafeInteger(item.lastRenewedActivityAt)) {
|
|
111
|
+
throw new Error("Invalid lease item schema");
|
|
112
|
+
}
|
|
113
|
+
const lease = item;
|
|
114
|
+
if (lease.hubUrl !== undefined && (typeof lease.hubUrl !== "string" || lease.hubUrl.length > 512)) {
|
|
115
|
+
throw new Error("Invalid lease hubUrl");
|
|
116
|
+
}
|
|
117
|
+
return lease;
|
|
118
|
+
}
|
|
30
119
|
export class TaskRecoveryManager {
|
|
31
|
-
|
|
32
|
-
|
|
120
|
+
statePath;
|
|
121
|
+
legacyOutboxPath;
|
|
122
|
+
legacyLeasesPath;
|
|
33
123
|
deviceId;
|
|
34
124
|
hubUrl;
|
|
35
125
|
maxInactivityMs;
|
|
36
126
|
maxDurationMs;
|
|
37
127
|
outbox = new Map();
|
|
38
128
|
activeLeases = new Map();
|
|
129
|
+
otherHubOutbox = [];
|
|
130
|
+
otherHubLeases = [];
|
|
39
131
|
haltedOrphans = [];
|
|
40
|
-
|
|
41
|
-
|
|
132
|
+
loadError = false;
|
|
133
|
+
quarantine = [];
|
|
42
134
|
constructor(options) {
|
|
43
135
|
this.deviceId = options.deviceId;
|
|
44
136
|
this.hubUrl = options.hubUrl;
|
|
45
137
|
this.maxInactivityMs = options.maxInactivityMs ?? TASK_RECOVERY_DEFAULTS.maxInactivityMs;
|
|
46
138
|
this.maxDurationMs = options.maxDurationMs ?? TASK_RECOVERY_DEFAULTS.maxDurationMs;
|
|
47
139
|
if (options.dataDir) {
|
|
48
|
-
this.
|
|
49
|
-
this.
|
|
140
|
+
this.statePath = join(options.dataDir, "task-recovery-state.json");
|
|
141
|
+
this.legacyOutboxPath = join(options.dataDir, "task-recovery-outbox.json");
|
|
142
|
+
this.legacyLeasesPath = join(options.dataDir, "task-active-leases.json");
|
|
50
143
|
}
|
|
51
|
-
this.
|
|
52
|
-
|
|
144
|
+
this.loadStateAndMigrate(options.onHaltOrphaned);
|
|
145
|
+
}
|
|
146
|
+
get outboxLoadError() {
|
|
147
|
+
return this.loadError;
|
|
148
|
+
}
|
|
149
|
+
get leasesLoadError() {
|
|
150
|
+
return this.loadError;
|
|
53
151
|
}
|
|
54
152
|
leaseKey(taskId, attemptId) {
|
|
55
153
|
return `${taskId}:${attemptId}`;
|
|
56
154
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
throw new Error("Invalid outbox data root");
|
|
65
|
-
const seenKeys = new Set();
|
|
66
|
-
for (const item of data) {
|
|
67
|
-
if (!item ||
|
|
68
|
-
typeof item !== "object" ||
|
|
69
|
-
typeof item.id !== "string" ||
|
|
70
|
-
item.id.length > 128 ||
|
|
71
|
-
typeof item.taskId !== "string" ||
|
|
72
|
-
item.taskId.length > 128 ||
|
|
73
|
-
typeof item.attemptId !== "string" ||
|
|
74
|
-
item.attemptId.length > 128 ||
|
|
75
|
-
typeof item.botId !== "string" ||
|
|
76
|
-
item.botId.length > 128 ||
|
|
77
|
-
item.deviceId !== this.deviceId ||
|
|
78
|
-
!["completed", "failed", "interrupted"].includes(item.status) ||
|
|
79
|
-
!Number.isSafeInteger(item.createdAt) ||
|
|
80
|
-
item.createdAt <= 0 ||
|
|
81
|
-
!Number.isSafeInteger(item.updatedAt) ||
|
|
82
|
-
!Number.isSafeInteger(item.attempts)) {
|
|
83
|
-
throw new Error("Invalid outbox item schema");
|
|
84
|
-
}
|
|
85
|
-
if (item.reportMetadata !== undefined)
|
|
86
|
-
item.reportMetadata = parseReportMetadata(item.reportMetadata);
|
|
87
|
-
const key = item.id;
|
|
88
|
-
if (seenKeys.has(key))
|
|
89
|
-
throw new Error("Duplicate outbox key");
|
|
90
|
-
seenKeys.add(key);
|
|
91
|
-
this.outbox.set(key, item);
|
|
155
|
+
matchesHub(entryHubUrl) {
|
|
156
|
+
return this.hubUrl === entryHubUrl;
|
|
157
|
+
}
|
|
158
|
+
cleanupLegacyFiles() {
|
|
159
|
+
if (this.legacyOutboxPath) {
|
|
160
|
+
try {
|
|
161
|
+
unlinkSync(this.legacyOutboxPath);
|
|
92
162
|
}
|
|
163
|
+
catch { }
|
|
93
164
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
165
|
+
if (this.legacyLeasesPath) {
|
|
166
|
+
try {
|
|
167
|
+
unlinkSync(this.legacyLeasesPath);
|
|
97
168
|
}
|
|
98
|
-
|
|
99
|
-
this.outboxLoadError = true;
|
|
169
|
+
catch { }
|
|
100
170
|
}
|
|
101
171
|
}
|
|
102
|
-
|
|
103
|
-
if (!this.
|
|
172
|
+
writeStateToDiskSync(state) {
|
|
173
|
+
if (!this.statePath)
|
|
104
174
|
return;
|
|
105
|
-
if (this.
|
|
175
|
+
if (this.loadError) {
|
|
106
176
|
throw new Error("Outbox persistence blocked: corrupted storage was preserved");
|
|
107
|
-
|
|
108
|
-
|
|
177
|
+
}
|
|
178
|
+
const text = JSON.stringify({ ...state, version: 2, quarantine: state.quarantine ?? this.quarantine }, null, 2) + "\n";
|
|
179
|
+
if (Buffer.byteLength(text) > TASK_RECOVERY_STATE_MAX_BYTES)
|
|
180
|
+
throw new Error("Recovery state capacity exceeded; evidence retained");
|
|
181
|
+
writeAtomicSync(this.statePath, text, { mode: 0o600, fsync: true });
|
|
109
182
|
}
|
|
110
|
-
|
|
111
|
-
if (!this.
|
|
183
|
+
loadStateAndMigrate(onHaltOrphaned) {
|
|
184
|
+
if (!this.statePath)
|
|
112
185
|
return;
|
|
113
|
-
|
|
114
|
-
throw new Error("Active leases persistence blocked: corrupted storage was preserved");
|
|
115
|
-
const text = JSON.stringify(entries, null, 2) + "\n";
|
|
116
|
-
writeAtomicSync(this.leasesPath, text, { mode: 0o600 });
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Detect uncompleted leases from a previous process crash/restart.
|
|
120
|
-
* Explicit safety requirement: NEVER blindly rerun side-effects!
|
|
121
|
-
* Mark orphaned tasks as safely interrupted and stage them into Outbox,
|
|
122
|
-
* EXCEPT if an existing completed/terminal outcome is already in Outbox.
|
|
123
|
-
*/
|
|
124
|
-
loadAndDetectOrphanLeases(onHaltOrphaned) {
|
|
125
|
-
if (!this.leasesPath)
|
|
126
|
-
return;
|
|
127
|
-
let stored = [];
|
|
186
|
+
let loadedState = null;
|
|
128
187
|
try {
|
|
129
|
-
const raw = readFileSync(this.
|
|
188
|
+
const raw = readFileSync(this.statePath, "utf8");
|
|
189
|
+
if (Buffer.byteLength(raw) > TASK_RECOVERY_STATE_MAX_BYTES)
|
|
190
|
+
throw new Error("Recovery state capacity exceeded");
|
|
130
191
|
const parsed = JSON.parse(raw);
|
|
131
|
-
if (!
|
|
132
|
-
throw new Error("Invalid
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
192
|
+
if (!parsed || typeof parsed !== "object" || ![1, 2].includes(parsed.version)) {
|
|
193
|
+
throw new Error("Invalid recovery state file version or root");
|
|
194
|
+
}
|
|
195
|
+
if (typeof parsed.deviceId !== "string" || parsed.deviceId !== this.deviceId) {
|
|
196
|
+
throw new Error("Invalid recovery state file deviceId binding");
|
|
197
|
+
}
|
|
198
|
+
if (!Array.isArray(parsed.outbox) || !Array.isArray(parsed.activeLeases)) {
|
|
199
|
+
throw new Error("Invalid recovery state file collections");
|
|
200
|
+
}
|
|
201
|
+
if (parsed.version === 2) {
|
|
202
|
+
if (!Array.isArray(parsed.quarantine))
|
|
203
|
+
throw new Error("Invalid quarantine collection");
|
|
204
|
+
const seen = new Set();
|
|
205
|
+
const archive = parsed.quarantine.map((item) => {
|
|
206
|
+
if (!item ||
|
|
207
|
+
!["terminal_conflict", "attempt_expired", "legacy_lease_missing"].includes(item.reasonCode) ||
|
|
208
|
+
!Number.isSafeInteger(item.quarantinedAt) ||
|
|
209
|
+
item.quarantinedAt < 0)
|
|
210
|
+
throw new Error("Invalid quarantined outcome");
|
|
211
|
+
const outcome = validateOutboxItem(item.outcome, this.deviceId);
|
|
212
|
+
const key = JSON.stringify([outcome.hubUrl, outcome.id]);
|
|
213
|
+
if (seen.has(key))
|
|
214
|
+
throw new Error("Duplicate quarantined outcome");
|
|
215
|
+
seen.add(key);
|
|
216
|
+
return { outcome, reasonCode: item.reasonCode, quarantinedAt: item.quarantinedAt };
|
|
217
|
+
});
|
|
218
|
+
if (archive.length > TASK_QUARANTINE_LIMITS.maxEntries ||
|
|
219
|
+
Buffer.byteLength(JSON.stringify(archive)) > TASK_QUARANTINE_LIMITS.maxTotalBytes)
|
|
220
|
+
throw new Error("Quarantine capacity exceeded");
|
|
221
|
+
this.quarantine = archive;
|
|
160
222
|
}
|
|
223
|
+
else if (parsed.quarantine !== undefined)
|
|
224
|
+
throw new Error("Unexpected v1 quarantine");
|
|
225
|
+
const seenOutboxKeys = new Set();
|
|
226
|
+
const validOutbox = [];
|
|
227
|
+
for (const item of parsed.outbox) {
|
|
228
|
+
const entry = validateOutboxItem(item, this.deviceId);
|
|
229
|
+
entry.hubUrl ??= typeof parsed.hubUrl === "string" ? parsed.hubUrl : this.hubUrl;
|
|
230
|
+
const scopedKey = JSON.stringify([entry.hubUrl, entry.id]);
|
|
231
|
+
if (seenOutboxKeys.has(scopedKey) ||
|
|
232
|
+
this.quarantine.some((item) => JSON.stringify([item.outcome.hubUrl, item.outcome.id]) === scopedKey))
|
|
233
|
+
throw new Error("Duplicate outbox or quarantined key in state file");
|
|
234
|
+
seenOutboxKeys.add(scopedKey);
|
|
235
|
+
validOutbox.push(entry);
|
|
236
|
+
}
|
|
237
|
+
const seenLeaseKeys = new Set();
|
|
238
|
+
const validLeases = [];
|
|
239
|
+
for (const item of parsed.activeLeases) {
|
|
240
|
+
const lease = validateLeaseItem(item, this.deviceId);
|
|
241
|
+
lease.hubUrl ??= typeof parsed.hubUrl === "string" ? parsed.hubUrl : this.hubUrl;
|
|
242
|
+
const key = JSON.stringify([lease.hubUrl, this.leaseKey(lease.taskId, lease.attemptId)]);
|
|
243
|
+
if (seenLeaseKeys.has(key))
|
|
244
|
+
throw new Error("Duplicate active lease key in state file");
|
|
245
|
+
seenLeaseKeys.add(key);
|
|
246
|
+
validLeases.push(lease);
|
|
247
|
+
}
|
|
248
|
+
loadedState = {
|
|
249
|
+
version: 1,
|
|
250
|
+
deviceId: this.deviceId,
|
|
251
|
+
hubUrl: typeof parsed.hubUrl === "string" ? parsed.hubUrl : this.hubUrl,
|
|
252
|
+
outbox: validOutbox,
|
|
253
|
+
activeLeases: validLeases,
|
|
254
|
+
};
|
|
255
|
+
this.cleanupLegacyFiles();
|
|
161
256
|
}
|
|
162
257
|
catch (error) {
|
|
163
258
|
if (error.code === "ENOENT") {
|
|
259
|
+
loadedState = null;
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
this.loadError = true;
|
|
164
263
|
return;
|
|
165
264
|
}
|
|
166
|
-
this.leasesLoadError = true;
|
|
167
|
-
return;
|
|
168
265
|
}
|
|
169
|
-
if (
|
|
266
|
+
if (loadedState === null) {
|
|
267
|
+
let legacyOutbox = [];
|
|
268
|
+
let legacyLeases = [];
|
|
269
|
+
let hasLegacy = false;
|
|
270
|
+
if (this.legacyOutboxPath) {
|
|
271
|
+
try {
|
|
272
|
+
const raw = readFileSync(this.legacyOutboxPath, "utf8");
|
|
273
|
+
hasLegacy = true;
|
|
274
|
+
const parsed = JSON.parse(raw);
|
|
275
|
+
if (!Array.isArray(parsed))
|
|
276
|
+
throw new Error("Invalid legacy outbox root");
|
|
277
|
+
const seenKeys = new Set();
|
|
278
|
+
for (const item of parsed) {
|
|
279
|
+
const entry = validateOutboxItem(item, this.deviceId);
|
|
280
|
+
if (seenKeys.has(entry.id))
|
|
281
|
+
throw new Error("Duplicate outbox key in legacy file");
|
|
282
|
+
seenKeys.add(entry.id);
|
|
283
|
+
entry.hubUrl ??= this.hubUrl;
|
|
284
|
+
legacyOutbox.push(entry);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
if (error.code !== "ENOENT") {
|
|
289
|
+
this.loadError = true;
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (this.legacyLeasesPath) {
|
|
295
|
+
try {
|
|
296
|
+
const raw = readFileSync(this.legacyLeasesPath, "utf8");
|
|
297
|
+
hasLegacy = true;
|
|
298
|
+
const parsed = JSON.parse(raw);
|
|
299
|
+
if (!Array.isArray(parsed))
|
|
300
|
+
throw new Error("Invalid legacy leases root");
|
|
301
|
+
const seenKeys = new Set();
|
|
302
|
+
for (const item of parsed) {
|
|
303
|
+
const lease = validateLeaseItem(item, this.deviceId);
|
|
304
|
+
const key = this.leaseKey(lease.taskId, lease.attemptId);
|
|
305
|
+
if (seenKeys.has(key))
|
|
306
|
+
throw new Error("Duplicate active lease key in legacy file");
|
|
307
|
+
seenKeys.add(key);
|
|
308
|
+
lease.hubUrl ??= this.hubUrl;
|
|
309
|
+
legacyLeases.push(lease);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
if (error.code !== "ENOENT") {
|
|
314
|
+
this.loadError = true;
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (this.loadError)
|
|
320
|
+
return;
|
|
321
|
+
loadedState = {
|
|
322
|
+
version: 1,
|
|
323
|
+
deviceId: this.deviceId,
|
|
324
|
+
hubUrl: this.hubUrl,
|
|
325
|
+
outbox: legacyOutbox,
|
|
326
|
+
activeLeases: legacyLeases,
|
|
327
|
+
};
|
|
328
|
+
if (hasLegacy) {
|
|
329
|
+
try {
|
|
330
|
+
this.writeStateToDiskSync(loadedState);
|
|
331
|
+
this.cleanupLegacyFiles();
|
|
332
|
+
}
|
|
333
|
+
catch {
|
|
334
|
+
this.loadError = true;
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
for (const entry of loadedState.outbox) {
|
|
340
|
+
if (this.matchesHub(entry.hubUrl)) {
|
|
341
|
+
this.outbox.set(entry.id, entry);
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
344
|
+
this.otherHubOutbox.push(entry);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
for (const lease of loadedState.activeLeases) {
|
|
348
|
+
if (this.matchesHub(lease.hubUrl)) {
|
|
349
|
+
this.activeLeases.set(this.leaseKey(lease.taskId, lease.attemptId), lease);
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
this.otherHubLeases.push(lease);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (this.activeLeases.size > 0) {
|
|
356
|
+
const stored = Array.from(this.activeLeases.values());
|
|
170
357
|
const now = Date.now();
|
|
171
358
|
const newOutboxEntries = [];
|
|
172
359
|
for (const lease of stored) {
|
|
@@ -177,60 +364,114 @@ export class TaskRecoveryManager {
|
|
|
177
364
|
catch { }
|
|
178
365
|
const outboxKey = this.leaseKey(lease.taskId, lease.attemptId);
|
|
179
366
|
const existingOutbox = this.outbox.get(outboxKey);
|
|
180
|
-
// If outbox already has a terminal outcome (e.g. completed before crash), NEVER overwrite it!
|
|
181
367
|
if (!existingOutbox) {
|
|
368
|
+
const report = "Execution safely halted on device process restart to prevent duplicate side effects.";
|
|
369
|
+
const error = "Device process restarted during execution";
|
|
370
|
+
const fingerprint = computeOutcomeFingerprint(lease.taskId, lease.attemptId, "interrupted", report, error);
|
|
182
371
|
const outboxEntry = {
|
|
183
372
|
id: outboxKey,
|
|
184
373
|
taskId: lease.taskId,
|
|
185
374
|
attemptId: lease.attemptId,
|
|
186
375
|
botId: lease.botId,
|
|
187
376
|
deviceId: this.deviceId,
|
|
377
|
+
hubUrl: lease.hubUrl ?? this.hubUrl,
|
|
188
378
|
roomId: lease.roomId,
|
|
189
379
|
status: "interrupted",
|
|
190
|
-
report
|
|
191
|
-
error
|
|
380
|
+
report,
|
|
381
|
+
error,
|
|
382
|
+
fingerprint,
|
|
192
383
|
createdAt: now,
|
|
193
384
|
updatedAt: now,
|
|
194
385
|
attempts: 0,
|
|
386
|
+
leaseToken: lease.leaseToken,
|
|
387
|
+
leaseGeneration: lease.leaseGeneration,
|
|
195
388
|
};
|
|
196
389
|
newOutboxEntries.push(outboxEntry);
|
|
197
390
|
}
|
|
198
391
|
}
|
|
199
|
-
|
|
200
|
-
this.
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
392
|
+
const allOutbox = [...this.otherHubOutbox, ...Array.from(this.outbox.values()), ...newOutboxEntries];
|
|
393
|
+
const allLeases = [...this.otherHubLeases];
|
|
394
|
+
const nextState = {
|
|
395
|
+
version: 1,
|
|
396
|
+
deviceId: this.deviceId,
|
|
397
|
+
hubUrl: this.hubUrl,
|
|
398
|
+
outbox: allOutbox,
|
|
399
|
+
activeLeases: allLeases,
|
|
400
|
+
};
|
|
401
|
+
this.writeStateToDiskSync(nextState);
|
|
204
402
|
for (const entry of newOutboxEntries) {
|
|
205
403
|
this.outbox.set(entry.id, entry);
|
|
206
404
|
}
|
|
207
405
|
this.activeLeases.clear();
|
|
208
406
|
}
|
|
209
407
|
}
|
|
210
|
-
/** Get list of orphaned tasks halted at process startup. */
|
|
211
408
|
getHaltedOrphans() {
|
|
212
409
|
return structuredClone(this.haltedOrphans);
|
|
213
410
|
}
|
|
214
|
-
/**
|
|
215
|
-
* Record a newly running task lease.
|
|
216
|
-
*/
|
|
217
411
|
recordTaskStart(lease) {
|
|
412
|
+
if (!lease ||
|
|
413
|
+
typeof lease !== "object" ||
|
|
414
|
+
typeof lease.taskId !== "string" ||
|
|
415
|
+
!lease.taskId ||
|
|
416
|
+
lease.taskId.length > 128 ||
|
|
417
|
+
typeof lease.attemptId !== "string" ||
|
|
418
|
+
!lease.attemptId ||
|
|
419
|
+
lease.attemptId.length > 128 ||
|
|
420
|
+
typeof lease.botId !== "string" ||
|
|
421
|
+
!lease.botId ||
|
|
422
|
+
lease.botId.length > 128 ||
|
|
423
|
+
typeof lease.roomId !== "string" ||
|
|
424
|
+
!lease.roomId ||
|
|
425
|
+
lease.roomId.length > 128 ||
|
|
426
|
+
typeof lease.leaseToken !== "string" ||
|
|
427
|
+
!lease.leaseToken ||
|
|
428
|
+
lease.leaseToken.length > 128 ||
|
|
429
|
+
(lease.leaseGeneration !== undefined && !Number.isSafeInteger(lease.leaseGeneration)) ||
|
|
430
|
+
(lease.deviceId && lease.deviceId !== this.deviceId)) {
|
|
431
|
+
throw new Error("Invalid task lease parameters for recordTaskStart");
|
|
432
|
+
}
|
|
433
|
+
const key = this.leaseKey(lease.taskId, lease.attemptId);
|
|
434
|
+
if (this.outbox.has(key) ||
|
|
435
|
+
this.quarantine.some((item) => this.matchesHub(item.outcome.hubUrl) && item.outcome.id === key)) {
|
|
436
|
+
throw new Error(`Cannot start task: outcome for task ${lease.taskId} attempt ${lease.attemptId} already finalized in outbox`);
|
|
437
|
+
}
|
|
438
|
+
for (const [existingKey, existingLease] of this.activeLeases) {
|
|
439
|
+
if (existingLease.taskId === lease.taskId) {
|
|
440
|
+
if (existingKey === key) {
|
|
441
|
+
throw new Error(`Task lease already active for task ${lease.taskId} attempt ${lease.attemptId}`);
|
|
442
|
+
}
|
|
443
|
+
throw new Error(`Conflicting attempt active for task ${lease.taskId}: existing=${existingLease.attemptId}, new=${lease.attemptId}`);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (lease.hubUrl !== undefined && lease.hubUrl !== this.hubUrl)
|
|
447
|
+
throw new Error("Task lease Hub identity mismatch");
|
|
448
|
+
const capacity = this.hasOutboxCapacity();
|
|
449
|
+
if (!capacity.available)
|
|
450
|
+
throw new Error(capacity.reason);
|
|
218
451
|
const now = Date.now();
|
|
219
452
|
const record = {
|
|
220
453
|
...lease,
|
|
454
|
+
deviceId: this.deviceId,
|
|
455
|
+
hubUrl: lease.hubUrl ?? this.hubUrl,
|
|
221
456
|
activitySequence: lease.activitySequence ?? 0,
|
|
222
457
|
startedAt: lease.startedAt ?? now,
|
|
223
458
|
lastActivityAt: now,
|
|
224
459
|
lastRenewedActivityAt: now,
|
|
225
460
|
};
|
|
226
|
-
const
|
|
227
|
-
this.
|
|
228
|
-
this.
|
|
461
|
+
const nextLeases = [...Array.from(this.activeLeases.values()), record];
|
|
462
|
+
const allOutbox = [...this.otherHubOutbox, ...Array.from(this.outbox.values())];
|
|
463
|
+
const allLeases = [...this.otherHubLeases, ...nextLeases];
|
|
464
|
+
const state = {
|
|
465
|
+
version: 1,
|
|
466
|
+
deviceId: this.deviceId,
|
|
467
|
+
hubUrl: this.hubUrl,
|
|
468
|
+
outbox: allOutbox,
|
|
469
|
+
activeLeases: allLeases,
|
|
470
|
+
};
|
|
471
|
+
this.writeStateToDiskSync(state);
|
|
472
|
+
this.activeLeases.set(key, record);
|
|
229
473
|
return structuredClone(record);
|
|
230
474
|
}
|
|
231
|
-
/**
|
|
232
|
-
* Record verified local activity (LLM streaming or tool invocation).
|
|
233
|
-
*/
|
|
234
475
|
recordActivity(taskId, attemptId) {
|
|
235
476
|
const record = this.activeLeases.get(this.leaseKey(taskId, attemptId));
|
|
236
477
|
if (!record)
|
|
@@ -238,33 +479,30 @@ export class TaskRecoveryManager {
|
|
|
238
479
|
record.lastActivityAt = Date.now();
|
|
239
480
|
return true;
|
|
240
481
|
}
|
|
241
|
-
/**
|
|
242
|
-
* Remove active lease when task ends locally.
|
|
243
|
-
*/
|
|
244
482
|
removeActiveLease(taskId, attemptId) {
|
|
245
483
|
const key = this.leaseKey(taskId, attemptId);
|
|
246
484
|
if (this.activeLeases.has(key)) {
|
|
247
|
-
const
|
|
248
|
-
this.
|
|
485
|
+
const nextLeases = Array.from(this.activeLeases.values()).filter((l) => this.leaseKey(l.taskId, l.attemptId) !== key);
|
|
486
|
+
const allOutbox = [...this.otherHubOutbox, ...Array.from(this.outbox.values())];
|
|
487
|
+
const allLeases = [...this.otherHubLeases, ...nextLeases];
|
|
488
|
+
const state = {
|
|
489
|
+
version: 1,
|
|
490
|
+
deviceId: this.deviceId,
|
|
491
|
+
hubUrl: this.hubUrl,
|
|
492
|
+
outbox: allOutbox,
|
|
493
|
+
activeLeases: allLeases,
|
|
494
|
+
};
|
|
495
|
+
this.writeStateToDiskSync(state);
|
|
249
496
|
this.activeLeases.delete(key);
|
|
250
497
|
}
|
|
251
498
|
}
|
|
252
|
-
/**
|
|
253
|
-
* Get an active lease by taskId and attemptId.
|
|
254
|
-
*/
|
|
255
499
|
getActiveLease(taskId, attemptId) {
|
|
256
500
|
const lease = this.activeLeases.get(this.leaseKey(taskId, attemptId));
|
|
257
501
|
return lease ? structuredClone(lease) : undefined;
|
|
258
502
|
}
|
|
259
|
-
/**
|
|
260
|
-
* Get all active leases (used for reconnection claim).
|
|
261
|
-
*/
|
|
262
503
|
getActiveLeases() {
|
|
263
504
|
return structuredClone(Array.from(this.activeLeases.values()));
|
|
264
505
|
}
|
|
265
|
-
/**
|
|
266
|
-
* Prepare claim payload for all running tasks upon reconnection.
|
|
267
|
-
*/
|
|
268
506
|
prepareClaims() {
|
|
269
507
|
const claims = [];
|
|
270
508
|
for (const lease of this.activeLeases.values()) {
|
|
@@ -278,16 +516,11 @@ export class TaskRecoveryManager {
|
|
|
278
516
|
}
|
|
279
517
|
return claims;
|
|
280
518
|
}
|
|
281
|
-
/**
|
|
282
|
-
* Prepare heartbeat renewal frames for tasks with verified recent activity.
|
|
283
|
-
* monotonic activity progression: only true if new activity occurred since last renewal.
|
|
284
|
-
*/
|
|
285
519
|
prepareLeaseRenews(options) {
|
|
286
520
|
const now = Date.now();
|
|
287
521
|
const renews = [];
|
|
288
522
|
for (const lease of this.activeLeases.values()) {
|
|
289
523
|
const isWaitingHuman = options?.waitingForHumanTaskIds?.has(lease.taskId) ?? false;
|
|
290
|
-
// Human wait pauses inactivity expiration
|
|
291
524
|
const inactive = !isWaitingHuman && now - lease.lastActivityAt > this.maxInactivityMs;
|
|
292
525
|
const totalExceeded = this.maxDurationMs > 0 && now - lease.startedAt > this.maxDurationMs;
|
|
293
526
|
if (!inactive && !totalExceeded) {
|
|
@@ -309,9 +542,6 @@ export class TaskRecoveryManager {
|
|
|
309
542
|
}
|
|
310
543
|
return renews;
|
|
311
544
|
}
|
|
312
|
-
/**
|
|
313
|
-
* Check whether a task has timed out due to inactivity or hard duration ceiling.
|
|
314
|
-
*/
|
|
315
545
|
checkTaskTimeout(taskId, attemptId, isWaitingHuman = false) {
|
|
316
546
|
const lease = this.activeLeases.get(this.leaseKey(taskId, attemptId));
|
|
317
547
|
if (!lease)
|
|
@@ -331,12 +561,10 @@ export class TaskRecoveryManager {
|
|
|
331
561
|
}
|
|
332
562
|
return { expired: false };
|
|
333
563
|
}
|
|
334
|
-
/**
|
|
335
|
-
* Bounded capacity reservation guard: fail-closed before starting new execution
|
|
336
|
-
* if the recovery outbox has reached its budget ceiling, preserving existing results.
|
|
337
|
-
*/
|
|
338
564
|
hasOutboxCapacity() {
|
|
339
|
-
|
|
565
|
+
const reservations = this.activeLeases.size + this.otherHubLeases.length;
|
|
566
|
+
const totalEntries = this.outbox.size + this.otherHubOutbox.length + reservations;
|
|
567
|
+
if (totalEntries >= TASK_OUTBOX_LIMITS.maxEntries) {
|
|
340
568
|
return {
|
|
341
569
|
available: false,
|
|
342
570
|
reason: `Recovery outbox reached maximum capacity (${TASK_OUTBOX_LIMITS.maxEntries} entries). Reconnect to Hub to flush pending outcomes before starting new tasks.`,
|
|
@@ -346,7 +574,10 @@ export class TaskRecoveryManager {
|
|
|
346
574
|
for (const entry of this.outbox.values()) {
|
|
347
575
|
totalBytes += Buffer.byteLength(JSON.stringify(entry));
|
|
348
576
|
}
|
|
349
|
-
|
|
577
|
+
for (const entry of this.otherHubOutbox) {
|
|
578
|
+
totalBytes += Buffer.byteLength(JSON.stringify(entry));
|
|
579
|
+
}
|
|
580
|
+
if (totalBytes + (reservations + 1) * TASK_OUTBOX_RESERVATION_BYTES > TASK_OUTBOX_LIMITS.maxTotalBytes) {
|
|
350
581
|
return {
|
|
351
582
|
available: false,
|
|
352
583
|
reason: `Recovery outbox reached byte budget (${Math.round(TASK_OUTBOX_LIMITS.maxTotalBytes / 1024)} KiB). Reconnect to Hub to flush pending outcomes before starting new tasks.`,
|
|
@@ -354,38 +585,57 @@ export class TaskRecoveryManager {
|
|
|
354
585
|
}
|
|
355
586
|
return { available: true };
|
|
356
587
|
}
|
|
357
|
-
/**
|
|
358
|
-
* Enqueue task terminal outcome into durable outbox.
|
|
359
|
-
* First write disk synchronously; throw on error so execution doesn't falsely succeed.
|
|
360
|
-
* Idempotent: identical (taskId, attemptId, status) returns existing; conflicting rejects.
|
|
361
|
-
*/
|
|
362
588
|
recordTaskFinish(task, outcome) {
|
|
589
|
+
if (!task ||
|
|
590
|
+
typeof task !== "object" ||
|
|
591
|
+
typeof task.id !== "string" ||
|
|
592
|
+
!task.id ||
|
|
593
|
+
task.id.length > 128 ||
|
|
594
|
+
typeof task.attemptId !== "string" ||
|
|
595
|
+
!task.attemptId ||
|
|
596
|
+
task.attemptId.length > 128 ||
|
|
597
|
+
typeof task.botId !== "string" ||
|
|
598
|
+
!task.botId ||
|
|
599
|
+
task.botId.length > 128) {
|
|
600
|
+
throw new Error("Invalid task parameters for recordTaskFinish");
|
|
601
|
+
}
|
|
602
|
+
if (task.hubUrl !== undefined && task.hubUrl !== this.hubUrl)
|
|
603
|
+
throw new Error("Task outcome Hub identity mismatch");
|
|
363
604
|
const now = Date.now();
|
|
364
|
-
const
|
|
365
|
-
const safeError = outcome.error ? deviceTaskFailure(outcome.error) : undefined;
|
|
366
|
-
const reportMetadata = outcome.reportMetadata === undefined ? undefined : parseReportMetadata(outcome.reportMetadata);
|
|
367
|
-
const fingerprint = computeOutcomeFingerprint(task.id, task.attemptId, outcome.status, safeReport, safeError, reportMetadata);
|
|
605
|
+
const normalized = normalizeTaskOutcome(task.id, task.attemptId, outcome);
|
|
368
606
|
const key = this.leaseKey(task.id, task.attemptId);
|
|
369
|
-
const
|
|
607
|
+
const activeLease = this.activeLeases.get(key);
|
|
608
|
+
const leaseToken = task.leaseToken ?? activeLease?.leaseToken;
|
|
609
|
+
const leaseGeneration = task.leaseGeneration ?? activeLease?.leaseGeneration;
|
|
610
|
+
const hubUrl = task.hubUrl ?? activeLease?.hubUrl ?? this.hubUrl;
|
|
611
|
+
const contractDigest = task.contractDigest ?? activeLease?.contractDigest;
|
|
612
|
+
const manifestDigest = task.manifestDigest ?? activeLease?.manifestDigest;
|
|
613
|
+
const existing = this.outbox.get(key) ??
|
|
614
|
+
this.quarantine.find((item) => this.matchesHub(item.outcome.hubUrl) && item.outcome.id === key)?.outcome;
|
|
370
615
|
if (existing) {
|
|
371
616
|
const targetDeviceId = task.deviceId || this.deviceId;
|
|
372
|
-
|
|
617
|
+
const leaseTokenMatches = leaseToken === undefined || existing.leaseToken === leaseToken;
|
|
618
|
+
const leaseGenMatches = leaseGeneration === undefined || existing.leaseGeneration === leaseGeneration;
|
|
619
|
+
const artifactMatches = existing.artifact?.digest === normalized.artifact?.digest;
|
|
620
|
+
const proofMatches = existing.executionCompleted === normalized.executionCompleted &&
|
|
621
|
+
existing.artifactProduced === normalized.artifactProduced;
|
|
622
|
+
if (existing.report === normalized.report &&
|
|
623
|
+
existing.error === normalized.error &&
|
|
624
|
+
existing.status === normalized.status &&
|
|
373
625
|
existing.botId === task.botId &&
|
|
374
626
|
existing.deviceId === targetDeviceId &&
|
|
375
|
-
existing.roomId === task.roomId &&
|
|
376
|
-
JSON.stringify(existing.reportMetadata) === JSON.stringify(reportMetadata)
|
|
627
|
+
(task.roomId === undefined || existing.roomId === task.roomId) &&
|
|
628
|
+
JSON.stringify(existing.reportMetadata) === JSON.stringify(normalized.reportMetadata) &&
|
|
629
|
+
leaseTokenMatches &&
|
|
630
|
+
leaseGenMatches &&
|
|
631
|
+
artifactMatches &&
|
|
632
|
+
proofMatches) {
|
|
633
|
+
if (this.activeLeases.has(key)) {
|
|
634
|
+
this.removeActiveLease(task.id, task.attemptId);
|
|
635
|
+
}
|
|
377
636
|
return structuredClone(existing);
|
|
378
637
|
}
|
|
379
|
-
existing.status
|
|
380
|
-
existing.report = safeReport;
|
|
381
|
-
existing.error = safeError;
|
|
382
|
-
existing.reportMetadata = reportMetadata;
|
|
383
|
-
existing.fingerprint = fingerprint;
|
|
384
|
-
existing.updatedAt = now;
|
|
385
|
-
const nextOutbox = Array.from(this.outbox.values());
|
|
386
|
-
this.persistOutboxSync(nextOutbox);
|
|
387
|
-
this.removeActiveLease(task.id, task.attemptId);
|
|
388
|
-
return structuredClone(existing);
|
|
638
|
+
throw new Error(`Task outcome conflict: outcome for task ${task.id} attempt ${task.attemptId} already finalized with status=${existing.status}, fingerprint=${existing.fingerprint} (attempted status=${normalized.status}, fingerprint=${normalized.fingerprint})`);
|
|
389
639
|
}
|
|
390
640
|
const entry = {
|
|
391
641
|
id: key,
|
|
@@ -393,60 +643,152 @@ export class TaskRecoveryManager {
|
|
|
393
643
|
attemptId: task.attemptId,
|
|
394
644
|
botId: task.botId,
|
|
395
645
|
deviceId: task.deviceId || this.deviceId,
|
|
646
|
+
hubUrl,
|
|
396
647
|
roomId: task.roomId,
|
|
397
|
-
status:
|
|
398
|
-
reportMetadata,
|
|
399
|
-
report:
|
|
400
|
-
error:
|
|
401
|
-
fingerprint,
|
|
648
|
+
status: normalized.status,
|
|
649
|
+
reportMetadata: normalized.reportMetadata,
|
|
650
|
+
report: normalized.report,
|
|
651
|
+
error: normalized.error,
|
|
652
|
+
fingerprint: normalized.fingerprint,
|
|
653
|
+
artifact: normalized.artifact,
|
|
654
|
+
executionCompleted: normalized.executionCompleted,
|
|
655
|
+
artifactProduced: normalized.artifactProduced,
|
|
656
|
+
contractDigest,
|
|
657
|
+
manifestDigest,
|
|
402
658
|
createdAt: now,
|
|
403
659
|
updatedAt: now,
|
|
404
660
|
attempts: 0,
|
|
661
|
+
leaseToken,
|
|
662
|
+
leaseGeneration,
|
|
405
663
|
};
|
|
406
|
-
|
|
664
|
+
validateOutboxItem(entry, this.deviceId);
|
|
665
|
+
if (activeLease &&
|
|
666
|
+
(activeLease.botId !== entry.botId ||
|
|
667
|
+
(task.roomId !== undefined && activeLease.roomId !== task.roomId) ||
|
|
668
|
+
activeLease.leaseToken !== leaseToken ||
|
|
669
|
+
activeLease.leaseGeneration !== leaseGeneration))
|
|
670
|
+
throw new Error("Task outcome lease identity mismatch");
|
|
407
671
|
const nextOutbox = [...Array.from(this.outbox.values()), entry];
|
|
408
|
-
this.
|
|
409
|
-
|
|
672
|
+
const allPending = [...this.otherHubOutbox, ...nextOutbox];
|
|
673
|
+
if (allPending.length > TASK_OUTBOX_LIMITS.maxEntries ||
|
|
674
|
+
allPending.reduce((bytes, pending) => bytes + Buffer.byteLength(JSON.stringify(pending)), 0) >
|
|
675
|
+
TASK_OUTBOX_LIMITS.maxTotalBytes)
|
|
676
|
+
throw new Error("Recovery outbox capacity exceeded; outcome lease retained");
|
|
677
|
+
const nextLeases = Array.from(this.activeLeases.values()).filter((l) => this.leaseKey(l.taskId, l.attemptId) !== key);
|
|
678
|
+
const allOutbox = [...this.otherHubOutbox, ...nextOutbox];
|
|
679
|
+
const allLeases = [...this.otherHubLeases, ...nextLeases];
|
|
680
|
+
const state = {
|
|
681
|
+
version: 1,
|
|
682
|
+
deviceId: this.deviceId,
|
|
683
|
+
hubUrl: this.hubUrl,
|
|
684
|
+
outbox: allOutbox,
|
|
685
|
+
activeLeases: allLeases,
|
|
686
|
+
};
|
|
687
|
+
this.writeStateToDiskSync(state);
|
|
410
688
|
this.outbox.set(entry.id, entry);
|
|
411
|
-
|
|
412
|
-
this.removeActiveLease(task.id, task.attemptId);
|
|
689
|
+
this.activeLeases.delete(key);
|
|
413
690
|
return structuredClone(entry);
|
|
414
691
|
}
|
|
415
|
-
/**
|
|
416
|
-
* List all pending outbox entries waiting for Hub ACK.
|
|
417
|
-
*/
|
|
418
692
|
getPendingOutbox() {
|
|
419
693
|
return structuredClone(Array.from(this.outbox.values()));
|
|
420
694
|
}
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
695
|
+
getPendingOutboxEntry(taskId, attemptId) {
|
|
696
|
+
const entry = this.outbox.get(this.leaseKey(taskId, attemptId));
|
|
697
|
+
return entry ? structuredClone(entry) : undefined;
|
|
698
|
+
}
|
|
424
699
|
markOutboxAttempt(id) {
|
|
425
700
|
const entry = this.outbox.get(id);
|
|
426
701
|
if (entry) {
|
|
427
702
|
const updated = { ...entry, attempts: entry.attempts + 1, updatedAt: Date.now() };
|
|
428
|
-
const
|
|
429
|
-
this.
|
|
703
|
+
const nextOutbox = Array.from(this.outbox.values()).map((e) => (e.id === id ? updated : e));
|
|
704
|
+
const allOutbox = [...this.otherHubOutbox, ...nextOutbox];
|
|
705
|
+
const allLeases = [...this.otherHubLeases, ...Array.from(this.activeLeases.values())];
|
|
706
|
+
const state = {
|
|
707
|
+
version: 1,
|
|
708
|
+
deviceId: this.deviceId,
|
|
709
|
+
hubUrl: this.hubUrl,
|
|
710
|
+
outbox: allOutbox,
|
|
711
|
+
activeLeases: allLeases,
|
|
712
|
+
};
|
|
713
|
+
this.writeStateToDiskSync(state);
|
|
430
714
|
this.outbox.set(id, updated);
|
|
431
715
|
}
|
|
432
716
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
717
|
+
getQuarantinedOutcomes() {
|
|
718
|
+
return structuredClone(this.quarantine.filter((item) => this.matchesHub(item.outcome.hubUrl)));
|
|
719
|
+
}
|
|
720
|
+
quarantineOutbox(taskId, attemptId, fingerprint, reasonCode, status, artifactDigest) {
|
|
721
|
+
if (this.loadError ||
|
|
722
|
+
typeof reasonCode !== "string" ||
|
|
723
|
+
!["terminal_conflict", "attempt_expired", "legacy_lease_missing"].includes(reasonCode))
|
|
724
|
+
return false;
|
|
725
|
+
const key = this.leaseKey(taskId, attemptId);
|
|
726
|
+
const outcome = this.outbox.get(key);
|
|
727
|
+
if (!outcome ||
|
|
728
|
+
typeof fingerprint !== "string" ||
|
|
729
|
+
fingerprint !== outcome.fingerprint ||
|
|
730
|
+
status !== outcome.status ||
|
|
731
|
+
artifactDigest !== outcome.artifact?.digest)
|
|
732
|
+
return false;
|
|
733
|
+
const next = [
|
|
734
|
+
...this.quarantine,
|
|
735
|
+
{
|
|
736
|
+
outcome: structuredClone(outcome),
|
|
737
|
+
reasonCode: reasonCode,
|
|
738
|
+
quarantinedAt: Date.now(),
|
|
739
|
+
},
|
|
740
|
+
];
|
|
741
|
+
if (next.length > TASK_QUARANTINE_LIMITS.maxEntries ||
|
|
742
|
+
Buffer.byteLength(JSON.stringify(next)) > TASK_QUARANTINE_LIMITS.maxTotalBytes)
|
|
743
|
+
return false;
|
|
744
|
+
this.writeStateToDiskSync({
|
|
745
|
+
version: 2,
|
|
746
|
+
deviceId: this.deviceId,
|
|
747
|
+
hubUrl: this.hubUrl,
|
|
748
|
+
outbox: [...this.otherHubOutbox, ...Array.from(this.outbox.values()).filter((e) => e.id !== key)],
|
|
749
|
+
activeLeases: [...this.otherHubLeases, ...Array.from(this.activeLeases.values())],
|
|
750
|
+
quarantine: next,
|
|
751
|
+
});
|
|
752
|
+
this.quarantine = next;
|
|
753
|
+
this.outbox.delete(key);
|
|
754
|
+
return true;
|
|
755
|
+
}
|
|
756
|
+
ackOutbox(taskId, attemptId, fingerprint, extra) {
|
|
757
|
+
const key = this.leaseKey(taskId, attemptId);
|
|
758
|
+
const entry = this.outbox.get(key);
|
|
759
|
+
if (!entry)
|
|
760
|
+
return false;
|
|
761
|
+
if (entry.fingerprint) {
|
|
762
|
+
if (!fingerprint || entry.fingerprint !== fingerprint) {
|
|
763
|
+
return false;
|
|
447
764
|
}
|
|
448
|
-
return true;
|
|
449
765
|
}
|
|
450
|
-
|
|
766
|
+
else if (fingerprint) {
|
|
767
|
+
return false;
|
|
768
|
+
}
|
|
769
|
+
if (extra?.status !== undefined && entry.status !== extra.status)
|
|
770
|
+
return false;
|
|
771
|
+
if (extra?.artifactDigest !== undefined && entry.artifact?.digest !== extra.artifactDigest) {
|
|
772
|
+
return false;
|
|
773
|
+
}
|
|
774
|
+
if (extra?.contractDigest !== undefined && entry.contractDigest !== extra.contractDigest) {
|
|
775
|
+
return false;
|
|
776
|
+
}
|
|
777
|
+
if (extra?.manifestDigest !== undefined && entry.manifestDigest !== extra.manifestDigest) {
|
|
778
|
+
return false;
|
|
779
|
+
}
|
|
780
|
+
const nextOutbox = Array.from(this.outbox.values()).filter((e) => e.id !== key);
|
|
781
|
+
const allOutbox = [...this.otherHubOutbox, ...nextOutbox];
|
|
782
|
+
const allLeases = [...this.otherHubLeases, ...Array.from(this.activeLeases.values())];
|
|
783
|
+
const state = {
|
|
784
|
+
version: 1,
|
|
785
|
+
deviceId: this.deviceId,
|
|
786
|
+
hubUrl: this.hubUrl,
|
|
787
|
+
outbox: allOutbox,
|
|
788
|
+
activeLeases: allLeases,
|
|
789
|
+
};
|
|
790
|
+
this.writeStateToDiskSync(state);
|
|
791
|
+
this.outbox.delete(key);
|
|
792
|
+
return true;
|
|
451
793
|
}
|
|
452
794
|
}
|