Skip to content

fix(update): don't hijack the agents target on legacy Codex upgrade - #1522

Open
clay-good wants to merge 2 commits into
mainfrom
fix/legacy-upgrade-agents-ownership
Open

fix(update): don't hijack the agents target on legacy Codex upgrade#1522
clay-good wants to merge 2 commits into
mainfrom
fix/legacy-upgrade-agents-ownership

Conversation

@clay-good

@clay-good clay-good commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Status

LGTM — surgical fix for an init/update interaction found by an adversarial swarm on post-v1.8.0 main. Not a release blocker for the published v1.8.0 artifact (latent, opt-in path); good fast-follow.

What was wrong

Codex (#1511) and the vendor-neutral agents target (#1303) both write to .agents/skills. The main generation path reconciles which tool owns a shared root before writing, but upgradeLegacyTools in update.ts did not.

So for a project that owns the agents target (.agents/skills/.openspec-target == "agents", generic /openspec- skills) whose user also has leftover global ~/.codex/prompts/* from an old Codex install: on openspec update --force (or any non-interactive run), Codex is auto-selected as an unconfigured legacy tool, and the loop wrote Codex $openspec- skills into .agents/skills and called writeSharedSkillTarget(projectPath, 'codex') — silently rewriting the generic tree with Codex-specific syntax and flipping the ownership marker agents → codex. Future updates then suppress the agents target entirely.

How it was fixed

New sharedSkillRootOwnedByOther(projectPath, toolId) in shared-skill-target.ts returns true only when the shared root already carries an ownership signal (a marker or an existing generated tree) and reconciliation resolves it to a different tool. upgradeLegacyTools consults it and skips skill generation for that tool, leaving the established owner in place — mirroring the one-writer rule init applies when both targets are selected.

Crucially, an empty/unclaimed root returns false, so a genuine first-time Codex upgrade (no .agents yet) still creates its skills — no over-correction.

Replication / proof (live CLI on this branch)

Bug scenario — agents target + global ~/.codex/prompts, then update --force:

ℹ Skipped Codex: .agents/skills is already managed by another tool.
marker: agents        # unchanged
generic-syntax — PRESERVED

Counter-check — Codex-only user, no .agents yet, then update --force:

✔ Setup complete for Codex
marker: codex
codex-syntax present — codex configured (not over-skipped)

New test/core/shared-skill-target.test.ts covers the predicate (marker-owned, inferred-owned, codex-owned, empty root, non-shared tool). 285 tests pass across update/init/migration/detection suites.

Notes

Companion PR (scenario-loss parity) fixes the other, unrelated defect from the same swarm.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Prevented legacy tool upgrades from overwriting shared skills already managed by another tool.
    • Preserved existing vendor-neutral .agents skill trees and ownership markers during Codex upgrades.
    • First-time Codex upgrades without an existing .agents tree continue to work as expected.
    • Skipped conflicting upgrades with an informational message.

Codex and the vendor-neutral `agents` target share `.agents/skills`. In
upgradeLegacyTools, a Codex install inferred only from global ~/.codex/prompts
wrote Codex skills into `.agents` and flipped the ownership marker
agents -> codex, silently rewriting an existing agents-owned tree. The main
generation path reconciles shared-target ownership first; this legacy-upgrade
path did not. Add sharedSkillRootOwnedByOther() and skip generation when a
different tool already owns the shared root (marker or existing tree), while
still allowing a genuine first-time Codex upgrade with no `.agents` yet.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@clay-good
clay-good requested a review from a team as a code owner August 5, 2026 21:49
@clay-good
clay-good requested review from TabishB and removed request for a team August 5, 2026 21:49
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds shared skill-root ownership detection. Legacy tool upgrades now skip generation when another tool owns the shared .agents/skills root. Tests cover ownership signals, Codex upgrades, and non-sharing tools.

Changes

Shared skill ownership protection

Layer / File(s) Summary
Shared-root ownership detection
src/core/shared-skill-target.ts, test/core/shared-skill-target.test.ts
Adds sharedSkillRootOwner and sharedSkillRootOwnedByOther. Tests cover marker-based ownership, inferred ownership, Codex handling, first-time upgrades, ambiguous trees, and non-sharing tools.
Legacy upgrade guard
src/core/update.ts, test/core/update.test.ts, .changeset/fix-legacy-upgrade-agents-ownership.md
Legacy upgrades skip skill generation and new-tool registration when another tool owns the shared root. Regression coverage confirms that Codex detection preserves an existing agents owner.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant LegacyUpgrade
  participant SharedSkillRootOwner
  participant SkillGenerator
  LegacyUpgrade->>SharedSkillRootOwner: Check shared-root ownership
  SharedSkillRootOwner-->>LegacyUpgrade: Return owner or undefined
  LegacyUpgrade->>SkillGenerator: Generate skills when no other owner exists
  LegacyUpgrade-->>LegacyUpgrade: Skip generation when another tool owns root
Loading

Possibly related PRs

Suggested reviewers: tabishb, alfred-openspec

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preventing legacy Codex upgrades from taking ownership of the agents target.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/legacy-upgrade-agents-ownership

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.

@alfred-openspec alfred-openspec left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The ownership predicate looks right and the focused suite passes, but the regression only tests the helper. Please add an UpdateCommand test for the actual legacy-upgrade path: existing agents-owned .agents/skills plus global legacy Codex prompts, then non-interactive update --force, asserting the generic files and agents marker remain unchanged. Add the inverse no-root case proving first-time Codex generation still writes the codex marker. This control-flow boundary is the bug.

Harden the agents-target ownership fix after a multi-agent review:
- Add an integration test that runs the real update flow for the bug
  scenario (agents-owned .agents + a legacy global Codex prompt) and asserts
  the marker stays `agents` and skills keep generic `/openspec-` syntax. A
  unit test of the predicate can't catch a future refactor that stops calling
  it; this can.
- Name the owning tool in the skip message ("...managed by another tool
  (Shared .agents skills)") via a new sharedSkillRootOwner() helper that
  sharedSkillRootOwnedByOther now delegates to.
- Add a unit case for the ambiguous-tree branch (existing skills, no marker,
  no inferable syntax) and one asserting sharedSkillRootOwner names agents.
- Document the known, harmless re-offer tradeoff (a skipped tool isn't
  recorded as configured, so a persistent legacy prompt re-offers it).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
test/core/update.test.ts (1)

616-639: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Assert that the skipped Codex tool is not persisted.

The test proves that the .agents/skills tree is not rewritten. It does not prove that codex is absent from the persisted configured-tool set. Add an assertion against the persisted configuration or the existing getConfiguredToolsForProfileSync(testDir) helper. Otherwise, a regression can preserve the marker and syntax while recording Codex for later updates. The PR objective requires both generation and configuration to be skipped.

🤖 Prompt for 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.

In `@test/core/update.test.ts` around lines 616 - 639, Extend the test around
UpdateCommand.execute so it also verifies Codex is absent from the persisted
configured-tool set, using getConfiguredToolsForProfileSync(testDir) or the
existing persisted configuration access. Keep the current marker and syntax
assertions, and assert the configured tools do not include codex.
🤖 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/core/shared-skill-target.test.ts`:
- Around line 78-79: Extend the test for the ambiguous shared-skill tree to
assert the inferred owner directly by checking that
sharedSkillRootOwner(projectPath, 'codex') returns 'agents', while retaining the
existing sharedSkillRootOwnedByOther boolean assertion.

---

Nitpick comments:
In `@test/core/update.test.ts`:
- Around line 616-639: Extend the test around UpdateCommand.execute so it also
verifies Codex is absent from the persisted configured-tool set, using
getConfiguredToolsForProfileSync(testDir) or the existing persisted
configuration access. Keep the current marker and syntax assertions, and assert
the configured tools do not include codex.
🪄 Autofix

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: 73b189f0-86f4-46d0-ab3f-4ca601d57059

📥 Commits

Reviewing files that changed from the base of the PR and between 133f585 and 2bf0eaf.

📒 Files selected for processing (4)
  • src/core/shared-skill-target.ts
  • src/core/update.ts
  • test/core/shared-skill-target.test.ts
  • test/core/update.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/core/update.ts

Comment on lines +78 to +79
expect(sharedSkillRootOwnedByOther(projectPath, 'codex')).toBe(true);
});

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Assert the inferred owner, not only the boolean.

Line [78] verifies only that a non-codex owner exists. It does not verify that the ambiguous tree resolves to agents, although the test name and comment claim that result. Add an exact sharedSkillRootOwner(projectPath, 'codex') assertion.

Proposed assertion
+    expect(sharedSkillRootOwner(projectPath, 'codex')).toBe('agents');
     expect(sharedSkillRootOwnedByOther(projectPath, 'codex')).toBe(true);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
expect(sharedSkillRootOwnedByOther(projectPath, 'codex')).toBe(true);
});
expect(sharedSkillRootOwner(projectPath, 'codex')).toBe('agents');
expect(sharedSkillRootOwnedByOther(projectPath, 'codex')).toBe(true);
});
🤖 Prompt for 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.

In `@test/core/shared-skill-target.test.ts` around lines 78 - 79, Extend the test
for the ambiguous shared-skill tree to assert the inferred owner directly by
checking that sharedSkillRootOwner(projectPath, 'codex') returns 'agents', while
retaining the existing sharedSkillRootOwnedByOther boolean assertion.

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