Resolve accepted host status once and preserve delivery order

This commit is contained in:
Brennan Benson
2026-11-13 18:08:48 -08:00
parent 580372c79d
commit f22c01f39b
4 changed files with 100 additions and 62 deletions
@@ -223,36 +223,59 @@ describe('mobile session tabs: a process started after its pane last reported',
vi.useRealTimers()
})
it('publishes identity only, and legacy done to clients that cannot read it', async () => {
const completedBeforeRestart: AgentStatusIpcPayload = {
paneKey: PANE_KEY,
state: 'done',
prompt: 'ship it',
agentType: 'codex',
connectionId: null,
receivedAt: T0 - 120_000,
stateStartedAt: T0 - 120_000,
tabId: TAB_ID,
worktreeId: WORKTREE_ID,
providerSession: PROVIDER_SESSION
}
const runtime = await createRuntime([completedBeforeRestart])
runtime.noteTerminalSpawnCommit({ id: PTY_ID })
runtime.onPtyData(PTY_ID, '\x1b]0;zsh\x07', T0)
it.each(['headless', 'published'] as const)(
'publishes %s identity only after a new process starts',
async (route) => {
const completedBeforeRestart: AgentStatusIpcPayload = {
paneKey: PANE_KEY,
state: 'done',
prompt: 'ship it',
agentType: 'codex',
connectionId: null,
receivedAt: T0 - 120_000,
stateStartedAt: T0 - 120_000,
tabId: TAB_ID,
worktreeId: WORKTREE_ID,
providerSession: PROVIDER_SESSION
}
const rows = [completedBeforeRestart]
const runtime = await createRuntime(rows)
runtime.noteTerminalSpawnCommit({ id: PTY_ID })
vi.setSystemTime(T0 + 20)
runtime.onPtyData(PTY_ID, '\x1b]0;zsh\x07', T0 + 20)
if (route === 'published') {
publishRendererWorkingPane(runtime, 'zsh', {
state: 'done',
prompt: '',
paneKey: PANE_KEY,
agentType: 'codex',
updatedAt: T0 - 120_000,
stateStartedAt: T0 - 120_000,
stateHistory: [],
paneSleep: { ptyId: PTY_ID }
})
}
const result = await runtime.listMobileSessionTabs(`id:${WORKTREE_ID}`)
const tab = result.tabs[0]
expect(tab?.type === 'terminal' ? tab.agentStatus : null).toEqual(
expect.objectContaining({ restoredUnconfirmed: true, providerSession: PROVIDER_SESSION })
)
const legacy = projectSessionTabAgentStatus(result, 'runtime', []).tabs[0]
expect(legacy?.type === 'terminal' ? legacy.agentStatus : null).toEqual(
expect.objectContaining({ state: 'done', providerSession: PROVIDER_SESSION })
)
expect(legacy?.type === 'terminal' ? legacy.agentStatus : null).not.toHaveProperty(
'restoredUnconfirmed'
)
})
const result = await runtime.listMobileSessionTabs(`id:${WORKTREE_ID}`)
const tab = result.tabs[0]
expect(tab?.type === 'terminal' ? tab.agentStatus : null).toEqual(
expect.objectContaining({ restoredUnconfirmed: true, providerSession: PROVIDER_SESSION })
)
const legacy = projectSessionTabAgentStatus(result, 'runtime', []).tabs[0]
expect(legacy?.type === 'terminal' ? legacy.agentStatus : null).toEqual(
expect.objectContaining({ state: 'done', providerSession: PROVIDER_SESSION })
)
expect(legacy?.type === 'terminal' ? legacy.agentStatus : null).not.toHaveProperty(
'restoredUnconfirmed'
)
rows.length = 0
vi.setSystemTime(T0 + 40)
runtime.onPtyData(PTY_ID, '\x1b]0;⠧ New turn\x07', T0 + 40)
const successor = await projectAgentStatus(runtime)
expect(successor?.state).toBe('working')
expect(successor).not.toHaveProperty('paneSleep')
}
)
})
// A title repaint and physical Sleep must not make the same finished turn newly unread.
@@ -267,9 +290,14 @@ describe('mobile session tabs: accepted completion identity through Sleep', () =
vi.useRealTimers()
})
it.each(['headless', 'published'] as const)(
'preserves the accepted end on every %s output through stop and settlement',
async (route) => {
it.each([
{ route: 'headless', stopTitle: 'root@host: ~/repo' },
{ route: 'published', stopTitle: 'root@host: ~/repo' },
{ route: 'headless', stopTitle: 'zsh' },
{ route: 'published', stopTitle: 'zsh' }
])(
'preserves the accepted end on $route output through $stopTitle and settlement',
async ({ route, stopTitle }) => {
const rows: AgentStatusIpcPayload[] = []
const server = new AgentHookServer()
server.setListener(() =>
@@ -333,15 +361,16 @@ describe('mobile session tabs: accepted completion identity through Sleep', () =
prompt: '',
paneKey: PANE_KEY,
agentType: 'claude',
updatedAt: T0 + 20,
updatedAt: T0 + 1_000,
stateStartedAt: T0 + 20,
stateHistory: []
})
expect((await projectAgentStatus(runtime))?.updatedAt).toBeGreaterThanOrEqual(T0 + 1_000)
}
await observe()
finishStop = runtime.intentionalPtyStops.mark(PTY_ID, 'reversible', null)
vi.setSystemTime(T0 + 60_000)
runtime.onPtyData(PTY_ID, '\x1b]0;root@host: ~/repo\x07', T0 + 60_000)
runtime.onPtyData(PTY_ID, `\x1b]0;${stopTitle}\x07`, T0 + 60_000)
await observe()
await runtime.onPtyExit(PTY_ID, 0, undefined, { providerExitObserved: true })
finishStop(true)
@@ -211,6 +211,9 @@ export function buildRuntimeMobileAgentStatus(
ownerAgent,
ownerOptions
)
if (liveRow.paneSleep) {
return { agentStatus: liveStatus }
}
// A live question outranks only the shell title that currently obscures it.
const renewedStatus = renewRuntimeMobileAgentStatusFromPtyTitle(liveStatus, pty, {
preserveQuestionUnderShellTitle: true
@@ -53,10 +53,9 @@ export function renewRuntimeMobileAgentStatusFromPtyTitle(
// Idle/shutdown titles describe presence, not a second end of an already completed turn.
if (
status.state === 'done' &&
(status.paneSleep ||
(status.restoredUnconfirmed !== true &&
pty.lastAgentStatus !== 'working' &&
pty.lastAgentStatus !== 'permission'))
status.restoredUnconfirmed !== true &&
pty.lastAgentStatus !== 'working' &&
pty.lastAgentStatus !== 'permission'
) {
const updatedAt = Math.max(status.updatedAt, titleEvidenceAt ?? 0)
return updatedAt === status.updatedAt
@@ -111,12 +111,13 @@ export function projectRuntimeMobileSessionTabs(
: `${tab.parentTabId}:${legacyPaneId ?? tab.leafId}`
const mobileStatusPty = livePty ?? pty
// Use one main hook lookup for both headless title ownership and status to prevent divergence.
const retainedAgentStatus = tab.agentStatus
? null
: host.getRetainedStatus(paneKey, liveLeafPty ?? mobileStatusPty, tab, getStatusRows)
const hookAgentStatus = tab.agentStatus
? selectRuntimeHookAgentRowForPane(getHookRowsForPane(paneKey))
: null
const retainedAgentStatus = host.getRetainedStatus(
paneKey,
liveLeafPty ?? mobileStatusPty,
tab,
getStatusRows
)
const hookAgentStatus = selectRuntimeHookAgentRowForPane(getHookRowsForPane(paneKey))
// Do not revive rejected persisted tab.ptyId; after restart it may name another pane and publish its title here.
const trackerOnlyTitle = host.getTrackedTitle(liveLeafPtyId ?? pty?.ptyId ?? null)
const displayLeaf = leaf
@@ -178,28 +179,34 @@ export function projectRuntimeMobileSessionTabs(
const statusPty = nativeStatusPty
? getPtyDisplayRecord(nativeStatusPty, host.getTitleDisplayClear(nativeStatusPty.ptyId))
: null
const acceptedRow = hookAgentStatus?.live
// A renderer's finished copy cannot replace the host's accepted completion identity.
const acceptedStatus =
const acceptedRow = retainedAgentStatus ?? hookAgentStatus.live
// Resolve the host record once; identity-only and title fallbacks are already adjudicated.
const resolvedHostStatus =
tab.agentStatus &&
acceptedRow?.payload.state === 'done' &&
(tab.agentStatus.state === 'done' || tab.agentStatus.updatedAt <= acceptedRow.updatedAt)
? host.buildPtyStatus(statusPty, tab, null, null, getHookRowsForPane).agentStatus
? host.buildPtyStatus(statusPty, tab, null, retainedAgentStatus, getHookRowsForPane)
.agentStatus
: null
const normalizedTabAgentStatus = renewRuntimeMobileAgentStatusFromPtyTitle(
tab.agentStatus
? normalizeCompatibleAgentStatusEntryForOwner(
{
...(acceptedStatus ?? tab.agentStatus),
...(hookProviderSession ? { providerSession: hookProviderSession } : {})
},
ownerAgent,
ownerOptions
)
: null,
statusPty,
{ preserveQuestionUnderShellTitle: true }
)
const normalizedTabAgentStatus = resolvedHostStatus
? {
...resolvedHostStatus,
updatedAt: Math.max(resolvedHostStatus.updatedAt, tab.agentStatus?.updatedAt ?? 0)
}
: renewRuntimeMobileAgentStatusFromPtyTitle(
tab.agentStatus
? normalizeCompatibleAgentStatusEntryForOwner(
{
...tab.agentStatus,
...(hookProviderSession ? { providerSession: hookProviderSession } : {})
},
ownerAgent,
ownerOptions
)
: null,
statusPty,
{ preserveQuestionUnderShellTitle: true }
)
// Live prompt/tool status must survive non-agent titles or interactivePrompt disappears.
const hasLiveAgentSignal =
normalizedTabAgentStatus?.interactivePrompt != null ||