happier icon indicating copy to clipboard operation
happier copied to clipboard

fix: match session webhook PID to parent when wrapper scripts are used

Open bitfieldz opened this issue 9 months ago • 2 comments

Summary

  • When the daemon spawns a session through a wrapper script (e.g. a Node.js entrypoint that spawns the actual binary as a child process), the daemon tracks the wrapper's PID while the session reports the binary's PID via webhook
  • This PID mismatch causes the session to be registered as "externally-started," a ~90-second webhook timeout, and delayed spawn response to the client
  • When an unknown PID reports via webhook, now checks if its parent PID (PPID) matches any daemon-tracked PID
  • If a match is found, re-keys the tracked session to the actual PID and resolves pending awaiters immediately

Implementation

  • Adds getParentPid() helper using ps -o ppid= -p <pid> (Unix only, <10ms, 1s timeout guard)
  • On Windows the PPID check is skipped and existing behavior is preserved
  • Only runs when a PID is NOT in the tracked map (infrequent path)
  • Failures are caught gracefully — worst case falls back to existing "register as externally-started" behavior

Context

Discovered with compiled binary deployments where a Node.js entrypoint wrapper spawns the actual Happier binary. The daemon tracks the wrapper's PID, but the session webhook reports the binary's PID. This causes a PID mismatch that results in a 90-second timeout before the spawn response reaches the client, even though the session itself works fine.

Test plan

  • [ ] Verify daemon-spawned sessions are correctly tracked when spawned directly (no wrapper)
  • [ ] Verify daemon-spawned sessions via wrapper scripts are re-keyed to the correct PID
  • [ ] Verify the ~90-second webhook timeout no longer occurs in the wrapper scenario
  • [ ] Verify externally-started sessions (not spawned by daemon) are still registered correctly
  • [ ] Verify Windows behavior is unaffected (PPID check skipped)

bitfieldz avatar Feb 28 '26 13:02 bitfieldz

[!WARNING]

Rate limit exceeded

@Spirotot has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 29 minutes and 20 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 1037536329cc13dbad12d896169fb7dc84c12337 and a6aec5ce3cf70eac96b9883d0f34cbb7e0851983.

📒 Files selected for processing (1)
  • apps/cli/src/daemon/sessions/onHappySessionWebhook.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

[!TIP] Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). Share your feedback on Discord.


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 and usage tips.

coderabbitai[bot] avatar Feb 28 '26 13:02 coderabbitai[bot]

Greptile Summary

Fixes daemon session tracking when wrapper scripts spawn the actual binary. When a session webhook reports an unknown PID, checks if its parent PID (PPID) matches a daemon-tracked session, then re-keys the tracking to the actual process PID and resolves pending awaiters immediately.

  • Added getParentPid() helper using ps -o ppid= (Unix only, Windows returns null)
  • Re-keys tracked session from wrapper PID to child PID when PPID match found
  • Resolves pending awaiters immediately instead of waiting 90s
  • Gracefully falls back to existing "externally-started" behavior on failures

Important: Missing test coverage for the new PPID matching logic. Should add tests to verify re-keying behavior, awaiter resolution, and error handling.

Confidence Score: 4/5

  • safe to merge with recommendation to add test coverage
  • implementation is sound with proper error handling and graceful fallbacks, but lacks test coverage for the new PPID matching logic which is important for this critical daemon functionality
  • no files require special attention - implementation is straightforward and well-documented

Important Files Changed

Filename Overview
apps/cli/src/daemon/sessions/onHappySessionWebhook.ts adds PPID matching to fix wrapper script session tracking, logic is sound but needs test coverage and input validation

Sequence Diagram

sequenceDiagram
    participant Daemon
    participant Wrapper as Wrapper Script<br/>(PID 1000)
    participant Binary as Session Binary<br/>(PID 1001)
    
    Note over Daemon: Tracks session by PID 1000
    Daemon->>Wrapper: spawn wrapper script
    activate Wrapper
    Wrapper->>Binary: spawn actual binary
    activate Binary
    Wrapper-->>Daemon: returns (tracked as PID 1000)
    
    Note over Daemon: Awaiting webhook from PID 1000
    Binary->>Daemon: webhook with PID 1001
    Note over Daemon: PID 1001 not in tracked map!
    
    Daemon->>Daemon: getParentPid(1001) → 1000
    Daemon->>Daemon: find session for PID 1000
    Note over Daemon: Match found! Re-key session
    
    Daemon->>Daemon: delete session[1000]
    Daemon->>Daemon: set session[1001]
    Daemon->>Daemon: resolve awaiter immediately
    deactivate Binary
    deactivate Wrapper

Last reviewed commit: a6aec5c

greptile-apps avatar Feb 28 '26 13:02 greptile-apps