Preserve accepted session copies through observation retries

Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
This commit is contained in:
Brennan Benson
2026-11-10 13:21:51 -08:00
co-authored by Jinjing
parent e852ec1a38
commit 25feff6e06
8 changed files with 148 additions and 21 deletions
@@ -186,7 +186,7 @@ it('transfers a deferred singleton forced read into later competitive publicatio
store,
() => true,
undefined,
(path) => queue.hasPublicationDebt(path)
(candidate) => queue.hasPublicationDebt(candidate.file.path)
)
expect(hits('predecessorneedle')).toBe(1)
finish(first, 'acceptedwinnerneedle')
@@ -126,7 +126,7 @@ export abstract class SessionSearchCandidatePublications {
store: SessionSearchIndexStore,
overdue: () => boolean,
signal?: AbortSignal,
owed: (path: string) => boolean = () => false
owed: (candidate: SessionFileCandidate, root: string) => boolean = () => false
): Promise<void> {
const available = this.dirtyPublications.size
let consumed = 0
@@ -145,9 +145,9 @@ export abstract class SessionSearchCandidatePublications {
}
const members = rows(this.groups.get(key))
const selected = this.selected.get(key)
for (const path of selected ?? []) {
if (owed(path)) {
publication.required.add(path)
for (const { candidate, root } of members) {
if (selected?.has(candidate.file.path) && owed(candidate, root)) {
publication.required.add(candidate.file.path)
}
}
if (
@@ -4,7 +4,6 @@ import { SessionSearchContainerInventory } from './session-search-container-inve
import { SessionFileInventory } from '../ai-vault/session-file-inventory'
import type { DiscoverSessionFilesArgs } from '../ai-vault/session-scanner-discovery'
import type { SessionFileCandidate, SessionFileDiscovery } from '../ai-vault/session-scanner-types'
import { splitOpenCodeSqliteCandidate } from '../../shared/opencode-sqlite-session-path'
import { sessionSearchPhysicalRoot, type SessionSearchScanRoots } from './session-search-scan-roots'
import type { SessionDiscoveryContext } from '../ai-vault/session-discovery-context'
import type { AiVaultScanIssue } from '../../shared/ai-vault/contracts/ai-vault-types'
@@ -92,7 +91,7 @@ export class SessionSearchDiscoveryInventory implements SessionDiscoveryContext
return map
}
async observeForRead(
private physicalInventory(
candidate: SessionFileCandidate,
logicalRoot: string,
roots: SessionSearchScanRoots
@@ -101,17 +100,39 @@ export class SessionSearchDiscoveryInventory implements SessionDiscoveryContext
agent,
file: { path }
} = candidate
if (
(agent === 'opencode' || agent === 'opencode2' || agent === 'zcode') &&
splitOpenCodeSqliteCandidate(path, agent === 'zcode' ? 'zcode' : 'opencode')
) {
const rootDir = sessionSearchPhysicalRoot(roots, { agent, rootDir: logicalRoot }, path)
if (rootDir === null) {
return null
}
const rootDir = sessionSearchPhysicalRoot(roots, { agent, rootDir: logicalRoot }, path)
const root = rootDir === null ? undefined : this.roots.get(`${agent}:${rootDir}`)
const root = rootDir === undefined ? undefined : this.roots.get(`${agent}:${rootDir}`)
if (!root?.inventory.files.has(path)) {
throw new Error('Session search read has no physical inventory owner')
}
return root
}
hasPublicationDebt(
candidate: SessionFileCandidate,
logicalRoot: string,
roots: SessionSearchScanRoots
): boolean {
return (
this.physicalInventory(candidate, logicalRoot, roots)?.inventory.hasPublicationDebt(
candidate.file.path
) ?? false
)
}
async observeForRead(
candidate: SessionFileCandidate,
logicalRoot: string,
roots: SessionSearchScanRoots
) {
const root = this.physicalInventory(candidate, logicalRoot, roots)
if (!root) {
return null
}
const path = candidate.file.path
const change = await root.inventory.observeForRead(path, root.args, this.recentLimit)
if (change) {
for (const observer of root.dependencies.values()) {
@@ -4,13 +4,18 @@ import {
notifySessionSearchOverflow
} from './session-search-watcher.test-fixture'
import { appendFile, mkdir, rm, stat, utimes, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { dirname, join } from 'node:path'
import { expect, it, vi } from 'vitest'
import * as filesystem from '../native-chat/wsl-transcript-fs-access'
import * as parser from '../ai-vault/parse-cache/session-scanner-parse-cache'
import * as policy from './session-search-filesystem'
import { SessionSearchIndexer } from './session-search-indexer'
import { SessionFileInventory } from '../ai-vault/session-file-inventory'
import {
CODEX_ROLLOUT_FILE,
codexRolloutLines,
parseTranscript
} from './session-search-transcript-fixtures'
import {
FakeSessionSearchClock,
openSessionSearchIndexerHarness,
@@ -685,3 +690,85 @@ it.each([
}
}
)
it('keeps an accepted alias while its deferred replacement is owed by a held-out observation', async () => {
const harness = await openSessionSearchIndexerHarness('ss-alias-admission-debt')
const clock = new FakeSessionSearchClock()
clock.costPerNowMs = 1000
const primary = join(harness.roots.codexSessionsDir!, CODEX_ROLLOUT_FILE)
const copyHome = join(harness.root, 'copied-home')
const copyRoot = join(copyHome, 'sessions')
const copy = join(copyRoot, CODEX_ROLLOUT_FILE)
const roots = {
...harness.roots,
defaultCodexHomeDir: dirname(harness.roots.codexSessionsDir!),
additionalCodexSessionsDirs: [copyRoot]
}
const polling = vi.spyOn(policy, 'sessionSearchRequiresPolling').mockResolvedValue(true)
const actualStat = filesystem.wslGatedStat
let refusing = false
const probe = vi.spyOn(filesystem, 'wslGatedStat').mockImplementation((path, ...rest) => {
if (refusing && path === primary) {
return Promise.reject(
Object.assign(new Error('fixture observation refusal'), { code: 'EIO' })
)
}
return actualStat(path, ...rest)
})
const subject = new SessionSearchIndexer({
databasePath: harness.databasePath,
roots,
historyDays: null,
clock,
passDeadlineMs: 1000,
discoveryDeadlineMs: Infinity
})
const matching = (text: string) =>
harness.read((db) =>
db.prepare('SELECT count(*) AS n FROM messages_fts WHERE messages_fts MATCH ?').get(text)
)
const rollout = (text: string) =>
`${codexRolloutLines(['echo'], 'plainresult', text).join('\n')}\n`
const fixedTime = new Date('2024-01-01')
try {
await mkdir(dirname(primary), { recursive: true })
await mkdir(copyRoot, { recursive: true })
await writeFile(primary, rollout('canonicaloldneedle'))
await utimes(primary, fixedTime, fixedTime)
await subject.start()
expect(matching('canonicaloldneedle')).toEqual({ n: 1 })
// A foreground read can publish an alias before background discovery notices it.
await writeFile(copy, rollout('predecessorowedneedle'))
await parseTranscript(copy, 'codex', copyHome)
expect(matching('predecessorowedneedle')).toEqual({ n: 1 })
await writeFile(primary, rollout('canonicalnewneedle'))
await utimes(primary, fixedTime, fixedTime)
notifySessionSearchFileChange(primary)
await writeClaudeTranscript(
join(harness.claudeProjectDir, 'newer.jsonl'),
['newerbody'],
'newer'
)
await subject.reconcile()
expect(matching('canonicalnewneedle')).toEqual({ n: 0 })
expect(matching('canonicaloldneedle')).toEqual({ n: 1 })
refusing = true
for (let cycle = 0; cycle < 3; cycle++) {
await subject.reconcile()
}
refusing = false
notifySessionSearchFileChange(copy)
await subject.reconcile()
expect(matching('predecessorowedneedle')).toEqual({ n: 1 })
expect(matching('canonicalnewneedle')).toEqual({ n: 0 })
expect(subject.status().phase).not.toBe('indexing')
await subject.reconcile({ full: true })
expect(matching('canonicalnewneedle')).toEqual({ n: 1 })
expect(matching('predecessorowedneedle')).toEqual({ n: 0 })
} finally {
probe.mockRestore()
polling.mockRestore()
await subject.close()
await harness.cleanup()
}
})
@@ -189,8 +189,13 @@ export async function runSessionSearchPass(
return state.queue.get(path)
}
})
await state.candidates.reconcilePublications(store, readOverdue, signal, (path) =>
state.queue.hasPublicationDebt(path)
await state.candidates.reconcilePublications(
store,
readOverdue,
signal,
(candidate, root) =>
state.queue.hasPublicationDebt(candidate.file.path) ||
state.discovery.hasPublicationDebt(candidate, root, args.roots)
)
const listings = state.discovery.listings()
const roots = listings.map((listing) => listing.root)
@@ -1,3 +1,4 @@
import { splitOpenCodeSqliteCandidate } from '../../shared/opencode-sqlite-session-path'
import type { AiVaultScanIssue } from '../../shared/ai-vault/contracts/ai-vault-types'
import { AI_VAULT_AGENT_SOURCES } from '../ai-vault/session-scanner-agent-sources'
import { normalizedWslHomeDirs } from '../ai-vault/session-scanner-roots'
@@ -84,10 +85,19 @@ export function sessionSearchPhysicalRoot(
roots: SessionSearchScanRoots,
discovery: Pick<SessionFileDiscovery, 'agent' | 'rootDir'>,
path: string
): string | null {
return owningRoot(
constituentRoots(roots, normalizedWslHomeDirs(roots.wslHomeDirs), discovery),
path
): string | null | undefined {
const agent = discovery.agent
if (
(agent === 'opencode' || agent === 'opencode2' || agent === 'zcode') &&
splitOpenCodeSqliteCandidate(path, agent === 'zcode' ? 'zcode' : 'opencode')
) {
return null
}
return (
owningRoot(
constituentRoots(roots, normalizedWslHomeDirs(roots.wslHomeDirs), discovery),
path
) ?? undefined
)
}
@@ -68,7 +68,7 @@ it('keeps indexing after a native crawl never becomes ready and retires its chil
})
try {
await writeClaudeTranscript(path, ['beforepressure'], 'pending')
nativeRoot = await realpath(harness.roots.claudeProjectsDir)
nativeRoot = await realpath(harness.roots.claudeProjectsDir!)
const start = subject.start()
await vi.waitFor(() => expect(crawling).toBe(true))
await vi.advanceTimersByTimeAsync(60_000)
@@ -127,6 +127,10 @@ export class SessionFileInventory {
}
}
hasPublicationDebt(path: string): boolean {
return this.observations.failures.force(path)
}
async observeForRead(path: string, args: DiscoverSessionFilesArgs, limit: number) {
const file = await this.observations.observe(path, args, false, limit, true)
const change = this.observations.changes.get(path)