Skip to content

fix(sessions): leave the system prompt out of agent status reads - #1306

Merged
agegr merged 1 commit into
mainfrom
fix/issue-1302-slim-agent-status
Oct 18, 2026
Merged

agegr merged 1 commit into
mainfrom
fix/issue-1302-slim-agent-status

Conversation

@agegr

@agegr agegr commented Oct 16, 2026

Copy link
Copy Markdown
Owner

GET /api/agent/[id] returned the whole get_state snapshot, and the system
prompt (context files, skills, tool notes) is nearly all of it: 36.9 KB of
37.8 KB with a 25 KB AGENTS.md, ~325 KB of ~325 KB with a large one. The
browser reads that state after every completed assistant message (context
usage), on every event-stream connect, and in the settle, bash and
reconcile polls, none of which use the prompt. In a tool-heavy run that is
one full download per model call, fired without waiting for the previous
one, so on a slow link they pile up.

The route now drops systemPrompt for ?systemPrompt=0, and those status
reads go through agentStatusUrl(). agent_end's read, the mount's
/api/sessions/[id]/state and the system info view still fetch the prompt;
reconciliation's finish path, which used to take it from the poll, reads
it once when it settles a run whose agent_end was missed.

Fixes #1302

Testing

  • New app/api/agent/state-route.integration.test.mjs (real startRpcSession, temp agent dir, large AGENTS.md): full GET 325,013 B, status read 692 B, every other field unchanged.
  • hooks/context-usage.test.mjs checks the usage read uses ?systemPrompt=0.
  • Integrated with the other fix branches of this batch on main b017cd4: tsc --noEmit clean, npm test 2787 pass / 0 fail / 1 skipped.

🤖 Generated with Claude Code

GET /api/agent/[id] returned the whole get_state snapshot, and the system
prompt (context files, skills, tool notes) is nearly all of it: 36.9 KB of
37.8 KB with a 25 KB AGENTS.md, ~325 KB of ~325 KB with a large one. The
browser reads that state after every completed assistant message (context
usage), on every event-stream connect, and in the settle, bash and
reconcile polls, none of which use the prompt. In a tool-heavy run that is
one full download per model call, fired without waiting for the previous
one, so on a slow link they pile up.

The route now drops systemPrompt for ?systemPrompt=0, and those status
reads go through agentStatusUrl(). agent_end's read, the mount's
/api/sessions/[id]/state and the system info view still fetch the prompt;
reconciliation's finish path, which used to take it from the poll, reads
it once when it settles a run whose agent_end was missed.

Fixes #1302

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@agegr
agegr merged commit 4eed9e9 into main Oct 18, 2026
2 checks passed
agegr added a commit that referenced this pull request Oct 18, 2026
After #1306 status reads go through agentStatusUrl() (?systemPrompt=0).
The hidden-page release asked GET /api/agent/[id] with the full state
every grace per hidden tab; it is a status read too.

Refs #1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>
agegr added a commit that referenced this pull request Oct 20, 2026
* fix(sessions): let hidden pages release idle event streams

Over HTTP/1.1 a browser opens at most six connections per origin, shared
by all its tabs. Every pi-web tab kept the selected session's agent event
stream open even while idle (the warm-session effect; the idle grace in
scheduleEventStreamClose() skips the selected session), plus the active
file tab's watch while the right panel was open. A few background tabs
filled the pool, so Settings loads and message sync queued in the browser
until one tab closed.

A page hidden for HIDDEN_PAGE_GRACE_MS (30 s, hooks/usePageHidden.ts)
now asks GET /api/agent/[id] whether anything is under way: a run, a `!`
command, a compaction, an open extension dialog, or an unsettled subagent
at any depth (new subagentsActive, hasUnsettledSubagentRun()). Only when
nothing is does it close the selected session's stream
(canReleaseHiddenEventStream()); otherwise it checks again each grace. A
quick tab switch closes nothing, and a run that settles while hidden
starts the grace again, which covers an extension's follow-up right after
prompt_done. eventStreamReleasedRef keeps shouldMaintain() and the lease
renewal from reopening it. On return the stream reopens and the session
is read again with its state, applied as the mount does
(applyLoadedAgentState()), so work still under way shows as such; nothing
is resent.

