Files
fusion/scripts/lib/backend-db.mjs
gsxdsm ac67b8d585 fix(scripts): the FN-4000 consistency reconciler failed in BOTH directions on a renamed board (#2994)
## The FN-4000 consistency reconciler failed in *both* directions

`findTaskStateInconsistencies` keyed both checks on legacy lane
literals, and they break in opposite ways:

```js
const hasDoneTransient = task.column === "done" && (status failed || error || worktree || blockedBy || …);
if (task.status === "failed" && task.column !== "in-review") { … }
```

| check | on a renamed board | effect |
| --- | --- | --- |
| `hasDoneTransient` | **never fires** | a finished card still holding
`status:"failed"`, a worktree, a blockedBy or live recovery counters is
never reported and never normalized — precisely the stale state FN-4000
exists to clear |
| `failed-status-outside-in-review` | **fires for every failed card** |
no column equals the literal, so the report lists the whole board |

The second is the more dangerous of the two: a tool that reports nothing
looks broken, but a tool that reports everything looks like it is
working.

## Wiring, and why the resolver is injected rather than built inline

Lanes are resolved **per task** (a board can span workflows) and passed
in. Resolving inside the loop would drag `importCore()` — and therefore
a built `packages/core/dist` — into every unit test of a pure
reconciliation loop.

`main` wires the real resolver whenever it opened a real backend, so
this is **not** the inert optional-parameter shape this migration keeps
finding. A caller injecting its own store (tests) has no staged dist and
falls back to the documented legacy literals, which is exactly today's
behaviour.

`importCore` is now exported from `scripts/lib/backend-db.mjs` so
operator scripts reach core helpers through the **same staged-dist seam
`openBackend` already uses**, rather than each growing its own dist path
— `@fusion/core` is not resolvable from repo-root `scripts/`, which is
what made the obvious import fail.

The normalization move now targets the card's **own** column: naming
`"done"` was only ever a way of spelling *"where it already is"*, since
the move exists to trigger the store's done-normalization.

## One of my test expectations was wrong before the code was

My first version asserted that a card in a renamed complete lane with
`status:"failed"` yields only the transient-state finding. It yields
**both** — and that is correct, because a failed card outside the review
lane genuinely is flagged. I isolated the case (dropping
`status:"failed"`, keeping the worktree) so it pins one behaviour
instead of blurring two, rather than "fixing" the expectation to match
whatever came out.

## Revert proof

Restoring the four literals:

```
✖ reports stale transient state in a RENAMED complete lane
✖ does NOT flag a failed card that is sitting in the board's own review lane
✖ runReconciliation normalizes a renamed complete lane by moving the card to its OWN column
ℹ pass 5   ℹ fail 3
```

The remaining two new cases pass both ways by design — "still flags a
failed card outside the resolved review lane" and "unresolved lanes keep
exactly the legacy behaviour" guard against over-correction, so I am not
counting them as coverage of the defect.

## Verification (measured)

- `node --test` — **8 passed / 0 failed** (3 pre-existing + 5 new)
- sibling script suites (`recover-stale-blocked-by`,
`reconcile-leaked-soft-deletes`) — **7 passed**, unaffected by the
shared-lib export
- `node --check`, `eslint` — clean
- `lifecycle-column-census --strict`, `check-sql-column-literals`,
`check-lane-wiring`, `check-fnxc-future-dates` — green

No changeset: root `scripts/` is repo tooling, not part of the published
package.

## Still not addressed in this territory

`reconcile-leaked-soft-deletes.mjs` carries a raw `UPDATE
project."tasks" SET "column" = 'archived'` — on a renamed board that
writes a column the workflow does not declare, creating the
undeclared-column state this migration keeps repairing elsewhere. It
holds a raw backend rather than a store, so it needs the same
`importCore` seam this PR exports; left for a follow-up rather than
bundled here.
2026-07-30 23:49:05 -07:00

99 lines
4.2 KiB
JavaScript

/**
* FNXC:PostgresCutover 2026-07-05-13:00:
* Shared PostgreSQL backend access for operational scripts.
*
* The ops/maintenance scripts under scripts/ used to open `.fusion/fusion.db`
* directly (node:sqlite or the sqlite3 CLI). After the PostgreSQL cutover the
* live data lives in the embedded PostgreSQL cluster (or an external cluster
* via DATABASE_URL), so a direct SQLite open would silently operate on a
* stale/empty marker file. Every script now boots the real backend through
* @fusion/core's startup factory via this helper.
*
* Requires packages/core to be built (`pnpm --filter @fusion/core build`).
*/
import { cpSync, existsSync, readdirSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..");
/**
* Stage the PostgreSQL migration SQL into core's dist. `tsc` emits
* only JS, so dist lacks src/postgres/migrations/*.sql; the schema applier
* resolves them relative to the compiled file (__dirname/migrations). The CLI
* bundle does the same staging in packages/cli/tsup.config.ts.
*/
function ensureMigrationsStaged() {
const src = resolve(repoRoot, "packages/core/src/postgres/migrations");
const dest = resolve(repoRoot, "packages/core/dist/postgres/migrations");
/*
* FNXC:AutomationIsolation 2026-07-13-22:37:
* Operational scripts must stage every versioned PostgreSQL migration, not merely the initial baseline, so an already-initialized database receives the automation project-isolation upgrade before scripts open it.
*/
const requiredMigrations = existsSync(src)
? readdirSync(src).filter((file) => file.endsWith(".sql"))
: [];
if (existsSync(src) && requiredMigrations.some((file) => !existsSync(resolve(dest, file)))) {
cpSync(src, dest, { recursive: true });
}
}
/* FNXC:OperatorScriptLaneAssumptions 2026-07-30-26:10: exported so operator scripts can reach core
helpers (lane resolution) through the SAME staged-dist seam `openBackend` already uses, rather than
each growing its own dist path — `@fusion/core` is not resolvable from the repo-root `scripts/`. */
export async function importCore() {
ensureMigrationsStaged();
try {
return await import(pathToFileURL(resolve(repoRoot, "packages/core/dist/index.js")).href);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new Error(
`Unable to import packages/core/dist/index.js (${message}). Run: pnpm --filter @fusion/core build`,
);
}
}
/**
* Boot a PostgreSQL-backed TaskStore for the given project root.
*
* Returns `{ core, store, asyncLayer, sql, schema, shutdown }`:
* - `core` — the @fusion/core module (for helpers like recordRunAuditEvent).
* - `store` — the initialized TaskStore (backend mode).
* - `asyncLayer` — the AsyncDataLayer; `asyncLayer.db.execute(sql\`...\`)`
* runs raw SQL. Project tables are schema-qualified (`project."tasks"`).
* - `sql` — the drizzle-orm `sql` template tag (re-exported as drizzleSql).
* - `schema` — postgresSchema (drizzle table objects, e.g. schema.project.tasks).
* - `shutdown` — releases the pool and stops an embedded cluster this boot
* started. Always call it in `finally`.
*
* Throws when PostgreSQL cannot start. These scripts must never fall back to
* the removed SQLite runtime.
*/
export async function openBackend(rootDir = process.cwd()) {
const core = await importCore();
const boot = await core.createTaskStoreForBackend({ rootDir });
const asyncLayer = boot.taskStore.getAsyncLayer();
if (!asyncLayer) {
await boot.shutdown().catch(() => {});
throw new Error("Backend TaskStore has no AsyncDataLayer; cannot run this script.");
}
return {
core,
store: boot.taskStore,
asyncLayer,
sql: core.drizzleSql,
schema: core.postgresSchema,
shutdown: boot.shutdown,
};
}
/**
* Normalize a drizzle `db.execute(...)` result to a plain array of rows.
* postgres-js returns a RowList (array-like); this keeps call sites simple.
*/
export function rowsOf(result) {
if (Array.isArray(result)) return [...result];
if (result && Array.isArray(result.rows)) return [...result.rows];
return [];
}