@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,486 @@
|
|
|
1
|
+
import { open } from 'node:fs/promises';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { resolve } from 'node:path';
|
|
5
|
+
import { parseCodexHistoryLine, type ParsedCodexTelemetryEvent } from './codex-telemetry.ts';
|
|
6
|
+
import type {
|
|
7
|
+
ControlPlaneDirectoryRecord,
|
|
8
|
+
ControlPlaneRepositoryRecord,
|
|
9
|
+
} from '../store/control-plane-store.ts';
|
|
10
|
+
import { startPerfSpan } from '../perf/perf-core.ts';
|
|
11
|
+
|
|
12
|
+
const HISTORY_POLL_JITTER_RATIO = 0.35;
|
|
13
|
+
const HISTORY_POLL_MAX_DELAY_MS = 60_000;
|
|
14
|
+
const LINE_FEED_BYTE = '\n'.charCodeAt(0);
|
|
15
|
+
|
|
16
|
+
interface GitStatusSummary {
|
|
17
|
+
branch: string | null;
|
|
18
|
+
changedFiles: number;
|
|
19
|
+
additions: number;
|
|
20
|
+
deletions: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface GitRepositorySnapshot {
|
|
24
|
+
normalizedRemoteUrl: string | null;
|
|
25
|
+
commitCount: number | null;
|
|
26
|
+
lastCommitAt: string | null;
|
|
27
|
+
shortCommitHash: string | null;
|
|
28
|
+
inferredName: string | null;
|
|
29
|
+
defaultBranch: string | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface DirectoryGitStatusCacheEntry {
|
|
33
|
+
readonly summary: GitStatusSummary;
|
|
34
|
+
readonly repositorySnapshot: GitRepositorySnapshot;
|
|
35
|
+
readonly repositoryId: string | null;
|
|
36
|
+
readonly lastRefreshedAtMs: number;
|
|
37
|
+
readonly lastRefreshDurationMs: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface GitDirectorySnapshot {
|
|
41
|
+
summary: GitStatusSummary;
|
|
42
|
+
repository: GitRepositorySnapshot;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface BackgroundSessionState {
|
|
46
|
+
id: string;
|
|
47
|
+
adapterState: Record<string, unknown>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface HistoryFileHandle {
|
|
51
|
+
stat(): Promise<{ size: number }>;
|
|
52
|
+
read(
|
|
53
|
+
buffer: Buffer,
|
|
54
|
+
offset: number,
|
|
55
|
+
length: number,
|
|
56
|
+
position: number,
|
|
57
|
+
): Promise<{ bytesRead: number }>;
|
|
58
|
+
close(): Promise<void>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface BackgroundContext {
|
|
62
|
+
historyNextAllowedPollAtMs: number;
|
|
63
|
+
historyPollInFlight: boolean;
|
|
64
|
+
historyIdleStreak: number;
|
|
65
|
+
historyOffset: number;
|
|
66
|
+
historyRemainder: string;
|
|
67
|
+
readonly codexHistory: {
|
|
68
|
+
enabled: boolean;
|
|
69
|
+
filePath: string;
|
|
70
|
+
pollMs: number;
|
|
71
|
+
};
|
|
72
|
+
openHistoryFile?: (path: string, flags: 'r') => Promise<HistoryFileHandle>;
|
|
73
|
+
readonly sessions: Map<string, BackgroundSessionState>;
|
|
74
|
+
readonly stateStore: {
|
|
75
|
+
findConversationIdByCodexThreadId(threadId: string): string | null;
|
|
76
|
+
upsertRepository(input: {
|
|
77
|
+
repositoryId: string;
|
|
78
|
+
tenantId: string;
|
|
79
|
+
userId: string;
|
|
80
|
+
workspaceId: string;
|
|
81
|
+
name: string;
|
|
82
|
+
remoteUrl: string;
|
|
83
|
+
defaultBranch?: string;
|
|
84
|
+
metadata?: Record<string, unknown>;
|
|
85
|
+
}): ControlPlaneRepositoryRecord;
|
|
86
|
+
getRepository(repositoryId: string): ControlPlaneRepositoryRecord | null;
|
|
87
|
+
};
|
|
88
|
+
ingestParsedTelemetryEvent(sessionId: string | null, event: ParsedCodexTelemetryEvent): void;
|
|
89
|
+
resolveSessionIdByThreadId(threadId: string): string | null;
|
|
90
|
+
pollHistoryFileUnsafe?: () => Promise<boolean>;
|
|
91
|
+
gitStatusPollInFlight: boolean;
|
|
92
|
+
readonly gitStatusDirectoriesById: Map<string, ControlPlaneDirectoryRecord>;
|
|
93
|
+
readonly gitStatusByDirectoryId: Map<string, DirectoryGitStatusCacheEntry>;
|
|
94
|
+
readonly gitStatusRefreshInFlightDirectoryIds: Set<string>;
|
|
95
|
+
readonly gitStatusMonitor: {
|
|
96
|
+
maxConcurrency: number;
|
|
97
|
+
minDirectoryRefreshMs: number;
|
|
98
|
+
};
|
|
99
|
+
readonly readGitDirectorySnapshot: (cwd: string) => Promise<GitDirectorySnapshot>;
|
|
100
|
+
repositoryRecord(repository: ControlPlaneRepositoryRecord): Record<string, unknown>;
|
|
101
|
+
publishObservedEvent(
|
|
102
|
+
scope: {
|
|
103
|
+
tenantId: string;
|
|
104
|
+
userId: string;
|
|
105
|
+
workspaceId: string;
|
|
106
|
+
directoryId: string | null;
|
|
107
|
+
conversationId: string | null;
|
|
108
|
+
},
|
|
109
|
+
event: {
|
|
110
|
+
type: 'directory-git-updated';
|
|
111
|
+
directoryId: string;
|
|
112
|
+
summary: GitStatusSummary;
|
|
113
|
+
repositorySnapshot: GitRepositorySnapshot;
|
|
114
|
+
repositoryId: string | null;
|
|
115
|
+
repository: Record<string, unknown> | null;
|
|
116
|
+
observedAt: string;
|
|
117
|
+
},
|
|
118
|
+
): void;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function jitterDelayMs(baseMs: number): number {
|
|
122
|
+
const clampedBaseMs = Math.max(25, Math.floor(baseMs));
|
|
123
|
+
const jitterWindowMs = Math.max(1, Math.floor(clampedBaseMs * HISTORY_POLL_JITTER_RATIO));
|
|
124
|
+
const jitterOffsetMs = Math.floor(Math.random() * (2 * jitterWindowMs + 1) - jitterWindowMs);
|
|
125
|
+
return Math.max(25, clampedBaseMs + jitterOffsetMs);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function expandTildePath(pathValue: string): string {
|
|
129
|
+
if (pathValue === '~') {
|
|
130
|
+
return homedir();
|
|
131
|
+
}
|
|
132
|
+
if (pathValue.startsWith('~/')) {
|
|
133
|
+
return resolve(homedir(), pathValue.slice(2));
|
|
134
|
+
}
|
|
135
|
+
return pathValue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function gitSummaryEqual(left: GitStatusSummary, right: GitStatusSummary): boolean {
|
|
139
|
+
return (
|
|
140
|
+
left.branch === right.branch &&
|
|
141
|
+
left.changedFiles === right.changedFiles &&
|
|
142
|
+
left.additions === right.additions &&
|
|
143
|
+
left.deletions === right.deletions
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function gitRepositorySnapshotEqual(
|
|
148
|
+
left: GitRepositorySnapshot,
|
|
149
|
+
right: GitRepositorySnapshot,
|
|
150
|
+
): boolean {
|
|
151
|
+
return (
|
|
152
|
+
left.normalizedRemoteUrl === right.normalizedRemoteUrl &&
|
|
153
|
+
left.commitCount === right.commitCount &&
|
|
154
|
+
left.lastCommitAt === right.lastCommitAt &&
|
|
155
|
+
left.shortCommitHash === right.shortCommitHash &&
|
|
156
|
+
left.inferredName === right.inferredName &&
|
|
157
|
+
left.defaultBranch === right.defaultBranch
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function runWithConcurrencyLimit<T>(
|
|
162
|
+
values: readonly T[],
|
|
163
|
+
concurrency: number,
|
|
164
|
+
worker: (value: T) => Promise<void>,
|
|
165
|
+
): Promise<void> {
|
|
166
|
+
if (values.length === 0) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const workerCount = Math.min(values.length, Math.max(1, Math.floor(concurrency)));
|
|
170
|
+
let index = 0;
|
|
171
|
+
const runners: Promise<void>[] = [];
|
|
172
|
+
for (let workerIndex = 0; workerIndex < workerCount; workerIndex += 1) {
|
|
173
|
+
runners.push(
|
|
174
|
+
(async () => {
|
|
175
|
+
while (true) {
|
|
176
|
+
const nextIndex = index;
|
|
177
|
+
index += 1;
|
|
178
|
+
if (nextIndex >= values.length) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const value = values[nextIndex];
|
|
182
|
+
if (value === undefined) {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
await worker(value);
|
|
186
|
+
}
|
|
187
|
+
})(),
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
await Promise.all(runners);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export async function pollHistoryFile(ctx: BackgroundContext): Promise<void> {
|
|
194
|
+
const nowMs = Date.now();
|
|
195
|
+
if (nowMs < ctx.historyNextAllowedPollAtMs) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (ctx.historyPollInFlight) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
ctx.historyPollInFlight = true;
|
|
202
|
+
try {
|
|
203
|
+
const consumedNewBytes =
|
|
204
|
+
typeof ctx.pollHistoryFileUnsafe === 'function'
|
|
205
|
+
? await ctx.pollHistoryFileUnsafe()
|
|
206
|
+
: await pollHistoryFileUnsafe(ctx);
|
|
207
|
+
if (consumedNewBytes) {
|
|
208
|
+
ctx.historyIdleStreak = 0;
|
|
209
|
+
ctx.historyNextAllowedPollAtMs = Date.now() + jitterDelayMs(ctx.codexHistory.pollMs);
|
|
210
|
+
} else {
|
|
211
|
+
ctx.historyIdleStreak = Math.min(ctx.historyIdleStreak + 1, 4);
|
|
212
|
+
const backoffMs = Math.min(
|
|
213
|
+
HISTORY_POLL_MAX_DELAY_MS,
|
|
214
|
+
ctx.codexHistory.pollMs * (1 << ctx.historyIdleStreak),
|
|
215
|
+
);
|
|
216
|
+
ctx.historyNextAllowedPollAtMs = Date.now() + jitterDelayMs(backoffMs);
|
|
217
|
+
}
|
|
218
|
+
} catch {
|
|
219
|
+
ctx.historyIdleStreak = Math.min(ctx.historyIdleStreak + 1, 4);
|
|
220
|
+
const backoffMs = Math.min(
|
|
221
|
+
HISTORY_POLL_MAX_DELAY_MS,
|
|
222
|
+
ctx.codexHistory.pollMs * (1 << ctx.historyIdleStreak),
|
|
223
|
+
);
|
|
224
|
+
ctx.historyNextAllowedPollAtMs = Date.now() + jitterDelayMs(backoffMs);
|
|
225
|
+
} finally {
|
|
226
|
+
ctx.historyPollInFlight = false;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export async function pollHistoryFileUnsafe(ctx: BackgroundContext): Promise<boolean> {
|
|
231
|
+
if (!ctx.codexHistory.enabled) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
const resolvedHistoryPath = expandTildePath(ctx.codexHistory.filePath);
|
|
235
|
+
const openHistoryFile: NonNullable<BackgroundContext['openHistoryFile']> =
|
|
236
|
+
ctx.openHistoryFile ?? (open as unknown as NonNullable<BackgroundContext['openHistoryFile']>);
|
|
237
|
+
let handle: HistoryFileHandle;
|
|
238
|
+
try {
|
|
239
|
+
handle = await openHistoryFile(resolvedHistoryPath, 'r');
|
|
240
|
+
} catch (error) {
|
|
241
|
+
const errorWithCode = error as { code?: unknown };
|
|
242
|
+
if (errorWithCode.code === 'ENOENT') {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
throw error;
|
|
246
|
+
}
|
|
247
|
+
let delta = '';
|
|
248
|
+
let fileTruncated = false;
|
|
249
|
+
try {
|
|
250
|
+
const stats = await handle.stat();
|
|
251
|
+
const fileSize = Number(stats.size);
|
|
252
|
+
if (!Number.isFinite(fileSize)) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
if (fileSize < ctx.historyOffset) {
|
|
256
|
+
fileTruncated = true;
|
|
257
|
+
ctx.historyOffset = 0;
|
|
258
|
+
ctx.historyRemainder = '';
|
|
259
|
+
}
|
|
260
|
+
if (
|
|
261
|
+
!fileTruncated &&
|
|
262
|
+
ctx.historyOffset > 0 &&
|
|
263
|
+
ctx.historyRemainder.length === 0 &&
|
|
264
|
+
fileSize >= ctx.historyOffset
|
|
265
|
+
) {
|
|
266
|
+
const probe = Buffer.allocUnsafe(1);
|
|
267
|
+
const { bytesRead: probeBytesRead } = await handle.read(probe, 0, 1, ctx.historyOffset - 1);
|
|
268
|
+
const historyBoundaryMatches = probeBytesRead === 1 && probe[0] === LINE_FEED_BYTE;
|
|
269
|
+
if (!historyBoundaryMatches) {
|
|
270
|
+
fileTruncated = true;
|
|
271
|
+
ctx.historyOffset = 0;
|
|
272
|
+
ctx.historyRemainder = '';
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const remainingBytes = fileSize - ctx.historyOffset;
|
|
276
|
+
if (remainingBytes <= 0) {
|
|
277
|
+
return fileTruncated;
|
|
278
|
+
}
|
|
279
|
+
const buffer = Buffer.allocUnsafe(remainingBytes);
|
|
280
|
+
let bytesReadTotal = 0;
|
|
281
|
+
while (bytesReadTotal < remainingBytes) {
|
|
282
|
+
const { bytesRead } = await handle.read(
|
|
283
|
+
buffer,
|
|
284
|
+
bytesReadTotal,
|
|
285
|
+
remainingBytes - bytesReadTotal,
|
|
286
|
+
ctx.historyOffset + bytesReadTotal,
|
|
287
|
+
);
|
|
288
|
+
if (bytesRead <= 0) {
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
bytesReadTotal += bytesRead;
|
|
292
|
+
}
|
|
293
|
+
if (bytesReadTotal <= 0) {
|
|
294
|
+
return fileTruncated;
|
|
295
|
+
}
|
|
296
|
+
ctx.historyOffset += bytesReadTotal;
|
|
297
|
+
delta = buffer.toString('utf8', 0, bytesReadTotal);
|
|
298
|
+
} finally {
|
|
299
|
+
await handle.close();
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const buffered = `${ctx.historyRemainder}${delta}`;
|
|
303
|
+
const lines = buffered.split('\n');
|
|
304
|
+
ctx.historyRemainder = lines.pop() as string;
|
|
305
|
+
for (const line of lines) {
|
|
306
|
+
const trimmed = line.trim();
|
|
307
|
+
if (trimmed.length === 0) {
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
const parsed = parseCodexHistoryLine(trimmed, new Date().toISOString());
|
|
311
|
+
if (parsed === null) {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
const sessionId =
|
|
315
|
+
parsed.providerThreadId === null
|
|
316
|
+
? null
|
|
317
|
+
: ctx.resolveSessionIdByThreadId(parsed.providerThreadId);
|
|
318
|
+
ctx.ingestParsedTelemetryEvent(sessionId, parsed);
|
|
319
|
+
}
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export async function pollGitStatus(ctx: BackgroundContext): Promise<void> {
|
|
324
|
+
if (ctx.gitStatusPollInFlight) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
ctx.gitStatusPollInFlight = true;
|
|
328
|
+
try {
|
|
329
|
+
const directories = [...ctx.gitStatusDirectoriesById.values()];
|
|
330
|
+
if (directories.length === 0) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
const nowMs = Date.now();
|
|
334
|
+
const dueDirectories = directories.filter((directory) => {
|
|
335
|
+
const previous = ctx.gitStatusByDirectoryId.get(directory.directoryId);
|
|
336
|
+
if (previous === undefined) {
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
const minRefreshWindowMs = Math.max(
|
|
340
|
+
ctx.gitStatusMonitor.minDirectoryRefreshMs,
|
|
341
|
+
Math.min(10 * 60 * 1000, Math.max(1000, previous.lastRefreshDurationMs * 4)),
|
|
342
|
+
);
|
|
343
|
+
return nowMs - previous.lastRefreshedAtMs >= minRefreshWindowMs;
|
|
344
|
+
});
|
|
345
|
+
await runWithConcurrencyLimit(
|
|
346
|
+
dueDirectories,
|
|
347
|
+
ctx.gitStatusMonitor.maxConcurrency,
|
|
348
|
+
async (directory) => await refreshGitStatusForDirectory(ctx, directory),
|
|
349
|
+
);
|
|
350
|
+
} finally {
|
|
351
|
+
ctx.gitStatusPollInFlight = false;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export async function refreshGitStatusForDirectory(
|
|
356
|
+
ctx: BackgroundContext,
|
|
357
|
+
directory: ControlPlaneDirectoryRecord,
|
|
358
|
+
options: {
|
|
359
|
+
readonly forcePublish?: boolean;
|
|
360
|
+
} = {},
|
|
361
|
+
): Promise<void> {
|
|
362
|
+
if (ctx.gitStatusRefreshInFlightDirectoryIds.has(directory.directoryId)) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
ctx.gitStatusRefreshInFlightDirectoryIds.add(directory.directoryId);
|
|
366
|
+
const gitSpan = startPerfSpan('control-plane.background.git-status', {
|
|
367
|
+
directoryId: directory.directoryId,
|
|
368
|
+
});
|
|
369
|
+
const startedAtMs = Date.now();
|
|
370
|
+
const previous = ctx.gitStatusByDirectoryId.get(directory.directoryId) ?? null;
|
|
371
|
+
try {
|
|
372
|
+
const snapshot = await ctx.readGitDirectorySnapshot(directory.path);
|
|
373
|
+
let repositorySnapshot: GitRepositorySnapshot = snapshot.repository;
|
|
374
|
+
if (
|
|
375
|
+
repositorySnapshot.commitCount === null &&
|
|
376
|
+
previous !== null &&
|
|
377
|
+
previous.repositorySnapshot.normalizedRemoteUrl === repositorySnapshot.normalizedRemoteUrl &&
|
|
378
|
+
previous.repositorySnapshot.shortCommitHash === repositorySnapshot.shortCommitHash
|
|
379
|
+
) {
|
|
380
|
+
repositorySnapshot = {
|
|
381
|
+
...repositorySnapshot,
|
|
382
|
+
commitCount: previous.repositorySnapshot.commitCount,
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
let repositoryId: string | null = null;
|
|
386
|
+
let repositoryRecord: Record<string, unknown> | null = null;
|
|
387
|
+
if (repositorySnapshot.normalizedRemoteUrl !== null) {
|
|
388
|
+
if (
|
|
389
|
+
previous !== null &&
|
|
390
|
+
previous.repositoryId !== null &&
|
|
391
|
+
previous.repositorySnapshot.normalizedRemoteUrl === repositorySnapshot.normalizedRemoteUrl
|
|
392
|
+
) {
|
|
393
|
+
repositoryId = previous.repositoryId;
|
|
394
|
+
} else {
|
|
395
|
+
const upserted = ctx.stateStore.upsertRepository({
|
|
396
|
+
repositoryId: `repository-${randomUUID()}`,
|
|
397
|
+
tenantId: directory.tenantId,
|
|
398
|
+
userId: directory.userId,
|
|
399
|
+
workspaceId: directory.workspaceId,
|
|
400
|
+
name: repositorySnapshot.inferredName ?? 'repository',
|
|
401
|
+
remoteUrl: repositorySnapshot.normalizedRemoteUrl,
|
|
402
|
+
defaultBranch: repositorySnapshot.defaultBranch ?? 'main',
|
|
403
|
+
metadata: {
|
|
404
|
+
source: 'control-plane-git-status',
|
|
405
|
+
},
|
|
406
|
+
});
|
|
407
|
+
repositoryId = upserted.repositoryId;
|
|
408
|
+
repositoryRecord = ctx.repositoryRecord(upserted);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
const next: DirectoryGitStatusCacheEntry = {
|
|
412
|
+
summary: snapshot.summary,
|
|
413
|
+
repositorySnapshot,
|
|
414
|
+
repositoryId,
|
|
415
|
+
lastRefreshedAtMs: Date.now(),
|
|
416
|
+
lastRefreshDurationMs: Math.max(1, Date.now() - startedAtMs),
|
|
417
|
+
};
|
|
418
|
+
ctx.gitStatusByDirectoryId.set(directory.directoryId, next);
|
|
419
|
+
const changed =
|
|
420
|
+
previous === null ||
|
|
421
|
+
!gitSummaryEqual(previous.summary, next.summary) ||
|
|
422
|
+
!gitRepositorySnapshotEqual(previous.repositorySnapshot, next.repositorySnapshot) ||
|
|
423
|
+
previous.repositoryId !== next.repositoryId;
|
|
424
|
+
const shouldPublish = changed || options.forcePublish === true;
|
|
425
|
+
if (shouldPublish) {
|
|
426
|
+
if (repositoryRecord === null && repositoryId !== null) {
|
|
427
|
+
const existingRepository = ctx.stateStore.getRepository(repositoryId);
|
|
428
|
+
if (existingRepository !== null) {
|
|
429
|
+
repositoryRecord = ctx.repositoryRecord(existingRepository);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
ctx.publishObservedEvent(
|
|
433
|
+
{
|
|
434
|
+
tenantId: directory.tenantId,
|
|
435
|
+
userId: directory.userId,
|
|
436
|
+
workspaceId: directory.workspaceId,
|
|
437
|
+
directoryId: directory.directoryId,
|
|
438
|
+
conversationId: null,
|
|
439
|
+
},
|
|
440
|
+
{
|
|
441
|
+
type: 'directory-git-updated',
|
|
442
|
+
directoryId: directory.directoryId,
|
|
443
|
+
summary: {
|
|
444
|
+
branch: snapshot.summary.branch,
|
|
445
|
+
changedFiles: snapshot.summary.changedFiles,
|
|
446
|
+
additions: snapshot.summary.additions,
|
|
447
|
+
deletions: snapshot.summary.deletions,
|
|
448
|
+
},
|
|
449
|
+
repositorySnapshot: {
|
|
450
|
+
normalizedRemoteUrl: repositorySnapshot.normalizedRemoteUrl,
|
|
451
|
+
commitCount: repositorySnapshot.commitCount,
|
|
452
|
+
lastCommitAt: repositorySnapshot.lastCommitAt,
|
|
453
|
+
shortCommitHash: repositorySnapshot.shortCommitHash,
|
|
454
|
+
inferredName: repositorySnapshot.inferredName,
|
|
455
|
+
defaultBranch: repositorySnapshot.defaultBranch,
|
|
456
|
+
},
|
|
457
|
+
repositoryId,
|
|
458
|
+
repository: repositoryRecord,
|
|
459
|
+
observedAt: new Date().toISOString(),
|
|
460
|
+
},
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
gitSpan.end({
|
|
464
|
+
directoryId: directory.directoryId,
|
|
465
|
+
changed,
|
|
466
|
+
published: shouldPublish ? 1 : 0,
|
|
467
|
+
forcePublished: options.forcePublish ? 1 : 0,
|
|
468
|
+
repositoryLinked: repositoryId === null ? 0 : 1,
|
|
469
|
+
});
|
|
470
|
+
} catch {
|
|
471
|
+
if (previous !== null) {
|
|
472
|
+
ctx.gitStatusByDirectoryId.set(directory.directoryId, {
|
|
473
|
+
...previous,
|
|
474
|
+
lastRefreshedAtMs: Date.now(),
|
|
475
|
+
lastRefreshDurationMs: Math.max(1, Date.now() - startedAtMs),
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
gitSpan.end({
|
|
479
|
+
directoryId: directory.directoryId,
|
|
480
|
+
changed: false,
|
|
481
|
+
failed: true,
|
|
482
|
+
});
|
|
483
|
+
} finally {
|
|
484
|
+
ctx.gitStatusRefreshInFlightDirectoryIds.delete(directory.directoryId);
|
|
485
|
+
}
|
|
486
|
+
}
|