Files
kilocode/packages/opencode/test/preload.ts
marius-kilocode f40394f07a perf(opencode): start the first worktree snapshot without re-hashing the checkout
Seeding reuses the checkout index, including its stat data, whenever the
snapshot repository hashes files the same way (no autocrlf conversion, real
symlinks, no repository-private attributes). Entries a filter driver rewrites
are reset only when the snapshot repository cannot run that driver, and
assume-unchanged or skip-worktree entries are always reset. Snapshot
preparation reconciles the working tree so the first track only records later
changes, staging falls back to one bulk add for large candidate sets, and the
repack of borrowed objects waits for a quiet repository instead of holding the
lock during the running turn. Initialization writes its config in one step
and the large-file scan no longer wraps every stat in an Effect.

Agent Manager delays the replacement of a claimed pre-warmed worktree so the
checkout does not compete with the new session's first prompt.
2026-09-14 12:30:55 +02:00

119 lines
5.4 KiB
TypeScript

// IMPORTANT: Set env vars BEFORE any imports from src/ directory
// xdg-basedir reads env vars at import time, so we must set these first
import os from "os"
import path from "path"
import fs from "fs/promises"
import { setTimeout as sleep } from "node:timers/promises"
import { afterAll } from "bun:test"
import { remove as cleanup } from "./kilocode/cleanup" // kilocode_change
// Set XDG env vars FIRST, before any src/ imports
const dir = path.join(os.tmpdir(), "opencode-test-data-" + process.pid)
await fs.mkdir(dir, { recursive: true })
afterAll(async () => {
const { AppRuntime } = await import("../src/effect/app-runtime")
await AppRuntime.dispose()
const busy = (error: unknown) =>
typeof error === "object" && error !== null && "code" in error && error.code === "EBUSY"
const rm = async (left: number): Promise<void> => {
Bun.gc(true)
await sleep(100)
return fs.rm(dir, { recursive: true, force: true }).catch((error) => {
if (!busy(error)) throw error
if (left <= 1 && process.platform !== "win32") throw error
if (left <= 1) return
return rm(left - 1)
})
}
// Windows can keep SQLite WAL handles alive until GC finalizers run, so we
// force GC and retry teardown to avoid flaky EBUSY in test cleanup.
await rm(30)
await cleanup(dir) // kilocode_change
})
process.env["XDG_DATA_HOME"] = path.join(dir, "share")
process.env["XDG_CACHE_HOME"] = path.join(dir, "cache")
process.env["XDG_CONFIG_HOME"] = path.join(dir, "config")
process.env["XDG_STATE_HOME"] = path.join(dir, "state")
process.env["KILO_MODELS_PATH"] = path.join(import.meta.dir, "tool", "fixtures", "models-api.json")
process.env["KILO_EXPERIMENTAL_EVENT_SYSTEM"] = "true"
process.env["KILO_EXPERIMENTAL_WORKSPACES"] = "true"
process.env["KILO_EXPERIMENTAL_DISABLE_FILEWATCHER"] ??= "true" // kilocode_change - see test.yml: per-instance watchers are too heavy/racy for unit tests; watcher tests opt back in
process.env["KILO_SNAPSHOT_MATERIALIZE_IDLE_MS"] ??= "0" // kilocode_change - snapshot tests wait for materialization; the idle deferral test opts back in
// Set test home directory to isolate tests from user's actual home directory
// This prevents tests from picking up real user configs/skills from ~/.claude/skills
const testHome = path.join(dir, "home")
await fs.mkdir(testHome, { recursive: true })
process.env["KILO_TEST_HOME"] = testHome
// Set test managed config directory to isolate tests from system managed settings
const testManagedConfigDir = path.join(dir, "managed")
process.env["KILO_TEST_MANAGED_CONFIG_DIR"] = testManagedConfigDir
// Write the cache version file to prevent global/index.ts from clearing the cache
const cacheDir = path.join(dir, "cache", "kilo")
await fs.mkdir(cacheDir, { recursive: true })
await fs.writeFile(path.join(cacheDir, "version"), "21")
// Clear provider and server auth env vars to ensure clean test state
delete process.env["ANTHROPIC_API_KEY"]
delete process.env["OPENAI_API_KEY"]
delete process.env["GOOGLE_API_KEY"]
delete process.env["GOOGLE_GENERATIVE_AI_API_KEY"]
delete process.env["AZURE_OPENAI_API_KEY"]
delete process.env["AWS_ACCESS_KEY_ID"]
delete process.env["AWS_PROFILE"]
delete process.env["AWS_REGION"]
delete process.env["AWS_BEARER_TOKEN_BEDROCK"]
delete process.env["OPENROUTER_API_KEY"]
delete process.env["LLM_GATEWAY_API_KEY"]
delete process.env["GROQ_API_KEY"]
delete process.env["MISTRAL_API_KEY"]
delete process.env["PERPLEXITY_API_KEY"]
delete process.env["TOGETHER_API_KEY"]
delete process.env["XAI_API_KEY"]
delete process.env["DEEPSEEK_API_KEY"]
delete process.env["FIREWORKS_API_KEY"]
delete process.env["CEREBRAS_API_KEY"]
delete process.env["SAMBANOVA_API_KEY"]
delete process.env["KILO_SERVER_PASSWORD"]
delete process.env["KILO_SERVER_USERNAME"]
delete process.env["KILO_EXPERIMENTAL"]
delete process.env["KILO_ENABLE_EXPERIMENTAL_MODELS"]
delete process.env["OTEL_EXPORTER_OTLP_ENDPOINT"]
delete process.env["OTEL_EXPORTER_OTLP_HEADERS"]
delete process.env["OTEL_RESOURCE_ATTRIBUTES"]
// Use in-memory sqlite
process.env["KILO_DB"] = ":memory:"
// Now safe to import from src/
const { initProjectors } = await import("../src/server/projectors")
// kilocode_change: bind the package memory effect layer to opencode for tests (paths/instance/log/events)
const { installMemoryRuntime } = await import("../src/kilocode/memory/runtime") // kilocode_change
initProjectors()
installMemoryRuntime() // kilocode_change
// kilocode_change start - fail closed: unit tests must never open a disk database. Both DB
// path resolvers (core Database.path and the v1 client in src/storage/db.ts) honor KILO_DB
// verbatim when it is ":memory:", so asserting the resolved core path after all preload
// imports catches env mutations, import-order regressions, and channel/absolute fallbacks
// that would silently point tests at the real database under ~/.local/share/kilo.
// (Do not name the database file here: database-reset-safety.test.ts scans test sources
// for the file name next to removal calls, and this file legitimately contains fs.rm.)
if (process.env["KILO_DB"] !== ":memory:") {
throw new Error(`unit test preload: KILO_DB must be ":memory:", got "${process.env["KILO_DB"]}"`)
}
{
const { Database } = await import("@opencode-ai/core/database/database")
const resolved = Database.path()
if (resolved !== ":memory:") {
throw new Error(`unit test preload: database path must resolve to ":memory:", got "${resolved}"`)
}
}
// kilocode_change end