fix(schema): validate artifacts before forced init - #1446
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthrough
ChangesSchema init force validation
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant CommanderProgram
participant registerSchemaCommand
participant ArtifactValidation
participant Filesystem
CommanderProgram->>registerSchemaCommand: parseAsync schema init --force
registerSchemaCommand->>ArtifactValidation: collect and validate artifact inputs
ArtifactValidation-->>registerSchemaCommand: validation result
registerSchemaCommand->>Filesystem: remove existing schema after success
registerSchemaCommand->>Filesystem: create schema directory
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/commands/schema.test.ts`:
- Around line 288-317: Update the successful forced-init test around
runSchemaCommand to also assert that process.exitCode is 0, alongside the
existing created:true response and artifact checks. Ensure the assertion
verifies successful completion without changing the test’s existing setup or
validations.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: a4c9ee34-b2a4-4253-b04f-07afc4ba6bd1
📒 Files selected for processing (8)
.changeset/schema-init-validates-before-force.mdopenspec/changes/fix-schema-init-force-validation-order/.openspec.yamlopenspec/changes/fix-schema-init-force-validation-order/design.mdopenspec/changes/fix-schema-init-force-validation-order/proposal.mdopenspec/changes/fix-schema-init-force-validation-order/specs/schema-init-command/spec.mdopenspec/changes/fix-schema-init-force-validation-order/tasks.mdsrc/commands/schema.tstest/commands/schema.test.ts
alfred-openspec
left a comment
There was a problem hiding this comment.
Reviewed at 4005bb9. The validation-before-replacement ordering is correct; the focused command tests, build, strict change validation, and CI all pass.
Code review — verdict: good to mergeThis is the strongest PR in the current batch. Real data-loss bug, minimal fix, honest scoping, verified end-to-end. 1. Real need — yes, and it's a data-loss bugConfirmed against The command deletes the directory, then validates, then errors out — leaving nothing behind. A single-character typo destroys a hand-customized schema and all its templates, with no backup and no undo. That's about as clear-cut as a bug report gets, and the severity is higher than the diff size suggests. Same scenario on this branch: Note the error text and the valid-artifact list are byte-identical — only the destruction is gone. 2. Does it work — yes, and I checked the half that's easy to missThe interesting risk on a "move the destructive call later" fix is the opposite failure: that a successful Correct: the stale sentinel is gone, the directory is genuinely replaced rather than merged into, and the new artifact set is what was requested. I also read the code that now sits between the old Full suite: 112 files / 2255 tests, all passing (base main is 2253, so the two new command-level tests are additive; no existing expectation was modified). 3. Breaking changes — none
4. Scope — correctly surgical13 added / 9 removed lines in one function. No new abstractions, no rollback machinery, no temp-directory staging. The design doc's Non-Goals section is worth calling out approvingly:
That's the right call. Full transactional replacement (stage to temp, atomic rename) would drag in cross-platform rename semantics and turn a two-line reordering into a much riskier change. Fixing the deterministic, reachable, user-triggered failure and explicitly deferring the rare post-validation I/O failure is good engineering judgment, and it's documented rather than silently skipped. Notes (nothing blocking)
Nice, disciplined fix. |
Summary
Problem
openspec schema init --forceremoved the destination schema before validating--artifacts.When an unknown artifact such as
taskwas supplied, the command returned an error but had already deleted the existing schema.Root cause
The destination directory was removed before the artifact list was parsed and validated.
Fix
Defer removal of the existing schema until:
The existing error output, exit code, and successful force-overwrite behavior remain unchanged.
Tests
pnpm exec vitest run test/commands/schema.test.tspnpm run lintpnpm run buildopenspec validate fix-schema-init-force-validation-order --strictSummary by CodeRabbit
openspec schema init --forcenow validates requested artifact IDs before replacing an existing project schema.schema.yamland other existing files are preserved and the command exits with the expected error.