mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
Preserve the complete credential storage, migration, IPC, Settings and rate-limit refresh change alongside standalone GLM plans, current-main database diagnostics and the reviewed unknown-backend environment correction. Keep native discovery cancellation third and selected environment fourth. Original-topic-commit:7903f1cddbOriginal-topic-commit:588117b5cfOriginal-topic-commit:dedd4f8c86Original-topic-commit:6645dae104Original-topic-commit: a25b80c02c1af7830b0e6a65e72d965b3ad98276 Restacked-from: a25b80c02c1af7830b0e6a65e72d965b3ad98276 Restacked-onto:b032867021Co-authored-by: kespineira <kespineira@users.noreply.github.com> Co-authored-by: kevimux <kevimux@users.noreply.github.com> Reported-by: pullfrog Reviewed-full-source: 849fe093073f4c1606bd65d79a0c725d955d0d1f Native-helper-source:80dbe23237Reviewed-full-current-source: 36acb57d44adb3d378c0289c8c15f7da0fda214c
116 lines
3.9 KiB
TypeScript
116 lines
3.9 KiB
TypeScript
import { stat } from 'node:fs/promises'
|
|
import { basename, isAbsolute, join } from 'node:path'
|
|
import { wslGatedReaddir, wslGatedStat } from '../native-chat/wsl-transcript-fs-access'
|
|
import { WslTranscriptFsError } from '../native-chat/wsl-transcript-fs-gate'
|
|
import { resolveOpenCodeDataDirectory } from '../opencode/opencode-data-directory'
|
|
import type { OpenCodeUsageProcessedDatabase } from './types'
|
|
|
|
type OpenCodeDatabaseOverride = {
|
|
isConfigured: boolean
|
|
path: string | null
|
|
}
|
|
|
|
function getOpenCodeDatabaseOverride(
|
|
dataDirectory: string,
|
|
environment: NodeJS.ProcessEnv
|
|
): OpenCodeDatabaseOverride {
|
|
const raw = environment.OPENCODE_DB?.trim()
|
|
if (!raw) {
|
|
return { isConfigured: false, path: null }
|
|
}
|
|
if (raw === ':memory:') {
|
|
return { isConfigured: true, path: null }
|
|
}
|
|
return {
|
|
isConfigured: true,
|
|
path: isAbsolute(raw) ? raw : join(dataDirectory, raw)
|
|
}
|
|
}
|
|
|
|
// Why gated: the AI Vault's primary OpenCode source delegates here from inside
|
|
// its discovery fan-out, so a UNC data dir or a UNC OPENCODE_DB on a stalled
|
|
// distro would otherwise hang the whole scan on a raw syscall (STA-4049).
|
|
export async function listOpenCodeDatabases(
|
|
/** Lets a caller report the refusal; an empty list otherwise reads as
|
|
* "OpenCode not used" rather than "we could not look". */
|
|
onRefusal?: (path: string, error: WslTranscriptFsError) => void,
|
|
/** Every other stat/readdir failure, including ENOENT; also read as an empty list. */
|
|
onFsError?: (path: string, error: unknown) => void,
|
|
signal?: AbortSignal,
|
|
environment: NodeJS.ProcessEnv = process.env
|
|
): Promise<string[]> {
|
|
const dataDirectory = resolveOpenCodeDataDirectory(environment)
|
|
const databaseOverride = getOpenCodeDatabaseOverride(dataDirectory, environment)
|
|
if (databaseOverride.isConfigured) {
|
|
if (!databaseOverride.path) {
|
|
return []
|
|
}
|
|
try {
|
|
return (await wslGatedStat(databaseOverride.path, 'scan', signal)).isFile()
|
|
? [databaseOverride.path]
|
|
: []
|
|
} catch (error) {
|
|
signal?.throwIfAborted()
|
|
reportFailure(databaseOverride.path, error, onRefusal, onFsError)
|
|
return []
|
|
}
|
|
}
|
|
|
|
return listOpenCodeDatabasesInDirectory(dataDirectory, onRefusal, signal, onFsError)
|
|
}
|
|
|
|
export async function listOpenCodeDatabasesInDirectory(
|
|
dataDirectory: string,
|
|
onRefusal?: (path: string, error: WslTranscriptFsError) => void,
|
|
signal?: AbortSignal,
|
|
onFsError?: (path: string, error: unknown) => void
|
|
): Promise<string[]> {
|
|
try {
|
|
const entries = await wslGatedReaddir(dataDirectory, 'scan', signal)
|
|
return entries
|
|
.filter((entry) => entry.isFile() && /^opencode(?:-[A-Za-z0-9_.-]+)?\.db$/.test(entry.name))
|
|
.map((entry) => join(dataDirectory, entry.name))
|
|
.sort()
|
|
} catch (error) {
|
|
signal?.throwIfAborted()
|
|
reportFailure(dataDirectory, error, onRefusal, onFsError)
|
|
return []
|
|
}
|
|
}
|
|
|
|
function reportFailure(
|
|
path: string,
|
|
error: unknown,
|
|
onRefusal?: (path: string, error: WslTranscriptFsError) => void,
|
|
onFsError?: (path: string, error: unknown) => void
|
|
): void {
|
|
if (error instanceof WslTranscriptFsError) {
|
|
onRefusal?.(path, error)
|
|
} else {
|
|
onFsError?.(path, error)
|
|
}
|
|
}
|
|
|
|
export function compareOpenCodeClaimPriority(left: string, right: string): number {
|
|
// Why: the canonical opencode.db is the live database; it must claim
|
|
// duplicated sessions ahead of stale sibling copies. Remaining ties use
|
|
// path order so ownership is deterministic across rescans.
|
|
const leftRank = basename(left).toLowerCase() === 'opencode.db' ? 0 : 1
|
|
const rightRank = basename(right).toLowerCase() === 'opencode.db' ? 0 : 1
|
|
if (leftRank !== rightRank) {
|
|
return leftRank - rightRank
|
|
}
|
|
return left < right ? -1 : left > right ? 1 : 0
|
|
}
|
|
|
|
export async function getProcessedDatabaseInfo(
|
|
dbPath: string
|
|
): Promise<OpenCodeUsageProcessedDatabase> {
|
|
const dbStat = await stat(dbPath)
|
|
return {
|
|
path: dbPath,
|
|
mtimeMs: dbStat.mtimeMs,
|
|
size: dbStat.size
|
|
}
|
|
}
|