repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(os-notifier): confirm active user messages

b3fd1c6a73069d335b465eb0cda7b2fb43735b09

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (5)

docs/super/plans/2026-07-12-os-notifier.md

Modified
133 133 - encode Windows PowerShell script as UTF-16LE Base64 for `-EncodedCommand`
134 134 - pass macOS title/body through `osascript` argv to avoid shell interpolation
135 135 - add `oscBackend(env)`
136 -- add minimal candidate/prepared/active eligibility bookkeeping driven by `input`, `before_agent_start`, `agent_start`, and `agent_settled`; handled input cannot leak, while steering, retries, and follow-ups stay within the active lifecycle
136 +- add minimal candidate/prepared/active eligibility bookkeeping driven by `input`, `before_agent_start`, user `message_start`, `agent_start`, and `agent_settled`; handled input cannot leak, while accepted steering, retries, and follow-ups stay within the active lifecycle
137 137 - remove `notify:send`, `notify:ui`, startup probes, backend caching, and warning notifications
139 139 - [ ] **Step 6: Run extension tests**

docs/super/specs/2026-07-12-os-notifier-design.md

Modified
34 34 The extension records input origin in extension-instance memory because `agent_end` does not expose it.
35 35 It records idle `input` as a candidate, then confirms it at `before_agent_start` only when Pi accepts the prompt.
36 36 This prevents handled input from leaking eligibility into extension-triggered runs that start without an `input` event.
37 -Pi drains steering and queued follow-ups inside the active agent lifecycle, so direct interactive input received while active makes that lifecycle eligible without leaking into the next run.
37 +Pi drains steering and queued follow-ups inside the active agent lifecycle.
38 +Interactive input received while active becomes a candidate, then a user `message_start` confirms Pi accepted it before making the lifecycle eligible.
39 +Handled active input therefore cannot create a notification or leak into the next run.
38 40 Pi dispatches extension events serially, so this is event-order bookkeeping rather than cross-thread synchronization.
39 41 State is cleared on completion and session shutdown.
40 42 Nothing is written to disk or session history.

extensions/os-notifier/README.md

Modified
21 21 - `session_start` attaches terminal focus tracking in TUI mode.
22 22 - `input` records candidate direct interactive input origin.
23 -- `before_agent_start` confirms accepted prompt input.
23 +- `before_agent_start` confirms accepted idle prompt input.
24 +- `message_start` confirms accepted interactive steering and follow-ups.
24 25 - `agent_start` starts duration measurement for the active lifecycle.
25 26 - `agent_end` records the latest low-level outcome.
26 27 - `agent_settled` sends the final eligible notification while unfocused.

extensions/os-notifier/__tests__/harness.test.ts

Modified
114 114 state.prepare();
115 115 state.start("tui");
116 116 state.input("interactive", "followUp");
117 + state.confirmUserMessage();
117 118 expect(state.settle()).toBe(true);
119 120 state.input("extension");
128 129 state.prepare();
129 130 state.start("tui");
130 131 state.input("interactive", "steer");
132 + state.confirmUserMessage();
131 133 expect(state.settle()).toBe(true);
136 + it("does not accept handled interactive input during an active run", () => {
137 + const state = new TurnEligibility();
138 + state.input("extension");
139 + state.prepare();
140 + state.start("tui");
141 + state.input("interactive", "steer");
142 + expect(state.settle()).toBe(false);
143 + });
134 145 it("preserves eligibility across low-level retries until settlement", () => {
135 146 const state = new TurnEligibility();
136 147 state.input("interactive");

extensions/os-notifier/index.ts

Modified
318 318 export class TurnEligibility {
319 319 private active = false;
320 320 private activeTui = false;
321 + private activeCandidate = false;
321 322 private current = false;
322 323 private pending = false;
323 324 private prepared = false;
325 326 input(source: InputSource, _behavior?: StreamingBehavior): void {
326 327 const eligible = source === "interactive";
327 328 if (this.active) {
328 - this.current ||= this.activeTui && eligible;
329 + this.activeCandidate = eligible;
329 330 return;
331 332 this.pending = eligible;
336 337 this.pending = false;
340 + confirmUserMessage(): void {
341 + this.current ||= this.activeTui && this.activeCandidate;
342 + this.activeCandidate = false;
339 345 start(mode: string): void {
340 346 if (this.active) return;
341 347 this.active = true;
347 353 settle(): boolean {
348 354 const eligible = this.current;
349 - this.active = false;
350 - this.activeTui = false;
351 - this.current = false;
352 - this.pending = false;
353 - this.prepared = false;
355 + this.reset();
354 356 return eligible;
357 359 clear(): void {
360 + this.reset();
363 + private reset(): void {
358 364 this.active = false;
359 365 this.activeTui = false;
366 + this.activeCandidate = false;
360 367 this.current = false;
361 368 this.pending = false;
362 369 this.prepared = false;
384 391 eligibility.prepare();
394 + pi.on("message_start", async (event) => {
395 + if (event.message.role === "user") eligibility.confirmUserMessage();
396 + });
387 398 pi.on("agent_start", async (_event, ctx) => {
388 399 eligibility.start(ctx.mode);
389 400 agentStartedAt ??= Date.now();