somora 2026.930.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 +364 -0
- package/bin/node-version.d.mts +3 -0
- package/bin/node-version.mjs +45 -0
- package/bin/somora.mjs +77 -0
- package/config.example.yaml +896 -0
- package/docs/agents.md +677 -0
- package/docs/api.md +3194 -0
- package/docs/browser.md +317 -0
- package/docs/builder.md +331 -0
- package/docs/cache-strategy.md +373 -0
- package/docs/compaction.md +253 -0
- package/docs/display.md +154 -0
- package/docs/dream-phases.md +891 -0
- package/docs/files.md +387 -0
- package/docs/imagegen.md +366 -0
- package/docs/images/somora-hero.png +0 -0
- package/docs/images/somora-mobile-2026-05-23.png +0 -0
- package/docs/images/somora-tui-2026-05-23.png +0 -0
- package/docs/images/somora-web-2026-05-23.png +0 -0
- package/docs/index.md +178 -0
- package/docs/lsp.md +93 -0
- package/docs/mcp.md +332 -0
- package/docs/memory.md +407 -0
- package/docs/mobile.md +310 -0
- package/docs/models.md +379 -0
- package/docs/projects.md +425 -0
- package/docs/realtime-voice.md +293 -0
- package/docs/resources.md +230 -0
- package/docs/sampling.md +128 -0
- package/docs/security.md +189 -0
- package/docs/sentinel.md +295 -0
- package/docs/setup.md +1217 -0
- package/docs/skills.md +501 -0
- package/docs/team.md +189 -0
- package/docs/thinking.md +463 -0
- package/docs/tmux.md +399 -0
- package/docs/tools.md +117 -0
- package/docs/videogen.md +191 -0
- package/docs/voice.md +288 -0
- package/docs/web.md +893 -0
- package/docs/wiki.md +583 -0
- package/npm-shrinkwrap.json +5670 -0
- package/package.json +116 -0
- package/src/attachments/store.ts +196 -0
- package/src/browser/change-stream.ts +31 -0
- package/src/browser/display.ts +108 -0
- package/src/browser/policy.ts +121 -0
- package/src/browser/screencast.ts +374 -0
- package/src/browser/service.ts +1337 -0
- package/src/browser/session.ts +15 -0
- package/src/browser/snapshot.ts +40 -0
- package/src/cli/auth.ts +117 -0
- package/src/cli/codex.ts +41 -0
- package/src/cli/lsp.ts +71 -0
- package/src/cli/resolvers/clawhub.ts +400 -0
- package/src/cli/setup.ts +835 -0
- package/src/cli/skill.ts +619 -0
- package/src/cli/somora.ts +548 -0
- package/src/cli/systemd-unit.ts +72 -0
- package/src/cli/team.ts +148 -0
- package/src/cli/tui/activity-stream.ts +121 -0
- package/src/cli/tui/api.ts +479 -0
- package/src/cli/tui/app.tsx +1169 -0
- package/src/cli/tui/autocomplete.tsx +55 -0
- package/src/cli/tui/commands.ts +913 -0
- package/src/cli/tui/dream-glyphs.ts +22 -0
- package/src/cli/tui/footer.tsx +15 -0
- package/src/cli/tui/format.ts +28 -0
- package/src/cli/tui/header.tsx +279 -0
- package/src/cli/tui/index.tsx +120 -0
- package/src/cli/tui/markdown.tsx +63 -0
- package/src/cli/tui/sampling.ts +162 -0
- package/src/cli/tui/separator.tsx +13 -0
- package/src/cli/tui/state.ts +42 -0
- package/src/cli/tui/stream.ts +330 -0
- package/src/cli/tui/thinking-text.ts +54 -0
- package/src/cli/tui/turn-views.tsx +499 -0
- package/src/cli/tui/types.ts +410 -0
- package/src/cli/tui/work-queue.ts +161 -0
- package/src/cli/update-args.ts +55 -0
- package/src/cli/wiki.ts +406 -0
- package/src/compaction/index.ts +28 -0
- package/src/compaction/midturn.ts +210 -0
- package/src/compaction/policy.ts +154 -0
- package/src/compaction/summarize.ts +691 -0
- package/src/compaction/template.ts +90 -0
- package/src/compaction/types.ts +148 -0
- package/src/config/loader.ts +276 -0
- package/src/config/reload.ts +43 -0
- package/src/config/types.ts +2292 -0
- package/src/dream/deep-actions.ts +324 -0
- package/src/dream/deep-dispatcher.ts +237 -0
- package/src/dream/deep-llm.ts +388 -0
- package/src/dream/deep-prompts.ts +106 -0
- package/src/dream/deep-runner.ts +757 -0
- package/src/dream/deep-skip-cache.ts +199 -0
- package/src/dream/deep-worker.ts +229 -0
- package/src/dream/json-salvage.ts +34 -0
- package/src/dream/judge.ts +188 -0
- package/src/dream/loop-state.ts +286 -0
- package/src/dream/lucid-actions.ts +303 -0
- package/src/dream/lucid-batches.ts +94 -0
- package/src/dream/lucid-prompt.ts +82 -0
- package/src/dream/lucid-runner.ts +744 -0
- package/src/dream/lucid-storage.ts +277 -0
- package/src/dream/lucid-structure.ts +102 -0
- package/src/dream/lucid-types.ts +137 -0
- package/src/dream/lucid-worker.ts +224 -0
- package/src/dream/rem-dedup.ts +502 -0
- package/src/dream/rem-extract.ts +782 -0
- package/src/dream/rem-reset-run.ts +67 -0
- package/src/dream/rem-runner.ts +696 -0
- package/src/dream/rem-worker.ts +527 -0
- package/src/dream/review-window.ts +71 -0
- package/src/dream/scheduler-state.ts +129 -0
- package/src/dream/storage.ts +497 -0
- package/src/dream/types.ts +133 -0
- package/src/dream/wiki-context.ts +133 -0
- package/src/engine/a2a.ts +33 -0
- package/src/engine/availability.ts +63 -0
- package/src/engine/claude-cli.ts +969 -0
- package/src/engine/codex-app-server-client.ts +294 -0
- package/src/engine/codex-bin.ts +49 -0
- package/src/engine/codex-cli.ts +1014 -0
- package/src/engine/codex-dynamic-tools.ts +239 -0
- package/src/engine/codex-home.ts +63 -0
- package/src/engine/codex-thread-config.ts +74 -0
- package/src/engine/context-budget.ts +257 -0
- package/src/engine/engine-meta-labels.ts +211 -0
- package/src/engine/grok-cli.ts +941 -0
- package/src/engine/inline-think.ts +59 -0
- package/src/engine/model-availability.ts +105 -0
- package/src/engine/openai-compatible.ts +2464 -0
- package/src/engine/reasoning-retry.ts +83 -0
- package/src/engine/registry.ts +13 -0
- package/src/engine/replay.ts +195 -0
- package/src/engine/sampling.ts +90 -0
- package/src/engine/thinking-params.ts +264 -0
- package/src/engine/types.ts +269 -0
- package/src/engine/user-tag.ts +24 -0
- package/src/imagegen/aspect.ts +128 -0
- package/src/imagegen/generate.ts +642 -0
- package/src/imagegen/references.ts +75 -0
- package/src/imagegen/types.ts +154 -0
- package/src/lsp/client.ts +183 -0
- package/src/lsp/fake-server.helper.mjs +42 -0
- package/src/lsp/format.ts +29 -0
- package/src/lsp/index.ts +21 -0
- package/src/lsp/manager.ts +238 -0
- package/src/lsp/registry.ts +151 -0
- package/src/mcp/config.ts +117 -0
- package/src/mcp/hub/audit.ts +55 -0
- package/src/mcp/hub/bridge.ts +115 -0
- package/src/mcp/hub/catalog.ts +69 -0
- package/src/mcp/hub/credentials.ts +406 -0
- package/src/mcp/hub/env-expand.ts +14 -0
- package/src/mcp/hub/manager.ts +770 -0
- package/src/mcp/hub/normalize.ts +213 -0
- package/src/mcp/server.ts +368 -0
- package/src/mcp/tool-schema.ts +20 -0
- package/src/media/capabilities.ts +392 -0
- package/src/media/records.ts +175 -0
- package/src/media/store.ts +217 -0
- package/src/media/types.ts +69 -0
- package/src/memory/chunking.ts +168 -0
- package/src/memory/embeddings.ts +262 -0
- package/src/memory/index.ts +15 -0
- package/src/memory/inject.ts +197 -0
- package/src/memory/manager.ts +996 -0
- package/src/memory/registry.ts +176 -0
- package/src/memory/rescan.ts +101 -0
- package/src/memory/retrieval.ts +586 -0
- package/src/memory/shared-index.ts +372 -0
- package/src/memory/slug.ts +39 -0
- package/src/memory/storage.ts +385 -0
- package/src/memory/watcher.ts +134 -0
- package/src/multimodal/blocks.ts +101 -0
- package/src/multimodal/dimensions.ts +214 -0
- package/src/multimodal/load.ts +86 -0
- package/src/multimodal/mime.ts +135 -0
- package/src/multimodal/pdf-render.ts +80 -0
- package/src/multimodal/user-content.ts +266 -0
- package/src/persona/files.ts +144 -0
- package/src/persona/loader.ts +598 -0
- package/src/persona/skill-gating-store.ts +59 -0
- package/src/persona/tool-gating-store.ts +113 -0
- package/src/projects/focus.ts +186 -0
- package/src/projects/prompt-block.ts +106 -0
- package/src/projects/scheme.ts +107 -0
- package/src/projects/store.ts +144 -0
- package/src/projects/types.ts +73 -0
- package/src/sentinel/dispatcher.ts +97 -0
- package/src/sentinel/schedule.ts +271 -0
- package/src/sentinel/scheduler.ts +840 -0
- package/src/sentinel/store.ts +259 -0
- package/src/sentinel/types.ts +221 -0
- package/src/server/activity-stream.ts +160 -0
- package/src/server/ask-calls.ts +137 -0
- package/src/server/ask-wait-graph.ts +171 -0
- package/src/server/async-tasks.ts +199 -0
- package/src/server/builder-busy.ts +86 -0
- package/src/server/builder-prompt.ts +204 -0
- package/src/server/builder-questions.ts +117 -0
- package/src/server/builder-session.ts +249 -0
- package/src/server/chat-aborts.ts +83 -0
- package/src/server/claude-config-dir.ts +71 -0
- package/src/server/claude-credential-sync.ts +454 -0
- package/src/server/env-file.ts +100 -0
- package/src/server/env.ts +115 -0
- package/src/server/heal-session.ts +151 -0
- package/src/server/history-page.ts +46 -0
- package/src/server/index.ts +7244 -0
- package/src/server/lockfile.ts +89 -0
- package/src/server/logger.ts +67 -0
- package/src/server/logs.ts +187 -0
- package/src/server/loopback-fetch.ts +130 -0
- package/src/server/openai-client.ts +89 -0
- package/src/server/prompt-assembly.ts +334 -0
- package/src/server/restart-reconcile.ts +139 -0
- package/src/server/run-turn-fallback.ts +275 -0
- package/src/server/run-turn-types.ts +91 -0
- package/src/server/run-turn.ts +1474 -0
- package/src/server/sanitize-assistant-text.ts +112 -0
- package/src/server/session-export.ts +220 -0
- package/src/server/session-queue.ts +376 -0
- package/src/server/session-workdir.ts +49 -0
- package/src/server/sse-h2-safe.ts +61 -0
- package/src/server/sse-liveness.ts +162 -0
- package/src/server/sse-serializer.ts +161 -0
- package/src/server/start-turn.ts +249 -0
- package/src/server/steer-inbox.ts +107 -0
- package/src/server/tls-keeper.ts +153 -0
- package/src/server/tool-format.ts +167 -0
- package/src/server/turn-framing.ts +52 -0
- package/src/server/turn-origin-kind.ts +74 -0
- package/src/server/turn-origin.ts +40 -0
- package/src/server/work-ledger.ts +975 -0
- package/src/server/workspace.ts +114 -0
- package/src/setup/config-edit.ts +201 -0
- package/src/setup/presets.ts +71 -0
- package/src/setup/prompt.ts +163 -0
- package/src/setup/tailscale.ts +44 -0
- package/src/skills/bin-checks.ts +227 -0
- package/src/skills/bootstrap.ts +347 -0
- package/src/skills/env-scope.ts +175 -0
- package/src/skills/gating.ts +71 -0
- package/src/skills/lint.ts +202 -0
- package/src/skills/load.ts +368 -0
- package/src/skills/path-helpers.ts +102 -0
- package/src/skills/registry.ts +178 -0
- package/src/ssh/exec.ts +142 -0
- package/src/ssh/index.ts +12 -0
- package/src/ssh/known-hosts.ts +109 -0
- package/src/ssh/pool.ts +307 -0
- package/src/storage/sessions.ts +602 -0
- package/src/team/render.ts +149 -0
- package/src/team/resolve.ts +122 -0
- package/src/team/store.ts +124 -0
- package/src/team/types.ts +111 -0
- package/src/team/write.ts +95 -0
- package/src/testing/isolated-home.mts +46 -0
- package/src/tmux/attention-watcher.ts +272 -0
- package/src/tmux/attention.ts +336 -0
- package/src/tmux/list.ts +91 -0
- package/src/tmux/origin-store.ts +102 -0
- package/src/tmux/tui-markers.ts +267 -0
- package/src/tools/agents/ask-cancel.ts +94 -0
- package/src/tools/agents/ask-result.ts +235 -0
- package/src/tools/agents/ask.ts +704 -0
- package/src/tools/agents/builder-dispatch.ts +217 -0
- package/src/tools/agents/images.ts +104 -0
- package/src/tools/agents/index.ts +42 -0
- package/src/tools/agents/long-task-timeouts.ts +39 -0
- package/src/tools/agents/session-list.ts +136 -0
- package/src/tools/agents/session-model.ts +106 -0
- package/src/tools/agents/spawn.ts +845 -0
- package/src/tools/agents/status.ts +589 -0
- package/src/tools/browser/index.ts +6 -0
- package/src/tools/browser/ops.ts +145 -0
- package/src/tools/browser/tools.ts +184 -0
- package/src/tools/builder/short-descriptions.ts +80 -0
- package/src/tools/builder/tools.ts +222 -0
- package/src/tools/docs/index.ts +1 -0
- package/src/tools/docs/tools.ts +228 -0
- package/src/tools/dream/index.ts +11 -0
- package/src/tools/dream/tools.ts +1002 -0
- package/src/tools/exec/allowlist.ts +233 -0
- package/src/tools/exec/background-detach.helper.mts +27 -0
- package/src/tools/exec/blacklist.ts +336 -0
- package/src/tools/exec/concurrency.ts +150 -0
- package/src/tools/exec/fit-output.ts +82 -0
- package/src/tools/exec/index.ts +7 -0
- package/src/tools/exec/internal-env.ts +89 -0
- package/src/tools/exec/job-store.ts +403 -0
- package/src/tools/exec/local.ts +649 -0
- package/src/tools/exec/remote.ts +399 -0
- package/src/tools/exec/tools.ts +934 -0
- package/src/tools/file/analyze.ts +378 -0
- package/src/tools/file/index.ts +2 -0
- package/src/tools/file/local.ts +707 -0
- package/src/tools/file/lsp-hook.ts +72 -0
- package/src/tools/file/persona-guard.ts +61 -0
- package/src/tools/file/policy.ts +292 -0
- package/src/tools/file/read-format.ts +129 -0
- package/src/tools/file/remote.ts +680 -0
- package/src/tools/file/replace.ts +507 -0
- package/src/tools/file/search-window.ts +224 -0
- package/src/tools/file/tools.ts +658 -0
- package/src/tools/file/write-scope.ts +182 -0
- package/src/tools/gating.ts +106 -0
- package/src/tools/image/budget.ts +82 -0
- package/src/tools/image/index.ts +7 -0
- package/src/tools/image/tools.ts +601 -0
- package/src/tools/index.ts +99 -0
- package/src/tools/memory/index.ts +12 -0
- package/src/tools/memory/tools.ts +399 -0
- package/src/tools/projects/index.ts +1 -0
- package/src/tools/projects/tools.ts +643 -0
- package/src/tools/registry.ts +427 -0
- package/src/tools/resources/index.ts +8 -0
- package/src/tools/resources/tools.ts +176 -0
- package/src/tools/resources/visibility.ts +60 -0
- package/src/tools/sentinel/index.ts +1 -0
- package/src/tools/sentinel/tools.ts +592 -0
- package/src/tools/skill/index.ts +4 -0
- package/src/tools/skill/tools.ts +285 -0
- package/src/tools/time/index.ts +5 -0
- package/src/tools/time/tools.ts +123 -0
- package/src/tools/tmux/index.ts +1 -0
- package/src/tools/tmux/local.ts +277 -0
- package/src/tools/tmux/remote.ts +168 -0
- package/src/tools/tmux/tools.ts +1064 -0
- package/src/tools/tool-output.ts +97 -0
- package/src/tools/types.ts +227 -0
- package/src/tools/video/index.ts +1 -0
- package/src/tools/video/tools.ts +425 -0
- package/src/tools/web/fetch.ts +337 -0
- package/src/tools/web/index.ts +4 -0
- package/src/tools/web/tools.ts +186 -0
- package/src/tools/wiki/index.ts +1 -0
- package/src/tools/wiki/tools.ts +445 -0
- package/src/tts/cache-gc.ts +128 -0
- package/src/tts/prepare-for-tts.ts +114 -0
- package/src/tts/service.ts +332 -0
- package/src/types/events.ts +695 -0
- package/src/types/turn-origin.ts +53 -0
- package/src/version.ts +21 -0
- package/src/videogen/dialects.ts +222 -0
- package/src/videogen/generate.ts +484 -0
- package/src/videogen/jobs.ts +178 -0
- package/src/videogen/runner.ts +125 -0
- package/src/videogen/wake.ts +119 -0
- package/src/voice/realtime/call.ts +948 -0
- package/src/voice/realtime/consult.ts +293 -0
- package/src/voice/realtime/fake-provider.ts +134 -0
- package/src/voice/realtime/manager.ts +384 -0
- package/src/voice/realtime/openai-provider.ts +471 -0
- package/src/voice/realtime/persona.ts +227 -0
- package/src/voice/realtime/session-match.ts +116 -0
- package/src/voice/realtime/types.ts +182 -0
- package/src/wiki/conflict.ts +113 -0
- package/src/wiki/explorer.ts +578 -0
- package/src/wiki/index-builder.ts +172 -0
- package/src/wiki/language.ts +134 -0
- package/src/wiki/log-builder.ts +150 -0
- package/src/wiki/map.ts +258 -0
- package/src/wiki/migration/analyze.ts +304 -0
- package/src/wiki/migration/execute.ts +689 -0
- package/src/wiki/migration/refine.ts +402 -0
- package/src/wiki/migration/store.ts +100 -0
- package/src/wiki/move-page.ts +57 -0
- package/src/wiki/structure-file.ts +192 -0
- package/src/wiki/taxonomy.ts +140 -0
- package/src/wiki/templates.ts +163 -0
- package/src/wiki/types.ts +133 -0
- package/templates/skills/README.md +24 -0
- package/templates/skills/_templates/api-wrapper/SKILL.md +26 -0
- package/templates/skills/_templates/cli-wrapper/BOOTSTRAP.md +32 -0
- package/templates/skills/_templates/cli-wrapper/SKILL.md +24 -0
- package/templates/skills/_templates/default/SKILL.md +25 -0
- package/templates/skills/builder-handover/SKILL.md +96 -0
- package/templates/skills/builder-handover/assets/ORDER.md +12 -0
- package/templates/skills/skill-author/SKILL.md +110 -0
- package/tsconfig.json +22 -0
- package/web/dist/assets/index-BaVv6pB2.js +631 -0
- package/web/dist/assets/index-BaVv6pB2.js.map +1 -0
- package/web/dist/assets/index-V3FV3Z25.css +32 -0
- package/web/dist/favicon.svg +8 -0
- package/web/dist/index.html +20 -0
- package/web/package.json +36 -0
- package/web/tsconfig.json +25 -0
- package/web-mobile/dist/apple-touch-icon.png +0 -0
- package/web-mobile/dist/assets/index-BqKt7IMx.css +1 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js +88 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js.map +1 -0
- package/web-mobile/dist/favicon.svg +8 -0
- package/web-mobile/dist/icon-192.png +0 -0
- package/web-mobile/dist/icon-512.png +0 -0
- package/web-mobile/dist/index.html +27 -0
- package/web-mobile/dist/manifest.webmanifest +32 -0
- package/web-mobile/dist/service-worker.js +90 -0
- package/web-mobile/package.json +28 -0
- package/web-mobile/tsconfig.json +25 -0
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
// Disk-tracked background-job store for the `exec` tool. Per-job
|
|
2
|
+
// directory under ~/.somora/agents/<agent>/exec-jobs/<job_id>/ with:
|
|
3
|
+
//
|
|
4
|
+
// meta.json — job metadata + state (running/done/failed/killed)
|
|
5
|
+
// stdout.log — full stdout, append-only
|
|
6
|
+
// stderr.log — full stderr, append-only
|
|
7
|
+
// exit — integer exit code, written when process ends
|
|
8
|
+
//
|
|
9
|
+
// Survives server restart: jobs are fully detached (own process
|
|
10
|
+
// group, log files as raw FDs, exit-code file) so a running job
|
|
11
|
+
// legitimately outlives somora/MCP restarts. recoverOrphanedJobs()
|
|
12
|
+
// at boot and probeLocalJob() on poll/list resolve the real state
|
|
13
|
+
// cross-process (exit file → real code; PID probe → running/gone).
|
|
14
|
+
//
|
|
15
|
+
// Note: stdout.log / stderr.log are unbounded on disk — we never
|
|
16
|
+
// truncate the source-of-truth. The 256 KB output cap applies only
|
|
17
|
+
// to in-memory reads (process.log tail, sync exec result). For
|
|
18
|
+
// huge log files the model is expected to use file_read with
|
|
19
|
+
// offset/limit to page through.
|
|
20
|
+
|
|
21
|
+
import { mkdir, readdir, readFile, rename, stat, writeFile } from 'node:fs/promises';
|
|
22
|
+
import { homedir } from 'node:os';
|
|
23
|
+
import { join } from 'node:path';
|
|
24
|
+
import { logger } from '../../server/logger.ts';
|
|
25
|
+
|
|
26
|
+
const SOMORA_HOME = process.env.SOMORA_HOME ?? join(homedir(), '.somora');
|
|
27
|
+
|
|
28
|
+
export type JobState = 'running' | 'done' | 'failed' | 'killed';
|
|
29
|
+
|
|
30
|
+
export interface JobMeta {
|
|
31
|
+
job_id: string;
|
|
32
|
+
agent: string;
|
|
33
|
+
/** 'local' or a configured resource name. */
|
|
34
|
+
target: string;
|
|
35
|
+
command: string;
|
|
36
|
+
cwd?: string;
|
|
37
|
+
description?: string;
|
|
38
|
+
started_at: number;
|
|
39
|
+
state: JobState;
|
|
40
|
+
/** Local PID — null for remote jobs (we don't track remote PIDs). */
|
|
41
|
+
pid: number | null;
|
|
42
|
+
ended_at?: number;
|
|
43
|
+
exit_code?: number | null;
|
|
44
|
+
error?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function jobsDir(agent: string): string {
|
|
48
|
+
return join(SOMORA_HOME, 'agents', agent, 'exec-jobs');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function jobDir(agent: string, jobId: string): string {
|
|
52
|
+
return join(jobsDir(agent), jobId);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Generate a process-wide-unique job id. Format: `job_<msTs>_<rand4>`.
|
|
57
|
+
* 4-char random suffix on the millisecond timestamp gives ~17M
|
|
58
|
+
* collision-resistance for same-ms jobs which is plenty (we have
|
|
59
|
+
* single-digit concurrent jobs per agent realistically).
|
|
60
|
+
*/
|
|
61
|
+
export function newJobId(): string {
|
|
62
|
+
const ms = Date.now();
|
|
63
|
+
const rand = Math.random().toString(36).slice(2, 6).padEnd(4, '0');
|
|
64
|
+
return `job_${ms}_${rand}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function ensureJobDir(agent: string, jobId: string): Promise<string> {
|
|
68
|
+
const dir = jobDir(agent, jobId);
|
|
69
|
+
await mkdir(dir, { recursive: true });
|
|
70
|
+
return dir;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function writeJsonAtomic(path: string, value: unknown): Promise<void> {
|
|
74
|
+
// Per-call unique tmp so a `completeJob` and a `markJobKilled` racing
|
|
75
|
+
// on the same job meta.json don't share `<path>.tmp` and ENOENT each
|
|
76
|
+
// other. Audit 2026-05-16.
|
|
77
|
+
const tmp = `${path}.tmp.${process.pid}.${Date.now().toString(36)}.${Math.random().toString(36).slice(2, 8)}`;
|
|
78
|
+
await writeFile(tmp, JSON.stringify(value, null, 2), 'utf8');
|
|
79
|
+
await rename(tmp, path);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function writeMeta(agent: string, meta: JobMeta): Promise<void> {
|
|
83
|
+
const dir = await ensureJobDir(agent, meta.job_id);
|
|
84
|
+
await writeJsonAtomic(join(dir, 'meta.json'), meta);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function readMeta(agent: string, jobId: string): Promise<JobMeta | null> {
|
|
88
|
+
try {
|
|
89
|
+
const raw = await readFile(join(jobDir(agent, jobId), 'meta.json'), 'utf8');
|
|
90
|
+
return JSON.parse(raw) as JobMeta;
|
|
91
|
+
} catch (err) {
|
|
92
|
+
if ((err as NodeJS.ErrnoException).code === 'ENOENT') return null;
|
|
93
|
+
throw err;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Register a freshly-spawned background job. Caller should kick off
|
|
99
|
+
* the actual process AFTER this returns so the meta exists on disk
|
|
100
|
+
* before any output streams in.
|
|
101
|
+
*/
|
|
102
|
+
export async function registerJob(meta: JobMeta): Promise<void> {
|
|
103
|
+
await writeMeta(meta.agent, meta);
|
|
104
|
+
logger.info({
|
|
105
|
+
msg: 'exec.job.registered',
|
|
106
|
+
job_id: meta.job_id,
|
|
107
|
+
agent: meta.agent,
|
|
108
|
+
target: meta.target,
|
|
109
|
+
command_head: meta.command.slice(0, 80),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Mark a job as done with the captured exit code. Idempotent —
|
|
115
|
+
* second call is a no-op.
|
|
116
|
+
*/
|
|
117
|
+
export async function completeJob(
|
|
118
|
+
agent: string,
|
|
119
|
+
jobId: string,
|
|
120
|
+
exitCode: number | null,
|
|
121
|
+
): Promise<void> {
|
|
122
|
+
const meta = await readMeta(agent, jobId);
|
|
123
|
+
if (!meta) return;
|
|
124
|
+
if (meta.state !== 'running') return;
|
|
125
|
+
meta.state = exitCode === 0 ? 'done' : 'failed';
|
|
126
|
+
meta.exit_code = exitCode;
|
|
127
|
+
meta.ended_at = Date.now();
|
|
128
|
+
await writeMeta(agent, meta);
|
|
129
|
+
logger.info({
|
|
130
|
+
msg: 'exec.job.completed',
|
|
131
|
+
job_id: jobId,
|
|
132
|
+
agent,
|
|
133
|
+
state: meta.state,
|
|
134
|
+
exit_code: exitCode,
|
|
135
|
+
ms: meta.ended_at - meta.started_at,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Mark a job as failed with an error message. Use for spawn failures
|
|
141
|
+
* or uncaught exceptions in the streaming path.
|
|
142
|
+
*/
|
|
143
|
+
export async function failJob(
|
|
144
|
+
agent: string,
|
|
145
|
+
jobId: string,
|
|
146
|
+
errorMessage: string,
|
|
147
|
+
): Promise<void> {
|
|
148
|
+
const meta = await readMeta(agent, jobId);
|
|
149
|
+
if (!meta) return;
|
|
150
|
+
if (meta.state !== 'running') return;
|
|
151
|
+
meta.state = 'failed';
|
|
152
|
+
meta.error = errorMessage;
|
|
153
|
+
meta.ended_at = Date.now();
|
|
154
|
+
await writeMeta(agent, meta);
|
|
155
|
+
logger.warn({
|
|
156
|
+
msg: 'exec.job.failed',
|
|
157
|
+
job_id: jobId,
|
|
158
|
+
agent,
|
|
159
|
+
err: errorMessage,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Mark a job as killed (via process_kill action). The actual kill
|
|
165
|
+
* signaling happens in local.ts; this just updates state.
|
|
166
|
+
*/
|
|
167
|
+
export async function markJobKilled(
|
|
168
|
+
agent: string,
|
|
169
|
+
jobId: string,
|
|
170
|
+
signal: string,
|
|
171
|
+
): Promise<void> {
|
|
172
|
+
const meta = await readMeta(agent, jobId);
|
|
173
|
+
if (!meta) return;
|
|
174
|
+
if (meta.state !== 'running') return;
|
|
175
|
+
meta.state = 'killed';
|
|
176
|
+
meta.error = `killed with ${signal}`;
|
|
177
|
+
meta.ended_at = Date.now();
|
|
178
|
+
await writeMeta(agent, meta);
|
|
179
|
+
logger.info({ msg: 'exec.job.killed', job_id: jobId, agent, signal });
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* List all jobs for an agent, newest first by started_at.
|
|
184
|
+
*/
|
|
185
|
+
export async function listJobsForAgent(agent: string): Promise<JobMeta[]> {
|
|
186
|
+
const dir = jobsDir(agent);
|
|
187
|
+
let entries: string[];
|
|
188
|
+
try {
|
|
189
|
+
entries = await readdir(dir);
|
|
190
|
+
} catch (err) {
|
|
191
|
+
if ((err as NodeJS.ErrnoException).code === 'ENOENT') return [];
|
|
192
|
+
throw err;
|
|
193
|
+
}
|
|
194
|
+
const out: JobMeta[] = [];
|
|
195
|
+
for (const e of entries) {
|
|
196
|
+
if (!e.startsWith('job_')) continue;
|
|
197
|
+
const meta = await readMeta(agent, e);
|
|
198
|
+
if (meta) out.push(meta);
|
|
199
|
+
}
|
|
200
|
+
out.sort((a, b) => b.started_at - a.started_at);
|
|
201
|
+
return out;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Read a tail of stdout/stderr for a job. Returns the LAST
|
|
206
|
+
* `maxBytesPerStream` bytes of each (default 256 KB), with a
|
|
207
|
+
* truncated:true flag when the file was bigger.
|
|
208
|
+
*/
|
|
209
|
+
export async function readJobLog(
|
|
210
|
+
agent: string,
|
|
211
|
+
jobId: string,
|
|
212
|
+
opts: { maxBytesPerStream?: number } = {},
|
|
213
|
+
): Promise<{
|
|
214
|
+
job_id: string;
|
|
215
|
+
stdout_tail: string;
|
|
216
|
+
stderr_tail: string;
|
|
217
|
+
total_stdout_bytes: number;
|
|
218
|
+
total_stderr_bytes: number;
|
|
219
|
+
truncated: boolean;
|
|
220
|
+
} | null> {
|
|
221
|
+
const meta = await readMeta(agent, jobId);
|
|
222
|
+
if (!meta) return null;
|
|
223
|
+
const cap = opts.maxBytesPerStream ?? 256 * 1024;
|
|
224
|
+
|
|
225
|
+
const tailOf = async (name: 'stdout.log' | 'stderr.log'): Promise<{ tail: string; total: number; truncated: boolean }> => {
|
|
226
|
+
const path = join(jobDir(agent, jobId), name);
|
|
227
|
+
try {
|
|
228
|
+
const st = await stat(path);
|
|
229
|
+
const total = st.size;
|
|
230
|
+
// For small files: read whole. For big: read last `cap` bytes.
|
|
231
|
+
// node has no easy "read last N bytes" — we just read the whole
|
|
232
|
+
// and slice. For multi-MB log files this is wasteful but jobs
|
|
233
|
+
// that produce that much output are rare; if it becomes a
|
|
234
|
+
// problem we can switch to fs.read with offset.
|
|
235
|
+
const buf = await readFile(path);
|
|
236
|
+
if (buf.length <= cap) {
|
|
237
|
+
return { tail: buf.toString('utf8'), total, truncated: false };
|
|
238
|
+
}
|
|
239
|
+
const tail = buf.subarray(buf.length - cap).toString('utf8');
|
|
240
|
+
return { tail, total, truncated: true };
|
|
241
|
+
} catch (err) {
|
|
242
|
+
if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
243
|
+
return { tail: '', total: 0, truncated: false };
|
|
244
|
+
}
|
|
245
|
+
throw err;
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const [stdout, stderr] = await Promise.all([tailOf('stdout.log'), tailOf('stderr.log')]);
|
|
250
|
+
return {
|
|
251
|
+
job_id: jobId,
|
|
252
|
+
stdout_tail: stdout.tail,
|
|
253
|
+
stderr_tail: stderr.tail,
|
|
254
|
+
total_stdout_bytes: stdout.total,
|
|
255
|
+
total_stderr_bytes: stderr.total,
|
|
256
|
+
truncated: stdout.truncated || stderr.truncated,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Resolve the REAL state of a local background job whose meta says
|
|
262
|
+
* 'running'. Local jobs are fully detached (own process group, log
|
|
263
|
+
* files as raw FDs, exit-code file written by the wrapper shell), so
|
|
264
|
+
* they survive somora/MCP restarts — which also means the in-process
|
|
265
|
+
* 'exit' listener that normally updates meta may be long gone. This
|
|
266
|
+
* probe is the cross-process source of truth:
|
|
267
|
+
*
|
|
268
|
+
* 1. <jobdir>/exit exists → wrapper finished; complete with
|
|
269
|
+
* that code (even if a recycled PID
|
|
270
|
+
* happens to be alive).
|
|
271
|
+
* 2. no exit file, PID alive → still running, meta untouched.
|
|
272
|
+
* 3. no exit file, PID gone → died without a trace (SIGKILL,
|
|
273
|
+
* OOM, host reboot); mark failed.
|
|
274
|
+
*
|
|
275
|
+
* Called from process poll/list and from boot recovery — NEVER trust
|
|
276
|
+
* a bare meta.state==='running' for a local job without this probe
|
|
277
|
+
* (2026-07-27 report: poll said running for a PID that was gone).
|
|
278
|
+
*
|
|
279
|
+
* Returns the fresh meta (re-read after any state change).
|
|
280
|
+
*/
|
|
281
|
+
export async function probeLocalJob(agent: string, meta: JobMeta): Promise<JobMeta> {
|
|
282
|
+
if (meta.state !== 'running' || meta.target !== 'local' || meta.pid == null) {
|
|
283
|
+
return meta;
|
|
284
|
+
}
|
|
285
|
+
let exitCode: number | null = null;
|
|
286
|
+
let hasExitFile = false;
|
|
287
|
+
try {
|
|
288
|
+
const raw = await readFile(join(jobDir(agent, meta.job_id), 'exit'), 'utf8');
|
|
289
|
+
const n = Number.parseInt(raw.trim(), 10);
|
|
290
|
+
if (Number.isFinite(n)) {
|
|
291
|
+
exitCode = n;
|
|
292
|
+
hasExitFile = true;
|
|
293
|
+
}
|
|
294
|
+
} catch {
|
|
295
|
+
// no exit file (yet) — fall through to the PID probe
|
|
296
|
+
}
|
|
297
|
+
if (!hasExitFile) {
|
|
298
|
+
let alive = false;
|
|
299
|
+
try {
|
|
300
|
+
process.kill(meta.pid, 0);
|
|
301
|
+
alive = true;
|
|
302
|
+
} catch {
|
|
303
|
+
// ESRCH/EPERM → treat as gone
|
|
304
|
+
}
|
|
305
|
+
if (alive) return meta;
|
|
306
|
+
await failJob(
|
|
307
|
+
agent,
|
|
308
|
+
meta.job_id,
|
|
309
|
+
'process gone without exit record (external kill, OOM, or host reboot)',
|
|
310
|
+
);
|
|
311
|
+
} else {
|
|
312
|
+
await completeJob(agent, meta.job_id, exitCode);
|
|
313
|
+
}
|
|
314
|
+
clearLivePid(meta.job_id);
|
|
315
|
+
clearLiveStdin(meta.job_id);
|
|
316
|
+
return (await readMeta(agent, meta.job_id)) ?? meta;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Server-start recovery — analog to recoverOrphanRunningDreams, but
|
|
321
|
+
* since 2026-08 background jobs are fully detached and legitimately
|
|
322
|
+
* SURVIVE restarts. So instead of blanket-failing every running job:
|
|
323
|
+
*
|
|
324
|
+
* - local jobs: probeLocalJob resolves the truth (exit file →
|
|
325
|
+
* done/failed with real code; PID alive → adopted, stays running
|
|
326
|
+
* and killable via meta.pid; PID gone → failed). An adopted job
|
|
327
|
+
* has no live stdin handle — process_write reports that.
|
|
328
|
+
* - remote jobs: left as 'running'; they use nohup/setsid on the
|
|
329
|
+
* resource and the next process poll actively probes them via
|
|
330
|
+
* ssh (kill -0 + exit-file), which is too slow for boot.
|
|
331
|
+
*
|
|
332
|
+
* Concurrency note: in-memory slot counters reset on restart —
|
|
333
|
+
* per-agent cap enforcement therefore counts running jobs from DISK
|
|
334
|
+
* (post-probe) at spawn time, see tools.ts background path.
|
|
335
|
+
*/
|
|
336
|
+
export async function recoverOrphanedJobs(agents: string[]): Promise<number> {
|
|
337
|
+
let adopted = 0;
|
|
338
|
+
let resolved = 0;
|
|
339
|
+
for (const agent of agents) {
|
|
340
|
+
const jobs = await listJobsForAgent(agent);
|
|
341
|
+
for (const meta of jobs) {
|
|
342
|
+
if (meta.state !== 'running') continue;
|
|
343
|
+
if (meta.target !== 'local') continue;
|
|
344
|
+
try {
|
|
345
|
+
const fresh = await probeLocalJob(agent, meta);
|
|
346
|
+
if (fresh.state === 'running') adopted++;
|
|
347
|
+
else resolved++;
|
|
348
|
+
} catch (err) {
|
|
349
|
+
logger.warn({
|
|
350
|
+
msg: 'exec.job.recovery_probe_failed',
|
|
351
|
+
agent,
|
|
352
|
+
job_id: meta.job_id,
|
|
353
|
+
err: (err as Error).message,
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (adopted > 0 || resolved > 0) {
|
|
359
|
+
logger.info({
|
|
360
|
+
msg: 'exec.jobs_recovered_at_boot',
|
|
361
|
+
adopted_still_running: adopted,
|
|
362
|
+
resolved_terminal: resolved,
|
|
363
|
+
agents,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
return resolved;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* In-memory tracker for live PIDs of running jobs — needed for
|
|
371
|
+
* process_kill since we can't easily look up the spawned-process
|
|
372
|
+
* handle from disk after the fact. Populated by local.ts when a
|
|
373
|
+
* background job spawns; cleared on completion. Survives only the
|
|
374
|
+
* current server-process lifetime.
|
|
375
|
+
*/
|
|
376
|
+
const livePids = new Map<string, number>();
|
|
377
|
+
|
|
378
|
+
export function registerLivePid(jobId: string, pid: number): void {
|
|
379
|
+
livePids.set(jobId, pid);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export function getLivePid(jobId: string): number | undefined {
|
|
383
|
+
return livePids.get(jobId);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export function clearLivePid(jobId: string): void {
|
|
387
|
+
livePids.delete(jobId);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** Live writable handles for stdin (background process_write). */
|
|
391
|
+
const liveStdins = new Map<string, NodeJS.WritableStream>();
|
|
392
|
+
|
|
393
|
+
export function registerLiveStdin(jobId: string, stream: NodeJS.WritableStream): void {
|
|
394
|
+
liveStdins.set(jobId, stream);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export function getLiveStdin(jobId: string): NodeJS.WritableStream | undefined {
|
|
398
|
+
return liveStdins.get(jobId);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export function clearLiveStdin(jobId: string): void {
|
|
402
|
+
liveStdins.delete(jobId);
|
|
403
|
+
}
|