@jmoyers/harness 0.1.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/LICENSE +21 -0
- package/README.md +145 -0
- package/native/ptyd/Cargo.lock +16 -0
- package/native/ptyd/Cargo.toml +7 -0
- package/native/ptyd/src/main.rs +257 -0
- package/package.json +90 -0
- package/scripts/build-ptyd.sh +73 -0
- package/scripts/control-plane-daemon.ts +277 -0
- package/scripts/cursor-hook-relay.ts +82 -0
- package/scripts/harness-animate.ts +469 -0
- package/scripts/harness-bin.js +77 -0
- package/scripts/harness-core.ts +1 -0
- package/scripts/harness-inspector.ts +439 -0
- package/scripts/harness.ts +2493 -0
- package/src/adapters/agent-session-state.ts +390 -0
- package/src/cli/gateway-record.ts +173 -0
- package/src/codex/live-session.ts +872 -0
- package/src/config/config-core.ts +1359 -0
- package/src/config/secrets-core.ts +170 -0
- package/src/control-plane/agent-realtime-api.ts +2441 -0
- package/src/control-plane/codex-session-stream.ts +392 -0
- package/src/control-plane/codex-telemetry.ts +1325 -0
- package/src/control-plane/lifecycle-hooks.ts +706 -0
- package/src/control-plane/session-summary.ts +380 -0
- package/src/control-plane/status/agent-status-reducer.ts +21 -0
- package/src/control-plane/status/reducer-base.ts +170 -0
- package/src/control-plane/status/reducers/claude-status-reducer.ts +37 -0
- package/src/control-plane/status/reducers/codex-status-reducer.ts +48 -0
- package/src/control-plane/status/reducers/critique-status-reducer.ts +15 -0
- package/src/control-plane/status/reducers/cursor-status-reducer.ts +37 -0
- package/src/control-plane/status/reducers/terminal-status-reducer.ts +15 -0
- package/src/control-plane/status/session-status-engine.ts +76 -0
- package/src/control-plane/stream-client.ts +396 -0
- package/src/control-plane/stream-command-parser.ts +1673 -0
- package/src/control-plane/stream-protocol.ts +1808 -0
- package/src/control-plane/stream-server-background.ts +486 -0
- package/src/control-plane/stream-server-command.ts +2557 -0
- package/src/control-plane/stream-server-connection.ts +234 -0
- package/src/control-plane/stream-server-observed-filter.ts +112 -0
- package/src/control-plane/stream-server-session-runtime.ts +566 -0
- package/src/control-plane/stream-server-state-store.ts +15 -0
- package/src/control-plane/stream-server.ts +3192 -0
- package/src/cursor/managed-hooks.ts +282 -0
- package/src/domain/conversations.ts +414 -0
- package/src/domain/directories.ts +78 -0
- package/src/domain/repositories.ts +123 -0
- package/src/domain/tasks.ts +148 -0
- package/src/domain/workspace.ts +156 -0
- package/src/events/normalized-events.ts +124 -0
- package/src/mux/ansi-integrity.ts +103 -0
- package/src/mux/control-plane-op-queue.ts +212 -0
- package/src/mux/conversation-rail.ts +339 -0
- package/src/mux/double-click.ts +78 -0
- package/src/mux/dual-pane-core.ts +435 -0
- package/src/mux/harness-core-ui.ts +817 -0
- package/src/mux/input-shortcuts.ts +667 -0
- package/src/mux/live-mux/actions-conversation.ts +344 -0
- package/src/mux/live-mux/actions-repository.ts +246 -0
- package/src/mux/live-mux/actions-task.ts +115 -0
- package/src/mux/live-mux/args.ts +142 -0
- package/src/mux/live-mux/command-menu.ts +298 -0
- package/src/mux/live-mux/control-plane-records.ts +546 -0
- package/src/mux/live-mux/conversation-state.ts +188 -0
- package/src/mux/live-mux/directory-resolution.ts +34 -0
- package/src/mux/live-mux/event-mapping.ts +96 -0
- package/src/mux/live-mux/gateway-profiler.ts +152 -0
- package/src/mux/live-mux/gateway-render-trace.ts +177 -0
- package/src/mux/live-mux/gateway-status-timeline.ts +166 -0
- package/src/mux/live-mux/git-parsing.ts +131 -0
- package/src/mux/live-mux/git-snapshot.ts +263 -0
- package/src/mux/live-mux/git-state.ts +136 -0
- package/src/mux/live-mux/global-shortcut-handlers.ts +143 -0
- package/src/mux/live-mux/home-pane-actions.ts +58 -0
- package/src/mux/live-mux/home-pane-drop.ts +44 -0
- package/src/mux/live-mux/home-pane-entity-click.ts +96 -0
- package/src/mux/live-mux/home-pane-pointer.ts +96 -0
- package/src/mux/live-mux/input-forwarding.ts +112 -0
- package/src/mux/live-mux/layout.ts +30 -0
- package/src/mux/live-mux/left-nav-activation.ts +103 -0
- package/src/mux/live-mux/left-nav.ts +85 -0
- package/src/mux/live-mux/left-rail-actions.ts +118 -0
- package/src/mux/live-mux/left-rail-conversation-click.ts +82 -0
- package/src/mux/live-mux/left-rail-pointer.ts +74 -0
- package/src/mux/live-mux/modal-command-menu-handler.ts +101 -0
- package/src/mux/live-mux/modal-conversation-handlers.ts +217 -0
- package/src/mux/live-mux/modal-input-reducers.ts +94 -0
- package/src/mux/live-mux/modal-overlays.ts +287 -0
- package/src/mux/live-mux/modal-pointer.ts +70 -0
- package/src/mux/live-mux/modal-prompt-handlers.ts +187 -0
- package/src/mux/live-mux/modal-task-editor-handler.ts +156 -0
- package/src/mux/live-mux/observed-stream.ts +87 -0
- package/src/mux/live-mux/palette-parsing.ts +128 -0
- package/src/mux/live-mux/pointer-routing.ts +108 -0
- package/src/mux/live-mux/process-usage.ts +53 -0
- package/src/mux/live-mux/project-pane-pointer.ts +44 -0
- package/src/mux/live-mux/rail-layout.ts +244 -0
- package/src/mux/live-mux/render-trace-analysis.ts +213 -0
- package/src/mux/live-mux/render-trace-state.ts +84 -0
- package/src/mux/live-mux/repository-folding.ts +207 -0
- package/src/mux/live-mux/runtime-shutdown.ts +51 -0
- package/src/mux/live-mux/selection.ts +411 -0
- package/src/mux/live-mux/startup-utils.ts +187 -0
- package/src/mux/live-mux/status-timeline-state.ts +82 -0
- package/src/mux/live-mux/task-pane-shortcuts.ts +206 -0
- package/src/mux/live-mux/terminal-palette.ts +79 -0
- package/src/mux/new-thread-prompt.ts +165 -0
- package/src/mux/project-tree.ts +295 -0
- package/src/mux/render-frame.ts +113 -0
- package/src/mux/runtime-wiring.ts +185 -0
- package/src/mux/selector-index.ts +160 -0
- package/src/mux/startup-sequencer.ts +238 -0
- package/src/mux/task-composer.ts +289 -0
- package/src/mux/task-focused-pane.ts +417 -0
- package/src/mux/task-screen-keybindings.ts +539 -0
- package/src/mux/terminal-input-modes.ts +35 -0
- package/src/mux/workspace-path.ts +55 -0
- package/src/mux/workspace-rail-model.ts +701 -0
- package/src/mux/workspace-rail.ts +247 -0
- package/src/perf/perf-core.ts +307 -0
- package/src/pty/pty_host.ts +217 -0
- package/src/pty/session-broker.ts +158 -0
- package/src/recording/terminal-recording.ts +383 -0
- package/src/services/control-plane.ts +567 -0
- package/src/services/conversation-lifecycle.ts +176 -0
- package/src/services/conversation-startup-hydration.ts +47 -0
- package/src/services/directory-hydration.ts +49 -0
- package/src/services/event-persistence.ts +104 -0
- package/src/services/mux-ui-state-persistence.ts +82 -0
- package/src/services/output-load-sampler.ts +231 -0
- package/src/services/process-usage-refresh.ts +88 -0
- package/src/services/recording.ts +75 -0
- package/src/services/render-trace-recorder.ts +177 -0
- package/src/services/runtime-control-actions.ts +123 -0
- package/src/services/runtime-control-plane-ops.ts +131 -0
- package/src/services/runtime-conversation-actions.ts +113 -0
- package/src/services/runtime-conversation-activation.ts +78 -0
- package/src/services/runtime-conversation-starter.ts +171 -0
- package/src/services/runtime-conversation-title-edit.ts +149 -0
- package/src/services/runtime-directory-actions.ts +164 -0
- package/src/services/runtime-envelope-handler.ts +198 -0
- package/src/services/runtime-git-state.ts +92 -0
- package/src/services/runtime-input-pipeline.ts +50 -0
- package/src/services/runtime-input-router.ts +202 -0
- package/src/services/runtime-layout-resize.ts +236 -0
- package/src/services/runtime-left-rail-render.ts +159 -0
- package/src/services/runtime-main-pane-input.ts +230 -0
- package/src/services/runtime-modal-input.ts +119 -0
- package/src/services/runtime-navigation-input.ts +207 -0
- package/src/services/runtime-process-wiring.ts +68 -0
- package/src/services/runtime-rail-input.ts +287 -0
- package/src/services/runtime-render-flush.ts +146 -0
- package/src/services/runtime-render-lifecycle.ts +104 -0
- package/src/services/runtime-render-orchestrator.ts +108 -0
- package/src/services/runtime-render-pipeline.ts +167 -0
- package/src/services/runtime-render-state.ts +72 -0
- package/src/services/runtime-repository-actions.ts +197 -0
- package/src/services/runtime-right-pane-render.ts +132 -0
- package/src/services/runtime-shutdown.ts +79 -0
- package/src/services/runtime-stream-subscriptions.ts +56 -0
- package/src/services/runtime-task-composer-persistence.ts +139 -0
- package/src/services/runtime-task-editor-actions.ts +83 -0
- package/src/services/runtime-task-pane-actions.ts +198 -0
- package/src/services/runtime-task-pane-shortcuts.ts +189 -0
- package/src/services/runtime-task-pane.ts +62 -0
- package/src/services/runtime-workspace-actions.ts +153 -0
- package/src/services/runtime-workspace-observed-events.ts +190 -0
- package/src/services/session-projection-instrumentation.ts +190 -0
- package/src/services/startup-background-probe.ts +91 -0
- package/src/services/startup-background-resume.ts +65 -0
- package/src/services/startup-orchestrator.ts +166 -0
- package/src/services/startup-output-tracker.ts +54 -0
- package/src/services/startup-paint-tracker.ts +115 -0
- package/src/services/startup-persisted-conversation-queue.ts +45 -0
- package/src/services/startup-settled-gate.ts +67 -0
- package/src/services/startup-shutdown.ts +53 -0
- package/src/services/startup-span-tracker.ts +77 -0
- package/src/services/startup-state-hydration.ts +94 -0
- package/src/services/startup-visibility.ts +35 -0
- package/src/services/status-timeline-recorder.ts +144 -0
- package/src/services/task-pane-selection-actions.ts +153 -0
- package/src/services/task-planning-hydration.ts +58 -0
- package/src/services/task-planning-observed-events.ts +89 -0
- package/src/services/workspace-observed-events.ts +113 -0
- package/src/store/control-plane-store-normalize.ts +760 -0
- package/src/store/control-plane-store-types.ts +224 -0
- package/src/store/control-plane-store.ts +2951 -0
- package/src/store/event-store.ts +253 -0
- package/src/store/sqlite.ts +81 -0
- package/src/terminal/compat-matrix.ts +345 -0
- package/src/terminal/differential-checkpoints.ts +132 -0
- package/src/terminal/parity-suite.ts +441 -0
- package/src/terminal/snapshot-oracle.ts +1840 -0
- package/src/ui/conversation-input-forwarder.ts +114 -0
- package/src/ui/conversation-selection-input.ts +103 -0
- package/src/ui/debug-footer-notice.ts +39 -0
- package/src/ui/global-shortcut-input.ts +126 -0
- package/src/ui/input-preflight.ts +68 -0
- package/src/ui/input-token-router.ts +312 -0
- package/src/ui/input.ts +238 -0
- package/src/ui/kit.ts +509 -0
- package/src/ui/left-nav-input.ts +80 -0
- package/src/ui/left-rail-pointer-input.ts +148 -0
- package/src/ui/main-pane-pointer-input.ts +150 -0
- package/src/ui/modals/manager.ts +192 -0
- package/src/ui/mux-theme.ts +529 -0
- package/src/ui/panes/conversation.ts +19 -0
- package/src/ui/panes/home-gridfire.ts +302 -0
- package/src/ui/panes/home.ts +109 -0
- package/src/ui/panes/left-rail.ts +12 -0
- package/src/ui/panes/project.ts +44 -0
- package/src/ui/pointer-routing-input.ts +158 -0
- package/src/ui/repository-fold-input.ts +91 -0
- package/src/ui/screen.ts +210 -0
- package/src/ui/surface.ts +224 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { StreamSessionStatusModel } from '../../stream-protocol.ts';
|
|
2
|
+
import type { AgentStatusProjectionInput } from '../agent-status-reducer.ts';
|
|
3
|
+
import { BaseAgentStatusReducer } from '../reducer-base.ts';
|
|
4
|
+
|
|
5
|
+
export class TerminalStatusReducer extends BaseAgentStatusReducer {
|
|
6
|
+
readonly agentType = 'terminal';
|
|
7
|
+
|
|
8
|
+
constructor() {
|
|
9
|
+
super();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
override project(_input: AgentStatusProjectionInput): StreamSessionStatusModel | null {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { StreamSessionStatusModel, StreamTelemetrySummary } from '../stream-protocol.ts';
|
|
2
|
+
import {
|
|
3
|
+
AGENT_STATUS_REDUCER_RUNTIME_TOKEN,
|
|
4
|
+
type AgentStatusProjectionInput,
|
|
5
|
+
type AgentStatusReducer,
|
|
6
|
+
} from './agent-status-reducer.ts';
|
|
7
|
+
import { ClaudeStatusReducer } from './reducers/claude-status-reducer.ts';
|
|
8
|
+
import { CodexStatusReducer } from './reducers/codex-status-reducer.ts';
|
|
9
|
+
import { CritiqueStatusReducer } from './reducers/critique-status-reducer.ts';
|
|
10
|
+
import { CursorStatusReducer } from './reducers/cursor-status-reducer.ts';
|
|
11
|
+
import { TerminalStatusReducer } from './reducers/terminal-status-reducer.ts';
|
|
12
|
+
|
|
13
|
+
type SupportedAgentType = 'codex' | 'claude' | 'cursor' | 'terminal' | 'critique';
|
|
14
|
+
|
|
15
|
+
const reducers: Record<SupportedAgentType, AgentStatusReducer> = {
|
|
16
|
+
codex: new CodexStatusReducer(),
|
|
17
|
+
claude: new ClaudeStatusReducer(),
|
|
18
|
+
cursor: new CursorStatusReducer(),
|
|
19
|
+
terminal: new TerminalStatusReducer(),
|
|
20
|
+
critique: new CritiqueStatusReducer(),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
void AGENT_STATUS_REDUCER_RUNTIME_TOKEN;
|
|
24
|
+
|
|
25
|
+
function normalizeAgentType(value: string): SupportedAgentType {
|
|
26
|
+
const normalized = value.trim().toLowerCase();
|
|
27
|
+
if (
|
|
28
|
+
normalized === 'codex' ||
|
|
29
|
+
normalized === 'claude' ||
|
|
30
|
+
normalized === 'cursor' ||
|
|
31
|
+
normalized === 'terminal' ||
|
|
32
|
+
normalized === 'critique'
|
|
33
|
+
) {
|
|
34
|
+
return normalized;
|
|
35
|
+
}
|
|
36
|
+
return 'terminal';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface SessionStatusEngineInput {
|
|
40
|
+
readonly agentType: string;
|
|
41
|
+
readonly runtimeStatus: AgentStatusProjectionInput['runtimeStatus'];
|
|
42
|
+
readonly attentionReason: string | null;
|
|
43
|
+
readonly telemetry: StreamTelemetrySummary | null;
|
|
44
|
+
readonly observedAt: string;
|
|
45
|
+
readonly previous: StreamSessionStatusModel | null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class SessionStatusEngine {
|
|
49
|
+
private readonly reducers: Record<SupportedAgentType, AgentStatusReducer>;
|
|
50
|
+
|
|
51
|
+
constructor(reducersByType: Record<SupportedAgentType, AgentStatusReducer> = reducers) {
|
|
52
|
+
this.reducers = reducersByType;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
project(input: SessionStatusEngineInput): StreamSessionStatusModel | null {
|
|
56
|
+
const normalizedAgentType = normalizeAgentType(input.agentType);
|
|
57
|
+
const reducer = this.reducers[normalizedAgentType];
|
|
58
|
+
const telemetry = input.telemetry;
|
|
59
|
+
return reducer.project({
|
|
60
|
+
runtimeStatus: input.runtimeStatus,
|
|
61
|
+
attentionReason: input.attentionReason,
|
|
62
|
+
telemetry:
|
|
63
|
+
telemetry === null
|
|
64
|
+
? null
|
|
65
|
+
: {
|
|
66
|
+
source: telemetry.source,
|
|
67
|
+
eventName: telemetry.eventName,
|
|
68
|
+
severity: telemetry.severity,
|
|
69
|
+
summary: telemetry.summary,
|
|
70
|
+
observedAt: telemetry.observedAt,
|
|
71
|
+
},
|
|
72
|
+
observedAt: input.observedAt,
|
|
73
|
+
previous: input.previous,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { connect, type Socket } from 'node:net';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { recordPerfEvent, startPerfSpan } from '../perf/perf-core.ts';
|
|
4
|
+
import {
|
|
5
|
+
consumeJsonLines,
|
|
6
|
+
encodeStreamEnvelope,
|
|
7
|
+
parseServerEnvelope,
|
|
8
|
+
type StreamCommand,
|
|
9
|
+
type StreamCommandEnvelope,
|
|
10
|
+
type StreamServerEnvelope,
|
|
11
|
+
type StreamSignal,
|
|
12
|
+
} from './stream-protocol.ts';
|
|
13
|
+
|
|
14
|
+
interface ControlPlaneStreamClientOptions {
|
|
15
|
+
host: string;
|
|
16
|
+
port: number;
|
|
17
|
+
authToken?: string;
|
|
18
|
+
connectRetryWindowMs?: number;
|
|
19
|
+
connectRetryDelayMs?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface PendingCommand {
|
|
23
|
+
readonly type: string;
|
|
24
|
+
readonly span: ReturnType<typeof startPerfSpan>;
|
|
25
|
+
resolve: (result: Record<string, unknown>) => void;
|
|
26
|
+
reject: (error: Error) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class ControlPlaneStreamClient {
|
|
30
|
+
private readonly socket: Socket;
|
|
31
|
+
private readonly listeners = new Set<(envelope: StreamServerEnvelope) => void>();
|
|
32
|
+
private readonly pending = new Map<string, PendingCommand>();
|
|
33
|
+
private remainder = '';
|
|
34
|
+
private pendingAuth: {
|
|
35
|
+
resolve: () => void;
|
|
36
|
+
reject: (error: Error) => void;
|
|
37
|
+
} | null = null;
|
|
38
|
+
private closed = false;
|
|
39
|
+
|
|
40
|
+
constructor(socket: Socket) {
|
|
41
|
+
this.socket = socket;
|
|
42
|
+
|
|
43
|
+
socket.on('data', (chunk: Buffer) => {
|
|
44
|
+
this.handleData(chunk);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
socket.on('close', () => {
|
|
48
|
+
this.handleClose(new Error('control-plane stream closed'));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
socket.on('error', (error: Error) => {
|
|
52
|
+
this.handleClose(error);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
onEnvelope(listener: (envelope: StreamServerEnvelope) => void): () => void {
|
|
57
|
+
this.listeners.add(listener);
|
|
58
|
+
return () => {
|
|
59
|
+
this.listeners.delete(listener);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
sendCommand(command: StreamCommand): Promise<Record<string, unknown>> {
|
|
64
|
+
const commandId = `command-${randomUUID()}`;
|
|
65
|
+
const commandType = command.type;
|
|
66
|
+
const commandSpan = startPerfSpan('control-plane.command.rtt', {
|
|
67
|
+
role: 'client',
|
|
68
|
+
type: commandType,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return new Promise<Record<string, unknown>>((resolve, reject) => {
|
|
72
|
+
if (this.closed) {
|
|
73
|
+
commandSpan.end({
|
|
74
|
+
type: commandType,
|
|
75
|
+
status: 'client-closed',
|
|
76
|
+
});
|
|
77
|
+
reject(new Error('control-plane stream is closed'));
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this.pending.set(commandId, {
|
|
82
|
+
type: commandType,
|
|
83
|
+
span: commandSpan,
|
|
84
|
+
resolve,
|
|
85
|
+
reject,
|
|
86
|
+
});
|
|
87
|
+
recordPerfEvent('control-plane.command.sent', {
|
|
88
|
+
role: 'client',
|
|
89
|
+
type: commandType,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const envelope: StreamCommandEnvelope = {
|
|
93
|
+
kind: 'command',
|
|
94
|
+
commandId,
|
|
95
|
+
command,
|
|
96
|
+
};
|
|
97
|
+
this.socket.write(encodeStreamEnvelope(envelope));
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
authenticate(token: string): Promise<void> {
|
|
102
|
+
const authSpan = startPerfSpan('control-plane.auth.rtt', {
|
|
103
|
+
role: 'client',
|
|
104
|
+
});
|
|
105
|
+
return new Promise<void>((resolve, reject) => {
|
|
106
|
+
if (this.closed) {
|
|
107
|
+
authSpan.end({
|
|
108
|
+
status: 'client-closed',
|
|
109
|
+
});
|
|
110
|
+
reject(new Error('control-plane stream is closed'));
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (this.pendingAuth !== null) {
|
|
114
|
+
authSpan.end({
|
|
115
|
+
status: 'already-pending',
|
|
116
|
+
});
|
|
117
|
+
reject(new Error('auth is already pending'));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
this.pendingAuth = {
|
|
122
|
+
resolve: () => {
|
|
123
|
+
authSpan.end({
|
|
124
|
+
status: 'ok',
|
|
125
|
+
});
|
|
126
|
+
resolve();
|
|
127
|
+
},
|
|
128
|
+
reject: (error: Error) => {
|
|
129
|
+
authSpan.end({
|
|
130
|
+
status: 'error',
|
|
131
|
+
message: error.message,
|
|
132
|
+
});
|
|
133
|
+
reject(error);
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
this.socket.write(
|
|
137
|
+
encodeStreamEnvelope({
|
|
138
|
+
kind: 'auth',
|
|
139
|
+
token,
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
sendInput(sessionId: string, data: Buffer): void {
|
|
146
|
+
if (this.closed) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
this.socket.write(
|
|
151
|
+
encodeStreamEnvelope({
|
|
152
|
+
kind: 'pty.input',
|
|
153
|
+
sessionId,
|
|
154
|
+
dataBase64: data.toString('base64'),
|
|
155
|
+
}),
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
sendResize(sessionId: string, cols: number, rows: number): void {
|
|
160
|
+
if (this.closed) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
this.socket.write(
|
|
165
|
+
encodeStreamEnvelope({
|
|
166
|
+
kind: 'pty.resize',
|
|
167
|
+
sessionId,
|
|
168
|
+
cols,
|
|
169
|
+
rows,
|
|
170
|
+
}),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
sendSignal(sessionId: string, signal: StreamSignal): void {
|
|
175
|
+
if (this.closed) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
this.socket.write(
|
|
180
|
+
encodeStreamEnvelope({
|
|
181
|
+
kind: 'pty.signal',
|
|
182
|
+
sessionId,
|
|
183
|
+
signal,
|
|
184
|
+
}),
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
close(): void {
|
|
189
|
+
if (this.closed) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
this.closed = true;
|
|
193
|
+
this.socket.end();
|
|
194
|
+
this.rejectPending(new Error('control-plane stream closed'));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private handleData(chunk: Buffer): void {
|
|
198
|
+
const consumed = consumeJsonLines(`${this.remainder}${chunk.toString('utf8')}`);
|
|
199
|
+
this.remainder = consumed.remainder;
|
|
200
|
+
|
|
201
|
+
for (const message of consumed.messages) {
|
|
202
|
+
const envelope = parseServerEnvelope(message);
|
|
203
|
+
if (envelope === null) {
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
this.handleEnvelope(envelope);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private handleEnvelope(envelope: StreamServerEnvelope): void {
|
|
211
|
+
if (envelope.kind === 'auth.ok') {
|
|
212
|
+
const pendingAuth = this.pendingAuth;
|
|
213
|
+
if (pendingAuth !== null) {
|
|
214
|
+
this.pendingAuth = null;
|
|
215
|
+
pendingAuth.resolve();
|
|
216
|
+
}
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (envelope.kind === 'auth.error') {
|
|
221
|
+
const pendingAuth = this.pendingAuth;
|
|
222
|
+
if (pendingAuth !== null) {
|
|
223
|
+
this.pendingAuth = null;
|
|
224
|
+
pendingAuth.reject(new Error(envelope.error));
|
|
225
|
+
}
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (envelope.kind === 'command.accepted') {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (envelope.kind === 'command.completed') {
|
|
234
|
+
const pending = this.pending.get(envelope.commandId);
|
|
235
|
+
if (pending === undefined) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
this.pending.delete(envelope.commandId);
|
|
239
|
+
pending.span.end({
|
|
240
|
+
type: pending.type,
|
|
241
|
+
status: 'completed',
|
|
242
|
+
});
|
|
243
|
+
pending.resolve(envelope.result);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (envelope.kind === 'command.failed') {
|
|
248
|
+
const pending = this.pending.get(envelope.commandId);
|
|
249
|
+
if (pending === undefined) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
this.pending.delete(envelope.commandId);
|
|
253
|
+
pending.span.end({
|
|
254
|
+
type: pending.type,
|
|
255
|
+
status: 'failed',
|
|
256
|
+
message: envelope.error,
|
|
257
|
+
});
|
|
258
|
+
pending.reject(new Error(envelope.error));
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
for (const listener of this.listeners) {
|
|
263
|
+
listener(envelope);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private handleClose(error: Error): void {
|
|
268
|
+
if (this.closed) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
this.closed = true;
|
|
272
|
+
this.rejectPending(error);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private rejectPending(error: Error): void {
|
|
276
|
+
const pendingAuth = this.pendingAuth;
|
|
277
|
+
if (pendingAuth !== null) {
|
|
278
|
+
this.pendingAuth = null;
|
|
279
|
+
pendingAuth.reject(error);
|
|
280
|
+
}
|
|
281
|
+
for (const pending of this.pending.values()) {
|
|
282
|
+
pending.span.end({
|
|
283
|
+
type: pending.type,
|
|
284
|
+
status: 'closed',
|
|
285
|
+
message: error.message,
|
|
286
|
+
});
|
|
287
|
+
pending.reject(error);
|
|
288
|
+
}
|
|
289
|
+
this.pending.clear();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export async function connectControlPlaneStreamClient(
|
|
294
|
+
options: ControlPlaneStreamClientOptions,
|
|
295
|
+
): Promise<ControlPlaneStreamClient> {
|
|
296
|
+
const retryWindowMs = Math.max(0, options.connectRetryWindowMs ?? 0);
|
|
297
|
+
const retryDelayMs = Math.max(1, options.connectRetryDelayMs ?? 50);
|
|
298
|
+
const retryableCodes = new Set([
|
|
299
|
+
'ECONNREFUSED',
|
|
300
|
+
'ECONNRESET',
|
|
301
|
+
'EHOSTUNREACH',
|
|
302
|
+
'ENETUNREACH',
|
|
303
|
+
'ENOTFOUND',
|
|
304
|
+
'ETIMEDOUT',
|
|
305
|
+
]);
|
|
306
|
+
const startedAtMs = Date.now();
|
|
307
|
+
recordPerfEvent('control-plane.connect.begin', {
|
|
308
|
+
role: 'client',
|
|
309
|
+
host: options.host,
|
|
310
|
+
port: options.port,
|
|
311
|
+
retryWindowMs,
|
|
312
|
+
retryDelayMs,
|
|
313
|
+
});
|
|
314
|
+
let attempts = 0;
|
|
315
|
+
let socket: Socket | null = null;
|
|
316
|
+
while (socket === null) {
|
|
317
|
+
attempts += 1;
|
|
318
|
+
const attemptSpan = startPerfSpan('control-plane.connect.attempt', {
|
|
319
|
+
role: 'client',
|
|
320
|
+
attempt: attempts,
|
|
321
|
+
host: options.host,
|
|
322
|
+
port: options.port,
|
|
323
|
+
});
|
|
324
|
+
try {
|
|
325
|
+
socket = await new Promise<Socket>((resolve, reject) => {
|
|
326
|
+
const client = connect(options.port, options.host);
|
|
327
|
+
const onError = (error: Error): void => {
|
|
328
|
+
client.off('connect', onConnect);
|
|
329
|
+
reject(error);
|
|
330
|
+
};
|
|
331
|
+
const onConnect = (): void => {
|
|
332
|
+
client.off('error', onError);
|
|
333
|
+
resolve(client);
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
client.once('error', onError);
|
|
337
|
+
client.once('connect', onConnect);
|
|
338
|
+
});
|
|
339
|
+
attemptSpan.end({
|
|
340
|
+
attempt: attempts,
|
|
341
|
+
status: 'connected',
|
|
342
|
+
});
|
|
343
|
+
} catch (error: unknown) {
|
|
344
|
+
const code = (error as NodeJS.ErrnoException | null)?.code;
|
|
345
|
+
const elapsedMs = Date.now() - startedAtMs;
|
|
346
|
+
attemptSpan.end({
|
|
347
|
+
attempt: attempts,
|
|
348
|
+
status: 'error',
|
|
349
|
+
code: typeof code === 'string' ? code : 'unknown',
|
|
350
|
+
});
|
|
351
|
+
if (
|
|
352
|
+
retryWindowMs === 0 ||
|
|
353
|
+
typeof code !== 'string' ||
|
|
354
|
+
!retryableCodes.has(code) ||
|
|
355
|
+
elapsedMs >= retryWindowMs
|
|
356
|
+
) {
|
|
357
|
+
recordPerfEvent('control-plane.connect.failed', {
|
|
358
|
+
role: 'client',
|
|
359
|
+
host: options.host,
|
|
360
|
+
port: options.port,
|
|
361
|
+
attempts,
|
|
362
|
+
elapsedMs,
|
|
363
|
+
code: typeof code === 'string' ? code : 'unknown',
|
|
364
|
+
});
|
|
365
|
+
throw error;
|
|
366
|
+
}
|
|
367
|
+
const remainingMs = retryWindowMs - elapsedMs;
|
|
368
|
+
recordPerfEvent('control-plane.connect.retrying', {
|
|
369
|
+
role: 'client',
|
|
370
|
+
host: options.host,
|
|
371
|
+
port: options.port,
|
|
372
|
+
attempts,
|
|
373
|
+
elapsedMs,
|
|
374
|
+
remainingMs,
|
|
375
|
+
code: typeof code === 'string' ? code : 'unknown',
|
|
376
|
+
});
|
|
377
|
+
await new Promise<void>((resolve) => {
|
|
378
|
+
setTimeout(resolve, Math.max(1, Math.min(retryDelayMs, remainingMs)));
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const client = new ControlPlaneStreamClient(socket);
|
|
384
|
+
if (typeof options.authToken === 'string') {
|
|
385
|
+
await client.authenticate(options.authToken);
|
|
386
|
+
}
|
|
387
|
+
recordPerfEvent('control-plane.connect.ready', {
|
|
388
|
+
role: 'client',
|
|
389
|
+
host: options.host,
|
|
390
|
+
port: options.port,
|
|
391
|
+
attempts,
|
|
392
|
+
elapsedMs: Date.now() - startedAtMs,
|
|
393
|
+
authenticated: typeof options.authToken === 'string',
|
|
394
|
+
});
|
|
395
|
+
return client;
|
|
396
|
+
}
|