Skip to content

fix(kimi): bound orphaned managed-block recovery to installer-shaped tables - #19033

Closed
cat-xierluo wants to merge 6 commits into
stablyai:mainfrom
cat-xierluo:fix/kimi-orphan-marker-bounded-recovery
Closed

cat-xierluo wants to merge 6 commits into
stablyai:mainfrom
cat-xierluo:fix/kimi-orphan-marker-bounded-recovery

Conversation

@cat-xierluo

@cat-xierluo cat-xierluo commented Sep 6, 2026 •

Copy link
Copy Markdown

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_RE orphan recovery (a BLOCK_START whose BLOCK_END was hand-deleted) no longer matches to end-of-file: it now matches only the contiguous installer-shaped tables — an [[hooks]] header followed by event/command/timeout lines — and stops at the first line that does not mirror that shape.
  • The complete-block branch (BLOCK_START … BLOCK_END) is unchanged.
  • readManagedKimiHookEvents still 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 by removeManagedKimiHooks or swallowed by the applyManagedKimiHooks rewrite 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 survive removeManagedKimiHooks untouched, and applyManagedKimiHooks still converges to a single managed block while keeping them.

  • Full src/main/kimi/ suite green (22 tests, including hook-service integration), pnpm run typecheck:node and pnpm run check:code-quality:changed clean on macOS (Darwin 24.6.0, arm64).

Note on the vibe twin

vibe/hook-config-toml.ts does not exist on main yet — 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.

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

coderabbitai Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Kimi 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 b563a

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)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The Kimi implementation satisfies the bounded orphan-recovery and regression-testing requirements. However, issue #18861 requires equivalent changes in both Kimi and Vibe configurations, and this PR c… Apply the same bounded-recovery implementation and regression tests to the Vibe configuration, either by rebasing this PR after #18813 lands or by submitting a follow-up that completes the required Kimi and Vibe changes together.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: bounding orphaned managed-block recovery to installer-shaped tables.
Description check ✅ Passed The description explains the problem, implementation, linked issue, testing, and lack of UI impact. Some template sections, including the checklist and AI disclosure, are omitted, but the core require…
Out of Scope Changes check ✅ Passed The implementation changes and regression tests are directly related to preserving user TOML during orphaned managed-block recovery. No unrelated code changes are identified.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Full details: Linked Issues check

Explanation

The Kimi implementation satisfies the bounded orphan-recovery and regression-testing requirements. However, issue #18861 requires equivalent changes in both Kimi and Vibe configurations, and this PR changes only Kimi because the Vibe implementation is not yet present on main.


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: Team

Run ID: 10217d0e-fdb5-4036-8941-82869cf340ce

📥 Commits

Reviewing files that changed from the base of the PR and between a37a0b5 and c3d2a15.

📒 Files selected for processing (2)
  • src/main/kimi/kimi-hook-config-toml.test.ts
  • src/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.

Comment thread src/main/kimi/kimi-hook-config-toml.ts Resolved

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

✅ 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-deleted BLOCK_END survives remove/reinstall.
  • Regression test — new test asserts a user [[hooks]] + [[tools]] tail is preserved by both removeManagedKimiHooks and applyManagedKimiHooks, and that reinstall still converges to a single block.
  • Complete-block path — the BLOCK_START … BLOCK_END branch 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).

Pullfrog  | View workflow run | Using 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).

@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: Team

Run ID: 33b0f650-e60d-453c-98fe-e7ce27d17fbc

📥 Commits

Reviewing files that changed from the base of the PR and between c3d2a15 and df0da2b.

📒 Files selected for processing (2)
  • src/main/kimi/kimi-hook-config-toml.test.ts
  • src/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.

Comment thread src/main/kimi/kimi-hook-config-toml.ts Resolved

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

✅ No new issues found.

Reviewed changes

  • CRLF-tolerant orphan recovery — MANAGED_HOOK_TABLE and the orphaned-block branch now accept \r\n line boundaries, so a config resaved with Windows line endings after BLOCK_END is 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, readManagedKimiHookEvents still recognizes the orphaned tables, and applyManagedKimiHooks converges 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).

Pullfrog  | View workflow run | Using 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.
@cat-xierluo

Copy link
Copy Markdown
Author

