Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
5 changes: 4 additions & 1 deletion src/main/ai-vault/cached-session-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export async function listAiVaultSessions(
force: args?.force,
signal: options.signal,
start: async (scanSignal) => {
const result = await scanAiVaultSessionsInBackground(
const scanned = await scanAiVaultSessionsInBackground(
{
limit: args?.limit,
unlimited: args?.unlimited,
Expand All @@ -118,6 +118,9 @@ export async function listAiVaultSessions(
},
scanSignal
)
// The bound this scan ran under, echoed so a reader can tell a whole answer from a slice
// instead of guessing from the row count.
const result: AiVaultListResult = { ...scanned, appliedSessionDepth: depth }
// A delete (or other invalidation) landed while this scan was running:
// its result predates the delete, so caching it would resurrect the
// deleted session for the TTL. Return it to this caller but don't cache.
Expand Down
4 changes: 4 additions & 0 deletions src/main/ai-vault/remote-session-scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getRemoteHostPlatform } from '../ssh/ssh-remote-platform'
import { scanRemoteAiVaultSessions } from './remote-session-scanner'
import { MemoryRemoteProvider, jsonLines } from './remote-session-scanner-test-fixtures'
import { primeAgentFixture } from './session-scanner-prime-agent-fixtures'
import { DEFAULT_AI_VAULT_SCAN_LIMIT } from '../../shared/ai-vault-session-depth'

describe('scanRemoteAiVaultSessions', () => {
it('indexes Cline manifests on the SSH-owned disk without messages-file phantoms', async () => {
Expand Down Expand Up @@ -45,6 +46,9 @@ describe('scanRemoteAiVaultSessions', () => {
})

expect(result.issues).toEqual([])
// Silence here is read downstream as a possible slice, which refuses recovery on every
// SSH host; the scan must say the bound it actually ran under.
expect(result.appliedSessionDepth).toBe(DEFAULT_AI_VAULT_SCAN_LIMIT)
expect(result.sessions).toHaveLength(1)
expect(result.sessions[0]).toMatchObject({
agent: 'cline',
Expand Down
7 changes: 5 additions & 2 deletions src/main/ai-vault/remote-session-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { recordSessionScanIssue } from './session-scan-issues'
import { canStopParsingSessions } from './session-scan-cutoff'
import { refreshCodexTitleFromIndex } from './session-scanner-codex-cached-title'
import { limitRemoteScanFilesystemConcurrency } from './remote-session-scan-concurrency'
import { aiVaultScanLimit } from '../../shared/ai-vault-session-depth'
import { aiVaultScanLimit, requestedAiVaultSessionDepth } from '../../shared/ai-vault-session-depth'

const REMOTE_SCAN_CONCURRENCY = 8
const REMOTE_PARSE_CANDIDATE_MULTIPLIER = 2
Expand Down Expand Up @@ -129,7 +129,10 @@ export async function scanRemoteAiVaultSessions(args: {
return {
sessions: mergeRemoteSessions(cappedSessions, scopeSessions),
issues,
scannedAt: new Date().toISOString()
scannedAt: new Date().toISOString(),
// Echo the bound this scan ran under. Silence here is read downstream as a possible slice,
// which refuses recovery on every SSH host.
appliedSessionDepth: requestedAiVaultSessionDepth(args)
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/ai-vault/runtime-session-scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ describe('runtime AI Vault session scanner', () => {
])
})

it('keeps the scan depth the host applied, so coverage is not read as truncated', async () => {
mocks.callRuntimeEnvironment.mockResolvedValueOnce({
ok: true,
result: { ...result([session('local', 'session-1')]), appliedSessionDepth: 'unlimited' }
})

const scanResult = await scanRuntimeAiVaultSessions('/user-data', 'env-1', {})

// Retagging owns host identity only. Dropping this field reads downstream as a truncated
// scan, which refuses recovery on every runtime host.
expect(scanResult.appliedSessionDepth).toBe('unlimited')
})

it('stamps accepted sessions with the requested runtime host', async () => {
const scanResult = await scanRuntimeAiVaultSessions('/user-data', 'env-1', {})

Expand Down
7 changes: 5 additions & 2 deletions src/main/ai-vault/runtime-session-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,13 @@ function withRuntimeExecutionHost(
result: AiVaultListResult,
executionHostId: `runtime:${string}`
): AiVaultListResult {
// Spread, not a field list: retagging owns host identity only. Rebuilding the result by hand
// silently drops whatever else the host answered — `appliedSessionDepth` went that way, and its
// absence reads as a truncated scan, which disables recovery for every runtime host.
return {
...result,
sessions: result.sessions.map((session) => retagRuntimeSession(session, executionHostId)),
issues: result.issues.map((issue) => ({ ...issue, executionHostId })),
scannedAt: result.scannedAt
issues: result.issues.map((issue) => ({ ...issue, executionHostId }))
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/main/ai-vault/session-list-result-validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,43 @@ describe('parseAiVaultListResult', () => {
)
})

// EP-COMPAT: hosts and clients update independently, so both wire shapes have to survive this
// parser — it rebuilds the result field by field and would otherwise drop what it does not name.
it('carries the depth a newer host says it applied', () => {
const parsed = parseAiVaultListResult({
sessions: [validSession()],
issues: [],
scannedAt: '2026-07-27T00:00:00.000Z',
appliedSessionDepth: 'unlimited'
})

expect(parsed.appliedSessionDepth).toBe('unlimited')
})

it('leaves the applied depth absent for a host that predates the field', () => {
const parsed = parseAiVaultListResult({
sessions: [validSession()],
issues: [],
scannedAt: '2026-07-27T00:00:00.000Z'
})

expect(parsed.appliedSessionDepth).toBeUndefined()
})

// A value this build cannot read is no more provable than silence, so it degrades to absent
// rather than to a number a reader would trust.
it('drops a malformed applied depth instead of failing the whole result', () => {
const parsed = parseAiVaultListResult({
sessions: [validSession()],
issues: [],
scannedAt: '2026-07-27T00:00:00.000Z',
appliedSessionDepth: 'most-of-them'
})

expect(parsed.sessions).toHaveLength(1)
expect(parsed.appliedSessionDepth).toBeUndefined()
})

it('keeps known-agent sessions while silently dropping unknown-agent rows', () => {
const parsed = parseAiVaultListResult({
sessions: [validSession(), { ...validSession('session-new'), agent: 'future-agent' }],
Expand Down
17 changes: 15 additions & 2 deletions src/main/ai-vault/session-list-result-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ const aiVaultScanIssueSchema = z.object({
const aiVaultListResultEnvelopeSchema = z.object({
sessions: z.array(z.unknown()),
issues: z.array(z.unknown()),
scannedAt: z.string()
scannedAt: z.string(),
// Absent from a host older than the field, and that absence is the answer: the caller reads it
// as "coverage unprovable". A malformed value is the same thing, so it is dropped, not coerced.
appliedSessionDepth: z
.union([z.literal('unlimited'), z.number().int().positive()])
.optional()
.catch(undefined)
})

export function parseAiVaultListResult(value: unknown): AiVaultListResult {
Expand Down Expand Up @@ -151,5 +157,12 @@ export function parseAiVaultListResult(value: unknown): AiVaultListResult {
message: `Skipped ${invalidCount} invalid Agent Session History result ${invalidCount === 1 ? 'entry' : 'entries'}.`
})
}
return { sessions, issues, scannedAt: envelope.data.scannedAt }
return {
sessions,
issues,
scannedAt: envelope.data.scannedAt,
...(envelope.data.appliedSessionDepth === undefined
? {}
: { appliedSessionDepth: envelope.data.appliedSessionDepth })
}
}
49 changes: 49 additions & 0 deletions src/main/ai-vault/session-list-results.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ function listResult(issues: AiVaultScanIssue[]): AiVaultListResult {
return { sessions: [], issues, scannedAt: '2026-08-02T00:00:00.000Z' }
}

function legWithDepth(depth: AiVaultListResult['appliedSessionDepth']): AiVaultListResult {
return {
sessions: [],
issues: [],
scannedAt: '2026-08-02T00:00:00.000Z',
appliedSessionDepth: depth
}
}

function session(index: number): AiVaultSession {
const id = `session-${index}`
const timestamp = new Date(Date.UTC(2026, 7, 2, 0, 0, index)).toISOString()
Expand Down Expand Up @@ -223,3 +232,43 @@ describe('mergeAiVaultListResults', () => {
expect(merged.issues.filter((issue) => !issue.kind)).toEqual([])
})
})

describe('merged scan coverage', () => {
it('reports unlimited only when the merge and every leg were uncapped', () => {
const merged = mergeAiVaultListResults(
[legWithDepth('unlimited'), legWithDepth('unlimited')],
undefined,
true
)

expect(merged.appliedSessionDepth).toBe('unlimited')
})

it('takes the tightest bound any leg applied', () => {
const merged = mergeAiVaultListResults(
[legWithDepth('unlimited'), legWithDepth(25)],
undefined,
true
)

expect(merged.appliedSessionDepth).toBe(25)
})

// One leg that cannot prove its coverage makes the merge unprovable: the rows it withheld are
// exactly the ones missing here, and no other leg can vouch for them.
it('reports nothing when a leg predates the signal', () => {
const merged = mergeAiVaultListResults(
[legWithDepth('unlimited'), listResult([])],
undefined,
true
)

expect(merged.appliedSessionDepth).toBeUndefined()
})

it('reports its own limit when the merge itself is the tighter bound', () => {
const merged = mergeAiVaultListResults([legWithDepth('unlimited')], 10)

expect(merged.appliedSessionDepth).toBe(10)
})
})
28 changes: 26 additions & 2 deletions src/main/ai-vault/session-list-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
} from '../../shared/ai-vault-types'
import type { ExecutionHostId } from '../../shared/execution-host'
import { sessionSortTime } from './session-scanner-accumulator'
import { aiVaultScanLimit } from '../../shared/ai-vault-session-depth'
import { aiVaultScanLimit, type AiVaultSessionDepth } from '../../shared/ai-vault-session-depth'

export function aiVaultScanIssueResult(args: {
executionHostId?: ExecutionHostId
Expand Down Expand Up @@ -52,10 +52,33 @@ export function restampAiVaultListResult(
}
),
issues: result.issues.map((issue) => ({ ...issue, executionHostId })),
scannedAt: result.scannedAt
scannedAt: result.scannedAt,
...(result.appliedSessionDepth === undefined
? {}
: { appliedSessionDepth: result.appliedSessionDepth })
}
}

/** The tightest bound any leg applied, or undefined when a leg did not say — one unprovable leg
* makes the merged answer unprovable, because the rows it withheld are the ones this merge is
* missing. */
function mergedAppliedSessionDepth(
results: readonly AiVaultListResult[],
ownDepth: AiVaultSessionDepth
): AiVaultSessionDepth | undefined {
let tightest = ownDepth
for (const result of results) {
const depth = result.appliedSessionDepth
if (depth === undefined) {
return undefined
}
if (depth !== 'unlimited' && (tightest === 'unlimited' || depth < tightest)) {
tightest = depth
}
}
return tightest
}

export function mergeAiVaultListResults(
results: readonly AiVaultListResult[],
rawLimit: number | undefined,
Expand All @@ -74,6 +97,7 @@ export function mergeAiVaultListResults(
sessions: [...byId.values()]
.sort((left, right) => sessionSortTime(right) - sessionSortTime(left))
.slice(0, limit),
appliedSessionDepth: mergedAppliedSessionDepth(results, unlimited ? 'unlimited' : limit),
issues,
// Why: a merge is not a new scan. Reminting here made every all-host cache
// hit look fresh to the renderer, which only skipped apply when scannedAt
Expand Down
7 changes: 4 additions & 3 deletions src/main/ai-vault/session-scan-issues.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { AiVaultScanIssue } from '../../shared/ai-vault-types'
import { AI_VAULT_SCAN_ISSUE_LIMIT, type AiVaultScanIssue } from '../../shared/ai-vault-types'

// Why: a stalled WSL distro or an unreachable remote host fails one probe per
// discovered path, so an uncapped list ships one row per transcript file in the
// tree over IPC. Local and remote scans share the bound — the failure shape is
// the same and both feed the same panel.
const SCAN_ISSUE_LIMIT = 500
// the same and both feed the same panel. Shared, because a list AT the bound is
// how a reader tells that issues were dropped.
const SCAN_ISSUE_LIMIT = AI_VAULT_SCAN_ISSUE_LIMIT

/** Append a scan issue, collapsing everything past the cap into one notice. */
export function recordSessionScanIssue(issues: AiVaultScanIssue[], issue: AiVaultScanIssue): void {
Expand Down
9 changes: 8 additions & 1 deletion src/main/ipc/ai-vault-scan-coalescing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ vi.mock('./ssh', () => ({
requestActiveSshAiVaultSessionTitles: mocks.requestActiveSshAiVaultSessionTitles
}))

const { DEFAULT_AI_VAULT_SCAN_LIMIT } = await import('../../shared/ai-vault-session-depth')
const { _internals, registerAiVaultHandlers } = await import('./ai-vault')
const EMPTY_RESULT: AiVaultListResult = {
sessions: [],
Expand Down Expand Up @@ -90,7 +91,13 @@ describe('Agent Session History scan coalescing', () => {
await expect(first).rejects.toMatchObject({ name: 'AbortError' })
expect(scan).toHaveBeenCalledTimes(1)
resolveScan?.(EMPTY_RESULT)
await expect(second).resolves.toEqual(EMPTY_RESULT)
// The local leg echoes the depth it scanned under, so a reader can tell a whole answer from a
// slice; a mocked remote leg comes back exactly as the mock produced it.
await expect(second).resolves.toEqual(
scope === 'local'
? { ...EMPTY_RESULT, appliedSessionDepth: DEFAULT_AI_VAULT_SCAN_LIMIT }
: EMPTY_RESULT
)
})

it('coalesces every all-host leg while isolating caller cancellation', async () => {
Expand Down
Loading