@cogitator-ai/bridge 0.5.0 → 0.6.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.
Files changed (65) hide show
  1. package/README.md +18 -15
  2. package/dist/adapter.d.ts +33 -2
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/adapters/claude.d.ts +95 -2
  5. package/dist/adapters/claude.d.ts.map +1 -1
  6. package/dist/adapters/claude.js +207 -3
  7. package/dist/adapters/claude.js.map +1 -1
  8. package/dist/adapters/hosted.d.ts +3 -2
  9. package/dist/adapters/hosted.d.ts.map +1 -1
  10. package/dist/adapters/hosted.js +2 -1
  11. package/dist/adapters/hosted.js.map +1 -1
  12. package/dist/client.d.ts +11 -1
  13. package/dist/client.d.ts.map +1 -1
  14. package/dist/client.js +14 -0
  15. package/dist/client.js.map +1 -1
  16. package/dist/dispatcher.d.ts +33 -1
  17. package/dist/dispatcher.d.ts.map +1 -1
  18. package/dist/dispatcher.js +112 -4
  19. package/dist/dispatcher.js.map +1 -1
  20. package/dist/index.d.ts +7 -5
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +2 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/inputs.d.ts +24 -0
  25. package/dist/inputs.d.ts.map +1 -1
  26. package/dist/inputs.js +4 -0
  27. package/dist/inputs.js.map +1 -1
  28. package/dist/permit.d.ts +43 -0
  29. package/dist/permit.d.ts.map +1 -0
  30. package/dist/permit.js +118 -0
  31. package/dist/permit.js.map +1 -0
  32. package/dist/person-channel.d.ts.map +1 -1
  33. package/dist/person-channel.js +6 -0
  34. package/dist/person-channel.js.map +1 -1
  35. package/dist/person-inbox.d.ts +2 -1
  36. package/dist/person-inbox.d.ts.map +1 -1
  37. package/dist/person-inbox.js +10 -1
  38. package/dist/person-inbox.js.map +1 -1
  39. package/dist/person.d.ts +13 -0
  40. package/dist/person.d.ts.map +1 -1
  41. package/dist/person.js +1 -0
  42. package/dist/person.js.map +1 -1
  43. package/dist/protocol.d.ts +2 -0
  44. package/dist/protocol.d.ts.map +1 -1
  45. package/dist/protocol.js +2 -0
  46. package/dist/protocol.js.map +1 -1
  47. package/dist/server.d.ts.map +1 -1
  48. package/dist/server.js +2 -0
  49. package/dist/server.js.map +1 -1
  50. package/dist/setup/claude.d.ts +4 -2
  51. package/dist/setup/claude.d.ts.map +1 -1
  52. package/dist/setup/claude.js +9 -3
  53. package/dist/setup/claude.js.map +1 -1
  54. package/dist/testing/fake-harnesses.d.ts +15 -2
  55. package/dist/testing/fake-harnesses.d.ts.map +1 -1
  56. package/dist/testing/fake-harnesses.js +17 -2
  57. package/dist/testing/fake-harnesses.js.map +1 -1
  58. package/dist/types.d.ts +6 -0
  59. package/dist/types.d.ts.map +1 -1
  60. package/dist/types.js +2 -0
  61. package/dist/types.js.map +1 -1
  62. package/fakes/claude.mjs +175 -8
  63. package/fakes/lib.mjs +58 -7
  64. package/fakes/mcp.mjs +5 -1
  65. package/package.json +1 -1
package/fakes/lib.mjs CHANGED
@@ -467,7 +467,13 @@ export function abandonWork() {
467
467
  for (const file of [...heldLocks]) unlock(file);
468
468
  }
469
469
 
470
- /** @typedef {{ command: string, code: number | null, stdout: string, stderr: string }} CommandRun */
470
+ /**
471
+ * A command of a turn. `denied` marks one that never ran because leave for it was refused, with
472
+ * the refusal in `stderr`
473
+ * @typedef {{ command: string, code: number | null, stdout: string, stderr: string, denied?: boolean }} CommandRun
474
+ */
475
+
476
+ /** @typedef {(call: { tool: string, input: Record<string, unknown> }) => Promise<{ allowed: boolean, message?: string }>} LeaveAsker */
471
477
 
472
478
  /**
473
479
  * A tool call of a turn: `args` as the model wrote them, `sent` as they reached the server when
@@ -482,11 +488,11 @@ export function abandonWork() {
482
488
  /**
483
489
  * The directives of an input, in the order of its lines. A line counts after its leading
484
490
  * whitespace is trimmed, a quoted line (`> run: ...`) is not a directive.
485
- * @returns {{ kind: 'run' | 'sleep' | 'say' | 'tool', value: string }[]}
491
+ * @returns {{ kind: 'run' | 'bash' | 'edit' | 'sleep' | 'say' | 'tool', value: string }[]}
486
492
  */
