GeezerVison is showing github.com — fix(core): import legacy auth.json on fresh-database bootstrap · neumark/opencode@eedb493
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: FORK.md +5-1Lines changed: 5 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -54,6 +54,10 @@ v1: `45dfb2b` + `318f171` (hardened). `util/fence-breaker.ts` ported nearly verb | 54 | 54 |
| 55 | 55 | v1 served on the trusted tailnet with no auth; stock v2 always enforces a password (random when unset — rotating per boot, pairing screen per browser). The server layer already supports an open mode (`ServerAuth.required` is false for an empty password) but nothing could reach it: the CLI always generated one, and the pre-API `dispatch` layer enforced credentials independently of `required()`. `OPENCODE_SERVER_UNSECURED=1` (cli `server-process.ts`, read directly like the fork's other knobs — Effect Config treats an empty env value as unset, so the empty-`OPENCODE_SERVER_PASSWORD` spelling can't work) serves with an empty password; `ServerProcess.start`'s guard and `dispatch` now let the empty password through, and the middleware no-ops. Guests set it in the serve unit; `fc-sessions`/`fc-transcript-archive` detect the open mode (and verify journal passwords before trusting them — stale lines linger across mode switches). | 56 | 56 |
| | 57 | +### 9. Legacy auth.json import on fresh-database bootstrap (fix, v2-only) | | 58 | + | | 59 | +v2's `20260805200742_import_legacy_credentials` migration imports a v1 `~/.local/share/opencode/auth.json`, but only on the upgrade path: a FRESH database takes `DatabaseMigration.apply`'s bootstrap branch, which creates the full schema and records every migration id WITHOUT running any `up()` — so a fresh install over a pre-staged `auth.json` (exactly the fc-opencode guest model: secrets disk stages `auth.json` into every new VM) silently loses all provider credentials. The fork runs `importLegacyCredentials` inside the bootstrap transaction (idempotent — per-integration existence checks — and tolerant of a missing file), so a seeded `auth.json` lands in `credential` on first boot. The stop-side round trip (guest DB credentials exported back to the slot's `auth.json` at VM stop) lives in fc-opencode. | | 60 | + | 57 | 61 | ## The overlayfs plugin (rewrite) | 58 | 62 |
| 59 | 63 | v1: `~/.config/opencode/plugins/overlay-workspace.js` against `experimental_workspace`. v2 rewrite against the promise plugin API (`@opencode/plugin`, `Plugin.define`): | @@ -70,7 +74,7 @@ v1: `~/.config/opencode/plugins/overlay-workspace.js` against `experimental_work | 70 | 74 | - `fc-transcript-archive`: continuous capture until v2 grows an archive API — the plugin exports after every run reaches a terminal state and an hourly timer sweeps `--all` (same design as the deployment's claude-code hook in `docs/.claude/hooks/archive-transcript.sh`); deletion costs at most the in-flight run. Replaced by an archive-API hook when upstream ships one. | 71 | 75 | - `fc-sessions` / `fc-transcript-archive`: dual-version (v1 unauthenticated `/session`, v2 `/api/session` under basic auth with the password pulled from the guest's serve journal); v2 links are `/server/<base64url(origin)>/session/<id>`. | 72 | 76 | - Release resolver: unchanged — verified against `v2.0.27-neumark.3` (asset name `opencode-linux-x64.tar.gz`, `find`-based install, serve smoke test all match). | 73 | | -- Secrets channel: v2 imports legacy `~/.local/share/opencode/auth.json` credentials at migration — keep shipping `auth.json` through `secrets/vm$SLOT`. | | 77 | +- Secrets channel: v2 imports legacy `~/.local/share/opencode/auth.json` credentials — on the upgrade path at migration, and (fork feature 9) on fresh-database bootstrap. Keep shipping `auth.json` through `secrets/vm$SLOT`; fc-opencode's stop path exports guest DB credentials back into it. | 74 | 78 |
| 75 | 79 | ## Phases | 76 | 80 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/database/migration.ts +12-1Lines changed: 12 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | export * as DatabaseMigration from "./migration.js" | 2 | 2 |
| 3 | 3 | import { sql } from "drizzle-orm" | | 4 | +import path from "node:path" | 4 | 5 | import { Effect } from "effect" | 5 | 6 | import { supportsForeignKeyToggle } from "#sqlite" | 6 | 7 | import type { EffectDrizzleSqlite } from "./drizzle.js" | 7 | 8 | import { migrations } from "./migration.gen.js" | 8 | 9 | import schema from "./schema.gen.js" | 9 | 10 | import { Global } from "@opencode/util/global" | | 11 | +import { importLegacyCredentials } from "./migration/20260805200742_import_legacy_credentials.js" | 10 | 12 |
| 11 | 13 | type Database = EffectDrizzleSqlite.EffectSQLiteDatabase | 12 | 14 | type Transaction = Parameters<Parameters<Database["transaction"]>[0]>[0] | @@ -19,7 +21,7 @@ export type Migration = { | 19 | 21 |
| 20 | 22 | // Not serialized here: the Database layer holds a lock scoped to the database | 21 | 23 | // it is bootstrapping, since two instances over one file must not race. | 22 | | -export function apply(db: Database) { | | 24 | +export function apply(db: Database): Effect.Effect<void, unknown, Global.Service> { | 23 | 25 | return Effect.gen(function* () { | 24 | 26 | // OpenCode owns the unprefixed table namespace. Embedders sharing this | 25 | 27 | // database may own underscore-prefixed tables, which bootstrap ignores. | @@ -42,6 +44,15 @@ export function apply(db: Database) { | 42 | 44 | sql`INSERT INTO ${sql.identifier("migration")} (id, time_completed) VALUES (${migration.id}, ${Date.now()})`, | 43 | 45 | ), | 44 | 46 | ) | | 47 | + // (fork, v1 parity) fresh installs must still import a seeded legacy | | 48 | + // auth.json: bootstrap records every migration id without running its | | 49 | + // up(), so the v1->v2 credential import never executes on a fresh | | 50 | + // database — deployments that stage auth.json into a new machine | | 51 | + // (fc-opencode guests) silently lose every provider credential. Run | | 52 | + // the same import the upgrade path would have run; it is idempotent | | 53 | + // (per-integration existence check) and tolerates a missing file. | | 54 | + const global = yield* Global.Service | | 55 | + yield* importLegacyCredentials(tx, path.join(global.data, "auth.json")) | 45 | 56 | }), | 46 | 57 | ) | 47 | 58 | yield* Effect.logInfo("database schema bootstrap completed", { | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/database-migration.test.ts +49-3Lines changed: 49 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -193,6 +193,44 @@ describe("DatabaseMigration", () => { | 193 | 193 | ) | 194 | 194 | }) | 195 | 195 |
| | 196 | + test("fresh bootstrap imports a seeded legacy auth.json", async () => { | | 197 | + await using tmp = await tmpdir() | | 198 | + await Bun.write( | | 199 | + path.join(tmp.path, "auth.json"), | | 200 | + JSON.stringify({ "fireworks-ai": { type: "api", key: "fw-seeded" } }), | | 201 | + ) | | 202 | + | | 203 | + await run( | | 204 | + Effect.gen(function* () { | | 205 | + const db = yield* makeDb | | 206 | + yield* DatabaseMigration.apply(db) | | 207 | + | | 208 | + expect(yield* db.all(sql`SELECT integration_id, label, value FROM credential`)).toEqual([ | | 209 | + { | | 210 | + integration_id: "fireworks-ai", | | 211 | + label: "API key", | | 212 | + value: JSON.stringify({ type: "key", key: "fw-seeded" }), | | 213 | + }, | | 214 | + ]) | | 215 | + }), | | 216 | + Global.make({ data: tmp.path }), | | 217 | + ) | | 218 | + }) | | 219 | + | | 220 | + test("fresh bootstrap without auth.json imports no credentials", async () => { | | 221 | + await using tmp = await tmpdir() | | 222 | + | | 223 | + await run( | | 224 | + Effect.gen(function* () { | | 225 | + const db = yield* makeDb | | 226 | + yield* DatabaseMigration.apply(db) | | 227 | + | | 228 | + expect(yield* db.get(sql`SELECT count(*) AS count FROM credential`)).toEqual({ count: 0 }) | | 229 | + }), | | 230 | + Global.make({ data: tmp.path }), | | 231 | + ) | | 232 | + }) | | 233 | + | 196 | 234 | test("adds nullable attention state to existing sessions", async () => { | 197 | 235 | await run( | 198 | 236 | Effect.gen(function* () { | @@ -448,13 +486,16 @@ describe("DatabaseMigration", () => { | 448 | 486 | await run( | 449 | 487 | Effect.gen(function* () { | 450 | 488 | const db = yield* makeDb | | 489 | + // bootstrap imports the seeded auth.json wholesale (fork: v1 parity) | 451 | 490 | yield* DatabaseMigration.apply(db) | 452 | 491 | const now = Date.now() | 453 | 492 | yield* db.run(sql` | 454 | 493 | INSERT INTO credential (id, integration_id, label, value, time_created, time_updated) | 455 | | - VALUES ('existing', 'anthropic', 'Existing', ${JSON.stringify({ type: "key", key: "current-key" })}, ${now}, ${now}) | | 494 | + VALUES ('existing', 'existing-integration', 'Existing', ${JSON.stringify({ type: "key", key: "current-key" })}, ${now}, ${now}) | 456 | 495 | `) | 457 | 496 |
| | 497 | + // re-running the legacy import changes nothing: per-integration | | 498 | + // existence checks skip everything bootstrap already imported | 458 | 499 | yield* db.run(sql`DELETE FROM migration WHERE id = ${legacyCredentialsMigration.id}`) | 459 | 500 | yield* DatabaseMigration.applyOnly(db, [legacyCredentialsMigration]) | 460 | 501 | yield* DatabaseMigration.applyOnly(db, [legacyCredentialsMigration]) | @@ -463,14 +504,19 @@ describe("DatabaseMigration", () => { | 463 | 504 | [ | 464 | 505 | { | 465 | 506 | integration_id: "anthropic", | 466 | | - label: "Existing", | 467 | | - value: JSON.stringify({ type: "key", key: "current-key" }), | | 507 | + label: "API key", | | 508 | + value: JSON.stringify({ type: "key", key: "legacy-key", metadata: { region: "us" } }), | 468 | 509 | }, | 469 | 510 | { | 470 | 511 | integration_id: "custom-provider", | 471 | 512 | label: "API key", | 472 | 513 | value: JSON.stringify({ type: "key", key: "custom-key" }), | 473 | 514 | }, | | 515 | + { | | 516 | + integration_id: "existing-integration", | | 517 | + label: "Existing", | | 518 | + value: JSON.stringify({ type: "key", key: "current-key" }), | | 519 | + }, | 474 | 520 | { | 475 | 521 | integration_id: "github-copilot", | 476 | 522 | label: "OAuth", | |
|