Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 15 additions & 4 deletions .github/workflows/platform-smoke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
os:
- ubuntu-latest
- windows-latest
- macos-13
- macos-15-intel
- macos-15

steps:
Expand All @@ -43,12 +43,12 @@ jobs:
- name: Typecheck source
run: bun run typecheck

- name: Run test suite
run: bun test

- name: Build package artifacts
run: bun run build

- name: Run test suite
run: bun test

- name: Pack and install published package shape
shell: bash
run: |
Expand All @@ -64,6 +64,17 @@ jobs:

cd "$smoke_dir"
npm init -y
# npm ignores overrides declared by installed dependencies, so mirror the plugin's
# native-runtime pin at the temporary root used by this package smoke test.
override_version=$(node --input-type=module -e '
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
const pkg = JSON.parse(readFileSync(resolve(process.env.GITHUB_WORKSPACE, "package.json"), "utf8"));
process.stdout.write(pkg.overrides?.["onnxruntime-node"] ?? "");
')
if [ -n "$override_version" ]; then
npm pkg set "overrides.onnxruntime-node=$override_version"
fi
npm install --ignore-scripts "$tarball_path"
cp "$GITHUB_WORKSPACE/scripts/native-deps-smoke.mjs" ./native-deps-smoke.mjs
cp "$GITHUB_WORKSPACE/scripts/verify-libsql-vector.mjs" ./verify-libsql-vector.mjs
Expand Down
89 changes: 68 additions & 21 deletions scripts/verify-libsql-vector.mjs
Original file line number Diff line number Diff line change
@@ -1,30 +1,77 @@
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { setTimeout as delay } from "node:timers/promises";
import { createClient } from "@libsql/client";

const dbPath = join(tmpdir(), `opencode-mem-libsql-smoke-${process.pid}-${Date.now()}.db`);
const client = createClient({ url: `file:${dbPath}` });
const CHILD_DB_PATH_ENV = "OPENCODE_MEM_LIBSQL_SMOKE_CHILD_DB_PATH";
const FILE_LOCK_RETRY_DELAYS_MS = [10, 25, 50, 100, 200, 400, 800];
const RETRYABLE_FILE_LOCK_CODES = new Set(["EBUSY", "EPERM", "EACCES"]);

async function verifyLibsqlVector(dbPath) {
const client = createClient({ url: `file:${dbPath}` });
try {
await client.batch(
[
"CREATE TABLE vectors (id INTEGER PRIMARY KEY, vector F32_BLOB(4) NOT NULL)",
"CREATE INDEX vectors_idx ON vectors (libsql_vector_idx(vector, 'metric=cosine'))",
"INSERT INTO vectors (vector) VALUES (vector32('[1,0,0,0]'))",
],
"write"
);
const result = await client.execute(
"SELECT id FROM vector_top_k('vectors_idx', vector32('[1,0,0,0]'), 1)"
);
assert.equal(result.rows.length, 1, "vector_top_k must return the inserted vector");
assert.equal(Number(result.rows[0]?.id), 1, "vector_top_k must return the expected row");
console.log("libSQL vector smoke test passed");
} finally {
client.close();
}
}

try {
await client.batch(
[
"CREATE TABLE vectors (id INTEGER PRIMARY KEY, vector F32_BLOB(4) NOT NULL)",
"CREATE INDEX vectors_idx ON vectors (libsql_vector_idx(vector, 'metric=cosine'))",
"INSERT INTO vectors (vector) VALUES (vector32('[1,0,0,0]'))",
],
"write"
);
const result = await client.execute(
"SELECT id FROM vector_top_k('vectors_idx', vector32('[1,0,0,0]'), 1)"
);
assert.equal(result.rows.length, 1, "vector_top_k must return the inserted vector");
assert.equal(Number(result.rows[0]?.id), 1, "vector_top_k must return the expected row");
console.log("libSQL vector smoke test passed");
} finally {
client.close();
async function removeDatabaseFiles(dbPath) {
for (const suffix of ["", "-shm", "-wal"]) {
rmSync(`${dbPath}${suffix}`, { force: true });
const path = `${dbPath}${suffix}`;
for (let attempt = 0; ; attempt += 1) {
try {
rmSync(path, { force: true });
break;
} catch (error) {
const code = error?.code;
if (
attempt >= FILE_LOCK_RETRY_DELAYS_MS.length ||
!RETRYABLE_FILE_LOCK_CODES.has(code)
) {
throw error;
}
await delay(FILE_LOCK_RETRY_DELAYS_MS[attempt]);
}
}
}
}

const childDbPath = process.env[CHILD_DB_PATH_ENV];
if (childDbPath) {
await verifyLibsqlVector(childDbPath);
} else {
const dbPath = join(tmpdir(), `opencode-mem-libsql-smoke-${process.pid}-${Date.now()}.db`);
const child = spawnSync(process.execPath, [fileURLToPath(import.meta.url)], {
env: { ...process.env, [CHILD_DB_PATH_ENV]: dbPath },
stdio: "inherit",
});

try {
if (child.error) throw child.error;
if (child.status !== 0) {
throw new Error(
`libSQL vector smoke child exited with ${child.signal ?? `status ${child.status}`}`
);
}
} finally {
await removeDatabaseFiles(dbPath);
}
}
9 changes: 5 additions & 4 deletions src/services/migration-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { log } from "./logger.js";
import { formatTagsForEmbedding } from "./turso/vector-utils.js";
import type { MemoryRecord, ShardInfo } from "./turso/types.js";
import { acquireTursoOperationLock } from "./turso/operation-lock.js";
import { withSqliteFileLockRetry } from "./turso/sqlite-handle-release.js";

export interface DimensionMismatch {
needsMigration: boolean;
Expand Down Expand Up @@ -349,11 +350,11 @@ export class MigrationService {
"utf-8"
);
await tursoConnectionManager.closeConnection(shard.dbPath);
renameSync(shard.dbPath, backupPath);
await withSqliteFileLockRetry(() => renameSync(shard.dbPath, backupPath));
try {
renameSync(stagedPath, shard.dbPath);
await withSqliteFileLockRetry(() => renameSync(stagedPath, shard.dbPath));
} catch (error) {
renameSync(backupPath, shard.dbPath);
await withSqliteFileLockRetry(() => renameSync(backupPath, shard.dbPath));
throw error;
}

Expand All @@ -368,7 +369,7 @@ export class MigrationService {
} catch (error) {
await tursoConnectionManager.closeConnection(stagedPath);
if (existsSync(stagedPath)) {
unlinkSync(stagedPath);
await withSqliteFileLockRetry(() => unlinkSync(stagedPath));
}
if (existsSync(swapStatePath) && existsSync(shard.dbPath)) {
unlinkSync(swapStatePath);
Expand Down
4 changes: 3 additions & 1 deletion src/services/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ export function getGitCommonDir(directory: string): string | null {
: normalize(resolve(directory, commonDir));

if (existsSync(resolved)) {
return realpathSync(resolved);
const canonical =
process.platform === "win32" ? realpathSync.native(resolved) : realpathSync(resolved);
return normalize(canonical);
}

return resolved;
Expand Down
53 changes: 43 additions & 10 deletions src/services/turso/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { existsSync, mkdirSync } from "node:fs";
import { dirname, resolve, relative, isAbsolute, sep } from "node:path";
import { CONFIG } from "../../config.js";
import { log } from "../logger.js";
import { collectReleasedSqliteHandles } from "./sqlite-handle-release.js";
import { TursoDb } from "./turso-db.js";

function toFileUrl(dbPath: string): string {
Expand All @@ -22,12 +23,19 @@ function assertPathInsideStorage(dbPath: string): void {
export class TursoConnectionManager {
private readonly connections = new Map<string, TursoDb>();
private readonly pending = new Map<string, Promise<TursoDb>>();
private readonly closingConnections = new Map<string, Promise<void>>();
private closingPromise: Promise<void> | null = null;

constructor(private readonly clientFactory: typeof createClient = createClient) {}

async getConnection(dbPath: string): Promise<TursoDb> {
if (this.closingPromise) {
await this.closingPromise;
}
const closingConnection = this.closingConnections.get(dbPath);
if (closingConnection) {
await closingConnection;
}
assertPathInsideStorage(dbPath);

const existing = this.connections.get(dbPath);
Expand All @@ -46,7 +54,7 @@ export class TursoConnectionManager {
mkdirSync(dir, { recursive: true });
}

const client: Client = createClient({ url: toFileUrl(dbPath) });
const client: Client = this.clientFactory({ url: toFileUrl(dbPath) });
try {
const db = new TursoDb(client);
await db.execute("PRAGMA foreign_keys = ON");
Expand Down Expand Up @@ -75,23 +83,47 @@ export class TursoConnectionManager {
}

async closeConnection(dbPath: string): Promise<void> {
const db = this.connections.get(dbPath);
if (!db) return;

try {
await db.close();
} catch (error) {
log("Error closing Turso database", { path: dbPath, error: String(error) });
if (this.closingPromise) {
await this.closingPromise;
}
const existingClose = this.closingConnections.get(dbPath);
if (existingClose) {
return existingClose;
}

const closePromise = Promise.resolve()
.then(async () => {
const pending = this.pending.get(dbPath);
if (pending) {
await Promise.allSettled([pending]);
}

const db = this.connections.get(dbPath);
if (db) {
try {
await db.close();
} catch (error) {
log("Error closing Turso database", { path: dbPath, error: String(error) });
}

this.connections.delete(dbPath);
}

await collectReleasedSqliteHandles();
})
.finally(() => {
this.closingConnections.delete(dbPath);
});

this.connections.delete(dbPath);
this.closingConnections.set(dbPath, closePromise);
return closePromise;
}

async closeAll(): Promise<void> {
if (this.closingPromise) return this.closingPromise;

this.closingPromise = (async () => {
await Promise.allSettled(this.pending.values());
await Promise.allSettled([...this.pending.values(), ...this.closingConnections.values()]);
for (const [path, db] of this.connections) {
try {
await db.close();
Expand All @@ -101,6 +133,7 @@ export class TursoConnectionManager {
}
this.connections.clear();
this.pending.clear();
await collectReleasedSqliteHandles();
})();

try {
Expand Down
10 changes: 6 additions & 4 deletions src/services/turso/legacy-migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CONFIG } from "../../config.js";
import { log } from "../logger.js";
import { tursoConnectionManager } from "./connection-manager.js";
import { tursoShardManager } from "./shard-manager.js";
import { withSqliteFileLockRetry } from "./sqlite-handle-release.js";
import { tursoVectorSearch } from "./vector-search.js";
import { blobToFloat32Array } from "./vector-utils.js";
import type { MemoryRecord } from "./types.js";
Expand Down Expand Up @@ -228,10 +229,10 @@ async function restoreFromBackup(dbPath: string): Promise<void> {
await tursoConnectionManager.closeConnection(dbPath);

if (existsSync(dbPath)) {
unlinkSync(dbPath);
await withSqliteFileLockRetry(() => unlinkSync(dbPath));
}

renameSync(backup, dbPath);
await withSqliteFileLockRetry(() => renameSync(backup, dbPath));

const sidecarPathFile = sidecarPath(dbPath);
if (existsSync(sidecarPathFile)) {
Expand Down Expand Up @@ -327,7 +328,7 @@ async function migrateMemoryShard(dbPath: string): Promise<ShardMigrationSidecar
return readSidecar(dbPath);
}

const db = await tursoConnectionManager.getConnection(dbPath);
let db: TursoDb | null = await tursoConnectionManager.getConnection(dbPath);
const hasTable = await hasMemoriesTable(db);

if (!hasTable) {
Expand Down Expand Up @@ -419,9 +420,10 @@ async function migrateMemoryShard(dbPath: string): Promise<ShardMigrationSidecar
});

const backup = backupPath(dbPath);
db = null;
await tursoConnectionManager.closeConnection(dbPath);
if (existsSync(dbPath)) {
renameSync(dbPath, backup);
await withSqliteFileLockRetry(() => renameSync(dbPath, backup));
}

const freshDb = await tursoConnectionManager.getConnection(dbPath);
Expand Down
7 changes: 4 additions & 3 deletions src/services/turso/shard-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { assertSafeScopeHash } from "../memory-scope.js";
import { tursoConnectionManager } from "./connection-manager.js";
import { log } from "../logger.js";
import { assertNoTursoMigrationInProgress } from "./operation-lock.js";
import { withSqliteFileLockRetry } from "./sqlite-handle-release.js";
import type { ShardInfo } from "./types.js";
import type { TursoDb } from "./turso-db.js";

Expand Down Expand Up @@ -504,7 +505,7 @@ export class TursoShardManager {

try {
if (existsSync(fullPath)) {
unlinkSync(fullPath);
await withSqliteFileLockRetry(() => unlinkSync(fullPath));
}
} catch (error) {
log("Error deleting shard file", {
Expand All @@ -526,13 +527,13 @@ export class TursoShardManager {
const archivePath = `${fullPath}.${reason}-${process.pid}-${Date.now()}.bak`;

if (existsSync(fullPath)) {
renameSync(fullPath, archivePath);
await withSqliteFileLockRetry(() => renameSync(fullPath, archivePath));
}
try {
await metadataDb.run(`DELETE FROM shards WHERE id = ?`, [shardId]);
} catch (error) {
if (existsSync(archivePath) && !existsSync(fullPath)) {
renameSync(archivePath, fullPath);
await withSqliteFileLockRetry(() => renameSync(archivePath, fullPath));
}
throw error;
}
Expand Down
Loading