@the-open-engine/zeroshot 5.4.0
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/CHANGELOG.md +539 -0
- package/LICENSE +21 -0
- package/README.md +508 -0
- package/cli/commands/inspect-render.js +135 -0
- package/cli/commands/inspect.js +294 -0
- package/cli/commands/providers.js +149 -0
- package/cli/index.js +5431 -0
- package/cli/lib/first-run.js +211 -0
- package/cli/lib/update-checker.js +281 -0
- package/cli/message-formatter-utils.js +75 -0
- package/cli/message-formatters-normal.js +275 -0
- package/cli/message-formatters-watch.js +185 -0
- package/cluster-templates/base-templates/debug-workflow.json +422 -0
- package/cluster-templates/base-templates/full-workflow.json +727 -0
- package/cluster-templates/base-templates/heavy-validation.json +272 -0
- package/cluster-templates/base-templates/quick-validation.json +285 -0
- package/cluster-templates/base-templates/single-worker.json +71 -0
- package/cluster-templates/base-templates/worker-validator.json +230 -0
- package/cluster-templates/conductor-bootstrap.json +130 -0
- package/docker/zeroshot-cluster/Dockerfile +153 -0
- package/docker/zeroshot-cluster/pre-baked-deps.json +28 -0
- package/lib/agent-cli-provider/adapters/claude-parser.d.ts +3 -0
- package/lib/agent-cli-provider/adapters/claude-parser.d.ts.map +1 -0
- package/lib/agent-cli-provider/adapters/claude-parser.js +122 -0
- package/lib/agent-cli-provider/adapters/claude-parser.js.map +1 -0
- package/lib/agent-cli-provider/adapters/claude-recovery.d.ts +15 -0
- package/lib/agent-cli-provider/adapters/claude-recovery.d.ts.map +1 -0
- package/lib/agent-cli-provider/adapters/claude-recovery.js +165 -0
- package/lib/agent-cli-provider/adapters/claude-recovery.js.map +1 -0
- package/lib/agent-cli-provider/adapters/claude.d.ts +3 -0
- package/lib/agent-cli-provider/adapters/claude.d.ts.map +1 -0
- package/lib/agent-cli-provider/adapters/claude.js +181 -0
- package/lib/agent-cli-provider/adapters/claude.js.map +1 -0
- package/lib/agent-cli-provider/adapters/codex-parser.d.ts +3 -0
- package/lib/agent-cli-provider/adapters/codex-parser.d.ts.map +1 -0
- package/lib/agent-cli-provider/adapters/codex-parser.js +192 -0
- package/lib/agent-cli-provider/adapters/codex-parser.js.map +1 -0
- package/lib/agent-cli-provider/adapters/codex.d.ts +3 -0
- package/lib/agent-cli-provider/adapters/codex.d.ts.map +1 -0
- package/lib/agent-cli-provider/adapters/codex.js +151 -0
- package/lib/agent-cli-provider/adapters/codex.js.map +1 -0
- package/lib/agent-cli-provider/adapters/common.d.ts +28 -0
- package/lib/agent-cli-provider/adapters/common.d.ts.map +1 -0
- package/lib/agent-cli-provider/adapters/common.js +71 -0
- package/lib/agent-cli-provider/adapters/common.js.map +1 -0
- package/lib/agent-cli-provider/adapters/gemini.d.ts +3 -0
- package/lib/agent-cli-provider/adapters/gemini.d.ts.map +1 -0
- package/lib/agent-cli-provider/adapters/gemini.js +176 -0
- package/lib/agent-cli-provider/adapters/gemini.js.map +1 -0
- package/lib/agent-cli-provider/adapters/index.d.ts +9 -0
- package/lib/agent-cli-provider/adapters/index.d.ts.map +1 -0
- package/lib/agent-cli-provider/adapters/index.js +95 -0
- package/lib/agent-cli-provider/adapters/index.js.map +1 -0
- package/lib/agent-cli-provider/adapters/opencode.d.ts +3 -0
- package/lib/agent-cli-provider/adapters/opencode.d.ts.map +1 -0
- package/lib/agent-cli-provider/adapters/opencode.js +228 -0
- package/lib/agent-cli-provider/adapters/opencode.js.map +1 -0
- package/lib/agent-cli-provider/contract-actions.d.ts +5 -0
- package/lib/agent-cli-provider/contract-actions.d.ts.map +1 -0
- package/lib/agent-cli-provider/contract-actions.js +140 -0
- package/lib/agent-cli-provider/contract-actions.js.map +1 -0
- package/lib/agent-cli-provider/contract-env.d.ts +11 -0
- package/lib/agent-cli-provider/contract-env.d.ts.map +1 -0
- package/lib/agent-cli-provider/contract-env.js +113 -0
- package/lib/agent-cli-provider/contract-env.js.map +1 -0
- package/lib/agent-cli-provider/contract-envelope.d.ts +49 -0
- package/lib/agent-cli-provider/contract-envelope.d.ts.map +1 -0
- package/lib/agent-cli-provider/contract-envelope.js +52 -0
- package/lib/agent-cli-provider/contract-envelope.js.map +1 -0
- package/lib/agent-cli-provider/contract-errors.d.ts +22 -0
- package/lib/agent-cli-provider/contract-errors.d.ts.map +1 -0
- package/lib/agent-cli-provider/contract-errors.js +71 -0
- package/lib/agent-cli-provider/contract-errors.js.map +1 -0
- package/lib/agent-cli-provider/contract-fallback.d.ts +7 -0
- package/lib/agent-cli-provider/contract-fallback.d.ts.map +1 -0
- package/lib/agent-cli-provider/contract-fallback.js +102 -0
- package/lib/agent-cli-provider/contract-fallback.js.map +1 -0
- package/lib/agent-cli-provider/contract-invoke.d.ts +5 -0
- package/lib/agent-cli-provider/contract-invoke.d.ts.map +1 -0
- package/lib/agent-cli-provider/contract-invoke.js +89 -0
- package/lib/agent-cli-provider/contract-invoke.js.map +1 -0
- package/lib/agent-cli-provider/contract-options.d.ts +3 -0
- package/lib/agent-cli-provider/contract-options.d.ts.map +1 -0
- package/lib/agent-cli-provider/contract-options.js +140 -0
- package/lib/agent-cli-provider/contract-options.js.map +1 -0
- package/lib/agent-cli-provider/contract-parse.d.ts +15 -0
- package/lib/agent-cli-provider/contract-parse.d.ts.map +1 -0
- package/lib/agent-cli-provider/contract-parse.js +81 -0
- package/lib/agent-cli-provider/contract-parse.js.map +1 -0
- package/lib/agent-cli-provider/contract-support.d.ts +18 -0
- package/lib/agent-cli-provider/contract-support.d.ts.map +1 -0
- package/lib/agent-cli-provider/contract-support.js +154 -0
- package/lib/agent-cli-provider/contract-support.js.map +1 -0
- package/lib/agent-cli-provider/contract.d.ts +13 -0
- package/lib/agent-cli-provider/contract.d.ts.map +1 -0
- package/lib/agent-cli-provider/contract.js +30 -0
- package/lib/agent-cli-provider/contract.js.map +1 -0
- package/lib/agent-cli-provider/env-safety.d.ts +11 -0
- package/lib/agent-cli-provider/env-safety.d.ts.map +1 -0
- package/lib/agent-cli-provider/env-safety.js +83 -0
- package/lib/agent-cli-provider/env-safety.js.map +1 -0
- package/lib/agent-cli-provider/errors.d.ts +5 -0
- package/lib/agent-cli-provider/errors.d.ts.map +1 -0
- package/lib/agent-cli-provider/errors.js +115 -0
- package/lib/agent-cli-provider/errors.js.map +1 -0
- package/lib/agent-cli-provider/executable.d.ts +3 -0
- package/lib/agent-cli-provider/executable.d.ts.map +1 -0
- package/lib/agent-cli-provider/executable.js +24 -0
- package/lib/agent-cli-provider/executable.js.map +1 -0
- package/lib/agent-cli-provider/index.d.ts +8 -0
- package/lib/agent-cli-provider/index.d.ts.map +1 -0
- package/lib/agent-cli-provider/index.js +31 -0
- package/lib/agent-cli-provider/index.js.map +1 -0
- package/lib/agent-cli-provider/invoke-evidence.d.ts +4 -0
- package/lib/agent-cli-provider/invoke-evidence.d.ts.map +1 -0
- package/lib/agent-cli-provider/invoke-evidence.js +17 -0
- package/lib/agent-cli-provider/invoke-evidence.js.map +1 -0
- package/lib/agent-cli-provider/json.d.ts +16 -0
- package/lib/agent-cli-provider/json.d.ts.map +1 -0
- package/lib/agent-cli-provider/json.js +110 -0
- package/lib/agent-cli-provider/json.js.map +1 -0
- package/lib/agent-cli-provider/log-prefix.d.ts +2 -0
- package/lib/agent-cli-provider/log-prefix.d.ts.map +1 -0
- package/lib/agent-cli-provider/log-prefix.js +22 -0
- package/lib/agent-cli-provider/log-prefix.js.map +1 -0
- package/lib/agent-cli-provider/process-runner.d.ts +17 -0
- package/lib/agent-cli-provider/process-runner.d.ts.map +1 -0
- package/lib/agent-cli-provider/process-runner.js +89 -0
- package/lib/agent-cli-provider/process-runner.js.map +1 -0
- package/lib/agent-cli-provider/redaction.d.ts +9 -0
- package/lib/agent-cli-provider/redaction.d.ts.map +1 -0
- package/lib/agent-cli-provider/redaction.js +227 -0
- package/lib/agent-cli-provider/redaction.js.map +1 -0
- package/lib/agent-cli-provider/schema.d.ts +5 -0
- package/lib/agent-cli-provider/schema.d.ts.map +1 -0
- package/lib/agent-cli-provider/schema.js +136 -0
- package/lib/agent-cli-provider/schema.js.map +1 -0
- package/lib/agent-cli-provider/single-agent-runtime.d.ts +15 -0
- package/lib/agent-cli-provider/single-agent-runtime.d.ts.map +1 -0
- package/lib/agent-cli-provider/single-agent-runtime.js +228 -0
- package/lib/agent-cli-provider/single-agent-runtime.js.map +1 -0
- package/lib/agent-cli-provider/types.d.ts +194 -0
- package/lib/agent-cli-provider/types.d.ts.map +1 -0
- package/lib/agent-cli-provider/types.js +12 -0
- package/lib/agent-cli-provider/types.js.map +1 -0
- package/lib/completion.js +174 -0
- package/lib/detached-startup.js +220 -0
- package/lib/docker-config.js +220 -0
- package/lib/git-remote-utils.js +165 -0
- package/lib/id-detector.js +55 -0
- package/lib/provider-defaults.js +62 -0
- package/lib/provider-detection.js +59 -0
- package/lib/provider-names.js +57 -0
- package/lib/repo-settings.js +69 -0
- package/lib/settings/claude-auth.js +78 -0
- package/lib/settings.js +542 -0
- package/lib/start-cluster.js +321 -0
- package/lib/stream-json-parser.js +67 -0
- package/package.json +162 -0
- package/scripts/fix-node-pty-permissions.js +75 -0
- package/scripts/record-demo.sh +279 -0
- package/scripts/setup-merge-queue.sh +170 -0
- package/scripts/test-install.sh +40 -0
- package/scripts/validate-templates.js +107 -0
- package/src/agent/agent-config.js +266 -0
- package/src/agent/agent-context-builder.js +189 -0
- package/src/agent/agent-context-sections.js +338 -0
- package/src/agent/agent-context-sources.js +147 -0
- package/src/agent/agent-hook-executor.js +721 -0
- package/src/agent/agent-input-injector.js +141 -0
- package/src/agent/agent-lifecycle.js +982 -0
- package/src/agent/agent-quality-gate-schema.js +93 -0
- package/src/agent/agent-quality-gates-context.js +51 -0
- package/src/agent/agent-stuck-detector.js +256 -0
- package/src/agent/agent-task-executor.js +2028 -0
- package/src/agent/agent-trigger-evaluator.js +67 -0
- package/src/agent/context-metrics.js +160 -0
- package/src/agent/context-pack-builder.js +367 -0
- package/src/agent/context-replay-policy.js +51 -0
- package/src/agent/guidance-queue.js +77 -0
- package/src/agent/output-extraction.js +367 -0
- package/src/agent/output-reformatter.js +175 -0
- package/src/agent/pr-verification.js +653 -0
- package/src/agent/rate-limit-backoff.js +82 -0
- package/src/agent/schema-utils.js +146 -0
- package/src/agent/validation-platform.js +35 -0
- package/src/agent-cli-provider/adapters/claude-parser.ts +133 -0
- package/src/agent-cli-provider/adapters/claude-recovery.ts +203 -0
- package/src/agent-cli-provider/adapters/claude.ts +247 -0
- package/src/agent-cli-provider/adapters/codex-parser.ts +211 -0
- package/src/agent-cli-provider/adapters/codex.ts +217 -0
- package/src/agent-cli-provider/adapters/common.ts +124 -0
- package/src/agent-cli-provider/adapters/gemini.ts +243 -0
- package/src/agent-cli-provider/adapters/index.ts +126 -0
- package/src/agent-cli-provider/adapters/opencode.ts +286 -0
- package/src/agent-cli-provider/contract-actions.ts +150 -0
- package/src/agent-cli-provider/contract-env.ts +111 -0
- package/src/agent-cli-provider/contract-envelope.ts +110 -0
- package/src/agent-cli-provider/contract-errors.ts +66 -0
- package/src/agent-cli-provider/contract-fallback.ts +121 -0
- package/src/agent-cli-provider/contract-invoke.ts +104 -0
- package/src/agent-cli-provider/contract-options.ts +173 -0
- package/src/agent-cli-provider/contract-parse.ts +94 -0
- package/src/agent-cli-provider/contract-support.ts +167 -0
- package/src/agent-cli-provider/contract.ts +56 -0
- package/src/agent-cli-provider/env-safety.ts +82 -0
- package/src/agent-cli-provider/errors.ts +122 -0
- package/src/agent-cli-provider/executable.ts +24 -0
- package/src/agent-cli-provider/index.ts +83 -0
- package/src/agent-cli-provider/invoke-evidence.ts +18 -0
- package/src/agent-cli-provider/json.ts +114 -0
- package/src/agent-cli-provider/log-prefix.ts +20 -0
- package/src/agent-cli-provider/process-runner.ts +145 -0
- package/src/agent-cli-provider/redaction.ts +282 -0
- package/src/agent-cli-provider/schema.ts +115 -0
- package/src/agent-cli-provider/single-agent-runtime.ts +311 -0
- package/src/agent-cli-provider/types.ts +237 -0
- package/src/agent-wrapper.js +615 -0
- package/src/agents/git-pusher-template.js +705 -0
- package/src/attach/attach-client.js +438 -0
- package/src/attach/attach-server.js +543 -0
- package/src/attach/index.js +37 -0
- package/src/attach/protocol.js +220 -0
- package/src/attach/ring-buffer.js +121 -0
- package/src/attach/send-input.js +88 -0
- package/src/attach/socket-discovery.js +267 -0
- package/src/claude-task-runner.js +661 -0
- package/src/config-router.js +89 -0
- package/src/config-validator.js +2202 -0
- package/src/copy-worker.js +43 -0
- package/src/guidance-topics.js +10 -0
- package/src/input-helpers.js +65 -0
- package/src/isolation-manager.js +1734 -0
- package/src/issue-providers/README.md +305 -0
- package/src/issue-providers/azure-devops-provider.js +307 -0
- package/src/issue-providers/base-provider.js +232 -0
- package/src/issue-providers/github-provider.js +210 -0
- package/src/issue-providers/gitlab-provider.js +262 -0
- package/src/issue-providers/index.js +196 -0
- package/src/issue-providers/jira-provider.js +260 -0
- package/src/ledger.js +692 -0
- package/src/lib/gc.js +232 -0
- package/src/lib/safe-exec.js +88 -0
- package/src/logic-engine.js +201 -0
- package/src/message-buffer.js +81 -0
- package/src/message-bus-bridge.js +144 -0
- package/src/message-bus.js +256 -0
- package/src/name-generator.js +232 -0
- package/src/orchestrator.js +3924 -0
- package/src/preflight.js +712 -0
- package/src/process-metrics.js +608 -0
- package/src/providers/anthropic/index.js +3 -0
- package/src/providers/base-provider.js +355 -0
- package/src/providers/capabilities.js +60 -0
- package/src/providers/google/index.js +3 -0
- package/src/providers/index.js +293 -0
- package/src/providers/openai/index.js +3 -0
- package/src/providers/opencode/index.js +3 -0
- package/src/quality-gates.js +143 -0
- package/src/schemas/sub-cluster.js +208 -0
- package/src/state-snapshot.js +398 -0
- package/src/state-snapshotter.js +142 -0
- package/src/status-footer.js +1026 -0
- package/src/sub-cluster-wrapper.js +693 -0
- package/src/task-runner.js +30 -0
- package/src/template-resolver.js +425 -0
- package/src/template-validation/index.js +338 -0
- package/src/template-validation/simulate-consensus-gates.js +324 -0
- package/src/template-validation/simulate-random-topology.js +541 -0
- package/src/template-validation/simulate-two-stage-validation.js +270 -0
- package/src/worktree-claude-config.js +135 -0
- package/src/worktree-tooling-env.js +150 -0
- package/task-lib/attachable-watcher.js +381 -0
- package/task-lib/commands/clean.js +50 -0
- package/task-lib/commands/episodes.js +105 -0
- package/task-lib/commands/get-log-path.js +23 -0
- package/task-lib/commands/kill.js +32 -0
- package/task-lib/commands/list.js +105 -0
- package/task-lib/commands/logs.js +439 -0
- package/task-lib/commands/resume.js +42 -0
- package/task-lib/commands/run.js +57 -0
- package/task-lib/commands/schedule.js +105 -0
- package/task-lib/commands/scheduler-cmd.js +96 -0
- package/task-lib/commands/schedules.js +148 -0
- package/task-lib/commands/status.js +44 -0
- package/task-lib/commands/unschedule.js +16 -0
- package/task-lib/completion.js +9 -0
- package/task-lib/config.js +11 -0
- package/task-lib/name-generator.js +230 -0
- package/task-lib/package.json +3 -0
- package/task-lib/provider-helper-runtime.js +29 -0
- package/task-lib/runner.js +190 -0
- package/task-lib/scheduler.js +252 -0
- package/task-lib/store.js +529 -0
- package/task-lib/watcher.js +305 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AttachClient - Terminal client for attaching to running tasks/agents
|
|
3
|
+
*
|
|
4
|
+
* Key sequences:
|
|
5
|
+
* - Ctrl+C: Clean detach (return to shell, task continues running)
|
|
6
|
+
* - Ctrl+B d: Also detach (for tmux muscle memory)
|
|
7
|
+
* - Ctrl+B ?: Show help
|
|
8
|
+
* - Ctrl+B c: Send SIGINT to process (interrupt agent - USE WITH CAUTION)
|
|
9
|
+
* - Ctrl+Z: Forward SIGTSTP to process
|
|
10
|
+
*
|
|
11
|
+
* Features:
|
|
12
|
+
* - Raw terminal mode (passes through all input)
|
|
13
|
+
* - Output history replay on attach
|
|
14
|
+
* - Terminal resize forwarding
|
|
15
|
+
* - Graceful cleanup on exit
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const net = require('net');
|
|
19
|
+
const EventEmitter = require('events');
|
|
20
|
+
const crypto = require('crypto');
|
|
21
|
+
|
|
22
|
+
const protocol = require('./protocol');
|
|
23
|
+
|
|
24
|
+
// Key codes
|
|
25
|
+
const CTRL_B = '\x02';
|
|
26
|
+
const CTRL_C = '\x03';
|
|
27
|
+
const CTRL_Z = '\x1a';
|
|
28
|
+
|
|
29
|
+
// Detach timeout (ms) - how long to wait for second key after Ctrl+B
|
|
30
|
+
const DETACH_TIMEOUT = 500;
|
|
31
|
+
|
|
32
|
+
class AttachClient extends EventEmitter {
|
|
33
|
+
/**
|
|
34
|
+
* @param {object} options
|
|
35
|
+
* @param {string} options.socketPath - Unix socket path to connect to
|
|
36
|
+
* @param {object} [options.stdin] - Input stream (default process.stdin)
|
|
37
|
+
* @param {object} [options.stdout] - Output stream (default process.stdout)
|
|
38
|
+
*/
|
|
39
|
+
constructor(options) {
|
|
40
|
+
super();
|
|
41
|
+
|
|
42
|
+
if (!options.socketPath) {
|
|
43
|
+
throw new Error('AttachClient: socketPath is required');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.socketPath = options.socketPath;
|
|
47
|
+
this.stdin = options.stdin || process.stdin;
|
|
48
|
+
this.stdout = options.stdout || process.stdout;
|
|
49
|
+
|
|
50
|
+
this.clientId = crypto.randomUUID();
|
|
51
|
+
this.socket = null;
|
|
52
|
+
this.decoder = new protocol.MessageDecoder();
|
|
53
|
+
this.connected = false;
|
|
54
|
+
this.wasRawMode = null;
|
|
55
|
+
|
|
56
|
+
// Ctrl+B sequence detection
|
|
57
|
+
this.ctrlBPressed = false;
|
|
58
|
+
this.ctrlBTimeout = null;
|
|
59
|
+
|
|
60
|
+
// Bind handlers
|
|
61
|
+
this._onSocketData = this._onSocketData.bind(this);
|
|
62
|
+
this._onSocketClose = this._onSocketClose.bind(this);
|
|
63
|
+
this._onSocketError = this._onSocketError.bind(this);
|
|
64
|
+
this._onStdinData = this._onStdinData.bind(this);
|
|
65
|
+
this._onResize = this._onResize.bind(this);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Connect to the attach server
|
|
70
|
+
* @returns {Promise<void>}
|
|
71
|
+
*/
|
|
72
|
+
connect() {
|
|
73
|
+
if (this.connected) {
|
|
74
|
+
throw new Error('AttachClient: Already connected');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
this.socket = net.createConnection(this.socketPath);
|
|
79
|
+
|
|
80
|
+
this.socket.on('connect', () => {
|
|
81
|
+
this.connected = true;
|
|
82
|
+
|
|
83
|
+
// Send attach message with terminal dimensions
|
|
84
|
+
const cols = this.stdout.columns || 80;
|
|
85
|
+
const rows = this.stdout.rows || 24;
|
|
86
|
+
|
|
87
|
+
this.socket.write(protocol.encode(protocol.createAttachMessage(this.clientId, cols, rows)));
|
|
88
|
+
|
|
89
|
+
// Set up terminal
|
|
90
|
+
this._setupTerminal();
|
|
91
|
+
|
|
92
|
+
// Set up socket handlers
|
|
93
|
+
this.socket.on('data', this._onSocketData);
|
|
94
|
+
this.socket.on('close', this._onSocketClose);
|
|
95
|
+
this.socket.on('error', this._onSocketError);
|
|
96
|
+
|
|
97
|
+
resolve();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
this.socket.on('error', (err) => {
|
|
101
|
+
if (!this.connected) {
|
|
102
|
+
reject(err);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Connection timeout
|
|
107
|
+
const timeout = setTimeout(() => {
|
|
108
|
+
if (!this.connected) {
|
|
109
|
+
this.socket.destroy();
|
|
110
|
+
reject(new Error('Connection timeout'));
|
|
111
|
+
}
|
|
112
|
+
}, 5000);
|
|
113
|
+
|
|
114
|
+
this.socket.on('connect', () => clearTimeout(timeout));
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Disconnect from the server
|
|
120
|
+
*/
|
|
121
|
+
disconnect() {
|
|
122
|
+
if (!this.connected) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Send detach message
|
|
127
|
+
try {
|
|
128
|
+
this.socket.write(protocol.encode(protocol.createDetachMessage(this.clientId)));
|
|
129
|
+
} catch {
|
|
130
|
+
// Ignore
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
this._cleanup();
|
|
134
|
+
this.emit('detach');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Send a signal to the remote process
|
|
139
|
+
* @param {string} signal - Signal name
|
|
140
|
+
*/
|
|
141
|
+
sendSignal(signal) {
|
|
142
|
+
if (!this.connected) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
this.socket.write(protocol.encode(protocol.createSignalMessage(signal)));
|
|
148
|
+
} catch {
|
|
149
|
+
// Ignore
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ─────────────────────────────────────────────────────────────────
|
|
154
|
+
// Private methods
|
|
155
|
+
// ─────────────────────────────────────────────────────────────────
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Set up terminal for raw mode
|
|
159
|
+
* @private
|
|
160
|
+
*/
|
|
161
|
+
_setupTerminal() {
|
|
162
|
+
// Enable raw mode if stdin is a TTY
|
|
163
|
+
if (this.stdin.isTTY && this.stdin.setRawMode) {
|
|
164
|
+
this.wasRawMode = this.stdin.isRaw;
|
|
165
|
+
this.stdin.setRawMode(true);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Resume stdin (may be paused)
|
|
169
|
+
this.stdin.resume();
|
|
170
|
+
|
|
171
|
+
// Listen for input
|
|
172
|
+
this.stdin.on('data', this._onStdinData);
|
|
173
|
+
|
|
174
|
+
// Listen for resize events
|
|
175
|
+
if (this.stdout.isTTY) {
|
|
176
|
+
this.stdout.on('resize', this._onResize);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Handle process signals for cleanup
|
|
180
|
+
process.on('SIGINT', () => {
|
|
181
|
+
// Clean detach on Ctrl+C - task continues running
|
|
182
|
+
this.disconnect();
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
process.on('SIGTERM', () => {
|
|
186
|
+
this._cleanup();
|
|
187
|
+
process.exit(0);
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Restore terminal state
|
|
193
|
+
* @private
|
|
194
|
+
*/
|
|
195
|
+
_restoreTerminal() {
|
|
196
|
+
// Restore raw mode
|
|
197
|
+
if (this.stdin.isTTY && this.stdin.setRawMode && this.wasRawMode !== null) {
|
|
198
|
+
this.stdin.setRawMode(this.wasRawMode);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Remove listeners
|
|
202
|
+
this.stdin.removeListener('data', this._onStdinData);
|
|
203
|
+
if (this.stdout.isTTY) {
|
|
204
|
+
this.stdout.removeListener('resize', this._onResize);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Pause stdin
|
|
208
|
+
this.stdin.pause();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Handle data from socket
|
|
213
|
+
* @private
|
|
214
|
+
*/
|
|
215
|
+
_onSocketData(data) {
|
|
216
|
+
try {
|
|
217
|
+
const messages = this.decoder.feed(data);
|
|
218
|
+
for (const msg of messages) {
|
|
219
|
+
this._handleMessage(msg);
|
|
220
|
+
}
|
|
221
|
+
} catch (e) {
|
|
222
|
+
this.emit('error', new Error(`Protocol error: ${e.message}`));
|
|
223
|
+
this._cleanup();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Handle message from server
|
|
229
|
+
* @private
|
|
230
|
+
*/
|
|
231
|
+
_handleMessage(message) {
|
|
232
|
+
switch (message.type) {
|
|
233
|
+
case protocol.MessageType.OUTPUT: {
|
|
234
|
+
const data = protocol.decodeData(message);
|
|
235
|
+
if (data) {
|
|
236
|
+
this.stdout.write(data);
|
|
237
|
+
}
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
case protocol.MessageType.HISTORY: {
|
|
242
|
+
const data = protocol.decodeData(message);
|
|
243
|
+
if (data) {
|
|
244
|
+
this.stdout.write(data);
|
|
245
|
+
}
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
case protocol.MessageType.STATE: {
|
|
250
|
+
this.emit('state', message);
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
case protocol.MessageType.EXIT: {
|
|
255
|
+
const { code, signal } = message;
|
|
256
|
+
this.emit('exit', { code, signal });
|
|
257
|
+
this._cleanup();
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
case protocol.MessageType.ERROR: {
|
|
262
|
+
this.emit('error', new Error(message.message));
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Handle stdin data
|
|
270
|
+
* @private
|
|
271
|
+
*/
|
|
272
|
+
_onStdinData(data) {
|
|
273
|
+
const str = data.toString();
|
|
274
|
+
|
|
275
|
+
// Handle Ctrl+B sequence
|
|
276
|
+
if (this.ctrlBPressed) {
|
|
277
|
+
this.ctrlBPressed = false;
|
|
278
|
+
if (this.ctrlBTimeout) {
|
|
279
|
+
clearTimeout(this.ctrlBTimeout);
|
|
280
|
+
this.ctrlBTimeout = null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Check for command keys
|
|
284
|
+
if (str === 'd' || str === 'D') {
|
|
285
|
+
// Detach
|
|
286
|
+
this.disconnect();
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (str === 'c' || str === 'C') {
|
|
291
|
+
// Send SIGINT to process (interrupt agent - USE WITH CAUTION)
|
|
292
|
+
this.stdout.write('\r\n⚠️ Sending SIGINT to agent (interrupting task)...\r\n');
|
|
293
|
+
this.sendSignal('SIGINT');
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (str === '?') {
|
|
298
|
+
// Show help
|
|
299
|
+
this._showHelp();
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Not a recognized command, forward Ctrl+B + this key
|
|
304
|
+
this._forwardInput(Buffer.from([0x02]));
|
|
305
|
+
this._forwardInput(data);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Check for Ctrl+B
|
|
310
|
+
if (str === CTRL_B) {
|
|
311
|
+
this.ctrlBPressed = true;
|
|
312
|
+
|
|
313
|
+
// Set timeout to forward if no follow-up key
|
|
314
|
+
this.ctrlBTimeout = setTimeout(() => {
|
|
315
|
+
if (this.ctrlBPressed) {
|
|
316
|
+
this.ctrlBPressed = false;
|
|
317
|
+
this._forwardInput(data);
|
|
318
|
+
}
|
|
319
|
+
}, DETACH_TIMEOUT);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Check for Ctrl+C - clean detach (task continues running)
|
|
324
|
+
if (str === CTRL_C) {
|
|
325
|
+
this.disconnect();
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Check for Ctrl+Z
|
|
330
|
+
if (str === CTRL_Z) {
|
|
331
|
+
this.sendSignal('SIGTSTP');
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Forward other input (future interactive mode)
|
|
336
|
+
this._forwardInput(data);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Forward input to remote process
|
|
341
|
+
* @private
|
|
342
|
+
*/
|
|
343
|
+
_forwardInput(data) {
|
|
344
|
+
if (!this.connected) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
try {
|
|
349
|
+
this.socket.write(protocol.encode(protocol.createStdinMessage(data)));
|
|
350
|
+
} catch {
|
|
351
|
+
// Ignore
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Handle terminal resize
|
|
357
|
+
* @private
|
|
358
|
+
*/
|
|
359
|
+
_onResize() {
|
|
360
|
+
if (!this.connected) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const cols = this.stdout.columns;
|
|
365
|
+
const rows = this.stdout.rows;
|
|
366
|
+
|
|
367
|
+
try {
|
|
368
|
+
this.socket.write(protocol.encode(protocol.createResizeMessage(cols, rows)));
|
|
369
|
+
} catch {
|
|
370
|
+
// Ignore
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Handle socket close
|
|
376
|
+
* @private
|
|
377
|
+
*/
|
|
378
|
+
_onSocketClose() {
|
|
379
|
+
if (this.connected) {
|
|
380
|
+
this.emit('close');
|
|
381
|
+
this._cleanup();
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Handle socket error
|
|
387
|
+
* @private
|
|
388
|
+
*/
|
|
389
|
+
_onSocketError(err) {
|
|
390
|
+
this.emit('error', err);
|
|
391
|
+
this._cleanup();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Show help message
|
|
396
|
+
* @private
|
|
397
|
+
*/
|
|
398
|
+
_showHelp() {
|
|
399
|
+
const help = `
|
|
400
|
+
\r\n╭──────────────────────────────────────────────────────────╮
|
|
401
|
+
\r\n│ Vibe Attach - Key Bindings │
|
|
402
|
+
\r\n├──────────────────────────────────────────────────────────┤
|
|
403
|
+
\r\n│ Ctrl+C Detach (task continues running) │
|
|
404
|
+
\r\n│ Ctrl+B d Also detach (for tmux muscle memory) │
|
|
405
|
+
\r\n│ Ctrl+B ? Show this help │
|
|
406
|
+
\r\n│ Ctrl+B c ⚠️ Interrupt agent (sends SIGINT) │
|
|
407
|
+
\r\n│ Ctrl+Z Suspend process (sends SIGTSTP) │
|
|
408
|
+
\r\n╰──────────────────────────────────────────────────────────╯
|
|
409
|
+
\r\n`;
|
|
410
|
+
this.stdout.write(help);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Clean up resources
|
|
415
|
+
* @private
|
|
416
|
+
*/
|
|
417
|
+
_cleanup() {
|
|
418
|
+
this.connected = false;
|
|
419
|
+
|
|
420
|
+
// Clear Ctrl+B timeout
|
|
421
|
+
if (this.ctrlBTimeout) {
|
|
422
|
+
clearTimeout(this.ctrlBTimeout);
|
|
423
|
+
this.ctrlBTimeout = null;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Restore terminal
|
|
427
|
+
this._restoreTerminal();
|
|
428
|
+
|
|
429
|
+
// Close socket
|
|
430
|
+
if (this.socket) {
|
|
431
|
+
this.socket.removeAllListeners();
|
|
432
|
+
this.socket.destroy();
|
|
433
|
+
this.socket = null;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
module.exports = AttachClient;
|