pendingyou 0.43.1 → 0.43.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,73 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.43.3 (2026-11-17)
4
+
5
+ - An assistant in the middle of a long task when you answer is told at its next tool call, once, in one line, instead
6
+ of when the task is done: "Pending You, while you were working: your person answered “Ship the webhook retries
7
+ tonight?” (req_…). Read it now with get_request (name “infra”), and act on it at the next sensible point in this
8
+ task." The same for a message on one of its cards, a reply on one of its answers, and a question you hand it. Never
9
+ your answer's own words: it reads it with `get_request`. The line is a hint, said once a turn: only the assistant's
10
+ own read counts, so one that doesn't read it is woken when its turn ends, as before, the wake ending "You were told
11
+ of this while working and haven’t read it." (Codex: "You may have been told of this while working, and haven’t read
12
+ it.", since nothing says Codex showed the line to the model). An idle assistant is woken as before. With nothing waiting, a tool call costs
13
+ nothing more.
14
+ - Claude Code: the wake mod adds the line after the next tool result of the session's main conversation (never a
15
+ subagent's). Nothing to restart: open sessions reload the mod by themselves.
16
+ - Codex: the hooks keep each thread's turn on disk (begun by your message or its first tool call, kept fresh by each
17
+ tool call, ended by the Stop hook, and over by itself after 5 minutes with no tool call), and while a turn runs the
18
+ listener leaves the wake for the next tool call's PostToolUse, which Codex 0.169 runs for calls made inside its code
19
+ mode too. Whatever it hasn't read when the turn ends is queued at once, from the Stop hook. Where Codex's
20
+ permission-prompt hooks are installed (the default with this computer's own connection), their every-call hook
21
+ carries the line, so nothing needs trusting again and no tool call costs more; elsewhere (`--no-permission-cards`,
22
+ or a sign-in that only hears) `init` adds one hook on every call, a process of its own each time (about a tenth of a
23
+ second), and says "Codex has new Pending You hooks (answers while it works)": trust it once in `/hooks`. `status`
24
+ has a "Mid-task" line. Every wake `codex queue` carries now ends "If you already read it with get_request and acted
25
+ on it, end this turn with no message.", since a queued wake can't be taken back.
26
+ - Pi: unchanged: a wake still waits for the run under way to end.
27
+ - OpenCode already took a wake into the turn under way.
28
+ - Fixed: a question Claude Code held through a turn whose read failed as the turn ended is read again shortly, not
29
+ dropped.
30
+ - Guide 2.61.3 (a wording patch) says that an answer that didn’t work also has `failed` true, and that a to-do answered with `failed` and no step goes back with its fix as one with `failedStep` does; it carries the same mod in the Pending You plugin for Claude.
31
+ - Run `npx pendingyou@latest init`. Restart Codex: its hooks take the new command line at once, but a listener already
32
+ running keeps the old one.
33
+
34
+ ## 0.43.2 (2026-11-17)
35
+
36
+ - Fixed: a Claude Code session Pending You showed as running didn't hear a question you handed its agent. The wake mod
37
+ listened only for 12 hours after the session last used Pending You, and kept its name only in memory when it had no
38
+ cards of its own, so after 12 quiet hours, or after a restart, `claude --resume`, `/resume`, `/clear` or an upgrade
39
+ that reloaded the mod, the session heard nothing until you typed to it. Now an open session that has used Pending You
40
+ under a name hears a question handed to it for as long as it's open: within seconds through the computer's one wait,
41
+ as before, and on its own timer as the fallback (every minute for the first half hour after it last used Pending
42
+ You, every 3 minutes up to 12 hours, then every 10 minutes). Its name, and when it last used Pending You, are kept
43
+ with its cards in the mod's store, so a restart, a resume and a reload keep hearing. A session the mod knows no name
44
+ for goes by the one its presence last gave Pending You, which is the name Pending You shows running, so sessions open
45
+ across this upgrade hear too. Of several sessions by one name, the one that used Pending You most recently takes a
46
+ question handed to it; another takes it only if nobody has after half a minute. A session doesn't tell the same
47
+ question twice after a reload, and a subagent never renames its session. `/clear` starts a new conversation, which
48
+ hears from its first call with its name, as before. A branch (`/branch`) is a new task: it neither says nor hears its
49
+ parent's name until it uses Pending You itself, and Pending You no longer shows it running under that name. Who
50
+ hears it is otherwise unchanged: the session in the card's folder, one session only, never the one that asked.
51
+ - Codex: of two threads by one name, an open one takes a handed question before a closed one used more recently.
52
+ - Codex, OpenCode and Pi: a session its app says is open (presence) keeps listening for a question handed to it however
53
+ long it's been quiet, not only for 12 hours, and its listener is started again when it stops (Codex's keeper,
54
+ OpenCode's plugin and Pi's extension, every 5 minutes). Closed, the 12 hours as before.
55
+ - Fixed: `status` said "Setup: waiting for its first session. Start Claude Code (or restart it)…" beside "Ready" for a
56
+ sign-in its assistants had been using for days, whose own test question never went all the way round. It now says
57
+ "Setup: finished. It’s in use, so its unfinished test question no longer matters.", and
58
+ for a sign-in that reported but has posted nothing, "reported, but its test question isn’t sent yet…". A session of a
59
+ sign-in in use is no longer told, as it starts, that "setup’s test question isn’t sent yet" (every `init` started
60
+ those reminders over). Needs Pending You's server from the same release, which says when a sign-in is in use.
61
+ - Fixed: Codex, OpenCode and Pi: when a card came back from the assistant you handed it to, its session heard why, but
62
+ not your answer after: the listener forgot the card with the notice. It keeps it now, until it's answered.
63
+ - When the session-start or next-message hook can't reach Pending You, its line now says to call `list_pending` with
64
+ your name.
65
+ - Guide 2.61.2 (a patch: no words change) carries the same mod in the Pending You plugin for Claude.
66
+ - Run `npx pendingyou@latest init`. Claude Code: nothing to restart (open sessions reload the mod by themselves).
67
+ Codex: restart it, or each thread picks it up once its keeper starts again (its next session start or message after
68
+ its last one ended). OpenCode and Pi: restart them (Pi: `/reload`), since their plugin points at the version init
69
+ installed.
70
+
3
71
  ## 0.43.1 (2026-11-16)
