Skip to content
8 changes: 8 additions & 0 deletions src/main/build-initial-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { initialPermissions } from '../shared/state/permissions'
import { initialSubscriptions, type WorktreeSubscription } from '../shared/state/subscriptions'
import { initialOrigins, type WorktreeOrigin } from '../shared/state/origins'
import { initialNotifications } from '../shared/state/notifications'
import { initialInbox } from '../shared/state/inbox'
import {
initialReviews,
type ReviewAuthor,
Expand Down Expand Up @@ -272,6 +273,13 @@ export function buildInitialAppState(
? { byPath: sanitizeOrigins(config.worktreeOrigins) }
: initialOrigins,
notifications: initialNotifications,
inbox: config.inbox
? {
turns: { ...config.inbox.turns },
seenAt: { ...config.inbox.seenAt },
prSeen: { ...config.inbox.prSeen }
}
: initialInbox,
settings: {
...initialSettings,
themeMode:
Expand Down
3 changes: 2 additions & 1 deletion src/main/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ export function tailLog(terminalId: string, store: Store): void {
payload: {
id: terminalId,
status: update.status,
pendingTool: update.pendingTool
pendingTool: update.pendingTool,
at: Date.now()
}
})
}
Expand Down
255 changes: 255 additions & 0 deletions src/main/inbox-deriver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { mkdtempSync, writeFileSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'

vi.mock('./debug', () => ({ log: () => {} }))

type StopListener = (ev: { terminalId: string; sessionId: string; transcriptPath: string; ts: number }) => void
const stopListeners: StopListener[] = []
vi.mock('./hooks', () => ({
onStopEvent: (l: StopListener) => {
stopListeners.push(l)
return () => {}
}
}))

import { Store } from './store'
import { InboxDeriver, lastAssistantTextFromJsonl } from './inbox-deriver'
import { initialState } from '../shared/state'
import type { PaneNode, TerminalTab } from '../shared/state/terminals'
import type { JsonClaudeSession } from '../shared/state/json-claude'

const WT = '/wt/a'

function paneWith(tabs: TerminalTab[]): PaneNode {
return { type: 'leaf', id: 'p1', tabs, activeTabId: tabs[0].id }
}

function chatSession(id: string, text: string): JsonClaudeSession {
return {
sessionId: id,
worktreePath: WT,
state: 'running',
exitCode: null,
exitReason: null,
entries: [
{ entryId: '1', kind: 'user', text: 'go', timestamp: 1 },
{ entryId: '2', kind: 'assistant', blocks: [{ type: 'text', text }], timestamp: 2 },
{
entryId: '3',
kind: 'assistant',
parentToolUseId: 'task1',
blocks: [{ type: 'text', text: 'sub-agent chatter?' }],
timestamp: 3
}
],
entriesHydrated: true,
busy: false,
permissionMode: 'default',
slashCommands: [],
autoApprovedDecisions: {},
sessionToolApprovals: [],
sessionAllowedDecisions: {},
backgroundAgents: {}
}
}

function status(store: Store, id: string, s: 'processing' | 'waiting'): void {
store.dispatch({ type: 'terminals/statusChanged', payload: { id, status: s, pendingTool: null } })
}

beforeEach(() => {
stopListeners.length = 0
})

describe('InboxDeriver', () => {
it('records a chat turn from its last top-level assistant message', () => {
const store = new Store({
...initialState,
terminals: {
...initialState.terminals,
panes: { [WT]: paneWith([{ id: 'c1', type: 'json-claude', label: 'Chat' }]) }
},
jsonClaude: { ...initialState.jsonClaude, sessions: { c1: chatSession('c1', 'Shall I push it?') } }
})
new InboxDeriver(store).start()
status(store, 'c1', 'processing')
status(store, 'c1', 'waiting')
const turn = store.getSnapshot().state.inbox.turns.c1
expect(turn).toMatchObject({ worktreePath: WT, kind: 'question', text: 'Shall I push it?' })
})

it('does not treat a replayed boot status as a finished turn', () => {
const store = new Store({
...initialState,
terminals: {
...initialState.terminals,
panes: { [WT]: paneWith([{ id: 'c1', type: 'json-claude', label: 'Chat' }]) }
},
jsonClaude: { ...initialState.jsonClaude, sessions: { c1: chatSession('c1', 'Done.') } }
})
new InboxDeriver(store).start()
status(store, 'c1', 'waiting')
expect(store.getSnapshot().state.inbox.turns.c1).toBeUndefined()
expect(store.getSnapshot().state.terminals.statuses.c1).toBe('waiting')
})

it('fills a terminal Claude turn in from the Stop hook transcript', async () => {
const dir = mkdtempSync(join(tmpdir(), 'inbox-deriver-'))
const transcript = join(dir, 's.jsonl')
writeFileSync(
transcript,
[
JSON.stringify({ type: 'user', message: { content: 'hi' } }),
JSON.stringify({ type: 'assistant', message: { content: [{ type: 'text', text: 'All tests pass.' }] } }),
''
].join('\n')
)
const store = new Store({
...initialState,
terminals: {
...initialState.terminals,
panes: { [WT]: paneWith([{ id: 'a1', type: 'agent', label: 'Claude' }]) }
}
})
new InboxDeriver(store).start()
status(store, 'a1', 'processing')
for (const l of stopListeners) l({ terminalId: 'a1', sessionId: 's', transcriptPath: transcript, ts: 0 })
status(store, 'a1', 'waiting')
expect(store.getSnapshot().state.inbox.turns.a1).toMatchObject({ kind: 'finished', text: '' })
await vi.waitFor(() => {
expect(store.getSnapshot().state.inbox.turns.a1?.text).toBe('All tests pass.')
})
})

it('records Codex terminal turns as finished with no text', () => {
const store = new Store({
...initialState,
terminals: {
...initialState.terminals,
panes: { [WT]: paneWith([{ id: 'x1', type: 'agent', label: 'Codex', agentKind: 'codex' }]) }
}
})
new InboxDeriver(store).start()
for (const l of stopListeners) l({ terminalId: 'x1', sessionId: 's', transcriptPath: '/nope', ts: 0 })
status(store, 'x1', 'processing')
status(store, 'x1', 'waiting')
expect(store.getSnapshot().state.inbox.turns.x1).toMatchObject({ kind: 'finished', text: '' })
})

it('forgets turns for tabs closed in a worktree', () => {
const store = new Store({
...initialState,
terminals: {
...initialState.terminals,
panes: { [WT]: paneWith([{ id: 'c1', type: 'json-claude', label: 'Chat' }]) }
},
jsonClaude: { ...initialState.jsonClaude, sessions: { c1: chatSession('c1', 'Done.') } }
})
new InboxDeriver(store).start()
status(store, 'c1', 'processing')
status(store, 'c1', 'waiting')
store.dispatch({
type: 'terminals/panesForWorktreeChanged',
payload: { worktreePath: WT, panes: paneWith([{ id: 's1', type: 'shell', label: 'Shell' }]) }
})
expect(store.getSnapshot().state.inbox.turns.c1).toBeUndefined()
})
})

describe('InboxDeriver edge cases', () => {
it('a slow transcript read never overwrites a newer turn', async () => {
const dir = mkdtempSync(join(tmpdir(), 'inbox-deriver-'))
const transcript = join(dir, 's.jsonl')
writeFileSync(
transcript,
JSON.stringify({ type: 'assistant', message: { content: [{ type: 'text', text: 'Stale first turn' }] } }) + '\n'
)
const store = new Store({
...initialState,
terminals: {
...initialState.terminals,
panes: { [WT]: paneWith([{ id: 'a1', type: 'agent', label: 'Claude' }]) }
}
})
new InboxDeriver(store).start()
vi.useFakeTimers({ toFake: ['Date'] })
try {
vi.setSystemTime(1000)
status(store, 'a1', 'processing')
for (const l of stopListeners) l({ terminalId: 'a1', sessionId: 's', transcriptPath: transcript, ts: 0 })
status(store, 'a1', 'waiting') // read #1 starts
vi.setSystemTime(2000)
status(store, 'a1', 'processing')
status(store, 'a1', 'waiting') // no Stop path this time → no read
} finally {
vi.useRealTimers()
}
await new Promise((r) => setTimeout(r, 50))
const turn = store.getSnapshot().state.inbox.turns.a1
expect(turn.endedAt).toBe(2000)
expect(turn.text).toBe('')
})

it('seeds a PR read mark on first sight, so existing events stay quiet', () => {
const store = new Store(initialState)
new InboxDeriver(store).start()
const pr = {
number: 3, state: 'open', headSha: 'a', checks: [], checksOverall: 'failure',
hasConflict: false, reviews: [], reviewDecision: 'none'
} as never
store.dispatch({ type: 'prs/statusChanged', payload: { path: WT, status: pr } })
expect(store.getSnapshot().state.inbox.prSeen[WT]).toBe('ci:3:a')
// A later event on the same worktree is new, so it is NOT reseeded.
store.dispatch({
type: 'prs/statusChanged',
payload: { path: WT, status: { ...(pr as object), headSha: 'b' } as never }
})
expect(store.getSnapshot().state.inbox.prSeen[WT]).toBe('ci:3:a')
})

it('prunes records for worktrees gone from the list and from disk', () => {
const gonePath = '/definitely/not/here/wt'
const store = new Store({
...initialState,
inbox: {
turns: { t1: { worktreePath: gonePath, endedAt: 1, kind: 'finished', text: '' } },
seenAt: { t1: 2 },
prSeen: { [gonePath]: 'x', [tmpdir()]: 'y' }
}
})
new InboxDeriver(store).start()
store.dispatch({ type: 'worktrees/listChanged', payload: [] })
const inbox = store.getSnapshot().state.inbox
expect(inbox.turns).toEqual({})
expect(inbox.seenAt).toEqual({})
// Still on disk (e.g. a repo whose listing failed) — kept.
expect(inbox.prSeen).toEqual({ [tmpdir()]: 'y' })
})
})

describe('lastAssistantTextFromJsonl', () => {
it('skips sidechain lines and a truncated first line', () => {
const tail = [
'{"type":"assistant","mess',
JSON.stringify({ type: 'assistant', message: { content: [{ type: 'text', text: 'Main reply?' }] } }),
JSON.stringify({
type: 'assistant',
isSidechain: true,
message: { content: [{ type: 'text', text: 'sub-agent' }] }
}),
JSON.stringify({ type: 'assistant', message: { content: [{ type: 'tool_use', name: 'Bash' }] } })
].join('\n')
expect(lastAssistantTextFromJsonl(tail)).toBe('Main reply?')
})

it('stops at the user prompt that started the turn', () => {
const tail = [
JSON.stringify({ type: 'assistant', message: { content: [{ type: 'text', text: 'Old turn' }] } }),
JSON.stringify({ type: 'user', message: { content: [{ type: 'text', text: 'next' }] } }),
JSON.stringify({ type: 'assistant', message: { content: [{ type: 'tool_use', name: 'Bash' }] } })
].join('\n')
expect(lastAssistantTextFromJsonl(tail)).toBe('')
})
})
Loading
Loading