-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop-manager.ts
More file actions
425 lines (359 loc) · 12.7 KB
/
loop-manager.ts
File metadata and controls
425 lines (359 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
export type LoopSource = "tool" | "discovered";
export interface TrackedLoop {
id: string;
pid: number | null;
// Best-effort paths. Ralph's JSON schema has changed across versions.
directory: string | null;
worktree: string | null;
status: string; // running/completed/needs-review/... plus any unknown states
iteration: number;
maxIterations: number;
hat: string | null;
elapsedSecs: number;
backend: string | null;
source: LoopSource;
// Step 4/5 will populate this for tool-started loops.
ptySession: unknown | null;
// If a loop disappears from polling results, we keep it but mark it removed.
removed: boolean;
lastSeenAt: number;
}
/**
* LoopManager is the core state holder for pi-ralph.
*
* Step 2 responsibilities:
* - Poll `ralph loops list --json`
* - Maintain a merged list of TrackedLoop objects
* - Track a focused loop index (for widget/overlay cycling)
*/
export class LoopManager {
private readonly pi: ExtensionAPI;
private loops: TrackedLoop[] = [];
private focusedIndex = 0;
private pollTimer: ReturnType<typeof setInterval> | null = null;
private pollInFlight = false;
private changeListeners = new Set<() => void>();
// Diagnostics
public consecutivePollFailures = 0;
public lastPollError: string | null = null;
constructor(pi: ExtensionAPI) {
this.pi = pi;
}
onChange(listener: () => void): () => void {
this.changeListeners.add(listener);
return () => this.changeListeners.delete(listener);
}
private emitChange(): void {
for (const l of this.changeListeners) l();
}
getLoops(options?: { includeRemoved?: boolean }): TrackedLoop[] {
const includeRemoved = options?.includeRemoved ?? false;
return includeRemoved ? [...this.loops] : this.loops.filter((l) => !l.removed);
}
getFocused(): TrackedLoop | null {
const active = this.getLoops();
if (active.length === 0) return null;
// Clamp focused index against active list.
const idx = Math.min(Math.max(this.focusedIndex, 0), active.length - 1);
this.focusedIndex = idx;
return active[idx] ?? null;
}
cycleFocus(direction: 1 | -1 = 1): TrackedLoop | null {
const active = this.getLoops();
if (active.length === 0) {
this.focusedIndex = 0;
this.emitChange();
return null;
}
const n = active.length;
this.focusedIndex = (this.focusedIndex + direction + n) % n;
this.emitChange();
return active[this.focusedIndex] ?? null;
}
async poll(signal?: AbortSignal): Promise<TrackedLoop[] | null> {
if (this.pollInFlight) return null;
this.pollInFlight = true;
try {
// Collect unique project directories from known loops.
// `ralph loops list` must be run from a directory with ralph.yml.
const dirs = new Set<string>();
for (const loop of this.loops) {
if (loop.directory) dirs.add(loop.directory);
}
// Fallback: try cwd if no directories known.
if (dirs.size === 0) dirs.add(process.cwd());
// Poll each directory and merge all results.
let allParsed: TrackedLoop[] = [];
let anySuccess = false;
for (const dir of dirs) {
const result = await this.pi.exec("ralph", ["loops", "list", "--json"], {
timeout: 5000,
signal,
cwd: dir,
});
if (result.code === 0) {
anySuccess = true;
const stdout = (result.stdout ?? "").trim();
try {
const parsed = this.parseLoopsJson(stdout);
// Tag discovered loops with the project directory they were found in.
// `ralph loops list` doesn't include absolute paths, so we set directory
// from the cwd used for polling.
for (const loop of parsed) {
if (!loop.directory) loop.directory = dir;
}
allParsed = allParsed.concat(parsed);
} catch {
// ignore parse errors for this directory
}
}
}
// Deduplicate by ID (same loop might appear from different dirs).
const byId = new Map<string, TrackedLoop>();
for (const loop of allParsed) byId.set(loop.id, loop);
allParsed = Array.from(byId.values());
const result = anySuccess
? { code: 0, stdout: "", stderr: "" }
: { code: 1, stdout: "", stderr: "all poll attempts failed" };
if (!anySuccess) {
this.consecutivePollFailures++;
this.lastPollError = "all poll attempts failed";
return null;
}
this.mergePolled(allParsed);
this.consecutivePollFailures = 0;
this.lastPollError = null;
return this.getLoops();
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
this.consecutivePollFailures++;
this.lastPollError = msg;
return null;
} finally {
this.pollInFlight = false;
}
}
startPolling(intervalMs = 5000): void {
if (this.pollTimer) return;
// Fire once immediately.
void this.poll();
this.pollTimer = setInterval(() => {
void this.poll();
}, intervalMs);
}
stopPolling(): void {
if (this.pollTimer) {
clearInterval(this.pollTimer);
this.pollTimer = null;
}
}
/**
* Add or update a loop that was started by the tool.
* Polling may later fill in additional fields.
*/
upsertToolLoop(loop: {
id: string;
pid: number;
directory: string;
worktree?: string | null;
command?: string;
ptySession: unknown;
}): void {
const now = Date.now();
const existingIdx = this.loops.findIndex((l) => l.id === loop.id);
const next: TrackedLoop = {
id: loop.id,
pid: loop.pid,
directory: loop.directory,
worktree: loop.worktree ?? null,
status: "running",
iteration: 0,
maxIterations: 0,
hat: null,
elapsedSecs: 0,
backend: null,
source: "tool",
ptySession: loop.ptySession,
removed: false,
lastSeenAt: now,
};
if (existingIdx >= 0) {
this.loops[existingIdx] = {
...this.loops[existingIdx]!,
...next,
source: "tool",
ptySession: loop.ptySession,
removed: false,
lastSeenAt: now,
};
} else {
this.loops.push(next);
// If this is the first active loop, focus it.
if (this.getLoops().length === 1) this.focusedIndex = 0;
}
this.emitChange();
}
setFocusById(id: string): void {
const active = this.getLoops();
const idx = active.findIndex((l) => l.id === id);
if (idx >= 0) {
this.focusedIndex = idx;
this.emitChange();
}
}
/**
* Merge the latest polled loops into state.
*
* - Adds new loops
* - Updates existing loops
* - Marks missing loops as removed
*/
mergePolled(polled: TrackedLoop[]): void {
const now = Date.now();
const byId = new Map<string, TrackedLoop>(this.loops.map((l) => [l.id, l]));
const seen = new Set<string>();
for (const next of polled) {
seen.add(next.id);
let existing = byId.get(next.id);
// When polling returns "(primary)", try to match it to a tool-started primary
// loop in the same directory. The tool assigns IDs like "primary-20260207-..."
// from .ralph/current-loop-id, but `ralph loops list` uses "(primary)".
if (!existing && next.id === "(primary)") {
for (const [existId, existLoop] of byId) {
if (
existLoop.source === "tool" &&
existId.startsWith("primary-") &&
existLoop.directory === next.directory
) {
// Found the match. Replace the tool-assigned ID with "(primary)"
// so that ralph CLI commands (stop, merge, etc.) use the right ID.
byId.delete(existId);
existing = existLoop;
break;
}
}
}
if (!existing) {
byId.set(next.id, {
...next,
removed: false,
lastSeenAt: now,
});
continue;
}
// Preserve fields that polling can't reconstruct yet (ptySession, source when tool-started).
// Don't overwrite a known directory with display-only values like "(in-place)".
const directory = (next.directory && !next.directory.startsWith("("))
? next.directory
: existing.directory;
const worktree = next.worktree ?? existing.worktree;
byId.set(next.id, {
...existing,
...next,
directory,
worktree,
source: existing.source,
ptySession: existing.ptySession,
removed: false,
lastSeenAt: now,
});
}
// Mark missing loops as removed — but never mark tool-sourced loops as removed
// based on polling alone. Tool loops track liveness via their PTY session, not
// via `ralph loops list` which may not include them (e.g. primary loops).
for (const loop of byId.values()) {
if (!seen.has(loop.id) && loop.source !== "tool") {
loop.removed = true;
}
}
// Keep stable ordering: existing order first, then new ones.
const prevOrder = this.loops.map((l) => l.id);
const nextIds = Array.from(byId.keys());
const orderedIds: string[] = [];
for (const id of prevOrder) if (byId.has(id)) orderedIds.push(id);
for (const id of nextIds) if (!orderedIds.includes(id)) orderedIds.push(id);
this.loops = orderedIds.map((id) => byId.get(id)!).filter(Boolean);
// Clamp focus to active loops.
const active = this.getLoops();
if (active.length === 0) this.focusedIndex = 0;
else this.focusedIndex = Math.min(this.focusedIndex, active.length - 1);
this.emitChange();
}
private parseLoopsJson(stdout: string): TrackedLoop[] {
if (!stdout) return [];
let data: unknown;
try {
data = JSON.parse(stdout);
} catch {
throw new Error("Failed to parse JSON from `ralph loops list --json`");
}
if (!Array.isArray(data)) {
throw new Error("Unexpected JSON shape from `ralph loops list --json` (expected array)");
}
const now = Date.now();
return data
.map((raw: any): TrackedLoop | null => {
const id: string | undefined =
typeof raw?.loop_id === "string" ? raw.loop_id : typeof raw?.id === "string" ? raw.id : undefined;
if (!id) return null;
const status: string =
typeof raw?.state === "string"
? raw.state
: typeof raw?.status === "string"
? raw.status
: "unknown";
const pid: number | null = typeof raw?.pid === "number" ? raw.pid : null;
const worktree: string | null = typeof raw?.worktree === "string" ? raw.worktree : null;
// Ralph 2.4.4 emits `location` in JSON list output. It might be a worktree name or a path.
const location: string | null = typeof raw?.location === "string" ? raw.location : null;
// Best effort: directory is either `directory`, `path`, `workspace`, or `worktree_path`.
// Exclude display-only values like "(in-place)" or bare location names like "chipper-crane".
const isAbsPath = (s: string | null) => s != null && s.length > 0 && s.startsWith("/");
const workspace: string | null = typeof raw?.workspace === "string" ? raw.workspace : null;
const worktreePath: string | null = typeof raw?.worktree_path === "string" ? raw.worktree_path : null;
const directory: string | null =
typeof raw?.directory === "string" && isAbsPath(raw.directory)
? raw.directory
: typeof raw?.path === "string" && isAbsPath(raw.path)
? raw.path
: isAbsPath(workspace)
? workspace
: isAbsPath(worktreePath)
? worktreePath
: null;
const iteration: number = typeof raw?.iteration === "number" ? raw.iteration : 0;
const maxIterations: number =
typeof raw?.max_iterations === "number"
? raw.max_iterations
: typeof raw?.maxIterations === "number"
? raw.maxIterations
: 0;
const elapsedSecs: number =
typeof raw?.elapsed_secs === "number"
? raw.elapsed_secs
: typeof raw?.elapsedSecs === "number"
? raw.elapsedSecs
: 0;
const hat: string | null = typeof raw?.hat === "string" ? raw.hat : null;
const backend: string | null = typeof raw?.backend === "string" ? raw.backend : null;
return {
id,
pid,
directory,
worktree,
status,
iteration,
maxIterations,
hat,
elapsedSecs,
backend,
source: "discovered",
ptySession: null,
removed: false,
lastSeenAt: now,
};
})
.filter((x): x is TrackedLoop => x !== null);
}
}