4
72
 
5
73
  - Claude Code: when you answer a card while its session sits idle, the turn that wakes it now carries your answer in
@@ -0,0 +1,164 @@
1
+ import { readFile, rm, stat, utimes } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { configDir, readJson, withLock, writeWhole } from "../files.js";
4
+ import { isHandedWake } from "../format.js";
5
+ import { midTurnText } from "../midturn.js";
6
+ export const STALE_WAKE = ' If you already read it with get_request and acted on it, end this turn with no message.';
7
+ export const TURN_FOR_MS = 5 * 60_000;
8
+ const THREAD = /^[A-Za-z0-9][A-Za-z0-9_-]{0,99}$/;
9
+ export const UNREAD_NOTE = 'You may have been told of this while working, and haven’t read it.';
10
+ export const unreadWake = (text) => text.includes('\n') ? `${text}\n${UNREAD_NOTE}` : `${text} ${UNREAD_NOTE}`;
11
+ const turnPath = (io, thread) => join(configDir(io), 'codex-turns', `${thread}.json`);
12
+ const heardPath = (io, thread) => join(configDir(io), 'codex-heard', `${thread}.json`);
13
+ const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
14
+ async function turnOf(io, thread) {
15
+ try {
16
+ const path = turnPath(io, thread);
17
+ const at = (await stat(path)).mtimeMs;
18
+ const parsed = JSON.parse(await readFile(path, 'utf8'));
19
+ const turn = isObject(parsed) && typeof parsed.turn === 'string' ? parsed.turn : null;
20
+ return { turn, at };
21
+ }
22
+ catch {
23
+ return null;
24
+ }
25
+ }
26
+ export async function turnBegan(io, thread, turn) {
27
+ try {
28
+ if (!THREAD.test(thread))
29
+ return;
30
+ await writeWhole(turnPath(io, thread), `${JSON.stringify({ turn })}\n`, { secret: true });
31
+ }
32
+ catch { }
33
+ }
34
+ export async function turnEnded(io, thread, turn) {
35
+ try {
36
+ if (!THREAD.test(thread))
37
+ return;
38
+ const on = await turnOf(io, thread);
39
+ if (on && turn && on.turn && on.turn !== turn)
40
+ return;
41
+ await rm(turnPath(io, thread), { force: true });
42
+ }
43
+ catch { }
44
+ }
45
+ export async function midTurn(io, thread) {
46
+ if (!THREAD.test(thread))
47
+ return false;
48
+ try {
49
+ return io.now() - (await stat(turnPath(io, thread))).mtimeMs < TURN_FOR_MS;
50
+ }
51
+ catch {
52
+ return false;
53
+ }
54
+ }
55
+ async function readHeard(io, thread) {
56
+ const file = await readJson(heardPath(io, thread)).catch(() => null);
57
+ const items = isObject(file?.items) ? Object.values(file.items) : [];
58
+ return items.filter((each) => isObject(each) &&
59
+ typeof each.requestId === 'string' &&
60
+ typeof each.moment === 'string' &&
61
+ typeof each.wake === 'string' &&
62
+ isObject(each.item));
63
+ }
64
+ async function writeHeard(io, thread, list) {
65
+ const path = heardPath(io, thread);
66
+ if (list.length === 0)
67
+ await rm(path, { force: true });
68
+ else
69
+ await writeWhole(path, `${JSON.stringify({ version: 1, items: Object.fromEntries(list.map((each) => [each.requestId, each])) })}\n`, { secret: true });
70
+ }
71
+ export async function holdHeard(io, thread, list) {
72
+ if (!THREAD.test(thread) || list.length === 0)
73
+ return;
74
+ await withLock(heardPath(io, thread), io, async () => {
75
+ const kept = (await readHeard(io, thread)).filter((each) => !list.some((added) => added.requestId === each.requestId));
76
+ await writeHeard(io, thread, [...kept, ...list]);
77
+ });
78
+ }
79
+ export async function hasHeard(io, thread) {
80
+ if (!THREAD.test(thread))
81
+ return false;
82
+ try {
83
+ await stat(heardPath(io, thread));
84
+ return true;
85
+ }
86
+ catch {
87
+ return false;
88
+ }
89
+ }
90
+ export async function takeHeard(io, thread) {
91
+ if (!(await hasHeard(io, thread)))
92
+ return [];
93
+ return withLock(heardPath(io, thread), io, async () => {
94
+ const list = await readHeard(io, thread);
95
+ await writeHeard(io, thread, []);
96
+ return list.map((each) => (each.told ? unreadWake(each.wake) : each.wake));
97
+ });
98
+ }
99
+ export function queuedWakes(wakes) {
100
+ const own = wakes.filter((wake) => !isHandedWake(wake));
101
+ return [...(own.length ? [own.join('\n\n')] : []), ...wakes.filter(isHandedWake)];
102
+ }
103
+ export async function heardRead(io, thread, requestId) {
104
+ try {
105
+ if (!(await hasHeard(io, thread)))
106
+ return;
107
+ await withLock(heardPath(io, thread), io, async () => {
108
+ const list = await readHeard(io, thread);
109
+ const kept = list.filter((each) => each.requestId !== requestId);
110
+ if (kept.length !== list.length)
111
+ await writeHeard(io, thread, kept);
112
+ });
113
+ }
114
+ catch { }
115
+ }
116
+ export async function tellHeard(io, input) {
117
+ try {
118
+ const thread = typeof input.session_id === 'string' ? input.session_id : '';
119
+ const turn = typeof input.turn_id === 'string' && input.turn_id ? input.turn_id : null;
120
+ if (!THREAD.test(thread))
121
+ return null;
122
+ const now = io.now();
123
+ const on = await turnOf(io, thread);
124
+ const ours = !on || !turn || !on.turn || on.turn === turn;
125
+ if (ours && on) {
126
+ const at = new Date(now);
127
+ await utimes(turnPath(io, thread), at, at);
128
+ }
129
+ else if (!on || now - on.at >= TURN_FOR_MS)
130
+ await turnBegan(io, thread, turn);
131
+ else
132
+ return null;
133
+ if (!(await hasHeard(io, thread)))
134
+ return null;
135
+ const list = await withLock(heardPath(io, thread), io, async () => {
136
+ const all = await readHeard(io, thread);
137
+ const fresh = all.filter((each) => !each.told);
138
+ if (fresh.length)
139
+ await writeHeard(io, thread, all.map((each) => ({ ...each, told: true })));
140
+ return fresh;
141
+ });
142
+ if (list.length === 0)
143
+ return null;
144
+ const text = midTurnText(list.map((each) => each.item));
145
+ return text
146
+ ? `${JSON.stringify({ hookSpecificOutput: { hookEventName: 'PostToolUse', additionalContext: text } })}\n`
147
+ : null;
148
+ }
149
+ catch {
150
+ return null;
151
+ }
152
+ }
153
+ export async function heardHook(io) {
154
+ try {
155
+ const input = JSON.parse(await io.readStdin(1000, 8_000_000));
156
+ if (!isObject(input) || input.hook_event_name !== 'PostToolUse')
157
+ return 0;
158
+ const told = await tellHeard(io, input);
159
+ if (told)
160
+ io.out(told);
161
+ }
162
+ catch { }
163
+ return 0;
164
+ }
@@ -6,12 +6,13 @@ import { DEFAULT_ORIGIN } from "../args.js";
6
6
  import { connectionMachine, readCredential } from "../credentials.js";
