Skip to content

chore(ratchet): layout-writer ratchets for main and renderer - #27431

Merged
Jinwoo-H merged 4 commits into
mainfrom
layout-p8-writer-ratchets
Oct 11, 2026
Merged

Jinwoo-H merged 4 commits into
mainfrom
layout-p8-writer-ratchets

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​74 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​74
Prod 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​708 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​707

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.mjs runs two per-file counts:

  • Main process (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, and workspaceSession object keys.
  • Renderer (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 to set/setState (a shorthand elsewhere only passes a value on), and string keys. A variable key such as exitCodes[ptyId] names no field and is not counted. String keys catch computed-key writes such as patch[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 existing openFiles or 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's web-session-tabs-sync/store-patch.ts, and the future runtime/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: a check:layout-writer-ratchets script, a step in pnpm lint, and an "Enforce layout-writer ratchets" step in the PR workflow. --prune lowers 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-ratchets passes 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) in open-file-mutations.ts. A planted openFiles: [...s.openFiles, file] in the same file fails with 6 renderer layout write(s), baseline allows 5.

  • Other planted writes fail it too. Adding set({ tabsByWorktree: {} }) to tabs-create-actions.ts and state.workspaceSessionsByHostId = {} to repo-lifecycle-operations.ts gives:

    • ::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. …
    • New files fail the same way: store.setWorkspaceSession(s) in ipc/runtime.ts, and a computed patch[key] write keyed by 'activeTabIdByWorktree' in App.tsx.
  • config/scripts/check-layout-writer-ratchets.test.mjs covers what is counted and what is not. pr-preflight-gates.test.mjs is updated for the new workflow step.

  • pnpm tc, oxlint and pnpm run check:code-quality:changed pass.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

The script runs on any OS with Node. It has no effect at runtime, so SSH, mobile, compatibility and performance are unaffected.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 93a33cdf-1ffd-48b3-8e7d-86e12a706fa3

📥 Commits

Reviewing files that changed from the base of the PR and between a541b6a1ce1ca0530c392b913580e799cc89aaf0 and 3c303f5.


📒 Files selected for processing (1)
  • config/renderer-layout-writer-baseline.txt

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.



📝 Walkthrough

Walkthrough

The change adds a script that counts layout writes in main-process and renderer TypeScript files and checks the counts against separate per-file baselines. It adds baseline initialization and pruning options, tests for counting and path filtering, and runs the check from lint and from preflight when static analysis is selected.


Priority: ⬇️ Low

Merge Risk: 🔵 Low · up to 3c303

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 | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 53.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 15 functions across 3 files. (1 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly and concisely identifies the addition of layout-writer ratchets for both main and renderer code.
Description check Passed The description follows the required template and explains the purpose, implementation, testing, scope, and compatibility impact. It identifies the change as lint-only and documents that the remaining…
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.

Full details: Docstring Coverage

Explanation

Docstring coverage is 53.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 15 functions across 3 files. (1 skipped: 1 unsupported.)


  • Fix all pre-merge checks with AI
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@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


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 43791703-3d85-49c4-b2e5-c24b9f874f4d
📥 Commits

Reviewing files that changed from the base of the PR and between e3d747b and d8d1cab.

📒 Files selected for processing (7)
  • .github/workflows/pr.yml
  • config/main-layout-writer-baseline.txt
  • config/renderer-layout-writer-baseline.txt
  • config/scripts/check-layout-writer-ratchets.mjs
  • config/scripts/check-layout-writer-ratchets.test.mjs
  • config/scripts/pr-preflight-gates.test.mjs
  • package.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.

Comment on lines +304 to +317
} 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

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

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.

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

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟡 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 win

Fail closed on non-deletion read errors.

When a new tracked TypeScript file cannot be read, collectCurrentCounts skips it. diffCounts then 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.txt
  • config/renderer-layout-writer-baseline.txt
  • config/scripts/check-layout-writer-ratchets.mjs
  • config/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.
@Jinwoo-H
Jinwoo-H force-pushed the layout-p8-writer-ratchets branch from a541b6a to 3c303f5 Compare October 11, 2026 18:39
@Jinwoo-H
Jinwoo-H merged commit e969881 into main Oct 11, 2026
36 checks passed
Jinwoo-H added a commit that referenced this pull request Oct 11, 2026
#27431 and #27529 landed together; #27529 removed a writer from each spawn-commit file, so the ratchet now fails on main.
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.

1 participant