Skip to content

fix(claude): register a short PostCompact hook command - #25211

Merged
nwparker merged 2 commits into
stablyai:mainfrom
vampik33:fix/short-postcompact-hook-command
Oct 11, 2026
Merged

nwparker merged 2 commits into
stablyai:mainfrom
vampik33:fix/short-postcompact-hook-command

Conversation

@vampik33

@vampik33 vampik33 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

After you compact a Claude conversation, Claude Code shows Orca's hook command to the model. That command was about 3 KB of shell code. This PR makes the command for that one hook short, so compacting no longer refills the context with it.

What Changed

Before: Orca registers a PostCompact hook so a manual /compact can clear the pane's "working" status. Its command is the same portable wrapper every event uses, about 3 KB, because it covers Windows Git Bash, PowerShell, a missing HOME, a missing script and so on. Claude Code prints every PostCompact hook's full command into the post-compact transcript, which the model reads. So each /compact added about 3 KB of shell to the context it had just freed.

After: on POSIX hosts (local macOS/Linux and SSH remotes), PostCompact gets a 371-byte POSIX command. When HOME is set and nonempty and the managed script is readable and executable, it runs the script with the agent's parent PID captured. Otherwise it drains stdin silently. An unset or empty HOME skips script lookup entirely. Every other event, and Windows, keep the full wrapper, unchanged.

How:

  • runtime-home-hook-command.ts exposes the POSIX branch on its own (wrapRuntimeHomePosixHookCommand). The full wrapper builds the same branch through a shared helper.
  • hook-settings.ts adds getManagedEventHook / getRemoteManagedEventCommand, which pick the hook per event.
  • applyManagedHooks takes a per-event hook function instead of one hook for all events.
  • ClaudeHookService uses it for local install, remote install and the installed-status check. An existing full-wrapper PostCompact entry is replaced, and isn't reported as a partial install.

Why

Making the shared wrapper shorter wasn't an option: it is deliberately portable, so one settings.json works on any OS. Only the echoed event pays for that size, so only the echoed event changes.

The short form gives up two things, and PostCompact needs neither:

  • the cross-OS fallback;
  • the neutral {} reply when the script is missing.

Windows keeps its launcher, because the short form can't run there.

The change applies to Claude, OpenClaude, Qoder and Qoder China, whose managed event lists include PostCompact.

Linked Issue

Fixes #25209

Visual Proof

N/A. No UI change; the only visible difference is the shorter hook line in Claude's post-compact output.

Testing

New post-compact-hook-command.test.ts covers:

  • the command is short on macOS and Linux;
  • other events and Windows are unchanged;
  • the remote command stays POSIX from every client platform;
  • running the real command with /bin/sh captures the exact parent PID, and drains 1 MB of stdin silently when the script is missing or HOME is unset or empty;
  • an unset or empty HOME performs no script lookup;
  • local install writes the short form and replaces an old full-wrapper entry, reporting "installed";
  • remote (SFTP) install writes the short form for PostCompact and the full wrapper elsewhere.

The managed-hook command contract test now covers the PostCompact command for claude and openclaude on every platform.

pnpm tc and pnpm run check:code-quality:changed origin/main pass. In src/main/claude, src/main/agent-hooks, src/main/qoder, src/main/openclaude, src/main/qwen-code and src/main/codebuddy, 3,259 tests pass (44 skipped).

The contributor manually tested /compact on Linux. On macOS, Claude Code 2.1.296 was also exercised through a real PTY with a local model-response stub because the saved login had expired: the original 3,027-byte command was saved in the transcript and included in the next model request; the short command completed successfully and replaced it in the next request.

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

AI Disclosure

Written with Claude Code (Claude Opus 5.5), reviewed and tested by hand.

Review

Checked migration, repeated installation, removal, user-hook preservation, platform selection, shell quoting, stdin draining, and process identity. Eight comparisons confirmed that extracting the POSIX branch leaves the full wrapper byte-identical.

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Cross-platform: Windows output is unchanged. macOS/Linux and SSH remotes get the POSIX-only form.
  • SSH: remote hosts are always POSIX, so the remote install uses the short form from any client OS.
  • Backwards compatibility: reinstalling the hooks replaces an existing full-wrapper PostCompact entry, and the status check treats the old entry as outdated.

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)

@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 — the full PR (1 commit, 7 files): replacing the ~3 KB portable wrapper on PostCompact with a short POSIX-only form so a manual /compact no longer re-inflates the context it just freed.

  • POSIX branch extracted (runtime-home-hook-command.ts): buildPosixBranch + wrapRuntimeHomePosixHookCommand emit just the [ -f ] && [ -r ] && [ -x ] branch with the stdin-drain fallback; the full wrapper now builds its POSIX arm through the same helper, so the two cannot drift.
  • Per-event hook selection (hook-settings.ts): getManagedEventHook returns the Windows launcher on win32 and buildManagedCommandHook(getRemoteManagedEventCommand(...)) on POSIX; for every event except PostCompact that resolves to exactly the old getManagedLifecycleHook value (pinned by a test), so only the echoed event changes.
  • Install/status threading (hook-service.ts): applyManagedHooks takes hookForEvent(eventName), and local install, remote SFTP install, and the installed-status check all route through the new helpers.
  • Tests: new post-compact-hook-command.test.ts covers length < 400, other events/Windows unchanged, remote POSIX from every client platform, real /bin/sh execution with ORCA_HOOK_AGENT_PID capture, silent 1 MB stdin drain with a missing script/HOME, local install replacing an old full-wrapper entry, and remote install shape; the command-contract ratchet now scans the short form too.
