Repository navigation
chore(ratchet): layout-writer ratchets for main and renderer - #27431
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Priority: ⬇️ Low Merge Risk: 🔵 Low · up to In-place layout mutations can bypass the new safeguard. Current product behavior is unaffected, but the check is incomplete; merge with that limitation understood. Pre-merge checks |
|
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
43791703-3d85-49c4-b2e5-c24b9f874f4d
📒 Files selected for processing (7)
.github/workflows/pr.ymlconfig/main-layout-writer-baseline.txtconfig/renderer-layout-writer-baseline.txtconfig/scripts/check-layout-writer-ratchets.mjsconfig/scripts/check-layout-writer-ratchets.test.mjsconfig/scripts/pr-preflight-gates.test.mjspackage.json
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.
| } else if ( | ||
| ts.isBinaryExpression(node) && | ||
| ASSIGNMENT_OPERATORS.has(node.operatorToken.kind) && | ||
| targetedField(node.left, ratchet.assignedFields) !== null | ||
| ) { | ||
| const field = targetedField(node.left, ratchet.assignedFields) | ||
| if (!(ratchet.entryContentFields.has(field) && keepsMembership(node.right))) { | ||
| count += 1 | ||
| } | ||
| } else if ( | ||
| ts.isDeleteExpression(node) && | ||
| targetedField(node.expression, ratchet.assignedFields) !== null | ||
| ) { | ||
| count += 1 |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Count in-place array mutations on tracked fields.
countLayoutWrites counts only assignments and delete operations whose target goes through a tracked field. It does not count calls that change an array in place:
session.tabsByWorktree[id].push(tab)partition.unifiedTabs.splice(i, 1).sort()
These calls change tab membership or order, but the count does not go up. As a result, a new main-process writer that uses one of these calls passes the ratchet. To close this gap, count a call when the method name is in a set of mutating array methods and the receiver goes through a tracked field.
Proposed addition
+const MUTATING_METHODS = new Set(['push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse', 'fill', 'copyWithin'])
...
} else if (
ts.isDeleteExpression(node) &&
targetedField(node.expression, ratchet.assignedFields) !== null
) {
count += 1
+ } else if (
+ ts.isCallExpression(node) &&
+ ts.isPropertyAccessExpression(node.expression) &&
+ MUTATING_METHODS.has(node.expression.name.text) &&
+ targetedField(node.expression.expression, ratchet.assignedFields) !== null
+ ) {
+ count += 1
} else if (ts.isShorthandPropertyAssignment(node) && ratchet.keyedFields.has(node.name.text)) {If you adopt this change, re-run --init, because the baselines will change.
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to GitHub limitations.
🟡 Minor · Fail closed on non-deletion read errors. · check-layout-writer-ratchets.mjs:401-430
config/scripts/check-layout-writer-ratchets.mjs:401-430
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winFail closed on non-deletion read errors.
When a new tracked TypeScript file cannot be read,
collectCurrentCountsskips it.diffCountsthen sees neither a current nor baseline entry, so the ratchet can report success without checking the file. Ignore only a missing file. Propagate other read errors.Suggested fix
- } catch { - // Deleted in the working tree but still tracked. - continue + } catch (error) { + if (error?.code !== 'ENOENT') { + throw error + } + // Deleted in the working tree but still tracked. + continue
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
df7c3963-3c83-4587-a1dd-8f04ad89a5f8
📥 Commits
Reviewing files that changed from the base of the PR and between d8d1cab and a541b6a1ce1ca0530c392b913580e799cc89aaf0.
📒 Files selected for processing (4)
config/main-layout-writer-baseline.txtconfig/renderer-layout-writer-baseline.txtconfig/scripts/check-layout-writer-ratchets.mjsconfig/scripts/check-layout-writer-ratchets.test.mjs
🚧 Files skipped from review as they are similar to previous changes (1)
- config/main-layout-writer-baseline.txt
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Two per-file ratchets that may only go down: main-process callers of the Store's layout setters plus direct edits of saved session partitions, and renderer writes of the window's layout and per-view selection fields (including computed-key and spread writes, counted where the key or object is built). The frozen-mirror writers are exempt. Wired next to the focus-read ratchet in lint and the PR workflow.
…tries A .map over the existing openFiles or browser tab list keeps its entries and order, so it is a content update (draft, git status, URL) and is no longer counted by the renderer layout-writer ratchet.
A variable key such as exitCodes[ptyId] names no field, so only string keys count. A shorthand key counts only in an object handed to set, setState or a Store writer; elsewhere it passes a value on.
a541b6a to
3c303f5
Compare
ELI5
Workspace tab layout is about to get a single owner in the runtime. Before that happens, this adds two counters that fail lint and CI if anyone adds a new place that writes layout directly. Today's writers are written down with their counts, and those counts may only go down.
What Changed
No user-visible change. This adds a check and two baseline files. It changes nothing in the app.
config/scripts/check-layout-writer-ratchets.mjsruns two per-file counts:config/main-layout-writer-baseline.txt, 214 today in 69 files). It counts calls of the Store's layout setters (setWorkspaceSession,patchWorkspaceSession,persistPtyBinding, …) and of the Store-internal helpers that edit saved session partitions (catalog removal, repo removal, orcad import, SSH binding scrubs, …). It also counts direct edits of a partition: assignments and deletes on session, tab and pane-layout fields, andworkspaceSessionobject keys.config/renderer-layout-writer-baseline.txt, 701 today in 121 files). It counts writes of the window's layout fields (tabsByWorktree,terminalLayoutsByTabId,tabBarOrderByWorktree,unifiedTabsByWorktree,groupsByWorktree,layoutByWorktree,openFiles,browserTabsByWorktree) and its per-view selection fields (active*ByWorktree,active*,ptyIdsByTabId). Counted forms are assignments, deletes, object keys, shorthand keys inside an object handed toset/setState(a shorthand elsewhere only passes a value on), and string keys. A variable key such asexitCodes[ptyId]names no field and is not counted. String keys catch computed-key writes such aspatch[key] = …. A spread is counted where the spread object is built. Content updates of an existing editor or browser entry are not counted. These are a.map(…)over the existingopenFilesor browser tab list, or{ ...map, [key]: list.map(…) }, and they keep the same entries in the same order (drafts, git status, URL). Adding, removing or reordering entries is still counted, so editor and browser PRs that only touch content are not blocked. The three writers the layout plan allows are exempt:workspace-layout-cache.ts(the future single writer), the frozen mirror'sweb-session-tabs-sync/store-patch.ts, and the futureruntime/remote-workspace-frozen/folder.Known gap: a shorthand key in a patch builder that returns its object to a caller is not counted. Counting uses the TypeScript parser, so types, destructuring reads and
{ x: s.x }selector copies are not counted. Test, fixture and harness files are skipped. The check is wired exactly like the focus-read ratchet: acheck:layout-writer-ratchetsscript, a step inpnpm lint, and an "Enforce layout-writer ratchets" step in the PR workflow.--prunelowers the baselines after writers are removed. Deleted functions are not tracked here, because the compiler already catches any remaining caller.Why
The layout rework moves every tab-layout write to one owner, and it lands as a stack over several PRs. Without a gate, new direct writes can appear on main while the stack is in review, and each one is another writer the stack has to find and reroute. A count that may only go down blocks new writers without requiring today's ones to be fixed first. The rework then lowers the counts as it deletes code.
Alternatives considered: a hand-maintained deny list of call sites, which misses new files and computed keys, and a unit test that scans source, which the repo retired in favour of lint-style scripts. The per-file baseline format and helpers are reused from the focus-read ratchet rather than reimplemented.
Linked Issue
N/A (maintainer; groundwork for the workspace layout single-owner rework)
Visual Proof
N/A: lint-only change, nothing in the app changes.
Testing
pnpm check:layout-writer-ratchetspasses on this branch. It runs in about 2.5 s.A planted content-only write passes:
openFiles: s.openFiles.map(f => f.id === id ? { ...f, isDirty: true } : f)inopen-file-mutations.ts. A plantedopenFiles: [...s.openFiles, file]in the same file fails with6 renderer layout write(s), baseline allows 5.Other planted writes fail it too. Adding
set({ tabsByWorktree: {} })totabs-create-actions.tsandstate.workspaceSessionsByHostId = {}torepo-lifecycle-operations.tsgives:::error file=src/main/persistence/loading-store/repo-lifecycle-operations.ts::12 main-process layout writer(s), baseline allows 11. …::error file=src/renderer/src/store/slices/tabs/tabs-create-actions.ts::15 renderer layout write(s), baseline allows 14. …store.setWorkspaceSession(s)inipc/runtime.ts, and a computedpatch[key]write keyed by'activeTabIdByWorktree'inApp.tsx.config/scripts/check-layout-writer-ratchets.test.mjscovers what is counted and what is not.pr-preflight-gates.test.mjsis updated for the new workflow step.pnpm tc, oxlint andpnpm run check:code-quality:changedpass.I manually tested these changes locally
Automated tests added/updated, or explained why not below
Review
Agent skill upstream boundary
Notes
The script runs on any OS with Node. It has no effect at runtime, so SSH, mobile, compatibility and performance are unaffected.
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)