7
7
  import { configDir, readJson, withLock, writeWhole } from "../files.js";
8
8
  import { APPROVED_DRAFT_NOTICE, approvesDraft, clip, delegatedOf, handledOf, isClosed, noticeOf, } from "../format.js";
9
- import { chooseThread, goesBy, HANDED_FOR_MS, handedCard, handedMessage, namesAnotherComputer, } from "../handed.js";
9
+ import { chooseThread, goesBy, HANDED_FOR_MS, handedCard, handedMessage, namesAnotherComputer, OPEN_FOR_MS, } from "../handed.js";
10
10
  import { clearHerdr, herdrTarget, isUrgency, PANE_ID, reportCommand, writeHerdr, } from "../herdr.js";
11
11
  import { linkSoon } from "../link.js";
12
12
  import { macName, tailscaleName } from "../remote.js";
13
13
  import { claimHanded, markHanded, momentOf, releaseHanded, wasHanded } from "../state.js";
14
14
  import { findCodex } from "./codex-app.js";
15
+ import { hasHeard, heardRead, holdHeard, midTurn, queuedWakes, STALE_WAKE, takeHeard, } from "./codex-midturn.js";
15
16
  import { APP_IDS } from "./ids.js";
16
17
  const KEEP_TURN_MS = 24 * 60 * 60_000;