The file watch pauses after the same grace. On reconnect the viewers
compare the file's size and mtime (now in type=meta and type=read, the
pair the watch's change events carry) and reload only a changed file, so
coming back keeps a text file's Load more chunks and scroll position,
media playback and a PDF's place.

What a released page no longer hears: a run started in the session after
the release (another device or tab, an extension timer, a subagent
notification between two checks) plays no chime and shows no in-page
completion or attention notification in that tab until it returns. Web
push still announces completion when subscribed. Terminal streams are
left alone: the server kills a shell that has had no listener for two
minutes. Feedback in the UI when requests are stuck is not addressed.

Refs #1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): bound the hidden-page idle check; count pending subagent notices

Second review follow-up to the hidden-page release.

The release waited on GET /api/agent/[id] with no timeout and kept the
stream when no reply came. In the very state #1283 is about, a pool
already full of streams, that request queues in the browser behind them
and never leaves, so no hidden tab ever let go. The check now aborts
after HIDDEN_RELEASE_CHECK_TIMEOUT_MS (5 s); no answer in time, like no
network, releases (canReleaseHiddenEventStream("unanswered")). An error
reply from the server still keeps the stream until the next grace.

A background subagent run leaves the run map before notifyParent() starts
the parent's turn (after waitUntilReady() and the idle poll), so a check
in that window saw no subagent and released. notifyParent() now records
the delivery until it is sent or fails, and hasUnsettledSubagentRun()
counts it.

The text viewer skipped the git diff along with the content on an
unchanged reconnect, leaving the gutter stale after a commit made while
hidden; it reads the diff again.

Refs #1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): read the hidden-page idle check without the system prompt

After #1306 status reads go through agentStatusUrl() (?systemPrompt=0).
The hidden-page release asked GET /api/agent/[id] with the full state
every grace per hidden tab; it is a status read too.

Refs #1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): keep a released page's session alive on a lease of its own

