berry-agent 0.1.0-alpha.1
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 +21 -0
- package/README.md +96 -0
- package/dist/.api-emit.stamp +1 -0
- package/dist/.build-meta.json +6 -0
- package/dist/agent/events.js +1 -0
- package/dist/agent/index.js +2 -0
- package/dist/agent/loop.js +139 -0
- package/dist/agent/queue.js +119 -0
- package/dist/agent/stream.js +62 -0
- package/dist/agent/tools-batch.js +221 -0
- package/dist/agent/types.js +8 -0
- package/dist/api/berry-agent-llm.d.ts +18 -0
- package/dist/api/berry-agent.d.ts +9 -0
- package/dist/api/surface.json +1632 -0
- package/dist/api/tsconfig.paths.json +12 -0
- package/dist/api/typebox-compile.d.ts +2 -0
- package/dist/api/typebox-value.d.ts +2 -0
- package/dist/api/typebox.d.ts +2 -0
- package/dist/browser/cdp.js +256 -0
- package/dist/browser/codes.js +31 -0
- package/dist/browser/discover.js +107 -0
- package/dist/browser/engine.js +169 -0
- package/dist/browser/index.js +17 -0
- package/dist/browser/install.js +342 -0
- package/dist/browser/page.js +321 -0
- package/dist/browser/service.js +158 -0
- package/dist/browser/tools.js +198 -0
- package/dist/browser/types.js +81 -0
- package/dist/channels/ask-queue.js +82 -0
- package/dist/channels/commands.js +129 -0
- package/dist/channels/engine/cell.js +143 -0
- package/dist/channels/engine/diff.js +143 -0
- package/dist/channels/engine/engine.js +416 -0
- package/dist/channels/engine/index.js +8 -0
- package/dist/channels/engine/input-keys.js +86 -0
- package/dist/channels/engine/input.js +596 -0
- package/dist/channels/engine/memory-io.js +71 -0
- package/dist/channels/engine/process-io.js +38 -0
- package/dist/channels/engine/types.js +17 -0
- package/dist/channels/engine/width.js +122 -0
- package/dist/channels/index.js +15 -0
- package/dist/channels/registry.js +56 -0
- package/dist/channels/sdk/admit.js +29 -0
- package/dist/channels/sdk/backend.js +102 -0
- package/dist/channels/sdk/cursor.js +56 -0
- package/dist/channels/sdk/index.js +20 -0
- package/dist/channels/sdk/jsonl.js +208 -0
- package/dist/channels/sdk/protocol.js +24 -0
- package/dist/channels/sdk/schema.js +91 -0
- package/dist/channels/sdk/wire-core.js +446 -0
- package/dist/channels/service.js +169 -0
- package/dist/channels/tui/autocomplete/autocomplete.js +42 -0
- package/dist/channels/tui/autocomplete/file-mentions.js +106 -0
- package/dist/channels/tui/autocomplete/popup.js +148 -0
- package/dist/channels/tui/autocomplete/provider.js +7 -0
- package/dist/channels/tui/autocomplete/token.js +68 -0
- package/dist/channels/tui/backend/ansi-rows.js +211 -0
- package/dist/channels/tui/backend/main-screen.js +207 -0
- package/dist/channels/tui/backend/osc.js +91 -0
- package/dist/channels/tui/backend/transcript.js +230 -0
- package/dist/channels/tui/backend/tui-backend.js +915 -0
- package/dist/channels/tui/editor/editor-model.js +543 -0
- package/dist/channels/tui/editor/editor-view.js +140 -0
- package/dist/channels/tui/editor/editor.js +275 -0
- package/dist/channels/tui/editor/undo-stack.js +35 -0
- package/dist/channels/tui/editor/visual-lines.js +126 -0
- package/dist/channels/tui/editor/word-nav.js +106 -0
- package/dist/channels/tui/history/history-viewer.js +403 -0
- package/dist/channels/tui/index.js +3 -0
- package/dist/channels/tui/layout.js +167 -0
- package/dist/channels/tui/markdown/blocks.js +111 -0
- package/dist/channels/tui/markdown/inline.js +75 -0
- package/dist/channels/tui/markdown/markdown.js +182 -0
- package/dist/channels/tui/memory/memory-viewer.js +540 -0
- package/dist/channels/tui/overlay/alt-screen.js +105 -0
- package/dist/channels/tui/overlay/overlay.js +57 -0
- package/dist/channels/tui/overlay/select-confirm.js +156 -0
- package/dist/channels/tui/panels/todo-panel.js +54 -0
- package/dist/channels/tui/panels/tool-progress-panel.js +93 -0
- package/dist/channels/tui/scroll/scroll-view.js +254 -0
- package/dist/channels/tui/status/status-line.js +76 -0
- package/dist/channels/tui/text.js +38 -0
- package/dist/channels/tui/theme.js +41 -0
- package/dist/channels/types.js +11 -0
- package/dist/channels/ui-core.js +209 -0
- package/dist/checkpoint/capture.js +45 -0
- package/dist/checkpoint/codes.js +33 -0
- package/dist/checkpoint/command.js +82 -0
- package/dist/checkpoint/gate.js +48 -0
- package/dist/checkpoint/index.js +20 -0
- package/dist/checkpoint/restore.js +172 -0
- package/dist/checkpoint/store.js +247 -0
- package/dist/checkpoint/types.js +4 -0
- package/dist/checkpoint/walk.js +146 -0
- package/dist/compaction/ccr-tools.js +112 -0
- package/dist/compaction/ccr.js +84 -0
- package/dist/compaction/codes.js +21 -0
- package/dist/compaction/index.js +18 -0
- package/dist/compaction/policy.js +200 -0
- package/dist/compaction/service.js +422 -0
- package/dist/compaction/slots.js +121 -0
- package/dist/compaction/types.js +10 -0
- package/dist/context/codes.js +40 -0
- package/dist/context/events.js +160 -0
- package/dist/context/index.js +13 -0
- package/dist/context/logger.js +136 -0
- package/dist/context/scope.js +182 -0
- package/dist/context/workspace.js +104 -0
- package/dist/contracts/agent-events.d.ts +73 -0
- package/dist/contracts/agent-events.js +16 -0
- package/dist/contracts/api.d.ts +249 -0
- package/dist/contracts/api.js +299 -0
- package/dist/contracts/approval.d.ts +37 -0
- package/dist/contracts/approval.js +9 -0
- package/dist/contracts/env-ref.d.ts +41 -0
- package/dist/contracts/env-ref.js +32 -0
- package/dist/contracts/errors.d.ts +56 -0
- package/dist/contracts/errors.js +266 -0
- package/dist/contracts/events.d.ts +70 -0
- package/dist/contracts/events.js +329 -0
- package/dist/contracts/index.d.ts +32 -0
- package/dist/contracts/index.js +26 -0
- package/dist/contracts/llm.d.ts +275 -0
- package/dist/contracts/llm.js +12 -0
- package/dist/contracts/messages.d.ts +63 -0
- package/dist/contracts/messages.js +65 -0
- package/dist/contracts/redact.d.ts +46 -0
- package/dist/contracts/redact.js +223 -0
- package/dist/contracts/tools.d.ts +197 -0
- package/dist/contracts/tools.js +33 -0
- package/dist/contracts/types.d.ts +302 -0
- package/dist/contracts/types.js +50 -0
- package/dist/contracts/ui.d.ts +122 -0
- package/dist/contracts/ui.js +15 -0
- package/dist/conversation/agent-service.js +61 -0
- package/dist/conversation/approval-wiring.js +82 -0
- package/dist/conversation/backoff.js +38 -0
- package/dist/conversation/codes.js +47 -0
- package/dist/conversation/control-tools.js +102 -0
- package/dist/conversation/control.js +210 -0
- package/dist/conversation/driver.js +922 -0
- package/dist/conversation/index.js +12 -0
- package/dist/conversation/model-visible.js +169 -0
- package/dist/conversation/open-tools.js +126 -0
- package/dist/conversation/reseed.js +132 -0
- package/dist/conversation/sessions.js +215 -0
- package/dist/conversation/todo.js +203 -0
- package/dist/conversation/types.js +35 -0
- package/dist/conversation/wiring.js +204 -0
- package/dist/credentials/codes.js +59 -0
- package/dist/credentials/commands.js +186 -0
- package/dist/credentials/env-ref.js +34 -0
- package/dist/credentials/index.js +28 -0
- package/dist/credentials/migration.js +18 -0
- package/dist/credentials/oauth.js +266 -0
- package/dist/credentials/refresh.js +152 -0
- package/dist/credentials/secrets.js +118 -0
- package/dist/credentials/types.js +30 -0
- package/dist/exec/bash.js +279 -0
- package/dist/exec/codes.js +37 -0
- package/dist/exec/env.js +99 -0
- package/dist/exec/environment.js +39 -0
- package/dist/exec/git-guard.js +623 -0
- package/dist/exec/index.js +24 -0
- package/dist/exec/registry.js +120 -0
- package/dist/exec/spawn.js +281 -0
- package/dist/exec/tail.js +77 -0
- package/dist/exec/types.js +5 -0
- package/dist/goal/codes.js +33 -0
- package/dist/goal/command.js +175 -0
- package/dist/goal/fold.js +121 -0
- package/dist/goal/gates.js +122 -0
- package/dist/goal/index.js +22 -0
- package/dist/goal/migration.js +48 -0
- package/dist/goal/service.js +501 -0
- package/dist/goal/todo-tool.js +174 -0
- package/dist/goal/types.js +1 -0
- package/dist/goal/update-tool.js +46 -0
- package/dist/host/approval-cmd.js +276 -0
- package/dist/host/assembly.js +1146 -0
- package/dist/host/boot-failures.js +104 -0
- package/dist/host/budget-advisory.js +74 -0
- package/dist/host/budget-broadcast.js +50 -0
- package/dist/host/builtins.js +54 -0
- package/dist/host/cli.js +544 -0
- package/dist/host/codes.js +120 -0
- package/dist/host/config-schema.js +195 -0
- package/dist/host/conversation-stack.js +654 -0
- package/dist/host/core-plugins.js +1685 -0
- package/dist/host/credentials-cmd.js +42 -0
- package/dist/host/disclosure.js +30 -0
- package/dist/host/dispatch.js +105 -0
- package/dist/host/doors-cmd.js +154 -0
- package/dist/host/dump-config.js +44 -0
- package/dist/host/hook-dispatch-guard.js +27 -0
- package/dist/host/import-gate.js +153 -0
- package/dist/host/index.js +54 -0
- package/dist/host/issue-session.js +241 -0
- package/dist/host/loader.js +372 -0
- package/dist/host/main.js +161 -0
- package/dist/host/manifest.js +381 -0
- package/dist/host/mcp-entry.js +53 -0
- package/dist/host/plugin-boot.js +952 -0
- package/dist/host/plugin-context.js +672 -0
- package/dist/host/plugin-install.js +480 -0
- package/dist/host/plugin-reload.js +124 -0
- package/dist/host/plugin-store.js +462 -0
- package/dist/host/plugin-tools.js +359 -0
- package/dist/host/plugin-uninstall.js +257 -0
- package/dist/host/plugins-cmd.js +302 -0
- package/dist/host/plugins-command.js +130 -0
- package/dist/host/plugins-config.js +195 -0
- package/dist/host/prompt-sections.js +133 -0
- package/dist/host/run-entry.js +544 -0
- package/dist/host/runtime.js +209 -0
- package/dist/host/scheduler-tick.js +294 -0
- package/dist/host/serve-daemon.js +501 -0
- package/dist/host/serve-entry.js +279 -0
- package/dist/host/session-anchor.js +41 -0
- package/dist/host/sessions-cmd.js +219 -0
- package/dist/host/sessions-face.js +47 -0
- package/dist/host/settings-store.js +112 -0
- package/dist/host/signals.js +69 -0
- package/dist/host/single-instance.js +105 -0
- package/dist/host/subagent-factory.js +291 -0
- package/dist/host/testkit/harness.js +283 -0
- package/dist/host/testkit/index.js +14 -0
- package/dist/host/testkit/matrix.js +302 -0
- package/dist/host/tool-policy-store.js +177 -0
- package/dist/host/triggers.js +212 -0
- package/dist/host/tui-entry.js +216 -0
- package/dist/host/webui-bridge.js +165 -0
- package/dist/issue/codes.js +38 -0
- package/dist/issue/filter.js +146 -0
- package/dist/issue/github.js +116 -0
- package/dist/issue/index.js +17 -0
- package/dist/issue/mount.js +70 -0
- package/dist/issue/poll.js +58 -0
- package/dist/issue/service.js +341 -0
- package/dist/issue/tools.js +70 -0
- package/dist/issue/types.js +34 -0
- package/dist/issue/webhook.js +126 -0
- package/dist/llm/codes.js +77 -0
- package/dist/llm/complete.js +196 -0
- package/dist/llm/events.js +15 -0
- package/dist/llm/index.js +24 -0
- package/dist/llm/inflight.js +65 -0
- package/dist/llm/model-id.js +58 -0
- package/dist/llm/provider-face.d.ts +32 -0
- package/dist/llm/provider-face.js +34 -0
- package/dist/llm/recovery.js +146 -0
- package/dist/llm/runtime.js +46 -0
- package/dist/llm/stream-fn.js +147 -0
- package/dist/lsp/codes.js +31 -0
- package/dist/lsp/connection.js +147 -0
- package/dist/lsp/frame.js +110 -0
- package/dist/lsp/index.js +16 -0
- package/dist/lsp/inject.js +58 -0
- package/dist/lsp/instance.js +264 -0
- package/dist/lsp/service.js +522 -0
- package/dist/lsp/tools.js +53 -0
- package/dist/lsp/types.js +201 -0
- package/dist/mcp/bridge.js +222 -0
- package/dist/mcp/codes.js +21 -0
- package/dist/mcp/index.js +19 -0
- package/dist/mcp/jsonrpc.js +230 -0
- package/dist/mcp/service.js +125 -0
- package/dist/mcp/tools.js +145 -0
- package/dist/mcp/types.js +108 -0
- package/dist/memory/cite.js +131 -0
- package/dist/memory/codes.js +50 -0
- package/dist/memory/command.js +71 -0
- package/dist/memory/consolidate.js +219 -0
- package/dist/memory/cycle.js +109 -0
- package/dist/memory/dao.js +878 -0
- package/dist/memory/diff.js +238 -0
- package/dist/memory/extract.js +181 -0
- package/dist/memory/fts.js +58 -0
- package/dist/memory/index.js +42 -0
- package/dist/memory/inject.js +354 -0
- package/dist/memory/merge.js +178 -0
- package/dist/memory/migration.js +132 -0
- package/dist/memory/pollution.js +59 -0
- package/dist/memory/port.js +385 -0
- package/dist/memory/review.js +201 -0
- package/dist/memory/scan.js +89 -0
- package/dist/memory/tools.js +508 -0
- package/dist/memory/types.js +140 -0
- package/dist/obs/codes.js +38 -0
- package/dist/obs/db.js +115 -0
- package/dist/obs/index.js +20 -0
- package/dist/obs/rollup.js +61 -0
- package/dist/obs/service.js +363 -0
- package/dist/obs/session-tools.js +282 -0
- package/dist/obs/session-view.js +225 -0
- package/dist/obs/tool.js +97 -0
- package/dist/obs/types.js +1 -0
- package/dist/persist/audit.js +63 -0
- package/dist/persist/aux.js +39 -0
- package/dist/persist/codes.js +35 -0
- package/dist/persist/index.js +30 -0
- package/dist/persist/load-history.js +50 -0
- package/dist/persist/migrations.js +37 -0
- package/dist/persist/paths.js +108 -0
- package/dist/persist/persistence.js +229 -0
- package/dist/persist/schema.js +99 -0
- package/dist/persist/secret-box.js +95 -0
- package/dist/persist/store.js +706 -0
- package/dist/persist/write-behind.js +238 -0
- package/dist/safety/approval.js +171 -0
- package/dist/safety/bwrap.js +123 -0
- package/dist/safety/codes.js +57 -0
- package/dist/safety/danger.js +505 -0
- package/dist/safety/gate.js +226 -0
- package/dist/safety/index.js +24 -0
- package/dist/safety/presets.js +73 -0
- package/dist/safety/roots.js +139 -0
- package/dist/safety/sandbox.js +214 -0
- package/dist/safety/seatbelt.js +88 -0
- package/dist/safety/sensitive.js +34 -0
- package/dist/safety/tool-policy.js +203 -0
- package/dist/safety/types.js +13 -0
- package/dist/scheduler/codes.js +54 -0
- package/dist/scheduler/cron-backend.js +135 -0
- package/dist/scheduler/engine.js +280 -0
- package/dist/scheduler/gates.js +60 -0
- package/dist/scheduler/index.js +25 -0
- package/dist/scheduler/migration.js +22 -0
- package/dist/scheduler/runner.js +171 -0
- package/dist/scheduler/schedule.js +193 -0
- package/dist/scheduler/service.js +283 -0
- package/dist/scheduler/tick.js +139 -0
- package/dist/scheduler/types.js +1 -0
- package/dist/sdk/http.js +865 -0
- package/dist/sdk/index.js +22 -0
- package/dist/sdk/mcp.js +289 -0
- package/dist/sdk/plugin-route-registry.js +126 -0
- package/dist/sdk/plugin-routes.js +149 -0
- package/dist/sdk/security.js +116 -0
- package/dist/sdk/types.js +32 -0
- package/dist/session/budget.js +159 -0
- package/dist/session/codes.js +40 -0
- package/dist/session/derive.js +180 -0
- package/dist/session/event-data.js +1 -0
- package/dist/session/fork.js +72 -0
- package/dist/session/import-gates.js +148 -0
- package/dist/session/index.js +18 -0
- package/dist/session/recover.js +103 -0
- package/dist/session/session.js +279 -0
- package/dist/session/snapshot.js +107 -0
- package/dist/skills/agents.js +242 -0
- package/dist/skills/codes.js +51 -0
- package/dist/skills/discovery.js +266 -0
- package/dist/skills/frontmatter.js +215 -0
- package/dist/skills/index.js +28 -0
- package/dist/skills/load.js +118 -0
- package/dist/skills/manage.js +201 -0
- package/dist/skills/registry.js +138 -0
- package/dist/skills/render.js +124 -0
- package/dist/skills/sections.js +171 -0
- package/dist/skills/types.js +21 -0
- package/dist/subagent/codes.js +50 -0
- package/dist/subagent/declarative.js +55 -0
- package/dist/subagent/index.js +19 -0
- package/dist/subagent/notify.js +51 -0
- package/dist/subagent/provide.js +7 -0
- package/dist/subagent/registry.js +149 -0
- package/dist/subagent/service.js +338 -0
- package/dist/subagent/surface.js +44 -0
- package/dist/subagent/tool.js +153 -0
- package/dist/subagent/types.js +25 -0
- package/dist/tools/apply-patch.js +164 -0
- package/dist/tools/codes.js +109 -0
- package/dist/tools/fs.js +501 -0
- package/dist/tools/index.js +17 -0
- package/dist/tools/observed.js +99 -0
- package/dist/tools/pipeline.js +176 -0
- package/dist/tools/protected-read.js +54 -0
- package/dist/tools/registry.js +250 -0
- package/dist/tools/search.js +451 -0
- package/dist/tools/worktree.js +327 -0
- package/dist/web/codes.js +37 -0
- package/dist/web/gate.js +43 -0
- package/dist/web/hygiene.js +195 -0
- package/dist/web/index.js +16 -0
- package/dist/web/service.js +207 -0
- package/dist/web/ssrf-guard.js +44 -0
- package/dist/web/tool.js +76 -0
- package/dist/web/types.js +10 -0
- package/dist/webui/assets/index-B-t2O8gh.js +22 -0
- package/dist/webui/assets/index-DOlgSZ9Q.css +2 -0
- package/dist/webui/index.html +14 -0
- package/dist/webui/index.js +14 -0
- package/dist/webui/server.js +549 -0
- package/dist/webui/types.js +38 -0
- package/examples/README.md +26 -0
- package/examples/minimal-code-plugin/entry.js +43 -0
- package/examples/minimal-code-plugin/package.json +12 -0
- package/examples/pure-skill-pack/package.json +14 -0
- package/examples/pure-skill-pack/skills/markdown-table/SKILL.md +32 -0
- package/package.json +86 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* host/runtime — 装配序生命周期骨架(04 篇 §1 进程模型;07 §1.1 表 #10 host 席)。
|
|
3
|
+
*
|
|
4
|
+
* 启动序:解析数据目录(memory 形豁免)→ 单活跃机占标记(先占标记再开库——
|
|
5
|
+
* 拒双开于开库之前)→ Persistence 开库(openStore 门禁序内嵌)→ 披露段组装。
|
|
6
|
+
* 退出序(04 §1 六步全序有界):① abort 置位(在飞 run 收打断信号)→
|
|
7
|
+
* ② closer 队列 drain(有界 5s——在飞子代理/子进程结算,超时强杀)→
|
|
8
|
+
* ③ write-behind flush(durable 落盘)→ ④ session_shutdown 并行有界 2s
|
|
9
|
+
* (件级收口钩子)→ ⑤ 作用域 LIFO 回卷(dispose 全序)→ ⑥ 释放活跃标记 +
|
|
10
|
+
* 关库。一步崩不阻后续步(退出序容错——落盘步永达)。SIGINT②→130 /
|
|
11
|
+
* SIGTERM 同① / crash.log 崩溃取证 = 进程编舞归 CLI 分派层(批 12c)——
|
|
12
|
+
* 本件只提供编舞本体(不 process.exit,可测)。
|
|
13
|
+
*
|
|
14
|
+
* :memory: 同构纪律(05 §6.6/07 §5):dump-config 类诊断不开真库、不动活跃
|
|
15
|
+
* 标记——memory 形走同一运行时装配入口、会话主库零落盘。
|
|
16
|
+
*/
|
|
17
|
+
import { appendFileSync, mkdirSync } from 'node:fs';
|
|
18
|
+
import { release as osRelease } from 'node:os';
|
|
19
|
+
import { join } from 'node:path';
|
|
20
|
+
import { AUDIT_MIGRATION, LOAD_GENERATIONS_MIGRATION, MEMORY_DB_PATH, Persistence, resolveDataDir, resolveDatabasePathIn, } from '../persist/index.js';
|
|
21
|
+
// core: 表族迁移声明(05 §6.4 机械聚合——声明来自插件、执行在宿主;host 行
|
|
22
|
+
// 拓扑边在册)。版本升序:scheduler v2 → goal v3 → memory v4-6·9·11 →
|
|
23
|
+
// credentials v7 → audit v8 → load-generations v10 → goal v12(audit v8 与
|
|
24
|
+
// load-generations v10 = 宿主域表——persist export-only 声明,非 core: 表族)。
|
|
25
|
+
import { MEMORY_MIGRATIONS } from '../memory/index.js';
|
|
26
|
+
import { GOAL_MIGRATION, GOAL_APPROVAL_MIGRATION } from '../goal/index.js';
|
|
27
|
+
import { SCHEDULER_MIGRATION } from '../scheduler/index.js';
|
|
28
|
+
import { CREDENTIALS_MIGRATION } from '../credentials/index.js';
|
|
29
|
+
import { collectDate, collectPlatform, renderEnvironmentDisclosure } from './disclosure.js';
|
|
30
|
+
import { acquireActiveMarker } from './single-instance.js';
|
|
31
|
+
/**
|
|
32
|
+
* 宿主迁移链尾(core: 插件表族声明 + 审计流表——05 §6.4 机械聚合单源)。
|
|
33
|
+
* sessions-cmd 读侧/维护动词开库同源复用:开库门禁按链 head 校验
|
|
34
|
+
* user_version(降级运行拒开),读侧动词若带短链开真库会被同库拒——链必须
|
|
35
|
+
* 与运行时全同。
|
|
36
|
+
*/
|
|
37
|
+
export const HOST_MIGRATION_TAIL = [
|
|
38
|
+
SCHEDULER_MIGRATION,
|
|
39
|
+
GOAL_MIGRATION,
|
|
40
|
+
// 2026-09-13 f-1 批聚合(goals 增列 write_approved 人面批准位 v12——03 §10.5
|
|
41
|
+
// f-1 定形注①;goal 表族第二条迁移,号随注册序顺延)
|
|
42
|
+
GOAL_APPROVAL_MIGRATION,
|
|
43
|
+
...MEMORY_MIGRATIONS,
|
|
44
|
+
// 2026-09-08 c-2 存储腿聚合(credentials 表 namespace 扩容 v7——03 §10.9;
|
|
45
|
+
// 声明在 core:credentials 件、执行在宿主——05 §6.4 机械聚合单源)
|
|
46
|
+
CREDENTIALS_MIGRATION,
|
|
47
|
+
// 2026-09-08 U3 落码批 U3-5 聚合(audit_events 进程级 durable 审计流 v8
|
|
48
|
+
// ——05 §9;宿主域表单写者 = 装配根,persist export-only 声明同形)
|
|
49
|
+
AUDIT_MIGRATION,
|
|
50
|
+
// 2026-09-09 装载史批 h-2 聚合(load_generations 宿主形态镜像落账面 v10
|
|
51
|
+
// ——05 §9;单写者 = 装配根〔boot 完成点 + /reload reapply 尾,h-3 接线〕,
|
|
52
|
+
// persist export-only 声明同形)
|
|
53
|
+
LOAD_GENERATIONS_MIGRATION,
|
|
54
|
+
];
|
|
55
|
+
/**
|
|
56
|
+
* 装配运行时(启动序一站式——分派层唯一入口)。
|
|
57
|
+
*
|
|
58
|
+
* 单活跃机拒入(HOST_DATA_DIR_BUSY)与开库失败(TOO_NEW/UNRECOGNIZED/密钥
|
|
59
|
+
* 不可读)都 fail-loud 抛出——半装配不留残(标记先占后开库,开库抛时标记
|
|
60
|
+
* 已释放)。
|
|
61
|
+
*/
|
|
62
|
+
export function createHostRuntime(options = {}) {
|
|
63
|
+
const memory = options.memory === true;
|
|
64
|
+
// memory 形 dataDir 两态:显式传入 = 同构诊断形(真数据目录读侧 + 主库
|
|
65
|
+
// :memory: + 不占标记——07 §5 dump-config 纪律);未传 = 纯诊断测试形
|
|
66
|
+
const dataDir = memory ? (options.dataDir ?? null) : (options.dataDir ?? defaultDataDir());
|
|
67
|
+
if (dataDir === null && !memory)
|
|
68
|
+
throw new Error('不变式破坏:非 memory 形必有数据目录');
|
|
69
|
+
// 启动序①:单活跃机占标记(memory 形豁免——不开真库不动标记)
|
|
70
|
+
let lease = null;
|
|
71
|
+
if (!memory) {
|
|
72
|
+
lease = acquireActiveMarker(dataDir);
|
|
73
|
+
}
|
|
74
|
+
// 启动序②:开库(memory 形 dbPath 哨兵直通;失败释放标记不留残)
|
|
75
|
+
let persistence;
|
|
76
|
+
try {
|
|
77
|
+
persistence = Persistence.open({
|
|
78
|
+
...(options.persistence ?? {}),
|
|
79
|
+
// 非 memory 形库路径锚定显式 dataDir(bug 回归锁位:原先只传 dataDir
|
|
80
|
+
// 而 Persistence 层 dbPath 缺省走 env 梯子——显式目录被无视,库开在
|
|
81
|
+
// env/家目录位、secret.key 与库分家;resolveDatabasePathIn 保
|
|
82
|
+
// BERRY_AGENT_DB_PATH 单文件覆盖恒赢——tier-2 不被目录锚定吞掉)
|
|
83
|
+
...(memory
|
|
84
|
+
? { dbPath: MEMORY_DB_PATH }
|
|
85
|
+
: { dbPath: resolveDatabasePathIn(dataDir), dataDir: dataDir }),
|
|
86
|
+
// 迁移链机械聚合(05 §6.4):core: 插件表族声明并入宿主单链——调用方
|
|
87
|
+
// 迁移在前、追加件按版本升序插队(scheduler v2 → goal v3 先于 memory
|
|
88
|
+
// v4-6;调用方版本须全链严格递增——19c 追加件同律校验)
|
|
89
|
+
migrations: [...(options.persistence?.migrations ?? []), ...HOST_MIGRATION_TAIL],
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
lease?.release();
|
|
94
|
+
throw err;
|
|
95
|
+
}
|
|
96
|
+
// 退出序基建:abort 柄 / closer 队列 / shutdown 钩子 / disposer 栈
|
|
97
|
+
const abortController = new AbortController();
|
|
98
|
+
const closers = [];
|
|
99
|
+
const shutdownHooks = [];
|
|
100
|
+
const disposers = [];
|
|
101
|
+
const closersMs = options.exitBudget?.closersMs ?? 5000;
|
|
102
|
+
const shutdownHooksMs = options.exitBudget?.shutdownHooksMs ?? 2000;
|
|
103
|
+
let shutDown = false;
|
|
104
|
+
const runtime = {
|
|
105
|
+
memory,
|
|
106
|
+
dataDir,
|
|
107
|
+
persistence,
|
|
108
|
+
get abortSignal() {
|
|
109
|
+
return abortController.signal;
|
|
110
|
+
},
|
|
111
|
+
disclosure: () => renderEnvironmentDisclosure({
|
|
112
|
+
platform: collectPlatform(() => osRelease(), process.platform),
|
|
113
|
+
cwd: process.cwd(),
|
|
114
|
+
date: collectDate(() => new Date()),
|
|
115
|
+
gitSummary: options.gitSummaryProvider?.() ?? null,
|
|
116
|
+
plugins: options.pluginsProvider?.() ?? null,
|
|
117
|
+
}),
|
|
118
|
+
registerCloser: (closer) => {
|
|
119
|
+
closers.push(closer);
|
|
120
|
+
},
|
|
121
|
+
registerShutdownHook: (hook) => {
|
|
122
|
+
shutdownHooks.push(hook);
|
|
123
|
+
},
|
|
124
|
+
registerDisposer: (fn) => {
|
|
125
|
+
disposers.push(fn);
|
|
126
|
+
},
|
|
127
|
+
shutdown: async () => {
|
|
128
|
+
if (shutDown)
|
|
129
|
+
return; // 幂等(SIGINT② 与优雅序并发到同一收口)
|
|
130
|
+
shutDown = true;
|
|
131
|
+
// ① abort 置位(在飞 run 收打断信号——不再收新输入)
|
|
132
|
+
abortController.abort();
|
|
133
|
+
// ② closer drain(注册序串行,有界 5s——超时强杀 = 放弃等待)
|
|
134
|
+
for (const closer of closers) {
|
|
135
|
+
try {
|
|
136
|
+
await withTimeout(Promise.resolve(closer.fn()), closersMs, `closer ${closer.label}`);
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
console.error(`[exit] closer ${closer.label} 超时或抛错(强杀继续): ${describe(err)}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// ③ write-behind flush(durable 落盘——退出序永达步)
|
|
143
|
+
try {
|
|
144
|
+
await persistence.flush();
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
console.error(`[exit] write-behind flush 抛错(继续收口): ${describe(err)}`);
|
|
148
|
+
}
|
|
149
|
+
// ④ session_shutdown 并行有界 2s(件级收口钩子)
|
|
150
|
+
await Promise.allSettled(shutdownHooks.map((hook) => withTimeout(Promise.resolve(hook()), shutdownHooksMs, 'session_shutdown 钩子').catch((err) => {
|
|
151
|
+
console.error(`[exit] shutdown 钩子超时或抛错(继续收口): ${describe(err)}`);
|
|
152
|
+
})));
|
|
153
|
+
// ⑤ 作用域 LIFO 回卷(dispose 全序——后注册先回卷)
|
|
154
|
+
for (const dispose of disposers.reverse()) {
|
|
155
|
+
try {
|
|
156
|
+
dispose();
|
|
157
|
+
}
|
|
158
|
+
catch (err) {
|
|
159
|
+
console.error(`[exit] disposer 抛错(继续回卷): ${describe(err)}`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// ⑥ 释放活跃标记 + 关库(close 内含 write-behind 终批落账)
|
|
163
|
+
lease?.release();
|
|
164
|
+
await persistence.close();
|
|
165
|
+
},
|
|
166
|
+
writeCrashLog: (error) => {
|
|
167
|
+
if (memory || dataDir === null)
|
|
168
|
+
return; // memory 形无真库归属地——跳过
|
|
169
|
+
appendCrashLog(dataDir, error);
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
return runtime;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* 崩溃取证直写(数据目录 crash.log 同步追加一行)。
|
|
176
|
+
*
|
|
177
|
+
* 独立于运行时导出——main 装配位在运行时尚未组装的前置窗口(解析/开库
|
|
178
|
+
* 阶段)也能取证;写失败静默(崩溃路径唯一允许——进程将退再抛无消费方)。
|
|
179
|
+
*/
|
|
180
|
+
export function appendCrashLog(dataDir, error) {
|
|
181
|
+
try {
|
|
182
|
+
mkdirSync(dataDir, { recursive: true });
|
|
183
|
+
appendFileSync(join(dataDir, 'crash.log'), `[${new Date().toISOString()}] ${describe(error)}\n`);
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
// 同上——崩溃路径唯一允许的静默
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/** 数据目录缺省解析(persist 三级梯子真源——BERRY_AGENT_DATA_DIR > ~/.berry-agent) */
|
|
190
|
+
function defaultDataDir() {
|
|
191
|
+
return resolveDataDir();
|
|
192
|
+
}
|
|
193
|
+
/** 错误速写(退出序 warn 载荷) */
|
|
194
|
+
function describe(err) {
|
|
195
|
+
return err instanceof Error ? `${err.stack ?? err.message}` : String(err);
|
|
196
|
+
}
|
|
197
|
+
/** 有界等待(超时抛——超时后底层 promise 不取消,仅放弃等待 = 强杀语义);导出共用于装载器三时钟(批 12d) */
|
|
198
|
+
export function withTimeout(p, ms, label) {
|
|
199
|
+
return new Promise((resolve, reject) => {
|
|
200
|
+
const timer = setTimeout(() => reject(new Error(`${label} 超时(${ms}ms 帽——放弃等待)`)), ms);
|
|
201
|
+
p.then(() => {
|
|
202
|
+
clearTimeout(timer);
|
|
203
|
+
resolve();
|
|
204
|
+
}, (err) => {
|
|
205
|
+
clearTimeout(timer);
|
|
206
|
+
reject(err);
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* host/scheduler-tick — 甲案进程内 tick 执行腿(u-2 G0 修复笔——04 §12
|
|
3
|
+
* 「无人值守执行链定形注」①②的码面兑现)。
|
|
4
|
+
*
|
|
5
|
+
* 定位:RunnerFactory 接缝的**进程内第二实装**(第一实装 = runner.ts 进程
|
|
6
|
+
* spawn 形,乙案 OS cron 触发腿/CLI 手动形专用)。引擎编舞(抢占/闸评估/
|
|
7
|
+
* 墙钟/settle)零改动——只换装配位注入的 runner 真身:
|
|
8
|
+
*
|
|
9
|
+
* - **进程内推进律(定形注①)**:fire 不 spawn——宿主进程内经
|
|
10
|
+
* conversation-stack 起 headless run(与 `run --read-only --tick` 同语义
|
|
11
|
+
* 链)。既有护栏全执法照常:RunLaneGate 宿主级 run 并发帽(经
|
|
12
|
+
* stack.submitText 与前台 run 同池同帽)、§5 预算闸(settle 后记后台道
|
|
13
|
+
* ——与 run-entry --background 记账律同形)、§9 审批(走宿主审批瀑布
|
|
14
|
+
* ——无人应答时 unavailable fail-closed 语义不变)。
|
|
15
|
+
* - **builtin 行程序化分派律(定形注②)**:builtin 标记行按名程序化分派
|
|
16
|
+
* 件内处理器——零模型调用零 spawn;行 prompt 字段 = 审计呈现占位
|
|
17
|
+
* (占位串不进模型面)。goal 挂钟行 → goal 件唤醒判定链、`issue-poll`
|
|
18
|
+
* → IssueService.pollOnce、`memory-review` → 件内编排入口(v1 前瞻位
|
|
19
|
+
* ——处理器缺席诚实 gated 拒)。
|
|
20
|
+
* - **墙钟守卫两形(定形注④)**:kill = stack.interrupt 协作中止
|
|
21
|
+
* (driver.abort()——乙案 kill 形归 process runner 实装,两形互不代偿)。
|
|
22
|
+
*
|
|
23
|
+
* 分派处理器 fire 时动态解析(装载序无关——goal/issue 件可后装);
|
|
24
|
+
* 处理器缺席 = 配置漂移形,gated 结局诚实拒(不动 last_fire_at——引擎
|
|
25
|
+
* settled 段按 reason==='gated' 分流 settleGated)。
|
|
26
|
+
*/
|
|
27
|
+
import { cwd as processCwd } from 'node:process';
|
|
28
|
+
import { canonicalWorkspaceRoot } from '../context/index.js';
|
|
29
|
+
/** 结局预览截断(process runner 同值——RunOutcome.finalTextPreview 帽) */
|
|
30
|
+
const PREVIEW_CAP = 200;
|
|
31
|
+
/**
|
|
32
|
+
* 进程内 tick runner 工厂。编舞三路:builtin 分派(零模型)/ goal 挂钟行
|
|
33
|
+
* (wake 判定 + 落地则提交 goal 会话)/ 用户行(到点跑其提示词)。
|
|
34
|
+
*/
|
|
35
|
+
export function createSchedulerTickRunner(deps) {
|
|
36
|
+
const now = deps.now ?? (() => new Date().toISOString());
|
|
37
|
+
const warn = deps.warn ?? ((message) => console.error(message));
|
|
38
|
+
return {
|
|
39
|
+
async spawn(req) {
|
|
40
|
+
const { row, trigger } = req;
|
|
41
|
+
// —— builtin 行:程序化分派(零模型零 spawn——占位串不进模型面) ——
|
|
42
|
+
if (row.builtin) {
|
|
43
|
+
if (row.name === 'issue-poll')
|
|
44
|
+
return spawnIssuePoll(row, trigger);
|
|
45
|
+
if (row.name.startsWith('goal-'))
|
|
46
|
+
return spawnGoalRow(row, trigger);
|
|
47
|
+
if (row.name === 'memory-review')
|
|
48
|
+
return spawnMemoryReview(row, trigger);
|
|
49
|
+
// 未知 builtin 名:行在册而分派表不识——配置漂移诚实拒(不猜处理器)
|
|
50
|
+
return settledHandle(trigger, {
|
|
51
|
+
reason: 'gated',
|
|
52
|
+
error: `未知 builtin 行「${row.name}」——分派表无此名(goal-<id>/issue-poll/memory-review 三员之外)`,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
// —— 用户行:进程内 headless run(新建会话 + 行 prompt 提交) ——
|
|
56
|
+
return spawnUserRow(row, trigger);
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
/** 一次性结局句柄(零跑形态——gated 拒/spawn 同步失败等) */
|
|
60
|
+
function settledHandle(trigger, outcome) {
|
|
61
|
+
return {
|
|
62
|
+
pid: process.pid,
|
|
63
|
+
kill: () => { }, // 已收场——kill 幂等空放
|
|
64
|
+
settled: Promise.resolve({ ...outcome, trigger, finishedAt: now() }),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/** 在飞 run 句柄铸造(kill = 协作中止;killedReason 优先折结算局——trigger 已由 settle 载入 outcome) */
|
|
68
|
+
function inflightHandle(sessionId, settle) {
|
|
69
|
+
let killedReason = null;
|
|
70
|
+
const settled = settle().then((outcome) => {
|
|
71
|
+
// kill 已先行(墙钟超时/新实例抢占)——reason 折 kill 形(process runner 同律)
|
|
72
|
+
if (killedReason !== null) {
|
|
73
|
+
return {
|
|
74
|
+
...outcome,
|
|
75
|
+
reason: killedReason,
|
|
76
|
+
exitCode: undefined,
|
|
77
|
+
error: `被中止(${killedReason === 'timeout' ? '墙钟超时' : '新实例抢占'})`,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
return outcome;
|
|
81
|
+
});
|
|
82
|
+
return {
|
|
83
|
+
pid: process.pid,
|
|
84
|
+
kill(reason) {
|
|
85
|
+
killedReason = reason;
|
|
86
|
+
if (sessionId !== null)
|
|
87
|
+
deps.stack.interrupt(sessionId); // 协作中止(driver.abort()——定形注④甲案形)
|
|
88
|
+
},
|
|
89
|
+
settled,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* 用户行编舞:新建会话 → submitText(source='schedule')→ 三终态映射 + 后台道
|
|
94
|
+
* 记账。会话终态收口(04 §12 第 5 律):用户行每次 fire 新建会话(fresh per
|
|
95
|
+
* fire)——settle 尾即 retire(dismantle + 摘登记),活体登记无界驻留收口;
|
|
96
|
+
* durable 面不受影响。goal 行复用 goal 绑定会话(广播唤醒依赖),不走此收口。
|
|
97
|
+
*/
|
|
98
|
+
function spawnUserRow(row, trigger) {
|
|
99
|
+
const workspaceRoot = canonicalWorkspaceRoot(row.cwd ?? processCwd());
|
|
100
|
+
let sessionId;
|
|
101
|
+
try {
|
|
102
|
+
sessionId = deps.stack.manager.create({ workspaceRoot }).sessionId;
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
return settledHandle(trigger, {
|
|
106
|
+
reason: 'spawn',
|
|
107
|
+
error: `会话创建失败:${err instanceof Error ? err.message : String(err)}`,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
const base = runSession(trigger, sessionId, row.prompt);
|
|
111
|
+
// settle 尾收口(finally 面——映射异常路径同样摘登记;kill 折叠序在先,
|
|
112
|
+
// retire 后 interrupt 对已摘行 = 可选链 no-op,幂等无害)
|
|
113
|
+
return {
|
|
114
|
+
...base,
|
|
115
|
+
settled: base.settled.finally(() => {
|
|
116
|
+
deps.stack.manager.retire(sessionId);
|
|
117
|
+
}),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/** goal 挂钟行编舞:wake 判定先行(零模型)——落地则 goal 会话提交 promptSnapshot。判定段(毫秒级 DB 读)先于句柄铸造,kill 不可达窗可忽略 */
|
|
121
|
+
async function spawnGoalRow(row, trigger) {
|
|
122
|
+
const goalId = row.name.slice('goal-'.length);
|
|
123
|
+
const goal = deps.resolveGoal();
|
|
124
|
+
if (goal === undefined) {
|
|
125
|
+
// goal 行在册而 goal 件未装载——配置漂移(run-entry 同款诚实拒语义)
|
|
126
|
+
warn(`[SCHEDULER_GOAL_MISSING] goal 挂钟行「${row.name}」在册而 core:goal 件未装载——配置漂移,本轮诚实拒`);
|
|
127
|
+
return settledHandle(trigger, {
|
|
128
|
+
reason: 'gated',
|
|
129
|
+
error: 'goal 件未装载——挂钟行配置漂移(wake 判定链不可用)',
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
const decision = await goal.wake(goalId, {
|
|
133
|
+
// 引擎载体归因(与 run-entry --tick 形同串——tick:<行名> 可审计)
|
|
134
|
+
trigger: 'clock',
|
|
135
|
+
attribution: `tick:${row.name}`,
|
|
136
|
+
});
|
|
137
|
+
if (!decision.landed) {
|
|
138
|
+
// 诚实零跑:不落即本轮无事(inactive/stalled/wake_budget 各由 message 说明)
|
|
139
|
+
return settledHandle(trigger, {
|
|
140
|
+
reason: 'gated',
|
|
141
|
+
error: `goal「${goalId}」本轮未唤醒(${decision.reason}):${decision.message}`,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
// 唤醒起跑前池检(u-3——04 §5 定形注③第二形态):wake 判定链只看 goal
|
|
145
|
+
// 自身(停滞/唤醒预算),日池尽在此拦——改停靠-唤醒(disable 挂钟行 +
|
|
146
|
+
// 会话落词 + 广播登记,budget_extended 恢复时同链唤醒),gated 零跑收场
|
|
147
|
+
const park = deps.resolveGoalPark?.();
|
|
148
|
+
if (park !== undefined && (await park(goalId))) {
|
|
149
|
+
return settledHandle(trigger, {
|
|
150
|
+
reason: 'gated',
|
|
151
|
+
error: `goal「${goalId}」唤醒落地而起跑前日池尽——预算停靠(挂钟停摆 + session/paused 落词,待 budget_extended 广播唤醒)`,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
// 落地:goal 绑定会话幂等开驱动后提交 promptSnapshot(与 run-entry tick 形同链)
|
|
155
|
+
try {
|
|
156
|
+
const sessionId = deps.stack.manager.open(decision.goal.sessionId).sessionId;
|
|
157
|
+
return runSession(trigger, sessionId, row.prompt);
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
return settledHandle(trigger, {
|
|
161
|
+
reason: 'spawn',
|
|
162
|
+
error: `goal 会话打开失败:${err instanceof Error ? err.message : String(err)}`,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** issue-poll 行编舞:直调 pollOnce(零模型——定形注②「占位串不进模型」正位) */
|
|
167
|
+
function spawnIssuePoll(row, trigger) {
|
|
168
|
+
void row; // 行 prompt 恒为占位——分派只认名,prompt 不消费
|
|
169
|
+
return inflightHandle(null, async () => {
|
|
170
|
+
const poller = deps.resolveIssuePoll();
|
|
171
|
+
if (poller === undefined) {
|
|
172
|
+
// issue-poll 行在册而 issue 件未装载——配置漂移(处理器缺席诚实拒)
|
|
173
|
+
warn('[SCHEDULER_POLL_MISSING] issue-poll 行在册而 core:issue 件未装载——配置漂移,本轮诚实拒');
|
|
174
|
+
return {
|
|
175
|
+
trigger,
|
|
176
|
+
reason: 'gated',
|
|
177
|
+
error: 'issue 件未装载——轮询处理器缺席(pollOnce 不可达)',
|
|
178
|
+
finishedAt: now(),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
const report = await poller.pollOnce();
|
|
183
|
+
const preview = `轮询 ${report.repos.length} 仓:候选 ${report.candidates}、入队 ${report.enqueued}、互斥 ${report.duplicates}、拒 ${report.rejected}`;
|
|
184
|
+
return {
|
|
185
|
+
trigger,
|
|
186
|
+
reason: 'exit_code',
|
|
187
|
+
exitCode: 0,
|
|
188
|
+
finalTextPreview: preview.slice(0, PREVIEW_CAP),
|
|
189
|
+
finishedAt: now(),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
catch (err) {
|
|
193
|
+
return {
|
|
194
|
+
trigger,
|
|
195
|
+
reason: 'exit_code',
|
|
196
|
+
exitCode: 1,
|
|
197
|
+
error: err instanceof Error ? err.message : String(err),
|
|
198
|
+
finishedAt: now(),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
/** memory-review 行编舞:前瞻位——编排入口装载态未提供,诚实 gated(定形注②第三员) */
|
|
204
|
+
function spawnMemoryReview(row, trigger) {
|
|
205
|
+
void row;
|
|
206
|
+
return inflightHandle(null, async () => {
|
|
207
|
+
const review = deps.resolveMemoryReview?.();
|
|
208
|
+
if (review === undefined) {
|
|
209
|
+
// v1 恒此路:件内编排入口未装载(行若在册即漂移)——零模型诚实拒
|
|
210
|
+
warn('[SCHEDULER_REVIEW_MISSING] memory-review 行在册而编排入口未装载——前瞻分派位诚实拒');
|
|
211
|
+
return {
|
|
212
|
+
trigger,
|
|
213
|
+
reason: 'gated',
|
|
214
|
+
error: 'memory-review 编排入口未装载(v1 前瞻分派位)',
|
|
215
|
+
finishedAt: now(),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
try {
|
|
219
|
+
await review();
|
|
220
|
+
return { trigger, reason: 'exit_code', exitCode: 0, finishedAt: now() };
|
|
221
|
+
}
|
|
222
|
+
catch (err) {
|
|
223
|
+
return {
|
|
224
|
+
trigger,
|
|
225
|
+
reason: 'exit_code',
|
|
226
|
+
exitCode: 1,
|
|
227
|
+
error: err instanceof Error ? err.message : String(err),
|
|
228
|
+
finishedAt: now(),
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* 会话 run 编舞共用(用户行/goal 落地形单源):起跑前 seq 锚 →
|
|
235
|
+
* submitText(source='schedule' + backgroundLane 声明——04 §5 后台道
|
|
236
|
+
* 无头 run 的 run 级预警分族判据〔2026-09-13 修复批〕)→ 三终态映射 +
|
|
237
|
+
* 后台道记账。
|
|
238
|
+
*/
|
|
239
|
+
function runSession(trigger, sessionId, prompt) {
|
|
240
|
+
const runPromise = deps.stack.submitText(sessionId, prompt, {
|
|
241
|
+
source: 'schedule',
|
|
242
|
+
backgroundLane: true,
|
|
243
|
+
});
|
|
244
|
+
if (runPromise === undefined) {
|
|
245
|
+
// 驱动缺席(open/create 与提交间被拆——理论不达防御,诚实失败)
|
|
246
|
+
return settledHandle(trigger, {
|
|
247
|
+
reason: 'spawn',
|
|
248
|
+
error: `会话 ${sessionId} 无驱动在册——提交序不可达`,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
return inflightHandle(sessionId, async () => {
|
|
252
|
+
const result = await runPromise;
|
|
253
|
+
// 后台道记账已上移组合根 settled 桥接单点(04 §5 #41/#44——本 submit
|
|
254
|
+
// 已声明 backgroundLane: true,priority 由回执声明位承载;此处再扫即双计)
|
|
255
|
+
if (result.status === 'completed') {
|
|
256
|
+
const outcome = { reason: 'exit_code', exitCode: 0, trigger, finishedAt: now() };
|
|
257
|
+
const preview = lastAssistantText(sessionId);
|
|
258
|
+
if (preview !== undefined)
|
|
259
|
+
outcome.finalTextPreview = preview.slice(0, PREVIEW_CAP);
|
|
260
|
+
return outcome;
|
|
261
|
+
}
|
|
262
|
+
if (result.status === 'failed') {
|
|
263
|
+
return {
|
|
264
|
+
reason: 'exit_code',
|
|
265
|
+
exitCode: 1,
|
|
266
|
+
error: (result.errorMessage ?? 'run 失败(无错误说明)').slice(0, PREVIEW_CAP),
|
|
267
|
+
trigger,
|
|
268
|
+
finishedAt: now(),
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
// aborted(interrupt 协作中止——kill 未先行时为外部中止形)
|
|
272
|
+
return { reason: 'killed', error: 'run 中止(协作 abort)', trigger, finishedAt: now() };
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
/** 末条 assistant 文本(text 块拼接——run-entry 同律,thinking 不入产物) */
|
|
276
|
+
function lastAssistantText(sessionId) {
|
|
277
|
+
const events = deps.stack.driverOf(sessionId)?.session.events() ?? [];
|
|
278
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
279
|
+
const ev = events[i];
|
|
280
|
+
if (ev === undefined || ev.type !== 'assistant/message')
|
|
281
|
+
continue;
|
|
282
|
+
const content = ev.data.content;
|
|
283
|
+
if (!Array.isArray(content))
|
|
284
|
+
return undefined;
|
|
285
|
+
const text = content
|
|
286
|
+
.filter((b) => b.type === 'text')
|
|
287
|
+
.map((b) => b.text)
|
|
288
|
+
.join('');
|
|
289
|
+
return text === '' ? undefined : text;
|
|
290
|
+
}
|
|
291
|
+
return undefined;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
// 计量桶映射单源已上移 llm/events.ts usageBucketsOf(04 §5 #41/#44 桥接单点化——旧扫退役)
|