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,1064 @@
|
|
|
1
|
+
// `tmux` tool — Phase 5b. Single tool with action enum so the model
|
|
2
|
+
// can drive long-lived terminal sessions on local or on a configured
|
|
3
|
+
// SSH resource.
|
|
4
|
+
//
|
|
5
|
+
// Use-case: spawn an interactive TUI tool (claude --dangerously-skip-
|
|
6
|
+
// permissions, codex, vim, REPL) once, then drive it across many
|
|
7
|
+
// agent turns. Persistent state survives between tool calls because
|
|
8
|
+
// tmux's session keeps the underlying process alive.
|
|
9
|
+
//
|
|
10
|
+
// vs `exec`: exec is for one-shot commands (sync) or detached
|
|
11
|
+
// background jobs. tmux is when you NEED an actual terminal session
|
|
12
|
+
// you can come back to later. Two distinct use-cases, both valid.
|
|
13
|
+
|
|
14
|
+
import { z } from 'zod';
|
|
15
|
+
import { logger } from '../../server/logger.ts';
|
|
16
|
+
import type { ToolDefinition } from '../types.ts';
|
|
17
|
+
import {
|
|
18
|
+
tmuxLocalCapture,
|
|
19
|
+
tmuxLocalCreate,
|
|
20
|
+
tmuxLocalKill,
|
|
21
|
+
tmuxLocalList,
|
|
22
|
+
tmuxLocalSend,
|
|
23
|
+
tmuxLocalSendKey,
|
|
24
|
+
} from './local.ts';
|
|
25
|
+
import {
|
|
26
|
+
tmuxRemoteCapture,
|
|
27
|
+
tmuxRemoteCreate,
|
|
28
|
+
tmuxRemoteKill,
|
|
29
|
+
tmuxRemoteList,
|
|
30
|
+
tmuxRemoteSend,
|
|
31
|
+
tmuxRemoteSendKey,
|
|
32
|
+
} from './remote.ts';
|
|
33
|
+
import {
|
|
34
|
+
getTmuxOrigin,
|
|
35
|
+
recordTmuxOrigin,
|
|
36
|
+
removeTmuxOrigin,
|
|
37
|
+
type TmuxSessionKind,
|
|
38
|
+
} from '../../tmux/origin-store.ts';
|
|
39
|
+
import { detectTuiState, detectSuggestion, type TuiDetectResult } from '../../tmux/tui-markers.ts';
|
|
40
|
+
import {
|
|
41
|
+
readAttentionMirror,
|
|
42
|
+
recordTmuxObservation,
|
|
43
|
+
type AttentionDisplay,
|
|
44
|
+
} from '../../tmux/attention.ts';
|
|
45
|
+
|
|
46
|
+
// One or more tmux key tokens separated by whitespace. Each token is
|
|
47
|
+
// `[A-Za-z0-9_-]+` — covers tmux's full key-name vocabulary (Escape,
|
|
48
|
+
// Enter, BSpace, Tab, F1..F12, Up/Down/…, modifier-prefixed like C-c,
|
|
49
|
+
// M-Enter, S-Tab, C-M-Enter) and excludes shell metacharacters so the
|
|
50
|
+
// validated value is safe to interpolate into the shell command.
|
|
51
|
+
const KEY_TOKEN_RE = /^[A-Za-z0-9_-]+(\s+[A-Za-z0-9_-]+)*$/;
|
|
52
|
+
|
|
53
|
+
const POLL_INTERVAL_MS = 200;
|
|
54
|
+
const DEFAULT_WAIT_TIMEOUT_MS = 30_000;
|
|
55
|
+
const MAX_WAIT_TIMEOUT_MS = 600_000; // 10 min — same ceiling as longTaskMax
|
|
56
|
+
const DEFAULT_IDLE_STABLE_MS = 500;
|
|
57
|
+
|
|
58
|
+
const TmuxInput = z
|
|
59
|
+
.object({
|
|
60
|
+
action: z
|
|
61
|
+
.enum(['create', 'send', 'capture', 'wait_idle', 'list', 'kill'])
|
|
62
|
+
.describe(
|
|
63
|
+
'create (new session) | send (keys to existing session) | capture (read pane content) | ' +
|
|
64
|
+
'wait_idle (poll until pane content stops changing — pattern-free; for "wait until ' +
|
|
65
|
+
'TUI/command is done" without guessing a marker) | list (sessions on target) | ' +
|
|
66
|
+
'kill (remove session).',
|
|
67
|
+
),
|
|
68
|
+
target: z
|
|
69
|
+
.string()
|
|
70
|
+
.min(1)
|
|
71
|
+
.default('local')
|
|
72
|
+
.describe(
|
|
73
|
+
'"local" (default) = the somora server\'s machine. Otherwise a configured resource ' +
|
|
74
|
+
'name from resource_list. tmux must be installed on the target — Standard on most ' +
|
|
75
|
+
'macOS/Linux boxes; install via `brew install tmux` / `apt install tmux` if missing.',
|
|
76
|
+
),
|
|
77
|
+
name: z
|
|
78
|
+
.string()
|
|
79
|
+
.min(1)
|
|
80
|
+
.max(100)
|
|
81
|
+
.optional()
|
|
82
|
+
.describe(
|
|
83
|
+
'Session name. Required for create/send/capture/kill. Convention: meaningful slug ' +
|
|
84
|
+
'(e.g. "claude-bugfix-auth", "codex-experiment-1") so list output is readable. ' +
|
|
85
|
+
'Allowed: alphanumerics + dash + underscore; tmux rejects most other characters.',
|
|
86
|
+
),
|
|
87
|
+
cwd: z
|
|
88
|
+
.string()
|
|
89
|
+
.min(1)
|
|
90
|
+
.optional()
|
|
91
|
+
.describe(
|
|
92
|
+
'create only — initial working directory for the session\'s shell. Useful when the ' +
|
|
93
|
+
'TUI tool you spawn cares about cwd (claude/codex usually do).',
|
|
94
|
+
),
|
|
95
|
+
keys: z
|
|
96
|
+
.string()
|
|
97
|
+
.optional()
|
|
98
|
+
.describe(
|
|
99
|
+
'send only — text to type into the session. By default, newlines (\\n) are sent as ' +
|
|
100
|
+
'Enter keypresses (CR), which is what shells expect — append \\n to actually run a ' +
|
|
101
|
+
'shell command. For interactive TUIs (claude --dangerously-skip-permissions, codex, ' +
|
|
102
|
+
'IPython, jupyter input boxes, etc.) where Enter SUBMITS the message, set ' +
|
|
103
|
+
'`multiline_safe: true` so embedded \\n becomes a soft-newline instead of a submit. ' +
|
|
104
|
+
'For control / function / modifier keys (Escape, Ctrl-C, Ctrl-U, F1, arrow keys, …) ' +
|
|
105
|
+
'use the `key` field instead — JSON-encoding raw control bytes is brittle.',
|
|
106
|
+
),
|
|
107
|
+
key: z
|
|
108
|
+
.string()
|
|
109
|
+
.min(1)
|
|
110
|
+
.max(100)
|
|
111
|
+
.regex(
|
|
112
|
+
KEY_TOKEN_RE,
|
|
113
|
+
'key: only [A-Za-z0-9_-] tokens, optionally space-separated (e.g. "Escape", "C-c", "F1", "C-x C-c")',
|
|
114
|
+
)
|
|
115
|
+
.optional()
|
|
116
|
+
.describe(
|
|
117
|
+
'send only — one or more tmux key names, space-separated. Use this for keys that are ' +
|
|
118
|
+
'hard to type as text: Escape, C-c, C-u, C-d, BSpace, Tab, F1..F12, Up/Down/Left/Right, ' +
|
|
119
|
+
'Home/End/PgUp/PgDn, modifier-prefixed (C- = Ctrl, M- = Meta/Alt, S- = Shift; ' +
|
|
120
|
+
'combinable: C-M-Enter). tmux delivers them as real key events, not text bytes — ' +
|
|
121
|
+
'reliable across TUIs (codex/claude-code/etc) for "interrupt running task" (Escape), ' +
|
|
122
|
+
'"abort" (C-c), "clear input line" (C-u). Mutually exclusive with `keys` — pass one ' +
|
|
123
|
+
'or the other per call. Multi-key example: `key: "C-x C-c"` sends Ctrl-X then Ctrl-C.',
|
|
124
|
+
),
|
|
125
|
+
multiline_safe: z
|
|
126
|
+
.boolean()
|
|
127
|
+
.default(false)
|
|
128
|
+
.describe(
|
|
129
|
+
'send only — when true, embedded \\n in `keys` is sent as M-Enter (Esc+CR / Alt+Enter) ' +
|
|
130
|
+
'instead of a literal Enter. M-Enter is the soft-newline convention in modern input ' +
|
|
131
|
+
'TUIs (Claude Code, codex, IPython, fish, Slack-style input boxes etc.) — the message ' +
|
|
132
|
+
'gets a real linebreak without being submitted. The LAST character of `keys` is still ' +
|
|
133
|
+
'sent as Enter when it\'s a \\n, so a trailing newline still submits. Default false ' +
|
|
134
|
+
'(plain Enter for shell use). Use this when a single send carries a multi-line ' +
|
|
135
|
+
'message into a TUI that auto-submits on Enter.',
|
|
136
|
+
),
|
|
137
|
+
wait_pattern: z
|
|
138
|
+
.string()
|
|
139
|
+
.min(1)
|
|
140
|
+
.optional()
|
|
141
|
+
.describe(
|
|
142
|
+
'capture only — string to wait for in the pane content. Capture polls every 200ms ' +
|
|
143
|
+
'until matched OR wait_timeout_ms elapses. Without wait_pattern, capture returns the ' +
|
|
144
|
+
'current pane content immediately. The match semantic is controlled by `wait_mode` — ' +
|
|
145
|
+
'see that field; the right choice depends on whether you\'re driving a Shell or a ' +
|
|
146
|
+
'TUI session.',
|
|
147
|
+
),
|
|
148
|
+
wait_mode: z
|
|
149
|
+
.enum(['auto', 'present', 'idle'])
|
|
150
|
+
.default('auto')
|
|
151
|
+
.describe(
|
|
152
|
+
'capture only — how `wait_pattern` matches. `auto` (default): smart for SHELL sessions ' +
|
|
153
|
+
'— matches when pattern occurrences GROW past baseline (the typed command itself often ' +
|
|
154
|
+
'echoes the pattern, growth = real new output) OR when the buffer ends with a shell ' +
|
|
155
|
+
'prompt sigil ($/#/>) and the pattern is present (= command finished, output rendered). ' +
|
|
156
|
+
'`present`: matches as soon as pattern appears anywhere in pane content — for TUI ' +
|
|
157
|
+
'sessions (Claude Code, codex, vim, htop, fzf) where the pattern is part of a static ' +
|
|
158
|
+
'rendered panel and the buffer never gets a shell prompt. `idle`: matches when ' +
|
|
159
|
+
'pattern is in pane AND content has been stable (no changes) for `idle_stable_ms` — ' +
|
|
160
|
+
'good for "wait until the TUI stops typing/redrawing".',
|
|
161
|
+
),
|
|
162
|
+
idle_stable_ms: z
|
|
163
|
+
.number()
|
|
164
|
+
.int()
|
|
165
|
+
.min(100)
|
|
166
|
+
.max(10_000)
|
|
167
|
+
.default(DEFAULT_IDLE_STABLE_MS)
|
|
168
|
+
.describe(
|
|
169
|
+
`capture wait_mode=idle / wait_idle — how many ms of unchanged content count as ` +
|
|
170
|
+
`"stable". Default ${DEFAULT_IDLE_STABLE_MS}ms. Lower = match faster but might catch ` +
|
|
171
|
+
`a brief pause mid-render; higher = waits longer for true idle.`,
|
|
172
|
+
),
|
|
173
|
+
wait_timeout_ms: z
|
|
174
|
+
.number()
|
|
175
|
+
.int()
|
|
176
|
+
.min(100)
|
|
177
|
+
.max(MAX_WAIT_TIMEOUT_MS)
|
|
178
|
+
.default(DEFAULT_WAIT_TIMEOUT_MS)
|
|
179
|
+
.describe(
|
|
180
|
+
`capture (with wait_pattern) / wait_idle — max wall time to wait. Default ` +
|
|
181
|
+
`${DEFAULT_WAIT_TIMEOUT_MS}ms (${DEFAULT_WAIT_TIMEOUT_MS / 1000}s); max ` +
|
|
182
|
+
`${MAX_WAIT_TIMEOUT_MS}ms (${MAX_WAIT_TIMEOUT_MS / 60_000} min) for slow tools. ` +
|
|
183
|
+
`On timeout, capture returns matched_pattern=false / wait_idle returns ` +
|
|
184
|
+
`became_idle=false, both with the latest content as-is.`,
|
|
185
|
+
),
|
|
186
|
+
include_ansi: z
|
|
187
|
+
.boolean()
|
|
188
|
+
.default(false)
|
|
189
|
+
.describe(
|
|
190
|
+
'capture only — when true, `content` returns the raw pane bytes including ANSI escape ' +
|
|
191
|
+
'sequences (colors, cursor moves, dim/bold styling). Default false (escapes stripped — ' +
|
|
192
|
+
'plain text easier to match against). Use `include_ansi:true` when you need to ' +
|
|
193
|
+
'distinguish DIM/grayed-out text (e.g. auto-suggestions in Claude Code/codex prompt ' +
|
|
194
|
+
'lines) from real user-typed input. SAFETY: never auto-Enter on a buffer containing ' +
|
|
195
|
+
'pending input you didn\'t type yourself — it could be an auto-suggestion the TUI ' +
|
|
196
|
+
'rendered, and submitting it could trigger destructive actions ("delete project", ' +
|
|
197
|
+
'"clear all"). When in doubt, ask the user before submitting.',
|
|
198
|
+
),
|
|
199
|
+
lines: z
|
|
200
|
+
.number()
|
|
201
|
+
.int()
|
|
202
|
+
.min(1)
|
|
203
|
+
.max(10_000)
|
|
204
|
+
.default(200)
|
|
205
|
+
.describe(
|
|
206
|
+
'capture only — how many lines from the END of the pane scrollback to return. Default ' +
|
|
207
|
+
'200 — covers a typical multi-step interaction without flooding. Bump higher if you ' +
|
|
208
|
+
'need to see further back.',
|
|
209
|
+
),
|
|
210
|
+
inherit_agent_env: z
|
|
211
|
+
.boolean()
|
|
212
|
+
.default(false)
|
|
213
|
+
.describe(
|
|
214
|
+
'create only, local target only. When false (default), somora-internal env vars ' +
|
|
215
|
+
'(CLAUDE_CONFIG_DIR, SOMORA_CLAUDE_BIN) are stripped from the new session so a ' +
|
|
216
|
+
'`claude` / `codex` started inside the pane uses the user\'s normal login state. ' +
|
|
217
|
+
'This is what a user expects when they ask the agent to open a coding-TUI in tmux. ' +
|
|
218
|
+
'Set true only when you intentionally want somora\'s isolated claude tree inherited ' +
|
|
219
|
+
'— rare: e.g. when the agent itself wants to drive a nested claude-cli through tmux ' +
|
|
220
|
+
'against somora\'s shared auth/state tree.',
|
|
221
|
+
),
|
|
222
|
+
kind: z
|
|
223
|
+
.enum(['shell', 'claude-code', 'codex', 'opencode'])
|
|
224
|
+
.default('shell')
|
|
225
|
+
.describe(
|
|
226
|
+
'create only — what runs inside the pane. Drives TUI-aware capture/wait_idle: when ' +
|
|
227
|
+
'set, capture+wait_idle return a `tui_state` block ({state:"ready"|"queued"|"running", ' +
|
|
228
|
+
'markers:[]}) AND wait_idle treats queued/running marker hits as "not idle" so it ' +
|
|
229
|
+
'doesn\'t return prematurely on a content-stable-but-not-actually-ready pane. ' +
|
|
230
|
+
'When to pick which: "shell" (default) for bash/zsh/build-tools/REPLs/vim/htop — no ' +
|
|
231
|
+
'TUI magic, pure content-stability (today\'s behavior). "claude-code" for ' +
|
|
232
|
+
'`claude --dangerously-skip-permissions` — detects "Press up to edit queued messages" ' +
|
|
233
|
+
'and "esc to interrupt" + Claude Code\'s spinner words. "codex" for codex CLI — ' +
|
|
234
|
+
'detects esc-to-interrupt running cue. "opencode" for the OpenCode TUI — detects its ' +
|
|
235
|
+
'"esc interrupt" running cue and the QUEUED label on a message submitted while a turn ' +
|
|
236
|
+
'runs; a "Permission required" dialog reads as ready (it waits for you: Enter = Allow ' +
|
|
237
|
+
'once). Pick "shell" when unsure; the TUI flags are ' +
|
|
238
|
+
'additive and only help if the correct kind is declared. Sessions created with a ' +
|
|
239
|
+
'coding-CLI kind are watched by the attention watcher: if the CLI finishes while ' +
|
|
240
|
+
'you are no longer looking, somora wakes you with a [tmux attention] turn so you ' +
|
|
241
|
+
'can read the output and continue.',
|
|
242
|
+
),
|
|
243
|
+
attention: z
|
|
244
|
+
.boolean()
|
|
245
|
+
.default(true)
|
|
246
|
+
.describe(
|
|
247
|
+
'create only, coding-CLI kinds (claude-code/codex/opencode) only. false opts this session out of the ' +
|
|
248
|
+
'attention watcher (no needs_attention flag, no wake turns). Default true.',
|
|
249
|
+
),
|
|
250
|
+
})
|
|
251
|
+
.strict();
|
|
252
|
+
|
|
253
|
+
interface CreateResult {
|
|
254
|
+
action: 'create';
|
|
255
|
+
ok: boolean;
|
|
256
|
+
target: string;
|
|
257
|
+
name: string;
|
|
258
|
+
hint?: string;
|
|
259
|
+
error?: string;
|
|
260
|
+
}
|
|
261
|
+
interface SendResult {
|
|
262
|
+
action: 'send';
|
|
263
|
+
ok: boolean;
|
|
264
|
+
target: string;
|
|
265
|
+
name: string;
|
|
266
|
+
ms: number;
|
|
267
|
+
error?: string;
|
|
268
|
+
}
|
|
269
|
+
interface TuiStateField {
|
|
270
|
+
/** "ready" = TUI prompt available to accept input. "queued" = agent
|
|
271
|
+
* typed something but the TUI hasn't submitted it yet (don't proceed,
|
|
272
|
+
* the input is still in composer). "running" = TUI is actively
|
|
273
|
+
* processing — wait or interrupt with key:"Escape". "idle_unknown" =
|
|
274
|
+
* detection failed (e.g. unknown kind). */
|
|
275
|
+
state: 'ready' | 'queued' | 'running' | 'idle_unknown';
|
|
276
|
+
/** Substrings from the pane that matched the kind's marker table. */
|
|
277
|
+
markers: string[];
|
|
278
|
+
/** True when the TUI is rendering ghost-text (a dim auto-suggestion)
|
|
279
|
+
* on its input line. This is the TUI predicting a next step for a
|
|
280
|
+
* HUMAN — it is NOT real typed input. Ignore it: don't delete it,
|
|
281
|
+
* don't narrate it, don't submit it. */
|
|
282
|
+
suggestion_visible?: boolean;
|
|
283
|
+
/** The ghost text, when suggestion_visible. Informational only. */
|
|
284
|
+
suggestion_text?: string;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
interface CaptureResult {
|
|
288
|
+
action: 'capture';
|
|
289
|
+
ok: boolean;
|
|
290
|
+
target: string;
|
|
291
|
+
name: string;
|
|
292
|
+
content: string;
|
|
293
|
+
matched_pattern: boolean;
|
|
294
|
+
/** Only present when wait_pattern was provided. */
|
|
295
|
+
wait_pattern?: string;
|
|
296
|
+
/** Only present when the session was created with kind != "shell". */
|
|
297
|
+
tui_state?: TuiStateField;
|
|
298
|
+
/** Attention-watcher metadata; only present for watched coding-CLI
|
|
299
|
+
* sessions while the watcher is enabled. */
|
|
300
|
+
attention?: AttentionDisplay;
|
|
301
|
+
ms: number;
|
|
302
|
+
error?: string;
|
|
303
|
+
}
|
|
304
|
+
interface WaitIdleResult {
|
|
305
|
+
action: 'wait_idle';
|
|
306
|
+
ok: boolean;
|
|
307
|
+
target: string;
|
|
308
|
+
name: string;
|
|
309
|
+
/** Final pane content snapshot (after stability was reached or timeout fired). */
|
|
310
|
+
content: string;
|
|
311
|
+
/** True if the pane went stable for `idle_stable_ms` before max_wait_ms expired.
|
|
312
|
+
* For TUI sessions (kind != "shell"), this is true only when the pane is BOTH
|
|
313
|
+
* content-stable AND the marker-detected state is "ready" — a stable pane that
|
|
314
|
+
* still shows queued-input or running markers reports became_idle=false. */
|
|
315
|
+
became_idle: boolean;
|
|
316
|
+
/** Only present when the session was created with kind != "shell". */
|
|
317
|
+
tui_state?: TuiStateField;
|
|
318
|
+
/** Attention-watcher metadata; only present for watched coding-CLI
|
|
319
|
+
* sessions while the watcher is enabled. */
|
|
320
|
+
attention?: AttentionDisplay;
|
|
321
|
+
/** Total wall time the action waited, including the final stability window. */
|
|
322
|
+
ms: number;
|
|
323
|
+
error?: string;
|
|
324
|
+
}
|
|
325
|
+
interface ListResult {
|
|
326
|
+
action: 'list';
|
|
327
|
+
ok: boolean;
|
|
328
|
+
target: string;
|
|
329
|
+
count: number;
|
|
330
|
+
sessions: Array<{
|
|
331
|
+
name: string;
|
|
332
|
+
created_at: number;
|
|
333
|
+
windows: number;
|
|
334
|
+
attention?: AttentionDisplay;
|
|
335
|
+
}>;
|
|
336
|
+
}
|
|
337
|
+
interface KillResult {
|
|
338
|
+
action: 'kill';
|
|
339
|
+
ok: boolean;
|
|
340
|
+
target: string;
|
|
341
|
+
name: string;
|
|
342
|
+
was_running: boolean;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
type TmuxResult =
|
|
346
|
+
| CreateResult
|
|
347
|
+
| SendResult
|
|
348
|
+
| CaptureResult
|
|
349
|
+
| WaitIdleResult
|
|
350
|
+
| ListResult
|
|
351
|
+
| KillResult;
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Count how many times `needle` appears in `haystack`. Used by the
|
|
355
|
+
* wait_pattern logic to detect "more occurrences than at baseline"
|
|
356
|
+
* = new output appeared.
|
|
357
|
+
*/
|
|
358
|
+
function countOccurrences(haystack: string, needle: string): number {
|
|
359
|
+
if (!needle) return 0;
|
|
360
|
+
let count = 0;
|
|
361
|
+
let idx = 0;
|
|
362
|
+
while (true) {
|
|
363
|
+
const next = haystack.indexOf(needle, idx);
|
|
364
|
+
if (next === -1) break;
|
|
365
|
+
count++;
|
|
366
|
+
idx = next + needle.length;
|
|
367
|
+
}
|
|
368
|
+
return count;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Heuristic: does the captured pane content end with a shell that's
|
|
373
|
+
* idle and waiting for input? True when the last non-blank line ends
|
|
374
|
+
* in a typical prompt sigil (`$`, `#`, or `>`) followed only by
|
|
375
|
+
* whitespace. False when the last non-blank line contains the tail
|
|
376
|
+
* of running output. Used together with `pattern-in-content` to
|
|
377
|
+
* detect "command finished and produced its output already".
|
|
378
|
+
*
|
|
379
|
+
* Survives common prompts: `bash$ `, `user@host:~$`, `root#`,
|
|
380
|
+
* `>` (psql/sqlite), `❯` won't match (intentionally — fish/starship
|
|
381
|
+
* prompt with unicode chevron is ambiguous; users on those shells
|
|
382
|
+
* can fall back to count-based by sending a marker that's NOT in
|
|
383
|
+
* the typed command).
|
|
384
|
+
*/
|
|
385
|
+
function hasReadyPromptAtEnd(content: string): boolean {
|
|
386
|
+
const lines = content.split('\n');
|
|
387
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
388
|
+
const ln = lines[i];
|
|
389
|
+
if (!ln || !ln.trim()) continue;
|
|
390
|
+
return /[\$#>]\s*$/.test(ln);
|
|
391
|
+
}
|
|
392
|
+
return false;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function requireName(input: z.infer<typeof TmuxInput>, action: string): string {
|
|
396
|
+
if (!input.name) {
|
|
397
|
+
throw new Error(`tmux: action='${action}' requires the name field`);
|
|
398
|
+
}
|
|
399
|
+
return input.name;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
async function runCapture(
|
|
403
|
+
target: string,
|
|
404
|
+
agent: string,
|
|
405
|
+
name: string,
|
|
406
|
+
lines: number,
|
|
407
|
+
includeAnsi: boolean,
|
|
408
|
+
): Promise<{ ok: boolean; content: string; error?: string }> {
|
|
409
|
+
if (target === 'local') {
|
|
410
|
+
const r = await tmuxLocalCapture({ name, lines, includeAnsi });
|
|
411
|
+
return r.ok
|
|
412
|
+
? { ok: true, content: r.stdout }
|
|
413
|
+
: { ok: false, content: r.stdout, error: r.stderr.trim() || `exit ${r.exit_code}` };
|
|
414
|
+
}
|
|
415
|
+
const r = await tmuxRemoteCapture(agent, target, name, lines, includeAnsi);
|
|
416
|
+
return r.ok
|
|
417
|
+
? { ok: true, content: r.stdout }
|
|
418
|
+
: { ok: false, content: r.stdout, error: r.stderr.trim() || `exit ${r.exit_code}` };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** Bottom-of-pane window for the ghost-text probe — the input box
|
|
422
|
+
* lives there; scanning full scrollback would only add stale hits. */
|
|
423
|
+
const SUGGESTION_PROBE_LINES = 40;
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Enrich a detected tui_state with ghost-text (auto-suggestion) info.
|
|
427
|
+
* Reuses the caller's capture when it already includes ANSI; otherwise
|
|
428
|
+
* runs one extra small `capture-pane -e` probe. Best-effort — a failed
|
|
429
|
+
* probe returns the plain state unchanged.
|
|
430
|
+
*/
|
|
431
|
+
async function enrichTuiState(
|
|
432
|
+
tui: TuiDetectResult,
|
|
433
|
+
target: string,
|
|
434
|
+
agent: string,
|
|
435
|
+
name: string,
|
|
436
|
+
kind: TmuxSessionKind | undefined,
|
|
437
|
+
ansiContent: string | null,
|
|
438
|
+
): Promise<TuiStateField> {
|
|
439
|
+
try {
|
|
440
|
+
const ansi =
|
|
441
|
+
ansiContent ??
|
|
442
|
+
(await runCapture(target, agent, name, SUGGESTION_PROBE_LINES, true)).content;
|
|
443
|
+
const s = detectSuggestion(ansi, kind);
|
|
444
|
+
if (!s) return tui;
|
|
445
|
+
return {
|
|
446
|
+
...tui,
|
|
447
|
+
suggestion_visible: s.suggestion_visible,
|
|
448
|
+
...(s.suggestion_text !== undefined ? { suggestion_text: s.suggestion_text } : {}),
|
|
449
|
+
};
|
|
450
|
+
} catch {
|
|
451
|
+
return tui;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Observation ledger write for the attention watcher: when the ORIGIN
|
|
457
|
+
* (agent, session) looks at (capture / wait_idle) or drives (send) a
|
|
458
|
+
* watched coding-CLI session, note the timestamp so a completion the
|
|
459
|
+
* agent saw itself never triggers a wake (anti-double-trigger contract,
|
|
460
|
+
* design doc §3.2). Non-origin agents don't count — someone else
|
|
461
|
+
* peeking doesn't mean the origin agent is informed. Best-effort.
|
|
462
|
+
*/
|
|
463
|
+
function noteObservation(
|
|
464
|
+
target: string,
|
|
465
|
+
name: string,
|
|
466
|
+
ctx: { agent: string; session?: string },
|
|
467
|
+
sawState?: TuiDetectResult['state'],
|
|
468
|
+
): void {
|
|
469
|
+
if (target !== 'local') return;
|
|
470
|
+
try {
|
|
471
|
+
const origin = getTmuxOrigin(name);
|
|
472
|
+
if (!origin || !origin.kind || origin.kind === 'shell') return;
|
|
473
|
+
if (origin.agent !== ctx.agent) return;
|
|
474
|
+
if (origin.session && ctx.session && origin.session !== ctx.session) return;
|
|
475
|
+
recordTmuxObservation(name, ctx.agent, ctx.session, sawState);
|
|
476
|
+
} catch {
|
|
477
|
+
/* ledger must never break the tool */
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/** Attention metadata for one local session, read from the watcher's
|
|
482
|
+
* mirror file. Empty when the watcher is off (boot clears the mirror)
|
|
483
|
+
* or the session isn't watched — zero traces when the feature is
|
|
484
|
+
* disabled (three-gates rule). */
|
|
485
|
+
function attentionFor(target: string, name: string): { attention?: AttentionDisplay } {
|
|
486
|
+
if (target !== 'local') return {};
|
|
487
|
+
try {
|
|
488
|
+
const a = readAttentionMirror()[name];
|
|
489
|
+
return a ? { attention: a } : {};
|
|
490
|
+
} catch {
|
|
491
|
+
return {};
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Look up the session's declared kind from origin-store. Only local
|
|
497
|
+
* sessions carry origin records (remote tmux runs on a different host
|
|
498
|
+
* and isn't tracked here yet) — remote always returns undefined,
|
|
499
|
+
* which downstream means "no TUI detection, behave like today".
|
|
500
|
+
*/
|
|
501
|
+
function lookupSessionKind(target: string, name: string): TmuxSessionKind | undefined {
|
|
502
|
+
if (target !== 'local') return undefined;
|
|
503
|
+
try {
|
|
504
|
+
const origin = getTmuxOrigin(name);
|
|
505
|
+
return origin?.kind;
|
|
506
|
+
} catch {
|
|
507
|
+
return undefined;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
export const tmux: ToolDefinition<z.infer<typeof TmuxInput>, TmuxResult> = {
|
|
512
|
+
name: 'tmux',
|
|
513
|
+
toolset: 'exec',
|
|
514
|
+
description:
|
|
515
|
+
'Drive a long-lived terminal (tmux) session on local (the somora server) or on a configured ' +
|
|
516
|
+
'SSH resource. Use when you need to spawn an interactive tool (claude in skip-permissions ' +
|
|
517
|
+
'mode, codex, vim, a REPL) and drive it across MULTIPLE agent turns — the session and its ' +
|
|
518
|
+
'process state survive between your tool calls. ' +
|
|
519
|
+
'\n\n' +
|
|
520
|
+
'Typical flow: action:"create" once → many action:"send"/"capture" pairs over many turns → ' +
|
|
521
|
+
'action:"kill" when done. action:"list" shows all sessions on the target. ' +
|
|
522
|
+
'\n\n' +
|
|
523
|
+
'Use exec INSTEAD when: you want to run a one-shot command (sync) or a detached background ' +
|
|
524
|
+
'job (background:true) that runs to completion without further interaction. tmux is ' +
|
|
525
|
+
'overkill for those — and forgotten tmux sessions accumulate. ' +
|
|
526
|
+
'\n\n' +
|
|
527
|
+
'capture supports wait_pattern with three modes (wait_mode field): "auto" (default, smart ' +
|
|
528
|
+
'for shell sessions), "present" (matches as soon as pattern appears anywhere — for TUIs), ' +
|
|
529
|
+
'"idle" (matches when pattern present AND content stable for idle_stable_ms). Without ' +
|
|
530
|
+
'wait_pattern, capture returns the current pane content immediately. ' +
|
|
531
|
+
'\n\n' +
|
|
532
|
+
'wait_idle: pattern-free polling — returns once the pane content stops changing for ' +
|
|
533
|
+
'idle_stable_ms. Use when you want to wait until "the TUI/command is done" without ' +
|
|
534
|
+
'guessing a marker string. Cheaper than capture+wait_pattern when you don\'t know what ' +
|
|
535
|
+
'the final output will look like (long agent runs, code-formatter passes, etc.). ' +
|
|
536
|
+
'\n\n' +
|
|
537
|
+
'Two opt-in flags help with TUIs that auto-submit on Enter or that draw auto-suggestions: ' +
|
|
538
|
+
'send `multiline_safe:true` keeps embedded \\n as soft-newlines (M-Enter) instead of ' +
|
|
539
|
+
'submits; capture `include_ansi:true` returns raw bytes with ANSI escapes so dim/grayed-out ' +
|
|
540
|
+
'text (= auto-suggestions) is distinguishable from real input. ' +
|
|
541
|
+
'\n\n' +
|
|
542
|
+
'When you create a session that will host a known coding-TUI, declare it at create-time ' +
|
|
543
|
+
'with `kind: "claude-code"` or `kind: "codex"` — capture and wait_idle then return a ' +
|
|
544
|
+
'structured `tui_state:{state, markers, suggestion_visible, suggestion_text}` block and ' +
|
|
545
|
+
'wait_idle correctly distinguishes ' +
|
|
546
|
+
'"content stable AND TUI ready" from "content stable but queued input not submitted / ' +
|
|
547
|
+
'still running". Default `kind: "shell"` keeps today\'s pure content-stability behavior ' +
|
|
548
|
+
'for plain shell jobs, builds, REPLs, vim/htop — anything that isn\'t one of the named TUIs. ' +
|
|
549
|
+
'\n\n' +
|
|
550
|
+
'send has two input forms (mutually exclusive): `keys` for text/printable input (with ' +
|
|
551
|
+
'`multiline_safe` for TUI-aware newline handling), or `key` for control/function/modifier ' +
|
|
552
|
+
'keys by symbolic name (Escape, C-c, C-u, F1, Up, …). Use `key` to interrupt a running TUI ' +
|
|
553
|
+
'task ("Escape" / "C-c") or to clear the input line ("C-u") — JSON-encoding raw control ' +
|
|
554
|
+
'bytes in `keys` is unreliable. ' +
|
|
555
|
+
'\n\n' +
|
|
556
|
+
'AUTO-SUGGESTIONS (ghost text): Claude Code and codex render a dim predicted-next-input ' +
|
|
557
|
+
'line in their input field — a hint for a human sitting in front of the app, NOT real ' +
|
|
558
|
+
'typed input. In stripped captures it looks identical to typed text. For kind-declared ' +
|
|
559
|
+
'sessions, `tui_state.suggestion_visible/suggestion_text` tell you explicitly what is ' +
|
|
560
|
+
'ghost text: IGNORE it — do not clear it, do not mention it, do not treat it as a stray ' +
|
|
561
|
+
'input line. It vanishes on its own when you type. ' +
|
|
562
|
+
'\n\n' +
|
|
563
|
+
'SAFETY for TUI sessions: never blindly press Enter on a buffer that already shows pending ' +
|
|
564
|
+
'input you didn\'t type. If tui_state reports suggestion_visible for that text it\'s just a ' +
|
|
565
|
+
'suggestion (Enter may submit it in some TUIs — type your own input instead); if it\'s real ' +
|
|
566
|
+
'pending input you didn\'t author, submitting it can trigger destructive actions ("delete ' +
|
|
567
|
+
'project", "clear all"). When in doubt, capture with include_ansi:true to see styling, or ' +
|
|
568
|
+
'ask the user.',
|
|
569
|
+
inputSchema: TmuxInput,
|
|
570
|
+
jsonSchema: {
|
|
571
|
+
type: 'object',
|
|
572
|
+
properties: {
|
|
573
|
+
action: {
|
|
574
|
+
type: 'string',
|
|
575
|
+
enum: ['create', 'send', 'capture', 'wait_idle', 'list', 'kill'],
|
|
576
|
+
},
|
|
577
|
+
target: { type: 'string', description: '"local" or resource name.', default: 'local' },
|
|
578
|
+
name: { type: 'string', description: 'Session name. Required for create/send/capture/kill.' },
|
|
579
|
+
cwd: { type: 'string' },
|
|
580
|
+
keys: { type: 'string' },
|
|
581
|
+
key: {
|
|
582
|
+
type: 'string',
|
|
583
|
+
description:
|
|
584
|
+
'send only — symbolic tmux key name(s) for control/function keys. Mutually ' +
|
|
585
|
+
'exclusive with `keys`. Examples: "Escape", "C-c", "C-u", "F1", "Up", "C-x C-c".',
|
|
586
|
+
},
|
|
587
|
+
multiline_safe: { type: 'boolean', default: false },
|
|
588
|
+
wait_pattern: { type: 'string' },
|
|
589
|
+
wait_mode: { type: 'string', enum: ['auto', 'present', 'idle'], default: 'auto' },
|
|
590
|
+
idle_stable_ms: {
|
|
591
|
+
type: 'integer',
|
|
592
|
+
minimum: 100,
|
|
593
|
+
maximum: 10_000,
|
|
594
|
+
default: DEFAULT_IDLE_STABLE_MS,
|
|
595
|
+
},
|
|
596
|
+
wait_timeout_ms: {
|
|
597
|
+
type: 'integer',
|
|
598
|
+
minimum: 100,
|
|
599
|
+
maximum: MAX_WAIT_TIMEOUT_MS,
|
|
600
|
+
default: DEFAULT_WAIT_TIMEOUT_MS,
|
|
601
|
+
},
|
|
602
|
+
include_ansi: { type: 'boolean', default: false },
|
|
603
|
+
lines: { type: 'integer', minimum: 1, maximum: 10_000, default: 200 },
|
|
604
|
+
inherit_agent_env: { type: 'boolean', default: false },
|
|
605
|
+
kind: {
|
|
606
|
+
type: 'string',
|
|
607
|
+
enum: ['shell', 'claude-code', 'codex', 'opencode'],
|
|
608
|
+
default: 'shell',
|
|
609
|
+
description:
|
|
610
|
+
'create only — what runs inside the pane, drives TUI-aware capture/wait_idle. ' +
|
|
611
|
+
'See the inputSchema for full per-kind semantics.',
|
|
612
|
+
},
|
|
613
|
+
attention: {
|
|
614
|
+
type: 'boolean',
|
|
615
|
+
default: true,
|
|
616
|
+
description:
|
|
617
|
+
'create only, coding-CLI kinds (claude-code/codex/opencode) only. false opts this session out of the ' +
|
|
618
|
+
'attention watcher (no needs_attention flag, no wake turns).',
|
|
619
|
+
},
|
|
620
|
+
},
|
|
621
|
+
required: ['action'],
|
|
622
|
+
additionalProperties: false,
|
|
623
|
+
},
|
|
624
|
+
defaultTimeoutMs: 30_000,
|
|
625
|
+
// capture with wait_pattern can legitimately block — bump the
|
|
626
|
+
// engine-level race to wait_timeout_ms + 2s buffer. Other actions
|
|
627
|
+
// are quick (single ssh exec or local spawn).
|
|
628
|
+
timeoutFromInput: (input) => {
|
|
629
|
+
if (input.action === 'capture' && input.wait_pattern && input.wait_timeout_ms) {
|
|
630
|
+
return input.wait_timeout_ms + 2_000;
|
|
631
|
+
}
|
|
632
|
+
if (input.action === 'wait_idle' && input.wait_timeout_ms) {
|
|
633
|
+
return input.wait_timeout_ms + 2_000;
|
|
634
|
+
}
|
|
635
|
+
return undefined;
|
|
636
|
+
},
|
|
637
|
+
maxTimeoutMs: MAX_WAIT_TIMEOUT_MS + 2_000,
|
|
638
|
+
async handler(input, ctx): Promise<TmuxResult> {
|
|
639
|
+
const target = input.target ?? 'local';
|
|
640
|
+
|
|
641
|
+
if (input.action === 'create') {
|
|
642
|
+
const name = requireName(input, 'create');
|
|
643
|
+
const r =
|
|
644
|
+
target === 'local'
|
|
645
|
+
? await tmuxLocalCreate({
|
|
646
|
+
name,
|
|
647
|
+
...(input.cwd ? { cwd: input.cwd } : {}),
|
|
648
|
+
inheritAgentEnv: input.inherit_agent_env === true,
|
|
649
|
+
})
|
|
650
|
+
: await tmuxRemoteCreate(ctx.agent, target, name, input.cwd);
|
|
651
|
+
if (!r.ok) {
|
|
652
|
+
return {
|
|
653
|
+
action: 'create',
|
|
654
|
+
ok: false,
|
|
655
|
+
target,
|
|
656
|
+
name,
|
|
657
|
+
error: r.stderr.trim() || `exit ${r.exit_code}`,
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
// Track origin for the web tmux-list view (local sessions only;
|
|
661
|
+
// remote sessions live on other hosts and aren't browse-able from
|
|
662
|
+
// here yet). Failure to record never blocks the tool.
|
|
663
|
+
const kind: TmuxSessionKind = input.kind ?? 'shell';
|
|
664
|
+
if (target === 'local') {
|
|
665
|
+
try {
|
|
666
|
+
recordTmuxOrigin({
|
|
667
|
+
name,
|
|
668
|
+
agent: ctx.agent,
|
|
669
|
+
...(ctx.session ? { session: ctx.session } : {}),
|
|
670
|
+
...(kind !== 'shell' ? { kind } : {}),
|
|
671
|
+
...(input.attention === false ? { attention: false } : {}),
|
|
672
|
+
});
|
|
673
|
+
} catch (err) {
|
|
674
|
+
logger.warn({
|
|
675
|
+
msg: 'tmux.origin.record_failed',
|
|
676
|
+
name,
|
|
677
|
+
err: (err as Error).message,
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
// Tailored hint depending on declared kind — points the agent at
|
|
682
|
+
// tui_state for the kinds that emit it.
|
|
683
|
+
const kindHint =
|
|
684
|
+
kind === 'shell'
|
|
685
|
+
? ''
|
|
686
|
+
: ` This session is kind:"${kind}" — capture and wait_idle return a ` +
|
|
687
|
+
`tui_state:{state, markers, suggestion_visible, suggestion_text} block. Before ` +
|
|
688
|
+
`assuming an input was processed, check tui_state.state — "queued" means it's ` +
|
|
689
|
+
`not submitted yet, "running" means the TUI is still working. If ` +
|
|
690
|
+
`suggestion_visible is true, the input line shows the TUI's dim ghost-text ` +
|
|
691
|
+
`prediction for a human — ignore it, it is not real input.`;
|
|
692
|
+
return {
|
|
693
|
+
action: 'create',
|
|
694
|
+
ok: true,
|
|
695
|
+
target,
|
|
696
|
+
name,
|
|
697
|
+
hint:
|
|
698
|
+
`Session ready. Use tmux({action:"send", target:"${target}", name:"${name}", ` +
|
|
699
|
+
`keys:"<text>\\n"}) to type into it, or tmux({..., key:"Escape"}) for control ` +
|
|
700
|
+
`keys (Escape, C-c, C-u, F1, …). tmux({action:"capture", ...}) to read, ` +
|
|
701
|
+
`tmux({action:"kill", ...}) when done.${kindHint}`,
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
if (input.action === 'send') {
|
|
706
|
+
const name = requireName(input, 'send');
|
|
707
|
+
const hasKeys = input.keys !== undefined;
|
|
708
|
+
const hasKey = input.key !== undefined;
|
|
709
|
+
if (hasKeys && hasKey) {
|
|
710
|
+
throw new Error(
|
|
711
|
+
'tmux send: pass either `keys` (text) or `key` (control/function key name), not both',
|
|
712
|
+
);
|
|
713
|
+
}
|
|
714
|
+
if (!hasKeys && !hasKey) {
|
|
715
|
+
throw new Error('tmux send: requires `keys` (text) or `key` (control/function key name)');
|
|
716
|
+
}
|
|
717
|
+
const start = Date.now();
|
|
718
|
+
const r = hasKey
|
|
719
|
+
? target === 'local'
|
|
720
|
+
? await tmuxLocalSendKey(name, input.key as string)
|
|
721
|
+
: await tmuxRemoteSendKey(ctx.agent, target, name, input.key as string)
|
|
722
|
+
: target === 'local'
|
|
723
|
+
? await tmuxLocalSend(name, input.keys as string, input.multiline_safe ?? false)
|
|
724
|
+
: await tmuxRemoteSend(
|
|
725
|
+
ctx.agent,
|
|
726
|
+
target,
|
|
727
|
+
name,
|
|
728
|
+
input.keys as string,
|
|
729
|
+
input.multiline_safe ?? false,
|
|
730
|
+
);
|
|
731
|
+
if (r.ok) noteObservation(target, name, ctx);
|
|
732
|
+
return {
|
|
733
|
+
action: 'send',
|
|
734
|
+
ok: r.ok,
|
|
735
|
+
target,
|
|
736
|
+
name,
|
|
737
|
+
ms: Date.now() - start,
|
|
738
|
+
...(r.ok ? {} : { error: r.stderr.trim() || `exit ${r.exit_code}` }),
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
if (input.action === 'capture') {
|
|
743
|
+
const name = requireName(input, 'capture');
|
|
744
|
+
const lines = input.lines ?? 200;
|
|
745
|
+
const includeAnsi = input.include_ansi ?? false;
|
|
746
|
+
const start = Date.now();
|
|
747
|
+
|
|
748
|
+
// No wait_pattern: one-shot capture, return whatever's in the
|
|
749
|
+
// pane right now.
|
|
750
|
+
if (!input.wait_pattern) {
|
|
751
|
+
const cap = await runCapture(target, ctx.agent, name, lines, includeAnsi);
|
|
752
|
+
const kind = lookupSessionKind(target, name);
|
|
753
|
+
const tui = detectTuiState(cap.content, kind);
|
|
754
|
+
const tuiOut = tui
|
|
755
|
+
? await enrichTuiState(tui, target, ctx.agent, name, kind, includeAnsi ? cap.content : null)
|
|
756
|
+
: null;
|
|
757
|
+
if (cap.ok) noteObservation(target, name, ctx, tui?.state);
|
|
758
|
+
return {
|
|
759
|
+
action: 'capture',
|
|
760
|
+
ok: cap.ok,
|
|
761
|
+
target,
|
|
762
|
+
name,
|
|
763
|
+
content: cap.content,
|
|
764
|
+
matched_pattern: false,
|
|
765
|
+
ms: Date.now() - start,
|
|
766
|
+
...(tuiOut ? { tui_state: tuiOut } : {}),
|
|
767
|
+
...attentionFor(target, name),
|
|
768
|
+
...(cap.error ? { error: cap.error } : {}),
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
const pattern = input.wait_pattern;
|
|
773
|
+
const mode = input.wait_mode ?? 'auto';
|
|
774
|
+
const timeout = input.wait_timeout_ms ?? DEFAULT_WAIT_TIMEOUT_MS;
|
|
775
|
+
const idleStableMs = input.idle_stable_ms ?? DEFAULT_IDLE_STABLE_MS;
|
|
776
|
+
const deadline = start + timeout;
|
|
777
|
+
|
|
778
|
+
// Tiny pre-baseline sleep to dodge a race: if the model called
|
|
779
|
+
// `tmux send keys="<cmd>\n"` immediately before this capture,
|
|
780
|
+
// tmux's display buffer might not have rendered the typed
|
|
781
|
+
// command yet. Without this delay our baseline could miss the
|
|
782
|
+
// typed text → first poll sees the command appear → false-
|
|
783
|
+
// positive match. 100ms is invisible to the model and reliable.
|
|
784
|
+
await new Promise((res) => setTimeout(res, 100));
|
|
785
|
+
const baselineCap = await runCapture(target, ctx.agent, name, lines, includeAnsi);
|
|
786
|
+
const baseline = baselineCap.ok ? baselineCap.content : '';
|
|
787
|
+
const baselineCount = countOccurrences(baseline, pattern);
|
|
788
|
+
let lastContent = baseline;
|
|
789
|
+
let lastError = baselineCap.ok ? undefined : baselineCap.error;
|
|
790
|
+
let matched = false;
|
|
791
|
+
|
|
792
|
+
// Mode-specific match check. Evaluated at baseline AND on each
|
|
793
|
+
// poll iteration so the same code-path catches early-match.
|
|
794
|
+
function matchCheck(content: string, count: number): boolean {
|
|
795
|
+
if (mode === 'present') {
|
|
796
|
+
// Pattern visible anywhere → match. For TUI sessions where
|
|
797
|
+
// the pattern is part of a static rendered panel.
|
|
798
|
+
return count > 0;
|
|
799
|
+
}
|
|
800
|
+
if (mode === 'idle') {
|
|
801
|
+
// Idle-match handled separately below (needs stability
|
|
802
|
+
// tracking), not a single-snapshot decision.
|
|
803
|
+
return false;
|
|
804
|
+
}
|
|
805
|
+
// mode === 'auto' — the smart shell-friendly default:
|
|
806
|
+
// * count grew past baseline (new output appeared), OR
|
|
807
|
+
// * pattern present AND buffer ends with shell prompt sigil
|
|
808
|
+
// ($/#/>) — command finished, output rendered. Different
|
|
809
|
+
// shells render typed input differently (some echo it
|
|
810
|
+
// once, some draw a draft line above); prompt-detect
|
|
811
|
+
// works across both because we look at tail-of-buffer.
|
|
812
|
+
if (count > baselineCount) return true;
|
|
813
|
+
if (count > 0 && hasReadyPromptAtEnd(content)) return true;
|
|
814
|
+
return false;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
// Idle-mode tracking: when did the content last change, when
|
|
818
|
+
// did we last see the pattern present? Match fires when both
|
|
819
|
+
// have been true for >= idleStableMs continuously.
|
|
820
|
+
let lastChangeTs = Date.now();
|
|
821
|
+
let lastSeenContent = baseline;
|
|
822
|
+
|
|
823
|
+
if (matchCheck(baseline, baselineCount)) {
|
|
824
|
+
matched = true;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
while (!matched && !lastError && Date.now() < deadline) {
|
|
828
|
+
const remaining = deadline - Date.now();
|
|
829
|
+
if (remaining <= 0) break;
|
|
830
|
+
// For idle-mode polling we sleep at most until the next
|
|
831
|
+
// stability check tick, which can be shorter than POLL_INTERVAL_MS.
|
|
832
|
+
const sleepFor =
|
|
833
|
+
mode === 'idle'
|
|
834
|
+
? Math.min(POLL_INTERVAL_MS, remaining, idleStableMs)
|
|
835
|
+
: Math.min(POLL_INTERVAL_MS, remaining);
|
|
836
|
+
await new Promise((res) => setTimeout(res, sleepFor));
|
|
837
|
+
const cap = await runCapture(target, ctx.agent, name, lines, includeAnsi);
|
|
838
|
+
if (!cap.ok) {
|
|
839
|
+
lastError = cap.error;
|
|
840
|
+
break;
|
|
841
|
+
}
|
|
842
|
+
lastContent = cap.content;
|
|
843
|
+
const currentCount = countOccurrences(lastContent, pattern);
|
|
844
|
+
|
|
845
|
+
if (mode === 'idle') {
|
|
846
|
+
// Idle-mode: match once pattern is present AND content has
|
|
847
|
+
// been unchanged for >= idleStableMs. Reset the change
|
|
848
|
+
// timer whenever content actually moves.
|
|
849
|
+
if (lastContent !== lastSeenContent) {
|
|
850
|
+
lastChangeTs = Date.now();
|
|
851
|
+
lastSeenContent = lastContent;
|
|
852
|
+
}
|
|
853
|
+
if (currentCount > 0 && Date.now() - lastChangeTs >= idleStableMs) {
|
|
854
|
+
matched = true;
|
|
855
|
+
break;
|
|
856
|
+
}
|
|
857
|
+
} else if (matchCheck(lastContent, currentCount)) {
|
|
858
|
+
matched = true;
|
|
859
|
+
break;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
if (!matched && !lastError) {
|
|
864
|
+
logger.info({
|
|
865
|
+
msg: 'tmux.capture.pattern_timeout',
|
|
866
|
+
target,
|
|
867
|
+
name,
|
|
868
|
+
mode,
|
|
869
|
+
pattern: pattern.slice(0, 80),
|
|
870
|
+
waited_ms: Date.now() - start,
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
const captureKind = lookupSessionKind(target, name);
|
|
875
|
+
const captureTui = detectTuiState(lastContent, captureKind);
|
|
876
|
+
const captureTuiOut = captureTui
|
|
877
|
+
? await enrichTuiState(captureTui, target, ctx.agent, name, captureKind, includeAnsi ? lastContent : null)
|
|
878
|
+
: null;
|
|
879
|
+
if (!lastError) noteObservation(target, name, ctx, captureTui?.state);
|
|
880
|
+
return {
|
|
881
|
+
action: 'capture',
|
|
882
|
+
ok: !lastError,
|
|
883
|
+
target,
|
|
884
|
+
name,
|
|
885
|
+
content: lastContent,
|
|
886
|
+
matched_pattern: matched,
|
|
887
|
+
wait_pattern: pattern,
|
|
888
|
+
ms: Date.now() - start,
|
|
889
|
+
...(captureTuiOut ? { tui_state: captureTuiOut } : {}),
|
|
890
|
+
...attentionFor(target, name),
|
|
891
|
+
...(lastError ? { error: lastError } : {}),
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
if (input.action === 'wait_idle') {
|
|
896
|
+
const name = requireName(input, 'wait_idle');
|
|
897
|
+
const lines = input.lines ?? 200;
|
|
898
|
+
const includeAnsi = input.include_ansi ?? false;
|
|
899
|
+
const idleStableMs = input.idle_stable_ms ?? DEFAULT_IDLE_STABLE_MS;
|
|
900
|
+
const timeout = input.wait_timeout_ms ?? DEFAULT_WAIT_TIMEOUT_MS;
|
|
901
|
+
const start = Date.now();
|
|
902
|
+
const deadline = start + timeout;
|
|
903
|
+
// Look up the session's kind once — drives whether a stable but
|
|
904
|
+
// queued/running pane should count as idle. Undefined / "shell"
|
|
905
|
+
// = no detection, falls back to today's pure-stability behavior.
|
|
906
|
+
const sessionKind = lookupSessionKind(target, name);
|
|
907
|
+
|
|
908
|
+
// Same pre-baseline race-dodge as capture's wait_pattern path:
|
|
909
|
+
// if the model called `send` immediately before this wait, give
|
|
910
|
+
// tmux a tick to render the typed text so the baseline already
|
|
911
|
+
// contains it.
|
|
912
|
+
await new Promise((res) => setTimeout(res, 100));
|
|
913
|
+
const baselineCap = await runCapture(target, ctx.agent, name, lines, includeAnsi);
|
|
914
|
+
if (!baselineCap.ok) {
|
|
915
|
+
return {
|
|
916
|
+
action: 'wait_idle',
|
|
917
|
+
ok: false,
|
|
918
|
+
target,
|
|
919
|
+
name,
|
|
920
|
+
content: baselineCap.content,
|
|
921
|
+
became_idle: false,
|
|
922
|
+
ms: Date.now() - start,
|
|
923
|
+
error: baselineCap.error,
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
let lastContent = baselineCap.content;
|
|
928
|
+
let lastChangeTs = Date.now();
|
|
929
|
+
let becameIdle = false;
|
|
930
|
+
let lastError: string | undefined;
|
|
931
|
+
|
|
932
|
+
while (Date.now() < deadline) {
|
|
933
|
+
const remaining = deadline - Date.now();
|
|
934
|
+
if (remaining <= 0) break;
|
|
935
|
+
// Sleep at most until the next stability check tick. Capping at
|
|
936
|
+
// idleStableMs means a freshly-stable pane can be detected on
|
|
937
|
+
// the very next poll instead of waiting a full POLL_INTERVAL.
|
|
938
|
+
const sleepFor = Math.min(POLL_INTERVAL_MS, remaining, idleStableMs);
|
|
939
|
+
await new Promise((res) => setTimeout(res, sleepFor));
|
|
940
|
+
const cap = await runCapture(target, ctx.agent, name, lines, includeAnsi);
|
|
941
|
+
if (!cap.ok) {
|
|
942
|
+
lastError = cap.error;
|
|
943
|
+
break;
|
|
944
|
+
}
|
|
945
|
+
if (cap.content !== lastContent) {
|
|
946
|
+
lastContent = cap.content;
|
|
947
|
+
lastChangeTs = Date.now();
|
|
948
|
+
continue;
|
|
949
|
+
}
|
|
950
|
+
if (Date.now() - lastChangeTs >= idleStableMs) {
|
|
951
|
+
// Content has been stable. For shell sessions that's enough;
|
|
952
|
+
// for TUI sessions, also require the TUI state to be "ready"
|
|
953
|
+
// — a pane sitting on "Press up to edit queued messages" or
|
|
954
|
+
// "esc to interrupt" is content-stable but the TUI is NOT
|
|
955
|
+
// actually done. Treating it as idle is the exact failure
|
|
956
|
+
// hans reported 2026-05-17 (queued-input feedback).
|
|
957
|
+
const tui = detectTuiState(lastContent, sessionKind);
|
|
958
|
+
if (!tui || tui.state === 'ready') {
|
|
959
|
+
becameIdle = true;
|
|
960
|
+
break;
|
|
961
|
+
}
|
|
962
|
+
// TUI state is queued or running. Reset the stability timer
|
|
963
|
+
// so the next "stable" decision is computed from now — that
|
|
964
|
+
// way a still-running TUI eventually times out via deadline
|
|
965
|
+
// (rather than spinning the inner branch every poll).
|
|
966
|
+
lastChangeTs = Date.now();
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
if (!becameIdle && !lastError) {
|
|
971
|
+
logger.info({
|
|
972
|
+
msg: 'tmux.wait_idle.timeout',
|
|
973
|
+
target,
|
|
974
|
+
name,
|
|
975
|
+
waited_ms: Date.now() - start,
|
|
976
|
+
idle_stable_ms: idleStableMs,
|
|
977
|
+
kind: sessionKind,
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
const tuiFinal = detectTuiState(lastContent, sessionKind);
|
|
982
|
+
const tuiFinalOut = tuiFinal
|
|
983
|
+
? await enrichTuiState(tuiFinal, target, ctx.agent, name, sessionKind, includeAnsi ? lastContent : null)
|
|
984
|
+
: null;
|
|
985
|
+
if (!lastError) noteObservation(target, name, ctx, tuiFinal?.state);
|
|
986
|
+
return {
|
|
987
|
+
action: 'wait_idle',
|
|
988
|
+
ok: !lastError,
|
|
989
|
+
target,
|
|
990
|
+
name,
|
|
991
|
+
content: lastContent,
|
|
992
|
+
became_idle: becameIdle,
|
|
993
|
+
ms: Date.now() - start,
|
|
994
|
+
...(tuiFinalOut ? { tui_state: tuiFinalOut } : {}),
|
|
995
|
+
...attentionFor(target, name),
|
|
996
|
+
...(lastError ? { error: lastError } : {}),
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
if (input.action === 'list') {
|
|
1001
|
+
const r =
|
|
1002
|
+
target === 'local'
|
|
1003
|
+
? await tmuxLocalList()
|
|
1004
|
+
: await tmuxRemoteList(ctx.agent, target);
|
|
1005
|
+
// Annotate local sessions with attention metadata (watched
|
|
1006
|
+
// coding-CLI sessions only; empty when the watcher is off).
|
|
1007
|
+
const mirror = target === 'local' ? readAttentionMirror() : {};
|
|
1008
|
+
const sessions = r.sessions.map((s) =>
|
|
1009
|
+
mirror[s.name] ? { ...s, attention: mirror[s.name] } : s,
|
|
1010
|
+
);
|
|
1011
|
+
return {
|
|
1012
|
+
action: 'list',
|
|
1013
|
+
ok: r.ok,
|
|
1014
|
+
target,
|
|
1015
|
+
count: sessions.length,
|
|
1016
|
+
sessions,
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
// kill
|
|
1021
|
+
const name = requireName(input, 'kill');
|
|
1022
|
+
const r =
|
|
1023
|
+
target === 'local'
|
|
1024
|
+
? await tmuxLocalKill(name)
|
|
1025
|
+
: await tmuxRemoteKill(ctx.agent, target, name);
|
|
1026
|
+
// Drop the origin record on successful kill — also when we're
|
|
1027
|
+
// about to return was_running=false since the session is gone
|
|
1028
|
+
// either way. Best-effort, never blocks.
|
|
1029
|
+
if (target === 'local') {
|
|
1030
|
+
try {
|
|
1031
|
+
removeTmuxOrigin(name);
|
|
1032
|
+
} catch (err) {
|
|
1033
|
+
logger.warn({
|
|
1034
|
+
msg: 'tmux.origin.remove_failed',
|
|
1035
|
+
name,
|
|
1036
|
+
err: (err as Error).message,
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
// tmux kill-session exits non-zero with "can't find session" on
|
|
1041
|
+
// stderr if the name is gone — treat that as "was_running:false"
|
|
1042
|
+
// rather than an error.
|
|
1043
|
+
if (!r.ok) {
|
|
1044
|
+
const wasMissing =
|
|
1045
|
+
r.stderr.includes("can't find session") ||
|
|
1046
|
+
r.stderr.includes('no server running') ||
|
|
1047
|
+
r.stderr.includes('session not found');
|
|
1048
|
+
if (wasMissing) {
|
|
1049
|
+
return { action: 'kill', ok: true, target, name, was_running: false };
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
return {
|
|
1053
|
+
action: 'kill',
|
|
1054
|
+
ok: r.ok,
|
|
1055
|
+
target,
|
|
1056
|
+
name,
|
|
1057
|
+
was_running: r.ok,
|
|
1058
|
+
};
|
|
1059
|
+
},
|
|
1060
|
+
};
|
|
1061
|
+
|
|
1062
|
+
export function tmuxTools(): ToolDefinition[] {
|
|
1063
|
+
return [tmux] as ToolDefinition[];
|
|
1064
|
+
}
|