Skip to content

fix(service): combine startup ownership, token binding, and slot retention - #5512

Closed
luvs01 wants to merge 13 commits into
devfrom
stack/c-svc-startup
Closed

luvs01 wants to merge 13 commits into
devfrom
stack/c-svc-startup

Conversation

@luvs01

@luvs01 luvs01 commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Combine the startup/service-ownership fixes from #5477, #5306, and #5357 into the bottom layer of the service stack targeting dev.

The next stack layer adds package-tree replacement self-healing (drain-and-restart).

Verification

  • Current head: a12b2ad38deafdcd9672e8fea597d5f7a679aa78. This follow-up changes only documentation and the specific test consumers listed below; production source is byte-identical to the preceding published head 085d1dd2e8cdc88374e2eefd7b970cb9f7bf1ce3.
  • Hosted CI exposed structure/runtime.md growing to 602 lines when the branch was combined with newer dev. Two overlapping Remote Workspace descriptions were consolidated without losing their contracts or links. The same patch was verified against both exact failing merge trees (602 to 600) and dev (600 to 598), without raising the 600-line budget. Final combined structure checks passed.
  • The final service-fixture behavior was tested at b837cc09fec5e04a1165f4f0d07b4649fd0c914d: pinned Bun 1.4.0, three explicit files (package-tree-integrity, server-auth-localhost-bind, server-auth) filtered to package integrity / dotted localhost / management CORS / non-loopback management: 28 passed, 0 failed, 110 filtered, 119 assertions, 16.92 seconds. Those runtime and fixture files remain unchanged by this documentation follow-up. This is not the entire auth suite.
  • Earlier samples failed with 26 passed / 2 hook timeouts, then an instrumented 27 / 1. Instrumentation localized redundant startup migrations and deferred real ACL work inside beforeEach. Current-schema fixture projection removes those unrelated upgrade writes before one real saveConfig; actual ACL, token, lease, listener, guards, assertions and 5-second/900-second limits remain. Typecheck, structure and size checks passed after that correction; the final lower-layer typecheck and final upper-layer structure check also passed.
  • Full-suite/cross-platform completion, exact updated-head hosted CI and independent security review remain incomplete. No test timeout or safety boundary was weakened. This PR stays draft.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed (structure docs carried by source commits).
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

Review readiness

  • Local CI is green on this exact head.
  • Branch is based on the current dev commit at preparation time.
  • All correct Codex and CodeRabbit findings are fixed on the combined head.
  • Ready-for-review confirmation.

luvs01 and others added 4 commits September 22, 2026 18:25
…L ownership state

Combines three fork PRs touching service guards: bind reused-token hardening to the validated file (#502), normalize qualified localhost binds (#458), and accept legacy WSL ownership state (#262), rebased onto current dev.

bun test: service-secrets + service-auth-qualified-localhost + codex-home-wsl + server-auth-localhost-bind 21 pass; server-auth + windows-deploy-close-regressions 117 pass with 2 env-flaky (stalled-400 fails on clean dev baseline; catalog admission fails only in full-file ordering, passes isolated and on baseline)

(cherry picked from commit 6c9c3d8)
@coderabbitai

coderabbitai Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

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.

@luvs01
luvs01 added this pull request to stack #5514 September 22, 2026 09:47
@github-actions

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

✅ READY

  • all PR quality gates passed.

Hygiene

✅ Deterministic PR hygiene checks passed.

@lidge-jun

Copy link
Copy Markdown
Owner

리뷰 · 우선순위 65 / 80

이 PR은 서비스가 켜질 때 자주 틀어지던 네 가지를 한 번에 고친 스택 바닥(stack/c svc startup)입니다. 베이스는 선호하는 dev이고, 위로는 #5513(selfheal)이 이 브랜치를 밟습니다. 설명란은 템플릿만 있어서 비어 있고, draft로 남아 있습니다.

첫째, 이미 있는 데이터면 토큰 파일을 다시 쓸 때 경로를 두 번 보지 않고, 연 파일 핸들에서만 읽고 검사한 뒤 atomicWriteFileNoFollow로 다시 올립니다. 그래서 검사 중에 경로가 바뀌어도(심볼릭 링크 교체 등) 엉뚱한 파일을 조이지 않습니다. 이 작업은 설정 변경 잠금(withConfigMutationLockSync) 안에서만 돌아서, 허브 클라이언트 키 교체와 겹쳐 옛 토큰이 다시 덮어쓰지 않게 막습니다. 둘째, 호스트 이름 localhost.(끝점 있는 표기)도 localhost와 같이 루프백으로 보고 127.0.0.1에 묶고, 서비스 설치도 토큰을 요구하지 않습니다. 공통 isLoopbackHostname을 쓰도록 바꿨습니다. 셋째, WSL에서 예전에 리눅스 ~/.codex로 기록된 소유 상태를, 지금은 윈도우 Codex 홈을 찾는 환경에서도 같은 설치로 인정합니다(serviceCodexHomeMatchesInstall). 넷째, 윈도우 시작 시 소유권 검사를 두 번 할 때 예전처럼 첫 번째 fallback 목록을 재사용하지 않고, 두 번째 결정은 목록을 새로 받습니다. 다섯째, 스트리밍 응답이 끝날 때까지 워크플로 입장권(lease)을 턴 lease에 붙여 두어, 스트림이 도는 동안 슬롯이 먼저 풀리지 않게 합니다. 테스트·구조 문서도 같이 왔습니다. 커밋은 #5477·#5306·#5357 쪽 고치기를 cherry-pick해 모은 형태입니다.

라인 - PR 설명: Summary/Verification이 템플릿 그대로라 enforce-target이 bad description (empty)로 실패하고 draft가 유지됩니다.
라인 - src/lib/service-secrets.ts hardenReusedServiceApiToken — 빈/깨진 토큰 파일은 unsafe로 두고 새로 쓰지 않습니다. 원격 바인드에서는 설치가 여기서 막힙니다. 의도한 계약이면 설명란에 한 줄로 밝히면 좋습니다.
라인 - 작성자 커밋 메모: server-auth / windows-deploy-close-regressions 묶음에 환경·순서에 흔들리는 실패 2건이 있다고 적혀 있습니다. 머지 전에 재현·격리 여부를 한 번 더 확인하는 편이 안전합니다.
라인 - 같은 고치기가 열린 단독 PR로도 있습니다. #5477(토큰 바인딩·qualified localhost·WSL 소유), #5306(두 번째 시작 소유권용 목록 새로 받기), #5357(스트리밍 턴 워크플로 lease 유지).

메인테이너의 판단이 필요한 지점

이 스택(#5512 → #5513)을 본선으로 두고 #5477·#5306·#5357을 무효로 닫을지, 아니면 단독 PR을 남기고 스택을 버릴지 정해야 합니다. WSL에서 옛 리눅스 홈 기록을 “같은 설치”로 인정하는 범위가 제품 의도에 맞는지(명시 CODEX_HOME은 여전히 권위)도 한 번 확인이 필요합니다. 맥 CI는 취소로 빨간 상태인데, 코드 회귀인지 러너 취소인지 구분한 뒤 재실행이 필요한지 보면 됩니다.

너의 추천

방향은 맞습니다. 머지 전에 설명란을 커밋 요약 수준으로 채우고 draft를 풀 준비를 하세요. 스택을 채택한다면 중복 단독 PR(#5477·#5306·#5357)은 닫는 편이 낫습니다. types.ts/config.ts 쪼개기·preview deploy는 이번 변경과 무관합니다.

이 댓글은 grok-bot이 작성했습니다

The management CORS test can exhaust its 5s body limit during Windows startup,
before its local finally runs. Teardown then removes the home while the real
proxy still owns its spend SQLite transaction.

Give the CORS fixture an abort signal, tracked body and memoized listener stop.
Settle those before restoring seams and homes, retain the existing producer/ACL
drains, and await failed-start rollback when setup throws. Consume both HTTP
response bodies and prove cancellation releases the actual spend owner.

Keep real token ACL creation, HTTP admission and CORS decoration. Isolate native
Codex sync, host service ownership and settings-only runtime/service diagnostic
projections from this fixture, whose assertions concern management CORS headers.
Restore its scoped runtime spy after shutdown. No timeout, skip, ACL policy,
production source or dependency change.
Move CORS setup, request/server lifetime, scoped runtime restoration,
authenticated headers and fixture-state drains into a sibling test helper.
Keep server-auth.test.ts within its existing 4589-line ratchet cap without
changing the baseline or reducing assertions.

The CORS and real spend-lease cancellation bodies are unchanged. Preserve
the real token ACL path, production authentication/CORS behavior, setup
rollback, memoized shutdown, body settlement and existing test deadlines.
Reuse the owned management server fixture for the non-loopback settings test,
keeping its real 0.0.0.0 listener, LAN Host/Origin, missing-token rejection,
authenticated response and CORS assertion.

Prepare real token/ACL state before the HTTP body, isolate only unrelated host
diagnostic projections, track cancellation and consume both response bodies.
Teardown settles the body and actual listener/spend owner before restoring homes.
Rename the helper to its shared management-server role without changing policy,
timeouts, assertions or the file-size baseline.
@lidge-jun

Copy link
Copy Markdown
Owner

Carried into #5610 as 07f04a1 (squash of this PR's own diff at head a12b2ad, authorship kept). A review follow-up in #5610 (717ad8b) removes the WSL legacy-home allowance: stop and repair could act on a different Codex home than the recorded one, so service commands now require the recorded home and name it in the refusal. Closing as superseded by #5610. Thanks @luvs01.

@lidge-jun lidge-jun closed this Sep 22, 2026
lidge-jun added a commit that referenced this pull request Sep 23, 2026
)

* fix(service): combine startup ownership, token binding, and slot retention

Carries #5512 by @luvs01 (head a12b2ad), which
consolidates #5477, #5306 and #5357:

- bind the service API token to its owning state, canonicalize qualified-localhost
  binds, and carry WSL ownership state honestly (#5477);
- take a fresh task listing for the second startup ownership decision (#5306);
- retain workflow slots for streaming turns (#5357);
- own server-auth fixture lifetime and project a current-schema config for it.

Squashed from the PR's own diff (origin/dev...a12b2ad) onto current dev.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(server): self-heal a replaced package tree via drain-and-restart

Carries #5513 by @luvs01 (head 4d168f1), which
consolidates #5393 and its scheduler follow-up: detect a replaced installed package
tree, degrade health honestly, and drive a timer-driven, retryable drain-and-restart
whose verify step is deferred past scheduler re-entry. The guard factory lives in
src/server/index/package-tree-guard.ts.

Squashed from the PR's own diff (a12b2ad...4d168f1) onto the #5512 carry.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(security): combine install discovery, credential, and transport hardening

Carries #5515 by @luvs01 (head 843f299), which
consolidates #5359, #5285 and #5322:

- keep selected Codex installation discovery off network filesystems, probe
  oversized wrappers through a held-handle prefix read, and stop a PATH scan at a
  refused probe (#5359);
- exclude npm candidates inside the launch directory subtree (#5285);
- refuse plaintext remote hub origins, fail closed on POSIX chmod for credential
  files, and skip the frame-log write when descriptor hardening fails (#5322).

Squashed from the PR's own diff (origin/dev...843f299) onto the chain carry.
Integration: structure/runtime.md wording reflowed by two lines so the combined
service and security stacks stay within the 600-line structure budget.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(security): combine management-auth and boundary hardening

Carries #5516 by @luvs01 (head 245d542), which
consolidates #5326, #5312, #5363 and #5317:

- harden pairing redemption, agent roster intake, and SOCKS5 decoding (#5326);
- guard gh resolution, anchor the grok managed-region fences to whole lines, and
  bound provider-controlled text (#5312);
- harden management-auth admission and provenance (#5363);
- bound the /healthz version before it reaches diagnostics (#5317).

Squashed from the PR's own diff (843f299...245d542) onto the #5515 carry.
Integration: both stacks rewrote the shared server-auth test fixtures. The carry
keeps the #5512 current-schema fixture projection and config helper (including
its 4 KiB boundary case) and adds this PR's Aside sync capability assertions.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(security): combine adapter argv and upstream-body hardening

Carries #5517 by @luvs01 (head 260a87b), which
consolidates #5315 and #5336:

- stage Qoder and CodeBuddy system prompts in private files instead of
  child-process argv, with exclusive creation and owned cleanup (#5315);
- bound upstream error bodies and resolve account-scoped transports (Copilot,
  Devin) from the same OAuth snapshot as the bearer (#5336).

Squashed from the PR's own diff (245d542...260a87b) onto the #5516 carry.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* feat(codebuddy): integrate capture-only tools with private prompt staging

Carries #5582 by @luvs01 (head 3061ef9), which
integrates the capture-only CodeBuddy tool bridge from #5148 by @mdwsk88 with the
private prompt staging from #5517. Requests with a tool catalog advertise only the
allowed tools through an isolated MCP server that captures calls without executing
them; the client keeps approval, sandboxing and execution. Pre-init, undeclared,
excessive or incomplete calls are rejected, streamed malformed tool arguments are
suppressed, bridge staging failures return a fixed message, and an opt-in live
acceptance harness is included. Design context: #5146.

Squashed from the PR's own diff (260a87b...3061ef9) onto the #5517 carry.

Co-authored-by: mdwsk88 <924038395@qq.com>

* fix(client): bound total hub catalog response lifetime

Carries #5252 by @luvs01 (head 779ef91): give the
hub catalog body read an overall deadline (24x the inactivity window, capped at
120 s) on top of the inactivity window, and release refused, HTTP-error and 304
bodies without awaiting their cancellation.

Squashed from the PR's own diff (origin/dev...779ef91).

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(grok): reserve model aliases only when the written config stays valid

Reimplements #5281 by @luvs01. A user sub-table such as [model.ocx-mine.extra]
only creates an implicit parent, so it no longer forces the generated table to a
suffixed alias. The alias choice is now checked against the bytes actually
written: the unsuffixed alias is used only when the final config (after
model-reference rewriting) parses; otherwise the conservative choice that also
reserves deeper headers is used, and a valid user file for which neither choice
parses is refused without writing. Malformed user TOML keeps the previous
conservative reservation.

The original change reserved only exact two-segment headers, which could emit a
duplicate [model.x] table when the user defines model.x through dotted keys.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(codex-auth): scope Codex OAuth cancellation to the originating flow

Reimplements #4923 by @luvs01 on the current login-state layout (in-flight
controllers moved to src/oauth/login-flow-state.ts in #5220). Cancelling a Codex
login was keyed only by provider, so a stale modal posting an old flowId could
abort a newer attempt, and a cancel without a flowId expired every pending flow.

- Each in-flight controller records the flowId that started it; a cancel whose
  flowId does not match the active attempt is refused before anything aborts.
- POST /api/codex-auth/login/cancel requires a non-empty flowId, rejects unknown
  or non-pending flows with 400 without touching any row, and expires only that
  flow. Provider-wide cancellation through /api/oauth/login/cancel is unchanged.
- ocx account cancel requires --flow for Codex providers and sends no request
  without it.

The dashboard's 409 recovery keeps its code; its ownerless cancel is now refused,
so it ends in the existing "already in progress" message instead of superseding a
flow it does not own.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(socks5): bound compressed event streams by expansion, not total size

Review follow-up to the #5516 carry. The 32 MiB decoded-body cap applied to every
gzip/deflate response, so a long, normally compressed SSE stream through the
SOCKS5 tunnel was cut once its cumulative output crossed the cap. Buffered
responses keep the absolute cap; event streams may continue while decoded bytes
stay within the greater of 32 MiB or 128x the coded bytes consumed, which still
stops high-ratio bombs.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(codex): keep scanning PATH past a missing Windows candidate

Review follow-up to the #5515 carry. The held-handle reader reported a missing
file or directory as open-refused, so the default existence probe stopped the
PATH scan at the first absent PATHEXT candidate (for example codex.com) before it
reached an installed codex.cmd. NtCreateFile's object-name-not-found and
object-path-not-found statuses now map to a distinct not-found result that lets
the scan continue; every other failure still refuses.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(server): require Windows ACL hardening before a frame-log append

Review follow-up to the #5515 carry. On Windows the frame log ignored a failed
permission change and appended anyway. Each append now hardens the target with
the required Windows ACL helper and checks that the path still names the opened
file before writing; any failure writes nothing.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(devin): bind catalog authority to the tenant destination

Review follow-up to the #5517 carry.

- The observe-only OAuth snapshot applied the Copilot-validated apiBaseUrl to
  every provider, so a crafted Devin credential could carry a Copilot host that
  the snapshot claimed as its own. The overlay now applies only to github-copilot.
- Devin's live roster, stale fallback and cooldown were keyed by the token alone
  while discovery also depends on the validated tenant URL. The catalog authority
  and the matching routing-cache resolver now fingerprint the token together with
  the validated destination URL.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(codebuddy): fail closed on unverified bridge turns and staging collisions

Review follow-up to the #5582 carry.

- With the capture-only tool bridge armed, a successful terminal event is no
  longer accepted unless the CLI's system/init frame confirmed the bridge server;
  a turn that ends without it fails with tool_bridge_init_missing.
- A tool_use block that arrives only in the complete assistant message, without
  the partial tool events the bridge captures, now fails the turn instead of
  being dropped silently; partial captures are deduplicated by id.
- The catalog and MCP config staging files are created exclusively (wx, 0600),
  like the prompt file, so a pre-existing file fails before spawn.
- The history-argument repair for a missing JSON object prefix is documented and
  tested as a provider-agnostic contract; other malformed strings keep {}.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>
Co-authored-by: mdwsk88 <924038395@qq.com>

* fix(service): keep service-command ownership bound to the recorded home

Review follow-up to the #5512 carry. On WSL with CODEX_HOME unset, the carried
allowance treated a legacy Linux ~/.codex install record as owned when discovery
now selects the Windows profile, so service stop could stop the Linux-home
service and then restore native Codex in the Windows home, and repair could
rewrite the recorded home. Service commands again require the exact recorded
home and name it in the refusal; the unattended startup inspector reaches the
same foreign verdict.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(server): veto a package-tree restart when its server stops or loses ownership

Review follow-up to the #5513 carry.

- A package-tree restart accepted by the guard stayed scheduled after an explicit
  server.stop(), so the drain-and-respawn could reopen a server the caller had
  stopped. The caller that accepted a pending restart now receives a veto, and
  the guard uses it on dispose.
- When running as a supervised service child, the automatic path checks service
  home ownership when accepting and again before the handoff; a mismatch keeps
  the 503 fence and skips the restart.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(security): resolve gh from fixed paths and look up pairing grants by digest

Review follow-ups to the #5516 carry.

- On Windows the automatically polled star-status route derived gh.exe roots from
  ProgramFiles and LOCALAPPDATA, so a process environment could select any
  absolute directory. Windows candidates are now the fixed system install paths,
  and the child PATH is only the resolved executable's directory. Other installs
  report gh as unavailable, which only hides the sidebar star state.
- Pairing redemption looked each guess up by scanning every live grant; the map
  is keyed by the grant digest, so the lookup is now a direct get. A valid grant
  still redeems behind a throttled source.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* test(server): cover the one-shot Aside sync capability end to end

Review follow-up to the #5516 carry, which added a one-shot, HMAC-bound
capability for the default ocx sync path without exercising it. A real listener
now proves single use, refusal on replay, wrong path, query, method, pid or port,
expiry and a bad MAC, and that the CLI default path performs the attestation and
a bodyless POST (through a narrow transport seam).

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* test: register the review follow-up test files in the layout maps

Adds the three new test files from the L4 review follow-ups to both
scripts/test-layout/layout.json and tests/fixtures/test-layout-expected.json.

* test(grok): pin re-injection and strip for a nested user model table

Review follow-up to the #5281 reimplementation: two injections are byte
identical, every intermediate file parses, and strip restores the exact user
content.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(server): harden a Windows frame log once per file identity

Re-review follow-up: requiring Windows ACL hardening on every append spawned
icacls for every relayed frame and could stall the realtime relay. The hardened
file identity (device and inode) is now remembered for the log path; an
unchanged file skips the respawn, and a replaced file at the same path is
hardened again before any write.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* docs(structure): describe the package-tree restart veto and ownership recheck

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(server): stop an automatic restart from handing off after an explicit shutdown

Security review follow-up to the #5513 carry. Once an automatic package-tree
restart entered its drain, an operator shutdown (signal or management stop)
could still be followed by the restart handoff, because the drain cannot tell
its own listener stop from an independent one. Explicit shutdown paths now mark
the process, and an admission-bound restart checks that mark before every
handoff step. Manually requested restarts keep their behavior.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix(server): mark a management stop before its asynchronous teardown

Security re-review follow-up: the management stop route marked the explicit
shutdown only after awaiting the shared teardown, so an automatic restart
draining concurrently could reach its handoff in that window. The mark now
precedes the first await after the stop is accepted.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* test(server): allow post-lookup pruning in the pairing digest regression

The digest-lookup regression trapped every iteration of the grant map, so a
valid redemption failed once session minting pruned expired grants after the
lookup (hosted CI test 4/4). The trap now fails only on a scan that precedes the
digest lookup, which is the regression it guards.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

* fix: repair standalone bridge and restart ownership

Use the compiled CLI as the capture-only MCP entrypoint, release automatic restart fences on veto, align Devin discovery, and tighten Windows and local transport handling. Apply the documented Qoder prompt environment for both regions and update focused regressions and operator docs.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>

Co-authored-by: mdwsk88 <924038395@qq.com>

---------

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>
Co-authored-by: mdwsk88 <924038395@qq.com>
@lidge-jun
lidge-jun deleted the stack/c-svc-startup branch September 26, 2026 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants