@bridge4dev/runner 0.64.1 → 0.65.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.
@@ -6,6 +6,8 @@ import { RUNNER_VERSION } from '../version.js';
6
6
  import { repairCodexAuth } from './codex-home.js';
7
7
  import { AppServerClient, asRecord, num, RpcError, RpcTimeoutError, str, } from './codex-protocol.js';
8
8
  import { truncate } from './claude.js';
9
+ import { AgentTaskTray } from './agent-tasks.js';
10
+ import { CodexSubagents, threadProbeOver } from './codex-subagents.js';
9
11
  import { availableModes, cardDescription, DIRECT_BRANCH_RULE, folderRuleFor, MODE_REFUSED_TEXT, MODE_WITHDRAWN_TEXT, policyContextFor, DEVBRIDGE_MCP_SERVER_NAME, } from './types.js';
10
12
  import { clampPercent, rateWindowKeyFromMinutes } from './rate-limits.js';
11
13
  import { answerSummary, invalidationMessage, mirrorOptions, newAskId, MAX_OPTIONS, MAX_QUESTIONS, OPTION_TEXT_LIMIT, QUESTION_TEXT_LIMIT, } from './questions.js';
@@ -182,6 +184,24 @@ const APPROVAL_METHODS = new Set([
182
184
  'item/permissions/requestApproval',
183
185
  'mcpServer/elicitation/request',
184
186
  ]);