The event stream's lease is what keeps the selected session's idle wrapper
from being reaped (#705 liveness, "keep selected sessions alive"). A hidden
page that let go of the stream also let go of that lease and stopped
renewing, so ten minutes later (PI_WEB_IDLE_TIMEOUT_MS) the wrapper shut
down: an extension's timer in it never fired, and nothing ran to announce
by push either, while the session was still selected in that tab.

The page now holds a lease of its own in the stream's place, at the release
and on each renewal tick (POST /api/agent/[id]/lease?holder=<page id>,
holdSessionLivenessLease()): one per page and session, ended a TTL after the
page stops renewing it. The return renews the stream's lease again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): keep the selected session alive on the page's lease, not its stream

The selected session's liveness lease was carried by its event stream, so
a hidden page that let go of the stream (#1283) let go of the session too;
539e4cc patched that with a second, page-held lease taken only at the
release. The lease now belongs to the page alone: POST
/api/agent/[id]/lease?holder=<page id> creates or renews one lease per
page and session (holdSessionLivenessLease()), at selection, every 30 s and
on return, whether or not the stream is open. A closed tab's lease ends a
TTL (90 s) after its last renewal. The stream no longer takes a lease, and
the route answers 400 without a holder.

A renewal that found no lease used to mean a dead stream and reconnected
it. The page's own hold always finds one, so that probe could no longer
see a dead stream. The server now sends its 30 s heartbeat as a data
event ({ type: "heartbeat" }; an SSE comment never reaches EventSource),
and AgentEventConnection drops a ready stream silent for 75 s and
reconnects (staleAfterMs), which catches half-open connections directly.
The heartbeat never reaches onEvent.

Refs #1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): let the lease reply decide when a hidden page lets go of its stream and when it opens it again

The page already renews its selected session's lease every 30 s whether or
not it holds the event stream. Its reply now says what is under way there,
read from the wrapper alone (never starting one, never resetting its idle
timer): running (isRunning(): a prompt, a queued one, a `!` command, a
compaction), awaitingInput (an extension dialog waiting for an answer) and
subagentsActive (hasUnsettledSubagentRun()).

That one reply drives both ends of the release:
- At the end of the hidden page's grace, the release check is a lease
  request (shared with the renewal tick when one is in flight) instead of
  GET /api/agent/[id]; the stream closes only if the reply shows nothing
  under way. No reply within SESSION_LEASE_TIMEOUT_MS (5 s) still releases.
  GET /api/agent/[id] is back to main's.
- While released and still hidden, a renewal reply that shows activity
  opens the stream again and reads the session, so a run another tab, a
  device or an extension's timer starts there, and an extension dialog,
  again reach the hidden page with their chime and browser notifications.
  The reopen arms the grace again, so the stream is let go a grace after
  the activity ends.

A run that starts and settles between two renewals is still missed on the
hidden page; web push covers its completion when subscribed.

Refs #1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@agegr
agegr deleted the fix/issue-1302-slim-agent-status branch October 29, 2026 13:01
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
…gr#1306)

GET /api/agent/[id] returned the whole get_state snapshot, and the system
prompt (context files, skills, tool notes) is nearly all of it: 36.9 KB of
37.8 KB with a 25 KB AGENTS.md, ~325 KB of ~325 KB with a large one. The
browser reads that state after every completed assistant message (context
usage), on every event-stream connect, and in the settle, bash and
reconcile polls, none of which use the prompt. In a tool-heavy run that is
one full download per model call, fired without waiting for the previous
one, so on a slow link they pile up.

The route now drops systemPrompt for ?systemPrompt=0, and those status
reads go through agentStatusUrl(). agent_end's read, the mount's
/api/sessions/[id]/state and the system info view still fetch the prompt;
reconciliation's finish path, which used to take it from the poll, reads
it once when it settles a run whose agent_end was missed.

Fixes agegr#1302

Co-authored-by: Claude Opus 5.5 <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
* fix(sessions): let hidden pages release idle event streams

Over HTTP/1.1 a browser opens at most six connections per origin, shared
by all its tabs. Every pi-web tab kept the selected session's agent event
stream open even while idle (the warm-session effect; the idle grace in
scheduleEventStreamClose() skips the selected session), plus the active
file tab's watch while the right panel was open. A few background tabs
filled the pool, so Settings loads and message sync queued in the browser
until one tab closed.

A page hidden for HIDDEN_PAGE_GRACE_MS (30 s, hooks/usePageHidden.ts)
now asks GET /api/agent/[id] whether anything is under way: a run, a `!`
command, a compaction, an open extension dialog, or an unsettled subagent
at any depth (new subagentsActive, hasUnsettledSubagentRun()). Only when
nothing is does it close the selected session's stream
(canReleaseHiddenEventStream()); otherwise it checks again each grace. A
quick tab switch closes nothing, and a run that settles while hidden
starts the grace again, which covers an extension's follow-up right after
prompt_done. eventStreamReleasedRef keeps shouldMaintain() and the lease
renewal from reopening it. On return the stream reopens and the session
is read again with its state, applied as the mount does
(applyLoadedAgentState()), so work still under way shows as such; nothing
is resent.

The file watch pauses after the same grace. On reconnect the viewers
compare the file's size and mtime (now in type=meta and type=read, the
pair the watch's change events carry) and reload only a changed file, so
coming back keeps a text file's Load more chunks and scroll position,
media playback and a PDF's place.

What a released page no longer hears: a run started in the session after
the release (another device or tab, an extension timer, a subagent
notification between two checks) plays no chime and shows no in-page
completion or attention notification in that tab until it returns. Web
push still announces completion when subscribed. Terminal streams are
left alone: the server kills a shell that has had no listener for two
minutes. Feedback in the UI when requests are stuck is not addressed.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): bound the hidden-page idle check; count pending subagent notices

Second review follow-up to the hidden-page release.

The release waited on GET /api/agent/[id] with no timeout and kept the
stream when no reply came. In the very state agegr#1283 is about, a pool
already full of streams, that request queues in the browser behind them
and never leaves, so no hidden tab ever let go. The check now aborts
after HIDDEN_RELEASE_CHECK_TIMEOUT_MS (5 s); no answer in time, like no
network, releases (canReleaseHiddenEventStream("unanswered")). An error
reply from the server still keeps the stream until the next grace.

A background subagent run leaves the run map before notifyParent() starts
the parent's turn (after waitUntilReady() and the idle poll), so a check
in that window saw no subagent and released. notifyParent() now records
the delivery until it is sent or fails, and hasUnsettledSubagentRun()
counts it.

The text viewer skipped the git diff along with the content on an
unchanged reconnect, leaving the gutter stale after a commit made while
hidden; it reads the diff again.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): read the hidden-page idle check without the system prompt

