@cortex-agent/server 2026.7.30 → 2026.8.3-2
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/README.md +1 -1
- package/defaults/CORTEX.md +1 -0
- package/defaults/config/CORTEX.md +2 -1
- package/defaults/config/hooks/13-status-md-guard.json +11 -0
- package/defaults/config/hooks/CORTEX.md +1 -0
- package/defaults/config/mcp-config-empty.json +3 -0
- package/defaults/config/thread-templates/agents/benchmark-coder.json +22 -0
- package/defaults/config/thread-templates/agents/benchmark-reviewer.json +22 -0
- package/defaults/config/thread-templates/agents/coder-reviewer.json +4 -9
- package/defaults/config/thread-templates/agents/coder.json +4 -14
- package/defaults/config/thread-templates/templates/benchmark-coder-review.json +37 -0
- package/defaults/config/thread-templates/templates/coder-review.json +3 -27
- package/defaults/context/projects/CORTEX.md +1 -1
- package/defaults/data/schedules.json +12 -0
- package/defaults/hooks/CORTEX.md +1 -0
- package/defaults/hooks/new-session-hook.mjs +2 -2
- package/defaults/hooks/status-md-guard.mjs +193 -0
- package/defaults/plugins/cortex-coder/.claude-plugin/plugin.json +1 -1
- package/defaults/plugins/cortex-coder/skills/develop/SKILL.md +28 -2
- package/defaults/plugins/cortex-common/.claude-plugin/plugin.json +1 -1
- package/defaults/plugins/cortex-common/skills/compound/SKILL.md +1 -1
- package/defaults/plugins/cortex-common/skills/compound-simple/SKILL.md +4 -2
- package/defaults/plugins/cortex-common/skills/postmortem/SKILL.md +1 -1
- package/defaults/plugins/cortex-stage-gate/.claude-plugin/plugin.json +1 -1
- package/defaults/plugins/cortex-stage-gate/skills/director-method/SKILL.md +1 -1
- package/defaults/plugins/cortex-stage-gate/skills/need-approval/SKILL.md +1 -0
- package/defaults/plugins/cortex-stage-gate/skills/reorient/SKILL.md +2 -2
- package/defaults/plugins/cortex-stage-gate/skills/task/SKILL.md +11 -8
- package/defaults/plugins/cortex-system/skills/feedback/SKILL.md +1 -1
- package/defaults/plugins/cortex-system/skills/project-init/SKILL.md +13 -16
- package/defaults/prompts/directives/coder-reviewer.md +24 -20
- package/defaults/prompts/directives/coder.md +4 -5
- package/defaults/prompts/directives/director.md +4 -10
- package/defaults/prompts/directives/manager.md +9 -10
- package/defaults/prompts/systemPrompts/worker.md +2 -2
- package/defaults/rules/cortex-md.md +31 -0
- package/defaults/rules/experiment-format.md +8 -1
- package/defaults/rules/issues-md.md +10 -9
- package/defaults/rules/meta-conventions.md +5 -4
- package/defaults/rules/mission-md.md +2 -1
- package/defaults/rules/status-md.md +39 -21
- package/dist/agent-adapter/bg-wait.d.ts +10 -12
- package/dist/agent-adapter/bg-wait.js +209 -129
- package/dist/agent-adapter/claude/adapter-tui.d.ts +3 -1
- package/dist/agent-adapter/claude/adapter-tui.js +8 -5
- package/dist/agent-adapter/claude/adapter.d.ts +43 -14
- package/dist/agent-adapter/claude/adapter.js +276 -161
- package/dist/agent-adapter/claude/defaults.d.ts +2 -0
- package/dist/agent-adapter/claude/defaults.js +5 -5
- package/dist/agent-adapter/claude/event-parser.d.ts +3 -8
- package/dist/agent-adapter/claude/event-parser.js +28 -31
- package/dist/agent-adapter/claude/hooks-builder.js +4 -3
- package/dist/agent-adapter/claude/jsonl-tail.js +54 -35
- package/dist/agent-adapter/claude/spawn-args.d.ts +15 -12
- package/dist/agent-adapter/claude/spawn-args.js +44 -38
- package/dist/agent-adapter/claude/tmux-control.d.ts +4 -17
- package/dist/agent-adapter/claude/tmux-control.js +39 -32
- package/dist/agent-adapter/event-tee.d.ts +19 -0
- package/dist/agent-adapter/event-tee.js +145 -0
- package/dist/agent-adapter/index.d.ts +1 -0
- package/dist/agent-adapter/index.js +7 -3
- package/dist/agent-adapter/normalize/event-types.d.ts +17 -9
- package/dist/agent-adapter/normalize/event-types.js +4 -4
- package/dist/agent-adapter/pi/adapter.d.ts +16 -4
- package/dist/agent-adapter/pi/adapter.js +69 -36
- package/dist/agent-adapter/pi/discovery.d.ts +19 -3
- package/dist/agent-adapter/pi/discovery.js +81 -43
- package/dist/agent-adapter/pi/event-parser.js +4 -2
- package/dist/agent-adapter/pi/spawn-args.d.ts +2 -4
- package/dist/agent-adapter/pi/spawn-args.js +6 -5
- package/dist/agent-adapter/types.d.ts +43 -2
- package/dist/agent-adapter/types.js +8 -4
- package/dist/core/atomic-write.d.ts +6 -1
- package/dist/core/atomic-write.js +183 -7
- package/dist/core/cli-utils.d.ts +15 -2
- package/dist/core/cli-utils.js +46 -41
- package/dist/core/config-generator.d.ts +4 -0
- package/dist/core/config-generator.js +34 -21
- package/dist/core/json-repository.d.ts +4 -0
- package/dist/core/json-repository.js +6 -3
- package/dist/core/locales/en.d.ts +94 -0
- package/dist/core/locales/slices/commands.d.ts +76 -0
- package/dist/core/locales/slices/commands.js +155 -1
- package/dist/core/locales/slices/status.d.ts +18 -0
- package/dist/core/locales/slices/status.js +43 -2
- package/dist/core/log.d.ts +5 -0
- package/dist/core/log.js +25 -23
- package/dist/core/pi-session-filename.d.ts +2 -0
- package/dist/core/pi-session-filename.js +18 -0
- package/dist/core/resume-reminder.d.ts +5 -0
- package/dist/core/resume-reminder.js +18 -0
- package/dist/core/runtime-env.d.ts +1 -0
- package/dist/core/runtime-env.js +15 -0
- package/dist/core/settings-migration.d.ts +5 -0
- package/dist/core/settings-migration.js +128 -0
- package/dist/core/settings-spec.d.ts +174 -0
- package/dist/core/settings-spec.js +150 -0
- package/dist/core/settings.d.ts +10 -0
- package/dist/core/settings.js +235 -0
- package/dist/core/task-parser.d.ts +11 -1
- package/dist/core/task-parser.js +34 -3
- package/dist/core/types/agent-types.d.ts +19 -0
- package/dist/core/types/agent-types.js +1 -1
- package/dist/core/types/thread-types.d.ts +48 -3
- package/dist/core/types/thread-types.js +2 -2
- package/dist/core/utils.d.ts +21 -1
- package/dist/core/utils.js +37 -1
- package/dist/domain/agent-run/access-probe-cli.d.ts +7 -0
- package/dist/domain/agent-run/access-probe-cli.js +139 -0
- package/dist/domain/agent-run/access-probe-policy.d.ts +61 -0
- package/dist/domain/agent-run/access-probe-policy.js +614 -0
- package/dist/domain/agent-run/access-probe.d.ts +21 -0
- package/dist/domain/agent-run/access-probe.js +194 -0
- package/dist/domain/agent-run/agent-run-cli.d.ts +19 -0
- package/dist/domain/agent-run/agent-run-cli.js +171 -0
- package/dist/domain/agent-run/atif.d.ts +54 -0
- package/dist/domain/agent-run/atif.js +211 -0
- package/dist/domain/agent-run/benchmark-local-thread-orchestrator.d.ts +32 -0
- package/dist/domain/agent-run/benchmark-local-thread-orchestrator.js +616 -0
- package/dist/domain/agent-run/benchmark-thread-identity.d.ts +34 -0
- package/dist/domain/agent-run/benchmark-thread-identity.js +106 -0
- package/dist/domain/agent-run/identity.d.ts +62 -0
- package/dist/domain/agent-run/identity.js +118 -0
- package/dist/domain/agent-run/journal.d.ts +51 -0
- package/dist/domain/agent-run/journal.js +259 -0
- package/dist/domain/agent-run/manifest-contract.d.ts +44 -0
- package/dist/domain/agent-run/manifest-contract.js +134 -0
- package/dist/domain/agent-run/manifest.d.ts +51 -0
- package/dist/domain/agent-run/manifest.js +801 -0
- package/dist/domain/agent-run/pinned-node-process.d.ts +35 -0
- package/dist/domain/agent-run/pinned-node-process.js +206 -0
- package/dist/domain/agent-run/role-surface.d.ts +4 -0
- package/dist/domain/agent-run/role-surface.js +99 -0
- package/dist/domain/agent-run/run-config.d.ts +22 -0
- package/dist/domain/agent-run/run-config.js +135 -0
- package/dist/domain/agent-run/runner.d.ts +7 -0
- package/dist/domain/agent-run/runner.js +518 -0
- package/dist/domain/agent-run/supervisor.d.ts +78 -0
- package/dist/domain/agent-run/supervisor.js +425 -0
- package/dist/domain/agent-run/trajectory-merge-cli.d.ts +11 -0
- package/dist/domain/agent-run/trajectory-merge-cli.js +111 -0
- package/dist/domain/agent-run/trajectory-merge.d.ts +39 -0
- package/dist/domain/agent-run/trajectory-merge.js +505 -0
- package/dist/domain/agents/config.d.ts +10 -0
- package/dist/domain/agents/config.js +149 -44
- package/dist/domain/agents/facade.d.ts +42 -7
- package/dist/domain/agents/facade.js +174 -170
- package/dist/domain/agents/provider-run-lifecycle.d.ts +18 -0
- package/dist/domain/agents/provider-run-lifecycle.js +60 -0
- package/dist/domain/auth/auth-events.d.ts +18 -0
- package/dist/domain/auth/auth-events.js +38 -0
- package/dist/domain/auth/auth-format.d.ts +2 -0
- package/dist/domain/auth/auth-format.js +58 -0
- package/dist/domain/auth/auth-status.d.ts +61 -0
- package/dist/domain/auth/auth-status.js +304 -0
- package/dist/domain/auth/auth-watch.d.ts +16 -0
- package/dist/domain/auth/auth-watch.js +125 -0
- package/dist/domain/auth/cc-login.d.ts +7 -0
- package/dist/domain/auth/cc-login.js +16 -0
- package/dist/domain/auth/cc-subscription.d.ts +38 -0
- package/dist/domain/auth/cc-subscription.js +204 -0
- package/dist/domain/auth/index.d.ts +12 -0
- package/dist/domain/auth/index.js +16 -0
- package/dist/domain/auth/login-flow.d.ts +120 -0
- package/dist/domain/auth/login-flow.js +253 -0
- package/dist/domain/auth/login-service.d.ts +32 -0
- package/dist/domain/auth/login-service.js +97 -0
- package/dist/domain/auth/logout.d.ts +32 -0
- package/dist/domain/auth/logout.js +123 -0
- package/dist/domain/auth/pi-login.d.ts +36 -0
- package/dist/domain/auth/pi-login.js +72 -0
- package/dist/domain/auth/pi-oauth.d.ts +36 -0
- package/dist/domain/auth/pi-oauth.js +84 -0
- package/dist/domain/auth/pi-runtime.d.ts +72 -0
- package/dist/domain/auth/pi-runtime.js +86 -0
- package/dist/domain/mcp/benchmark-thread-server.d.ts +1 -0
- package/dist/domain/mcp/benchmark-thread-server.js +26 -0
- package/dist/domain/mcp/tools/benchmark-thread-run.d.ts +23 -0
- package/dist/domain/mcp/tools/benchmark-thread-run.js +146 -0
- package/dist/domain/memory/user-context.js +6 -8
- package/dist/domain/monitor/disk-monitor.js +47 -16
- package/dist/domain/projects/project-store.js +23 -6
- package/dist/domain/remote/client-hot-reload.d.ts +9 -1
- package/dist/domain/remote/client-hot-reload.js +60 -12
- package/dist/domain/remote/client-manager.js +12 -6
- package/dist/domain/scheduling/jobs/auth-expiry-scan.d.ts +14 -0
- package/dist/domain/scheduling/jobs/auth-expiry-scan.js +86 -0
- package/dist/domain/scheduling/jobs/task-dispatch.js +36 -40
- package/dist/domain/scheduling/runner.d.ts +2 -0
- package/dist/domain/scheduling/runner.js +7 -4
- package/dist/domain/sessions/session-backup.d.ts +14 -20
- package/dist/domain/sessions/session-backup.js +57 -68
- package/dist/domain/system/doctor.d.ts +4 -0
- package/dist/domain/system/doctor.js +113 -28
- package/dist/domain/system/install-cli.js +8 -3
- package/dist/domain/system/server-update-check.js +7 -5
- package/dist/domain/system/system-notice.d.ts +3 -1
- package/dist/domain/system/system-notice.js +6 -11
- package/dist/domain/tasks/archiver.d.ts +1 -0
- package/dist/domain/tasks/archiver.js +13 -7
- package/dist/domain/tasks/claim-recovery.d.ts +3 -3
- package/dist/domain/tasks/claim-recovery.js +12 -8
- package/dist/domain/tasks/dispatch-utils.d.ts +15 -14
- package/dist/domain/tasks/dispatch-utils.js +22 -18
- package/dist/domain/tasks/dispatcher.d.ts +1 -0
- package/dist/domain/tasks/dispatcher.js +9 -5
- package/dist/domain/tasks/mutator.d.ts +21 -11
- package/dist/domain/tasks/mutator.js +30 -24
- package/dist/domain/tasks/pending-tracker.d.ts +5 -2
- package/dist/domain/tasks/pending-tracker.js +11 -5
- package/dist/domain/tasks/system/cortex-run.d.ts +2 -1
- package/dist/domain/tasks/system/cortex-run.js +24 -4
- package/dist/domain/tasks/system/task-cli.d.ts +1 -0
- package/dist/domain/tasks/system/task-cli.js +72 -22
- package/dist/domain/tasks/system/task-completion.d.ts +19 -2
- package/dist/domain/tasks/system/task-completion.js +31 -17
- package/dist/domain/tasks/system/task-file-input.d.ts +10 -0
- package/dist/domain/tasks/system/task-file-input.js +57 -0
- package/dist/domain/tasks/system/task-id-utils.d.ts +9 -9
- package/dist/domain/tasks/system/task-id-utils.js +13 -4
- package/dist/domain/tasks/system/task-lifecycle-edit.d.ts +6 -2
- package/dist/domain/tasks/system/task-lifecycle-edit.js +200 -6
- package/dist/domain/tasks/system/task-lock.js +19 -5
- package/dist/domain/tasks/system/task-mutations.d.ts +22 -12
- package/dist/domain/tasks/system/task-mutations.js +28 -4
- package/dist/domain/tasks/system/task-process.js +10 -8
- package/dist/domain/tasks/system/task-state.d.ts +65 -11
- package/dist/domain/tasks/system/task-state.js +86 -13
- package/dist/domain/threads/local-runtime-deps.d.ts +32 -0
- package/dist/domain/threads/local-runtime-deps.js +91 -0
- package/dist/domain/threads/local-runtime-scope.d.ts +6 -0
- package/dist/domain/threads/local-runtime-scope.js +12 -0
- package/dist/domain/threads/pending-user-inputs.d.ts +3 -0
- package/dist/domain/threads/pending-user-inputs.js +37 -0
- package/dist/domain/threads/prompt-builder.d.ts +12 -1
- package/dist/domain/threads/prompt-builder.js +73 -26
- package/dist/domain/threads/runner.d.ts +15 -3
- package/dist/domain/threads/runner.js +261 -105
- package/dist/domain/threads/state-machine.d.ts +1 -1
- package/dist/domain/threads/state-machine.js +17 -8
- package/dist/domain/ui-service/app-router.d.ts +86 -6
- package/dist/domain/ui-service/app-router.js +171 -133
- package/dist/domain/ui-service/input-schemas.d.ts +132 -10
- package/dist/domain/ui-service/input-schemas.js +59 -5
- package/dist/domain/ui-service/mutate/approvals.d.ts +2 -1
- package/dist/domain/ui-service/mutate/approvals.js +2 -1
- package/dist/domain/ui-service/mutate/auth.d.ts +6 -0
- package/dist/domain/ui-service/mutate/auth.js +83 -0
- package/dist/domain/ui-service/mutate/config.js +11 -10
- package/dist/domain/ui-service/query/approvals.js +3 -0
- package/dist/domain/ui-service/query/auth.d.ts +6 -0
- package/dist/domain/ui-service/query/auth.js +12 -0
- package/dist/domain/ui-service/query/config.d.ts +5 -5
- package/dist/domain/ui-service/query/config.js +27 -17
- package/dist/domain/ui-service/query/tasks.d.ts +1 -1
- package/dist/domain/ui-service/query/tasks.js +69 -30
- package/dist/domain/ui-service/query/threads.js +3 -3
- package/dist/domain/ui-service/types.d.ts +67 -8
- package/dist/domain/ui-service/types.js +2 -2
- package/dist/domain/ui-service/ui-service.js +16 -2
- package/dist/entry/admin-channel-hot-reload.d.ts +4 -0
- package/dist/entry/admin-channel-hot-reload.js +22 -0
- package/dist/entry/app.js +38 -38
- package/dist/entry/auth-cli.d.ts +10 -0
- package/dist/entry/auth-cli.js +52 -0
- package/dist/entry/cli-help.d.ts +5 -0
- package/dist/entry/cli-help.js +130 -0
- package/dist/entry/cli.d.ts +5 -5
- package/dist/entry/cli.js +174 -248
- package/dist/entry/daemon.d.ts +11 -0
- package/dist/entry/daemon.js +204 -103
- package/dist/entry/draft-attachments.d.ts +8 -0
- package/dist/entry/draft-attachments.js +83 -0
- package/dist/entry/feishu-login.d.ts +2 -6
- package/dist/entry/feishu-login.js +16 -21
- package/dist/entry/init.js +6 -4
- package/dist/entry/start-ui-http.d.ts +2 -8
- package/dist/entry/start-ui-http.js +7 -34
- package/dist/events/event-logger.js +11 -13
- package/dist/events/event-types.d.ts +19 -1
- package/dist/events/event-types.js +3 -3
- package/dist/events/index.d.ts +1 -1
- package/dist/events/index.js +3 -3
- package/dist/hooks/new-session-hook.mjs +2 -2
- package/dist/hooks/status-md-guard.mjs +193 -0
- package/dist/orchestration/agent-file-send.js +44 -14
- package/dist/orchestration/agent-runner.d.ts +7 -3
- package/dist/orchestration/agent-runner.js +65 -41
- package/dist/orchestration/conversation-runner.d.ts +5 -1
- package/dist/orchestration/conversation-runner.js +16 -7
- package/dist/orchestration/lifecycle.d.ts +23 -1
- package/dist/orchestration/lifecycle.js +103 -40
- package/dist/orchestration/mid-turn-inject.d.ts +2 -0
- package/dist/orchestration/mid-turn-inject.js +29 -13
- package/dist/orchestration/resume-dispatcher.d.ts +3 -5
- package/dist/orchestration/resume-dispatcher.js +9 -17
- package/dist/orchestration/routing/commands/index.d.ts +4 -1
- package/dist/orchestration/routing/commands/index.js +83 -61
- package/dist/orchestration/routing/commands/login-notice.d.ts +21 -0
- package/dist/orchestration/routing/commands/login-notice.js +97 -0
- package/dist/orchestration/routing/commands/login.d.ts +12 -0
- package/dist/orchestration/routing/commands/login.js +555 -0
- package/dist/orchestration/routing/commands/status.js +45 -59
- package/dist/orchestration/routing/edit-handler.d.ts +5 -0
- package/dist/orchestration/routing/edit-handler.js +127 -73
- package/dist/orchestration/routing/webhook.js +5 -4
- package/dist/orchestration/session-rewind.d.ts +5 -1
- package/dist/orchestration/session-rewind.js +106 -62
- package/dist/orchestration/session-send.d.ts +2 -0
- package/dist/orchestration/session-send.js +5 -6
- package/dist/orchestration/status-helpers.d.ts +1 -2
- package/dist/orchestration/status-helpers.js +7 -10
- package/dist/orchestration/thread-callback.d.ts +19 -11
- package/dist/orchestration/thread-callback.js +132 -61
- package/dist/orchestration/thread-executor.js +37 -27
- package/dist/orchestration/turn-mutation-lock.d.ts +3 -0
- package/dist/orchestration/turn-mutation-lock.js +36 -0
- package/dist/orchestration/turn-notify.d.ts +2 -5
- package/dist/orchestration/turn-notify.js +9 -21
- package/dist/platform/adapter.d.ts +1 -0
- package/dist/platform/adapter.js +2 -2
- package/dist/platform/adapters/composite-adapter.d.ts +2 -0
- package/dist/platform/adapters/composite-adapter.js +13 -3
- package/dist/platform/adapters/feishu.d.ts +2 -4
- package/dist/platform/adapters/feishu.js +38 -59
- package/dist/platform/adapters/index.d.ts +1 -1
- package/dist/platform/adapters/index.js +13 -12
- package/dist/platform/adapters/slack.d.ts +1 -1
- package/dist/platform/adapters/slack.js +12 -35
- package/dist/platform/index.d.ts +2 -1
- package/dist/platform/index.js +3 -3
- package/dist/platform/testing.d.ts +1 -0
- package/dist/platform/testing.js +13 -4
- package/dist/platform/tool-trace.js +6 -11
- package/dist/platform/ui-http/ui-http-server.d.ts +1 -1
- package/dist/platform/ui-http/ui-http-server.js +6 -18
- package/dist/store/execution-repo.d.ts +17 -1
- package/dist/store/execution-repo.js +55 -4
- package/dist/store/prompt-migration-replacements.d.ts +4 -0
- package/dist/store/prompt-migration-replacements.js +149 -0
- package/dist/store/thread-repo.d.ts +15 -4
- package/dist/store/thread-repo.js +61 -24
- package/dist/store/version-migrations.js +36 -1
- package/native/cortex-supervisor/dist/build-manifest.json +22 -0
- package/native/cortex-supervisor/dist/cortex-supervisor +0 -0
- package/package.json +5 -2
- package/web/dist/assets/index-XqBCYtkw.js +139 -0
- package/web/dist/assets/index-zpf3OSKC.css +1 -0
- package/web/dist/index.html +2 -2
- package/dist/domain/scheduling/jobs/failed-dispatch-reconciliation.d.ts +0 -1
- package/dist/domain/scheduling/jobs/failed-dispatch-reconciliation.js +0 -78
- package/web/dist/assets/index-B19jLgji.js +0 -138
- package/web/dist/assets/index-BuFyPyRW.css +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// input: PI --mode rpc stdout JSONL lines
|
|
2
|
-
// output:
|
|
3
|
-
// pos: Pure
|
|
2
|
+
// output: normalized events with nullable exact accounting
|
|
3
|
+
// pos: Pure PI RPC event translator
|
|
4
4
|
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
5
|
import { toCanonical } from '../normalize/tool-names.js';
|
|
6
6
|
import { isPlanFilePath } from '../claude/event-parser.js';
|
|
@@ -271,6 +271,8 @@ function handleAgentEnd(ev, state) {
|
|
|
271
271
|
model: summary.model,
|
|
272
272
|
tokens_in: summary.tokensIn,
|
|
273
273
|
tokens_out: summary.tokensOut,
|
|
274
|
+
prompt_tokens: null,
|
|
275
|
+
cached_tokens: null,
|
|
274
276
|
cost_usd: summary.totalCostUsd,
|
|
275
277
|
}];
|
|
276
278
|
}
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import type { AgentSpawnConfig } from '../types.js';
|
|
2
2
|
export interface PISpawnOptions {
|
|
3
3
|
sessionDir: string;
|
|
4
|
-
/** Session UUID for
|
|
5
|
-
* by filename or internal session id field — no need for the full file path. */
|
|
4
|
+
/** Session UUID for legacy callers that intentionally delegate lookup to PI. */
|
|
6
5
|
sessionId?: string | null;
|
|
7
|
-
/**
|
|
8
|
-
* which lets PI handle session lookup internally. Kept for backward compat. */
|
|
6
|
+
/** Exact session JSONL path; preferred so PI opens one transcript without scanning. */
|
|
9
7
|
sessionPath?: string | null;
|
|
10
8
|
/** Model identifier (e.g. "deepseek-v4-flash[1m]"); context-window suffix is stripped. */
|
|
11
9
|
model?: string | null;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// input: PI spawn options
|
|
2
|
-
// output: PI
|
|
1
|
+
// input: PI spawn options and task-aware AgentSpawnConfig context
|
|
2
|
+
// output: PI argv and isolated CORTEX_* subprocess environment
|
|
3
3
|
// pos: Pure PI argument and environment construction
|
|
4
4
|
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
5
|
/** Strip context-window suffix like "[1m]" from model strings (e.g. "deepseek-v4-flash[1m]" → "deepseek-v4-flash"). */
|
|
@@ -10,6 +10,7 @@ const RESET_CONTEXT_KEYS = [
|
|
|
10
10
|
'CORTEX_SESSION_ID', 'CORTEX_THREAD_ID', 'CORTEX_PROFILE',
|
|
11
11
|
'CORTEX_PROJECT', 'CORTEX_SESSION_NAME', 'CORTEX_EXECUTION_ID',
|
|
12
12
|
'CORTEX_THREAD_DEPTH', 'CORTEX_TASK_ID', 'CORTEX_TASK_PROJECT',
|
|
13
|
+
'CORTEX_TASK_GENERATION',
|
|
13
14
|
'CORTEX_CALLBACK_SOURCE', 'CORTEX_SCHEDULE_TASK_ID',
|
|
14
15
|
'CORTEX_PI_ALLOWED_TOOLS',
|
|
15
16
|
];
|
|
@@ -30,6 +31,7 @@ function applyContext(env, options) {
|
|
|
30
31
|
setOptional(env, 'CORTEX_THREAD_DEPTH', context?.threadDepth);
|
|
31
32
|
setOptional(env, 'CORTEX_TASK_ID', context?.taskId);
|
|
32
33
|
setOptional(env, 'CORTEX_TASK_PROJECT', context?.taskProject);
|
|
34
|
+
setOptional(env, 'CORTEX_TASK_GENERATION', context?.taskGeneration);
|
|
33
35
|
}
|
|
34
36
|
export function buildPiEnv(options, inheritedEnv = process.env) {
|
|
35
37
|
const env = { ...inheritedEnv, ...(options.extraEnv ?? {}) };
|
|
@@ -57,9 +59,8 @@ export function buildSpawnArgs(opts) {
|
|
|
57
59
|
args.push('--provider', opts.provider);
|
|
58
60
|
}
|
|
59
61
|
}
|
|
60
|
-
// --session accepts
|
|
61
|
-
//
|
|
62
|
-
// differ from the session UUID.
|
|
62
|
+
// --session accepts a UUID or an exact path. The adapter resolves and supplies a path;
|
|
63
|
+
// sessionId remains for explicit legacy callers.
|
|
63
64
|
if (opts.sessionId && opts.sessionId.length > 0) {
|
|
64
65
|
args.push('--session', opts.sessionId);
|
|
65
66
|
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import type { ChildProcessWithoutNullStreams, SpawnOptionsWithoutStdio } from 'node:child_process';
|
|
1
2
|
import type { Capability } from './capabilities.js';
|
|
2
3
|
import type { NormalizedEvent } from './normalize/event-types.js';
|
|
3
4
|
import type { NormalizedHookSpec } from './normalize/hooks.js';
|
|
4
5
|
import type { AgentResult, ContextUsage } from '../core/types/agent-types.js';
|
|
5
6
|
export type Backend = 'claude' | 'pi';
|
|
7
|
+
export type McpComposition = 'direct' | 'thread-control' | 'none' | 'benchmark-thread-run';
|
|
8
|
+
export declare function resolveMcpComposition(explicit: McpComposition | undefined, useCoreMcp: boolean | undefined): McpComposition;
|
|
6
9
|
export interface UserMessage {
|
|
7
10
|
text: string;
|
|
8
11
|
attachments?: {
|
|
@@ -21,6 +24,28 @@ export interface McpServerConfig {
|
|
|
21
24
|
env?: Record<string, string>;
|
|
22
25
|
url?: string;
|
|
23
26
|
}
|
|
27
|
+
export interface AgentProcessSupervision {
|
|
28
|
+
started: Promise<{
|
|
29
|
+
pid: number;
|
|
30
|
+
pgid: number;
|
|
31
|
+
}>;
|
|
32
|
+
exited: Promise<{
|
|
33
|
+
code: number | null;
|
|
34
|
+
signal: string | null;
|
|
35
|
+
}>;
|
|
36
|
+
quiescent: Promise<void>;
|
|
37
|
+
closed: Promise<{
|
|
38
|
+
code: number | null;
|
|
39
|
+
signal: NodeJS.Signals | null;
|
|
40
|
+
}>;
|
|
41
|
+
cancel(reason: 'cancel' | 'deadline'): void;
|
|
42
|
+
dispose(): Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
export interface SpawnedAgentProcess {
|
|
45
|
+
process: ChildProcessWithoutNullStreams;
|
|
46
|
+
supervision?: AgentProcessSupervision;
|
|
47
|
+
}
|
|
48
|
+
export type AgentProcessSpawner = (command: string, args: string[], options: SpawnOptionsWithoutStdio) => SpawnedAgentProcess;
|
|
24
49
|
export interface AgentSpawnConfig {
|
|
25
50
|
sessionId: string | null;
|
|
26
51
|
/** Used to deduplicate sessions within a channel — multiple thread agents share a channel but need separate sessions. */
|
|
@@ -38,6 +63,19 @@ export interface AgentSpawnConfig {
|
|
|
38
63
|
hooks?: NormalizedHookSpec[];
|
|
39
64
|
outputStyle?: string;
|
|
40
65
|
cwd?: string;
|
|
66
|
+
mcpComposition?: McpComposition;
|
|
67
|
+
/** Concrete MCP files for a frozen one-shot role. */
|
|
68
|
+
mcpConfigPaths?: string[];
|
|
69
|
+
/** Suppress ambient lifecycle hooks for an isolated one-shot role. */
|
|
70
|
+
disableHooks?: boolean;
|
|
71
|
+
/** Explicit delta policy avoids loading watched daemon settings in one-shot mode. */
|
|
72
|
+
streamDeltas?: boolean;
|
|
73
|
+
/** Disable legacy raw/text transcript files when the required journal is authoritative. */
|
|
74
|
+
captureTranscriptLogs?: boolean;
|
|
75
|
+
/** Preserve absent backend cost/usage for provenance-sensitive one-shot runs. */
|
|
76
|
+
preserveUnreportedAccounting?: boolean;
|
|
77
|
+
/** Optional process boundary used by daemon-free runs. Ordinary callers spawn directly. */
|
|
78
|
+
processSpawner?: AgentProcessSpawner;
|
|
41
79
|
/** Channel identifier used for Claude session-pool key fallback. */
|
|
42
80
|
channel?: string;
|
|
43
81
|
/** DR-0008 Phase 3 cleanup target. Claude `--agent` CLI flag. */
|
|
@@ -86,9 +124,10 @@ export interface AgentSpawnConfig {
|
|
|
86
124
|
/** When true, load core + tasks + manager-answer + thread MCP layers. */
|
|
87
125
|
useCoreMcp?: boolean;
|
|
88
126
|
threadDepth?: number | null;
|
|
89
|
-
/** Owning dispatch task
|
|
127
|
+
/** Owning dispatch task identity surfaced through CORTEX_TASK_* variables. */
|
|
90
128
|
taskId?: string | null;
|
|
91
129
|
taskProject?: string | null;
|
|
130
|
+
taskGeneration?: string | null;
|
|
92
131
|
};
|
|
93
132
|
}
|
|
94
133
|
/**
|
|
@@ -100,7 +139,7 @@ export interface AgentSpawnConfig {
|
|
|
100
139
|
*/
|
|
101
140
|
export interface ContinuationSink {
|
|
102
141
|
/** Assistant text from the continuation turn (append to the original reply). */
|
|
103
|
-
onAssistantText: (text: string) => void;
|
|
142
|
+
onAssistantText: (text: string, model?: string | null) => void;
|
|
104
143
|
/** Optional tool_use trace from the continuation turn, preserving its correlation id. */
|
|
105
144
|
onToolUse?: (name: string, input: any, toolUseId?: string) => void;
|
|
106
145
|
/** Optional full normalized tool result from the continuation turn. */
|
|
@@ -146,6 +185,8 @@ export interface AgentCompactResult {
|
|
|
146
185
|
}
|
|
147
186
|
export interface AgentProcess {
|
|
148
187
|
sessionKey: string;
|
|
188
|
+
/** Present when the process was launched through a containment supervisor. */
|
|
189
|
+
readonly supervision?: AgentProcessSupervision;
|
|
149
190
|
/** May be null at spawn time; adapter fills in asynchronously when the backend assigns a session id. */
|
|
150
191
|
sessionId: string | null;
|
|
151
192
|
/** Run one turn. Resolves with the AgentResult for that turn (carries rateLimited / planFilePath / askUserQuestions / cost accounting the outer fallback depends on). Events for the same turn are also emitted via the `events` iterable. */
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
// input: normalized events,
|
|
2
|
-
// output:
|
|
3
|
-
// pos: Shared
|
|
1
|
+
// input: normalized events, capabilities, child-process types
|
|
2
|
+
// output: adapter, process, task context, supervision, accounting and continuation contracts
|
|
3
|
+
// pos: Shared adapter runtime contracts
|
|
4
4
|
// >>> 一旦我被更新,务必更新我的开头注释与所属文件夹 CORTEX.md <<<
|
|
5
|
-
export {
|
|
5
|
+
export function resolveMcpComposition(explicit, useCoreMcp) {
|
|
6
|
+
if (explicit !== undefined)
|
|
7
|
+
return explicit;
|
|
8
|
+
return useCoreMcp === true ? 'thread-control' : 'direct';
|
|
9
|
+
}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface AtomicWriteOptions {
|
|
2
|
+
mode?: number;
|
|
3
|
+
signal?: AbortSignal;
|
|
4
|
+
}
|
|
5
|
+
export declare function mutateFileAtomically(filePath: string, mutate: (contents: string) => string | Promise<string>, options?: AtomicWriteOptions): Promise<void>;
|
|
6
|
+
export declare function atomicWrite(filePath: string, data: string, options?: AtomicWriteOptions): Promise<void>;
|
|
2
7
|
/** Synchronous variant for sync call sites (e.g. CLI write handlers). Same guard, same
|
|
3
8
|
* tmp+rename atomicity. */
|
|
4
9
|
export declare function atomicWriteSync(filePath: string, data: string): void;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
// input:
|
|
2
|
-
// output: atomic
|
|
3
|
-
// pos:
|
|
4
|
-
// >>>
|
|
1
|
+
// input: file paths, text mutations, modes, abort signals
|
|
2
|
+
// output: cancellable atomic writes and serialized mutations
|
|
3
|
+
// pos: Safe file replacement and mutation primitive
|
|
4
|
+
// >>> 一旦我被更新,务必更新我的开头注释与所属文件夹 CORTEX.md <<<
|
|
5
5
|
import fs from 'node:fs/promises';
|
|
6
6
|
import { writeFileSync, renameSync, mkdirSync } from 'node:fs';
|
|
7
7
|
import path from 'node:path';
|
|
@@ -33,13 +33,189 @@ function assertNotRealHomeInTest(filePath) {
|
|
|
33
33
|
+ `\`npm test\` / \`npm run test:file <file>\` (both set an isolated CORTEX_HOME).`);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
-
|
|
36
|
+
const MUTATION_LOCK_WAIT_MS = 10;
|
|
37
|
+
const MUTATION_LOCK_TIMEOUT_MS = 30_000;
|
|
38
|
+
const OWNERLESS_LOCK_GRACE_MS = 1_000;
|
|
39
|
+
function throwIfWriteAborted(signal) {
|
|
40
|
+
if (!signal?.aborted)
|
|
41
|
+
return;
|
|
42
|
+
if (signal.reason instanceof Error)
|
|
43
|
+
throw signal.reason;
|
|
44
|
+
throw new Error('Atomic write aborted.');
|
|
45
|
+
}
|
|
46
|
+
function processIsAlive(pid) {
|
|
47
|
+
try {
|
|
48
|
+
process.kill(pid, 0);
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
return error?.code !== 'ESRCH';
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async function processStartIdentity(pid) {
|
|
56
|
+
try {
|
|
57
|
+
const stat = await fs.readFile(`/proc/${pid}/stat`, 'utf8');
|
|
58
|
+
return stat.slice(stat.lastIndexOf(')') + 2).split(' ')[19] ?? null;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async function readLockOwner(lockPath) {
|
|
65
|
+
try {
|
|
66
|
+
const owner = JSON.parse(await fs.readFile(path.join(lockPath, 'owner.json'), 'utf8'));
|
|
67
|
+
if (typeof owner.pid !== 'number' || typeof owner.token !== 'string')
|
|
68
|
+
return null;
|
|
69
|
+
return {
|
|
70
|
+
pid: owner.pid,
|
|
71
|
+
token: owner.token,
|
|
72
|
+
processStart: typeof owner.processStart === 'string' ? owner.processStart : null,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function lockIsStale(lockPath) {
|
|
80
|
+
let ageMs;
|
|
81
|
+
try {
|
|
82
|
+
ageMs = Date.now() - (await fs.stat(lockPath)).mtimeMs;
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
const owner = await readLockOwner(lockPath);
|
|
88
|
+
if (!owner)
|
|
89
|
+
return ageMs > OWNERLESS_LOCK_GRACE_MS;
|
|
90
|
+
if (!processIsAlive(owner.pid))
|
|
91
|
+
return true;
|
|
92
|
+
const currentStart = await processStartIdentity(owner.pid);
|
|
93
|
+
return owner.processStart !== null && currentStart !== null && owner.processStart !== currentStart;
|
|
94
|
+
}
|
|
95
|
+
async function releaseOwnedLock(lockPath, token) {
|
|
96
|
+
if ((await readLockOwner(lockPath))?.token !== token)
|
|
97
|
+
return;
|
|
98
|
+
const releasedPath = `${lockPath}.released.${encodeURIComponent(token)}`;
|
|
99
|
+
try {
|
|
100
|
+
await fs.rename(lockPath, releasedPath);
|
|
101
|
+
if ((await readLockOwner(releasedPath))?.token === token) {
|
|
102
|
+
await fs.rm(releasedPath, { recursive: true, force: true });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch { }
|
|
106
|
+
}
|
|
107
|
+
async function removeStaleLock(lockPath) {
|
|
108
|
+
if (!(await lockIsStale(lockPath)))
|
|
109
|
+
return;
|
|
110
|
+
try {
|
|
111
|
+
const initial = await fs.stat(lockPath);
|
|
112
|
+
await fs.writeFile(path.join(lockPath, '.reclaimed'), '');
|
|
113
|
+
const current = await fs.stat(lockPath);
|
|
114
|
+
if (current.dev !== initial.dev || current.ino !== initial.ino)
|
|
115
|
+
return;
|
|
116
|
+
if ((await readLockOwner(lockPath)) && !(await lockIsStale(lockPath)))
|
|
117
|
+
return;
|
|
118
|
+
const identity = `${current.dev}.${current.ino}.${String(current.ctimeMs).replace('.', '-')}`;
|
|
119
|
+
const tombstone = `${lockPath}.reclaimed.${identity}`;
|
|
120
|
+
await fs.rename(lockPath, tombstone);
|
|
121
|
+
await fs.rm(tombstone, { recursive: true, force: true });
|
|
122
|
+
}
|
|
123
|
+
catch { }
|
|
124
|
+
}
|
|
125
|
+
async function tryAcquireLock(lockPath) {
|
|
126
|
+
const token = `${process.pid}:${Date.now()}:${Math.random()}`;
|
|
127
|
+
const candidate = `${lockPath}.candidate.${encodeURIComponent(token)}`;
|
|
128
|
+
try {
|
|
129
|
+
await fs.mkdir(candidate);
|
|
130
|
+
const owner = {
|
|
131
|
+
pid: process.pid, token, processStart: await processStartIdentity(process.pid),
|
|
132
|
+
};
|
|
133
|
+
await fs.writeFile(path.join(candidate, 'owner.json'), JSON.stringify(owner), { mode: 0o600 });
|
|
134
|
+
await fs.rename(candidate, lockPath);
|
|
135
|
+
return () => releaseOwnedLock(lockPath, token);
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
await fs.rm(candidate, { recursive: true, force: true });
|
|
139
|
+
if (error?.code === 'EEXIST' || error?.code === 'ENOTEMPTY')
|
|
140
|
+
return null;
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async function acquireMutationLock(filePath, signal) {
|
|
145
|
+
throwIfWriteAborted(signal);
|
|
146
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
147
|
+
const lockPath = `${filePath}.mutation-lock`;
|
|
148
|
+
const deadline = Date.now() + MUTATION_LOCK_TIMEOUT_MS;
|
|
149
|
+
while (Date.now() < deadline) {
|
|
150
|
+
throwIfWriteAborted(signal);
|
|
151
|
+
const release = await tryAcquireLock(lockPath);
|
|
152
|
+
if (release)
|
|
153
|
+
return release;
|
|
154
|
+
if (await lockIsStale(lockPath))
|
|
155
|
+
await removeStaleLock(lockPath);
|
|
156
|
+
else
|
|
157
|
+
await new Promise(resolve => setTimeout(resolve, MUTATION_LOCK_WAIT_MS));
|
|
158
|
+
}
|
|
159
|
+
throw new Error('Timed out waiting for file mutation lock.');
|
|
160
|
+
}
|
|
161
|
+
async function readOptionalText(filePath) {
|
|
162
|
+
try {
|
|
163
|
+
return await fs.readFile(filePath, 'utf8');
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
if (error.code === 'ENOENT')
|
|
167
|
+
return '';
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async function applyModeIfPresent(filePath, mode) {
|
|
172
|
+
try {
|
|
173
|
+
await fs.chmod(filePath, mode);
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
if (error.code !== 'ENOENT')
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
export async function mutateFileAtomically(filePath, mutate, options = {}) {
|
|
37
181
|
assertNotRealHomeInTest(filePath);
|
|
182
|
+
const release = await acquireMutationLock(filePath, options.signal);
|
|
183
|
+
try {
|
|
184
|
+
throwIfWriteAborted(options.signal);
|
|
185
|
+
const contents = await readOptionalText(filePath);
|
|
186
|
+
throwIfWriteAborted(options.signal);
|
|
187
|
+
const updated = await mutate(contents);
|
|
188
|
+
throwIfWriteAborted(options.signal);
|
|
189
|
+
if (updated !== contents)
|
|
190
|
+
await atomicWrite(filePath, updated, options);
|
|
191
|
+
else if (options.mode !== undefined)
|
|
192
|
+
await applyModeIfPresent(filePath, options.mode);
|
|
193
|
+
}
|
|
194
|
+
finally {
|
|
195
|
+
await release();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
export async function atomicWrite(filePath, data, options = {}) {
|
|
199
|
+
assertNotRealHomeInTest(filePath);
|
|
200
|
+
throwIfWriteAborted(options.signal);
|
|
38
201
|
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
202
|
+
throwIfWriteAborted(options.signal);
|
|
39
203
|
const rnd = Math.random().toString(36).slice(2, 8);
|
|
40
204
|
const tmp = `${filePath}.tmp.${process.pid}.${Date.now()}.${rnd}`;
|
|
41
|
-
|
|
42
|
-
|
|
205
|
+
let committed = false;
|
|
206
|
+
try {
|
|
207
|
+
await fs.writeFile(tmp, data, { encoding: 'utf8', mode: options.mode });
|
|
208
|
+
throwIfWriteAborted(options.signal);
|
|
209
|
+
if (options.mode !== undefined)
|
|
210
|
+
await fs.chmod(tmp, options.mode);
|
|
211
|
+
throwIfWriteAborted(options.signal);
|
|
212
|
+
renameSync(tmp, filePath);
|
|
213
|
+
committed = true;
|
|
214
|
+
}
|
|
215
|
+
finally {
|
|
216
|
+
if (!committed)
|
|
217
|
+
await fs.rm(tmp, { force: true });
|
|
218
|
+
}
|
|
43
219
|
}
|
|
44
220
|
/** Synchronous variant for sync call sites (e.g. CLI write handlers). Same guard, same
|
|
45
221
|
* tmp+rename atomicity. */
|
package/dist/core/cli-utils.d.ts
CHANGED
|
@@ -11,6 +11,12 @@ interface ExampleSpec {
|
|
|
11
11
|
description: string;
|
|
12
12
|
command: string;
|
|
13
13
|
}
|
|
14
|
+
interface HelpLabels {
|
|
15
|
+
usage: string;
|
|
16
|
+
commands: string;
|
|
17
|
+
options: string;
|
|
18
|
+
examples: string;
|
|
19
|
+
}
|
|
14
20
|
interface HelpSpec {
|
|
15
21
|
name: string;
|
|
16
22
|
description: string;
|
|
@@ -22,16 +28,23 @@ interface HelpSpec {
|
|
|
22
28
|
commands?: CommandSpec[];
|
|
23
29
|
options?: OptionSpec[];
|
|
24
30
|
examples?: ExampleSpec[];
|
|
31
|
+
labels?: Partial<HelpLabels>;
|
|
25
32
|
}
|
|
26
33
|
declare function formatHelp(spec: HelpSpec): string;
|
|
34
|
+
interface ErrorLabels {
|
|
35
|
+
validValues: string;
|
|
36
|
+
hint: string;
|
|
37
|
+
}
|
|
27
38
|
declare function formatError(message: string, opts?: {
|
|
28
39
|
validValues?: string[];
|
|
29
40
|
hint?: string;
|
|
41
|
+
labels?: Partial<ErrorLabels>;
|
|
30
42
|
}): string;
|
|
43
|
+
declare function readStdinBufferSync(): Buffer;
|
|
31
44
|
declare function readStdinSync(): string;
|
|
32
45
|
/** Create an Error with a cliMessage property for structured CLI error handling */
|
|
33
46
|
declare function cliError(message: string): Error & {
|
|
34
47
|
cliMessage?: string;
|
|
35
48
|
};
|
|
36
|
-
export { formatHelp, formatError, readStdinSync, cliError, };
|
|
37
|
-
export type { HelpSpec, CommandSpec, OptionSpec, ExampleSpec };
|
|
49
|
+
export { formatHelp, formatError, readStdinSync, readStdinBufferSync, cliError, };
|
|
50
|
+
export type { HelpSpec, HelpLabels, CommandSpec, OptionSpec, ExampleSpec };
|
package/dist/core/cli-utils.js
CHANGED
|
@@ -1,63 +1,68 @@
|
|
|
1
|
-
// input: help spec objects, error context, stdin
|
|
2
|
-
// output:
|
|
3
|
-
// pos:
|
|
1
|
+
// input: help spec objects, optional labels, error context, stdin
|
|
2
|
+
// output: localized help/error formatting and stdin readers
|
|
3
|
+
// pos: Shared CLI presentation and stdin utilities
|
|
4
4
|
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
5
|
import * as fs from 'node:fs';
|
|
6
|
-
function
|
|
7
|
-
const lines = [];
|
|
8
|
-
lines.push(spec.description);
|
|
9
|
-
lines.push('');
|
|
10
|
-
lines.push(`Usage: ${spec.usage}`);
|
|
6
|
+
function appendCommands(lines, spec, label) {
|
|
11
7
|
if (spec.commandGroups && spec.commandGroups.length > 0) {
|
|
12
|
-
lines.push('');
|
|
13
|
-
lines.push('Commands:');
|
|
8
|
+
lines.push('', label);
|
|
14
9
|
for (const group of spec.commandGroups) {
|
|
15
10
|
lines.push(` ${group.heading}:`);
|
|
16
|
-
for (const cmd of group.commands)
|
|
11
|
+
for (const cmd of group.commands)
|
|
17
12
|
lines.push(` ${cmd.name.padEnd(22)} ${cmd.description}`);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
else if (spec.commands && spec.commands.length > 0) {
|
|
22
|
-
lines.push('');
|
|
23
|
-
lines.push('Commands:');
|
|
24
|
-
for (const cmd of spec.commands) {
|
|
25
|
-
lines.push(` ${cmd.name.padEnd(22)} ${cmd.description}`);
|
|
26
13
|
}
|
|
14
|
+
return;
|
|
27
15
|
}
|
|
28
|
-
if (spec.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
16
|
+
if (!spec.commands || spec.commands.length === 0)
|
|
17
|
+
return;
|
|
18
|
+
lines.push('', label);
|
|
19
|
+
for (const cmd of spec.commands)
|
|
20
|
+
lines.push(` ${cmd.name.padEnd(22)} ${cmd.description}`);
|
|
21
|
+
}
|
|
22
|
+
function appendOptions(lines, options, label) {
|
|
23
|
+
if (!options || options.length === 0)
|
|
24
|
+
return;
|
|
25
|
+
lines.push('', label);
|
|
26
|
+
for (const option of options) {
|
|
27
|
+
const defaultText = option.default != null ? ` (default: ${option.default})` : '';
|
|
28
|
+
lines.push(` ${option.flag.padEnd(28)} ${option.description}${defaultText}`);
|
|
35
29
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
30
|
+
}
|
|
31
|
+
function appendExamples(lines, examples, label) {
|
|
32
|
+
if (!examples || examples.length === 0)
|
|
33
|
+
return;
|
|
34
|
+
lines.push('', label);
|
|
35
|
+
for (const example of examples) {
|
|
36
|
+
lines.push(` # ${example.description}`, ` ${example.command}`, '');
|
|
44
37
|
}
|
|
38
|
+
}
|
|
39
|
+
function formatHelp(spec) {
|
|
40
|
+
const labels = {
|
|
41
|
+
usage: 'Usage:', commands: 'Commands:', options: 'Options:', examples: 'Examples:',
|
|
42
|
+
...spec.labels,
|
|
43
|
+
};
|
|
44
|
+
const lines = [spec.description, '', `${labels.usage} ${spec.usage}`];
|
|
45
|
+
appendCommands(lines, spec, labels.commands);
|
|
46
|
+
appendOptions(lines, spec.options, labels.options);
|
|
47
|
+
appendExamples(lines, spec.examples, labels.examples);
|
|
45
48
|
return lines.join('\n').trimEnd();
|
|
46
49
|
}
|
|
47
|
-
// ─── Error Formatting (Rule ④) ─────────────────────────────────
|
|
48
50
|
function formatError(message, opts) {
|
|
51
|
+
const labels = { validValues: 'Valid values:', hint: 'Hint:', ...opts?.labels };
|
|
49
52
|
let result = message;
|
|
50
53
|
if (opts?.validValues && opts.validValues.length > 0) {
|
|
51
|
-
result += `\
|
|
52
|
-
}
|
|
53
|
-
if (opts?.hint) {
|
|
54
|
-
result += `\nHint: ${opts.hint}`;
|
|
54
|
+
result += `\n${labels.validValues} ${opts.validValues.join(', ')}`;
|
|
55
55
|
}
|
|
56
|
+
if (opts?.hint)
|
|
57
|
+
result += `\n${labels.hint} ${opts.hint}`;
|
|
56
58
|
return result;
|
|
57
59
|
}
|
|
58
60
|
// ─── Stdin Reading (Rule ③) ────────────────────────────────────
|
|
61
|
+
function readStdinBufferSync() {
|
|
62
|
+
return fs.readFileSync(0);
|
|
63
|
+
}
|
|
59
64
|
function readStdinSync() {
|
|
60
|
-
return
|
|
65
|
+
return readStdinBufferSync().toString('utf8');
|
|
61
66
|
}
|
|
62
67
|
// ─── CLI Error Helper ─────────────────────────────────────────
|
|
63
68
|
/** Create an Error with a cliMessage property for structured CLI error handling */
|
|
@@ -66,4 +71,4 @@ function cliError(message) {
|
|
|
66
71
|
error.cliMessage = message;
|
|
67
72
|
return error;
|
|
68
73
|
}
|
|
69
|
-
export { formatHelp, formatError, readStdinSync, cliError, };
|
|
74
|
+
export { formatHelp, formatError, readStdinSync, readStdinBufferSync, cliError, };
|
|
@@ -8,6 +8,10 @@ export declare function buildTasksConfig(serverRoot: string): object;
|
|
|
8
8
|
export declare function buildManagerQaConfig(serverRoot: string): object;
|
|
9
9
|
/** Thread lifecycle control, loaded only for thread sessions. */
|
|
10
10
|
export declare function buildThreadConfig(serverRoot: string): object;
|
|
11
|
+
/** Strict empty composition with no declared MCP servers. */
|
|
12
|
+
export declare function buildEmptyConfig(): object;
|
|
13
|
+
/** Benchmark composition declaration; the server implementation is supplied separately. */
|
|
14
|
+
export declare function buildBenchmarkThreadConfig(serverRoot: string): object;
|
|
11
15
|
/** TUI MCP config — loaded ONLY by Claude TUI-mode sessions (DR-0012). Isolated tool set:
|
|
12
16
|
* cortex_plan_enter / cortex_plan_exit / cortex_ask_user replace the native
|
|
13
17
|
* EnterPlanMode / ExitPlanMode / AskUserQuestion tools, which are excluded from --tools in TUI mode. */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// input: filesystem, config paths,
|
|
2
|
-
// output:
|
|
3
|
-
// pos:
|
|
1
|
+
// input: filesystem, config paths, server root
|
|
2
|
+
// output: full, restricted, platform MCP configs
|
|
3
|
+
// pos: Generates declared MCP compositions
|
|
4
4
|
// >>> 一旦我被更新,务必更新我的开头注释与所属文件夹 CORTEX.md <<<
|
|
5
5
|
import { writeFileSync } from 'fs';
|
|
6
6
|
import * as path from 'path';
|
|
@@ -12,6 +12,8 @@ const CORE_MCP_CONFIG_PATH = path.join(CONFIG_DIR, 'mcp-config-core.json');
|
|
|
12
12
|
const TASKS_MCP_CONFIG_PATH = path.join(CONFIG_DIR, 'mcp-config-tasks.json');
|
|
13
13
|
const MANAGER_QA_MCP_CONFIG_PATH = path.join(CONFIG_DIR, 'mcp-config-manager-qa.json');
|
|
14
14
|
const THREAD_MCP_CONFIG_PATH = path.join(CONFIG_DIR, 'mcp-config-thread.json');
|
|
15
|
+
const EMPTY_MCP_CONFIG_PATH = path.join(CONFIG_DIR, 'mcp-config-empty.json');
|
|
16
|
+
const BENCHMARK_THREAD_MCP_CONFIG_PATH = path.join(CONFIG_DIR, 'mcp-config-benchmark-thread.json');
|
|
15
17
|
const TUI_MCP_CONFIG_PATH = path.join(CONFIG_DIR, 'mcp-config-tui.json');
|
|
16
18
|
const SLACK_MCP_CONFIG_PATH = path.join(CONFIG_DIR, 'mcp-config-slack.json');
|
|
17
19
|
const FEISHU_MCP_CONFIG_PATH = path.join(CONFIG_DIR, 'mcp-config-feishu.json');
|
|
@@ -71,6 +73,18 @@ export function buildThreadConfig(serverRoot) {
|
|
|
71
73
|
},
|
|
72
74
|
};
|
|
73
75
|
}
|
|
76
|
+
/** Strict empty composition with no declared MCP servers. */
|
|
77
|
+
export function buildEmptyConfig() {
|
|
78
|
+
return { mcpServers: {} };
|
|
79
|
+
}
|
|
80
|
+
/** Benchmark composition declaration; the server implementation is supplied separately. */
|
|
81
|
+
export function buildBenchmarkThreadConfig(serverRoot) {
|
|
82
|
+
return {
|
|
83
|
+
mcpServers: {
|
|
84
|
+
'cortex-benchmark-thread': serverEntry('dist/domain/mcp/benchmark-thread-server.js', serverRoot),
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
74
88
|
/** TUI MCP config — loaded ONLY by Claude TUI-mode sessions (DR-0012). Isolated tool set:
|
|
75
89
|
* cortex_plan_enter / cortex_plan_exit / cortex_ask_user replace the native
|
|
76
90
|
* EnterPlanMode / ExitPlanMode / AskUserQuestion tools, which are excluded from --tools in TUI mode. */
|
|
@@ -115,22 +129,21 @@ export function buildWebConfig(serverRoot) {
|
|
|
115
129
|
};
|
|
116
130
|
}
|
|
117
131
|
export function generateMcpConfig() {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
log.info(`Generated Web MCP config at ${WEB_MCP_CONFIG_PATH}`);
|
|
132
|
+
const configs = [
|
|
133
|
+
['full', MCP_CONFIG_PATH, buildFullConfig(SERVER_ROOT)],
|
|
134
|
+
['core', CORE_MCP_CONFIG_PATH, buildCoreConfig(SERVER_ROOT)],
|
|
135
|
+
['tasks', TASKS_MCP_CONFIG_PATH, buildTasksConfig(SERVER_ROOT)],
|
|
136
|
+
['manager-Q&A', MANAGER_QA_MCP_CONFIG_PATH, buildManagerQaConfig(SERVER_ROOT)],
|
|
137
|
+
['thread', THREAD_MCP_CONFIG_PATH, buildThreadConfig(SERVER_ROOT)],
|
|
138
|
+
['empty', EMPTY_MCP_CONFIG_PATH, buildEmptyConfig()],
|
|
139
|
+
['benchmark thread', BENCHMARK_THREAD_MCP_CONFIG_PATH, buildBenchmarkThreadConfig(SERVER_ROOT)],
|
|
140
|
+
['TUI', TUI_MCP_CONFIG_PATH, buildTuiConfig(SERVER_ROOT)],
|
|
141
|
+
['Slack', SLACK_MCP_CONFIG_PATH, buildSlackConfig(SERVER_ROOT)],
|
|
142
|
+
['Feishu', FEISHU_MCP_CONFIG_PATH, buildFeishuConfig(SERVER_ROOT)],
|
|
143
|
+
['Web', WEB_MCP_CONFIG_PATH, buildWebConfig(SERVER_ROOT)],
|
|
144
|
+
];
|
|
145
|
+
for (const [label, configPath, config] of configs) {
|
|
146
|
+
writeFileSync(configPath, JSON.stringify(config, null, 2));
|
|
147
|
+
log.info(`Generated ${label} MCP config at ${configPath}`);
|
|
148
|
+
}
|
|
136
149
|
}
|
|
@@ -3,6 +3,9 @@ export interface JsonRepoOptions<T> {
|
|
|
3
3
|
defaultValue: () => T;
|
|
4
4
|
/** Optional migration function. Called on every disk read; use to normalise legacy shapes without zod. */
|
|
5
5
|
migrate?: (raw: unknown) => T;
|
|
6
|
+
/** Serialize without pretty-printing. For multi-MB stores the indentation costs ~40% extra
|
|
7
|
+
* stringify CPU on every write (synchronous, blocks the event loop) — opt in for those. */
|
|
8
|
+
compact?: boolean;
|
|
6
9
|
}
|
|
7
10
|
export declare class JsonRepository<T> {
|
|
8
11
|
private readonly opts;
|
|
@@ -30,6 +33,7 @@ export declare class JsonRepository<T> {
|
|
|
30
33
|
* Does not hold the mutex — callers that need serialization should use mutate().
|
|
31
34
|
*/
|
|
32
35
|
write(next: T): Promise<void>;
|
|
36
|
+
private serialize;
|
|
33
37
|
/**
|
|
34
38
|
* Wait until any in-flight `mutate()` has fully completed (tmp written + rename landed).
|
|
35
39
|
* Acquire-release the mutex without doing any work; FIFO ordering guarantees that once this
|