Technical details
# Verification notes (no action requested)

## What I checked
- `applyManagedHooks` signature change: every caller updated (`hook-service.ts` ×2, the two
  `claude`/`compact` tests). Grep found no stale single-hook call sites.
- Equivalence: on POSIX, `getManagedEventHook` for non-`PostCompact` events equals
  `getManagedLifecycleHook` because `getRemoteManagedCommand` already passes
  `{ neutralJsonWhenMissing: true }`. Confirmed by `hook-service`/contract suites, not just by reading.
- Migration: a pre-existing full-wrapper `PostCompact` is swept by the shared file-name matcher and
  replaced on the next startup reconcile (`installManagedAgentHooks` in `main-process-ready-runtime.ts`),
  so a transient `getStatus() === 'partial'` before that reconcile is expected and self-heals.
- Cross-platform: win32 install/status both still use the launcher; remote is always POSIX regardless of
  client OS. The short form keeps the `ORCA_HOOK_AGENT_PID` capture for `claude-hook`/`openclaude-hook`.

## Commands run
- `pnpm exec vitest run --config config/vitest.config.ts` on the five touched suites → 225 passed / 5 skipped.
- Same on `src/main/agent-hooks`, `src/main/qoder`, `src/main/openclaude` → 1169 passed / 12 skipped.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@coderabbitai

coderabbitai Bot commented Oct 4, 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: b2172e70-8916-474b-8080-2b13c3007a0a


📥 Commits

Reviewing files that changed from the base of the PR and between d9173ff and e452010706f04b3b209275dfa4de18e29ab8e279.



📒 Files selected for processing (7)
  • src/main/agent-hooks/managed-hook-command-contract.test.ts
  • src/main/agent-hooks/runtime-home-hook-command.ts
  • src/main/claude/claude-hook-event-install.test.ts
  • src/main/claude/compact-status-registration.test.ts
  • src/main/claude/hook-service.ts
  • src/main/claude/hook-settings.ts
  • src/main/claude/post-compact-hook-command.test.ts


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

Walkthrough

Claude hook command generation now uses a short POSIX wrapper for PostCompact, while other events retain their existing managed commands. Status checks and local and remote installations derive commands per event. Tests cover command generation, POSIX execution, and installation behavior.



Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to e4520

The shorter PostCompact command is recognized during installation and removal, and no issue requiring a pre-merge fix was established.

Security Architecture Review

Security architecture risk: 🔵 Low · up to e4520

The shorter command preserves existing execution permissions and authentication checks. No new attack path was identified, but complete security coverage is not established.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed command operates within the invoking user's local or SSH environment and invokes the same managed script. The inspected path does not introduce broader privileges, a new service destination, or additional data-store authority.

Trust Boundaries and Controls

  • observed — The short invocation preserves runtime-home resolution, script availability checks, and parent-process capture. The script sink retains required endpoint, port, token, and pane values, with authenticated localhost posting and timeout controls.

Resilience and Maintainability Implications

  • inferred — Concurrent read-modify-write installations can still overwrite intervening settings changes, and remote installation lacks a rolling backup. These are pre-existing recovery and configuration-integrity limitations, not demonstrated regressions from per-event selection.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 13.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 15 functions across 7 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check Passed Issue [#25209] requires a short PostCompact command on non-Windows hosts. getManagedEventHook uses the POSIX-only command for local PostCompact on macOS and Linux. getRemoteManagedEventCommand…
Out of Scope Changes check Passed The changes to the shared runtime wrapper, per-event hook selection, Claude-compatible settings, and command contract tests all support the short PostCompact command or verify that existing event an…
Title check Passed The title clearly identifies the main change: registering a shorter Claude PostCompact hook command.
Description check Passed The description is complete and follows the required template. It explains the user impact, implementation, rationale, linked issue, testing, platform coverage, compatibility considerations, and check…

  • Fix all pre-merge checks with AI
  • Autopilot · 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.

vampik33 and others added 2 commits October 10, 2026 21:19
Claude Code echoes every PostCompact hook's full command into the
post-compact transcript, so Orca's ~3KB portable wrapper landed in the
model's context after every /compact. On POSIX hosts (local macOS/Linux and
SSH remotes) PostCompact now gets only the POSIX branch of the wrapper;
every other event, and Windows, keep the full wrapper.

Fixes stablyai#25209
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.

[Bug]: PostCompact hook command adds ~3KB of shell to the context after every /compact

2 participants