Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
feat(persistence): persistence_worktree_refreshes table (F0 placeholder migration f0_worktree_refreshes)
garrytan9aff60d378b2
diff --git a/scripts/build-schema.ts b/scripts/build-schema.tsindex 649d7c3287..c5011158c1 100644--- a/scripts/build-schema.ts+++ b/scripts/build-schema.ts@@ -35,6 +35,7 @@ import { PAGE_PROJECTION_SCHEMA_SQL } from '../src/core/page-state/projection-scimport { PAGE_STATE_SCHEMA_SQL } from '../src/core/page-state/schema.ts';import { POSTGRES_CONCURRENT_PERSISTENCE_INDEXES, PERSISTENCE_SCHEMA_STATEMENTS } from '../src/core/persistence/schema.ts';import { PERSISTENCE_TOPOLOGY_SCHEMA_SQL } from '../src/core/persistence/topology-schema.ts';+import { WORKTREE_REFRESH_SCHEMA_SQL } from '../src/core/persistence/worktree-refresh-schema.ts';import { SHARED_SKILLS_SCHEMA_SQL } from '../src/core/shared-skills/schema-all.ts';import { DECIDE_SCHEMA_SQL } from '../src/core/ai/decide/schema.ts';import { FACT_RELINK_SCHEMA_SQL } from '../src/core/facts/relink-schema.ts';@@ -89,6 +90,7 @@ export const FRAGMENTS: readonly Fragment[] = [},{ source: 'src/core/page-state/projection-schema.ts', expr: 'PAGE_PROJECTION_SCHEMA_SQL', postgres: PAGE_PROJECTION_SCHEMA_SQL, pglite: PAGE_PROJECTION_SCHEMA_SQL },{ source: 'src/core/persistence/topology-schema.ts', expr: 'PERSISTENCE_TOPOLOGY_SCHEMA_SQL', postgres: PERSISTENCE_TOPOLOGY_SCHEMA_SQL, pglite: PERSISTENCE_TOPOLOGY_SCHEMA_SQL },+ { source: 'src/core/persistence/worktree-refresh-schema.ts', expr: 'WORKTREE_REFRESH_SCHEMA_SQL', postgres: WORKTREE_REFRESH_SCHEMA_SQL, pglite: WORKTREE_REFRESH_SCHEMA_SQL },{ source: 'src/core/company-brain/receipt-schema.ts', expr: 'SOURCE_INGESTION_RECEIPTS_SCHEMA_SQL', postgres: SOURCE_INGESTION_RECEIPTS_SCHEMA_SQL, pglite: SOURCE_INGESTION_RECEIPTS_SCHEMA_SQL },{ source: 'src/core/shared-skills/schema-all.ts', expr: 'SHARED_SKILLS_SCHEMA_SQL', postgres: SHARED_SKILLS_SCHEMA_SQL, pglite: SHARED_SKILLS_SCHEMA_SQL },{ source: 'src/core/ai/decide/schema.ts', expr: 'DECIDE_SCHEMA_SQL', postgres: DECIDE_SCHEMA_SQL, pglite: DECIDE_SCHEMA_SQL },diff --git a/src/core/persistence/worktree-refresh-schema.ts b/src/core/persistence/worktree-refresh-schema.tsnew file mode 100644index 0000000000..0f94a15c58--- /dev/null+++ b/src/core/persistence/worktree-refresh-schema.ts@@ -0,0 +1,57 @@+/**+ * `gbrain sources refresh` storage (F0): one row per coordinated fast-forward+ * of a managed worktree. The canonical DDL copy is used by the schema+ * migration and, through scripts/build-schema.ts FRAGMENTS, by fresh installs.+ * The partial unique index makes "one active refresh per worktree" a database+ * fact; its unique violation maps to `refresh_in_progress`.+ *+ * The predicate constants live here, beside the DDL and free of imports, so+ * journal.ts and effect-journal.ts can embed them without an import cycle.+ */++export const WORKTREE_REFRESH_SCHEMA_SQL = `+CREATE TABLE IF NOT EXISTS persistence_worktree_refreshes (+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),+ worktree_id uuid NOT NULL REFERENCES persistence_worktrees(id),+ source_ids text[] NOT NULL,+ principal_id uuid NOT NULL,+ owner_epoch bigint NOT NULL,+ topology_generation bigint NOT NULL,+ state text NOT NULL CHECK (state IN ('draining','fenced','merged','syncing','completed','aborted','recovery_required')),+ old_head text NOT NULL,+ target_head text NOT NULL,+ upstream_ref text NOT NULL,+ preserved_uncommitted text[] NOT NULL DEFAULT '{}',+ outcome jsonb NOT NULL DEFAULT '{}',+ created_at timestamptz NOT NULL DEFAULT now(),+ updated_at timestamptz NOT NULL DEFAULT now(),+ completed_at timestamptz+);+CREATE UNIQUE INDEX IF NOT EXISTS persistence_worktree_refreshes_active+ ON persistence_worktree_refreshes(worktree_id)+ WHERE state IN ('draining','fenced','merged','syncing','recovery_required');+`;++export const ACTIVE_REFRESH_STATES = ['draining', 'fenced', 'merged', 'syncing', 'recovery_required'] as const;+export type WorktreeRefreshState = typeof ACTIVE_REFRESH_STATES[number] | 'completed' | 'aborted';++/** SQL list of the active states, matching the partial unique index. */+export const ACTIVE_REFRESH_STATES_SQL = "('draining','fenced','merged','syncing','recovery_required')";++/**+ * Effect kinds that touch the checkout. Embedding and facts-backstop effects+ * read only the database, so a refresh neither waits for them nor stops them.+ */+export const CHECKOUT_EFFECT_KINDS_SQL = "('git','withdrawal-mirror')";++/**+ * Claim predicate for a row aliased `alias` with a `worktree_id` column: while+ * a refresh holds the checkout fence (`fenced`, `merged`) or its HEAD is+ * unverified (`recovery_required`), nothing on that worktree is claimed.+ * `draining` and `syncing` keep claiming, so queued work drains and the+ * refresh's own managed sync publishes.+ */+export function refreshFenceClear(alias: string): string {+ return `NOT EXISTS (SELECT 1 FROM persistence_worktree_refreshes fence WHERE fence.worktree_id=${alias}.worktree_id+ AND fence.state IN ('fenced','merged','recovery_required'))`;+}diff --git a/src/core/pglite-schema.generated.ts b/src/core/pglite-schema.generated.tsindex 24451f0f17..27ad9e3e2b 100644--- a/src/core/pglite-schema.generated.ts+++ b/src/core/pglite-schema.generated.ts@@ -1321,6 +1321,27 @@ CREATE TABLE IF NOT EXISTS persistence_topology_changes (ALTER TABLE persistence_topology_changes ADD COLUMN IF NOT EXISTS intent_bytes bigint NOT NULL DEFAULT 0 CHECK(intent_bytes>=0);CREATE INDEX IF NOT EXISTS persistence_topology_recovering ON persistence_topology_changes(created_at) WHERE state='recovering';+CREATE TABLE IF NOT EXISTS persistence_worktree_refreshes (+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),+ worktree_id uuid NOT NULL REFERENCES persistence_worktrees(id),+ source_ids text[] NOT NULL,+ principal_id uuid NOT NULL,+ owner_epoch bigint NOT NULL,+ topology_generation bigint NOT NULL,+ state text NOT NULL CHECK (state IN ('draining','fenced','merged','syncing','completed','aborted','recovery_required')),+ old_head text NOT NULL,+ target_head text NOT NULL,+ upstream_ref text NOT NULL,+ preserved_uncommitted text[] NOT NULL DEFAULT '{}',+ outcome jsonb NOT NULL DEFAULT '{}',+ created_at timestamptz NOT NULL DEFAULT now(),+ updated_at timestamptz NOT NULL DEFAULT now(),+ completed_at timestamptz+);+CREATE UNIQUE INDEX IF NOT EXISTS persistence_worktree_refreshes_active+ ON persistence_worktree_refreshes(worktree_id)+ WHERE state IN ('draining','fenced','merged','syncing','recovery_required');+CREATE TABLE IF NOT EXISTS source_ingestion_receipts (id uuid PRIMARY KEY,source_id text NOT NULL REFERENCES sources(id) ON DELETE CASCADE,diff --git a/src/core/schema-embedded.generated.ts b/src/core/schema-embedded.generated.tsindex 278e3da555..95606091f8 100644--- a/src/core/schema-embedded.generated.ts+++ b/src/core/schema-embedded.generated.ts@@ -2029,6 +2029,29 @@ ALTER TABLE persistence_topology_changes ADD COLUMN IF NOT EXISTS intent_bytes bCREATE INDEX IF NOT EXISTS persistence_topology_recovering ON persistence_topology_changes(created_at) WHERE state='recovering';-- END GENERATED from src/core/persistence/topology-schema.ts (PERSISTENCE_TOPOLOGY_SCHEMA_SQL)+-- BEGIN GENERATED from src/core/persistence/worktree-refresh-schema.ts (WORKTREE_REFRESH_SCHEMA_SQL). Edit that file, then run: bun run build:schema+CREATE TABLE IF NOT EXISTS persistence_worktree_refreshes (+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),+ worktree_id uuid NOT NULL REFERENCES persistence_worktrees(id),+ source_ids text[] NOT NULL,+ principal_id uuid NOT NULL,+ owner_epoch bigint NOT NULL,+ topology_generation bigint NOT NULL,+ state text NOT NULL CHECK (state IN ('draining','fenced','merged','syncing','completed','aborted','recovery_required')),+ old_head text NOT NULL,+ target_head text NOT NULL,+ upstream_ref text NOT NULL,+ preserved_uncommitted text[] NOT NULL DEFAULT '{}',+ outcome jsonb NOT NULL DEFAULT '{}',+ created_at timestamptz NOT NULL DEFAULT now(),+ updated_at timestamptz NOT NULL DEFAULT now(),+ completed_at timestamptz+);+CREATE UNIQUE INDEX IF NOT EXISTS persistence_worktree_refreshes_active+ ON persistence_worktree_refreshes(worktree_id)+ WHERE state IN ('draining','fenced','merged','syncing','recovery_required');+-- END GENERATED from src/core/persistence/worktree-refresh-schema.ts (WORKTREE_REFRESH_SCHEMA_SQL)+-- BEGIN GENERATED from src/core/company-brain/receipt-schema.ts (SOURCE_INGESTION_RECEIPTS_SCHEMA_SQL). Edit that file, then run: bun run build:schemaCREATE TABLE IF NOT EXISTS source_ingestion_receipts (id uuid PRIMARY KEY,diff --git a/src/core/schema-migrations/registry.generated.ts b/src/core/schema-migrations/registry.generated.tsindex a15abb5b68..97148d0947 100644--- a/src/core/schema-migrations/registry.generated.ts+++ b/src/core/schema-migrations/registry.generated.ts@@ -193,6 +193,7 @@ import { v189 } from './v189-pages-credential-projection-pending.ts';import { v190 } from './v190-sources-upstream-observation.ts';import { v191 } from './v191-alias-source-cascade.ts';import { v192 } from './v192-take-embedding-identity.ts';+import { v193 } from './v193-f0-worktree-refreshes.ts';// Array order is historical (see HISTORICAL_ARRAY_ORDER in the generator); the// runner sorts by version before applying.@@ -384,4 +385,5 @@ export const MIGRATIONS: Migration[] = [v190,v191,v192,+ v193,];diff --git a/src/core/schema-migrations/v193-f0-worktree-refreshes.ts b/src/core/schema-migrations/v193-f0-worktree-refreshes.tsnew file mode 100644index 0000000000..cb3cffa72b--- /dev/null+++ b/src/core/schema-migrations/v193-f0-worktree-refreshes.ts@@ -0,0 +1,14 @@+import type { Migration } from './types.ts';+import { WORKTREE_REFRESH_SCHEMA_SQL } from '../persistence/worktree-refresh-schema.ts';++// F0 `gbrain sources refresh`: the durable checkpoint of a worktree-wide+// coordinated fast-forward (draining -> fenced -> merged -> syncing ->+// completed), with a partial unique index for one active refresh per worktree.+// DDL in src/core/persistence/worktree-refresh-schema.ts. Placeholder number:+// the Foundations integrator renumbers it.+export const v193: Migration = {+ version: 193,+ name: 'f0_worktree_refreshes',+ idempotent: true,+ sql: WORKTREE_REFRESH_SCHEMA_SQL,+};diff --git a/src/schema.sql b/src/schema.sqlindex a557d8d739..04fcf2cfa0 100644--- a/src/schema.sql+++ b/src/schema.sql@@ -2025,6 +2025,29 @@ ALTER TABLE persistence_topology_changes ADD COLUMN IF NOT EXISTS intent_bytes bCREATE INDEX IF NOT EXISTS persistence_topology_recovering ON persistence_topology_changes(created_at) WHERE state='recovering';-- END GENERATED from src/core/persistence/topology-schema.ts (PERSISTENCE_TOPOLOGY_SCHEMA_SQL)+-- BEGIN GENERATED from src/core/persistence/worktree-refresh-schema.ts (WORKTREE_REFRESH_SCHEMA_SQL). Edit that file, then run: bun run build:schema+CREATE TABLE IF NOT EXISTS persistence_worktree_refreshes (+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),+ worktree_id uuid NOT NULL REFERENCES persistence_worktrees(id),+ source_ids text[] NOT NULL,+ principal_id uuid NOT NULL,+ owner_epoch bigint NOT NULL,+ topology_generation bigint NOT NULL,+ state text NOT NULL CHECK (state IN ('draining','fenced','merged','syncing','completed','aborted','recovery_required')),+ old_head text NOT NULL,+ target_head text NOT NULL,+ upstream_ref text NOT NULL,+ preserved_uncommitted text[] NOT NULL DEFAULT '{}',+ outcome jsonb NOT NULL DEFAULT '{}',+ created_at timestamptz NOT NULL DEFAULT now(),+ updated_at timestamptz NOT NULL DEFAULT now(),+ completed_at timestamptz+);+CREATE UNIQUE INDEX IF NOT EXISTS persistence_worktree_refreshes_active+ ON persistence_worktree_refreshes(worktree_id)+ WHERE state IN ('draining','fenced','merged','syncing','recovery_required');+-- END GENERATED from src/core/persistence/worktree-refresh-schema.ts (WORKTREE_REFRESH_SCHEMA_SQL)+-- BEGIN GENERATED from src/core/company-brain/receipt-schema.ts (SOURCE_INGESTION_RECEIPTS_SCHEMA_SQL). Edit that file, then run: bun run build:schemaCREATE TABLE IF NOT EXISTS source_ingestion_receipts (id uuid PRIMARY KEY,