Skip to content

fix(targets): extend user-symlink preservation to codex and opencode writers - #1094

Merged
tmchow merged 1 commit into
EveryInc:mainfrom
anishekkamal:fix/codex-opencode-preserve-user-symlinks
Jul 10, 2026
Merged

tmchow merged 1 commit into
EveryInc:mainfrom
anishekkamal:fix/codex-opencode-preserve-user-symlinks

Conversation

@anishekkamal

Copy link
Copy Markdown
Contributor

Summary

The user-managed-symlink preservation that #1089 gave the Pi writer now covers the Codex and OpenCode writers and the shared cleanup plumbing — closing the "same pattern in two other writers" gap that PR's scope notes disclosed. Before this, install --to codex and install --to opencode still replaced a user's symlink override (or manually authored path) with a fresh upstream copy, and the standalone cleanup command could relocate a user's symlink node into legacy-backup on all eight of its targets, because every one of those paths funnels through one unguarded fs.rename in managed-artifacts.ts. Codex agent files were the worst case: written with no existence check of any kind.

Related: #1048.

Fix

Same four-row contract as #1089, applied per writer:

Existing path state Behavior
Doesn't exist Create fresh (unchanged)
Real path recorded in the target's install manifest Replace (unchanged)
Real path not in the manifest Preserve, warn, skip the write
Symlink to anything, including dangling Preserve, warn, skip the write
  • managed-artifacts.ts now owns the shared lstatOrNull / isPreservedSymlink helpers, and guards cleanupCurrentManagedDirectory, both removed-entry sweeps, and moveLegacyArtifactToBackup. That last one is the highest-leverage line in the PR: it protects the OpenCode and Kiro legacy sweeps and every target of the standalone cleanup command (codex/opencode/pi/kiro/copilot/droid/qwen/windsurf) in one place.
  • codex.ts gets the pi-shaped guards across its skill loops, removed-entry sweeps, legacy backup, and previous-store cleanup — plus a new cleanupCurrentManagedAgentFile, since agent .toml writes previously had no existence check at all, and a guard on the orphan-sidecar fs.rm in the agent write loop (the one destructive write-path op the first pass missed — its twin in cleanupRemovedAgents was already guarded). Preserved names are excluded from the written manifest. Its local moveLegacyArtifactToBackup stays separate from the shared one (backup-path shapes differ materially) and gets the same guard.
  • opencode.ts applies the full contract to skills, agents, commands, and plugins — all four have ownership records in the manifest's groups map. For commands the guard runs before backupFile, which otherwise copies through the symlink and then overwrites through it, corrupting the fork while producing a "backup" of the fork's own content.
  • pi.ts drops its private helper copies and imports the shared ones. No behavior change; its 20 preservation tests pass unmodified.

One design decision reviewers should weigh

moveLegacyArtifactToBackup accepts an explicit skipSymlinkGuard: true escape hatch, used by exactly one caller: moveIfSymlinkManaged in the cleanup command's ~/.agents/skills/ sweep. That sweep's entire job is relocating symlinks — but only after isManagedCodexAgentsSymlink verifies the link resolves inside a CE-managed root, a strictly stronger ownership signal than the blanket guard. Without the hatch, the new guard broke that legitimate relocation (caught by the existing cleanup only backs up CE-owned symlinks test mid-implementation). The invariant is documented at the parameter definition; a repo-wide grep confirms the single call site. The alternative — having that caller do its own rename — was considered and rejected as duplicating the backup-path logic for no added safety.

Verification

  • 16 new tests (7 codex, 8 opencode, 1 shared-path dangling-symlink case) mirroring the fix(pi): preserve user-managed symlinks and unmanaged paths on install #1089 test patterns: symlinked sidecar surviving an agent that drops its sidecars, fork content untouched, manifest exclusion, managed paths still replaced, preserved symlinks surviving a second run against a stale manifest, legacy sweep sparing a symlink whose content matches the ownership fingerprint, and commands producing no .bak churn when preserved.
  • Red-green: with the src/ changes stashed, 9 of the initially added 11 tests fail (the two "still replaces" positive rows pass either way); restored, all pass.
  • Codex-agent freeze risk (the question raised on fix(pi): preserve user-managed symlinks and unmanaged paths on install #1089 for Pi) checked against git history: the .toml agent write and its manifest tracking landed in the same commit (c2d60b47, refactor(install): prefer native plugin install across targets #609) — before it, Codex emitted agents as skill dirs — so no released version wrote a codex agent file without recording it, and the preserve branch can only fire on genuinely user-authored files.
  • Full bun test failure count identical to unmodified main on the same machine (pre-existing, environment-related set; verified by name-level diff, not count alone).

Scope notes

  • kiro.ts's legacy sweep inherits the symlink guard through the shared moveLegacyArtifactToBackup — intentional and contract-consistent, though the Kiro writer itself is currently unreachable from the CLI (not registered in targets/index.ts).
  • Codex prompt files (~/.codex/prompts/<name>.md) remain unguarded bare writes — pre-existing, and the Pi reference implementation has the same surface, so aligning both is left as a follow-up rather than widening this PR's contract.
  • antigravity.ts has no manifest/ownership concept at all (every write unconditional); giving it preservation needs a design decision about what "owned" means there, not a mirror of this pattern.
  • Ancestor-symlink traversal is a known remaining class, deliberately out of scope. All guards here (and in the merged Pi fix) inspect the leaf node; lstat follows every ancestor. A user who symlinks a whole store directory (e.g. ~/.codex/skills/<plugin> → a fork) still has fs.rm/copy operate through the parent link into the fork. This predates both PRs, affects every writer including Pi, and the right fix is a containment check (realpath-inside-managed-root before any destructive op) applied uniformly — a design change, not a mirror of this leaf contract. Adversarial review surfaced it during this PR's pre-flight; flagging it here so it's tracked rather than discovered.
  • Same pre-existing family, lower stakes: a store root that is a dangling symlink makes the writer's initial ensureDir throw and abort the install (crash, no data loss).

Compound Engineering
Claude Code

…writers

The lstat-based preservation contract from the Pi writer fix now covers
the Codex and OpenCode writers and the shared cleanup plumbing. The
shared moveLegacyArtifactToBackup guard alone protects the OpenCode and
Kiro legacy sweeps plus all eight targets of the standalone cleanup
command. Codex agent .toml writes, previously fully unconditional, gain
manifest-gated guards, and the orphan-sidecar rm in the agent write loop
is guarded to match its twin in cleanupRemovedAgents; OpenCode guards
run before backupFile so nothing is copied or written through a user
symlink. Pi imports the now-shared helpers instead of private copies.

The cleanup command's ~/.agents/skills sweep legitimately relocates
CE-owned symlinks after verifying their resolved target sits inside a
managed root, so it opts out of the blanket guard via an explicit,
documented skipSymlinkGuard flag with exactly one call site.

Co-Authored-By: Claude Code <[email protected]>
@tmchow

tmchow commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

nice work @anishekkamal

@tmchow
tmchow merged commit 406ead1 into EveryInc:main Jul 10, 2026
2 checks passed
@github-actions github-actions Bot mentioned this pull request Jul 10, 2026
@anishekkamal
anishekkamal deleted the fix/codex-opencode-preserve-user-symlinks branch July 11, 2026 03: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.

2 participants