mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 19:07:41 +00:00
Resume accepted observation frontiers fairly, scope dependency invalidation to changed inputs, and preserve raw per-database membership across bounded recent observations and scheduled complete observations. Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
105 lines
3.7 KiB
TypeScript
105 lines
3.7 KiB
TypeScript
import {
|
|
currentSessionDiscovery,
|
|
discoverSessionContainer
|
|
} from '../../../session-discovery-context'
|
|
import { basename, extname, join } from 'node:path'
|
|
import type { AiVaultScanIssue } from '../../../../../shared/ai-vault/contracts/ai-vault-types'
|
|
import { discoverFiles } from '../../../session-scanner-discovery'
|
|
import { splitOpenCodeSqliteCandidate } from '../../../../../shared/opencode-sqlite-session-path'
|
|
import { listOpenCodeSqliteSessionsViaWorker } from '../reader-transport/session-scanner-opencode-sqlite-worker-spawn'
|
|
import type { FileWithMtime, SessionFileDiscovery } from '../../../session-scanner-types'
|
|
import { scopeWalksAgentFiles } from '../../../session-scope-cwd'
|
|
|
|
function sessionIdFromLegacyFilePath(filePath: string): string {
|
|
return basename(filePath, extname(filePath))
|
|
}
|
|
|
|
/** Prefer SQLite candidates over stale legacy files sharing a session ID, before parsing either. */
|
|
export async function discoverOpenCodeSessions(args: {
|
|
storageDir: string
|
|
dbPaths: readonly string[]
|
|
limitPerAgent: number
|
|
issues: AiVaultScanIssue[]
|
|
scopePaths?: readonly string[]
|
|
signal?: AbortSignal
|
|
}): Promise<SessionFileDiscovery> {
|
|
const walksScope = scopeWalksAgentFiles('opencode', args.scopePaths)
|
|
const listSqlite = (
|
|
scopePaths?: readonly string[],
|
|
dbPaths = args.dbPaths,
|
|
limit = args.limitPerAgent
|
|
) =>
|
|
listOpenCodeSqliteSessionsViaWorker({
|
|
dbPaths,
|
|
limit,
|
|
issues: args.issues,
|
|
scopePaths,
|
|
signal: args.signal
|
|
})
|
|
const [fileDiscovery, sqliteDiscovery, sqliteScopeCandidates] = await Promise.all([
|
|
discoverFiles({
|
|
rootDir: join(args.storageDir, 'session'),
|
|
limit: args.limitPerAgent,
|
|
agent: 'opencode',
|
|
issues: args.issues,
|
|
extensions: ['.json'],
|
|
searchDependencies: [
|
|
{
|
|
path: join(args.storageDir, 'message'),
|
|
fileKey: (path) => sessionIdFromLegacyFilePath(path),
|
|
eventKey: (path) =>
|
|
path.slice(join(args.storageDir, 'message').length + 1).split(/[\\/]/)[0] ?? null
|
|
}
|
|
],
|
|
keepScopeFiles: walksScope,
|
|
signal: args.signal
|
|
}),
|
|
discoverSessionContainer(
|
|
`opencode:${args.storageDir}`,
|
|
args.dbPaths,
|
|
async (paths, limit) => {
|
|
const before = args.issues.length
|
|
const candidates = await listSqlite(undefined, paths, limit)
|
|
return {
|
|
agent: 'opencode',
|
|
rootDir: args.storageDir,
|
|
files: candidates.map((candidate) => candidate.file),
|
|
complete: !args.issues.slice(before).some((issue) => issue.kind !== 'notice')
|
|
}
|
|
},
|
|
args.limitPerAgent,
|
|
args.issues
|
|
),
|
|
walksScope ? listSqlite(args.scopePaths) : undefined
|
|
])
|
|
|
|
const sqliteFiles = sqliteDiscovery.files
|
|
const sqliteScopeFiles = sqliteScopeCandidates?.map((c) => c.file)
|
|
const sqliteSessionIds = new Set<string>()
|
|
for (const file of [...sqliteFiles, ...(sqliteScopeFiles ?? [])]) {
|
|
const parsed = splitOpenCodeSqliteCandidate(file.path)
|
|
if (parsed) {
|
|
if (sqliteSessionIds instanceof Set) {
|
|
sqliteSessionIds.add(parsed.sessionId)
|
|
}
|
|
}
|
|
}
|
|
const withoutSqliteDuplicates = (files: readonly FileWithMtime[]): FileWithMtime[] =>
|
|
files.filter((file) => !sqliteSessionIds.has(sessionIdFromLegacyFilePath(file.path)))
|
|
const scopeFiles = walksScope
|
|
? [...withoutSqliteDuplicates(fileDiscovery.scopeFiles ?? []), ...(sqliteScopeFiles ?? [])]
|
|
: undefined
|
|
|
|
return {
|
|
agent: 'opencode' as const,
|
|
rootDir: fileDiscovery.rootDir,
|
|
files: [
|
|
...(currentSessionDiscovery()
|
|
? fileDiscovery.files
|
|
: withoutSqliteDuplicates(fileDiscovery.files)),
|
|
...sqliteFiles
|
|
],
|
|
...(scopeFiles ? { scopeFiles } : {})
|
|
}
|
|
}
|