Skip to content

Make topological_sort's level-0 seed insert idempotent#18

Merged
Gabrielebattimelli merged 1 commit into
mainfrom
add-agent-skill
Jul 8, 2026
Merged

Make topological_sort's level-0 seed insert idempotent#18
Gabrielebattimelli merged 1 commit into
mainfrom
add-agent-skill

Conversation

@Gabrielebattimelli

Copy link
Copy Markdown
Member

Why

With #17 (defer DB connection), the pipeline finally ran end-to-end through the analysis, module, symbol, and declaration loads (~50 min of DB writes, all succeeded) and reached the final step, `topological_sort()`, which crashed:

```
psycopg.errors.UniqueViolation: duplicate key value violates unique constraint "level_pkey"
DETAIL: Key (symbol_name)=(["ContinuousLinearMap", "adjoint", "isBoundedBilinearMap_real"]) already exists.
```

Root cause

Every other INSERT in `jixia_db.py` (`load_module`, `load_symbol`, both dependency inserts, `load_declaration`) uses `ON CONFLICT DO NOTHING`, since `module`/`symbol`/`dependency`/`declaration` accumulate across incremental runs. The iterative level insert in `topological_sort` already guards against re-processing via `WHERE NOT EXISTS (SELECT 1 FROM level l WHERE l.symbol_name = e.source)` — but the level-0 seed insert has no equivalent guard, so it collided on a symbol that already had a `level` row from an earlier successful indexing pass.

What

Add `ON CONFLICT DO NOTHING` to both level inserts, matching the established pattern in the rest of the file.

The full pipeline finally ran end-to-end through analysis, module,
symbol, and declaration loads (~50 min), reaching topological_sort --
which crashed:

  psycopg.errors.UniqueViolation: duplicate key value violates unique
  constraint "level_pkey"

Every other INSERT in this file (load_module, load_symbol, both
dependency inserts, load_declaration) uses ON CONFLICT DO NOTHING so
re-runs against a DB with prior data are safe. The iterative level
insert already guards against this via 'WHERE NOT EXISTS (SELECT 1
FROM level l ...)', but the level-0 seed insert had no such guard, so
it collided on symbols that already had a level from an earlier
successful run. Add ON CONFLICT DO NOTHING to match.
Copilot AI review requested due to automatic review settings July 8, 2026 00:16
@Gabrielebattimelli Gabrielebattimelli merged commit ffcf1f1 into main Jul 8, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes database/jixia_db.py’s topological_sort() resilient to incremental re-runs by making the level seeding/incremental inserts idempotent (avoiding UniqueViolation on already-seen symbols).

Changes:

  • Add ON CONFLICT DO NOTHING to the level-0 seed insert into level.
  • Add ON CONFLICT DO NOTHING to the iterative level insert into level.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread database/jixia_db.py
Comment on lines 182 to 186
WHERE NOT EXISTS (SELECT 1 FROM dependency e WHERE e.source = v.name)
ON CONFLICT DO NOTHING
""")
while cursor.rowcount:
logger.info("topological sort: %d rows affected", cursor.rowcount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants