Skip to content

fix(server): 遅いWebSocket接続でPTY出力をバイト上限で遮断 - #41

Open
yukihamada wants to merge 6 commits into
mainfrom
fix/pty-output-gate
Open

yukihamada wants to merge 6 commits into
mainfrom
fix/pty-output-gate

Conversation

@yukihamada

Copy link
Copy Markdown
Owner

クライアントが読み切れない間もPTY出力を溜め込みメモリを圧迫し得たため、未送信バイト数に上限を設け、超過時は安全に切断する。

  • packages/server/src/handlers/pty.ts: バイト上限ゲート追加
  • packages/server/test/pty-output-gate.test.ts: 回帰テスト3件

検証: 回帰テスト3件・型検査成功(ローカル実施)

yukihamada and others added 6 commits September 16, 2026 14:13
Adopt the agent-first CLI convention from block/buzz so a caller can branch
on the exit code alone without parsing human-readable output.

- src/cli/exit-code.ts: 0 success / 1 user / 2 network / 3 auth / 4 other /
  5 conflict, plus exitCodeForError() mapping named and tagged errors onto it
- run.ts: replace 10 undifferentiated process.exit(1) calls with classified
  codes; auth failures now exit 3 so callers stop retrying in a loop
- run.ts: --format json now keeps stdout a pure event stream and emits every
  warning (unknown agent, share URL, auto-rejected permission) to stderr as a
  structured {type:diagnostic} record instead of ANSI-styled prose
- error records carry exitCode so a consumer reads the classification from the
  stream as well as from the process status

Verified: 9/9 unit tests, 30/30 turbo typecheck, oxlint 0 errors, and
measured exit=1 with pure-JSON stderr across the usage-error paths.
Subprocess tests remain failing in this environment on the base commit too
(SENTE_CONFIG in the ambient shell overrides SENTE_CONFIG_CONTENT), so the
new end-to-end assertions were dropped rather than left red.
The contract landed in `sente run` (86d041f) but `sente tui` still used
bare `process.exitCode = 1`. All five sites are pre-launch argument
validation (--replay, --mini with network flags, unsupported --mini-only
options, --fork without a session, and worker connection failure), so all
five are ExitCode.User — the classification is unchanged, but it is now
stated instead of implied, and a future change cannot silently drift.

TUI remains interactive: it does not gain --format json, so no structured
output is added here.

Verified: 30/30 turbo typecheck, oxlint 0 errors, measured exit=1 for
--replay and --fork, and no new failures against the baseline.
…dicate

The launcher already used 75 (SENTE_RESTART_EXIT_CODE) for the TUI's
"compact and resume" request, but it was invisible to the contract — a
supervisor reading exit-code.ts would have classified it as Other and
retried it like a transient failure.

- ExitCode.Restart = 75, deliberately outside 0-5 so it cannot collide
- shouldRestart(code): true only for Network(2), Other(4) and Restart(75).
  Permanent failures (User 1, Auth 3, Conflict 5) return false.

The predicate exists because launchd KeepAlive restarts on any exit: a bad
flag or an expired key becomes an infinite loop. That is not hypothetical —
it burned ~4.3M credits over three days in 2026-09.

Verified: 14/14 unit tests including the KeepAlive cases, 30/30 typecheck.
…gh release (the manual Sente distribution recipe)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
高級モデルは1ターンで数千〜数万クレジットを消費しうる。残高が尽きると
推論が止まり Sente が黙るため、**モデルを選ぶ直前**に気づけるようにした。

- util/credits.ts を追加
  - creditsRemaining(): provider 層が last-route.json へ退避した
    x-teai-credits-remaining を読む。ファイル無し/破損/非数値は null
  - isPremiumModel(): cost.output が閾値(15)を超えるモデルを高級と判定。
    cost 不明なモデルは誤検知を避けるため false
  - LOW_CREDITS_THRESHOLD = 10_000cr
- context/local.tsx の model.set() と model.cycle() に警告を接続
  (/models からの選択と Fキー循環の両経路をカバー)

残高不明な場合は何も出さない — 推測でユーザーの選択を邪魔しないため。

検証: bun typecheck クリーン / bun test 206 pass 0 fail
クライアントが読み切れない間もPTY出力を溜め込みメモリを圧迫し得たため、
未送信バイト数に上限を設け、超過時は安全に切断する。
回帰テスト3件で固定。
@github-actions

Copy link
Copy Markdown

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

Copy link
Copy Markdown

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant