Skip to content

fix(host-service): classify worktree-gone terminal failures as NOT_FOUND - #6387

Merged
saddlepaddle merged 1 commit into
mainfrom
sentry/hs-terminal-worktree-notfound
Aug 12, 2026
Merged

saddlepaddle merged 1 commit into
mainfrom
sentry/hs-terminal-worktree-notfound

Conversation

@saddlepaddle

@saddlepaddle saddlepaddle commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

HOST-SERVICE-1N (14 events on 1.20.2): when a workspace's worktree directory is deleted while a terminal pane is still open, every terminal.snapshot poll throws INTERNAL_SERVER_ERROR and the Sentry middleware reports it as a 500 — even though a worktree removed underneath an open terminal is a routine lifecycle condition, not an internal failure.

Root cause

createTerminalSessionInternal (the adoption path under snapshotSession/writeFramedInputToSession) returns the plain string error Workspace worktree no longer exists: <path> when the worktree directory is gone. The router's toTerminalIoError helper name-checks a few known messages ("belong" → FORBIDDEN; "not found"/"not active"/"exited" → NOT_FOUND), but this message matches none of them, so it falls through to the INTERNAL_SERVER_ERROR branch and the dumb 500=report Sentry middleware files it on every poll.

No typed error class crosses this boundary — the runtime returns { error: string } — so the classification has to match the stable message prefix.

Fix

Add a branch to toTerminalIoError that recognizes the Workspace worktree no longer exists prefix and returns NOT_FOUND with cause: { kind: "WORKTREE_GONE" }. The message and all other behaviour are unchanged; genuine unexpected terminal I/O failures still fall through to INTERNAL_SERVER_ERROR and keep reporting.

All terminal procedures that funnel errors through this helper (send, snapshot) pick up the fix; there are no per-procedure copies.

Verification

$ bunx biome check packages/host-service/src/trpc/router/terminal/terminal.ts
Checked 1 file in 12ms. No fixes applied.

$ cd packages/host-service && bun run typecheck
$ tsc --noEmit --emitDeclarationOnly false
typecheck exit=0

No existing tests cover this helper (grep -rl "toTerminalIoError" **/*.test.ts → no matches), so no test run applies.

Refs HOST-SERVICE-1N

https://claude.ai/code/session_0c69890b-c39f-420c-b03a-8038231a89ad


Summary by cubic

Classifies "Workspace worktree no longer exists..." terminal errors as NOT_FOUND in host-service instead of INTERNAL_SERVER_ERROR, addressing HOST-SERVICE-1N and stopping false 500s/Sentry alerts. Old: terminal.snapshot and send returned 500; New: they return 404 with cause { kind: "WORKTREE_GONE" }; message unchanged.

Review notes

  • Change is isolated to toTerminalIoError; both procedures inherit it; other errors keep their codes.
  • No tests updated; the helper had no coverage.

Migration

  • Update any client logic that treated deleted worktree terminals as 500 to handle 404 NOT_FOUND instead.
  • No action needed if you already treat missing/closed sessions as NOT_FOUND.

Written for commit a0e24d8. Summary will update on new commits.

Review in cubic

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling when a workspace worktree no longer exists.
    • Users now receive a clear “not found” error with a specific worktree-related cause.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The terminal error translator now maps errors that begin with "Workspace worktree no longer exists" to a NOT_FOUND tRPC error with a { kind: "WORKTREE_GONE" } cause.

Changes

Terminal error classification

Layer / File(s) Summary
Map deleted worktree errors
packages/host-service/src/trpc/router/terminal/terminal.ts
toTerminalIoError converts deleted worktree errors into typed NOT_FOUND errors with a WORKTREE_GONE cause.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related PRs

Suggested reviewers: kitenite

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title uses conventional commit format and clearly identifies the main worktree-gone error classification fix.
Description check ✅ Passed The description clearly explains the problem, root cause, fix, affected procedures, verification, and testing limitations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sentry/hs-terminal-worktree-notfound

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.

🧹 Nitpick comments (1)
packages/host-service/src/trpc/router/terminal/terminal.ts (1)

25-34: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add focused tests for the new classification.

This branch changes the TRPCError contract. Add tests for the exact prefix, the prefix followed by additional details, and similar messages that do not match. Assert NOT_FOUND and cause.kind === "WORKTREE_GONE" for matching messages. Verify that existing mappings remain unchanged.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/host-service/src/trpc/router/terminal/terminal.ts` around lines 25 -
34, Add focused tests for the terminal error-classification logic covering the
exact “Workspace worktree no longer exists” prefix, that prefix with additional
details, and similar non-matching messages. Assert matching cases produce
TRPCError code NOT_FOUND with cause.kind equal to WORKTREE_GONE, and confirm
existing error mappings remain unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/host-service/src/trpc/router/terminal/terminal.ts`:
- Around line 25-34: Add focused tests for the terminal error-classification
logic covering the exact “Workspace worktree no longer exists” prefix, that
prefix with additional details, and similar non-matching messages. Assert
matching cases produce TRPCError code NOT_FOUND with cause.kind equal to
WORKTREE_GONE, and confirm existing error mappings remain unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ab73d99b-b355-4170-818a-e65a535128fc

📥 Commits

Reviewing files that changed from the base of the PR and between b671c27 and a0e24d8.

📒 Files selected for processing (1)
  • packages/host-service/src/trpc/router/terminal/terminal.ts

@cubic-dev-ai cubic-dev-ai 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 issues found across 1 file

You’re at about 99% of the monthly reviewed-line limit. You may want to disable incremental reviews to conserve quota. Reviews will continue until that limit is exceeded. If you need help avoiding interruptions, please contact contact@cubic.dev.

Re-trigger cubic

@github-actions

github-actions Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

🧹 Preview Cleanup Complete

The following preview resources have been cleaned up:

  • ✅ Neon database branch

Thank you for your contribution! 🎉

@saddlepaddle
saddlepaddle merged commit 12465b2 into main Aug 12, 2026
19 checks passed
@saddlepaddle
saddlepaddle deleted the sentry/hs-terminal-worktree-notfound branch August 12, 2026 17:48

This branch was previously deployed

1 inactive deployment
preview — a0e24d80 Deployed Aug 12, 2026 by saddlepaddle via Deploy API #7526
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