Re: the Linked Issues check on this PR — the lockstep target is not on main yet: src/main/vibe/hook-config-toml.ts ships with the still-open #18813, so there is no Vibe copy in this PR's base to apply the change to.

Status of #18861's required outcome as implemented here:

  • The to-EOF orphan fallback is gone: recovery is bounded to contiguous installer-shaped [[hooks]] tables, and each recovered table must be followed by the next table header or EOF, so a user table that reuses the installer field order but carries extra keys is never truncated.
  • Regression coverage spans the appended user tail, CRLF resaves, and extended user tables.

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.

@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: Team

Run ID: 7aa3a109-12a7-4a01-a95f-94c43b385d00

📥 Commits

Reviewing files that changed from the base of the PR and between df0da2b and f309800.

📒 Files selected for processing (2)
  • src/main/kimi/kimi-hook-config-toml.test.ts
  • src/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.

Comment thread src/main/kimi/kimi-hook-config-toml.ts Resolved
…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.

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

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 priority key) survives removeManagedKimiHooks intact, and that recovery declines entirely when the first table after the marker is extended.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

Comment thread src/main/kimi/kimi-hook-config-toml.ts Resolved

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

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 survives removeManagedKimiHooks and reinstall converges to one block, for both \n and \r\n.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

Comment thread src/main/kimi/kimi-hook-config-toml.ts Resolved
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.

@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: Team

Run ID: d5bcac2e-10a2-4aca-9cf7-adb9e55d1b2e

📥 Commits

Reviewing files that changed from the base of the PR and between c1bab1a and b563ae2.

📒 Files selected for processing (2)
  • src/main/kimi/kimi-hook-config-toml.test.ts
  • src/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.

Comment thread src/main/kimi/kimi-hook-config-toml.ts Resolved

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

✅ 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_SEPARATOR is now applied only between recovered tables, which also fixes a latent newline-gluing defect where removal could produce invalid TOML like default_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.

Pullfrog  | View workflow run | Using 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.

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

✅ No new issues found.

Reviewed changes

  • EOF-direct boundary — RECOVERED_TABLE_BOUNDARY now accepts end-of-input directly ((?:$|…)) instead of requiring a line ending first, so an orphaned block whose last table ends on the timeout line with no trailing newline is still recovered rather than stranding the final managed table.
  • No-trailing-newline regression test — a new test asserts removeManagedKimiHooks strips the orphan to default_model = "x"\n and readManagedKimiHookEvents still returns all KIMI_HOOK_EVENTS when the file ends directly on the last timeout line.

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

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@nwparker

Copy link
Copy Markdown
Contributor

Thank you for this — the fix has landed in #20148 (438e0f4), and you're credited as co-author on it.

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:

  • CRLF-aware line boundaries. Easy to miss, and a config saved by a Windows editor after the end marker was deleted is exactly the situation that needs to stay recoverable.
  • The boundary check so recovery doesn't truncate a user table. A user table that reuses the installer field order but carries extra keys must be left whole, not cut down to its installer-shaped prefix. That's a subtle failure and you'd already guarded it.

You also flagged the Vibe lockstep risk before either PR landed, and you were right: #18813 adds src/main/vibe/hook-config-toml.ts carrying a verbatim copy of the pre-fix regex, so it would have reintroduced the same data-loss path for Vibe. That's now called out on #18813 with a pointer to the shared module. Nobody else had spotted it.

Where #20148 ended up differing, for the record:

  1. A second instance of the same bug. src/main/codex/codex-hook-legacy-cleanup.ts had the same "no end marker means delete to EOF" shape, against a file that also holds the user's [projects.*] trust entries.
  2. Bounded recovery leaves a live hook. Stopping at the first unrecognized line means a managed table stranded below user text survives uninstall and keeps firing — invisible to status, and the next startup install then double-fires that event. So ownership became "a table invoking our script is ours wherever it sits", with remove, install and status sharing one test.
  3. Exact marker matching. Both our versions matched the marker as a prefix, so a user quoting it in a comment of their own could open a region. Caught in review on fix(hooks): stop orphaned managed markers from consuming user TOML #20148.

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.

@nwparker nwparker closed this Sep 12, 2026
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.

Managed hook marker regex: orphaned end-marker deletes user TOML (Kimi + Vibe)

2 participants