@max10240/opencode-plugin-infinite-loop 0.0.0-stage → 1.0.35
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/README.md +84 -2
- package/README.zh-CN.md +76 -0
- package/dist/index.js +3 -0
- package/dist/src/controller.js +379 -0
- package/dist/src/duration.js +100 -0
- package/dist/src/index.js +277 -0
- package/dist/src/options.js +98 -0
- package/dist/src/rpc.js +118 -0
- package/dist/src/tracker.js +360 -0
- package/dist/src/tui.js +1320 -0
- package/dist/tui.js +9 -0
- package/package.json +67 -4
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { isSessionBusy } from "./controller.js";
|
|
2
|
+
const MAX_SEED_PAGES = 20;
|
|
3
|
+
const SEED_PAGE_SIZE = 200;
|
|
4
|
+
/**
|
|
5
|
+
* Event-maintained session tree. It keeps the loop's hot path free of database reads:
|
|
6
|
+
* startup seeds once, then session/shell/execution events keep the snapshot current.
|
|
7
|
+
*/
|
|
8
|
+
export class SessionTracker {
|
|
9
|
+
records = new Map();
|
|
10
|
+
children = new Map();
|
|
11
|
+
shells = new Map();
|
|
12
|
+
images = new Map();
|
|
13
|
+
seeded = false;
|
|
14
|
+
seeding;
|
|
15
|
+
async seed(context, timeoutMs = 2_000, force = false) {
|
|
16
|
+
if (this.seeding)
|
|
17
|
+
return this.seeding;
|
|
18
|
+
if (this.seeded && !force)
|
|
19
|
+
return;
|
|
20
|
+
const started = Date.now();
|
|
21
|
+
this.seeding = (async () => {
|
|
22
|
+
const list = context.session?.list;
|
|
23
|
+
if (typeof list !== "function")
|
|
24
|
+
return;
|
|
25
|
+
try {
|
|
26
|
+
const infos = await withTimeout(listAll(list), timeoutMs);
|
|
27
|
+
if (infos === undefined)
|
|
28
|
+
return;
|
|
29
|
+
const live = new Set();
|
|
30
|
+
for (const info of infos) {
|
|
31
|
+
live.add(info.id);
|
|
32
|
+
this.upsert(info);
|
|
33
|
+
}
|
|
34
|
+
// A reconnect can miss deletion events. Remove records that were not in the
|
|
35
|
+
// snapshot and were already old when the snapshot started; sessions created
|
|
36
|
+
// while the seed was in flight stay.
|
|
37
|
+
for (const record of [...this.records.values()]) {
|
|
38
|
+
if (live.has(record.id) || record.updatedAt >= started)
|
|
39
|
+
continue;
|
|
40
|
+
this.remove(record.id);
|
|
41
|
+
}
|
|
42
|
+
this.seeded = true;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// Events keep the tree live even when the initial seed is unavailable.
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
this.seeding = undefined;
|
|
49
|
+
}
|
|
50
|
+
})();
|
|
51
|
+
return this.seeding;
|
|
52
|
+
}
|
|
53
|
+
handle(event) {
|
|
54
|
+
const type = event.type;
|
|
55
|
+
const data = isRecord(event.data) ? event.data : {};
|
|
56
|
+
if (type === "shell.created") {
|
|
57
|
+
const info = isRecord(data.info) ? data.info : undefined;
|
|
58
|
+
if (info?.status !== "running" || typeof info.id !== "string")
|
|
59
|
+
return;
|
|
60
|
+
const metadata = isRecord(info.metadata) ? info.metadata : undefined;
|
|
61
|
+
const owner = typeof metadata?.sessionID === "string" ? metadata.sessionID : undefined;
|
|
62
|
+
if (!owner)
|
|
63
|
+
return;
|
|
64
|
+
this.shells.set(info.id, { owner, root: this.rootOf(owner) });
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (type === "shell.exited" || type === "shell.deleted") {
|
|
68
|
+
if (typeof data.id === "string")
|
|
69
|
+
this.shells.delete(data.id);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const sessionID = typeof data.sessionID === "string" ? data.sessionID : undefined;
|
|
73
|
+
if (!sessionID)
|
|
74
|
+
return;
|
|
75
|
+
const now = typeof event.created === "number" ? event.created : Date.now();
|
|
76
|
+
if (type === "session.created") {
|
|
77
|
+
const location = isRecord(data.location) ? data.location : undefined;
|
|
78
|
+
this.upsert({
|
|
79
|
+
id: sessionID,
|
|
80
|
+
parentID: typeof data.parentID === "string" ? data.parentID : undefined,
|
|
81
|
+
title: typeof data.title === "string" ? data.title : undefined,
|
|
82
|
+
location: {
|
|
83
|
+
directory: typeof location?.directory === "string" ? location.directory : event.location?.directory,
|
|
84
|
+
},
|
|
85
|
+
time: { created: now, updated: now },
|
|
86
|
+
});
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (type === "session.deleted") {
|
|
90
|
+
this.remove(sessionID);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (type === "session.renamed") {
|
|
94
|
+
const record = this.ensure(sessionID, now);
|
|
95
|
+
if (typeof data.title === "string")
|
|
96
|
+
record.title = data.title;
|
|
97
|
+
record.updatedAt = now;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (type === "session.moved") {
|
|
101
|
+
const record = this.ensure(sessionID, now);
|
|
102
|
+
const location = isRecord(data.location) ? data.location : undefined;
|
|
103
|
+
if (typeof location?.directory === "string")
|
|
104
|
+
record.directory = location.directory;
|
|
105
|
+
record.updatedAt = now;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (type === "session.execution.started") {
|
|
109
|
+
this.onExecutionStarted(sessionID, now);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (type === "session.execution.succeeded" ||
|
|
113
|
+
type === "session.execution.failed" ||
|
|
114
|
+
type === "session.execution.interrupted") {
|
|
115
|
+
this.onExecutionEnded(sessionID, now);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
onPrompt(sessionID, own, now = Date.now()) {
|
|
119
|
+
if (own)
|
|
120
|
+
return;
|
|
121
|
+
const record = this.ensure(sessionID, now);
|
|
122
|
+
record.pending = true;
|
|
123
|
+
record.updatedAt = now;
|
|
124
|
+
}
|
|
125
|
+
onInboxEnqueued(sessionID, own, now = Date.now()) {
|
|
126
|
+
if (own)
|
|
127
|
+
return;
|
|
128
|
+
const record = this.ensure(sessionID, now);
|
|
129
|
+
record.pending = true;
|
|
130
|
+
record.updatedAt = now;
|
|
131
|
+
}
|
|
132
|
+
onInboxCancelled(sessionID, now = Date.now()) {
|
|
133
|
+
const record = this.records.get(sessionID);
|
|
134
|
+
if (!record)
|
|
135
|
+
return;
|
|
136
|
+
record.pending = false;
|
|
137
|
+
record.updatedAt = now;
|
|
138
|
+
}
|
|
139
|
+
onExecutionStarted(sessionID, now = Date.now()) {
|
|
140
|
+
const record = this.ensure(sessionID, now);
|
|
141
|
+
record.status = "running";
|
|
142
|
+
record.pending = false;
|
|
143
|
+
record.updatedAt = now;
|
|
144
|
+
}
|
|
145
|
+
onExecutionEnded(sessionID, now = Date.now()) {
|
|
146
|
+
const record = this.ensure(sessionID, now);
|
|
147
|
+
record.status = "idle";
|
|
148
|
+
record.pending = false;
|
|
149
|
+
record.updatedAt = now;
|
|
150
|
+
}
|
|
151
|
+
onIdle(sessionID, background, now = Date.now()) {
|
|
152
|
+
const record = this.ensure(sessionID, now);
|
|
153
|
+
record.status = "idle";
|
|
154
|
+
record.pending = false;
|
|
155
|
+
record.updatedAt = now;
|
|
156
|
+
const keepShells = new Set();
|
|
157
|
+
const keepImages = new Set();
|
|
158
|
+
for (const work of background) {
|
|
159
|
+
if (typeof work.sessionID !== "string")
|
|
160
|
+
continue;
|
|
161
|
+
if (work.sessionID !== sessionID) {
|
|
162
|
+
const child = this.ensure(work.sessionID, now);
|
|
163
|
+
if (child.parentID === undefined) {
|
|
164
|
+
child.parentID = sessionID;
|
|
165
|
+
this.indexChild(sessionID, work.sessionID);
|
|
166
|
+
}
|
|
167
|
+
if (work.kind === "subagent")
|
|
168
|
+
child.status = "running";
|
|
169
|
+
}
|
|
170
|
+
if (work.kind === "shell" && typeof work.shellID === "string") {
|
|
171
|
+
this.shells.set(work.shellID, { owner: work.sessionID, root: this.rootOf(work.sessionID) });
|
|
172
|
+
if (work.sessionID === sessionID)
|
|
173
|
+
keepShells.add(work.shellID);
|
|
174
|
+
}
|
|
175
|
+
if (work.kind === "image" && typeof work.path === "string") {
|
|
176
|
+
const key = imageKey(work.sessionID, work.path);
|
|
177
|
+
this.images.set(key, { owner: work.sessionID });
|
|
178
|
+
if (work.sessionID === sessionID)
|
|
179
|
+
keepImages.add(key);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// Reconcile only this session's own work. A descendant's execution events and idle
|
|
183
|
+
// hook own its status, so a parent snapshot never clears a still-running child.
|
|
184
|
+
for (const [id, shell] of this.shells) {
|
|
185
|
+
if (shell.owner === sessionID && !keepShells.has(id))
|
|
186
|
+
this.shells.delete(id);
|
|
187
|
+
}
|
|
188
|
+
for (const [key, image] of this.images) {
|
|
189
|
+
if (image.owner === sessionID && !keepImages.has(key))
|
|
190
|
+
this.images.delete(key);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/** Merge a session row fetched outside the event stream. */
|
|
194
|
+
remember(info) {
|
|
195
|
+
this.upsert(info);
|
|
196
|
+
}
|
|
197
|
+
statusOf(sessionID) {
|
|
198
|
+
const record = this.records.get(sessionID);
|
|
199
|
+
if (!record)
|
|
200
|
+
return undefined;
|
|
201
|
+
return record.status === "running" || record.pending ? "running" : "idle";
|
|
202
|
+
}
|
|
203
|
+
isBusy(sessionID) {
|
|
204
|
+
return this.statusOf(sessionID) === "running";
|
|
205
|
+
}
|
|
206
|
+
info(sessionID) {
|
|
207
|
+
const record = this.records.get(sessionID);
|
|
208
|
+
if (!record)
|
|
209
|
+
return {};
|
|
210
|
+
return {
|
|
211
|
+
...(record.title === undefined ? {} : { title: record.title }),
|
|
212
|
+
...(record.directory === undefined ? {} : { directory: record.directory }),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
parentOf(sessionID) {
|
|
216
|
+
return this.records.get(sessionID)?.parentID;
|
|
217
|
+
}
|
|
218
|
+
rootOf(sessionID) {
|
|
219
|
+
let current = sessionID;
|
|
220
|
+
const seen = new Set();
|
|
221
|
+
for (;;) {
|
|
222
|
+
if (seen.has(current))
|
|
223
|
+
return current;
|
|
224
|
+
seen.add(current);
|
|
225
|
+
const parent = this.records.get(current)?.parentID;
|
|
226
|
+
if (parent === undefined)
|
|
227
|
+
return current;
|
|
228
|
+
current = parent;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
family(sessionID) {
|
|
232
|
+
const family = new Set([sessionID]);
|
|
233
|
+
const stack = [sessionID];
|
|
234
|
+
while (stack.length > 0) {
|
|
235
|
+
const current = stack.pop();
|
|
236
|
+
if (current === undefined)
|
|
237
|
+
continue;
|
|
238
|
+
for (const child of this.children.get(current) ?? []) {
|
|
239
|
+
if (family.has(child))
|
|
240
|
+
continue;
|
|
241
|
+
family.add(child);
|
|
242
|
+
stack.push(child);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return family;
|
|
246
|
+
}
|
|
247
|
+
hasRunningWork(sessionID) {
|
|
248
|
+
const family = this.family(sessionID);
|
|
249
|
+
for (const id of family) {
|
|
250
|
+
const record = this.records.get(id);
|
|
251
|
+
if (record !== undefined && (record.status === "running" || record.pending))
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
for (const shell of this.shells.values()) {
|
|
255
|
+
if (family.has(shell.owner) || shell.root === sessionID)
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
for (const image of this.images.values()) {
|
|
259
|
+
if (family.has(image.owner))
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
shellCount() {
|
|
265
|
+
return this.shells.size;
|
|
266
|
+
}
|
|
267
|
+
upsert(info) {
|
|
268
|
+
const created = Number(info.time?.created);
|
|
269
|
+
const updated = Number(info.time?.updated);
|
|
270
|
+
const now = Date.now();
|
|
271
|
+
const existing = this.records.get(info.id);
|
|
272
|
+
// A live event may be newer than the snapshot used for a reconnect seed.
|
|
273
|
+
if (existing && Number.isFinite(updated) && existing.updatedAt > updated)
|
|
274
|
+
return;
|
|
275
|
+
const record = {
|
|
276
|
+
id: info.id,
|
|
277
|
+
...(info.parentID === undefined ? {} : { parentID: info.parentID }),
|
|
278
|
+
...(info.title === undefined ? {} : { title: info.title }),
|
|
279
|
+
...(info.location?.directory === undefined ? {} : { directory: info.location.directory }),
|
|
280
|
+
status: isSessionBusy(info) ? "running" : "idle",
|
|
281
|
+
pending: false,
|
|
282
|
+
createdAt: Number.isFinite(created) ? created : now,
|
|
283
|
+
updatedAt: Number.isFinite(updated) ? updated : now,
|
|
284
|
+
};
|
|
285
|
+
this.records.set(info.id, record);
|
|
286
|
+
if (record.parentID !== undefined)
|
|
287
|
+
this.indexChild(record.parentID, record.id);
|
|
288
|
+
}
|
|
289
|
+
ensure(sessionID, now = Date.now()) {
|
|
290
|
+
const existing = this.records.get(sessionID);
|
|
291
|
+
if (existing)
|
|
292
|
+
return existing;
|
|
293
|
+
const record = {
|
|
294
|
+
id: sessionID,
|
|
295
|
+
status: "idle",
|
|
296
|
+
pending: false,
|
|
297
|
+
createdAt: now,
|
|
298
|
+
updatedAt: now,
|
|
299
|
+
};
|
|
300
|
+
this.records.set(sessionID, record);
|
|
301
|
+
return record;
|
|
302
|
+
}
|
|
303
|
+
remove(sessionID) {
|
|
304
|
+
const record = this.records.get(sessionID);
|
|
305
|
+
if (record?.parentID !== undefined)
|
|
306
|
+
this.children.get(record.parentID)?.delete(sessionID);
|
|
307
|
+
this.records.delete(sessionID);
|
|
308
|
+
this.children.delete(sessionID);
|
|
309
|
+
// A running shell is kept until its exit/deleted event: deleting the owner does not
|
|
310
|
+
// prove the process is gone. Images die with their session and have no exit event.
|
|
311
|
+
for (const [key, image] of this.images) {
|
|
312
|
+
if (image.owner === sessionID)
|
|
313
|
+
this.images.delete(key);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
indexChild(parentID, childID) {
|
|
317
|
+
const list = this.children.get(parentID) ?? new Set();
|
|
318
|
+
list.add(childID);
|
|
319
|
+
this.children.set(parentID, list);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
async function listAll(list) {
|
|
323
|
+
const result = [];
|
|
324
|
+
let cursor;
|
|
325
|
+
for (let page = 0; page < MAX_SEED_PAGES; page += 1) {
|
|
326
|
+
const response = await list({
|
|
327
|
+
limit: SEED_PAGE_SIZE,
|
|
328
|
+
order: "desc",
|
|
329
|
+
archived: false,
|
|
330
|
+
...(cursor === undefined ? {} : { cursor }),
|
|
331
|
+
});
|
|
332
|
+
if (!response || !Array.isArray(response.data))
|
|
333
|
+
break;
|
|
334
|
+
for (const info of response.data) {
|
|
335
|
+
if (info && typeof info.id === "string")
|
|
336
|
+
result.push(info);
|
|
337
|
+
}
|
|
338
|
+
const next = response.cursor?.next;
|
|
339
|
+
if (typeof next !== "string" || !next)
|
|
340
|
+
break;
|
|
341
|
+
cursor = next;
|
|
342
|
+
}
|
|
343
|
+
return result;
|
|
344
|
+
}
|
|
345
|
+
function withTimeout(promise, timeoutMs) {
|
|
346
|
+
let timer;
|
|
347
|
+
const timeout = new Promise((resolve) => {
|
|
348
|
+
timer = setTimeout(() => resolve(undefined), timeoutMs);
|
|
349
|
+
});
|
|
350
|
+
return Promise.race([promise, timeout]).finally(() => {
|
|
351
|
+
if (timer !== undefined)
|
|
352
|
+
clearTimeout(timer);
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
function imageKey(owner, path) {
|
|
356
|
+
return `${owner}\u0000${path}`;
|
|
357
|
+
}
|
|
358
|
+
function isRecord(value) {
|
|
359
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
360
|
+
}
|