@octopi-agent/engine 0.70.7 → 0.86.38
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/LICENSE +201 -201
- package/NOTICE +7 -7
- package/README.md +64 -26
- package/dist/config-schema/agent.d.ts +89 -1
- package/dist/config-schema/agent.js +69 -0
- package/dist/config-schema/context.d.ts +23 -4
- package/dist/config-schema/context.js +28 -1
- package/dist/config-schema/engine.d.ts +201 -9
- package/dist/config-schema/engine.js +31 -2
- package/dist/config-schema/governance.d.ts +95 -0
- package/dist/config-schema/governance.js +91 -0
- package/dist/config-schema/models.d.ts +1 -1
- package/dist/config-schema/observability.d.ts +6 -0
- package/dist/config-schema/observability.js +16 -0
- package/dist/config-schema/run.d.ts +10 -0
- package/dist/config-schema/run.js +18 -0
- package/dist/config-schema/shared.d.ts +10 -0
- package/dist/config-schema/shared.js +10 -0
- package/dist/config-schema/substrate.d.ts +1 -1
- package/dist/config-schema/subsystems.d.ts +1 -1
- package/dist/config-schema/subsystems.js +2 -1
- package/dist/config.d.ts +178 -7
- package/dist/config.js +113 -3
- package/dist/harness/activation/compiler.js +3 -0
- package/dist/harness/activation/runtime.d.ts +7 -0
- package/dist/harness/activation/runtime.js +60 -2
- package/dist/harness/activation/sources/agent-signal.d.ts +11 -0
- package/dist/harness/activation/sources/agent-signal.js +33 -0
- package/dist/harness/activation/types.d.ts +6 -1
- package/dist/harness/agent/builder.d.ts +47 -3
- package/dist/harness/agent/builder.js +180 -54
- package/dist/harness/agent/config-bridge.d.ts +33 -25
- package/dist/harness/agent/config-bridge.js +81 -291
- package/dist/harness/agent/index.d.ts +3 -1
- package/dist/harness/agent/index.js +2 -1
- package/dist/harness/agent/tool-purpose.d.ts +38 -0
- package/dist/harness/agent/tool-purpose.js +118 -0
- package/dist/harness/agent/wire-agent-builder.d.ts +133 -0
- package/dist/harness/agent/wire-agent-builder.js +363 -0
- package/dist/harness/collaboration/autonomous-subsystem/runtime.d.ts +4 -1
- package/dist/harness/collaboration/autonomous-subsystem/runtime.js +14 -8
- package/dist/harness/collaboration/autonomous-subsystem/sense/metrics.d.ts +12 -0
- package/dist/harness/collaboration/autonomous-subsystem/sense/metrics.js +19 -0
- package/dist/harness/collaboration/autonomous-subsystem/sense/session-lifecycle-bridge.d.ts +6 -0
- package/dist/harness/collaboration/autonomous-subsystem/sense/session-lifecycle-bridge.js +24 -7
- package/dist/harness/collaboration/autonomous-subsystem/think/executor.d.ts +5 -2
- package/dist/harness/collaboration/autonomous-subsystem/think/executor.js +11 -3
- package/dist/harness/collaboration/autonomous-subsystem/think/index.d.ts +1 -1
- package/dist/harness/collaboration/autonomous-subsystem/think/index.js +1 -1
- package/dist/harness/collaboration/autonomous-subsystem/think/llm-port.d.ts +7 -4
- package/dist/harness/collaboration/autonomous-subsystem/think/llm-port.js +8 -9
- package/dist/harness/collaboration/autonomous-subsystem/think/model-resolver.d.ts +36 -10
- package/dist/harness/collaboration/autonomous-subsystem/think/model-resolver.js +60 -7
- package/dist/harness/collaboration/delegation/child-run.d.ts +144 -0
- package/dist/harness/collaboration/delegation/child-run.js +241 -0
- package/dist/harness/collaboration/delegation/index.d.ts +26 -0
- package/dist/harness/collaboration/delegation/index.js +25 -0
- package/dist/harness/collaboration/delegation/notifier.d.ts +46 -0
- package/dist/harness/collaboration/delegation/notifier.js +84 -0
- package/dist/harness/collaboration/delegation/registry-types.d.ts +200 -0
- package/dist/harness/collaboration/delegation/registry-types.js +61 -0
- package/dist/harness/collaboration/delegation/registry.d.ts +110 -0
- package/dist/harness/collaboration/delegation/registry.js +220 -0
- package/dist/harness/collaboration/delegation/service.d.ts +249 -0
- package/dist/harness/collaboration/delegation/service.js +499 -0
- package/dist/harness/collaboration/delegation/store.d.ts +155 -0
- package/dist/harness/collaboration/delegation/store.js +271 -0
- package/dist/harness/collaboration/delegation/transport.d.ts +73 -0
- package/dist/harness/collaboration/delegation/transport.js +61 -0
- package/dist/harness/collaboration/delegation/types.d.ts +231 -0
- package/dist/harness/collaboration/delegation/types.js +70 -0
- package/dist/harness/collaboration/delegation/wiring.d.ts +111 -0
- package/dist/harness/collaboration/delegation/wiring.js +145 -0
- package/dist/harness/collaboration/orchestration/index.d.ts +11 -13
- package/dist/harness/collaboration/orchestration/index.js +11 -11
- package/dist/harness/context/assembler.d.ts +2 -2
- package/dist/harness/context/assembler.js +12 -7
- package/dist/harness/context/capabilities/compact/engine.js +80 -38
- package/dist/harness/context/capabilities/compact/types.d.ts +12 -1
- package/dist/harness/context/capabilities/summary/registry.js +10 -0
- package/dist/harness/context/constitution/default-agents.md +230 -228
- package/dist/harness/context/default-context-engine.d.ts +101 -23
- package/dist/harness/context/default-context-engine.js +483 -172
- package/dist/harness/context/hybrid-compressor.d.ts +3 -3
- package/dist/harness/context/hybrid-compressor.js +18 -23
- package/dist/harness/context/index.d.ts +5 -1
- package/dist/harness/context/index.js +2 -0
- package/dist/harness/context/layer-snapshot.d.ts +4 -0
- package/dist/harness/context/layer-snapshot.js +30 -18
- package/dist/harness/context/layer-types.d.ts +49 -1
- package/dist/harness/context/layer-types.js +72 -4
- package/dist/harness/context/layers.d.ts +5 -2
- package/dist/harness/context/layers.js +11 -5
- package/dist/harness/context/llm-summarizer.d.ts +15 -1
- package/dist/harness/context/llm-summarizer.js +107 -56
- package/dist/harness/context/message-selector.js +6 -2
- package/dist/harness/context/root-instructions.d.ts +72 -0
- package/dist/harness/context/root-instructions.js +227 -0
- package/dist/harness/context/runtime-datetime.d.ts +34 -7
- package/dist/harness/context/runtime-datetime.js +66 -10
- package/dist/harness/context/smart-router.d.ts +1 -1
- package/dist/harness/context/smart-router.js +1 -1
- package/dist/harness/context/system-prompt-assembler.d.ts +30 -0
- package/dist/harness/context/system-prompt-assembler.js +67 -6
- package/dist/harness/context/token-constants.d.ts +2 -2
- package/dist/harness/context/token-constants.js +2 -2
- package/dist/harness/context/token-estimate-fns.d.ts +1 -3
- package/dist/harness/context/token-estimate-fns.js +2 -4
- package/dist/harness/context/token-estimator.d.ts +1 -1
- package/dist/harness/context/token-estimator.js +14 -6
- package/dist/harness/context/tool-result-aging.d.ts +140 -0
- package/dist/harness/context/tool-result-aging.js +331 -0
- package/dist/harness/context/tool-result-body.d.ts +10 -0
- package/dist/harness/context/tool-result-body.js +27 -0
- package/dist/harness/context/turn-envelope.d.ts +123 -0
- package/dist/harness/context/turn-envelope.js +188 -0
- package/dist/harness/context/types.d.ts +35 -47
- package/dist/harness/extension/execution-environment/effect-policy.d.ts +248 -0
- package/dist/harness/extension/execution-environment/effect-policy.js +334 -0
- package/dist/harness/extension/execution-environment/index.d.ts +18 -2
- package/dist/harness/extension/execution-environment/index.js +11 -2
- package/dist/harness/extension/execution-environment/memory-undo.d.ts +101 -0
- package/dist/harness/extension/execution-environment/memory-undo.js +192 -0
- package/dist/harness/extension/execution-environment/process-isolation.d.ts +84 -0
- package/dist/harness/extension/execution-environment/process-isolation.js +315 -0
- package/dist/harness/extension/execution-environment/resource-limits.d.ts +36 -0
- package/dist/harness/extension/execution-environment/resource-limits.js +84 -0
- package/dist/harness/extension/execution-environment/sandbox.d.ts +125 -4
- package/dist/harness/extension/execution-environment/sandbox.js +493 -51
- package/dist/harness/extension/execution-environment/shell-journal.d.ts +100 -0
- package/dist/harness/extension/execution-environment/shell-journal.js +299 -0
- package/dist/harness/extension/execution-environment/tool-capabilities.d.ts +45 -0
- package/dist/harness/extension/execution-environment/tool-capabilities.js +126 -0
- package/dist/harness/extension/execution-environment/types.d.ts +67 -39
- package/dist/harness/extension/execution-environment/types.js +2 -2
- package/dist/harness/extension/execution-environment/undo-log.d.ts +203 -0
- package/dist/harness/extension/execution-environment/undo-log.js +627 -0
- package/dist/harness/extension/execution-environment/undo-merge.d.ts +40 -0
- package/dist/harness/extension/execution-environment/undo-merge.js +167 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/index.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/client-tools/types.d.ts +9 -0
- package/dist/harness/extension/plugin-ecosystem/mcp/bridge.js +2 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/api.d.ts +1 -6
- package/dist/harness/extension/plugin-ecosystem/plugins/api.js +3 -8
- package/dist/harness/extension/plugin-ecosystem/plugins/capability.d.ts +0 -3
- package/dist/harness/extension/plugin-ecosystem/plugins/capability.js +0 -3
- package/dist/harness/extension/plugin-ecosystem/plugins/entry.d.ts +2 -10
- package/dist/harness/extension/plugin-ecosystem/plugins/entry.js +2 -9
- package/dist/harness/extension/plugin-ecosystem/plugins/index.d.ts +1 -3
- package/dist/harness/extension/plugin-ecosystem/plugins/index.js +1 -3
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.d.ts +2 -4
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.js +4 -21
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.d.ts +3 -9
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.js +4 -8
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.d.ts +1 -4
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.js +1 -3
- package/dist/harness/extension/plugin-ecosystem/skills/manager.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/skills/manager.js +1 -1
- package/dist/harness/extension/plugin-ecosystem/tools/allowlisted-run.d.ts +45 -0
- package/dist/harness/extension/plugin-ecosystem/tools/allowlisted-run.js +136 -0
- package/dist/harness/extension/plugin-ecosystem/tools/ask-user.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.d.ts +18 -3
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +277 -81
- package/dist/harness/extension/plugin-ecosystem/tools/delegation.d.ts +31 -0
- package/dist/harness/extension/plugin-ecosystem/tools/delegation.js +291 -0
- package/dist/harness/extension/plugin-ecosystem/tools/document-tools.js +10 -2
- package/dist/harness/extension/plugin-ecosystem/tools/env-info.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/tools/eol.d.ts +35 -0
- package/dist/harness/extension/plugin-ecosystem/tools/eol.js +43 -0
- package/dist/harness/extension/plugin-ecosystem/tools/file-edit.js +40 -9
- package/dist/harness/extension/plugin-ecosystem/tools/file-ops.d.ts +33 -0
- package/dist/harness/extension/plugin-ecosystem/tools/file-ops.js +376 -0
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.d.ts +12 -2
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.js +239 -61
- package/dist/harness/extension/plugin-ecosystem/tools/git-tools.d.ts +27 -0
- package/dist/harness/extension/plugin-ecosystem/tools/git-tools.js +554 -0
- package/dist/harness/extension/plugin-ecosystem/tools/http.d.ts +10 -6
- package/dist/harness/extension/plugin-ecosystem/tools/http.js +41 -47
- package/dist/harness/extension/plugin-ecosystem/tools/index.d.ts +2 -0
- package/dist/harness/extension/plugin-ecosystem/tools/index.js +2 -0
- package/dist/harness/extension/plugin-ecosystem/tools/knowledge.js +4 -0
- package/dist/harness/extension/plugin-ecosystem/tools/memory.js +25 -1
- package/dist/harness/extension/plugin-ecosystem/tools/package-tools.d.ts +13 -0
- package/dist/harness/extension/plugin-ecosystem/tools/package-tools.js +391 -0
- package/dist/harness/extension/plugin-ecosystem/tools/platform.d.ts +51 -2
- package/dist/harness/extension/plugin-ecosystem/tools/platform.js +151 -12
- package/dist/harness/extension/plugin-ecosystem/tools/session-history.js +2 -0
- package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.d.ts +10 -1
- package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.js +35 -19
- package/dist/harness/extension/plugin-ecosystem/tools/tool-set.d.ts +8 -0
- package/dist/harness/extension/plugin-ecosystem/tools/tool-set.js +5 -0
- package/dist/harness/extension/plugin-ecosystem/tools/web-search.js +1 -0
- package/dist/harness/governance/credentials/index.d.ts +1 -1
- package/dist/harness/governance/credentials/store.d.ts +16 -4
- package/dist/harness/governance/credentials/store.js +33 -6
- package/dist/harness/governance/credentials/types.d.ts +11 -0
- package/dist/harness/governance/human-in-the-loop/decision-cache.d.ts +105 -0
- package/dist/harness/governance/human-in-the-loop/decision-cache.js +184 -0
- package/dist/harness/governance/human-in-the-loop/index.d.ts +5 -5
- package/dist/harness/governance/human-in-the-loop/index.js +3 -3
- package/dist/harness/governance/human-in-the-loop/types.d.ts +7 -62
- package/dist/harness/governance/human-in-the-loop/types.js +6 -2
- package/dist/harness/governance/security/capability-enforcer.d.ts +9 -1
- package/dist/harness/governance/security/capability-enforcer.js +31 -8
- package/dist/harness/governance/security/degradation.js +5 -7
- package/dist/harness/governance/security/risk-evaluator.d.ts +1 -1
- package/dist/harness/governance/security/risk-evaluator.js +4 -82
- package/dist/harness/governance/security/temp-path.d.ts +41 -0
- package/dist/harness/governance/security/temp-path.js +117 -0
- package/dist/harness/governance/session-acl/index.d.ts +1 -0
- package/dist/harness/governance/session-acl/index.js +1 -0
- package/dist/harness/governance/session-acl/rights.d.ts +12 -0
- package/dist/harness/governance/session-acl/rights.js +89 -7
- package/dist/harness/governance/session-acl/seed-roles.js +9 -1
- package/dist/harness/governance/session-acl/service.js +21 -29
- package/dist/harness/governance/session-acl/tool-rights.d.ts +81 -0
- package/dist/harness/governance/session-acl/tool-rights.js +238 -0
- package/dist/harness/governance/session-acl/types.d.ts +22 -0
- package/dist/harness/index.d.ts +14 -12
- package/dist/harness/index.js +9 -6
- package/dist/harness/knowledge/file-index-store.js +11 -11
- package/dist/harness/knowledge/grounding.d.ts +2 -1
- package/dist/harness/knowledge/grounding.js +6 -3
- package/dist/harness/knowledge/index.d.ts +2 -2
- package/dist/harness/knowledge/index.js +1 -1
- package/dist/harness/knowledge/ingest.js +4 -1
- package/dist/harness/knowledge/job-control-state.js +4 -4
- package/dist/harness/knowledge/job-control.js +11 -11
- package/dist/harness/knowledge/membership-store.js +11 -11
- package/dist/harness/knowledge/network-guard.d.ts +61 -4
- package/dist/harness/knowledge/network-guard.js +390 -113
- package/dist/harness/knowledge/query-service.js +3 -3
- package/dist/harness/knowledge/writer-service.d.ts +2 -0
- package/dist/harness/knowledge/writer-service.js +14 -13
- package/dist/harness/memory/backfill-trigger.d.ts +2 -2
- package/dist/harness/memory/backfill-trigger.js +4 -4
- package/dist/harness/memory/index.d.ts +1 -0
- package/dist/harness/memory/index.js +1 -0
- package/dist/harness/memory/sqlite/agent-db.js +2 -0
- package/dist/harness/memory/sqlite/memory-store.d.ts +1 -0
- package/dist/harness/memory/sqlite/memory-store.js +14 -4
- package/dist/harness/memory/sqlite/wisdom-store.js +9 -9
- package/dist/harness/memory/store.d.ts +1 -0
- package/dist/harness/memory/store.js +3 -0
- package/dist/harness/memory/time-format.d.ts +28 -0
- package/dist/harness/memory/time-format.js +52 -0
- package/dist/harness/memory/types.d.ts +12 -0
- package/dist/harness/observability/observer/hub.js +23 -1
- package/dist/harness/observability/observer/index.d.ts +2 -2
- package/dist/harness/observability/observer/index.js +1 -1
- package/dist/harness/observability/observer/types.d.ts +57 -0
- package/dist/harness/observability/observer/types.js +118 -9
- package/dist/harness/run/agent/agent.d.ts +18 -0
- package/dist/harness/run/agent/agent.js +26 -0
- package/dist/harness/run/concurrency/tool-loop-detection-core.d.ts +1 -1
- package/dist/harness/run/concurrency/tool-loop-detection-core.js +1 -1
- package/dist/harness/run/reliability/harness-events.d.ts +4 -0
- package/dist/harness/run/reliability/run-agent.js +110 -18
- package/dist/harness/run/reliability/run-metrics-collector.d.ts +28 -2
- package/dist/harness/run/reliability/run-metrics-collector.js +92 -2
- package/dist/harness/run/run-guard/default-run-guard.d.ts +0 -5
- package/dist/harness/run/run-guard/default-run-guard.js +104 -44
- package/dist/harness/run/run-scope.d.ts +52 -0
- package/dist/harness/run/runner.d.ts +101 -16
- package/dist/harness/run/runner.js +556 -158
- package/dist/harness/session/attachments/llm-intent.js +17 -17
- package/dist/harness/session/history/port.js +1 -0
- package/dist/harness/session/in-memory-store.d.ts +1 -0
- package/dist/harness/session/in-memory-store.js +22 -1
- package/dist/harness/session/schedules/index.d.ts +10 -0
- package/dist/harness/session/schedules/index.js +10 -0
- package/dist/harness/session/schedules/render.d.ts +13 -0
- package/dist/harness/session/schedules/render.js +63 -0
- package/dist/harness/session/schedules/service.d.ts +179 -0
- package/dist/harness/session/schedules/service.js +695 -0
- package/dist/harness/session/schedules/store.d.ts +119 -0
- package/dist/harness/session/schedules/store.js +323 -0
- package/dist/harness/session/schedules/tools.d.ts +39 -0
- package/dist/harness/session/schedules/tools.js +442 -0
- package/dist/harness/session/schedules/types.d.ts +104 -0
- package/dist/harness/session/schedules/types.js +97 -0
- package/dist/harness/session/tasks/tools.js +22 -0
- package/dist/harness/session/types.d.ts +35 -0
- package/dist/harness/shared/events/agent-event-map.d.ts +13 -0
- package/dist/harness/shared/types/agent-definition.d.ts +23 -0
- package/dist/index.d.ts +23 -9
- package/dist/index.js +10 -2
- package/dist/integration/providers/anthropic.d.ts +10 -0
- package/dist/integration/providers/anthropic.js +44 -1
- package/dist/integration/providers/openai.d.ts +10 -0
- package/dist/integration/providers/openai.js +58 -2
- package/dist/integration/providers/wire-dump-diff.d.ts +191 -0
- package/dist/integration/providers/wire-dump-diff.js +351 -0
- package/dist/integration/providers/wire-dump.d.ts +178 -0
- package/dist/integration/providers/wire-dump.js +396 -0
- package/dist/integration/storage/jsonl.d.ts +11 -0
- package/dist/integration/storage/jsonl.js +148 -39
- package/dist/integration/storage/session-index.js +63 -8
- package/dist/subsystems/cognition-steward/conceptualize/SUBSYSTEM.md +43 -43
- package/dist/subsystems/cognition-steward/conceptualize/config.yaml +50 -50
- package/dist/subsystems/cognition-steward/consolidate/SUBSYSTEM.md +6 -6
- package/dist/subsystems/cognition-steward/consolidate/config.yaml +54 -54
- package/dist/subsystems/memory-steward/backfill/handler.js +9 -0
- package/dist/subsystems/wisdom-steward/evaluate/config.yaml +50 -50
- package/dist/subsystems/wisdom-steward/formation/SUBSYSTEM.md +42 -42
- package/dist/subsystems/wisdom-steward/formation/config.yaml +54 -54
- package/dist/subsystems/wisdom-steward/govern/config.yaml +55 -55
- package/package.json +115 -115
- package/dist/harness/collaboration/multi-agent/agent-registry-types.d.ts +0 -144
- package/dist/harness/collaboration/multi-agent/agent-registry-types.js +0 -25
- package/dist/harness/collaboration/multi-agent/index.d.ts +0 -17
- package/dist/harness/collaboration/multi-agent/index.js +0 -15
- package/dist/harness/collaboration/multi-agent/process.d.ts +0 -167
- package/dist/harness/collaboration/multi-agent/process.js +0 -256
- package/dist/harness/collaboration/multi-agent/registry.d.ts +0 -36
- package/dist/harness/collaboration/multi-agent/registry.js +0 -137
- package/dist/harness/collaboration/multi-agent/swarm.d.ts +0 -135
- package/dist/harness/collaboration/multi-agent/swarm.js +0 -317
- package/dist/harness/collaboration/multi-agent/types.d.ts +0 -66
- package/dist/harness/collaboration/multi-agent/types.js +0 -15
- package/dist/harness/collaboration/orchestration/async-task-store.d.ts +0 -107
- package/dist/harness/collaboration/orchestration/async-task-store.js +0 -12
- package/dist/harness/collaboration/orchestration/async-task.d.ts +0 -156
- package/dist/harness/collaboration/orchestration/async-task.js +0 -359
- package/dist/harness/collaboration/orchestration/planner/hybrid-planner.d.ts +0 -47
- package/dist/harness/collaboration/orchestration/planner/hybrid-planner.js +0 -61
- package/dist/harness/collaboration/orchestration/planner/index.d.ts +0 -9
- package/dist/harness/collaboration/orchestration/planner/index.js +0 -6
- package/dist/harness/collaboration/orchestration/planner/llm-planner.d.ts +0 -54
- package/dist/harness/collaboration/orchestration/planner/llm-planner.js +0 -167
- package/dist/harness/collaboration/orchestration/planner/rule-planner.d.ts +0 -85
- package/dist/harness/collaboration/orchestration/planner/rule-planner.js +0 -175
- package/dist/harness/collaboration/orchestration/quality/classifier.d.ts +0 -21
- package/dist/harness/collaboration/orchestration/quality/classifier.js +0 -235
- package/dist/harness/collaboration/orchestration/quality/gate.d.ts +0 -29
- package/dist/harness/collaboration/orchestration/quality/gate.js +0 -409
- package/dist/harness/collaboration/orchestration/quality/index.d.ts +0 -9
- package/dist/harness/collaboration/orchestration/quality/index.js +0 -8
- package/dist/harness/collaboration/orchestration/quality/types.d.ts +0 -159
- package/dist/harness/collaboration/orchestration/quality/types.js +0 -33
- package/dist/harness/collaboration/orchestration/scheduler/index.d.ts +0 -5
- package/dist/harness/collaboration/orchestration/scheduler/index.js +0 -4
- package/dist/harness/collaboration/orchestration/scheduler/task-scheduler.d.ts +0 -143
- package/dist/harness/collaboration/orchestration/scheduler/task-scheduler.js +0 -297
- package/dist/harness/collaboration/orchestration/strategy/classifier.d.ts +0 -12
- package/dist/harness/collaboration/orchestration/strategy/classifier.js +0 -81
- package/dist/harness/collaboration/orchestration/strategy/index.d.ts +0 -3
- package/dist/harness/collaboration/orchestration/strategy/index.js +0 -2
- package/dist/harness/collaboration/orchestration/strategy/router.d.ts +0 -28
- package/dist/harness/collaboration/orchestration/strategy/router.js +0 -105
- package/dist/harness/collaboration/orchestration/strategy/types.d.ts +0 -49
- package/dist/harness/collaboration/orchestration/strategy/types.js +0 -4
- package/dist/harness/collaboration/orchestration/workflow/engine.d.ts +0 -86
- package/dist/harness/collaboration/orchestration/workflow/engine.js +0 -298
- package/dist/harness/collaboration/orchestration/workflow/index.d.ts +0 -9
- package/dist/harness/collaboration/orchestration/workflow/index.js +0 -7
- package/dist/harness/collaboration/orchestration/workflow/types.d.ts +0 -143
- package/dist/harness/collaboration/orchestration/workflow/types.js +0 -7
- package/dist/harness/governance/human-in-the-loop/approval-manager.d.ts +0 -32
- package/dist/harness/governance/human-in-the-loop/approval-manager.js +0 -93
- /package/dist/harness/collaboration/{multi-agent → delegation}/message-channel-types.d.ts +0 -0
- /package/dist/harness/collaboration/{multi-agent → delegation}/message-channel-types.js +0 -0
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn 文件 undo(ToolEffectPolicy L3)
|
|
3
|
+
*
|
|
4
|
+
* 即时生效 + 本轮 undo log:工具写立刻落盘,同时记录写前字节。
|
|
5
|
+
* 「编辑重发」discard 只逆放 agent 工具效应;用户手改保留。
|
|
6
|
+
* 落盘:`sessions/<sid>/undo/`(与 attachments 同级,不进 agent home / workspace)。
|
|
7
|
+
*
|
|
8
|
+
* 统一效应账本:可逆条目进 log.jsonl(有 before);不可逆只进 notReversed 结果。
|
|
9
|
+
* 见 arch/tool-effect-policy.md §5.6。
|
|
10
|
+
*/
|
|
11
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
12
|
+
import { appendFile as fsAppendFile, chmod, mkdir, readFile, realpath, rename, rm, stat, writeFile, } from 'node:fs/promises';
|
|
13
|
+
import { join } from 'node:path';
|
|
14
|
+
import { toSessionFileName } from '@octopi-agent/core/session-filename.js';
|
|
15
|
+
import { isProbablyText, pathKey, reverseAgentOverwrite } from './undo-merge.js';
|
|
16
|
+
import { TurnMemoryJournal, discardMemoryUndo, } from './memory-undo.js';
|
|
17
|
+
/**
|
|
18
|
+
* 解析会话 undo 目录
|
|
19
|
+
*
|
|
20
|
+
* @param sessionsDir - 通常 `OCTOPI_HOME/sessions`
|
|
21
|
+
* @param sessionId - 会话 id
|
|
22
|
+
* @returns 路径
|
|
23
|
+
*/
|
|
24
|
+
export function resolveSessionUndoPaths(sessionsDir, sessionId) {
|
|
25
|
+
const root = join(sessionsDir, toSessionFileName(sessionId), 'undo');
|
|
26
|
+
return {
|
|
27
|
+
root,
|
|
28
|
+
logPath: join(root, 'log.jsonl'),
|
|
29
|
+
blobsDir: join(root, 'blobs'),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 原子覆写文件:tmp + rename,单次完整替换。
|
|
34
|
+
* 防两类撕裂:同路径并发写交错字节、进程中途死掉留半截文件。
|
|
35
|
+
* 语义差异(已接受):rename 以新 inode 替换目标,断开既有硬链接;
|
|
36
|
+
* symlink 归一到实体(realpath)后落盘,保持 writeFile 穿透链接的语义;
|
|
37
|
+
* 既有文件的权限位经 chmod 保留;新建文件走默认 umask。
|
|
38
|
+
*/
|
|
39
|
+
async function writeFileAtomic(path, data) {
|
|
40
|
+
const target = await realpath(path).catch(() => path);
|
|
41
|
+
const mode = await stat(target).then((s) => s.mode & 0o7777).catch(() => undefined);
|
|
42
|
+
const tmp = `${target}.octopi-tmp-${process.pid}-${randomBytes(6).toString('hex')}`;
|
|
43
|
+
try {
|
|
44
|
+
await writeFile(tmp, data);
|
|
45
|
+
if (mode !== undefined)
|
|
46
|
+
await chmod(tmp, mode);
|
|
47
|
+
await rename(tmp, target);
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
// 吞掉临时文件清理失败:tmp 是无主残留,唯一后果是磁盘留垃圾;原始错误重抛
|
|
51
|
+
await rm(tmp, { force: true }).catch(() => undefined);
|
|
52
|
+
throw err;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function sha256Hex(data) {
|
|
56
|
+
return createHash('sha256').update(data).digest('hex');
|
|
57
|
+
}
|
|
58
|
+
function toBytes(data) {
|
|
59
|
+
return typeof data === 'string' ? Buffer.from(data, 'utf8') : Buffer.from(data);
|
|
60
|
+
}
|
|
61
|
+
async function pathExists(p) {
|
|
62
|
+
try {
|
|
63
|
+
await stat(p);
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** owner.json:跨进程所有权标记(清扫端据此跳过他人活跃 undo) */
|
|
71
|
+
async function writeUndoOwner(paths) {
|
|
72
|
+
try {
|
|
73
|
+
await writeFileAtomic(join(paths.root, 'owner.json'), JSON.stringify({ pid: process.pid, startedAt: Date.now() }) + '\n');
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// owner 元数据失败不影响 undo 主流程;只是清扫端的保守性降低
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* undo 根是否属于「他人仍在运行的进程」。
|
|
81
|
+
*
|
|
82
|
+
* @param paths - 会话 undo 路径
|
|
83
|
+
* @returns owner pid 存活且非本进程 → true(清扫端应跳过);无 owner / 已死 → false
|
|
84
|
+
*/
|
|
85
|
+
export async function isForeignLiveUndoOwner(paths) {
|
|
86
|
+
try {
|
|
87
|
+
const raw = await readFile(join(paths.root, 'owner.json'), 'utf8');
|
|
88
|
+
const owner = JSON.parse(raw);
|
|
89
|
+
if (typeof owner.pid !== 'number' || owner.pid === process.pid)
|
|
90
|
+
return false;
|
|
91
|
+
try {
|
|
92
|
+
process.kill(owner.pid, 0);
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
// ESRCH = 进程已死(崩溃残留,可清扫);EPERM = 活着但无权限(保守跳过)
|
|
97
|
+
return err.code === 'EPERM';
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// 无 owner(旧版本数据 / 损坏):按残留处理,不阻止清扫
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 本轮文件 undo log — 实现 `ToolFileEffects`
|
|
107
|
+
*/
|
|
108
|
+
export class TurnFileUndoLog {
|
|
109
|
+
paths;
|
|
110
|
+
entriesByPath = new Map();
|
|
111
|
+
seq = 0;
|
|
112
|
+
sealed = false;
|
|
113
|
+
/** flush 串行:并行工具写盘不得丢条目 */
|
|
114
|
+
flushChain = Promise.resolve();
|
|
115
|
+
limit;
|
|
116
|
+
blobBytes = 0;
|
|
117
|
+
limitHit = false;
|
|
118
|
+
/** 记忆面 journal(P2);与文件共用 undo 根 */
|
|
119
|
+
memoryJournal;
|
|
120
|
+
constructor(paths, limit) {
|
|
121
|
+
this.paths = paths;
|
|
122
|
+
this.limit = limit;
|
|
123
|
+
this.memoryJournal = new TurnMemoryJournal(paths.root);
|
|
124
|
+
}
|
|
125
|
+
/** ToolExecutionContext.memoryEffects */
|
|
126
|
+
get memoryEffects() {
|
|
127
|
+
return this.memoryJournal;
|
|
128
|
+
}
|
|
129
|
+
/** undoLimit 是否已截断 */
|
|
130
|
+
get truncated() {
|
|
131
|
+
return this.limitHit;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* 开始本轮 undo。若已有残留 log(上一轮中止后继续对话),先 commit。
|
|
135
|
+
*
|
|
136
|
+
* @param options - sessionsDir + sessionId + 可选 undoLimit
|
|
137
|
+
* @returns 日志实例
|
|
138
|
+
*/
|
|
139
|
+
static async begin(options) {
|
|
140
|
+
const paths = resolveSessionUndoPaths(options.sessionsDir, options.sessionId);
|
|
141
|
+
if (await pathExists(paths.root)) {
|
|
142
|
+
await rm(paths.root, { recursive: true, force: true });
|
|
143
|
+
}
|
|
144
|
+
await mkdir(paths.blobsDir, { recursive: true });
|
|
145
|
+
const log = new TurnFileUndoLog(paths, options.undoLimit);
|
|
146
|
+
await writeUndoOwner(paths);
|
|
147
|
+
await log.flushLog();
|
|
148
|
+
return log;
|
|
149
|
+
}
|
|
150
|
+
get root() {
|
|
151
|
+
return this.paths.root;
|
|
152
|
+
}
|
|
153
|
+
get size() {
|
|
154
|
+
return this.entriesByPath.size;
|
|
155
|
+
}
|
|
156
|
+
async writeFile(path, data) {
|
|
157
|
+
await this.recordWrite(path, data, 'overwrite');
|
|
158
|
+
}
|
|
159
|
+
async appendFile(path, data) {
|
|
160
|
+
await this.recordWrite(path, data, 'append');
|
|
161
|
+
}
|
|
162
|
+
/** 删除前调用:记 before,discard 时恢复 */
|
|
163
|
+
async recordDelete(path, before) {
|
|
164
|
+
if (this.sealed) {
|
|
165
|
+
throw new Error('turn file undo log already sealed');
|
|
166
|
+
}
|
|
167
|
+
const key = pathKey(path);
|
|
168
|
+
const bytes = toBytes(before);
|
|
169
|
+
const existing = this.entriesByPath.get(key);
|
|
170
|
+
if (existing) {
|
|
171
|
+
// first before 不覆盖;但必须记下「工具删了」以便 discard 恢复
|
|
172
|
+
existing.agentDeleted = true;
|
|
173
|
+
existing.afterHash = '';
|
|
174
|
+
existing.afterRef = undefined;
|
|
175
|
+
await this.flushLog();
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const beforeRef = await this.storeBlob(bytes);
|
|
179
|
+
if (beforeRef === undefined) {
|
|
180
|
+
this.limitHit = true;
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
this.seq += 1;
|
|
184
|
+
this.entriesByPath.set(key, {
|
|
185
|
+
op: 'delete',
|
|
186
|
+
path,
|
|
187
|
+
beforeRef,
|
|
188
|
+
afterHash: '',
|
|
189
|
+
agentDeleted: true,
|
|
190
|
+
seq: this.seq,
|
|
191
|
+
});
|
|
192
|
+
await this.flushLog();
|
|
193
|
+
}
|
|
194
|
+
/** 标记新建(零 before);已存在条目则不覆盖 first before */
|
|
195
|
+
async recordCreate(path, after) {
|
|
196
|
+
if (this.sealed) {
|
|
197
|
+
throw new Error('turn file undo log already sealed');
|
|
198
|
+
}
|
|
199
|
+
const key = pathKey(path);
|
|
200
|
+
if (!this.entriesByPath.has(key)) {
|
|
201
|
+
this.seq += 1;
|
|
202
|
+
this.entriesByPath.set(key, { op: 'create', path, afterHash: '', agentDeleted: false, seq: this.seq });
|
|
203
|
+
}
|
|
204
|
+
const entry = this.entriesByPath.get(key);
|
|
205
|
+
entry.agentDeleted = false;
|
|
206
|
+
if (after !== undefined) {
|
|
207
|
+
const afterBytes = toBytes(after);
|
|
208
|
+
entry.afterHash = sha256Hex(afterBytes);
|
|
209
|
+
if (isProbablyText(afterBytes)) {
|
|
210
|
+
entry.afterRef = await this.storeBlob(afterBytes);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
await this.flushLog();
|
|
214
|
+
}
|
|
215
|
+
/** rename:delete(from) + create/overwrite(to) */
|
|
216
|
+
async recordRename(from, to) {
|
|
217
|
+
const moved = await readFile(from).catch(() => undefined);
|
|
218
|
+
if (moved) {
|
|
219
|
+
await this.recordDelete(from, moved);
|
|
220
|
+
}
|
|
221
|
+
const priorTo = await readFile(to).catch(() => undefined);
|
|
222
|
+
if (priorTo) {
|
|
223
|
+
// to 已有内容:记 overwrite,discard 须恢复 to 的旧字节
|
|
224
|
+
await this.recordOverwrite(to, priorTo, moved);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
await this.recordCreate(to, moved);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/** 显式 before/after 覆盖(shell diff;磁盘已是 after 时不能用 writeFile 采 before) */
|
|
231
|
+
async noteNotReversed(payload) {
|
|
232
|
+
await mkdir(this.paths.root, { recursive: true });
|
|
233
|
+
await writeFileAtomic(join(this.paths.root, 'not-reversed.json'), JSON.stringify(payload) + '\n');
|
|
234
|
+
}
|
|
235
|
+
async recordOverwrite(path, before, after) {
|
|
236
|
+
if (this.sealed) {
|
|
237
|
+
throw new Error('turn file undo log already sealed');
|
|
238
|
+
}
|
|
239
|
+
const key = pathKey(path);
|
|
240
|
+
const beforeBytes = toBytes(before);
|
|
241
|
+
if (!this.entriesByPath.has(key)) {
|
|
242
|
+
const beforeRef = await this.storeBlob(beforeBytes);
|
|
243
|
+
if (beforeRef === undefined) {
|
|
244
|
+
this.limitHit = true;
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
this.seq += 1;
|
|
248
|
+
this.entriesByPath.set(key, {
|
|
249
|
+
op: 'overwrite',
|
|
250
|
+
path,
|
|
251
|
+
beforeRef,
|
|
252
|
+
afterHash: '',
|
|
253
|
+
agentDeleted: false,
|
|
254
|
+
seq: this.seq,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const entry = this.entriesByPath.get(key);
|
|
259
|
+
if (entry) {
|
|
260
|
+
entry.agentDeleted = false;
|
|
261
|
+
const afterBytes = after !== undefined ? toBytes(after) : await readFile(path).catch(() => undefined);
|
|
262
|
+
if (afterBytes) {
|
|
263
|
+
entry.afterHash = sha256Hex(afterBytes);
|
|
264
|
+
if (isProbablyText(afterBytes)) {
|
|
265
|
+
entry.afterRef = await this.storeBlob(afterBytes);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
await this.flushLog();
|
|
270
|
+
}
|
|
271
|
+
async recordWrite(path, data, mode) {
|
|
272
|
+
if (this.sealed) {
|
|
273
|
+
throw new Error('turn file undo log already sealed');
|
|
274
|
+
}
|
|
275
|
+
const key = pathKey(path);
|
|
276
|
+
const next = toBytes(data);
|
|
277
|
+
const existing = await readFile(path).catch(() => undefined);
|
|
278
|
+
if (!this.entriesByPath.has(key)) {
|
|
279
|
+
if (this.limit?.maxEntries !== undefined &&
|
|
280
|
+
this.limit.maxEntries > 0 &&
|
|
281
|
+
this.entriesByPath.size >= this.limit.maxEntries) {
|
|
282
|
+
this.limitHit = true;
|
|
283
|
+
await this.persistTruncated();
|
|
284
|
+
}
|
|
285
|
+
else if (existing) {
|
|
286
|
+
const beforeRef = await this.storeBlob(existing);
|
|
287
|
+
if (beforeRef === undefined) {
|
|
288
|
+
// 配额截断:业务仍写,不记 before(不可完整回滚)
|
|
289
|
+
this.limitHit = true;
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
this.seq += 1;
|
|
293
|
+
this.entriesByPath.set(key, {
|
|
294
|
+
op: 'overwrite',
|
|
295
|
+
path,
|
|
296
|
+
beforeRef,
|
|
297
|
+
afterHash: '',
|
|
298
|
+
agentDeleted: false,
|
|
299
|
+
seq: this.seq,
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
this.seq += 1;
|
|
305
|
+
this.entriesByPath.set(key, { op: 'create', path, afterHash: '', agentDeleted: false, seq: this.seq });
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
const afterBytes = mode === 'append'
|
|
309
|
+
? existing
|
|
310
|
+
? Buffer.concat([existing, next])
|
|
311
|
+
: next
|
|
312
|
+
: next;
|
|
313
|
+
const entry = this.entriesByPath.get(key);
|
|
314
|
+
if (entry) {
|
|
315
|
+
entry.agentDeleted = false;
|
|
316
|
+
entry.afterHash = sha256Hex(afterBytes);
|
|
317
|
+
// 文本 after 存一份,供 3WM;二进制只留 hash
|
|
318
|
+
if (isProbablyText(afterBytes)) {
|
|
319
|
+
entry.afterRef = await this.storeBlob(afterBytes);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (mode === 'append') {
|
|
323
|
+
await fsAppendFile(path, next);
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
await writeFileAtomic(path, next);
|
|
327
|
+
}
|
|
328
|
+
await this.flushLog();
|
|
329
|
+
}
|
|
330
|
+
/** @returns blob hash;超配额时 undefined(调用方标 truncated) */
|
|
331
|
+
async storeBlob(data) {
|
|
332
|
+
const hash = sha256Hex(data);
|
|
333
|
+
const blobPath = join(this.paths.blobsDir, hash);
|
|
334
|
+
if (await pathExists(blobPath))
|
|
335
|
+
return hash;
|
|
336
|
+
if (this.limit?.maxBlobBytes !== undefined && this.limit.maxBlobBytes > 0) {
|
|
337
|
+
if (this.blobBytes + data.length > this.limit.maxBlobBytes) {
|
|
338
|
+
this.limitHit = true;
|
|
339
|
+
await this.persistTruncated();
|
|
340
|
+
return undefined;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
try {
|
|
344
|
+
await writeFileAtomic(blobPath, data);
|
|
345
|
+
}
|
|
346
|
+
catch (err) {
|
|
347
|
+
// blobs 目录可能被外部清扫者删掉(另一进程收尾扫描误伤活 undo):
|
|
348
|
+
// 自愈重建后重试一次;仍失败才是真 IO 故障,交给调用方 fail-closed。
|
|
349
|
+
if (err.code !== 'ENOENT')
|
|
350
|
+
throw err;
|
|
351
|
+
await mkdir(this.paths.blobsDir, { recursive: true });
|
|
352
|
+
await writeFileAtomic(blobPath, data);
|
|
353
|
+
}
|
|
354
|
+
this.blobBytes += data.length;
|
|
355
|
+
return hash;
|
|
356
|
+
}
|
|
357
|
+
/** limitHit 持久化,供 gateway/close 异进程 discard 读回 */
|
|
358
|
+
async persistTruncated() {
|
|
359
|
+
try {
|
|
360
|
+
await mkdir(this.paths.root, { recursive: true });
|
|
361
|
+
await writeFileAtomic(join(this.paths.root, 'meta.json'), JSON.stringify({ truncated: true }) + '\n');
|
|
362
|
+
}
|
|
363
|
+
catch {
|
|
364
|
+
// 元数据失败不阻断业务写
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
flushLog() {
|
|
368
|
+
this.flushChain = this.flushChain.then(() => this.doFlushLog()).catch(() => undefined);
|
|
369
|
+
return this.flushChain;
|
|
370
|
+
}
|
|
371
|
+
async doFlushLog() {
|
|
372
|
+
const lines = [...this.entriesByPath.values()]
|
|
373
|
+
.sort((a, b) => a.seq - b.seq)
|
|
374
|
+
.map((e) => {
|
|
375
|
+
if (e.op === 'create') {
|
|
376
|
+
return { op: 'create', path: e.path, afterHash: e.afterHash, afterRef: e.afterRef };
|
|
377
|
+
}
|
|
378
|
+
if (e.op === 'delete' && e.agentDeleted !== false && !e.afterHash) {
|
|
379
|
+
return { op: 'delete', path: e.path, beforeRef: e.beforeRef, agentDeleted: true };
|
|
380
|
+
}
|
|
381
|
+
// delete 后又有写入:落成 overwrite + agentDeleted,避免异进程丢 after 状态
|
|
382
|
+
return {
|
|
383
|
+
op: 'overwrite',
|
|
384
|
+
path: e.path,
|
|
385
|
+
beforeRef: e.beforeRef,
|
|
386
|
+
afterHash: e.afterHash,
|
|
387
|
+
afterRef: e.afterRef,
|
|
388
|
+
agentDeleted: e.agentDeleted,
|
|
389
|
+
};
|
|
390
|
+
});
|
|
391
|
+
await mkdir(this.paths.root, { recursive: true });
|
|
392
|
+
const body = lines.map((l) => JSON.stringify(l)).join('\n');
|
|
393
|
+
await writeFileAtomic(this.paths.logPath, body + (lines.length ? '\n' : ''));
|
|
394
|
+
}
|
|
395
|
+
/** 提交:丢弃 undo,效应保留 */
|
|
396
|
+
async commit() {
|
|
397
|
+
this.sealed = true;
|
|
398
|
+
await rm(this.paths.root, { recursive: true, force: true });
|
|
399
|
+
}
|
|
400
|
+
/** 逆放并销毁(编辑重发) */
|
|
401
|
+
async discard(memoryRestorer) {
|
|
402
|
+
this.sealed = true;
|
|
403
|
+
this.memoryJournal.seal();
|
|
404
|
+
return discardFromDir(this.paths, memoryRestorer, this.limitHit);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* 未配置 sessionsDir 时的 fail-closed 写入口:拒绝落盘,禁止静默绕过 undo。
|
|
409
|
+
*/
|
|
410
|
+
export function createUnconfiguredFileEffects() {
|
|
411
|
+
const fail = async () => {
|
|
412
|
+
throw new Error('file_effects_not_configured: set SessionAwareRunnerConfig.sessionsDir to enable journaled file writes');
|
|
413
|
+
};
|
|
414
|
+
return {
|
|
415
|
+
writeFile: fail,
|
|
416
|
+
appendFile: fail,
|
|
417
|
+
recordDelete: fail,
|
|
418
|
+
recordCreate: fail,
|
|
419
|
+
recordRename: fail,
|
|
420
|
+
recordOverwrite: fail,
|
|
421
|
+
noteNotReversed: fail,
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* 未配置 sessionsDir 时的记忆面 fail-closed 写入口。
|
|
426
|
+
*/
|
|
427
|
+
export function createUnconfiguredMemoryEffects() {
|
|
428
|
+
const fail = async () => {
|
|
429
|
+
throw new Error('memory_effects_not_configured: set SessionAwareRunnerConfig.sessionsDir to enable journaled memory writes');
|
|
430
|
+
};
|
|
431
|
+
return { recordInsert: fail, recordUpdate: fail, recordDelete: fail };
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* 提交某会话残留 undo(继续对话 / turn 自然结束)
|
|
435
|
+
*
|
|
436
|
+
* @param sessionsDir - sessions 目录
|
|
437
|
+
* @param sessionId - 会话 id
|
|
438
|
+
*/
|
|
439
|
+
export async function commitSessionFileUndo(sessionsDir, sessionId) {
|
|
440
|
+
const { root } = resolveSessionUndoPaths(sessionsDir, sessionId);
|
|
441
|
+
await rm(root, { recursive: true, force: true });
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* 逆放某会话本轮文件效应(编辑重发)
|
|
445
|
+
*
|
|
446
|
+
* @param sessionsDir - sessions 目录
|
|
447
|
+
* @param sessionId - 会话 id
|
|
448
|
+
* @returns 逆放结果(宿主可见,不进 LLM 历史)
|
|
449
|
+
*/
|
|
450
|
+
export async function discardSessionFileUndo(sessionsDir, sessionId, memoryRestorer) {
|
|
451
|
+
return discardFromDir(resolveSessionUndoPaths(sessionsDir, sessionId), memoryRestorer, false);
|
|
452
|
+
}
|
|
453
|
+
async function discardFromDir(paths, memoryRestorer, limitHit) {
|
|
454
|
+
const memory = await discardMemoryUndo(paths.root, memoryRestorer);
|
|
455
|
+
const result = {
|
|
456
|
+
discarded: [],
|
|
457
|
+
keptUserEdits: [],
|
|
458
|
+
entries: 0,
|
|
459
|
+
memory,
|
|
460
|
+
};
|
|
461
|
+
// truncated 持久化在 meta.json(异进程 discard 必读)
|
|
462
|
+
try {
|
|
463
|
+
const metaRaw = await readFile(join(paths.root, 'meta.json'), 'utf8');
|
|
464
|
+
const meta = JSON.parse(metaRaw);
|
|
465
|
+
if (meta.truncated)
|
|
466
|
+
result.truncated = true;
|
|
467
|
+
}
|
|
468
|
+
catch {
|
|
469
|
+
// 无 meta
|
|
470
|
+
}
|
|
471
|
+
if (limitHit)
|
|
472
|
+
result.truncated = true;
|
|
473
|
+
// shell 等不可逆清单(§5.6:不进 log.jsonl)
|
|
474
|
+
try {
|
|
475
|
+
const nrPath = join(paths.root, 'not-reversed.json');
|
|
476
|
+
const nrRaw = await readFile(nrPath, 'utf8');
|
|
477
|
+
result.notReversed = JSON.parse(nrRaw);
|
|
478
|
+
}
|
|
479
|
+
catch {
|
|
480
|
+
// 无不可逆记录属正常
|
|
481
|
+
}
|
|
482
|
+
if (!(await pathExists(paths.logPath))) {
|
|
483
|
+
await rm(paths.root, { recursive: true, force: true });
|
|
484
|
+
return result;
|
|
485
|
+
}
|
|
486
|
+
const raw = await readFile(paths.logPath, 'utf8');
|
|
487
|
+
const entries = raw
|
|
488
|
+
.split('\n')
|
|
489
|
+
.filter((l) => l.trim())
|
|
490
|
+
.map((l) => JSON.parse(l));
|
|
491
|
+
result.entries = entries.length;
|
|
492
|
+
// 逆序:rename 的 delete+create 对须先处理 to 再处理 from
|
|
493
|
+
for (const entry of [...entries].reverse()) {
|
|
494
|
+
await revertEntry(paths, entry, result);
|
|
495
|
+
}
|
|
496
|
+
await rm(paths.root, { recursive: true, force: true });
|
|
497
|
+
return result;
|
|
498
|
+
}
|
|
499
|
+
async function revertEntry(paths, entry, result) {
|
|
500
|
+
const current = await readFile(entry.path).catch(() => undefined);
|
|
501
|
+
const currentHash = current ? sha256Hex(current) : undefined;
|
|
502
|
+
// 以**最后**工具效应为准:delete 条目在后续 write 后 agentDeleted=false
|
|
503
|
+
const agentDeleted = entry.op === 'delete'
|
|
504
|
+
? entry.agentDeleted !== false
|
|
505
|
+
: entry.op === 'overwrite' && entry.agentDeleted === true;
|
|
506
|
+
// 本轮新建:discard = 删除(用户改动则保留)
|
|
507
|
+
if (entry.op === 'create' && !('beforeRef' in entry && entry.beforeRef)) {
|
|
508
|
+
if (!current)
|
|
509
|
+
return;
|
|
510
|
+
if (currentHash !== entry.afterHash) {
|
|
511
|
+
result.keptUserEdits.push({ path: entry.path, reason: 'user_recreated' });
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
await rm(entry.path, { force: true });
|
|
515
|
+
result.discarded.push({ path: entry.path, action: 'deleted' });
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
const beforeRef = entry.op === 'create' ? undefined : entry.beforeRef;
|
|
519
|
+
if (!beforeRef)
|
|
520
|
+
return;
|
|
521
|
+
const before = await readFile(join(paths.blobsDir, beforeRef));
|
|
522
|
+
const beforeHash = sha256Hex(before);
|
|
523
|
+
if (!current) {
|
|
524
|
+
if (agentDeleted) {
|
|
525
|
+
// 工具删除 → 恢复 before
|
|
526
|
+
await writeFileAtomic(entry.path, before);
|
|
527
|
+
result.discarded.push({ path: entry.path, action: 'restored' });
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
// 用户在工具改写后删了文件:尊重用户
|
|
531
|
+
result.keptUserEdits.push({ path: entry.path, reason: 'user_deleted' });
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
if (currentHash === beforeHash)
|
|
535
|
+
return;
|
|
536
|
+
if (agentDeleted) {
|
|
537
|
+
// 工具删后文件又存在:用户重建则保留;与工具 after 一致则恢复
|
|
538
|
+
const afterHash = entry.op === 'overwrite' ? entry.afterHash : undefined;
|
|
539
|
+
if (afterHash && currentHash === afterHash) {
|
|
540
|
+
await writeFileAtomic(entry.path, before);
|
|
541
|
+
result.discarded.push({ path: entry.path, action: 'restored' });
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
result.keptUserEdits.push({ path: entry.path, reason: 'user_recreated' });
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
const entryAfterHash = entry.op === 'overwrite' || entry.op === 'create' ? entry.afterHash : '';
|
|
548
|
+
if (entryAfterHash && currentHash === entryAfterHash) {
|
|
549
|
+
await writeFileAtomic(entry.path, before);
|
|
550
|
+
result.discarded.push({ path: entry.path, action: 'restored' });
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
const afterRef = entry.op === 'create' ? entry.afterRef : entry.op === 'overwrite' ? entry.afterRef : undefined;
|
|
554
|
+
const afterHash = entry.op === 'create' ? entry.afterHash : entry.op === 'overwrite' ? entry.afterHash : '';
|
|
555
|
+
void afterHash;
|
|
556
|
+
const textOk = isProbablyText(before) && isProbablyText(current);
|
|
557
|
+
if (textOk && afterRef) {
|
|
558
|
+
const after = await readFile(join(paths.blobsDir, afterRef), 'utf8');
|
|
559
|
+
const rev = reverseAgentOverwrite(before.toString('utf8'), after, current.toString('utf8'));
|
|
560
|
+
if (rev.kind === 'restored') {
|
|
561
|
+
await writeFileAtomic(entry.path, rev.content);
|
|
562
|
+
result.discarded.push({ path: entry.path, action: 'restored' });
|
|
563
|
+
if (rev.skippedHunks > 0) {
|
|
564
|
+
result.keptUserEdits.push({ path: entry.path, reason: 'merge_conflict' });
|
|
565
|
+
}
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
result.keptUserEdits.push({ path: entry.path, reason: 'merge_conflict' });
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
result.keptUserEdits.push({ path: entry.path, reason: 'binary_user_wins' });
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* 会话关闭/删除时处理残留 undo(commit=manual 才有意义;auto 下 turn 已 commit)。
|
|
575
|
+
*
|
|
576
|
+
* @param options - sessionsDir + sessionId + onClose 策略
|
|
577
|
+
* @returns 实际动作
|
|
578
|
+
*/
|
|
579
|
+
export async function resolveSessionUndoOnClose(options) {
|
|
580
|
+
const paths = resolveSessionUndoPaths(options.sessionsDir, options.sessionId);
|
|
581
|
+
if (!(await pathExists(paths.root)))
|
|
582
|
+
return 'none';
|
|
583
|
+
if (options.skipLiveForeign && (await isForeignLiveUndoOwner(paths)))
|
|
584
|
+
return 'none';
|
|
585
|
+
if (options.onClose === 'discard') {
|
|
586
|
+
await discardSessionFileUndo(options.sessionsDir, options.sessionId, options.memoryRestorer);
|
|
587
|
+
return 'discarded';
|
|
588
|
+
}
|
|
589
|
+
await commitSessionFileUndo(options.sessionsDir, options.sessionId);
|
|
590
|
+
return 'committed';
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* 扫描会话 undo 孤儿目录(doctor 兜底)。
|
|
594
|
+
*
|
|
595
|
+
* 正常路径下 commit/discard 会删除 `sessions/<sid>/undo`;仍存在的即残留。
|
|
596
|
+
*
|
|
597
|
+
* @param sessionsDir - `OCTOPI_HOME/sessions`
|
|
598
|
+
* @returns 孤儿列表
|
|
599
|
+
*/
|
|
600
|
+
export async function scanOrphanSessionUndo(sessionsDir) {
|
|
601
|
+
const { readdir } = await import('node:fs/promises');
|
|
602
|
+
const names = await readdir(sessionsDir, { withFileTypes: true }).catch(() => []);
|
|
603
|
+
const out = [];
|
|
604
|
+
for (const ent of names) {
|
|
605
|
+
if (!ent.isDirectory())
|
|
606
|
+
continue;
|
|
607
|
+
const undoRoot = join(sessionsDir, ent.name, 'undo');
|
|
608
|
+
if (!(await pathExists(undoRoot)))
|
|
609
|
+
continue;
|
|
610
|
+
let entries = 0;
|
|
611
|
+
try {
|
|
612
|
+
const raw = await readFile(join(undoRoot, 'log.jsonl'), 'utf8');
|
|
613
|
+
entries = raw.split('\n').filter((l) => l.trim()).length;
|
|
614
|
+
}
|
|
615
|
+
catch {
|
|
616
|
+
entries = 0;
|
|
617
|
+
}
|
|
618
|
+
out.push({
|
|
619
|
+
sessionDirName: ent.name,
|
|
620
|
+
undoRoot,
|
|
621
|
+
entries,
|
|
622
|
+
hasMemory: await pathExists(join(undoRoot, 'memory.jsonl')),
|
|
623
|
+
hasNotReversed: await pathExists(join(undoRoot, 'not-reversed.json')),
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
return out;
|
|
627
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn 文件 undo 合并 — 补丁逆放(文本)/ 整文件策略(二进制)
|
|
3
|
+
*
|
|
4
|
+
* 逆放语义:把 agent 的 before→after 差分 **逆向** 打到 current 上,
|
|
5
|
+
* 只动仍与 after 对齐的行;用户改过/插入的行原样保留。
|
|
6
|
+
* 不做对称 diff3(会把「用户插行」误判成与「agent 改行」冲突)。
|
|
7
|
+
*/
|
|
8
|
+
/** 是否可按文本处理 */
|
|
9
|
+
export declare function isProbablyText(buf: Uint8Array): boolean;
|
|
10
|
+
export type ReverseOverwriteResult = {
|
|
11
|
+
kind: 'restored';
|
|
12
|
+
content: string;
|
|
13
|
+
skippedHunks: number;
|
|
14
|
+
} | {
|
|
15
|
+
kind: 'kept_current';
|
|
16
|
+
reason: 'hunk_not_found';
|
|
17
|
+
} | {
|
|
18
|
+
kind: 'conflict';
|
|
19
|
+
reason: 'too_large';
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* 逆放 agent overwrite(补丁逆放)。
|
|
23
|
+
*
|
|
24
|
+
* @param before - 写前文本
|
|
25
|
+
* @param after - 写后文本
|
|
26
|
+
* @param current - 磁盘现状文本
|
|
27
|
+
* @returns 逆放结果
|
|
28
|
+
*/
|
|
29
|
+
export declare function reverseAgentOverwrite(before: string, after: string, current: string): ReverseOverwriteResult;
|
|
30
|
+
/** 路径比较键(Windows 忽略大小写) */
|
|
31
|
+
export declare function pathKey(p: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* 保留兼容入口:补丁逆放(行级)。
|
|
34
|
+
*
|
|
35
|
+
* @param after - agent 写后
|
|
36
|
+
* @param current - 现状
|
|
37
|
+
* @param before - agent 写前
|
|
38
|
+
* @returns 合并行;失败 null
|
|
39
|
+
*/
|
|
40
|
+
export declare function merge3Lines(after: string[], current: string[], before: string[]): string[] | null;
|