487
493
  function directives(input) {
488
494
  return input.split(/\r?\n/).flatMap((line) => {
489
- const match = /^(run|sleep|say|tool): (.+)$/.exec(line.trimStart());
495
+ const match = /^(run|bash|edit|sleep|say|tool): (.+)$/.exec(line.trimStart());
490
496
  if (!match) return [];
491
497
  const [, kind, text] = match;
492
498
  const value = text.trim();
@@ -527,23 +533,68 @@ async function runTool(value, callTool) {
527
533
  }
528
534
 
529
535
  /**
530
- * The scripted model: runs every `run: <command>` line with `sh -c`, calls the tool of every
531
- * `tool: <server>.<tool> {json}` line and waits out every `sleep: <ms>` line, one after another,
532
- * and answers with the last `say: <text>` line, or `ok`.
536
+ * A `bash:` directive: a command the session's own shell tool runs, which the session needs
537
+ * leave for the way its harness decides. A refused one does not run.
538
+ * @param {string} command
539
+ * @param {LeaveAsker | undefined} askLeave
540
+ * @returns {Promise<CommandRun>}
541
+ */
542
+ async function runWithLeave(command, cwd, env, askLeave) {
543
+ const leave = askLeave ? await askLeave({ tool: 'Bash', input: { command } }) : { allowed: true };
544
+ if (leave.allowed) return runCommand(command, cwd, env);
545
+ return { command, code: null, stdout: '', stderr: leave.message ?? 'denied', denied: true };
546
+ }
547
+
548
+ /**
549
+ * An `edit:` directive: a file the session's own edit tool changes, by adding a line to it,
550
+ * which the session needs leave for the way its harness decides. It is in the turn as a command
551
+ * `edit <path>`, refused or done. A harness that imitates no edit tool of its own fails it.
552
+ * @param {string} path as the model wrote it, relative to the folder of the session or absolute
553
+ * @param {LeaveAsker | undefined} askLeave
554
+ * @returns {Promise<CommandRun>}
555
+ */
556
+ async function editWithLeave(path, cwd, askLeave) {
557
+ const file = resolve(cwd, path);
558
+ const command = `edit ${path}`;
559
+ if (!askLeave) {
560
+ return { command, code: 1, stdout: '', stderr: 'this stand-in has no edit tool of its own' };
561
+ }
562
+ const leave = await askLeave({ tool: 'Edit', input: { file_path: file } });
563
+ if (!leave.allowed) {
564
+ return { command, code: null, stdout: '', stderr: leave.message ?? 'denied', denied: true };
565
+ }
566
+ try {
567
+ mkdirSync(dirname(file), { recursive: true });
568
+ appendFileSync(file, 'edited\n');
569
+ return { command, code: 0, stdout: '', stderr: '' };
570
+ } catch (error) {
571
+ return { command, code: 1, stdout: '', stderr: error.message };
572
+ }
573
+ }
574
+
575
+ /**
576
+ * The scripted model: runs every `run: <command>` line with `sh -c`, asks leave for and then
577
+ * runs every `bash: <command>` line, asks leave for and then changes the file of every
578
+ * `edit: <path>` line, calls the tool of every `tool: <server>.<tool> {json}` line and waits
579
+ * out every `sleep: <ms>` line, one after another, and answers with the last `say: <text>`
580
+ * line, or `ok`.
533
581
  * @param {object} turn
534
582
  * @param {string} turn.input
535
583
  * @param {string} turn.cwd where the commands run
536
584
  * @param {NodeJS.ProcessEnv} turn.env what the commands see
537
585
  * @param {ToolCaller} [turn.callTool] how the session calls a tool of one of its MCP servers
586
+ * @param {LeaveAsker} [turn.askLeave] how the session gets leave for a call of its own tools
538
587
  * @returns {Promise<TurnOutcome>}
539
588
  */
540
- export async function performTurn({ input, cwd, env, callTool }) {
589
+ export async function performTurn({ input, cwd, env, callTool, askLeave }) {
541
590
  const at = new Date().toISOString();
542
591
  const ran = [];
543
592
  const tools = [];
544
593
  let answer = 'ok';
545
594
  for (const { kind, value } of directives(input)) {
546
595
  if (kind === 'run') ran.push(await runCommand(value, cwd, env));
596
+ else if (kind === 'bash') ran.push(await runWithLeave(value, cwd, env, askLeave));
597
+ else if (kind === 'edit') ran.push(await editWithLeave(value, cwd, askLeave));
547
598
  else if (kind === 'tool') tools.push(await runTool(value, callTool));
548
599
  else if (kind === 'sleep') await sleep(Number(value));
549
600
  else answer = value;
package/fakes/mcp.mjs CHANGED
@@ -5,7 +5,11 @@
5
5
  import { spawn } from 'node:child_process';
6
6
  import { createInterface } from 'node:readline';
7
7
 
8
- const REQUEST_TIMEOUT_MS = 15_000;
8
+ /**
9
+ * A server of tools is a process that loads the whole runtime before it answers its first
10
+ * request: on a busy CI runner that took over fifteen seconds, so a request waits a minute
11
+ */
12
+ const REQUEST_TIMEOUT_MS = 60_000;
9
13
  const PROTOCOL_VERSION = '2025-06-18';
10
14
 
11
15
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cogitator-ai/bridge",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
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",