After agegr#1306 status reads go through agentStatusUrl() (?systemPrompt=0).
The hidden-page release asked GET /api/agent/[id] with the full state
every grace per hidden tab; it is a status read too.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): keep a released page's session alive on a lease of its own

The event stream's lease is what keeps the selected session's idle wrapper
from being reaped (agegr#705 liveness, "keep selected sessions alive"). A hidden
page that let go of the stream also let go of that lease and stopped
renewing, so ten minutes later (PI_WEB_IDLE_TIMEOUT_MS) the wrapper shut
down: an extension's timer in it never fired, and nothing ran to announce
by push either, while the session was still selected in that tab.

The page now holds a lease of its own in the stream's place, at the release
and on each renewal tick (POST /api/agent/[id]/lease?holder=<page id>,
holdSessionLivenessLease()): one per page and session, ended a TTL after the
page stops renewing it. The return renews the stream's lease again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): keep the selected session alive on the page's lease, not its stream

The selected session's liveness lease was carried by its event stream, so
a hidden page that let go of the stream (agegr#1283) let go of the session too;
d6e726062 patched that with a second, page-held lease taken only at the
release. The lease now belongs to the page alone: POST
/api/agent/[id]/lease?holder=<page id> creates or renews one lease per
page and session (holdSessionLivenessLease()), at selection, every 30 s and
on return, whether or not the stream is open. A closed tab's lease ends a
TTL (90 s) after its last renewal. The stream no longer takes a lease, and
the route answers 400 without a holder.

A renewal that found no lease used to mean a dead stream and reconnected
it. The page's own hold always finds one, so that probe could no longer
see a dead stream. The server now sends its 30 s heartbeat as a data
event ({ type: "heartbeat" }; an SSE comment never reaches EventSource),
and AgentEventConnection drops a ready stream silent for 75 s and
reconnects (staleAfterMs), which catches half-open connections directly.
The heartbeat never reaches onEvent.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): let the lease reply decide when a hidden page lets go of its stream and when it opens it again

The page already renews its selected session's lease every 30 s whether or
not it holds the event stream. Its reply now says what is under way there,
read from the wrapper alone (never starting one, never resetting its idle
timer): running (isRunning(): a prompt, a queued one, a `!` command, a
compaction), awaitingInput (an extension dialog waiting for an answer) and
subagentsActive (hasUnsettledSubagentRun()).

That one reply drives both ends of the release:
- At the end of the hidden page's grace, the release check is a lease
  request (shared with the renewal tick when one is in flight) instead of
  GET /api/agent/[id]; the stream closes only if the reply shows nothing
  under way. No reply within SESSION_LEASE_TIMEOUT_MS (5 s) still releases.
  GET /api/agent/[id] is back to main's.
- While released and still hidden, a renewal reply that shows activity
  opens the stream again and reads the session, so a run another tab, a
  device or an extension's timer starts there, and an extension dialog,
  again reach the hidden page with their chime and browser notifications.
  The reopen arms the grace again, so the stream is let go a grace after
  the activity ends.

A run that starts and settles between two renewals is still missed on the
hidden page; web push covers its completion when subscribed.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
…gr#1306)

GET /api/agent/[id] returned the whole get_state snapshot, and the system
prompt (context files, skills, tool notes) is nearly all of it: 36.9 KB of
37.8 KB with a 25 KB AGENTS.md, ~325 KB of ~325 KB with a large one. The
browser reads that state after every completed assistant message (context
usage), on every event-stream connect, and in the settle, bash and
reconcile polls, none of which use the prompt. In a tool-heavy run that is
one full download per model call, fired without waiting for the previous
one, so on a slow link they pile up.

The route now drops systemPrompt for ?systemPrompt=0, and those status
reads go through agentStatusUrl(). agent_end's read, the mount's
/api/sessions/[id]/state and the system info view still fetch the prompt;
reconciliation's finish path, which used to take it from the poll, reads
it once when it settles a run whose agent_end was missed.

Fixes agegr#1302

Co-authored-by: Claude Opus 5.5 <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
* fix(sessions): let hidden pages release idle event streams

Over HTTP/1.1 a browser opens at most six connections per origin, shared
by all its tabs. Every pi-web tab kept the selected session's agent event
stream open even while idle (the warm-session effect; the idle grace in
scheduleEventStreamClose() skips the selected session), plus the active
file tab's watch while the right panel was open. A few background tabs
filled the pool, so Settings loads and message sync queued in the browser
until one tab closed.

A page hidden for HIDDEN_PAGE_GRACE_MS (30 s, hooks/usePageHidden.ts)
now asks GET /api/agent/[id] whether anything is under way: a run, a `!`
command, a compaction, an open extension dialog, or an unsettled subagent
at any depth (new subagentsActive, hasUnsettledSubagentRun()). Only when
nothing is does it close the selected session's stream
(canReleaseHiddenEventStream()); otherwise it checks again each grace. A
quick tab switch closes nothing, and a run that settles while hidden
starts the grace again, which covers an extension's follow-up right after
prompt_done. eventStreamReleasedRef keeps shouldMaintain() and the lease
renewal from reopening it. On return the stream reopens and the session
is read again with its state, applied as the mount does
(applyLoadedAgentState()), so work still under way shows as such; nothing
is resent.

The file watch pauses after the same grace. On reconnect the viewers
compare the file's size and mtime (now in type=meta and type=read, the
pair the watch's change events carry) and reload only a changed file, so
coming back keeps a text file's Load more chunks and scroll position,
media playback and a PDF's place.

What a released page no longer hears: a run started in the session after
the release (another device or tab, an extension timer, a subagent
notification between two checks) plays no chime and shows no in-page
completion or attention notification in that tab until it returns. Web
push still announces completion when subscribed. Terminal streams are
left alone: the server kills a shell that has had no listener for two
minutes. Feedback in the UI when requests are stuck is not addressed.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): bound the hidden-page idle check; count pending subagent notices

