feat(approvals): let a watching session keep quiet, and fix the tui gate

The badge alone left the row in NEEDS YOU, which is the thing the issue was
about. The fix is the alert that does not fire.

An idle prompt from a session that is watching its own background work now
opens ALREADY acknowledged. `hook-event-routes` passes `Session.watching` to
`notePrompt()`, which sets `acknowledgedAt` and records why in a new
`acknowledgedReason`. Nothing new suppresses anything: `acknowledge()` has
always meant "the alert this prompt armed is spent", and the prompt itself
stays pending, answerable and available as Read My Mind context. A wrong label
therefore costs a card that does not blink, never an alert that was never
created.

Every surface follows from that. The broadcast carries the reason, so a live
page declines to arm the tab alert and raises no desktop notification. The push
is skipped, since a false alarm is hardest to ignore on a phone. A reloading
page reads `acknowledgedAt` in `seedApprovals()`, which it already did. And
`classifySession()` now reads it too, which is a pre-existing bug fixed here:
acknowledging on one device cleared the alert everywhere except `codeman tui`.
It re-arms for free, because the next idle prompt supersedes the item and is
built fresh. Only `idle` is eligible, so a dialog that blocks the agent still
goes red whatever else it started.

The label is pane-derived and therefore prompt-injectable, so it is now read
from the last two rows of the screen only, with Claude's pattern anchored on
the `·` its footer joins items with, ANSI-stripped and length-capped at the
source. An agent that prints `· 1 monitor ·` into its own output finds no
match.

Verified on an isolated beta: a session that armed a monitor took its idle
prompt acknowledged with no alert on any surface, wore the badge, and showed
"quiet, watching 1 monitor" on its still-answerable card; the same session with
the monitor killed alerted normally on the next prompt. `test/watching-no-alert.test.ts`
pins both directions across all four surfaces.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Michael Grundberg
2026-09-22 08:10:05 +02:00
co-authored by Claude Opus 5
parent 3f2cde2db7
commit 74884a20eb
19 changed files with 635 additions and 43 deletions
+52
View File
@@ -330,6 +330,58 @@ describe('ApprovalInbox', () => {
expect(inbox.getById(second.id)).toBeDefined();
});
describe('a session watching its own background work', () => {
it('opens its idle prompt already acknowledged, and says why', () => {
const item = inbox.notePrompt({ sessionId: 's1', sessionName: 'w1', kind: 'idle', watching: '1 monitor' });
expect(item.acknowledgedAt).toBe(item.createdAt);
expect(item.acknowledgedReason).toBe('watching 1 monitor');
});
it('keeps the prompt pending and answerable: only its alert is spent', () => {
// Acknowledging rather than skipping creation is what makes a wrong label cheap.
// The prompt is real either way, and this way it is still in the drawer, still
// answerable and still Read My Mind context.
const item = inbox.notePrompt({ sessionId: 's1', sessionName: 'w1', kind: 'idle', watching: '2 shells' });
expect(inbox.listPending().map((i) => i.id)).toEqual([item.id]);
expect(inbox.getForSession('s1')?.id).toBe(item.id);
expect(inbox.verifyStillAnswerable(item.id)).toBe(true);
});
it('never pre-acknowledges a dialog that blocks the agent', () => {
// A permission or question dialog blocks the turn whatever else the agent started,
// so watching says nothing about whether a human is needed.
for (const kind of ['permission', 'question'] as const) {
const item = inbox.notePrompt({ sessionId: `s-${kind}`, sessionName: 'w1', kind, watching: '1 monitor' });
expect(item.acknowledgedAt).toBeUndefined();
expect(item.acknowledgedReason).toBeUndefined();
}
});
it('leaves an ordinary idle prompt alone', () => {
for (const watching of [undefined, null, '']) {
const item = inbox.notePrompt({ sessionId: 's1', sessionName: 'w1', kind: 'idle', watching });
expect(item.acknowledgedAt).toBeUndefined();
}
});
it('re-arms by itself once the background work is over', () => {
// The next prompt supersedes this one and is built fresh, so nothing has to
// remember to clear the flag when the monitor ends.
const watched = inbox.notePrompt({ sessionId: 's1', sessionName: 'w1', kind: 'idle', watching: '1 monitor' });
expect(watched.acknowledgedAt).toBeDefined();
const after = inbox.notePrompt({ sessionId: 's1', sessionName: 'w1', kind: 'idle' });
expect(after.id).not.toBe(watched.id);
expect(after.acknowledgedAt).toBeUndefined();
expect(after.acknowledgedReason).toBeUndefined();
});
it('cannot be acknowledged a second time by a human opening the session', () => {
const item = inbox.notePrompt({ sessionId: 's1', sessionName: 'w1', kind: 'idle', watching: '1 monitor' });
expect(inbox.acknowledge('s1')).toBeUndefined();
expect(inbox.getById(item.id)?.acknowledgedReason).toBe('watching 1 monitor');
});
});
describe('verifyStillAnswerable', () => {
it('resolves the item and refuses when a parsed dialog left the screen', () => {
const { resolved } = collect(inbox);