@cortex-agent/server 2026.5.25 → 2026.6.11
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/defaults/config/thread-templates.json +28 -1
- package/defaults/hooks/cortex-md-injector.mjs +47 -31
- package/defaults/hooks/post-task-hook.mjs +6 -0
- package/defaults/hooks/task-status-check.mjs +8 -0
- package/defaults/mcp-config-slack.json +11 -0
- package/defaults/plugins/cortex-stage-gate/skills/task/SKILL.md +18 -0
- package/defaults/plugins/cortex-system/skills/schedule/SKILL.md +5 -6
- package/defaults/prompts/directives/manager.md +67 -0
- package/dist/agent-adapter/claude/adapter-tui.d.ts +35 -3
- package/dist/agent-adapter/claude/adapter-tui.js +91 -12
- package/dist/agent-adapter/claude/adapter.js +11 -0
- package/dist/agent-adapter/claude/defaults.d.ts +30 -0
- package/dist/agent-adapter/claude/defaults.js +30 -0
- package/dist/agent-adapter/claude/jsonl-tail.d.ts +2 -2
- package/dist/agent-adapter/claude/jsonl-tail.js +25 -20
- package/dist/agent-adapter/claude/spawn-args.d.ts +11 -0
- package/dist/agent-adapter/claude/spawn-args.js +49 -7
- package/dist/agent-adapter/claude/tmux-control.d.ts +20 -0
- package/dist/agent-adapter/claude/tmux-control.js +54 -8
- package/dist/agent-adapter/codex/adapter.js +1 -1
- package/dist/agent-adapter/normalize/event-types.d.ts +1 -0
- package/dist/agent-adapter/pi/adapter.js +46 -19
- package/dist/agent-adapter/pi/agent-dir.d.ts +21 -0
- package/dist/agent-adapter/pi/agent-dir.js +53 -2
- package/dist/agent-adapter/pi/event-parser.js +13 -1
- package/dist/agent-adapter/pi/mcp-bridge.d.ts +12 -0
- package/dist/agent-adapter/pi/mcp-bridge.js +47 -9
- package/dist/agent-adapter/pi/tool-shims.d.ts +14 -0
- package/dist/agent-adapter/pi/tool-shims.js +167 -140
- package/dist/agent-adapter/types.d.ts +7 -2
- package/dist/core/atomic-write.js +21 -0
- package/dist/core/config-generator.d.ts +10 -0
- package/dist/core/config-generator.js +28 -0
- package/dist/core/gateway-generator.d.ts +77 -7
- package/dist/core/gateway-generator.js +301 -29
- package/dist/core/icons.d.ts +68 -0
- package/dist/core/icons.js +67 -0
- package/dist/core/profile-generator.d.ts +6 -0
- package/dist/core/profile-generator.js +35 -7
- package/dist/core/running-executions.d.ts +51 -41
- package/dist/core/running-executions.js +120 -73
- package/dist/core/status-format.js +3 -2
- package/dist/core/task-parser.d.ts +3 -0
- package/dist/core/task-parser.js +3 -1
- package/dist/core/types/thread-types.d.ts +54 -8
- package/dist/domain/agents/facade.d.ts +18 -0
- package/dist/domain/agents/facade.js +26 -3
- package/dist/domain/agents/profile-manager.d.ts +11 -0
- package/dist/domain/agents/profile-manager.js +21 -2
- package/dist/domain/costs/codex-usage-monitor.js +2 -1
- package/dist/domain/costs/rate-limit-throttle.js +14 -2
- package/dist/domain/executions/registry.d.ts +24 -0
- package/dist/domain/executions/registry.js +38 -0
- package/dist/domain/mcp/core-server.js +15 -3
- package/dist/domain/mcp/feishu/bitable.d.ts +3 -0
- package/dist/domain/mcp/feishu/bitable.js +168 -0
- package/dist/domain/mcp/feishu/block-types.d.ts +44 -0
- package/dist/domain/mcp/feishu/block-types.js +127 -0
- package/dist/domain/mcp/feishu/blocks-to-text.d.ts +19 -0
- package/dist/domain/mcp/feishu/blocks-to-text.js +43 -0
- package/dist/domain/mcp/feishu/client.d.ts +46 -0
- package/dist/domain/mcp/feishu/client.js +87 -0
- package/dist/domain/mcp/feishu/docx.d.ts +3 -0
- package/dist/domain/mcp/feishu/docx.js +163 -0
- package/dist/domain/mcp/feishu/drive.d.ts +23 -0
- package/dist/domain/mcp/feishu/drive.js +66 -0
- package/dist/domain/mcp/feishu/file.d.ts +14 -0
- package/dist/domain/mcp/feishu/file.js +107 -0
- package/dist/domain/mcp/feishu/index.d.ts +6 -0
- package/dist/domain/mcp/feishu/index.js +61 -0
- package/dist/domain/mcp/feishu/markdown-to-blocks.d.ts +40 -0
- package/dist/domain/mcp/feishu/markdown-to-blocks.js +191 -0
- package/dist/domain/mcp/feishu/sheets.d.ts +3 -0
- package/dist/domain/mcp/feishu/sheets.js +119 -0
- package/dist/domain/mcp/feishu/types.d.ts +26 -0
- package/dist/domain/mcp/feishu/types.js +38 -0
- package/dist/domain/mcp/feishu/user-auth.d.ts +140 -0
- package/dist/domain/mcp/feishu/user-auth.js +286 -0
- package/dist/domain/mcp/feishu/wiki.d.ts +3 -0
- package/dist/domain/mcp/feishu/wiki.js +77 -0
- package/dist/domain/mcp/feishu-server.d.ts +2 -0
- package/dist/domain/mcp/feishu-server.js +33 -0
- package/dist/domain/mcp/server.d.ts +0 -1
- package/dist/domain/mcp/server.js +6 -25
- package/dist/domain/mcp/slack-server.d.ts +3 -0
- package/dist/domain/mcp/slack-server.js +46 -0
- package/dist/domain/mcp/tools/context.js +1 -1
- package/dist/domain/mcp/tools/schedule.d.ts +1 -1
- package/dist/domain/mcp/tools/schedule.js +6 -20
- package/dist/domain/mcp/tools/slack.js +20 -7
- package/dist/domain/mcp/tools/thread-ops.d.ts +2 -0
- package/dist/domain/mcp/tools/thread-ops.js +121 -0
- package/dist/domain/mcp/tools/time.d.ts +2 -0
- package/dist/domain/mcp/tools/time.js +66 -0
- package/dist/domain/mcp/tools/tui-ask.js +1 -1
- package/dist/domain/mcp/tools/tui-plan.js +2 -2
- package/dist/domain/mcp/tui-server.js +1 -0
- package/dist/domain/monitor/disk-monitor.js +2 -1
- package/dist/domain/monitor/gpu-monitor.js +2 -1
- package/dist/domain/remote/client-hot-reload.js +3 -2
- package/dist/domain/remote/client-manager.js +8 -6
- package/dist/domain/scheduling/job-registry.d.ts +4 -0
- package/dist/domain/scheduling/job-registry.js +1 -0
- package/dist/domain/scheduling/jobs/_shared.js +2 -1
- package/dist/domain/scheduling/jobs/memory-index-regen.js +3 -2
- package/dist/domain/scheduling/jobs/scheduled-task.js +9 -23
- package/dist/domain/scheduling/jobs/sync-public.js +4 -3
- package/dist/domain/scheduling/jobs/target-dispatch.d.ts +1 -8
- package/dist/domain/scheduling/jobs/target-dispatch.js +0 -7
- package/dist/domain/scheduling/jobs/task-archive.js +3 -2
- package/dist/domain/scheduling/jobs/task-dispatch.js +120 -10
- package/dist/domain/scheduling/schedule-command.js +16 -15
- package/dist/domain/scheduling/scheduler.d.ts +1 -1
- package/dist/domain/scheduling/scheduler.js +2 -1
- package/dist/domain/sessions/session-hooks.d.ts +2 -2
- package/dist/domain/sessions/session-hooks.js +19 -18
- package/dist/domain/sessions/session-lifecycle.d.ts +38 -0
- package/dist/domain/sessions/session-lifecycle.js +44 -0
- package/dist/domain/system/install-cli.d.ts +7 -0
- package/dist/domain/system/install-cli.js +106 -0
- package/dist/domain/system/server-update-check.js +2 -2
- package/dist/domain/tasks/dispatch-utils.d.ts +58 -1
- package/dist/domain/tasks/dispatch-utils.js +53 -4
- package/dist/domain/tasks/dispatcher.js +26 -0
- package/dist/domain/tasks/lint.js +28 -0
- package/dist/domain/tasks/mutator.d.ts +16 -3
- package/dist/domain/tasks/mutator.js +41 -12
- package/dist/domain/tasks/pending-tracker.js +6 -5
- package/dist/domain/tasks/system/task-cli.d.ts +2 -0
- package/dist/domain/tasks/system/task-cli.js +37 -6
- package/dist/domain/tasks/system/task-lifecycle-edit.js +6 -1
- package/dist/domain/tasks/system/task-mutations.d.ts +41 -3
- package/dist/domain/tasks/system/task-mutations.js +166 -8
- package/dist/domain/threads/contract.d.ts +17 -0
- package/dist/domain/threads/contract.js +59 -0
- package/dist/domain/threads/hook-runner.js +7 -6
- package/dist/domain/threads/index.d.ts +2 -0
- package/dist/domain/threads/index.js +3 -1
- package/dist/domain/threads/prompt-builder.d.ts +15 -1
- package/dist/domain/threads/prompt-builder.js +41 -2
- package/dist/domain/threads/runner.d.ts +6 -5
- package/dist/domain/threads/runner.js +128 -79
- package/dist/domain/threads/state-machine.d.ts +23 -21
- package/dist/domain/threads/state-machine.js +113 -54
- package/dist/domain/threads/template-loader.js +3 -2
- package/dist/domain/threads/tree.d.ts +45 -0
- package/dist/domain/threads/tree.js +171 -0
- package/dist/domain/tui-session/index.d.ts +2 -0
- package/dist/domain/tui-session/index.js +3 -0
- package/dist/domain/tui-session/tui-session-service.d.ts +2 -0
- package/dist/domain/tui-session/tui-session-service.js +103 -0
- package/dist/domain/tui-session/types.d.ts +67 -0
- package/dist/domain/tui-session/types.js +4 -0
- package/dist/domain/ui-service/index.d.ts +5 -0
- package/dist/domain/ui-service/index.js +3 -0
- package/dist/domain/ui-service/mutate/executions.d.ts +4 -0
- package/dist/domain/ui-service/mutate/executions.js +15 -0
- package/dist/domain/ui-service/mutate/schedules.d.ts +10 -0
- package/dist/domain/ui-service/mutate/schedules.js +39 -0
- package/dist/domain/ui-service/mutate/tasks.d.ts +23 -0
- package/dist/domain/ui-service/mutate/tasks.js +59 -0
- package/dist/domain/ui-service/mutate/threads.d.ts +4 -0
- package/dist/domain/ui-service/mutate/threads.js +21 -0
- package/dist/domain/ui-service/query/cost.d.ts +3 -0
- package/dist/domain/ui-service/query/cost.js +6 -0
- package/dist/domain/ui-service/query/executions.d.ts +2 -0
- package/dist/domain/ui-service/query/executions.js +35 -0
- package/dist/domain/ui-service/query/projects.d.ts +2 -0
- package/dist/domain/ui-service/query/projects.js +30 -0
- package/dist/domain/ui-service/query/schedules.d.ts +2 -0
- package/dist/domain/ui-service/query/schedules.js +23 -0
- package/dist/domain/ui-service/query/sessions.d.ts +2 -0
- package/dist/domain/ui-service/query/sessions.js +35 -0
- package/dist/domain/ui-service/query/tasks.d.ts +2 -0
- package/dist/domain/ui-service/query/tasks.js +30 -0
- package/dist/domain/ui-service/query/threads.d.ts +2 -0
- package/dist/domain/ui-service/query/threads.js +27 -0
- package/dist/domain/ui-service/subscribe.d.ts +5 -0
- package/dist/domain/ui-service/subscribe.js +118 -0
- package/dist/domain/ui-service/types.d.ts +237 -0
- package/dist/domain/ui-service/types.js +5 -0
- package/dist/domain/ui-service/ui-service.d.ts +2 -0
- package/dist/domain/ui-service/ui-service.js +83 -0
- package/dist/entry/app.js +94 -5
- package/dist/entry/cli.d.ts +12 -0
- package/dist/entry/cli.js +122 -12
- package/dist/entry/feishu-login.d.ts +28 -0
- package/dist/entry/feishu-login.js +218 -0
- package/dist/entry/init.d.ts +25 -2
- package/dist/entry/init.js +211 -64
- package/dist/events/event-types.d.ts +14 -0
- package/dist/hooks/cortex-md-injector.mjs +47 -31
- package/dist/hooks/post-task-hook.mjs +6 -0
- package/dist/hooks/task-status-check.mjs +8 -0
- package/dist/orchestration/agent-runner.d.ts +2 -1
- package/dist/orchestration/agent-runner.js +55 -55
- package/dist/orchestration/conduit-queue.d.ts +18 -0
- package/dist/orchestration/conduit-queue.js +32 -0
- package/dist/orchestration/conversation-runner.d.ts +48 -0
- package/dist/orchestration/conversation-runner.js +109 -0
- package/dist/orchestration/dispatch-reconciler.js +8 -1
- package/dist/orchestration/interactions/ask-user-question.d.ts +1 -1
- package/dist/orchestration/interactions/ask-user-question.js +3 -3
- package/dist/orchestration/interactions/interaction-handlers.js +48 -17
- package/dist/orchestration/interactions/plan-handler.d.ts +2 -2
- package/dist/orchestration/interactions/plan-handler.js +4 -3
- package/dist/orchestration/interactions/update-prompt.d.ts +6 -0
- package/dist/orchestration/interactions/update-prompt.js +100 -0
- package/dist/orchestration/lifecycle.d.ts +4 -4
- package/dist/orchestration/lifecycle.js +24 -22
- package/dist/orchestration/orchestrator.d.ts +1 -1
- package/dist/orchestration/routing/commands/cancel.js +39 -47
- package/dist/orchestration/routing/commands/channel.js +24 -11
- package/dist/orchestration/routing/commands/cost.js +2 -1
- package/dist/orchestration/routing/commands/device.js +3 -2
- package/dist/orchestration/routing/commands/dispatch.js +6 -5
- package/dist/orchestration/routing/commands/index.d.ts +1 -1
- package/dist/orchestration/routing/commands/index.js +11 -10
- package/dist/orchestration/routing/commands/mode.js +19 -18
- package/dist/orchestration/routing/commands/nvtop.js +10 -9
- package/dist/orchestration/routing/commands/sendfile.js +7 -6
- package/dist/orchestration/routing/commands/session.d.ts +1 -1
- package/dist/orchestration/routing/commands/session.js +17 -33
- package/dist/orchestration/routing/commands/tail.js +4 -3
- package/dist/orchestration/routing/commands/task.js +4 -3
- package/dist/orchestration/routing/commands/thread-handlers.js +16 -6
- package/dist/orchestration/routing/edit-handler.js +8 -7
- package/dist/orchestration/routing/hook-bridge-subscribers.js +17 -1
- package/dist/orchestration/routing/message-router.d.ts +1 -1
- package/dist/orchestration/routing/message-router.js +9 -9
- package/dist/orchestration/routing/webhook.js +233 -2
- package/dist/orchestration/status-helpers.d.ts +5 -1
- package/dist/orchestration/status-helpers.js +7 -6
- package/dist/orchestration/thread-callback.d.ts +67 -0
- package/dist/orchestration/thread-callback.js +465 -0
- package/dist/orchestration/thread-executor.d.ts +22 -1
- package/dist/orchestration/thread-executor.js +85 -36
- package/dist/platform/adapter.d.ts +6 -2
- package/dist/platform/adapters/composite-adapter.d.ts +89 -0
- package/dist/platform/adapters/composite-adapter.js +318 -0
- package/dist/platform/adapters/feishu-output-stream.d.ts +26 -4
- package/dist/platform/adapters/feishu-output-stream.js +274 -36
- package/dist/platform/adapters/feishu.d.ts +50 -5
- package/dist/platform/adapters/feishu.js +370 -116
- package/dist/platform/adapters/index.d.ts +24 -3
- package/dist/platform/adapters/index.js +89 -11
- package/dist/platform/adapters/project-conduits.d.ts +9 -0
- package/dist/platform/adapters/project-conduits.js +42 -0
- package/dist/platform/adapters/slack-project-conduits.d.ts +1 -9
- package/dist/platform/adapters/slack-project-conduits.js +6 -40
- package/dist/platform/adapters/slack.d.ts +9 -2
- package/dist/platform/adapters/slack.js +83 -39
- package/dist/platform/adapters/tui/index.d.ts +9 -0
- package/dist/platform/adapters/tui/index.js +10 -0
- package/dist/platform/adapters/tui/ports.d.ts +20 -0
- package/dist/platform/adapters/tui/ports.js +4 -0
- package/dist/platform/adapters/tui/tui-conduit-state.d.ts +10 -0
- package/dist/platform/adapters/tui/tui-conduit-state.js +17 -0
- package/dist/platform/adapters/tui/tui-connection.d.ts +30 -0
- package/dist/platform/adapters/tui/tui-connection.js +53 -0
- package/dist/platform/adapters/tui/tui-gateway.d.ts +120 -0
- package/dist/platform/adapters/tui/tui-gateway.js +919 -0
- package/dist/platform/adapters/tui/tui-notifications.d.ts +12 -0
- package/dist/platform/adapters/tui/tui-notifications.js +60 -0
- package/dist/platform/adapters/tui/tui-output-stream.d.ts +36 -0
- package/dist/platform/adapters/tui/tui-output-stream.js +99 -0
- package/dist/platform/adapters/tui/tui-transcript.d.ts +8 -0
- package/dist/platform/adapters/tui/tui-transcript.js +56 -0
- package/dist/platform/index.d.ts +1 -0
- package/dist/platform/index.js +2 -1
- package/dist/platform/interactive-builder.js +3 -2
- package/dist/platform/output-stream.d.ts +5 -0
- package/dist/platform/testing.d.ts +7 -11
- package/dist/platform/testing.js +18 -15
- package/dist/platform/tool-trace.js +15 -5
- package/dist/platform/tui/protocol.d.ts +355 -0
- package/dist/platform/tui/protocol.js +199 -0
- package/dist/platform/types.d.ts +12 -3
- package/dist/store/execution-repo.d.ts +6 -1
- package/dist/store/execution-repo.js +13 -3
- package/dist/store/hook-sync.d.ts +18 -0
- package/dist/store/hook-sync.js +92 -0
- package/dist/store/outbound-queue.js +2 -2
- package/dist/store/profile-repo.js +3 -2
- package/dist/store/schedule-repo.d.ts +1 -8
- package/dist/store/session-registry-repo.d.ts +9 -1
- package/dist/store/session-registry-repo.js +19 -3
- package/dist/store/session-repo.d.ts +14 -0
- package/dist/store/session-repo.js +23 -0
- package/dist/store/thread-repo.d.ts +3 -0
- package/dist/store/thread-repo.js +49 -2
- package/dist/store/version-migrations.js +95 -0
- package/dist/tui/App.d.ts +27 -0
- package/dist/tui/App.js +219 -0
- package/dist/tui/components/AskUserModal.d.ts +11 -0
- package/dist/tui/components/AskUserModal.js +364 -0
- package/dist/tui/components/ConfirmModal.d.ts +12 -0
- package/dist/tui/components/ConfirmModal.js +31 -0
- package/dist/tui/components/Dashboard.d.ts +23 -0
- package/dist/tui/components/Dashboard.js +80 -0
- package/dist/tui/components/DashboardCostTab.d.ts +7 -0
- package/dist/tui/components/DashboardCostTab.js +16 -0
- package/dist/tui/components/DashboardExecutionsTab.d.ts +11 -0
- package/dist/tui/components/DashboardExecutionsTab.js +94 -0
- package/dist/tui/components/DashboardSchedulesTab.d.ts +11 -0
- package/dist/tui/components/DashboardSchedulesTab.js +124 -0
- package/dist/tui/components/DashboardTasksTab.d.ts +12 -0
- package/dist/tui/components/DashboardTasksTab.js +200 -0
- package/dist/tui/components/DashboardThreadsTab.d.ts +11 -0
- package/dist/tui/components/DashboardThreadsTab.js +81 -0
- package/dist/tui/components/Header.d.ts +11 -0
- package/dist/tui/components/Header.js +5 -0
- package/dist/tui/components/InputBox.d.ts +10 -0
- package/dist/tui/components/InputBox.js +19 -0
- package/dist/tui/components/MessageRow.d.ts +7 -0
- package/dist/tui/components/MessageRow.js +10 -0
- package/dist/tui/components/Notifications.d.ts +19 -0
- package/dist/tui/components/Notifications.js +72 -0
- package/dist/tui/components/PlanFeedbackModal.d.ts +11 -0
- package/dist/tui/components/PlanFeedbackModal.js +161 -0
- package/dist/tui/components/ProjectSwitcher.d.ts +18 -0
- package/dist/tui/components/ProjectSwitcher.js +43 -0
- package/dist/tui/components/SessionPicker.d.ts +14 -0
- package/dist/tui/components/SessionPicker.js +34 -0
- package/dist/tui/components/SidePanel.d.ts +19 -0
- package/dist/tui/components/SidePanel.js +8 -0
- package/dist/tui/components/StatusLine.d.ts +10 -0
- package/dist/tui/components/StatusLine.js +29 -0
- package/dist/tui/components/Transcript.d.ts +13 -0
- package/dist/tui/components/Transcript.js +56 -0
- package/dist/tui/hooks/useDashboardData.d.ts +32 -0
- package/dist/tui/hooks/useDashboardData.js +104 -0
- package/dist/tui/hooks/useKeybindings.d.ts +19 -0
- package/dist/tui/hooks/useKeybindings.js +87 -0
- package/dist/tui/hooks/useMutate.d.ts +22 -0
- package/dist/tui/hooks/useMutate.js +56 -0
- package/dist/tui/hooks/useNotifications.d.ts +28 -0
- package/dist/tui/hooks/useNotifications.js +70 -0
- package/dist/tui/hooks/useTranscript.d.ts +39 -0
- package/dist/tui/hooks/useTranscript.js +265 -0
- package/dist/tui/hooks/useWsClient.d.ts +29 -0
- package/dist/tui/hooks/useWsClient.js +69 -0
- package/dist/tui/index.d.ts +2 -0
- package/dist/tui/index.js +210 -0
- package/dist/tui/logic.d.ts +16 -0
- package/dist/tui/logic.js +49 -0
- package/dist/tui/render/markdown.d.ts +10 -0
- package/dist/tui/render/markdown.js +38 -0
- package/dist/tui/render/rich-blocks.d.ts +11 -0
- package/dist/tui/render/rich-blocks.js +24 -0
- package/dist/tui/ws-client.d.ts +44 -0
- package/dist/tui/ws-client.js +157 -0
- package/package.json +11 -3
- package/dist/orchestration/channel-queue.d.ts +0 -18
- package/dist/orchestration/channel-queue.js +0 -32
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// input: thread-types (ThreadContract / ThreadRecord)
|
|
2
|
+
// output: buildContractPrompt / buildMissionChain / checkContractBudget
|
|
3
|
+
// pos: Structured delegation contracts for recursive thread spawns (DR-0014) — prompt
|
|
4
|
+
// composition, ancestor goal chain (drift prevention), per-thread budget breaker
|
|
5
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
6
|
+
/** Max characters per mission-chain entry — keeps deep-tree prompts bounded. */
|
|
7
|
+
const CHAIN_ENTRY_MAX = 120;
|
|
8
|
+
function truncate(s, max) {
|
|
9
|
+
const oneLine = s.replace(/\s+/g, ' ').trim();
|
|
10
|
+
return oneLine.length > max ? oneLine.slice(0, max) + '…' : oneLine;
|
|
11
|
+
}
|
|
12
|
+
/** Compose the structured child prompt from a delegation contract (analogous to
|
|
13
|
+
* buildDispatchPrompt for tasks). With neither contract nor mission chain, the
|
|
14
|
+
* message passes through unchanged (legacy thread_start behavior). */
|
|
15
|
+
export function buildContractPrompt(args) {
|
|
16
|
+
const { message, contract, missionChain } = args;
|
|
17
|
+
const sections = [];
|
|
18
|
+
if (missionChain?.length) {
|
|
19
|
+
sections.push('## Mission Chain\n\nWhy your work exists, from the root goal down to your parent:\n'
|
|
20
|
+
+ missionChain.map((g, i) => `${i + 1}. ${g}`).join('\n'));
|
|
21
|
+
}
|
|
22
|
+
if (contract?.goal) {
|
|
23
|
+
sections.push(`## Goal\n\n${contract.goal}`);
|
|
24
|
+
}
|
|
25
|
+
if (contract?.doneWhen) {
|
|
26
|
+
sections.push(`## Done When\n\n${contract.doneWhen}\n\nYour parent will verify the deliverable against these criteria before accepting.`);
|
|
27
|
+
}
|
|
28
|
+
if (contract?.contextFiles?.length) {
|
|
29
|
+
sections.push('## Context (read these first)\n\n' + contract.contextFiles.map(f => `- ${f}`).join('\n'));
|
|
30
|
+
}
|
|
31
|
+
if (contract?.deliverablePath) {
|
|
32
|
+
sections.push(`## Deliverable\n\nWrite your output to: ${contract.deliverablePath}`);
|
|
33
|
+
}
|
|
34
|
+
if (contract?.budgetUsd != null) {
|
|
35
|
+
sections.push(`## Budget\n\n$${contract.budgetUsd.toFixed(2)} for your subtree. Exceeding it trips the circuit breaker (no further steps / spawns).`);
|
|
36
|
+
}
|
|
37
|
+
if (sections.length === 0)
|
|
38
|
+
return message;
|
|
39
|
+
return sections.join('\n\n') + '\n\n' + message;
|
|
40
|
+
}
|
|
41
|
+
/** Ancestor goal chain for a child spawned under `parent`, root-first:
|
|
42
|
+
* the parent's own chain plus the parent's goal (contract goal, else its truncated
|
|
43
|
+
* userMessage). Injected into every contract so leaves can see the whole "why". */
|
|
44
|
+
export function buildMissionChain(parent) {
|
|
45
|
+
if (!parent)
|
|
46
|
+
return [];
|
|
47
|
+
const parentChain = (parent.metadata?.missionChain ?? []).map(e => truncate(e, CHAIN_ENTRY_MAX));
|
|
48
|
+
const parentGoal = parent.metadata?.contract?.goal ?? parent.userMessage ?? '';
|
|
49
|
+
return [...parentChain, truncate(parentGoal, CHAIN_ENTRY_MAX)];
|
|
50
|
+
}
|
|
51
|
+
/** Per-thread contract budget breaker: true when the thread has spent its contract budget.
|
|
52
|
+
* Used by checkTemplateLimits (template threads) and the runner's wait re-entry branch
|
|
53
|
+
* (ad-hoc parents, which bypass transition evaluation). */
|
|
54
|
+
export function checkContractBudget(thread) {
|
|
55
|
+
const budget = thread.metadata?.contract?.budgetUsd;
|
|
56
|
+
if (budget == null)
|
|
57
|
+
return false;
|
|
58
|
+
return (thread.totalCostUsd || 0) >= budget;
|
|
59
|
+
}
|
|
@@ -11,6 +11,7 @@ import * as executionRegistry from '../executions/registry.js';
|
|
|
11
11
|
import { sessionStore } from '../../store/session-registry-repo.js';
|
|
12
12
|
import { DATA_DIR } from '../../core/utils.js';
|
|
13
13
|
import { createLogger } from '../../core/log.js';
|
|
14
|
+
import { Icons } from '../../core/icons.js';
|
|
14
15
|
import { runningExecutions } from '../../core/running-executions.js';
|
|
15
16
|
const log = createLogger('thread-hook');
|
|
16
17
|
const DEFAULT_HOOK_TIMEOUT = 30000;
|
|
@@ -153,8 +154,8 @@ async function runHookAgent(threadId, hookResult, phase, opts) {
|
|
|
153
154
|
const slackLabel = isTargetMode ? `→ ${slotId}` : `hook:${phase}`;
|
|
154
155
|
try {
|
|
155
156
|
await opts.adapter.postMessage(opts.destination, {
|
|
156
|
-
text:
|
|
157
|
-
}, opts.
|
|
157
|
+
text: `${Icons.hook} Hook agent (*${slackLabel}*) starting...`,
|
|
158
|
+
}, opts.threadAnchorId ? { threadId: opts.threadAnchorId } : undefined);
|
|
158
159
|
}
|
|
159
160
|
catch { }
|
|
160
161
|
// Register execution (inherit metadata from thread for correct attribution)
|
|
@@ -187,16 +188,16 @@ async function runHookAgent(threadId, hookResult, phase, opts) {
|
|
|
187
188
|
onFallback: null,
|
|
188
189
|
isUserInitiated: false,
|
|
189
190
|
onAssistantMessage: (text) => {
|
|
190
|
-
opts.adapter.postMessage(opts.destination, { text }, opts.
|
|
191
|
+
opts.adapter.postMessage(opts.destination, { text }, opts.threadAnchorId ? { threadId: opts.threadAnchorId } : undefined).catch(() => { });
|
|
191
192
|
},
|
|
192
193
|
onProgress: null,
|
|
193
194
|
});
|
|
194
|
-
|
|
195
|
-
runningExecutions.register(hookHandleKey, {
|
|
195
|
+
runningExecutions.register({
|
|
196
196
|
threadId,
|
|
197
197
|
channel: opts.channel,
|
|
198
198
|
agentSlotId: slotId,
|
|
199
199
|
executionId: execution.id,
|
|
200
|
+
kind: execution.kind,
|
|
200
201
|
kill: () => handle.kill(),
|
|
201
202
|
backend: getActiveBackend(),
|
|
202
203
|
});
|
|
@@ -205,7 +206,7 @@ async function runHookAgent(threadId, hookResult, phase, opts) {
|
|
|
205
206
|
result = await handle.promise;
|
|
206
207
|
}
|
|
207
208
|
finally {
|
|
208
|
-
runningExecutions.remove(
|
|
209
|
+
runningExecutions.remove(execution.id);
|
|
209
210
|
}
|
|
210
211
|
// Record step
|
|
211
212
|
const stepEndTime = new Date().toISOString();
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// Barrel re-export for domain/threads/ — single import surface for all callers.
|
|
2
|
-
// Physical split: utils / artifact-io / template-loader / prompt-builder / state-machine
|
|
2
|
+
// Physical split: utils / artifact-io / template-loader / prompt-builder / state-machine / tree
|
|
3
3
|
export * from './utils.js';
|
|
4
4
|
export * from './artifact-io.js';
|
|
5
5
|
export * from './template-loader.js';
|
|
6
6
|
export * from './prompt-builder.js';
|
|
7
7
|
export * from './state-machine.js';
|
|
8
|
+
export * from './tree.js';
|
|
9
|
+
export * from './contract.js';
|
|
@@ -22,4 +22,18 @@ export declare function pickStepTemplate(agentConfig: AgentSlotConfig, stage: st
|
|
|
22
22
|
};
|
|
23
23
|
/** System-level protocol preamble injected into every step prompt of threads that own a workspace artifact. */
|
|
24
24
|
export declare const THREAD_PROTOCOL_PREAMBLE: string;
|
|
25
|
-
export declare function buildStepPrompt(threadId: string, agentConfig: AgentSlotConfig, stage?: string | null
|
|
25
|
+
export declare function buildStepPrompt(threadId: string, agentConfig: AgentSlotConfig, stage?: string | null): string;
|
|
26
|
+
/**
|
|
27
|
+
* Assemble the prompt for a single plain user-conversation turn — the thread-independent
|
|
28
|
+
* counterpart of buildStepPrompt. Plain user messages are NOT wrapped in a thread, so there
|
|
29
|
+
* is no thread record, artifact, previous step, or transition to consider.
|
|
30
|
+
*
|
|
31
|
+
* Fidelity with the legacy default-thread path (templateName='default', isUserInitiated=true):
|
|
32
|
+
* - applies the default agent's promptTemplate (typically `{{input}}`) with empty thread vars;
|
|
33
|
+
* - prepends the agent directive (resolved for {{systemVar}});
|
|
34
|
+
* - prepends user context ONLY under the same gate the default thread used —
|
|
35
|
+
* loadUserContext({templateName:'default'}) returns null unless CORTEX_INJECT_USER_CONTEXT=1
|
|
36
|
+
* AND the template is a direct-* template, so 'default' yields null today (behavior preserved);
|
|
37
|
+
* - NEVER injects THREAD_PROTOCOL_PREAMBLE (no artifact, no [ABORT] protocol for conversations).
|
|
38
|
+
*/
|
|
39
|
+
export declare function buildConversationPrompt(agentConfig: AgentSlotConfig, input: string): string;
|
|
@@ -110,6 +110,11 @@ export const THREAD_PROTOCOL_PREAMBLE = [
|
|
|
110
110
|
'contradictory requirements, or repeated unrecoverable failures), append `[ABORT: <reason>]` to the',
|
|
111
111
|
'artifact to terminate the thread — terminal state `aborted`, distinct from `failed`. Use this only',
|
|
112
112
|
'when truly blocked: normal retries, minor issues, or disagreements with the plan are not abort cases.',
|
|
113
|
+
'Delegation (DR-0014): you may spawn child threads via the thread_start tool (pass a contract:',
|
|
114
|
+
'goal / done_when / deliverable_path / budget_usd). After spawning awaited children, end your step',
|
|
115
|
+
'with the marker `[WAIT_CHILDREN]` to suspend; you are re-entered once ALL awaited children finish,',
|
|
116
|
+
'with their results injected. Acceptance before trust: verify each child deliverable against its',
|
|
117
|
+
'done_when yourself (read files, run tests) — never accept a child\'s self-report as evidence.',
|
|
113
118
|
].join('\n');
|
|
114
119
|
// --- Prompt assembly ---
|
|
115
120
|
function buildPromptVars(thread, lastStep) {
|
|
@@ -128,7 +133,7 @@ function applyPromptTemplate(templateStr, vars) {
|
|
|
128
133
|
const withBlocks = templateStr.replace(/\{\{#if (\w+)\}\}([\s\S]*?)\{\{\/if\}\}/g, (_, varName, content) => vars[varName] ? content : '');
|
|
129
134
|
return withBlocks.replace(/\{\{(\w+)\}\}/g, (_, key) => vars[key] || '');
|
|
130
135
|
}
|
|
131
|
-
export function buildStepPrompt(threadId, agentConfig, stage = null
|
|
136
|
+
export function buildStepPrompt(threadId, agentConfig, stage = null) {
|
|
132
137
|
const thread = threadStore.get(threadId);
|
|
133
138
|
if (!thread)
|
|
134
139
|
return '';
|
|
@@ -149,7 +154,7 @@ export function buildStepPrompt(threadId, agentConfig, stage = null, isUserIniti
|
|
|
149
154
|
prefixes.push(userCtx);
|
|
150
155
|
if (agentConfig.directive)
|
|
151
156
|
prefixes.push(resolveSystemVars(agentConfig.directive));
|
|
152
|
-
if (thread.artifactPath
|
|
157
|
+
if (thread.artifactPath)
|
|
153
158
|
prefixes.push(THREAD_PROTOCOL_PREAMBLE);
|
|
154
159
|
if (prefixes.length > 0)
|
|
155
160
|
prompt = prefixes.join('\n\n') + '\n\n' + prompt;
|
|
@@ -174,3 +179,37 @@ export function buildStepPrompt(threadId, agentConfig, stage = null, isUserIniti
|
|
|
174
179
|
}
|
|
175
180
|
return prompt.trim();
|
|
176
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Assemble the prompt for a single plain user-conversation turn — the thread-independent
|
|
184
|
+
* counterpart of buildStepPrompt. Plain user messages are NOT wrapped in a thread, so there
|
|
185
|
+
* is no thread record, artifact, previous step, or transition to consider.
|
|
186
|
+
*
|
|
187
|
+
* Fidelity with the legacy default-thread path (templateName='default', isUserInitiated=true):
|
|
188
|
+
* - applies the default agent's promptTemplate (typically `{{input}}`) with empty thread vars;
|
|
189
|
+
* - prepends the agent directive (resolved for {{systemVar}});
|
|
190
|
+
* - prepends user context ONLY under the same gate the default thread used —
|
|
191
|
+
* loadUserContext({templateName:'default'}) returns null unless CORTEX_INJECT_USER_CONTEXT=1
|
|
192
|
+
* AND the template is a direct-* template, so 'default' yields null today (behavior preserved);
|
|
193
|
+
* - NEVER injects THREAD_PROTOCOL_PREAMBLE (no artifact, no [ABORT] protocol for conversations).
|
|
194
|
+
*/
|
|
195
|
+
export function buildConversationPrompt(agentConfig, input) {
|
|
196
|
+
const { template: templateStr } = pickStepTemplate(agentConfig, null);
|
|
197
|
+
const vars = {
|
|
198
|
+
input,
|
|
199
|
+
artifactPath: '',
|
|
200
|
+
previousOutput: '',
|
|
201
|
+
modifiedFiles: '',
|
|
202
|
+
modifiedFilesWithDiff: '',
|
|
203
|
+
...getSystemVars(),
|
|
204
|
+
};
|
|
205
|
+
let prompt = applyPromptTemplate(templateStr, vars);
|
|
206
|
+
const prefixes = [];
|
|
207
|
+
const userCtx = loadUserContext({ templateName: 'default' });
|
|
208
|
+
if (userCtx)
|
|
209
|
+
prefixes.push(userCtx);
|
|
210
|
+
if (agentConfig.directive)
|
|
211
|
+
prefixes.push(resolveSystemVars(agentConfig.directive));
|
|
212
|
+
if (prefixes.length > 0)
|
|
213
|
+
prompt = prefixes.join('\n\n') + '\n\n' + prompt;
|
|
214
|
+
return prompt.trim();
|
|
215
|
+
}
|
|
@@ -9,8 +9,6 @@ interface ThreadRunResult {
|
|
|
9
9
|
totalCostUsd: number;
|
|
10
10
|
/** Total number of turns across all steps */
|
|
11
11
|
totalNumTurns: number;
|
|
12
|
-
/** Was this a default (single-agent) thread? */
|
|
13
|
-
isDefault: boolean;
|
|
14
12
|
/** The result object from the last agent call (for backward compat with handleAgentSuccess) */
|
|
15
13
|
lastAgentResult: any;
|
|
16
14
|
/** The execution ID from the last completed step — used by handleAgentSuccess in the wrapper. */
|
|
@@ -19,10 +17,9 @@ interface ThreadRunResult {
|
|
|
19
17
|
/** Outer-scope state shared across the whole runThread() lifecycle. */
|
|
20
18
|
interface ThreadContext {
|
|
21
19
|
thread: ThreadRecord;
|
|
22
|
-
isDefault: boolean;
|
|
23
20
|
template: ThreadTemplate | null;
|
|
24
21
|
meta: ThreadRecord['metadata'];
|
|
25
|
-
stream: OutputStream
|
|
22
|
+
stream: OutputStream;
|
|
26
23
|
lastAgentResult: any;
|
|
27
24
|
totalNumTurns: number;
|
|
28
25
|
}
|
|
@@ -76,8 +73,12 @@ declare function evaluateAndTransition(threadId: string, stepCtx: StepContext, c
|
|
|
76
73
|
declare function finalizeThread(threadId: string, ctx: ThreadContext): Promise<ThreadRunResult>;
|
|
77
74
|
declare function runThread(threadId: string, opts: RunThreadOptions): Promise<ThreadRunResult>;
|
|
78
75
|
declare function continueThread(threadId: string, userMessage: string, opts: RunThreadOptions): Promise<ThreadRunResult>;
|
|
76
|
+
/** Re-enter a parent thread that was suspended via [WAIT_CHILDREN]. Unlike continueThread,
|
|
77
|
+
* the userMessage is NOT overwritten — the original contract stays in {{input}}; the child
|
|
78
|
+
* results arrive through metadata.pendingMessages (injected by the prompt builder). */
|
|
79
|
+
declare function resumeThread(threadId: string, opts: RunThreadOptions): Promise<ThreadRunResult>;
|
|
79
80
|
declare function buildThreadSummary(result: ThreadRunResult): string;
|
|
80
81
|
declare function cancelActiveThread(channel: string): boolean;
|
|
81
82
|
declare function getActiveHandle(channel: string): RunningExecution | null;
|
|
82
|
-
export { runThread, continueThread, buildThreadSummary, cancelActiveThread, getActiveHandle, initThreadContext, resolveAndNotifyStep, buildStepConfig, setupStepCallbacks, executeAndAwaitAgent, recordStepOutcome, evaluateAndTransition, finalizeThread, };
|
|
83
|
+
export { runThread, continueThread, resumeThread, buildThreadSummary, cancelActiveThread, getActiveHandle, initThreadContext, resolveAndNotifyStep, buildStepConfig, setupStepCallbacks, executeAndAwaitAgent, recordStepOutcome, evaluateAndTransition, finalizeThread, };
|
|
83
84
|
export type { ThreadRunResult, ThreadContext, StepContext, StepCallbacks, StepInfo };
|