187
+ /**
188
+ * Notifications that concern the whole session whichever thread they name
189
+ * (#382). Everything else from a thread other than the session's own belongs to
190
+ * a helper – see `CodexSession.onHelperNotification`.
191
+ *
192
+ * - `serverRequest/resolved`: a helper's approval is shown as an ordinary card,
193
+ * and the card has to close when it is answered elsewhere;
194
+ * - MCP start-up and the warnings: the helpers share the session's servers and
195
+ * configuration, and each distinct sentence is shown once anyway.
196
+ */
197
+ const SESSION_WIDE = new Set([
198
+ 'serverRequest/resolved',
199
+ 'mcpServer/startupStatus/updated',
200
+ 'configWarning',
201
+ 'warning',
202
+ 'guardianWarning',
203
+ 'deprecationNotice',
204
+ ]);
185
205
  class CodexSession {
186
206
  spec;
187
207
  home;
@@ -236,6 +256,15 @@ class CodexSession {
236
256
  stopped = false;
237
257
  ready = false;
238
258
  capabilitiesInFlight = false;
259
+ /**
260
+ * Helpers at work beside the conversation (#382) – the same tray, and so the
261
+ * same «is anybody still working» rule, as the Claude adapter's.
262
+ */
263
+ tray = new AgentTaskTray({
264
+ emit: (event) => this.emit(event),
265
+ isStopped: () => this.stopped,
266
+ });
267
+ subagents;
239
268
  events = this.output;
240
269
  constructor(spec, home, deps) {
241
270
  this.spec = spec;
@@ -272,6 +301,16 @@ class CodexSession {
272
301
  sessionId: spec.sessionId,
273
302
  ...wiring,
274
303
  });
304
+ this.subagents = new CodexSubagents({
305
+ tray: this.tray,
306
+ ownThreadId: () => this.threadId,
307
+ probe: threadProbeOver((method, params, timeoutMs) => this.client.request(method, params, timeoutMs)),
308
+ isStopped: () => this.stopped,
309
+ ...(deps.subagentReconcileMs === undefined ? {} : { reconcileMs: deps.subagentReconcileMs }),
310
+ ...(deps.subagentSpawnGraceMs === undefined
311
+ ? {}
312
+ : { spawnGraceMs: deps.subagentSpawnGraceMs }),
313
+ });
275
314
  if (this.modeRefusedAtLaunch) {
276
315
  this.notice('warn', MODE_REFUSED_TEXT);
277
316
  this.emit({ type: 'settings', mode: this.mode });
@@ -307,6 +346,17 @@ class CodexSession {
307
346
  throw new Error(`codex is using an unexpected CODEX_HOME (${reportedHome ?? 'not reported'}) — refusing to start the session`);
308
347
  }
309
348
  this.client.notify('initialized', {});
349
+ // The helper set is per PROCESS (#113, #382): a session relaunched after a
350
+ // runner restart must not go on showing the helpers of its previous life,
351
+ // and nothing else would say so until the next one starts or ends.
352
+ //
353
+ // Here and not in the constructor, and that is not tidiness: the
354
+ // supervisor reads the FIRST event of a session as «the process came up»
355
+ // and calls off the watch for a CLI that never boots (#225). A frame
356
+ // emitted before a single byte reached the app-server would answer that
357
+ // watch for every Codex session, on behalf of a process that has said
358
+ // nothing. This is the first moment the process really has.
359
+ this.tray.announceEmpty();
310
360
  if (this.home.auth === 'missing') {
311
361
  // One repair attempt before telling the user their login is broken: the
312
362
  // credential link can be removed under a running daemon, and putting it
@@ -956,6 +1006,9 @@ class CodexSession {
956
1006
  if (this.stopped)
957
1007
  return;
958
1008
  this.stopped = true;
1009
+ // The helpers go with the process; nothing more is published about them.
1010
+ this.subagents.close();
1011
+ this.tray.close();
959
1012
  // Release anything the agent is blocked on so the child can exit cleanly —
960
1013
  // and say in the feed that the runner did it, not the user. A pending card
961
1014
  // used to gutter out looking like a human decision.
@@ -1262,6 +1315,17 @@ class CodexSession {
1262
1315
  }
1263
1316
  // ─── Notifications ─────────────────────────────────────────────────
1264
1317
  onNotification(method, params) {
1318
+ // #382. One app-server runs the session's thread AND every helper it spawns,
1319
+ // and the helpers' notifications arrive on this same connection tagged with
1320
+ // their own thread id (measured on 0.154.0). Read as ours, a helper's
1321
+ // `turn/completed` ended THIS session's turn while it was still working, its
1322
+ // `turn/started` became the turn a Stop or a steer is aimed at, and its
1323
+ // prose went into the feed as this session's answer.
1324
+ const thread = str(params['threadId']);
1325
+ if (thread && this.threadId && thread !== this.threadId && !SESSION_WIDE.has(method)) {
1326
+ this.onHelperNotification(thread, method, params);
1327
+ return;
1328
+ }
1265
1329
  switch (method) {
1266
1330
  case 'item/started':
1267
1331
  case 'item/updated':
@@ -1417,21 +1481,100 @@ class CodexSession {
1417
1481
  return;
1418
1482
  }
1419
1483
  }
1484
+ /**
1485
+ * A notification from another thread in this app-server – a helper's, or one
1486
+ * of Codex's own (#382).
1487
+ *
1488
+ * What it may change is the helper count and, while a turn of this session is
1489
+ * running, that turn's «work was done» flags – and nothing else: not the turn
1490
+ * itself, not the feed, not the context meter or the settings on the header,
1491
+ * and not the session's health. A helper's failure is the helper's; the one
1492
+ * exception is the plan limit, which belongs to the account and refuses this
1493
+ * session next. Its tool calls stay
1494
+ * out of the feed too, and that is not tidiness: the supervisor reads any
1495
+ * tool event from a resting session as «the agent is working» (#185), and no
1496
+ * turn of THIS session would ever come to put it back – a helper finishing
1497
+ * does not start one.
1498
+ *
1499
+ * Its items are still remembered: a helper's command that needs a person
1500
+ * arrives as an ordinary approval request, and the card is only readable
1501
+ * because the item behind it is known.
1502
+ */
1503
+ onHelperNotification(thread, method, params) {
1504
+ switch (method) {
1505
+ case 'item/started':
1506
+ case 'item/updated':
1507
+ case 'item/completed': {
1508
+ const item = asRecord(params['item']);
1509
+ this.rememberItem(item);
1510
+ // What a helper does while this session's turn is running is that turn's
1511
+ // work (#252, #257) – the Claude adapter counts a subagent's tool calls
1512
+ // for the same reason: a helper's `git push` is still a push, and a
1513
+ // failed turn that pushed must not be sent again as if nothing happened.
1514
+ if (method === 'item/started' &&
1515
+ item['type'] === 'commandExecution' &&
1516
+ this.activeTurnId !== null &&
1517
+ this.subagents.isHelper(thread)) {
1518
+ this.noteWork(str(item['command']) ?? '');
1519
+ }
1520
+ this.subagents.onItem(thread, item);
1521
+ return;
1522
+ }
1523
+ case 'turn/started':
1524
+ this.subagents.onHelperTurn(thread, 'started');
1525
+ return;
1526
+ case 'turn/completed': {
1527
+ const turn = asRecord(params['turn']);
1528
+ // A helper refused by the plan limit is this ACCOUNT being refused: the
1529
+ // session's own next turn will be refused too, so the API is told and
1530
+ // the feed says why (#382). Only the announcement — no turn of this
1531
+ // session ended, so there is no ending to mark `limitBlocked`.
1532
+ if (str(turn['status']) === 'failed') {
1533
+ this.announceRateLimitRefusal(asRecord(turn['error']));
1534
+ }
1535
+ this.subagents.onHelperTurn(thread, 'completed', str(turn['status']));
1536
+ return;
1537
+ }
1538
+ case 'thread/closed':
1539
+ this.subagents.onThreadClosed(thread);
1540
+ return;
1541
+ case 'error':
1542
+ if (params['willRetry'] !== true && this.subagents.isHelper(thread)) {
1543
+ const detail = asRecord(params['error']);
1544
+ // The one failure of a helper that is not the helper's own business:
1545
+ // the plan limit is the account's, and the session is next (#382).
1546
+ if (!this.announceRateLimitRefusal(detail)) {
1547
+ log.warn('codex: a helper agent reported an error', {
1548
+ sessionId: this.spec.sessionId,
1549
+ message: maskString(str(detail['message']) ?? '').slice(0, 300),
1550
+ });
1551
+ }
1552
+ }
1553
+ return;
1554
+ default:
1555
+ return;
1556
+ }
1557
+ }
1558
+ /** Keep an item's last state – approval params alone are too thin. */
1559
+ rememberItem(item) {
1560
+ const id = str(item['id']);
1561
+ if (!id)
1562
+ return;
1563
+ this.items.set(id, item);
1564
+ // Bound the cache: a long session would otherwise hold every item.
1565
+ if (this.items.size > 400) {
1566
+ const oldest = this.items.keys().next().value;
1567
+ if (oldest !== undefined)
1568
+ this.items.delete(oldest);
1569
+ }
1570
+ }
1420
1571
  onItem(method, params) {
1421
1572
  const item = asRecord(params['item']);
1422
1573
  const type = str(item['type']);
1423
1574
  const id = str(item['id']);
1424
1575
  if (!type)
1425
1576
  return;
1426
- if (id) {
1427
- this.items.set(id, item);
1428
- // Bound the cache: a long session would otherwise hold every item.
1429
- if (this.items.size > 400) {
1430
- const oldest = this.items.keys().next().value;
1431
- if (oldest !== undefined)
1432
- this.items.delete(oldest);
1433
- }
1434
- }
1577
+ this.rememberItem(item);
1435
1578
  const done = method === 'item/completed';
1436
1579
  switch (type) {
1437
1580
  case 'agentMessage': {
@@ -1556,6 +1699,17 @@ class CodexSession {
1556
1699
  }
1557
1700
  return;
1558
1701
  }
1702
+ case 'collabAgentToolCall':
1703
+ case 'subAgentActivity': {
1704
+ // #382: helpers starting, being handed work and finishing. Counted, and
1705
+ // deliberately NOT a feed row: a helper's `completed` arrives after this
1706
+ // session's turn is over, and a tool event then would put a resting
1707
+ // session back to «working» with no turn to end it (see
1708
+ // `onHelperNotification`).
1709
+ if (this.threadId)
1710
+ this.subagents.onItem(this.threadId, item);
1711
+ return;
1712
+ }
1559
1713
  case 'webSearch':
1560
1714
  case 'dynamicToolCall': {
1561
1715
  if (!done) {
@@ -1741,6 +1895,29 @@ class CodexSession {
1741
1895
  * one turn.
1742
1896
  */
1743
1897
  static LIMIT_SETTLE_MS = 180_000;
1898
+ /**
1899
+ * The plan is spent: tell the API so it can arm its clock, and tell the
1900
+ * person in the feed. Says nothing about whose turn it was.
1901
+ *
1902
+ * Split out of `noteRateLimitRefusal` for the refusals that belong to no turn
1903
+ * of this session at all — a helper's (#382). The block is account-wide: the
1904
+ * same account, the same window, and the next turn of the session will be
1905
+ * refused too. Silence here was a false «your turn» over a plan that had run
1906
+ * out, with nothing in the feed to explain it and no pause armed.
1907
+ */
1908
+ announceRateLimitRefusal(error) {
1909
+ const blocked = this.rateLimitRefusal(error);
1910
+ if (!blocked)
1911
+ return false;
1912
+ this.emitRateLimits(blocked);
1913
+ this.emit({
1914
+ type: 'notice',
1915
+ level: 'warn',
1916
+ text: 'Codex refused the turn: the plan limit is spent' +
1917
+ (blocked.resetsAt ? `, and it lifts at ${describeResetTime(blocked.resetsAt)}.` : '.'),
1918
+ });
1919
+ return true;
1920
+ }
1744
1921
  /**
1745
1922
  * The plan is spent — say so, and keep the session alive to be woken.
1746
1923
  *
@@ -1758,16 +1935,8 @@ class CodexSession {
1758
1935
  * feed of the session this was found on.
1759
1936
  */
1760
1937
  noteRateLimitRefusal(error) {
1761
- const blocked = this.rateLimitRefusal(error);
1762
- if (!blocked)
1938
+ if (!this.announceRateLimitRefusal(error))
1763
1939
  return false;
1764
- this.emitRateLimits(blocked);
1765
- this.emit({
1766
- type: 'notice',
1767
- level: 'warn',
1768
- text: 'Codex refused the turn: the plan limit is spent' +
1769
- (blocked.resetsAt ? `, and it lifts at ${describeResetTime(blocked.resetsAt)}.` : '.'),
1770
- });
1771
1940
  /**
1772
1941
  * The flag is set ONLY while a turn is in flight, and that is not caution.
1773
1942
  *
@@ -1800,6 +1969,7 @@ class CodexSession {
1800
1969
  // is still working — a steer would be aimed at a turn that has ended,
1801
1970
  // and the next real turn would look like a turn already in flight.
1802
1971
  this.activeTurnId = null;
1972
+ this.subagents.endTurn();
1803
1973
  this.emit({
1804
1974
  type: 'turn_end',
1805
1975
  ok: false,
@@ -1864,6 +2034,10 @@ class CodexSession {
1864
2034
  return;
1865
2035
  }
1866
2036
  this.activeTurnId = null;
2037
+ // #382: what is still running, said BEFORE the ending – the supervisor
2038
+ // decides «is this the person's turn» on the count it holds when `turn_end`
2039
+ // arrives.
2040
+ this.subagents.endTurn();
1867
2041
  // A held plan means the turn ended by proposing, not by finishing the work.
1868
2042
  if (this.heldPlan)
1869
2043
  return;
@@ -2310,6 +2484,8 @@ class CodexSession {
2310
2484
  finish() {
2311
2485
  this.stopped = true;
2312
2486
  this.clearLimitSettle();
2487
+ this.subagents.close();
2488
+ this.tray.close();
2313
2489
  this.output.end();
2314
2490
  }
2315
2491
  }
@@ -1076,6 +1076,26 @@ export declare class Supervisor {
1076
1076
  */
1077
1077
  private armCompactionWatchdog;
1078
1078
  private clearCompactionWatchdog;
1079
+ /**
1080
+ * A card that interrupted a RESTING session has been answered: go back to
1081
+ * rest instead of reporting a turn (#382).
1082
+ *
1083
+ * Which card it was does not matter — a Codex helper's approval, a Claude
1084
+ * background subagent's, an ask either of them parked while the session was
1085
+ * already the person's. What matters is that no turn of this session was
1086
+ * running when the card went out, so there is no turn to go back to and
1087
+ * nothing that would end one: on Codex the helper's own ending is explicitly
1088
+ * not the session's, so «Working» stood until somebody typed.
1089
+ *
1090
+ * Only when the burst is over — both card sets empty — because answering one
1091
+ * of three still leaves the session parked on the other two. The frame
1092
+ * carries the background count like every other status report, so the badge
1093
+ * and the Inbox see «resting, with helpers» rather than «your turn».
1094
+ *
1095
+ * Returns true when it handled the resolution, so the callers' «the human
1096
+ * answered, bill again» branches stay out of it.
1097
+ */
1098
+ private restAfterCard;
1079
1099
  /**
1080
1100
  * Record how many subagents are alive, and say so when it matters (#236).
1081
1101
  *
@@ -3404,6 +3404,39 @@ export class Supervisor {
3404
3404
  clearTimeout(running.compactionWatchdog);
3405
3405
  delete running.compactionWatchdog;
3406
3406
  }
3407
+ /**
3408
+ * A card that interrupted a RESTING session has been answered: go back to
3409
+ * rest instead of reporting a turn (#382).
3410
+ *
3411
+ * Which card it was does not matter — a Codex helper's approval, a Claude
3412
+ * background subagent's, an ask either of them parked while the session was
3413
+ * already the person's. What matters is that no turn of this session was
3414
+ * running when the card went out, so there is no turn to go back to and
3415
+ * nothing that would end one: on Codex the helper's own ending is explicitly
3416
+ * not the session's, so «Working» stood until somebody typed.
3417
+ *
3418
+ * Only when the burst is over — both card sets empty — because answering one
3419
+ * of three still leaves the session parked on the other two. The frame
3420
+ * carries the background count like every other status report, so the badge
3421
+ * and the Inbox see «resting, with helpers» rather than «your turn».
3422
+ *
3423
+ * Returns true when it handled the resolution, so the callers' «the human
3424
+ * answered, bill again» branches stay out of it.
3425
+ */
3426
+ restAfterCard(running) {
3427
+ const back = running.restBeforeCard;
3428
+ if (!back)
3429
+ return false;
3430
+ if (running.openPermissions.size > 0 || running.openQuestions.size > 0)
3431
+ return true;
3432
+ // `reportStatus` clears the memory — the status it sends is the newest
3433
+ // truth about this session, whatever it is.
3434
+ this.reportStatus(running.descriptor.id, back, {
3435
+ costUsd: running.costUsd,
3436
+ activeMs: Supervisor.spentMs(running),
3437
+ });
3438
+ return true;
3439
+ }
3407
3440
  /**
3408
3441
  * Record how many subagents are alive, and say so when it matters (#236).
3409
3442
  *
@@ -3817,6 +3850,8 @@ export class Supervisor {
3817
3850
  source: event.source,
3818
3851
  reason: event.reason,
3819
3852
  });
3853
+ if (this.restAfterCard(running))
3854
+ return;
3820
3855
  if (event.source === 'user' &&
3821
3856
  event.allow &&
3822
3857
  running.lastReported === 'WAITING_PERMISSION') {
@@ -3844,6 +3879,11 @@ export class Supervisor {
3844
3879
  // is the bug session 7 removed (five of the first twelve prod sessions
3845
3880
  // died having spent their budget waiting for a human).
3846
3881
  running.openQuestions.add(event.askId);
3882
+ // #382, the same as a permission card: an ask from work that outlives
3883
+ // the turn finds the session at rest, and the answer must put it back.
3884
+ if (running.lastReported === 'WAITING_INPUT' || running.lastReported === 'REVIEW') {
3885
+ running.restBeforeCard = running.lastReported;
3886
+ }
3847
3887
  this.sendEvent(running, 'question', {
3848
3888
  askId: event.askId,
3849
3889
  questions: event.questions,
@@ -3872,6 +3912,8 @@ export class Supervisor {
3872
3912
  // `openQuestions.size` matters: both agents can park several asks at
3873
3913
  // once, and reporting RUNNING while another card is still waiting would
3874
3914
  // bill a human's thinking time all over again (QA-106 M4).
3915
+ if (this.restAfterCard(running))
3916
+ return;
3875
3917
  if (event.source === 'user' &&
3876
3918
  running.openQuestions.size === 0 &&
3877
3919
  running.lastReported === 'WAITING_INPUT') {
@@ -7563,6 +7605,13 @@ export class Supervisor {
7563
7605
  // The API infers WAITING_PERMISSION from the event itself, so this never
7564
7606
  // goes through reportStatus — but the budget clock still has to stop, or
7565
7607
  // an ask-mode session bills every second the human spends reading the card.
7608
+ //
7609
+ // #382: remember what the card interrupted. A card from work that
7610
+ // outlives the turn finds the session at rest, and the answer must put it
7611
+ // back there rather than into a turn nobody is running.
7612
+ if (running.lastReported === 'WAITING_INPUT' || running.lastReported === 'REVIEW') {
7613
+ running.restBeforeCard = running.lastReported;
7614
+ }
7566
7615
  running.lastReported = 'WAITING_PERMISSION';
7567
7616
  this.syncBudgetClock(running);
7568
7617
  }
@@ -7572,6 +7621,8 @@ export class Supervisor {
7572
7621
  const running = this.sessions.get(sessionId);
7573
7622
  if (running) {
7574
7623
  running.lastReported = status;
7624
+ // #382: any status report is newer than the rest a card interrupted.
7625
+ delete running.restBeforeCard;
7575
7626
  // Coming to rest ends the busy period the once-per-turn notices were
7576
7627
  // limited to: the next one is about a new answer and deserves saying.
7577
7628
  if (!Supervisor.MID_TURN_STATUSES.includes(status)) {
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const RUNNER_VERSION = "0.64.1";
1
+ export declare const RUNNER_VERSION = "0.65.0";
2
2
  //# sourceMappingURL=version.d.ts.map
package/dist/version.js CHANGED
@@ -1,3 +1,3 @@
1
1
  // Kept in sync with package.json by the release script (manual for now).
2
- export const RUNNER_VERSION = '0.64.1';
2
+ export const RUNNER_VERSION = '0.65.0';
3
3
  //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bridge4dev/runner",
3
- "version": "0.64.1",
3
+ "version": "0.65.0",
4
4
  "description": "DevBridge dev runner — connects a dev server to DevBridge and runs agent sessions (Claude Code / Codex)",
5
5
  "homepage": "https://bridge4.dev",
6
6
  "license": "MIT",