Repository navigation
fix(studio-server): history writes and claims follow Studio's write rule - #7988
Conversation
History undo, restore and Cmd+Z now leave alone every file that Studio's file routes refuse to write. This covers tool-trusted, private, hidden and node_modules paths, and scripts the project's package.json runs. Cmd+Z passes over an edit that only changed such files. A claim no longer takes such a file's pending change. A claim's `overwrote` version now counts only when the server itself saw that version at that path. A version stored for another file can no longer become a file's earlier state. The write rule also refuses more files that tools and agents load on their own: opencode and crush settings, compose override files, Dockerfile.* and Containerfile, every lefthook spelling, mise, nodemon, devbox, wrangler, vercel and netlify settings, *.config.jsonc, tsconfig.*.json, Taskfile.dist, simple-git-hooks, Ruby build files, and .nix and .fish scripts.
|
Preview deployment for your docs. Learn more about Mintlify Previews.
💡 Tip: Enable Automations to automatically generate PRs for you. |
Edit accuracy: accurate 267 (base branch 267), smooth 93 of thoseThe gate passes. Quarantined, measured but not gated (8)
Unstable (2)
|
eddy-heygen
left a comment
There was a problem hiding this comment.
(Eddy's agent) Reviewed by Eddy's Claude agent at commit 5aa35a7.
Verdict: approved, good to ship.
Summary. Studio's history routes (undo, restore, the undo-shortcut step and claims) wrote files with containment checks only, so they were looser than the file routes: they could roll back, delete or replace files the file routes refuse to write. This PR makes the single write sink behind undo, restore and step, the claim path, and the next-step lookup apply the same rule the file routes use. A claim's "overwrote" version is accepted only if the server itself recorded that version at that path. The shared tool-trusted name list is widened, and Studio also refuses script files that the project's root package manifest names. The rule is checked at the time of use, so history entries already on disk are covered. 7 files; a reviewer agent read the full diff and I read its findings against the changed files.
Blockers
None.
Non-blockers
packages/studio-server/src/history/projectHistory.ts: the undo conflict check still counts files that undo will no longer write, so an undo can report a conflict on a file it would have skipped anyway.packages/core/src/safePath.ts: on Windows, the short (8.3) forms of the newly added file-name spellings are not recognised by the name list.packages/studio-server/src/helpers/safePath.ts: the detection of script files named by the package manifest reads only the root manifest's main, bin and scripts entries. It misses other ways a manifest can name code, and it can also refuse an ordinary composition file that happens to be named there. Worth tightening in a follow-up; neither case is worse than before this PR.- Tests: the four new route tests cover the main cases; a test for an entry that was already on disk before the change would pin the time-of-use behaviour.
CI: all checks pass at this commit, including the Windows test jobs; none failing or pending.
How this was reviewed: a code-review pass and a security-review pass by a reviewer agent in an isolated checkout of this commit; I then read its findings against the diff. Nothing was built or run.
What
overwroteversion counts only when the server itself saw that version at that path.package.jsonruns.Why
A composition's script in an ordinary preview runs on the same origin as the Studio API. #7964 stopped the file routes from writing
package.json, agent instructions and scripts, but the history routes had a second way in:package.json, for example the developer's editor, agent or package manager. The change stays pending for a moment.overwrote. History had heard no overwrite of its own, so it took the client's word and made that hash the entry's earlier state.package.json, because history writes ran only containment checks.Undo, restore and step could also roll back or delete such files with no setup at all.
Separately, the name rule was a fixed list. It missed agent settings such as
opencode.json, override files such asdocker-compose.override.ymlandlefthook-local.yml, and the filesnodemon,miseand other tools load. It also missed a project-root script such asbuild.mjsthatpackage.jsonruns.How
packages/studio-server/src/history/projectHistory.tsstudioWouldWrite(path)applies Studio's write rule (isWritableInProject) to a project-relative path.writeAs, which backs undo, restore and step, drops target paths the rule refuses. Those files keep their current bytes.next()skips a pending group or entry with no file the rule allows. So Cmd+Z never gets stuck on a change it cannot undo.takeClaimedignores refused paths.SeenVersionskeeps, per path, the latest 32 hashes the server found there (every tracked hash) or that a Studio write replaced there. A claim'soverwrotehash is used only if it is in that set. Otherwise the claim takes the whole pending change, and its earlier state is what history really saw. A legitimate cut is always a version the server saw at that path, because a cut also needs the stored copy that only a sweep or a heard write makes. So no real claim loses anything.packages/studio-server/src/helpers/safePath.tsisWritableInProject(projectDir, relPath)isisWritableProjectPathplus one more refusal: a JS or TS file that the project'spackage.jsonnames inmain,binorscripts(node ./build.mjs,tsx scripts/gen.ts --out=dist). The parsed list is cached untilpackage.jsonchanges.isWritableProjectFile, which the file routes use, now goes through it.script.jsbesideindex.html, and Studio has to keep editing it.packages/core/src/safePath.tsadds these names totoolTrustedFileKind:opencode.json,opencode.jsonc,crush.jsondocker-compose.override.*,compose.override.*docker composeloads them next to the main file.Dockerfile.*,*.dockerfile,Containerfilelefthook-local.*,lefthook.{yml,yaml,json,jsonc,toml}Taskfile.dist.yml/.yamlTaskfile.ymlis missing.mise.toml,mise.*.toml,mise/config.tomlnodemon.json,devbox.json,devenv.yamlwrangler.{toml,json,jsonc},vercel.json,netlify.tomlcomposer.jsonGemfile,Rakefile,Vagrantfile,Tiltfile,Earthfile,Brewfile*.config.jsonc,tsconfig.*.json,jsconfig.*.jsontsconfig.jsonextends the named variants.simple-git-hooks.{json,js,cjs,mjs}.nix,.fishnix-shellor the fish shell run.Hidden names such as
.envrc,.mise.toml,.mcp.jsonand.vscode/were already refused by the hidden-path rule.The CLI history docs now say what
undoandrestoreleave alone.Test plan
routes/history.protected.test.ts, which drives the HTTP routes:index.htmlback and leaveAGENTS.md, apackage.jsonscript and a newopencode.jsonbyte-identical.package.jsonalone when an outside edit changed only that file.other.htmlasindex.html's overwritten version cannot make undo write them.package.jsontakes nothing.mainand pass here.helpers/safePath.test.ts:package.jsonscripts are refused in any spelling the rule folds. Composition scripts, folders and unnamed files stay writable. A changedpackage.jsonis re-read, and a missing or broken one changes nothing.core/src/safePath.test.ts: every new name and spelling, plus look-alikes such ascompositions/docker.html,assets/mise-en-scene.pngandassets/script.jsthat must stay writable.vitest runon studio-serverhelpers/safePath,routes/history*,history/*androutes/files*(687 passed, 3 skipped), on coresafePath(177 passed) and on the CLIregistry/installer(69 passed). Also rantsc --noEmitfor core and studio-server,scripts/lint-registry-items.mjs(448 items, 0 errors) and the comment ratchet.