Second review follow-up to the hidden-page release.

The release waited on GET /api/agent/[id] with no timeout and kept the
stream when no reply came. In the very state agegr#1283 is about, a pool
already full of streams, that request queues in the browser behind them
and never leaves, so no hidden tab ever let go. The check now aborts
after HIDDEN_RELEASE_CHECK_TIMEOUT_MS (5 s); no answer in time, like no
network, releases (canReleaseHiddenEventStream("unanswered")). An error
reply from the server still keeps the stream until the next grace.

A background subagent run leaves the run map before notifyParent() starts
the parent's turn (after waitUntilReady() and the idle poll), so a check
in that window saw no subagent and released. notifyParent() now records
the delivery until it is sent or fails, and hasUnsettledSubagentRun()
counts it.

The text viewer skipped the git diff along with the content on an
unchanged reconnect, leaving the gutter stale after a commit made while
hidden; it reads the diff again.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): read the hidden-page idle check without the system prompt

After agegr#1306 status reads go through agentStatusUrl() (?systemPrompt=0).
The hidden-page release asked GET /api/agent/[id] with the full state
every grace per hidden tab; it is a status read too.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): keep a released page's session alive on a lease of its own

The event stream's lease is what keeps the selected session's idle wrapper
from being reaped (agegr#705 liveness, "keep selected sessions alive"). A hidden
page that let go of the stream also let go of that lease and stopped
renewing, so ten minutes later (PI_WEB_IDLE_TIMEOUT_MS) the wrapper shut
down: an extension's timer in it never fired, and nothing ran to announce
by push either, while the session was still selected in that tab.

The page now holds a lease of its own in the stream's place, at the release
and on each renewal tick (POST /api/agent/[id]/lease?holder=<page id>,
holdSessionLivenessLease()): one per page and session, ended a TTL after the
page stops renewing it. The return renews the stream's lease again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): keep the selected session alive on the page's lease, not its stream

The selected session's liveness lease was carried by its event stream, so
a hidden page that let go of the stream (agegr#1283) let go of the session too;
d6e726062 patched that with a second, page-held lease taken only at the
release. The lease now belongs to the page alone: POST
/api/agent/[id]/lease?holder=<page id> creates or renews one lease per
page and session (holdSessionLivenessLease()), at selection, every 30 s and
on return, whether or not the stream is open. A closed tab's lease ends a
TTL (90 s) after its last renewal. The stream no longer takes a lease, and
the route answers 400 without a holder.

A renewal that found no lease used to mean a dead stream and reconnected
it. The page's own hold always finds one, so that probe could no longer
see a dead stream. The server now sends its 30 s heartbeat as a data
event ({ type: "heartbeat" }; an SSE comment never reaches EventSource),
and AgentEventConnection drops a ready stream silent for 75 s and
reconnects (staleAfterMs), which catches half-open connections directly.
The heartbeat never reaches onEvent.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): let the lease reply decide when a hidden page lets go of its stream and when it opens it again

The page already renews its selected session's lease every 30 s whether or
not it holds the event stream. Its reply now says what is under way there,
read from the wrapper alone (never starting one, never resetting its idle
timer): running (isRunning(): a prompt, a queued one, a `!` command, a
compaction), awaitingInput (an extension dialog waiting for an answer) and
subagentsActive (hasUnsettledSubagentRun()).

That one reply drives both ends of the release:
- At the end of the hidden page's grace, the release check is a lease
  request (shared with the renewal tick when one is in flight) instead of
  GET /api/agent/[id]; the stream closes only if the reply shows nothing
  under way. No reply within SESSION_LEASE_TIMEOUT_MS (5 s) still releases.
  GET /api/agent/[id] is back to main's.
- While released and still hidden, a renewal reply that shows activity
  opens the stream again and reads the session, so a run another tab, a
  device or an extension's timer starts there, and an extension dialog,
  again reach the hidden page with their chime and browser notifications.
  The reopen arms the grace again, so the stream is let go a grace after
  the activity ends.

A run that starts and settles between two renewals is still missed on the
hidden page; web push covers its completion when subscribed.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
* fix(sessions): let hidden pages release idle event streams

Over HTTP/1.1 a browser opens at most six connections per origin, shared
by all its tabs. Every pi-web tab kept the selected session's agent event
stream open even while idle (the warm-session effect; the idle grace in
scheduleEventStreamClose() skips the selected session), plus the active
file tab's watch while the right panel was open. A few background tabs
filled the pool, so Settings loads and message sync queued in the browser
until one tab closed.

A page hidden for HIDDEN_PAGE_GRACE_MS (30 s, hooks/usePageHidden.ts)
now asks GET /api/agent/[id] whether anything is under way: a run, a `!`
command, a compaction, an open extension dialog, or an unsettled subagent
at any depth (new subagentsActive, hasUnsettledSubagentRun()). Only when
nothing is does it close the selected session's stream
(canReleaseHiddenEventStream()); otherwise it checks again each grace. A
quick tab switch closes nothing, and a run that settles while hidden
starts the grace again, which covers an extension's follow-up right after
prompt_done. eventStreamReleasedRef keeps shouldMaintain() and the lease
renewal from reopening it. On return the stream reopens and the session
is read again with its state, applied as the mount does
(applyLoadedAgentState()), so work still under way shows as such; nothing
is resent.

The file watch pauses after the same grace. On reconnect the viewers
compare the file's size and mtime (now in type=meta and type=read, the
pair the watch's change events carry) and reload only a changed file, so
coming back keeps a text file's Load more chunks and scroll position,
media playback and a PDF's place.

What a released page no longer hears: a run started in the session after
the release (another device or tab, an extension timer, a subagent
notification between two checks) plays no chime and shows no in-page
completion or attention notification in that tab until it returns. Web
push still announces completion when subscribed. Terminal streams are
left alone: the server kills a shell that has had no listener for two
minutes. Feedback in the UI when requests are stuck is not addressed.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): bound the hidden-page idle check; count pending subagent notices

