Skip to content

fix(domain): seed scope import → 400 for bad kind / slug collision (no opaque 500) - #144

Merged
mrchatam merged 1 commit into
mainfrom
fix/p44-seed-scope-validation
Sep 26, 2026
Merged

mrchatam merged 1 commit into
mainfrom
fix/p44-seed-scope-validation

Conversation

@mrchatam

Copy link
Copy Markdown
Owner

Problem

ImportSeedScope (internal/domain/seed_import.go) passes seed scopes straight to store.UpsertScope. When a client sends a bad seed, POST /v1/seed/import answers with an opaque 500 INTERNAL_ERROR "internal error" (the #104/#114 class):

  • Invalid kind ("Feature", "", …) is only rejected by the store, with a plain fmt.Errorf, which is not a *domain.ErrValidation.
  • Slug collision: a seed scope whose slug already exists under a different id fails with a raw UNIQUE constraint failed: scopes.slug (2067). This also blocks merging seeds from two clones that each created the same slug.

Probe on main: both cases are non-ErrValidation, and the new HTTP tests get 500 {"error":{"code":"INTERNAL_ERROR"}}.

Fix

  • The domain checks kind against feature/layer/business and returns ErrValidation naming the slug.
  • The slug is looked up before the upsert:
    • Same slug with an explicit, different id → ErrValidation: scope slug "x" already exists with id A while seed has id B.
    • Same slug and no seed id → reuse the existing id. This makes it an idempotent upsert by slug, like CreateScope.
  • The import is still atomic, so the whole seed rolls back on these errors.
  • The diff also carries gofmt normalization of the SeedImportSummary alignment. That file was listed by gofmt -l on main; there is no semantic change.

Tests

  • internal/domain/scope_seed_validation_test.go: bad kind, slug collision, re-import with the same id+slug (exactly one entity.created), missing slug/title, empty id with an existing slug (no duplicate scope, a single entity.created).
  • internal/httpapi/seed_scope_validation_test.go: bad kind → 400 VALIDATION_ERROR; slug collision → 400 VALIDATION_ERROR, with both ids in the message.

The bad-kind, collision and empty-id domain tests and both HTTP tests fail on main and pass here. CGO_ENABLED=1 go test ./... passes locally.

Patch produced by the Kilo worker (Nemotron 3 Ultra; task kw-20260926-192614-f795) and independently reviewed and tested by the supervisor. It took 2 revisions: the first fixed compile errors and added the empty-id slug reuse; the second finished after the worker stopped partway through.

@coderabbitai review

…lug collision

ImportSeedScope now validates kind in the domain and detects a slug that
already exists under a different explicit id, returning ErrValidation (HTTP
400) instead of a plain store error / raw UNIQUE constraint failure (500).
A seed scope without id reuses the existing scope for its slug (idempotent,
matching CreateScope).

Patch produced by the Kilo worker (Nemotron 3 Ultra), independently reviewed.
@coderabbitai

coderabbitai Bot commented Sep 26, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 46 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository: mrchatam/Trace/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: dfaef4d7-8cce-4363-8ec5-b67413f7b9fc

📥 Commits

Reviewing files that changed from the base of the PR and between 908d498 and 8342b7c.

📒 Files selected for processing (3)
  • internal/domain/scope_seed_validation_test.go
  • internal/domain/seed_import.go
  • internal/httpapi/seed_scope_validation_test.go

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@mrchatam
mrchatam merged commit 993ab3e into main Sep 26, 2026
6 checks passed
@mrchatam
mrchatam deleted the fix/p44-seed-scope-validation branch September 30, 2026 21:15
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.

1 participant