Skip to content
Merged
Show file tree
Hide file tree
Changes from 72 commits
Commits
Show all changes
140 commits
Select commit Hold shift + click to select a range
6732d57
fix(agent-hooks): record the exit when a hookless agent quits to its …
brennanb2025 Nov 16, 2026
d70520f
fix(agent-hooks): keep shell-return exits to process-less rows
brennanb2025 Nov 16, 2026
b6640ad
Merge remote-tracking branch 'origin/main' into brennanb2025/codex-qu…
brennanb2025 Nov 16, 2026
3e22e83
test: type the provider session in the hookless quit test
brennanb2025 Nov 16, 2026
0f06dac
Keep pane conversation identity through sleep and wake
brennanb2025 Nov 16, 2026
232e5a5
Read canonical pane identity and preserve saved turn verdicts
brennanb2025 Nov 16, 2026
dbd2422
Merge remote-tracking branch 'origin/brennanb2025/codex-quit-after-wa…
brennanb2025 Nov 16, 2026
15993dc
Retire exited pane recovery before exit evidence expires
brennanb2025 Nov 16, 2026
86f4612
Complete canonical sleep settlement and skip empty recovery edits
brennanb2025 Nov 16, 2026
53d80c7
Keep wake continuity scoped to resume boundaries and satisfy source g…
brennanb2025 Nov 16, 2026
1ea5808
fix(agent-hooks): record the exit when main sees the launched agent's…
brennanb2025 Nov 16, 2026
bab7e7a
Spy on durable mutations in the exit performance contract
brennanb2025 Nov 16, 2026
153a248
Exercise Interrupted through an idle wake and second sleep
brennanb2025 Nov 16, 2026
21de434
Scope sleep notification assertions to the sleep transition
brennanb2025 Nov 16, 2026
4dc3a6b
test: expect the confirmed-exit flag on launch retirement
brennanb2025 Nov 16, 2026
0cc34b7
Preserve saved turn verdicts and keep sleep replay out of completion …
brennanb2025 Nov 16, 2026
b6889c6
Supply renderer history when checking recovered completion eligibility
brennanb2025 Nov 16, 2026
8ff883a
fix(agent-hooks): don't treat a suspended launch as an exit
brennanb2025 Nov 16, 2026
03ac47c
fix(agent-hooks): keep a suspended agent's launch held
brennanb2025 Nov 16, 2026
12bdc5e
Revert "fix(agent-hooks): keep a suspended agent's launch held"
brennanb2025 Nov 16, 2026
c44caaf
Run the sleep runtime integration on the native test worker
brennanb2025 Nov 16, 2026
67bf14e
Merge remote-tracking branch 'origin/brennanb2025/codex-quit-after-wa…
brennanb2025 Nov 16, 2026
9964ec4
Preserve recovered turns on wake and avoid resuming shared agent proc…
brennanb2025 Nov 16, 2026
59b0a97
Merge remote-tracking branch 'origin/main' into brennanb2025/codex-qu…
brennanb2025 Nov 16, 2026
a9a8262
Keep accepted turns through startup restatements and separate sleep f…
brennanb2025 Nov 16, 2026
be6b47a
Merge remote-tracking branch 'origin/brennanb2025/codex-quit-after-wa…
brennanb2025 Nov 16, 2026
80e564a
Merge remote-tracking branch 'origin/main' into brennanb2025/sleep-pa…
brennanb2025 Nov 16, 2026
ea1d677
Follow the process observation module moved on main
brennanb2025 Nov 16, 2026
9072b20
Keep status ingestion within the module size gate
brennanb2025 Nov 16, 2026
c317afe
Replace accepted rows when observing a surviving tmux host
brennanb2025 Nov 16, 2026
4145992
Merge remote-tracking branch 'origin/brennanb2025/codex-quit-after-wa…
brennanb2025 Nov 16, 2026
2990d81
Consult pane ownership before the hook-row restore fallback
brennanb2025 Nov 16, 2026
75ee24c
Keep accepted turns and process ownership through startup reports
brennanb2025 Nov 16, 2026
504bd3c
Keep shared conversation ownership through both sleep cleanup paths
brennanb2025 Nov 16, 2026
803f8cd
Record a shell-confirmed agent exit only for pane-owned processes, pr…
brennanb2025 Nov 16, 2026
90736df
Type the proving test's fixtures
brennanb2025 Nov 16, 2026
66a630c
Preserve stopped agent recovery and detached shared ownership
brennanb2025 Nov 16, 2026
3b33928
Retire quit agent recovery without conflating shell foreground and exit
brennanb2025 Nov 16, 2026
75c61ad
Route quiet shell confirmation through the current launch owner
brennanb2025 Nov 16, 2026
f7f8084
Keep suspended Codex recovery through listings and release detached t…
brennanb2025 Nov 16, 2026
d130f34
Release tmux-hosted checkpoints after a restart and leave verifiable …
brennanb2025 Nov 16, 2026
7443f5e
Prove unmarked exits with the host's shell-alone check and retire sha…
brennanb2025 Nov 16, 2026
e767c7f
Let a verifiable owner's presence probe answer a quiet shell
brennanb2025 Nov 16, 2026
de0164b
Keep Windows unmarked quits provable and probe tmux-shown checkpoints…
brennanb2025 Nov 16, 2026
611e3b5
Prove Windows unmarked quits from the host's job census
brennanb2025 Nov 16, 2026
fe98eed
Pin the Windows exit proof to an explicit child census
brennanb2025 Nov 16, 2026
6b4b56e
Refuse a running background job as exit proof on the local shell check
brennanb2025 Nov 16, 2026
ebc6529
Merge remote-tracking branch 'origin/main' into brennanb2025/sleep-pa…
brennanb2025 Nov 16, 2026
86ac1eb
Mark a tmux-hosted conversation shared from its own hook
brennanb2025 Nov 17, 2026
11d85c4
Fix gate findings: pin test clock, drop renderer-clearing tests the m…
brennanb2025 Nov 17, 2026
0407041
Retire an exited record without copying the session; probe tmux hosts…
brennanb2025 Nov 17, 2026
a62ae42
Merge remote-tracking branch 'origin/main' into brennanb2025/sleep-pa…
brennanb2025 Nov 17, 2026
36898d2
Keep the legacy tmux sleep test's harness beside it after main remove…
brennanb2025 Nov 17, 2026
0d80c2b
Retire an exited record through the store's single-record retirement …
brennanb2025 Nov 17, 2026
7e38fe5
Place the saved-resume retirement store test above the comment it dis…
brennanb2025 Nov 17, 2026
efdae4c
Merge remote-tracking branch 'origin/main' into brennanb2025/sleep-pa…
brennanb2025 Nov 17, 2026
f62e0d4
Format the sleep handoff tests after main's directory move
brennanb2025 Nov 17, 2026
15c6ec5
Close new-vs-main gaps: tmux proofs, shared read budget, sleep verdic…
brennanb2025 Nov 18, 2026
147aef2
Fix new-test types; forward the shell-alone proof option through the …
brennanb2025 Nov 18, 2026
a8e5278
End a resumed Claude's startup window at its SessionStart; swallow on…
brennanb2025 Nov 18, 2026
da01c2a
Revert "End a resumed Claude's startup window at its SessionStart; sw…
brennanb2025 Nov 18, 2026
3e42b2a
End a resumed agent's startup window at the user's first submission; …
brennanb2025 Nov 18, 2026
59b0ab4
Let a sleep replay carry a finished main agent's completion
brennanb2025 Nov 18, 2026
1051d39
Record a submitted line for every pane so remote panes end a resumed …
brennanb2025 Nov 18, 2026
3aa32e5
Count only Enter as a submission, prune it with the pane, and refuse …
brennanb2025 Nov 18, 2026
d115983
Keep paste framing across writes and drop submission marks of closed …
brennanb2025 Nov 18, 2026
6064a05
Forget a closed tab's submission marks with its other pane caches
brennanb2025 Nov 18, 2026
82d4299
Judge a submission from the write alone and bound remembered submissions
brennanb2025 Nov 18, 2026
e661841
Count an Enter in merged keystroke writes
brennanb2025 Nov 18, 2026
0525f63
Show terminal work after a wake as main does: drop the startup swallo…
brennanb2025 Nov 18, 2026
4d60e7a
Merge remote-tracking branch 'origin/main' into brennanb2025/prb-resume
brennanb2025 Nov 18, 2026
d0b4b6d
fix(sleep): keep main's behavior for six review findings
brennanb2025 Nov 18, 2026
49a3ec2
Merge remote-tracking branch 'origin/main' into brennanb2025/prb-resume
brennanb2025 Nov 18, 2026
f02dad8
Merge remote-tracking branch 'origin/main' into brennanb2025/prb-resume
brennanb2025 Nov 19, 2026
b1f0bd9
Merge remote-tracking branch 'origin/main' into brennanb2025/prb-resume
brennanb2025 Nov 19, 2026
ced7e47
fix(sleep): count tmux's unambiguous attach prefixes as attaching
brennanb2025 Nov 19, 2026
5f70f95
fix(sleep): read grouped tmux flags when proving an attached client
brennanb2025 Nov 19, 2026
f557ee1
perf(sleep): index each PTY inventory snapshot once for per-pane lookups
brennanb2025 Nov 19, 2026
17b4a4b
fix(sleep): a foreground tmux server is no attached client; index inv…
brennanb2025 Nov 19, 2026
55678ae
fix(sleep): repeat one pane's inventory lookup from memory; -V/-h are…
brennanb2025 Nov 19, 2026
ec7a1d0
fix(sleep): index the PTY inventory only as far as lookups scan; new-…
brennanb2025 Nov 19, 2026
8d8443e
fix(sleep): only a tmux client's harmless flags may precede an attach…
brennanb2025 Nov 19, 2026
6f3a93f
perf(sleep): keep main's inventory lookups; index only for sleep capt…
brennanb2025 Nov 19, 2026
87dc6ed
refactor(sleep): record tmux/shared topology when the hook row is wri…
brennanb2025 Nov 19, 2026
c1dfd24
fix(sleep): an SSH pane's returning shell retires its record, as on main
brennanb2025 Nov 19, 2026
8bba1f8
fix(sleep): prove an agent's exit from the PTY's root shell in a fres…
brennanb2025 Nov 19, 2026
2ce5f30
fix(sleep): a command mark ends a process-less agent only on the host…
brennanb2025 Nov 19, 2026
53b39c1
test: name the session cast in the SSH shell-return test
brennanb2025 Nov 19, 2026
ab9caa7
fix(sleep): persist shared topology, prove exits from login-rooted PT…
brennanb2025 Nov 19, 2026
02d6d8d
fix(sleep): prove unseen agent exits per pane without rescanning, and…
brennanb2025 Nov 19, 2026
d4c3224
fix(sleep): prove the quit of an agent typed at the prompt, and keep …
brennanb2025 Nov 19, 2026
7e60394
perf(sleep): read an exit proof's root PID from its PTY alone, not ev…
brennanb2025 Nov 19, 2026
4297ba1
fix(sleep): read hydrated sharedHost through a type guard; expect job…
brennanb2025 Nov 19, 2026
eeb3729
perf(sleep): keep each live PTY's root PID until its teardown instead…
brennanb2025 Nov 19, 2026
138d13f
fix(sleep): only the pane's own stopped agent keeps its exit unproven
brennanb2025 Nov 19, 2026
f954dfa
perf(sleep): take an exit proof's root PID from the PID its PTY publi…
brennanb2025 Nov 19, 2026
1948bb6
fix(sleep): a proof doesn't count once input reached the PTY, and lau…
brennanb2025 Nov 19, 2026
dcd0c2f
perf(sleep): a PTY whose host published no PID pays one root inventor…
brennanb2025 Nov 19, 2026
b8d1fc5
refactor(sleep): a command mark asks the host whether the agent ended…
brennanb2025 Nov 19, 2026
3f1fb88
fix(sleep): sleep reads the quit its exit proof is still deciding; pl…
brennanb2025 Nov 19, 2026
0113e95
fix(sleep): CLI and mobile input void a running exit proof; sleep wai…
brennanb2025 Nov 19, 2026
84a99cd
fix(sleep): a WSL or SSH agent's command mark ends it as on main
brennanb2025 Nov 19, 2026
a551ae1
perf(sleep): a quit checkpoint reads terminal identity once for all s…
brennanb2025 Nov 19, 2026
0dec9ec
fix(sleep): query replies don't void exit proofs; launchless marks ar…
brennanb2025 Nov 19, 2026
30d88e2
fix(sleep): a trusted WSL or SSH command mark retires the agent's che…
brennanb2025 Nov 19, 2026
0421185
fix(sleep): a sleep re-proves a tracked pane once; quit checkpoints r…
brennanb2025 Nov 19, 2026
43ff8f3
fix(sleep): only a sleep capture re-proves a tracked pane, and it wai…
brennanb2025 Nov 19, 2026
e2498cb
perf(sleep): a sleep settles all panes' exit proofs together and reso…
brennanb2025 Nov 19, 2026
86fde5e
fix(sleep): the pane's agent still running in the background keeps it…
brennanb2025 Nov 19, 2026
099a7cf
fix(sleep): settle proofs only when the hook server is wired
brennanb2025 Nov 19, 2026
f15281c
perf(sleep): a capture that read no saved record says so, so identity…
brennanb2025 Nov 19, 2026
35bbecc
perf(sleep): a quit's exit proof reads the launch through the pane's …
brennanb2025 Nov 19, 2026
fe92eec
perf(terminal): an exit confirmation reuses its presence check's pane…
brennanb2025 Nov 19, 2026
4e9cc25
perf(sleep): materialization passes its absent record; an agent that …
brennanb2025 Nov 19, 2026
1536777
perf(sleep): spawn, exit and reconcile callbacks check their row and …
brennanb2025 Nov 19, 2026
52d42fe
perf(sleep): a proven quiet exit reuses its pre-proof identity; row-o…
brennanb2025 Nov 19, 2026
66eea73
fix(sleep): a mark on a restored surface whose launch already finishe…
brennanb2025 Nov 19, 2026
b049621
fix(hooks): a Codex Stop read from its transcript keeps the tmux host…
brennanb2025 Nov 19, 2026
7848b71
fix(sleep): a typed agent's quit on Windows or WSL retires its record…
brennanb2025 Nov 19, 2026
491d98c
perf(sleep): a tracked pane whose agent became shared or ended drops …
brennanb2025 Nov 19, 2026
a9aab5e
fix(sleep): a quiet shell's return proves a typed agent's exit withou…
brennanb2025 Nov 19, 2026
d81eaa8
perf(sleep): a quiet shell's return skips the proof for an ended agen…
brennanb2025 Nov 19, 2026
9604c00
perf(sleep): one quiet exit reported by both renderer callbacks runs …
brennanb2025 Nov 19, 2026
8e29171
fix(sleep): a live process whose argv the host couldn't read keeps an…
brennanb2025 Nov 19, 2026
9a64960
perf(sleep): cold root-PID lookups started together share one owner i…
brennanb2025 Nov 19, 2026
0af573d
perf(sleep): a batch of cold root-PID lookups indexes its shared repl…
brennanb2025 Nov 19, 2026
7f4387e
fix(sleep): a quiet return defers to a running mark proof, a second e…
brennanb2025 Nov 19, 2026
91b1354
perf(sleep): a host that published no shell PID gives no proof, so no…
brennanb2025 Nov 19, 2026
0655561
fix(sleep): the window learns a tmux-hosted conversation is shared, s…
brennanb2025 Nov 19, 2026
e9e8bf5
fix(sleep): an identity-only status of a tmux-hosted conversation lea…
brennanb2025 Nov 19, 2026
6b03f53
fix(sleep): an ended agent's retained identity reaches the window no …
brennanb2025 Nov 19, 2026
f1be31a
Merge origin/main to resolve the conflict blocking CI
brennanb2025 Nov 19, 2026
750c77a
test(sleep): the woken terminal holds the pane's layout, as the windo…
brennanb2025 Nov 19, 2026
f56b20c
fix(sleep): only a slept pane's record lists its agent; a live checkp…
brennanb2025 Nov 19, 2026
bf91f7f
refactor(sleep): one serialized executionHost field records who runs …
brennanb2025 Nov 19, 2026
1b4459f
fix(sleep): a resumed turn takes its host from the resuming hook, so …
brennanb2025 Nov 19, 2026
a195606
fix(sleep): a resumed turn keeps the host when the hook reuses the ro…
brennanb2025 Nov 19, 2026
bac860d
refactor(sleep): one name for who runs a conversation; the window and…
brennanb2025 Nov 19, 2026
efe23d7
fix(sleep): a typed WSL agent's mark ends it again; only a process ma…
brennanb2025 Nov 19, 2026
463ed3b
fix(sleep): a slept conversation with no turn yet stays slept across …
brennanb2025 Nov 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const NODE_RUNTIME_INCLUDE = [
'src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-background-stop-unreadable.test.ts',

'src/main/runtime/construction/orca-runtime.test.ts',
'src/main/runtime/terminal/stop/worktree-sleep-resume-recording.test.ts',
'src/main/runtime/client/settings/runtime-client-agent-launch-settings-durability.test.ts',
'src/cli/**/*.test.ts',
'src/main/persistence/**/*.test.ts',
Expand Down
26 changes: 24 additions & 2 deletions src/main/agent-hooks/observations/local-hook-root-evidence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ import {
createLocalPtyInventoryReader,
type LocalPtyInventoryReader
} from './local-tmux-managed-pty'
import { createLocalHookRootEvidenceReader } from '../../../shared/agent-hook-listener/presence/hook-process-root-evidence-reader'
import {
createHookProcessReadSlots,
createLocalHookRootEvidenceReader,
hookProcessReadGate
} from '../../../shared/agent-hook-listener/presence/hook-process-root-evidence-reader'
import { readPaneTmuxHost } from '../../../shared/agent-hook-listener/presence/pane-tmux-host-proof'
import {
readSelectedRows,
readTerminalRows
} from '../../../shared/agent-hook-listener/presence/tmux-host-attachment-probe'
import { getPtyIdForPaneKey } from '../../ipc/pty/pane/key-state'
import { getLocalPtyProvider } from '../../ipc/pty/provider/registry'

Expand All @@ -16,8 +25,21 @@ export function installLocalHookRootEvidence(server: AgentHookServer): LocalPtyI
listProcesses: () => getLocalPtyProvider().listProcesses()
})
if (process.platform !== 'win32') {
const resolveRoot = createLocalHookPaneRootResolver(readInventory)
// Root evidence and tmux host probes share one process-read budget; hook admission goes first.
const slots = createHookProcessReadSlots()
const gate = hookProcessReadGate(slots, undefined, 1)
server.setHookRootEvidenceReader(
createLocalHookRootEvidenceReader(createLocalHookPaneRootResolver(readInventory))
createLocalHookRootEvidenceReader(resolveRoot, slots),
(request) =>
readPaneTmuxHost(
{
resolveRoot,
readProcesses: (pids) => readSelectedRows(pids, gate).catch(() => []),
readTerminal: (tty) => readTerminalRows(tty, gate).catch(() => [])
},
request
)
)
}
return readInventory
Expand Down
43 changes: 43 additions & 0 deletions src/main/agent-hooks/runtime/agent-hook-runtime-deps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { afterEach, expect, it, vi } from 'vitest'
import { makePaneKey } from '../../../shared/workspace-layout/stable-pane-id'
import { AgentHookServer } from '../server/agent-hook-server'
import { selectFreshExplicitAgentStatusRow } from '../../runtime/workspaces/process-listing/runtime-hook-agent-row-selection'
import { agentHookRuntimeDeps } from './agent-hook-runtime-deps'
import { AgentStatusObservedPaneIdentities } from '../../runtime/terminal/identity/agent-status-observed-pane-identity'

vi.mock('../../telemetry/client', () => ({ track: vi.fn() }))
vi.mock('../../telemetry/cohort-classifier', () => ({ getCohortAtEmit: () => ({}) }))

const PANE = makePaneKey('tab-dismissed', '11111111-1111-4111-8111-111111111111')
const servers: AgentHookServer[] = []
afterEach(() => {
for (const server of servers.splice(0)) {
server.stop()
}
})

it('keeps a dismissed running agent out of status readers', () => {
const server = new AgentHookServer()
servers.push(server)
server.ingestRemote(
{
paneKey: PANE,
tabId: 'tab-dismissed',
worktreeId: 'wt-dismissed',
source: 'claude',
hookEventName: 'UserPromptSubmit',
hasExplicitPrompt: true,
providerSession: { key: 'session_id', id: 'claude-conversation' },
payload: { agentType: 'claude', state: 'working', prompt: 'Keep going' }
},
null
)
server.dropStatusEntry(PANE)
const rows = server.getStatusSnapshot()
expect(rows).toMatchObject([{ paneKey: PANE, providerSessionOnly: true }])
const deps = agentHookRuntimeDeps(server, new AgentStatusObservedPaneIdentities(), () => ({}))
expect(deps.getAgentStatusSnapshot()).toEqual([])
expect(
selectFreshExplicitAgentStatusRow({ handles: [], paneKeys: [PANE], hookRows: rows })
).toBeNull()
})
17 changes: 14 additions & 3 deletions src/main/agent-hooks/runtime/agent-hook-runtime-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ export function agentHookRuntimeDeps(
onTerminalProgramStatusFacet: (event) => server.ingestProgramStatusFacet(event),
onClaudeTerminalEvidence: (paneKey, evidence) =>
server.observeClaudeTerminalEvidence(paneKey, evidence),
getPaneAgentIdentity: (paneKey, savedRecord) => server.paneAgentIdentity(paneKey, savedRecord),
getAgentStatusSnapshot: () =>
server.getStatusSnapshot().filter((entry) => entry.providerSessionOnly !== true),
server.getStatusSnapshot().flatMap((row) => {
if (row.structuredHost) {
return [row]
}
// Resume-only rows carry identity for recovery, never status.
if (row.providerSessionOnly) {
return []
}
const identity = server.paneAgentIdentity(row.paneKey)
return identity && !identity.ended ? [identity.status] : []
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}),
Comment thread
brennanb2025 marked this conversation as resolved.
Resolved
getAgentStatusSnapshotForPane: (paneKey) => server.getStatusSnapshotForPane(paneKey),
getPublicAgentStatusSnapshotForPane: (paneKey: string, terminalHandle?: string | null) =>
server.getPublicStatusSnapshotForPane(paneKey, terminalHandle),
Expand Down Expand Up @@ -72,10 +83,10 @@ export function agentHookRuntimeDeps(
readChildWorkHandles: (subject) => server.getStructuredChildWorkHandles(subject)
},
readHookCliWriterObservation: (row) => server.readCliWriterObservation(row),
checkHookAgentPresence: (paneKey, exitedSince) =>
checkHookAgentPresence: (paneKey, exitedSince, options) =>
exitedSince === undefined
? server.checkAgentPresence(paneKey)
: server.checkAgentPresenceSince(paneKey, exitedSince),
: server.checkAgentPresenceSince(paneKey, exitedSince, options),
reconcileAgentStatusForEndedProcess: (paneKeys, sleep) => {
const keys = [...paneKeys]
if (sleep) {
Expand Down
10 changes: 9 additions & 1 deletion src/main/agent-hooks/server/agent-exit-evidence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { isNewTurnEvent } from '../../../shared/agent-hook-listener/provider-eve
import {
isSameAgentProcess,
readAgentProcessIdentity,
type AgentProcessIdentity
type AgentProcessIdentity,
type AgentProcessPresence
} from '../../../shared/agent-status/process/agent-process-presence'

/** All pane aliases share the same exit evidence. */
Expand All @@ -24,6 +25,13 @@ export type PersistedAgentExitEvidence = {

type EndedAgent = { agent?: string; process?: AgentProcessIdentity }

/** The row's owner marked ended, keeping whatever process identity hooks proved. */
export function endedPresenceOf(
row: Pick<AgentHookEventPayload, 'payload' | 'agentPresence'>
): AgentProcessPresence {
return { agent: row.payload.agentType ?? 'unknown', ...row.agentPresence, ended: true }
}

export function recordAgentExit(
exits: Map<string, AgentExitEvidence>,
paneKeys: Iterable<string>,
Expand Down
55 changes: 45 additions & 10 deletions src/main/agent-hooks/server/agent-hook-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { normalizeHookPayload } from '../../../shared/agent-hook-listener/normal
import { parseFormEncodedBody } from '../../../shared/agent-hook-listener/request-body'
import type { AgentHookEventPayload } from '../../../shared/agent-hook-listener/listener-event'
import type { AgentHookSource } from '../../../shared/agent-hook-listener/transport/agent-hook-relay'
import { cliWriterTopology, type CliWriterObservation } from './cli-writer-observation'
import {
cliWriterTopology,
tmuxCliWriterEvent,
type CliWriterObservation
} from './cli-writer-observation'
import type { AgentStatusIpcPayload } from '../../../shared/agent-status/contracts/agent-status-ipc-payload'
import type { ParsedAgentStatusPayload } from '../../../shared/agent-status/contracts/agent-status-types'
import type { EnrichedAgentHookEventPayload } from './server-types'
import {
ClaudeSessionContinuationTracker,
resolvePaneClaudeContinuation
Expand Down Expand Up @@ -66,13 +71,6 @@ export class AgentHookServer extends AgentHookServerLifecycle {
// A live sender means the owner's exit cannot establish pane exit.
private readonly ownerChallengesByLiveSender = new Map<string, number>()

hasVerifiableAgentProcess(paneKey: string): boolean {
const presence = this.state.lastStatusByPaneKey.get(
this.resolvePaneKeyAlias(paneKey)
)?.agentPresence
return presence?.process !== undefined && !presence.ended
}

/** Pin inspection to this row so a replacement owner cannot become its subject. */
readCliWriterObservation(expected: AgentStatusIpcPayload): CliWriterObservation | null {
const resolved = this.resolvePaneKeyAlias(expected.paneKey)
Expand Down Expand Up @@ -112,9 +110,13 @@ export class AgentHookServer extends AgentHookServerLifecycle {
/** Include retained exits proven at or after `exitedSince`. */
async checkAgentPresenceSince(
paneKey: string,
exitedSince: number
exitedSince: number,
options?: { sleepCapture?: true }
): Promise<AgentProcessVerdict | null> {
const verdict = await this.checkAgentPresence(paneKey)
const [verdict] = await Promise.all([
this.checkAgentPresence(paneKey),
this.observeTmuxHostedWriter(paneKey, options?.sleepCapture === true)
])
// A replaced owner's probe result counts only if its exit was retained.
if (verdict !== null && verdict !== 'exited') {
return verdict
Expand All @@ -130,6 +132,39 @@ export class AgentHookServer extends AgentHookServerLifecycle {
return exit !== undefined && exit.at >= exitedSince ? 'exited' : null
}

/** Legacy hooks cannot say a tmux server hosts them; the pane's foreground client can, before
* a sleep captures the conversation as the pane's own. Other presence reads never probe. */
private async observeTmuxHostedWriter(paneKey: string, sleepCapture: boolean): Promise<void> {
if (!sleepCapture) {
return
}
const resolved = this.resolvePaneKeyAlias(paneKey)
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: main stores enriched accepted rows in the base-typed cache.
const row = this.state.lastStatusByPaneKey.get(resolved) as
| EnrichedAgentHookEventPayload
| undefined
const binding = row?.providerSession
const read = this.readPaneTmuxHost
if (!row) {
return this.releaseTmuxHostedCheckpoint(resolved)
}
if (!read || !binding || row.connectionId !== null || cliWriterTopology(row) !== 'pane-owned') {
return
}
const presence = row.agentPresence
const hosted = await read({
paneKey: resolved,
worktreeId: row.worktreeId,
...(presence?.process && !presence.ended ? { process: presence.process } : {})
}).catch(() => false)
if (hosted && this.state.lastStatusByPaneKey.get(resolved) === row) {
Comment thread
brennanb2025 marked this conversation as resolved.
Resolved
const hostedRow = tmuxCliWriterEvent(row)
if (this.writeLegacyStatusRow(hostedRow)) {
this.commitStatusRowMutation(row, hostedRow)
}
}
}

checkAgentPresence(paneKey: string): Promise<AgentProcessVerdict | null> {
const resolved = this.resolvePaneKeyAlias(paneKey)
this.followClaudeSessionContinuation(resolved)
Expand Down
54 changes: 53 additions & 1 deletion src/main/agent-hooks/server/cli-writer-observation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import {
} from '../../../shared/agent-status/process/agent-process-presence'
import type { AgentHookEventPayload } from '../../../shared/agent-hook-listener/listener-event'
import type { AgentHookSource } from '../../../shared/agent-hook-listener/transport/agent-hook-relay'
import { agentProviderSessionsEqual } from '../../../shared/agent-session/lifecycle/resume/agent-provider-session-equality'
import { sameAgentStatusTerminalOwner } from '../../../shared/agent-status/identity/agent-status-terminal-owner'
import {
readTmuxHookPane,
readTmuxHookPaneHint
} from '../../../shared/agent-hook-listener/presence/tmux-client-attachment'

// Execution metadata follows the conversation binding and disappears at serialization.
export const OPEN_CODE_WRITER_TOPOLOGY = Symbol('openCodeWriterTopology')
Expand Down Expand Up @@ -68,10 +74,41 @@ export function tmuxCliWriterEvent<T extends AgentHookEventPayload>(row: T): T {
return row
}
const providerSession = { ...row.providerSession }
Object.defineProperty(providerSession, OPEN_CODE_WRITER_TOPOLOGY, { value: 'shared' })
retainTmuxWriterTopology(providerSession)
return { ...row, providerSession }
}

/** A hook that ran inside a tmux pane reaches this pane through a client; its server outlives the
* pane even after the client detaches, when the pane's foreground no longer shows tmux. A pane
* that only inherited Orca's own tmux environment hosts nothing. */
export function retainTmuxHookWriterTopology(
event: AgentHookEventPayload | null,
body: unknown,
env: NodeJS.ProcessEnv = process.env
): void {
if (!event?.providerSession || !body || typeof body !== 'object') {
return
}
const tmux =
readTmuxHookPane('tmux' in body ? body.tmux : undefined) ??
readTmuxHookPaneHint('tmuxPane' in body ? body.tmuxPane : undefined)
const inherited = readTmuxHookPaneHint(
env.TMUX_PANE && env.TMUX ? `${env.TMUX_PANE} ${env.TMUX.split(',')[0]}` : undefined
)
if (tmux && !(inherited?.pane === tmux.pane && inherited.socket === tmux.socket)) {
retainTmuxWriterTopology(event.providerSession)
}
}

/** A binding the pane reaches through a tmux client; a decided binding keeps its topology. */
export function retainTmuxWriterTopology(
binding: NonNullable<AgentHookEventPayload['providerSession']>
): void {
if (!(OPEN_CODE_WRITER_TOPOLOGY in binding) && Object.isExtensible(binding)) {
Object.defineProperty(binding, OPEN_CODE_WRITER_TOPOLOGY, { value: 'shared' })
}
}

export type CliWriterExecutionMatches = (
row: AgentHookEventPayload,
baseline?: AgentHookEventPayload
Expand Down Expand Up @@ -111,6 +148,21 @@ export function deriveCliWriterTopology<T extends AgentHookEventPayload>(
): T {
const binding = row.providerSession
if (!binding || !(OPEN_CODE_WRITER_TOPOLOGY in binding)) {
if (
binding &&
baseline &&
cliWriterTopology(baseline) === 'shared' &&
!newRun &&
row.payload.agentType === baseline.payload.agentType &&
sameAgentStatusTerminalOwner(baseline, row) &&
agentProviderSessionsEqual(row.payload.agentType, baseline.providerSession, binding) &&
(!sender ||
!baseline.agentPresence?.process ||
isSameAgentProcess(sender, baseline.agentPresence.process)) &&
matches?.(row, baseline)
) {
return tmuxCliWriterEvent(row)
}
return row
}
const inherited = baseline?.providerSession === binding
Expand Down
24 changes: 24 additions & 0 deletions src/main/agent-hooks/server/pane-agent-identity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, it } from 'vitest'
import { paneAgentIdentity } from './pane-agent-identity'

it('ends a pane whose only report since the exit is a replay of older evidence', () => {
const row = {
paneKey: 'tab:1',
worktreeId: 'wt',
connectionId: null,
receivedAt: 2_000,
evidenceObservedAt: 1_000,
stateStartedAt: 1_000,
state: 'done',
agentType: 'claude',
prompt: ''
} as const
expect(paneAgentIdentity({ paneKey: 'tab:1', row, exitedAt: 1_500 })?.ended).toBe(true)
expect(
paneAgentIdentity({
paneKey: 'tab:1',
row: { ...row, evidenceObservedAt: 1_800 },
exitedAt: 1_500
})?.ended
).toBe(false)
})
Loading
Loading