Skip to content

bug: historical scope backfill can leave GET /v1/scopes returning 422 after a successful upgrade #2847

Todo

Description

@Zxf-xufeng

Describe the bug

Early releases allowed records to carry a scope ID before Scope was stored as a separate entity. When the historical migration finds such an ID without a matching row in pc_scopes, it preserves the ID but inserts empty strings for both title and summary (#2394). Neither value satisfies the ScopeDescriptor API contract introduced in #1401. A single invalid descriptor prevents the entire page of otherwise valid scopes from being returned.

Steps to reproduce

  1. Start with a historical database that has a source referencing a scope ID but no corresponding descriptor. Include another scope with valid metadata.
  2. Upgrade through the historical migration to 1.6.0.
  3. Make an authenticated GET /v1/scopes?limit=50 with both scopes on the first page. Open the Dashboard scope picker without a search term.

Actual behavior

On an upgraded SeekDB installation, migration reported ready, yet the unfiltered list returned HTTP 422 (invalid_request, string_too_short for title and summary). The picker showed only the current scope. Searching specifically for a different, valid scope succeeded. The database contained a descriptor with two empty strings; whether that particular row already existed before upgrade has not been established.

Expected behavior and regression coverage

  • Upgrading historical data yields descriptors the API can return, without changing scope IDs, losing linked records, or overwriting valid metadata.
  • Provide a repair path for databases already marked ready that contain invalid descriptors.
  • Cover both cases with a regression test that checks the unfiltered scope list returns HTTP 200 and includes the recovered and unaffected scopes.

Environment

PowerContext 0.2.1 development build upgraded to 1.6.0; embedded SeekDB, Python 3.12, macOS.

Are you willing to submit a PR?

Yes, I would like to submit a PR.

Activity

  1. added
    bug
    on Nov 26, 2026
  2. Zxf-xufeng commented on Nov 26, 2026

    @Zxf-xufeng
    MemberAuthor

    Affected upgrade paths

    This issue affects upgrades across SQLite, embedded SeekDB, and OceanBase whenever a database goes through the historical migration backfill in historical._scopes() (introduced in #2394, shipped in v1.5.0+) and any pc_* table contains a scope_id without a matching row in pc_scopes:

    1. Direct upgrade from pre-pc_scopes releases (v0.0.1, v0.0.2, v0.1.0) to v1.5.0–v1.6.0 (with existing data)

      • pc_scopes was introduced in v0.2.0 (feat: implement scope organization runtime for RFC #1345 #1401).
      • During historical migration, historical._scopes() scans all pc_* tables with a scope_id column and inserts missing rows into pc_scopes with title = '' and summary = ''.
      • Those backfilled descriptors pass table constraints and domain-model loading, but fail HTTP ScopeDescriptor validation (min_length=1, pattern=".*\S.*") on GET /v1/scopes, causing the entire page to return HTTP 422.
    2. Multi-hop upgrade starting on <= v0.1.0, upgraded in place through v0.2.0–v1.4.x, then upgraded to v1.5.0–v1.6.0

      • Unmanaged schema initialization (create_all()) on v0.2.0–v1.4.x created pc_scopes without backfilling legacy scope_id values left in pc_sources, pc_artifacts, etc. (those tables do not have a foreign key to pc_scopes).
      • Because the database still lacks pc_schema_revision, upgrading to v1.5.0+ enters historical admission and runs historical._scopes() whenever any compatibility step is pending (historical_structure, historical_processing, or historical_atomic_memory), backfilling those orphaned scope_id values with empty strings.
    3. Subsequent upgrades from an already-migrated v1.5.0 / v1.5.1 / v1.5.2 database to v1.6.0

    4. Any pre-pc_schema_revision database (<= v1.4.x) containing orphaned scope_id values

      • Even if initialized on v0.2.0+, if any pc_* table contains a scope_id missing from pc_scopes (for example from direct database writes or unmanaged tooling), historical._scopes() will backfill it with title = '' and summary = ''.

    Paths not affected

    • Clean installations on v0.2.0+ that only wrote through normal Server APIs (where ScopeDraft/ScopeMutation require non-empty title and summary, and _scope_operation() verifies the scope exists before writes), as well as databases already managed under pc_schema_revision without pre-existing empty descriptors.

    Checking an upgraded database

    SELECT scope_id, title, summary
    FROM pc_scopes
    WHERE TRIM(title) = '' OR TRIM(summary) = '';
  3. DEMONCODEX commented on Nov 26, 2026

    @DEMONCODEX

    I'll take this.

  4. added
    module:core
    on Dec 2, 2026
  5. hireview001 commented on Dec 2, 2026

    @hireview001
    Collaborator

    This issue belongs to the Core module. @hidb4ai, please lead follow-up, with support from @frf12, @Fengzdadi, @beatenevo. Please provide a substantive response within 72 hours of issue creation, outlining the proposed disposition, any additional information needed, or the next investigation steps. After sufficient discussion and agreement on the scope and acceptance criteria, a contributor who volunteers to take it on can undertake implementation.

  6. hidb4ai commented on Dec 3, 2026

    @hidb4ai
    Contributor

    Confirmed the API failure with real SQLite and the current ASGI application. After creating a valid Scope, I seeded empty title/summary fields to represent an invalid historical descriptor. GET /v1/scopes?limit=50 then returned 422 with string_too_short validation errors, instead of a usable list.

    This reproduces the read-path symptom; it is not a full released-version upgrade replay. The repair in #2877 should cover both historical backfill and databases already admitted with invalid descriptors, preserve Scope IDs and valid metadata, and verify successful listing after repair/restart. The supported backend upgrade matrix still needs separate acceptance.

    Validation baseline: master at d896b799ab316a452e868565c228f6807818e734.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugmodule:core

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions