Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e57db3b
fix: end the run and empty the queue on screen when a bridge goes away
YishenTu Nov 25, 2026
b6ff058
refactor: Esc reads the chat view's run instead of a separate busy flag
YishenTu Nov 25, 2026
eb39986
fix: show a restarted pi's history preview until it's ready
YishenTu Nov 25, 2026
ca9b8f6
fix: don't restart a chat's pi after the user left it
YishenTu Nov 25, 2026
50370a4
fix: keep paused jobs past their time, through a queued alarm or a br…
YishenTu Nov 25, 2026
4ecf40b
refactor: wait for the open chat inside inOrder and count bridge requ…
YishenTu Nov 25, 2026
c3c5a84
docs: note that closing a connection ends its run and queue on screen
YishenTu Nov 25, 2026
52b7775
fix: a tool that never finished stops spinning once its run ends
YishenTu Nov 25, 2026
bf9f21f
refactor: the panel's port is one connected-or-waiting state
YishenTu Nov 25, 2026
9fc3a06
refactor: the Scheduled screen shows one mode, list or a job's form
YishenTu Nov 25, 2026
84efd69
refactor: the queue tracker's message in flight is one value, not a t…
YishenTu Nov 25, 2026
e23c489
refactor: ready updates the chat before its storage write
YishenTu Nov 25, 2026
d5d1026
refactor: held message-box text lives in its own module
YishenTu Nov 25, 2026
adec21d
fix: a panel that closed unreported can't interrupt the background, s…
YishenTu Nov 25, 2026
0e61a70
fix: log a held-text write that failed, and test that later ones stil…
YishenTu Nov 25, 2026
f8aeb7e
test: ordered requests all fail when startup does, and acks count mes…
YishenTu Nov 25, 2026
713aa1a
fix: schedule_job lists a paused job with no next run
YishenTu Nov 25, 2026
0dc8cf4
refactor: the chat view tracks only live tool calls
YishenTu Nov 25, 2026
48c4a6a
fix: text handed back as the last panel closes is held, not lost
YishenTu Nov 25, 2026
53ec100
refactor: the chat view forgets a run's tool calls once it ends
YishenTu Nov 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/background/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
- Settings are owned here (`settings.ts`): this is the only code that writes them, and it answers the side panel's `PanelQuestion`s (the @ menu's in `tabs.ts`).
- Post on every port (native and panel) with `postInParts` and read every port through `joinParts` (both in `src/parts.ts`). Chrome caps messages at 1 MB from the bridge and 64 MiB elsewhere; long chats and images go past both.
- Each chat with pi running has its own bridge connection, and a bridge's pi stays on one session for life. Route a bridge's events to the chat that opened it, never to whichever chat is on screen; tell chats apart only by their connection, never by timing or by counting replies.
- Switching chats never stops a run. A chat off screen keeps its bridge while its connection has work: the bridge's last report was busy (only the bridge knows its run, queue, input in flight and compaction), or the bridge hasn't yet reported on every request sent on that connection (`acked` < `sent`). Once neither holds, its bridge stops. Count every request posted to a bridge in `sent`, except replies to pi's tools (`browser_reply`, `schedule_reply`), which the bridge doesn't count either. Text for a chat's message box that no panel showing it can take waits in chrome.storage.local under that chat's id (`held`), not in memory. Never infer idleness from pi's events. Deleting a chat stops its bridge.
- Switching chats never stops a run. A chat off screen keeps its bridge while its connection has work: the bridge's last report was busy (only the bridge knows its run, queue, input in flight and compaction), or the bridge hasn't yet reported on every request sent on that connection (`acked` < `sent`). Once neither holds, its bridge stops. Count every request posted to a bridge in `sent`, except replies to pi's tools (`browser_reply`, `schedule_reply`), which the bridge doesn't count either. Text for a chat's message box that no panel showing it can take waits in chrome.storage.local under that chat's id (`held.ts`), not in memory. Never infer idleness from pi's events. Deleting a chat stops its bridge.
- A connection that closes ends what the panel shows for it: `closeConnection` sends a plain `run_end` (not `interrupted`, which only Stop sets) for a run left open, and an empty queue. The panel keeps no run or queue state of its own, so every close path goes through it.
- Chat bubbles come only from pi (`user` chunks). Don't echo a sent message: until pi answers (`prompt_result`) it is held in `unanswered` and goes back to the message box if the bridge goes away.
- Requests that change the chat or reach pi (prompt, chat switches, compact, model) run one at a time through `inOrder` in `pi-session.ts`, so a message still being stored can't land in the next chat. Stop, restart and queue edits (steer, edit, remove) skip it: they act on what is on screen now, and the bridge orders them against pi. Nothing queued may wait on pi.
- Scheduled jobs are owned here (`schedules.ts`): chrome.storage.local is the record, each job has a chrome.alarms alarm for its next run, re-armed on browser start. Alarm listeners are registered synchronously in `startBackground`. A run due before the browser session started (`chrome.storage.session`) was missed while it was closed and is skipped. `pi-session.ts` runs a due job (`runJob`): a check-back (check_back tool, one per chat) prompts its chat, any other job (schedule_job tool or the panel) a new chat; deleting a chat drops its check-back (Stop doesn't: the model set it, so only the model cancels it).
52 changes: 52 additions & 0 deletions src/background/held.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { ImageAttachment } from "../chat";
import { restoreOf } from "../restore";

// Text and images for a chat's message box that no panel showing that chat
// could take (none open, or the chat is off screen), by chat id, until one
// can. In chrome.storage.local: Chrome may stop the worker meanwhile.
// Changes run one at a time, so none overwrites another's.
export type Returned = { text: string; images?: ImageAttachment[] }[];
type Restore = NonNullable<ReturnType<typeof restoreOf>>;
type Held = Record<string, Returned>;

const KEY = "held";

let queue: Promise<void> = Promise.resolve();

// Runs `change` on what's held, in turn; it returns what to store, or
// nothing to leave it as it is. A change that fails (storage full, say) is
// lost, but the ones after it still run.
function update(change: (held: Held) => Held | undefined): Promise<void> {
queue = queue
.then(async () => {
const { [KEY]: held = {} } = await chrome.storage.local.get(KEY);
const next = change(held as Held);
if (next) await chrome.storage.local.set({ [KEY]: next });
})
// Not the text itself: it's the user's.
.catch((error: unknown) => console.warn("held text not saved:", error instanceof Error ? error.message : String(error)));
return queue;
}

export function hold(id: number, messages: Returned): void {
void update((held) => ({ ...held, [id]: [...(held[id] ?? []), ...messages] }));
}

// Offers what's held for the chat `id` names when its turn comes (the chat
// on screen then, not when asked) to `deliver`, and clears it only once
// `deliver` says a panel took it.
export function handOver(id: () => number, deliver: (restore: Restore) => boolean): Promise<void> {
return update((held) => {
const { [id()]: mine, ...rest } = held;
const restore = mine && restoreOf(mine);
return restore && deliver(restore) ? rest : undefined;
});
}

// Drops what's held for a deleted chat.
export function drop(id: number): void {
void update((held) => {
const { [id]: _, ...rest } = held;
return rest;
});
}
Loading