Skip to content

fix(rosettify): merge steps by id in plan upsert entire_plan - #455

Merged
isolomatov-gd merged 3 commits into
griddynamics:mainfrom
xyrolle:fix/plan-upsert-entire-plan-merge-steps
Dec 1, 2026
Merged

isolomatov-gd merged 3 commits into
griddynamics:mainfrom
xyrolle:fix/plan-upsert-entire-plan-merge-steps

Conversation

@xyrolle

@xyrolle xyrolle commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Impact

rosettify plan upsert <file> entire_plan now merges the steps of an existing phase by id:

  • a step the patch names is merge-patched and keeps its status;
  • a step the patch does not name stays as it is;
  • a new step is appended.

Before, a steps array in the phase patch replaced the stored array. Steps the patch did not name were deleted, with their status. The call succeeded, so the caller got no signal. The only trace was the backup file.

A step without id in such a patch is now rejected with missing_id, and nothing is written. Before, it was stored in place of the phase's steps.

entire_plan and the phase target (upsert <file> <phase-id>) now patch an existing phase the same way.

Fixes #393.

Before and after

The reproduction from #393:

rosettify plan create e.json '{"name":"E","phases":[{"id":"p1","name":"P1","steps":[{"id":"s1","name":"S1","prompt":"x"},{"id":"s2","name":"S2","prompt":"x"}]}]}'
rosettify plan update_status e.json s1 complete
rosettify plan upsert e.json entire_plan '{"phases":[{"id":"p1","steps":[{"id":"s3","name":"S3","prompt":"x"}]}]}'

All three commands exit 0 before and after. Output of the third command, then the stored steps of p1 in e.json.

Before (main at 74e2f14):

{"plan":{"name":"E","status":"open","previous_version":"e.json.bak001"},"phases":[{"id":"p1","name":"P1","status":"open","steps":[{"id":"s3","name":"S3"}]}]}

{"id":"s3","name":"S3","prompt":"x"}

After:

{"plan":{"name":"E","status":"in_progress","previous_version":"e.json.bak001"},"phases":[{"id":"p1","name":"P1","status":"in_progress","steps":[{"id":"s1","name":"S1","status":"complete"},{"id":"s2","name":"S2","status":"open"},{"id":"s3","name":"S3"}]}]}

{"status":"complete","depends_on":[],"id":"s1","name":"S1","prompt":"x"}
{"status":"open","depends_on":[],"id":"s2","name":"S2","prompt":"x"}
{"id":"s3","name":"S3","prompt":"x"}

s3 has no status in either output. That is #390, which has its own PR.

Over MCP (tools/call on rosettify --mcp), the same three calls, then three more on the same file:

Call Before After
upsert entire_plan (the patch above), step ids in the result s3 s1, s2, s3
show_status p1 {"id":"p1","name":"P1","status":"open","steps":[{"id":"s3","name":"S3","status":"open"}]} {"id":"p1","name":"P1","status":"in_progress","steps":[{"id":"s1","name":"S1","status":"complete"},{"id":"s2","name":"S2","status":"open"},{"id":"s3","name":"S3","status":"open"}]}
upsert entire_plan with {"phases":[{"id":"p1","steps":[{"name":"no id","prompt":"x"}]}]} accepted; the result lists one step for p1, {"name":"no id"} isError: true, {"error":"missing_id"}
upsert p1 with {"steps":[{"name":"no id","prompt":"x"}]} isError: true, {"error":"missing_id"} the same

What changed

  • One function for both paths. The phase-target block of cmdUpsert in src/rosettify/src/commands/plan/upsert.ts moved into applyPhasePatch(phase, patch). Its logic is unchanged: merge steps by id with mergeById, merge-patch the other fields. The phase-target branch now calls it.
  • entire_plan uses it. applyEntirePlanPatch walks the patch phases. For an id that is already in the plan it calls applyPhasePatch. For a new id it appends a copy of the patch, as before. A phase without id gives missing_id, as before.
  • Why the phases no longer go through mergeById. mergeById patches an existing item with the generic mergePatch, and RFC 7396 replaces arrays. That is the defect. An existing phase needs the phase rule of FR-PLAN-0015 instead.
  • Requirement. FR-PLAN-0015: "(2) entire_plan with existing file: merges top-level fields; merges phases array by ID (existing patched, new appended). (3) Existing phase/step ID: merge-patches that item; if patch contains steps array, merges steps by ID." No requirement text changes.
  • First create. upsert entire_plan on a missing file runs the same function. It starts from a plan without phases, so a phase in the patch is appended whole, unless the same patch already added that id.

How it was verified

  • Tests. New file tests/unit/plan/upsert-entire-plan-steps.test.ts, 14 cases.

    • The [Bug]: rosettify plan upsert entire_plan replaces a phase's steps instead of merging them by id #393 reproduction through the plan dispatcher: the complete step is unchanged and still complete, the step the patch does not mention is unchanged, the new step is appended, and the plan status stays in_progress.
    • A patch that names a stored step and sends a phase field: both are applied, the other step fields are kept.
    • Two patches for the same phase id in one call, for an existing phase, for a new phase, and for a new phase of a new file.
    • A new phase next to a patched one is appended whole.
    • Six patches sent both ways, as upsert p1 <patch> and as upsert entire_plan {"phases":[{"id":"p1", …patch}]}: the error and the stored phases are equal. The six are a new step, a named step with a phase field, an empty steps array, a removed field without steps, a step without id, and a step id that another phase holds.
    • missing_id for a step of an existing phase and for a phase: the file bytes are unchanged and no backup is made.
  • Checks. npm ci, npm run typecheck, npm run build and npm run test:coverage pass on Node 24.

    Test files Tests Lines Branches
    main at 74e2f14 61 1655 97.17% 90.90%
    this branch 62 1669 97.18% 91.06%
  • Mutation runs. I changed upsert.ts only, kept the tests, and ran the three upsert test files (63 tests). Every failure is an assertion failure.

    Source change Failing tests
    upsert.ts as on main 11
    entire_plan patches an existing phase with the generic merge 11
    the step error of an existing phase is ignored 2
    no missing_id check for a phase 1
    a new phase is not appended 5
    a phase is looked up in the stored plan only, not in the phases added by the same patch 2
    applyPhasePatch uses the generic merge for steps 8
    applyPhasePatch drops the other phase fields 4

    Three new tests pass on main too: the two-way comparison for a patch without steps, the one for a step id held by another phase, and missing_id for a phase.

  • Comparison with main. I ran 20,000 random command sequences (163,757 commands) against a build of main and a build of this branch. The sequences mix create, every upsert target, upsert-with-template, update_status, next, show_status and query. After each command I compared the result and the plan file.

    • 17,354 sequences are identical.
    • 2,646 differ. In each, the first command that differs is an entire_plan patch that names a phase already in the plan and carries a steps array for it.
    • No sequence differs first at any other command.
  • The two paths against each other. For 20,000 random stored plans and phase patches I sent the patch both ways and compared the error, the stored phases and the write result.

    Build Equal Different
    main 12,194 7,806
    this branch 20,000 (12,045 accepted, 7,955 rejected) 0
  • Built CLI and MCP. The outputs in "Before and after" come from the built binary of each side.

Behaviour changes to note

Each row is a run of the built CLI. The patch is the phases item sent to upsert <file> entire_plan. In rows 1 to 3, p1 holds s1 (complete) and s2 (open). In row 4, p1 holds s1, and a step of p2 depends on s1.

Patch for p1 main This branch
{"id":"p1","steps":[]} exit 0; p1 has no steps exit 0; s1 (complete) and s2 stay
{"id":"p1","steps":[{"id":"s1","name":"S1 again"}]} exit 0; p1 holds only {"id":"s1","name":"S1 again"} exit 0; s1 is renamed and keeps prompt and complete; s2 stays
{"id":"p1","steps":[{"name":"no id","prompt":"x"}]} exit 0; stored in place of s1 and s2; one more backup exit 1, {"error":"missing_id"}; file unchanged; no backup
{"id":"p1","steps":[{"id":"s3","name":"S3","prompt":"x"}]} exit 1, {"error":"unknown_dependency"} exit 0; s3 is appended after s1
  • An entire_plan patch no longer removes steps from an existing phase. FR-PLAN-0015 defines no step removal through upsert.
  • A stored step that the patch sends again is merge-patched. Fields the patch does not send are kept, and so is the status.
  • A step without id in the patch of an existing phase is rejected with missing_id. The phase target already rejected it.
  • Some entire_plan patches that main rejected are now accepted: the last row above. main deleted the step that another step depends on, and validation then failed.
  • Two patches for the same new phase id in one call now merge their steps by id. Before, the second replaced the steps of the first.
  • A new phase in the patch is appended whole, as before.
  • Plans already written are not changed. Steps deleted by an earlier call are in that call's backup file.

Out of scope

Version bump: rosettify 3.2.9 → 3.2.10, in its own commit. The PR for the #390 fix bumps to the same number, so whichever merges second needs the next number. I will update it then. The two PRs merge cleanly in either order.

plan upsert <file> entire_plan merged phases by ID with the generic
merge patch, which replaces array values. A patch whose phase carried
a steps array replaced the stored steps of that phase: steps the patch
did not name were deleted, with their status. FR-PLAN-0015 requires
an existing phase to merge its steps by ID, and the phase target
already did that.

The phase-target logic moves into applyPhasePatch, and entire_plan
uses it for every phase that is already in the plan. A new phase is
appended whole, as before. A step without id in the patch of an
existing phase is now rejected with missing_id, the error the phase
target gives; before, entire_plan stored it in place of the old steps.

Signed-off-by: Serghei <43180231+xyrolle@users.noreply.github.com>
Patch bump for the change on this branch, made with scripts/bump_versions.sh.

Signed-off-by: Serghei <43180231+xyrolle@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

Rosetta Triage Review

Summary: This fixes the data loss in plan upsert entire_plan by merging existing phase steps by ID, preserving steps the patch omits and their statuses. It reuses the phase-target patch logic.

Type / Priority: Bug; Medium.

Findings:

  • The implementation matches the documented plan behavior, and the regression tests cover the reported data loss, merge parity with the phase-target path, missing IDs, and no write on rejection.
  • build-test, CodeQL, and language analysis checks pass; the triage check remains pending.

Caveats:

Recommendation: Recommend approval for the code change, with the version bump coordinated before the second merge.

Automated triage by Rosetta agent

@isolomatov-gd isolomatov-gd left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you @xyrolle. The merge by id matches FR-PLAN-0015 (3), and the tests cover kept, patched and appended steps. One change, please:

  • Add an acceptance criterion to FR-PLAN-0015 in docs/REQUIREMENTS/rosettify/PLAN.md: entire_plan on an existing phase keeps its stored steps and merges the patch steps by id. Add one line to docs/REQUIREMENTS/CHANGES.md.

… rosettify to 3.2.12

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: isolomatov-gd <isolomatov@griddynamics.com>
@isolomatov-gd
isolomatov-gd dismissed their stale review December 1, 2026 17:31

Addressed: we finished the requested changes on the branch (95b1779).

@isolomatov-gd

Copy link
Copy Markdown
Contributor

Thank you @xyrolle. We finished this PR: merged main, bumped rosettify to 3.2.12, and added the FR-PLAN-0015 clarification and acceptance criterion with a docs/REQUIREMENTS/CHANGES.md line.

@isolomatov-gd
isolomatov-gd merged commit ff27aea into griddynamics:main Dec 1, 2026
6 checks passed
isolomatov-gd added a commit to xyrolle/rosetta that referenced this pull request Dec 1, 2026
…_plan with griddynamics#455; bump rosettify to 3.2.13

Merges main (incl. griddynamics#455) into the PR branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: isolomatov-gd <isolomatov@griddynamics.com>
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.

[Bug]: rosettify plan upsert entire_plan replaces a phase's steps instead of merging them by id

2 participants