Second review follow-up to the hidden-page release.

The release waited on GET /api/agent/[id] with no timeout and kept the
stream when no reply came. In the very state agegr#1283 is about, a pool
already full of streams, that request queues in the browser behind them
and never leaves, so no hidden tab ever let go. The check now aborts
after HIDDEN_RELEASE_CHECK_TIMEOUT_MS (5 s); no answer in time, like no
network, releases (canReleaseHiddenEventStream("unanswered")). An error
reply from the server still keeps the stream until the next grace.

A background subagent run leaves the run map before notifyParent() starts
the parent's turn (after waitUntilReady() and the idle poll), so a check
in that window saw no subagent and released. notifyParent() now records
the delivery until it is sent or fails, and hasUnsettledSubagentRun()
counts it.

The text viewer skipped the git diff along with the content on an
unchanged reconnect, leaving the gutter stale after a commit made while
hidden; it reads the diff again.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): read the hidden-page idle check without the system prompt

After agegr#1306 status reads go through agentStatusUrl() (?systemPrompt=0).
The hidden-page release asked GET /api/agent/[id] with the full state
every grace per hidden tab; it is a status read too.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): keep a released page's session alive on a lease of its own

The event stream's lease is what keeps the selected session's idle wrapper
from being reaped (agegr#705 liveness, "keep selected sessions alive"). A hidden
page that let go of the stream also let go of that lease and stopped
renewing, so ten minutes later (PI_WEB_IDLE_TIMEOUT_MS) the wrapper shut
down: an extension's timer in it never fired, and nothing ran to announce
by push either, while the session was still selected in that tab.

The page now holds a lease of its own in the stream's place, at the release
and on each renewal tick (POST /api/agent/[id]/lease?holder=<page id>,
holdSessionLivenessLease()): one per page and session, ended a TTL after the
page stops renewing it. The return renews the stream's lease again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): keep the selected session alive on the page's lease, not its stream

The selected session's liveness lease was carried by its event stream, so
a hidden page that let go of the stream (agegr#1283) let go of the session too;
539e4cc patched that with a second, page-held lease taken only at the
release. The lease now belongs to the page alone: POST
/api/agent/[id]/lease?holder=<page id> creates or renews one lease per
page and session (holdSessionLivenessLease()), at selection, every 30 s and
on return, whether or not the stream is open. A closed tab's lease ends a
TTL (90 s) after its last renewal. The stream no longer takes a lease, and
the route answers 400 without a holder.

A renewal that found no lease used to mean a dead stream and reconnected
it. The page's own hold always finds one, so that probe could no longer
see a dead stream. The server now sends its 30 s heartbeat as a data
event ({ type: "heartbeat" }; an SSE comment never reaches EventSource),
and AgentEventConnection drops a ready stream silent for 75 s and
reconnects (staleAfterMs), which catches half-open connections directly.
The heartbeat never reaches onEvent.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(sessions): let the lease reply decide when a hidden page lets go of its stream and when it opens it again

The page already renews its selected session's lease every 30 s whether or
not it holds the event stream. Its reply now says what is under way there,
read from the wrapper alone (never starting one, never resetting its idle
timer): running (isRunning(): a prompt, a queued one, a `!` command, a
compaction), awaitingInput (an extension dialog waiting for an answer) and
subagentsActive (hasUnsettledSubagentRun()).

That one reply drives both ends of the release:
- At the end of the hidden page's grace, the release check is a lease
  request (shared with the renewal tick when one is in flight) instead of
  GET /api/agent/[id]; the stream closes only if the reply shows nothing
  under way. No reply within SESSION_LEASE_TIMEOUT_MS (5 s) still releases.
  GET /api/agent/[id] is back to main's.
- While released and still hidden, a renewal reply that shows activity
  opens the stream again and reads the session, so a run another tab, a
  device or an extension's timer starts there, and an extension dialog,
  again reach the hidden page with their chime and browser notifications.
  The reopen arms the grace again, so the stream is let go a grace after
  the activity ends.

A run that starts and settles between two renewals is still missed on the
hidden page; web push covers its completion when subscribed.

Refs agegr#1283

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent status polling re-downloads the full ~350 KB payload every ~1.5s; add ETag/304 or adaptive polling

1 participant