17
18
  const KEEP_THREAD_MS = 7 * 24 * 60 * 60_000;
@@ -68,6 +69,9 @@ function threadOf(value) {
68
69
  ...(names.length ? { names: names.slice(-NAMES) } : {}),
69
70
  ...(typeof value.cwd === 'string' && value.cwd ? { cwd: value.cwd } : {}),
70
71
  ...(typeof value.herdr === 'string' && PANE_ID.test(value.herdr) ? { herdr: value.herdr } : {}),
72
+ ...(typeof value.liveAt === 'number' && Number.isFinite(value.liveAt)
73
+ ? { liveAt: value.liveAt }
74
+ : {}),
71
75
  ...(typeof value.answeredAt === 'number' && Number.isFinite(value.answeredAt)
72
76
  ? { answeredAt: value.answeredAt }
73
77
  : {}),
@@ -88,7 +92,9 @@ async function load(io, app) {
88
92
  turns: isObject(file?.turns) ? file.turns : {},
89
93
  };
90
94
  }
91
- const mayBeHanded = (thread, now) => Boolean(thread.names?.length) && now - thread.at < HANDED_FOR_MS;
95
+ const mayBeHanded = (thread, now) => Boolean(thread.names?.length) &&
96
+ (now - thread.at < HANDED_FOR_MS ||
97
+ (thread.liveAt !== undefined && now - thread.liveAt < OPEN_FOR_MS));
92
98
  const listenable = (thread, now) => thread !== undefined && (Object.keys(thread.cards).length > 0 || mayBeHanded(thread, now));
