Repository navigation
fix(kimi): bound orphaned managed-block recovery to installer-shaped tables - #19033
cat-xierluo wants to merge 6 commits into
Conversation
…tables The managed-hook marker regex recovered an orphaned block (BLOCK_START with its BLOCK_END hand-deleted) by matching to end-of-file. Anything the user had appended after installation — their own [[hooks]] tables included — was silently deleted by remove or swallowed by the reinstall rewrite (stablyai#18861). Recovery is now bounded to the contiguous installer-shaped tables the generator emits ([[hooks]] header + event/command/timeout lines); the match stops at the first table that does not mirror that shape, so user TOML after an orphaned block survives both paths. The complete-block branch is unchanged, and orphan convergence (single block after reinstall) still holds. A user table that happens to mirror the exact installer shape remains indistinguishable by form — that is the accepted bound, matching the fix direction in the issue. Note: vibe/hook-config-toml.ts from stablyai#18813 is not on main yet; the same two-file lockstep change should be applied there when that PR lands.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughKimi TOML orphan recovery now separates table separators from boundary detection. It allows blank and comment lines before the next table header or end of file. It stops when a key/value line extends a table. Tests cover LF and CRLF input, blank-line separation, comments, extended user tables, removal and reinstall behavior, and cases without a complete managed-table match. Merge Risk: 🟡 Moderate · up to Kimi configurations without a final newline may retain an orphaned hook during removal and gain a duplicate on reinstall. This EOF case should be fixed before merge. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Linked Issues checkExplanation The Kimi implementation satisfies the bounded orphan-recovery and regression-testing requirements. However, issue 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Team
Run ID: 10217d0e-fdb5-4036-8941-82869cf340ce
📒 Files selected for processing (2)
src/main/kimi/kimi-hook-config-toml.test.tssrc/main/kimi/kimi-hook-config-toml.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Bounded orphan recovery —
MANAGED_BLOCK_RE's orphan branch now matches contiguous installer-shaped[[hooks]]tables (event/command/timeout) instead of running to end-of-file, so user TOML appended after a hand-deletedBLOCK_ENDsurvives remove/reinstall. - Regression test — new test asserts a user
[[hooks]]+[[tools]]tail is preserved by bothremoveManagedKimiHooksandapplyManagedKimiHooks, and that reinstall still converges to a single block. - Complete-block path — the
BLOCK_START … BLOCK_ENDbranch is unchanged.
ℹ️ Vibe twin must not drift
The bounded-recovery change needs to land on the vibe/hook-config-toml.ts twin when #18813 ships it on main; otherwise the vibe path re-introduces the EOF-swallow bug this fixes. You've already flagged this and offered to follow up, so nothing to do here beyond keeping that lockstep.
I verified the new test is meaningful: with the old EOF-only regex restored, removed.text collapses to default_model = "x"\n (the user tail is deleted), and the full kimi-hook-config-toml.test.ts suite is green (9 tests).
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
CodeRabbit review follow-up: the orphan branch matched LF-only line boundaries, so a config.toml resaved with Windows endings after the BLOCK_END hand-edit was no longer recoverable — remove left the orphaned tables behind and reinstall appended a duplicate block. Table boundaries now accept \r?\n throughout, with a CRLF regression test (recovery + user tail preservation + orphan recognition).
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Team
Run ID: 33b0f650-e60d-453c-98fe-e7ce27d17fbc
📒 Files selected for processing (2)
src/main/kimi/kimi-hook-config-toml.test.tssrc/main/kimi/kimi-hook-config-toml.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/main/kimi/kimi-hook-config-toml.test.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 5 remain after this review.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- CRLF-tolerant orphan recovery —
MANAGED_HOOK_TABLEand the orphaned-block branch now accept\r\nline boundaries, so a config resaved with Windows line endings afterBLOCK_ENDis hand-deleted still recovers the managed tables without swallowing the user tail. - CRLF regression test — new test exercises the mixed-endings case and asserts the user tail survives
removeManagedKimiHooks,readManagedKimiHookEventsstill recognizes the orphaned tables, andapplyManagedKimiHooksconverges to one block.
This resolves the CRLF gap CodeRabbit flagged: under the LF-only regex the (?:\n|$) terminator could not match the \r before the user tail, leaving the final managed table (and its command = COMMAND) behind — so the new test fails against the pre-CRLF regex and passes now. Full kimi-hook-config-toml.test.ts suite is green (10 tests).
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
CodeRabbit review follow-up: the orphan branch repeated MANAGED_HOOK_TABLE with only a newline-or-EOF terminator after the timeout line, so a user [[hooks]] table that reuses the installer field order but carries extra keys matched only its installer-shaped prefix — recovery deleted the header and known fields and detached the remaining keys from their table. Each recovered table must now be followed by the next table header or EOF; an extended table ends recovery intact (or declines it entirely when it is the first table after the orphaned marker), with regression coverage for both.
|
Re: the Linked Issues check on this PR — the lockstep target is not on Status of #18861's required outcome as implemented here:
The Vibe copy receives the same bounded-recovery change (this table boundary included) as soon as #18813 lands, keeping the two implementations identical as the issue requires. If the maintainers prefer the change to travel in this PR, I can rebase it onto #18813 and include the Vibe edit here instead. |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Team
Run ID: 7aa3a109-12a7-4a01-a95f-94c43b385d00
📒 Files selected for processing (2)
src/main/kimi/kimi-hook-config-toml.test.tssrc/main/kimi/kimi-hook-config-toml.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/main/kimi/kimi-hook-config-toml.test.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review.
…ndary CodeRabbit review follow-up: the table boundary added in the previous commit required the next table header on the immediately following line, so a blank line between the last orphaned table and appended user TOML made recovery decline entirely — remove left the orphaned hooks behind and reinstall appended duplicate managed hooks. The boundary now skips line breaks before the next-table lookahead (and before EOF), with LF and CRLF blank-separator regression cases. Extended user tables still end recovery intact.
There was a problem hiding this comment.
Important
The new (?=\[) boundary leaves the final managed table behind whenever a comment, blank line, or top-level key/value follows the orphaned block (see inline note), so removeManagedKimiHooks can report changed: true without actually removing the StopFailure hook.
Reviewed changes
- Table-boundary recovery — the orphan terminator changed from
(?:\r?\n|$)to(?:\r?\n(?=\[)|\r?\n*$), so each recovered table must be followed by another table header or EOF. This stops a user table that mirrors the installer field order but carries extra keys from being truncated to its installer-shaped prefix. - Extended-table regression tests — two new tests assert an extended user table (extra
prioritykey) survivesremoveManagedKimiHooksintact, and that recovery declines entirely when the first table after the marker is extended.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
Important
c1bab1a skips blank lines, but a # comment line between the last managed table and user content still breaks the boundary and leaves that final table behind — a partial strip that removeManagedKimiHooks reports as changed: true without actually removing the StopFailure hook. Details inline.
Reviewed changes
- Blank-line separators — the orphan terminator widened from
(?:\r?\n(?=\[)|\r?\n*$)to(?:(?:\r?\n)+(?=\[)|(?:\r?\n)*$), so one or more blank lines between the last managed table and the next table header no longer defeat recovery. - Blank-line regression test — a new test asserts a user
[tools]section following a blank line survivesremoveManagedKimiHooksand reinstall converges to one block, for both\nand\r\n.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
A '#' comment between the last orphaned managed table and the user's appended TOML defeated the terminator and stranded that table: removal reported changed=true while leaving a live managed hook command behind, and reinstall then left a stray duplicate table. Comments never extend a TOML table, so the zero-width recovery boundary now skips blank and comment lines before requiring a '[' header or EOF; the comment itself survives. A key/value line behind a comment still extends the table and declines recovery, keeping the round-2 guard. The boundary is now zero-width (separators are consumed only between recovered tables), so the last table keeps its trailing newline and removal no longer glues the surviving neighbors together.
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Team
Run ID: d5bcac2e-10a2-4aca-9cf7-adb9e55d1b2e
📒 Files selected for processing (2)
src/main/kimi/kimi-hook-config-toml.test.tssrc/main/kimi/kimi-hook-config-toml.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 6 remain after this review.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Comment-aware zero-width boundary — the orphan terminator is now
RECOVERED_TABLE_BOUNDARY, a zero-width lookahead that lets blank and#comment lines intervene between the last recovered table and the next[header or EOF. A comment ahead of user TOML no longer strands the final managed table, and a key/value continuation (even behind a comment) still correctly declines recovery. - Separator split — the consuming
RECOVERED_TABLE_SEPARATORis now applied only between recovered tables, which also fixes a latent newline-gluing defect where removal could produce invalid TOML likedefault_model = "x"[tools]. - Regression tests — three new tests cover comment → user table (LF + CRLF), comment → EOF, and comment → key-continuation (declines, leaving the extended table intact).
This resolves the prior [!IMPORTANT] finding: the full kimi-hook-config-toml.test.ts suite is green (16 tests), and I traced the comment → user table, comment → EOF, and comment → key-continuation cases through the new regex by hand against the assertions.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
The boundary required a line ending before its EOF branch could evaluate, so an orphaned block hand-trimmed to end on the timeout line stranded its final table: multi-table removals reported changed=true while leaving the last managed hook behind, and single-table blocks declined recovery entirely. Accept EOF as a direct alternative; the key/value-continuation guard is unchanged since $ only matches true end of input.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- EOF-direct boundary —
RECOVERED_TABLE_BOUNDARYnow accepts end-of-input directly ((?:$|…)) instead of requiring a line ending first, so an orphaned block whose last table ends on thetimeoutline with no trailing newline is still recovered rather than stranding the final managed table. - No-trailing-newline regression test — a new test asserts
removeManagedKimiHooksstrips the orphan todefault_model = "x"\nandreadManagedKimiHookEventsstill returns allKIMI_HOOK_EVENTSwhen the file ends directly on the lasttimeoutline.
The fix is minimal and correct: $ without the multiline flag only matches end-of-input (or before a final line terminator), so it cannot spuriously terminate recovery when user content follows the last table. The new test is meaningful — against the prior regex it backtracks to six of seven tables and strands the StopFailure table, failing the exact-equality assertions. Full kimi-hook-config-toml.test.ts suite is green (17 tests).
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
|
Thank you for this — the fix has landed in #20148 ( Your diagnosis was right and your mechanism is where that PR started: bound orphan recovery to contiguous installer-shaped tables and stop at the first line that doesn't match, instead of running to EOF. Two details in your patch we had to rediscover the hard way:
You also flagged the Vibe lockstep risk before either PR landed, and you were right: #18813 adds Where #20148 ended up differing, for the record:
None of that takes away from the diagnosis, which you got to first. Closing this one as superseded — thanks for the careful work, and for the Vibe catch especially. |

ELI5
If a user deletes one comment line (the block's end marker) and later adds their own settings after Orca's managed block, the cleanup regex used to delete their settings too. Now it only ever removes the settings Orca itself wrote.
What Changed
MANAGED_BLOCK_REorphan recovery (aBLOCK_STARTwhoseBLOCK_ENDwas hand-deleted) no longer matches to end-of-file: it now matches only the contiguous installer-shaped tables — an[[hooks]]header followed byevent/command/timeoutlines — and stops at the first line that does not mirror that shape.BLOCK_START … BLOCK_END) is unchanged.readManagedKimiHookEventsstill recognizes orphaned tables; reinstall still converges to a single block.Why
Fixes #18861: anything the user appended after installation — their own
[[hooks]]tables included — was silently deleted byremoveManagedKimiHooksor swallowed by theapplyManagedKimiHooksrewrite whenever the trailing marker was lost. A user table that happens to mirror the exact installer shape remains indistinguishable by form alone; stopping at the first non-matching table is the accepted bound, as the issue describes.Linked Issue
Fixes #18861
Visual Proof
N/A — no UI change; this alters TOML text manipulation only.
Testing
I manually tested these changes locally
Automated tests added/updated, or explained why not below
New regression test per the issue's required outcome: user hook (
SessionStart+ custom command) and tool tables appended after an orphaned block surviveremoveManagedKimiHooksuntouched, andapplyManagedKimiHooksstill converges to a single managed block while keeping them.Full
src/main/kimi/suite green (22 tests, includinghook-serviceintegration),pnpm run typecheck:nodeandpnpm run check:code-quality:changedclean on macOS (Darwin 24.6.0, arm64).Note on the vibe twin
vibe/hook-config-toml.tsdoes not exist onmainyet — it arrives with #18813, which the issue says must stay lockstep-identical. The same change should be applied there when that PR lands; happy to rebase or follow up on top of #18813 if maintainers prefer.