@cogitator-ai/bridge 0.10.1 → 0.10.2
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/fakes/claude.mjs +2 -2
- package/fakes/codex.mjs +4 -4
- package/fakes/lib.mjs +50 -9
- package/package.json +1 -1
package/fakes/claude.mjs
CHANGED
|
@@ -759,8 +759,8 @@ function setActivity({ record, files }, status) {
|
|
|
759
759
|
patchJson(files.record, record.kind === 'background' ? { status, state } : { status });
|
|
760
760
|
}
|
|
761
761
|
|
|
762
|
-
function endSession(recordFile) {
|
|
763
|
-
abandonWork();
|
|
762
|
+
async function endSession(recordFile) {
|
|
763
|
+
await abandonWork();
|
|
764
764
|
patchJson(recordFile, {
|
|
765
765
|
pid: undefined,
|
|
766
766
|
status: undefined,
|
package/fakes/codex.mjs
CHANGED
|
@@ -289,9 +289,9 @@ function findThread(threadId) {
|
|
|
289
289
|
/** Runs one turn of `exec` to its end. The first event leaves before the turn starts */
|
|
290
290
|
async function foregroundTurn(thread, prompt, asJson) {
|
|
291
291
|
const { lock: lockFile, running } = threadFiles(thread.threadId);
|
|
292
|
-
onStopSignal(() => {
|
|
292
|
+
onStopSignal(async () => {
|
|
293
293
|
rmSync(running, { force: true });
|
|
294
|
-
abandonWork();
|
|
294
|
+
await abandonWork();
|
|
295
295
|
process.exit(EXIT_ON_STOP_SIGNAL);
|
|
296
296
|
});
|
|
297
297
|
await lock(lockFile);
|
|
@@ -521,8 +521,8 @@ async function serve(argv) {
|
|
|
521
521
|
const [kind] = argv;
|
|
522
522
|
if (argv.length !== 1 || !OWNER_KINDS.includes(kind)) throw new Unsupported();
|
|
523
523
|
if ((await awaitJson(ownerFile(kind))) === undefined) return;
|
|
524
|
-
boundLife(ownerFile(kind), () => {
|
|
525
|
-
abandonWork();
|
|
524
|
+
boundLife(ownerFile(kind), async () => {
|
|
525
|
+
await abandonWork();
|
|
526
526
|
process.exit(0);
|
|
527
527
|
});
|
|
528
528
|
const draining = new Set();
|
package/fakes/lib.mjs
CHANGED
|
@@ -26,6 +26,11 @@ export const POLL_MS = 25;
|
|
|
26
26
|
|
|
27
27
|
const RUN_TIMEOUT_MS = 30_000;
|
|
28
28
|
const STOP_GRACE_MS = 5_000;
|
|
29
|
+
/**
|
|
30
|
+
* How long the shells of a process that is ending get to end themselves. Shorter than
|
|
31
|
+
* STOP_GRACE_MS, so that the process is gone before whoever stops it gives up on it
|
|
32
|
+
*/
|
|
33
|
+
const SHELL_GRACE_MS = 3_000;
|
|
29
34
|
const DEFAULT_MAX_LIFE_MS = 120_000;
|
|
30
35
|
const STATE_WATCH_MS = 100;
|
|
31
36
|
const STOP_SIGNALS = ['SIGTERM', 'SIGINT'];
|
|
@@ -276,10 +281,11 @@ export function pending(dir) {
|
|
|
276
281
|
|
|
277
282
|
/**
|
|
278
283
|
* Takes the oldest message out of a spool folder. It is renamed away first, so no message is
|
|
279
|
-
* taken twice even when several processes look at one folder.
|
|
284
|
+
* taken twice even when several processes look at one folder. A process that is ending takes none.
|
|
280
285
|
* @returns the message, or `undefined` when nothing waits
|
|
281
286
|
*/
|
|
282
287
|
export function takeNext(dir) {
|
|
288
|
+
if (abandoning !== undefined) return undefined;
|
|
283
289
|
const [name] = pending(dir);
|
|
284
290
|
if (name === undefined) return undefined;
|
|
285
291
|
const taken = join(dir, `.taken-${process.pid}-${name}`);
|
|
@@ -398,8 +404,20 @@ export function boundLife(stateFile, stop) {
|
|
|
398
404
|
}, STATE_WATCH_MS);
|
|
399
405
|
}
|
|
400
406
|
|
|
401
|
-
/** Child shells of this process that still run */
|
|
402
|
-
const shells = new
|
|
407
|
+
/** Child shells of this process that still run, each with a promise of its end */
|
|
408
|
+
const shells = new Map();
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Settles when the process that is ending has nothing going any more. While it waits for that the
|
|
412
|
+
* process starts no shell, takes no message and goes no further with a turn: what was under way
|
|
413
|
+
* stays where it was, as if the process were gone already.
|
|
414
|
+
*/
|
|
415
|
+
let abandoning;
|
|
416
|
+
|
|
417
|
+
/** What a turn awaits before its next step: settles at once, and never in a process that is ending */
|
|
418
|
+
function unlessEnding() {
|
|
419
|
+
return abandoning === undefined ? Promise.resolve() : new Promise(() => undefined);
|
|
420
|
+
}
|
|
403
421
|
|
|
404
422
|
function signalGroup(pid, signal) {
|
|
405
423
|
if (pid !== undefined) send(-pid, signal);
|
|
@@ -415,7 +433,8 @@ function signalGroup(pid, signal) {
|
|
|
415
433
|
|
|
416
434
|
/**
|
|
417
435
|
* Starts `sh -c <command>` in a process group of its own, so that a kill reaches whatever the
|
|
418
|
-
* command started.
|
|
436
|
+
* command started. A process that is ending starts none, and the result of a shell it still had
|
|
437
|
+
* reaches no one.
|
|
419
438
|
* @param {string} command
|
|
420
439
|
* @param {object} options
|
|
421
440
|
* @param {string} options.cwd
|
|
@@ -426,13 +445,15 @@ function signalGroup(pid, signal) {
|
|
|
426
445
|
* @returns {{ pid: number | undefined, done: Promise<ShellResult> }}
|
|
427
446
|
*/
|
|
428
447
|
export function startShell(command, { cwd, env, timeoutMs, killSignal, input }) {
|
|
448
|
+
if (abandoning !== undefined) return { pid: undefined, done: new Promise(() => undefined) };
|
|
429
449
|
const child = spawn('sh', ['-c', command], {
|
|
430
450
|
cwd,
|
|
431
451
|
env,
|
|
432
452
|
detached: true,
|
|
433
453
|
stdio: [input === undefined ? 'ignore' : 'pipe', 'pipe', 'pipe'],
|
|
434
454
|
});
|
|
435
|
-
|
|
455
|
+
const { promise: ended, resolve: end } = Promise.withResolvers();
|
|
456
|
+
shells.set(child, ended);
|
|
436
457
|
const done = new Promise((settle) => {
|
|
437
458
|
const output = { stdout: '', stderr: '' };
|
|
438
459
|
let timedOut = false;
|
|
@@ -446,7 +467,8 @@ export function startShell(command, { cwd, env, timeoutMs, killSignal, input })
|
|
|
446
467
|
const finish = (code) => {
|
|
447
468
|
clearTimeout(timer);
|
|
448
469
|
shells.delete(child);
|
|
449
|
-
|
|
470
|
+
end();
|
|
471
|
+
if (abandoning === undefined) settle({ code, ...output, timedOut });
|
|
450
472
|
};
|
|
451
473
|
child.on('error', (error) => {
|
|
452
474
|
output.stderr += error.message;
|
|
@@ -461,10 +483,26 @@ export function startShell(command, { cwd, env, timeoutMs, killSignal, input })
|
|
|
461
483
|
return { pid: child.pid, done };
|
|
462
484
|
}
|
|
463
485
|
|
|
464
|
-
/**
|
|
486
|
+
/**
|
|
487
|
+
* Ends what this process still has going before it stops: its child shells and its locks. The
|
|
488
|
+
* process has to stay until this settles. A shell that is asked to end may still write to its
|
|
489
|
+
* pipes, if only the word of its shell on a child the signal killed, and one that writes to a pipe
|
|
490
|
+
* nobody reads any more dies of SIGPIPE before its own handler of the signal has run.
|
|
491
|
+
* @returns {Promise<void>}
|
|
492
|
+
*/
|
|
465
493
|
export function abandonWork() {
|
|
466
|
-
|
|
467
|
-
|
|
494
|
+
abandoning ??= endShells().then(() => {
|
|
495
|
+
for (const file of [...heldLocks]) unlock(file);
|
|
496
|
+
});
|
|
497
|
+
return abandoning;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/** Asks the shells to end and waits for them, killing those that take longer than SHELL_GRACE_MS */
|
|
501
|
+
async function endShells() {
|
|
502
|
+
const ended = [...shells.values()];
|
|
503
|
+
for (const child of shells.keys()) signalGroup(child.pid, 'SIGTERM');
|
|
504
|
+
await Promise.race([Promise.all(ended), sleep(SHELL_GRACE_MS)]);
|
|
505
|
+
for (const child of shells.keys()) signalGroup(child.pid, 'SIGKILL');
|
|
468
506
|
}
|
|
469
507
|
|
|
470
508
|
/**
|
|
@@ -562,6 +600,7 @@ async function editWithLeave(path, cwd, askLeave) {
|
|
|
562
600
|
return { command, code: 1, stdout: '', stderr: 'this stand-in has no edit tool of its own' };
|
|
563
601
|
}
|
|
564
602
|
const leave = await askLeave({ tool: 'Edit', input: { file_path: resolve(cwd, path) } });
|
|
603
|
+
await unlessEnding();
|
|
565
604
|
const moved = leave.cwd !== undefined && { movedTo: leave.cwd };
|
|
566
605
|
if (!leave.allowed) {
|
|
567
606
|
return {
|
|
@@ -604,6 +643,7 @@ export async function performTurn({ input, cwd: startedIn, env, callTool, askLea
|
|
|
604
643
|
let answer = 'ok';
|
|
605
644
|
let cwd = startedIn;
|
|
606
645
|
for (const { kind, value } of directives(input)) {
|
|
646
|
+
await unlessEnding();
|
|
607
647
|
if (kind === 'run') ran.push(await runCommand(value, cwd, env));
|
|
608
648
|
else if (kind === 'bash') ran.push(await runWithLeave(value, cwd, env, askLeave));
|
|
609
649
|
else if (kind === 'edit') {
|
|
@@ -614,6 +654,7 @@ export async function performTurn({ input, cwd: startedIn, env, callTool, askLea
|
|
|
614
654
|
else if (kind === 'sleep') await sleep(Number(value));
|
|
615
655
|
else answer = value;
|
|
616
656
|
}
|
|
657
|
+
await unlessEnding();
|
|
617
658
|
return { at, input, ran, tools, answer };
|
|
618
659
|
}
|
|
619
660
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cogitator-ai/bridge",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.2",
|
|
4
4
|
"description": "Agent bridge for Cogitator: Claude Code, Codex and other agents hand each other jobs, ask questions and report back, each in its own window (experimental)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cogitator",
|