93
99
  async function change(io, app, edit) {
94
100
  const path = statePath(io, app);
@@ -100,7 +106,8 @@ async function change(io, app, edit) {
100
106
  if (!(now - turn.at < KEEP_TURN_MS))
101
107
  delete state.turns[id];
102
108
  for (const [id, thread] of Object.entries(state.threads))
103
- if (!(now - thread.at < KEEP_THREAD_MS) || !listenable(thread, now))
109
+ if (!(now - Math.max(thread.at, thread.liveAt ?? 0) < KEEP_THREAD_MS) ||
110
+ !listenable(thread, now))
104
111
  delete state.threads[id];
105
112
  await writeWhole(path, `${JSON.stringify(state, null, 2)}\n`, { secret: true });
106
113
  return state;
@@ -233,7 +240,11 @@ async function named(io, options, input, thread) {
233
240
  return 0;
234
241
  if (isObject(input.tool_response) && input.tool_response.isError === true)
235
242
  return 0;
236
- const name = nameOf(isObject(input.tool_input) ? input.tool_input : {});
243
+ const args = isObject(input.tool_input) ? input.tool_input : {};
244
+ if (app === 'codex' && namedTool(app, input.tool_name) === 'get_request')
245
+ if (typeof args.requestId === 'string')
246
+ await heardRead(io, thread, args.requestId);
247
+ const name = nameOf(args);
237
248
  if (!name)
238
249
  return 0;
239
250
  const cwd = text(input.cwd, 1000);
@@ -327,6 +338,22 @@ export async function touchThread(io, app, thread, cwd) {
327
338
  }
328
339
  catch { }
329
340
  }
341
+ export async function markOpen(io, app, thread, state, at) {
342
+ try {
343
+ if (!THREAD.test(thread) || !(await load(io, app)).threads[thread])
344
+ return;
345
+ await change(io, app, (record) => {
346
+ const known = record.threads[thread];
347
+ if (!known)
348
+ return;
349
+ if (state === 'live')
350
+ known.liveAt = Math.max(known.liveAt ?? 0, at);
351
+ else if (known.liveAt !== undefined && known.liveAt <= at)
352
+ delete known.liveAt;
353
+ });
354
+ }
355
+ catch { }
356
+ }
330
357
  export async function threadNames(io, app, thread) {
331
358
  if (!thread)
332
359
  return [];
@@ -367,6 +394,7 @@ async function candidates(io, app) {
367
394
  ...(record.cwd ? { cwd: record.cwd } : {}),
368
395
  at: record.at,
369
396
  live: app === 'codex' || (await listening(io, app, thread)),
397
+ ...(record.liveAt !== undefined && now - record.liveAt < OPEN_FOR_MS ? { open: true } : {}),
370
398
  });
371
399
  }
372
400
  return found;
@@ -427,7 +455,7 @@ export async function keepListening(io, origin, thread) {
427
455
  return;
428
456
  const { threads } = await load(io, 'codex');
429
457
  const now = io.now();
430
- const own = Object.keys(threads[thread]?.cards ?? {}).length > 0;
458
+ const own = Object.keys(threads[thread]?.cards ?? {}).length > 0 || (await hasHeard(io, thread));
431
459
  const handed = Object.values(threads).some((each) => mayBeHanded(each, now));
432
460
  if (!own && !handed)
433
461
  return;
@@ -551,10 +579,30 @@ function byCodexQueue(io, thread) {
551
579
  let program = null;
552
580
  return async (message) => {
553
581
  program ??= findCodex(io).then((found) => found?.command ?? 'codex');
554
- const queued = await io.run(await program, ['queue', '--thread', thread, '--message', message], 30_000);
582
+ const queued = await io.run(await program, ['queue', '--thread', thread, '--message', `${message}${STALE_WAKE}`], 30_000);
555
583
  return queued.code === 0;
556
584
  };
557
585
  }
586
+ function midItemOf(heard, card) {
587
+ const kind = noticeOf(heard)
588
+ ? 'word'
589
+ : card.kind === 'answer'
590
+ ? 'replied'
591
+ : heard.status === 'expired'
592
+ ? 'fallback'
593
+ : heard.status === 'cancelled'
594
+ ? 'withdrawn'
595
+ : heard.status === 'answered'
596
+ ? 'answered'
597
+ : 'wrote';
598
+ const name = card.name ?? heard.sessionLabel;
599
+ return {
600
+ kind,
601
+ title: card.title ?? heard.title,
602
+ requestId: heard.requestId,
603
+ ...(name ? { name } : {}),
604
+ };
605
+ }
558
606
  function byPlugin(io, gone) {
559
607
  const queue = [];
560
608
  let waiting = null;
@@ -625,12 +673,19 @@ async function handedHere(io, options, requests) {
625
673
  }
626
674
  return mine;
627
675
  }
676
+ const stillTheirs = (heard) => noticeOf(heard)?.kind === 'came_back' && !isClosed(heard);
628
677
  async function giveBack(io, origin, list) {
629
678
  for (const { heard } of list)
630
679
  await releaseHanded(io, origin, heard.requestId, momentOf(heard)).catch(() => { });
631
680
  }
632
681
  export async function listen(io, options) {
633
682
  const { origin, app, thread } = options;
683
+ if (app === 'codex' && !(await midTurn(io, thread))) {
684
+ const wakes = await takeHeard(io, thread);
685
+ const send = byCodexQueue(io, thread);
686
+ for (const message of queuedWakes(wakes))
687
+ await send(message, []);
688
+ }
634
689
  const lease = await takeLease(io, app, thread);
635
690
  if (!lease)
636
691
  return 0;
@@ -649,9 +704,16 @@ export async function listen(io, options) {
649
704
  let since;
650
705
  let failures = 0;
651
706
  while (io.now() - started < LISTEN_MS && !signal.aborted) {
707
+ if (app === 'codex' && !(await midTurn(io, thread))) {
708
+ const wakes = await takeHeard(io, thread);
709
+ for (const message of queuedWakes(wakes))
710
+ await deliver(message, []);
711
+ }
712
+ const waiting = app === 'codex' && (await hasHeard(io, thread));
652
713
  const record = (await load(io, app)).threads[thread];
653
714
  const cards = record?.cards ?? {};
654
- if (!record || (Object.keys(cards).length === 0 && !(takes && mayBeHanded(record, io.now()))))
715
+ if (!waiting &&
716
+ (!record || (Object.keys(cards).length === 0 && !(takes && mayBeHanded(record, io.now())))))
655
717
  return 0;
656
718
  if (!(await renew(io, app, thread, lease)))
657
719
  return 0;
@@ -687,8 +749,10 @@ export async function listen(io, options) {
687
749
  const card = cards[heard.requestId];
688
750
  if (!card || !heard.ready || delegatedOf(heard))
689
751
  continue;
690
- if (await wasHanded(io, origin, heard.requestId, momentOf(heard)))
691
- done.push(heard.requestId);
752
+ if (await wasHanded(io, origin, heard.requestId, momentOf(heard))) {
753
+ if (!stillTheirs(heard))
754
+ done.push(heard.requestId);
755
+ }
692
756
  else
693
757
  ready.push({ heard, card });
694
758
  }
@@ -700,7 +764,14 @@ export async function listen(io, options) {
700
764
  };
701
765
  const message = wakeMessage(ready);
702
766
  if (message) {
703
- if (!(await deliver(message, ready.map(({ heard }) => heard.requestId)))) {
767
+ if (app === 'codex' && (await midTurn(io, thread)))
768
+ await holdHeard(io, thread, ready.map((each) => ({
769
+ requestId: each.heard.requestId,
770
+ moment: momentOf(each.heard),
771
+ item: midItemOf(each.heard, each.card),
772
+ wake: wakeMessage([each]) ?? message,
773
+ })));
774
+ else if (!(await deliver(message, ready.map(({ heard }) => heard.requestId)))) {
704
775
  await gone();
705
776
  return 0;
706
777
  }
@@ -715,7 +786,8 @@ export async function listen(io, options) {
715
786
  return 0;
716
787
  }
717
788
  for (const { heard } of ready)
718
- done.push(heard.requestId);
789
+ if (!stillTheirs(heard))
790
+ done.push(heard.requestId);
719
791
  for (const heard of requests)
720
792
  if (cards[heard.requestId] && isClosed(heard) && !done.includes(heard.requestId))
721
793
  done.push(heard.requestId);
@@ -755,7 +827,7 @@ export async function listenHanded(io, options) {
755
827
  let program = null;
756
828
  const queue = async (thread, message) => {
757
829
  program ??= findCodex(io).then((found) => found?.command ?? 'codex');
758
- const queued = await io.run(await program, ['queue', '--thread', thread, '--message', message], 30_000);
830
+ const queued = await io.run(await program, ['queue', '--thread', thread, '--message', `${message}${STALE_WAKE}`], 30_000);
759
831
  return queued.code === 0;
760
832
  };
761
833
  const elsewhere = await elsewhereFor(io, origin, app);
@@ -765,7 +837,14 @@ export async function listenHanded(io, options) {
765
837
  let failures = 0;
766
838
  while (io.now() - started < LISTEN_MS && !signal.aborted) {
767
839
  const now = io.now();
768
- if (!Object.values((await load(io, app)).threads).some((each) => mayBeHanded(each, now)))
840
+ const { threads } = await load(io, app);
841
+ for (const thread of Object.keys(threads))
842
+ if (!(await midTurn(io, thread))) {
843
+ const wakes = await takeHeard(io, thread);
844
+ for (const message of queuedWakes(wakes))
845
+ await queue(thread, message);
846
+ }
847
+ if (!Object.values(threads).some((each) => mayBeHanded(each, now)))
769
848
  return 0;
770
849
  if (!(await renew(io, app, HANDED, lease)))
771
850
  return 0;
@@ -814,6 +893,20 @@ export async function listenHanded(io, options) {
814
893
  }
815
894
  for (const [thread, list] of byThread) {
816
895
  const message = handedMessage(list.map(({ card }) => card), { words: false });
896
+ if (message && (await midTurn(io, thread))) {
897
+ await holdHeard(io, thread, list.map(({ heard, card }) => ({
898
+ requestId: heard.requestId,
899
+ moment: momentOf(heard),
900
+ item: {
901
+ kind: card.said ? 'handed-reply' : 'handed',
902
+ title: card.title,
903
+ requestId: card.requestId,
904
+ name: card.name,
905
+ },
906
+ wake: handedMessage([card], { words: false }) ?? message,
907
+ })));
908
+ continue;
909
+ }
817
910
  if (message && (await queue(thread, message)))
818
911
  continue;
819
912
  await giveBack(io, origin, list);
@@ -57,11 +57,13 @@ export const CODEX_PRESENCE_HOOKS = [
57
57
  export const CODEX_NAMED_HOOKS = [
58
58
  { event: 'PostToolUse', sub: 'named', matcher: NAMED_TOOLS },
59
59
  ];
60
+ export const CODEX_HEARD_HOOKS = [{ event: 'PostToolUse', sub: 'heard' }];
60
61
  export const ALL_CODEX_HOOKS = [
61
62
  ...CODEX_HOOKS,
62
63
  ...CODEX_PERMISSION_HOOKS,
63
64
  ...CODEX_PRESENCE_HOOKS,
64
65
  ...CODEX_NAMED_HOOKS,
66
+ ...CODEX_HEARD_HOOKS,
65
67
  ];
66
68
  const EVENT_LABELS = {
67
69
  SessionStart: 'session_start',
@@ -494,6 +496,7 @@ export const codex = {
494
496
  ...(extra && cards ? codexPermissionHooks(wait) : []),
495
497
  ...(extra ? CODEX_PRESENCE_HOOKS : []),
496
498
  ...CODEX_NAMED_HOOKS,
499
+ ...(extra && cards ? [] : CODEX_HEARD_HOOKS),
497
500
  ];
498
501
  const wantedKeys = new Set(wanted.map(lineKey));
499
502
  const unwanted = ALL_CODEX_HOOKS.filter((spec) => !wantedKeys.has(lineKey(spec)));
@@ -520,10 +523,11 @@ export const codex = {
520
523
  ...(fresh.some((spec) => spec.sub === 'named')
521
524
  ? ['questions handed to a thread']
522
525
  : []),
526
+ ...(fresh.some((spec) => spec.sub === 'heard') ? ['answers while it works'] : []),
523
527
  ]);
524
528
  report({
525
529
  ok: true,
526
- text: merged.changed
530
+ text: merged.changed && !(removed.changed && !cards)
527
531
  ? `Installed or updated Codex’s session-start pickup, next-message hand-off, card, name and Stop check hooks${extra ? `, ${cards ? 'its permission-prompt hooks (a card when Codex waits for your OK; --no-permission-cards leaves them out) ' : ''}and its session-end hook (Pending You hears when a thread is open)` : ''} in ${file.path}. Codex runs them once you trust them (how: below).`
528
532
  : removed.changed
529
533
  ? `Took out Codex’s permission-prompt hooks, as you asked: no card when Codex waits for your OK. To turn them on: npx pendingyou init --app codex --permission-cards${originArgs(ictx.origin)}`
@@ -717,6 +721,9 @@ export const codex = {
717
721
  : null;
718
722
  const presenceText = extraLine('Presence:', 'tells Pending You when this session is open', CODEX_PRESENCE_HOOKS.map((spec) => spec.sub), ownConnection ? null : notOwn);
719
723
  const namedText = extraLine('Handed questions:', 'a question you hand a thread that said its name to Pending You wakes it', CODEX_NAMED_HOOKS.map((spec) => spec.sub), null);
724
+ const midTaskText = extraLine('Mid-task:', 'an answer that comes while a thread works is told at its next tool call', ownConnection && manifest?.permissionCards !== false
725
+ ? ['permission-done']
726
+ : CODEX_HEARD_HOOKS.map((spec) => spec.sub), null);
720
727
  const wakes = detected
721
728
  ? atLeast(detected.version, detected.command ? CODEX_APP_WAKE : CODEX_WAKE)
722
729
  : false;
@@ -738,6 +745,7 @@ export const codex = {
738
745
  ...(askFirst ? [askFirst] : []),
739
746
  presenceText,
740
747
  namedText,
748
+ midTaskText,
741
749
  ` ${mark(wakes)} Wake: ${wakes
742
750
  ? detected?.command
743
751
  ? 'codex queue wakes a chat the Codex app has open when you answer, while the app runs (one it let go, idle 3 hours or behind 10 newer idle chats, hears it when you open it again)'
@@ -13,6 +13,7 @@ const TEXT_CHARS = 100_000;
13
13
  const CARD_TOOL = /^[A-Za-z0-9_-]*pendingyou[A-Za-z0-9_-]*_(post_request|post_answer|update_request|reply_in_thread|cancel_request|answer_delegated|hand_back)$/i;
14
14
  const NAMED_TOOL = /^[A-Za-z0-9_-]*pendingyou[A-Za-z0-9_-]*_(whoami|list_pending|get_request)$/i;
15
15
  export const HANDED_FOR_MS = 12 * 60 * 60_000;
16
+ export const OPEN_FOR_MS = 15 * 60_000;
16
17
  const NUDGED = 'Asked OpenCode to put what it needs from you on a card.';
17
18
  const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
18
19
  const ourMark = (part) => {
@@ -34,10 +35,11 @@ export function wantsListener(file, session, now = Date.now()) {
34
35
  return false;
35
36
  if (isObject(thread.cards) && Object.keys(thread.cards).length > 0)
36
37
  return true;
37
- return (Array.isArray(thread.names) &&
38
- thread.names.length > 0 &&
39
- typeof thread.at === 'number' &&
40
- now - thread.at < HANDED_FOR_MS);
38
+ if (!Array.isArray(thread.names) || thread.names.length === 0)
39
+ return false;
40
+ if (typeof thread.liveAt === 'number' && now - thread.liveAt < OPEN_FOR_MS)
41
+ return true;
42
+ return typeof thread.at === 'number' && now - thread.at < HANDED_FOR_MS;
41
43
  }
42
44
  const BASE62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
43
45
  export function partId(now = Date.now()) {
@@ -172,8 +174,12 @@ export async function pendingYou(input, deps) {
172
174
  deps.presence(id, 'live');
173
175
  stopBeat ??=
174
176
  deps.every?.(PRESENCE_EVERY_MS, () => {
175
- for (const each of present)
177
+ for (const each of present) {
176
178
  deps.presence?.(each, 'live');
179
+ const session = sessions.get(each);
180
+ if (session && !session.listener && !session.parent && deps.wanted?.(each))
181
+ listenFor(each, session);
182
+ }
177
183
  }) ?? null;
178
184
  };
179
185
  const closed = (id) => {
@@ -11,11 +11,12 @@ const STOP_MS = 2000;
11
11
  export const CARD_TOOL = /^mcp__[A-Za-z0-9_]*pendingyou[A-Za-z0-9_]*__(post_request|post_answer|update_request|reply_in_thread|cancel_request|answer_delegated|hand_back)$/i;
12
12
  export const NAMED_TOOL = /^mcp__[A-Za-z0-9_]*pendingyou[A-Za-z0-9_]*__(whoami|list_pending|get_request)$/i;
13
13
  export const HANDED_FOR_MS = 12 * 60 * 60_000;
14
+ export const OPEN_FOR_MS = 15 * 60_000;
14
15
  export const HANDED_PREFIX = 'Pending You: your person handed you';
15
16
  const HANDED_REPLY = ' on the question handed to you, ';
16
17
  const isHandedWake = (text) => {
17
18
  const first = text.trimStart().split('\n')[0] ?? '';
18
- return (first.startsWith(HANDED_PREFIX) ||
19
+ return (first.startsWith(`${HANDED_PREFIX} `) ||
19
20
  (first.startsWith('Pending You: ') && first.includes(HANDED_REPLY)));
20
21
  };
21
22
  const SOURCES = {
@@ -93,10 +94,11 @@ export function wantsListener(file, session, now = Date.now()) {
93
94
  return false;
94
95
  if (isObject(thread.cards) && Object.keys(thread.cards).length > 0)
95
96
  return true;
96
- return (Array.isArray(thread.names) &&
97
- thread.names.length > 0 &&
98
- typeof thread.at === 'number' &&
99
- now - thread.at < HANDED_FOR_MS);
97
+ if (!Array.isArray(thread.names) || thread.names.length === 0)
98
+ return false;
99
+ if (typeof thread.liveAt === 'number' && now - thread.liveAt < OPEN_FOR_MS)
100
+ return true;
101
+ return typeof thread.at === 'number' && now - thread.at < HANDED_FOR_MS;
100
102
  }
101
103
  function reasonOf(printed) {
102
104
  try {
@@ -172,6 +174,8 @@ export function pendingYou(setup, options = {}) {
172
174
  if (closed || !present)
173
175
  return;
174
176
  presence(present, 'live');
177
+ if (!listener)
178
+ listen();
175
179
  heartbeat();
176
180
  }, PRESENCE_EVERY_MS);
177
181
  unref(beat);
package/dist/args.js CHANGED
@@ -77,6 +77,7 @@ const COMMANDS = new Set([
77
77
  'mcp-headers',
78
78
  'posted',
79
79
  'named',
80
+ 'heard',
80
81
  'listen',
81
82
  'mcp',
82
83
  'permission',
@@ -111,6 +112,7 @@ const FLAGS = {
111
112
  stopcheck: { origin: true, app: true },
112
113
  posted: { origin: true, app: true },
113
114
  named: { origin: true, app: true },
115
+ heard: { origin: true, app: true },
114
116
  listen: { origin: true, app: true, thread: true, handed: false, since: true },
115
117
  permission: { origin: true, app: true },
116
118
  'permission-done': { origin: true, app: true },
@@ -125,6 +127,7 @@ const FLAGS = {
125
127
  name: true,
126
128
  transcript: true,
127
129
  keep: false,
130
+ fork: false,
128
131
  pid: true,
129
132
  at: true,
130
133
  },
@@ -209,6 +212,7 @@ export const HOOK_COMMANDS = new Set([
209
212
  'stopcheck',
210
213
  'posted',
211
214
  'named',
215
+ 'heard',
212
216
  'permission',
213
217
  'permission-done',
214
218
  'notify',
@@ -360,6 +364,7 @@ export function parseArgs(argv, env = {}) {
360
364
  return { name: 'stopcheck', origin, ...app() };
361
365
  case 'posted':
362
366
  case 'named':
367
+ case 'heard':
363
368
  none();
364
369
  return { name: first, origin, ...app() };
365
370
  case 'listen': {
@@ -428,6 +433,7 @@ export function parseArgs(argv, env = {}) {
428
433
  ...given('transcript'),
429
434
  ...(agentName ? { agentName } : {}),
430
435
  ...(flags.has('keep') ? { keep: true } : {}),
436
+ ...(flags.has('fork') ? { fork: true } : {}),
431
437
  ...(pid ? { pid } : {}),
432
438
  ...(at ? { at } : {}),
433
439
  };