Skip to content

[Bug]: beta.2 state migration creates agent_id index before adding column, blocking gateway startup #109867

Description

@lamkan0210

Bug type

Regression (worked before, now fails)

Beta release blocker

No

Summary

After upgrading from 2026.7.2-beta.1 to 2026.7.2-beta.2, the shared SQLite migration creates indexes referencing managed_outgoing_image_records.agent_id before adding that column, so doctor --fix fails with no such column: agent_id and the gateway cannot start.

Steps to reproduce

  1. Start with a working macOS installation on 2026.7.2-beta.1 whose shared state database still has the pre-beta.2 managed_outgoing_image_records shape (no agent_id, original_media_root, or cleanup_pending columns).
  2. Upgrade with npm i -g [email protected].
  3. Run openclaw gateway restart.
  4. Run openclaw doctor --fix --non-interactive as instructed by the error.
  5. Observe that schema repair fails with Error: no such column: agent_id; the LaunchAgent remains stopped and port 18789 remains free.

The affected table was empty in this reproduction, and PRAGMA integrity_check returned ok before repair.

Expected behavior

The beta.2 state migration should add the new additive columns before executing schema SQL that creates indexes using them, then complete the STRICT-table and operator-approval migrations. The gateway should restart normally.

Actual behavior

openclaw gateway restart and openclaw doctor --fix both fail on the same schema error. The service remains stopped and the dashboard is unreachable.

The installed beta.2 code in repairOpenClawStateDatabaseSchema() runs the canonical schema assertion/DDL before ensureAdditiveStateColumns(). The canonical schema includes indexes on (session_key, agent_id, ...), while the additive step that adds managed_outgoing_image_records.agent_id runs later.

OpenClaw version

2026.7.2-beta.2 (54d98a4)

Operating system

macOS 27.0 (26A5378n), Apple Silicon

Install method

npm global (npm i -g [email protected]), macOS LaunchAgent

Model

Not model-dependent; failure occurs during state migration before the gateway accepts requests.

Provider / routing chain

Not applicable; local CLI -> shared SQLite state migration -> LaunchAgent gateway startup.

Additional provider/model setup details

No provider or model request is required to reproduce the failure.

Logs

Config health-state write failed: OpenClaw state database ~/.openclaw/state/openclaw.sqlite has a legacy operator approval schema; run openclaw doctor --fix to migrate it.

Doctor warnings:
- Failed migrating shared state database schema at
  ~/.openclaw/state/openclaw.sqlite: Error: no such column: agent_id

[restart] failed to write gateway restart intent: Error: OpenClaw state database ~/.openclaw/state/openclaw.sqlite has a legacy operator approval schema; run openclaw doctor --fix to migrate it.

Gateway restart failed after 16s: service stayed stopped and port 18789 stayed free.
Service runtime: status=stopped, state=active
Gateway port 18789 status: free.

Pre-repair table shape:

CREATE TABLE managed_outgoing_image_records (
  attachment_id TEXT NOT NULL PRIMARY KEY,
  session_key TEXT NOT NULL,
  message_id TEXT,
  created_at TEXT NOT NULL,
  updated_at TEXT,
  retention_class TEXT,
  alt TEXT NOT NULL,
  original_media_id TEXT NOT NULL,
  original_media_subdir TEXT NOT NULL,
  original_content_type TEXT NOT NULL,
  original_width INTEGER,
  original_height INTEGER,
  original_size_bytes INTEGER,
  original_filename TEXT,
  record_json TEXT NOT NULL
);

Verified recovery after taking a database backup:

ALTER TABLE managed_outgoing_image_records ADD COLUMN original_media_root TEXT NOT NULL;
ALTER TABLE managed_outgoing_image_records ADD COLUMN agent_id TEXT;
ALTER TABLE managed_outgoing_image_records ADD COLUMN cleanup_pending INTEGER NOT NULL DEFAULT 0 CHECK (cleanup_pending IN (0, 1));

Then:

$ openclaw doctor --fix --non-interactive
Migrated shared state operator approvals -> OpenClaw system changes
Migrated shared state tables to SQLite STRICT typing (83)

$ openclaw gateway status --deep
Runtime: running
Connectivity probe: ok
Capability: admin-capable
Listening: *:18789

Screenshots, recordings, and evidence

Direct CLI output and installed release code were inspected. No screenshot is required to observe the deterministic failure.

Impact and severity

Affected: installations upgrading to beta.2 with this legacy shared-state table shape.

Severity: high for affected installations because the gateway does not start and all dashboard/channel access is unavailable.

Frequency: reproduced on the affected database on every restart/doctor attempt until the missing columns were added.

Consequence: complete local gateway outage requiring manual SQLite repair.

Additional information

Last known working version on this state database: 2026.7.2-beta.1.

First observed failing version: 2026.7.2-beta.2.

Suggested fix: run ensureAdditiveStateColumns(db) before any canonical schema assertion/DDL that creates indexes using additive columns, and add a migration fixture containing the exact pre-beta.2 managed_outgoing_image_records schema. The fixture should verify that doctor --fix completes without manual SQL and results in schema version 3 with PRAGMA integrity_check = ok.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P0Emergency: data loss, security bypass, crash loop, or unusable core runtime.clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.clawsweeper:queueable-fixClawSweeper marked this issue as an existing queue_fix_pr work candidate.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:crash-loopCrash, hang, restart loop, or process-level availability failure.impact:ux-release-blockerA non-technical user is blocked without terminal, logs, config, or support.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.no-staleExclude from stale automation

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions