# Phase 7, Tasks 4-5 -- Audit Report (Round 3)

**Auditor focus:** Re-audit after Round 2 amendments (AP-R2-6 through AP-R2-16, AU-R2-1).

**Summary:** Two substantive errors remain. Task 4 incorrectly claims `SessionManagerServiceLive` provides `SessionManagerStateTag` (it does not -- it provides `SessionManagerServiceTag`). Task 4 also references a nonexistent `PtySession` type. Task 5's `RelayStateLive` is consequently missing `makeSessionManagerStateLive()`, and its NOTE listing externally-provided bridge Tags is incomplete -- 7 bridge Tags still actively consumed by handlers are absent. The Task 5 test code references bridge Tags and calls imperative methods, which contradicts the Option B (state Tags only) direction but may be intentional for transition-period smoke-testing.

---

## Findings

| # | Category | Action | Issue | File:Line | Amendment / Question |
|---|----------|--------|-------|-----------|----------------------|
| 1 | Incorrect Code | **Amend Plan** | Task 4 line 541 states "`SessionManagerStateTag` -- already provided by `SessionManagerServiceLive`". This is wrong. `SessionManagerServiceLive` (session-manager-service.ts:173) is `Layer.succeed(SessionManagerServiceTag, {...})` -- it provides **`SessionManagerServiceTag`**, not `SessionManagerStateTag`. The `SessionManagerStateTag` is provided by `makeSessionManagerStateLive()` in `session-manager-state.ts:61-67`. | `session-manager-service.ts:173`, `session-manager-state.ts:61` | Fix line 541 to: "`SessionManagerStateTag` -- already self-constructing via `makeSessionManagerStateLive()` in `session-manager-state.ts`". Add a separate bullet for `SessionManagerServiceTag` if desired. |
| 2 | Incorrect Code | **Amend Plan** | Task 4 Step 2 imports `type { PtySession } from "../relay/pty-manager.js"` (plan line 577). No such type exists in `pty-manager.ts`. The actual type is `PtySessionState` (exported at line 19 of pty-manager.ts). The `PtyManagerState` interface and all functions (`registerPtySession`, `removePtySession`, `getPtySession`) use this wrong type name. | `pty-manager.ts:19` | Replace every occurrence of `PtySession` with `PtySessionState` in the Task 4 Step 2 code snippet. |
| 3 | Missing Wiring | **Amend Plan** | Task 5 `RelayStateLive` (plan lines 707-727) is missing `makeSessionManagerStateLive()`. The `SessionManagerServiceLive` included in the merge only provides `SessionManagerServiceTag` (function references). The functions themselves (`listSessions`, `createSession`, etc.) require `SessionManagerStateTag` at call time. Without `makeSessionManagerStateLive()` in `RelayStateLive`, any call to these functions will fail with a missing Tag error. | Plan line 707-727 | Add `makeSessionManagerStateLive(),` to the `RelayStateLive` composition, e.g. after `makeOverridesStateLive()`. |
| 4 | Missing Wiring | **Amend Plan** | Task 5 Step 2 NOTE (plan lines 728-731) lists 8 externally-provided bridge Tags, but the existing `makeHandlerLayer` in `layers.ts` provides 15 bridge Tags. The NOTE omits 7 bridge Tags that are still actively consumed by Effect handlers: `SessionManagerTag`, `WebSocketHandlerTag`, `SessionOverridesTag`, `PtyManagerTag`, `StatusPollerTag`, `SessionRegistryTag`, `PollerManagerTag`. These must still be provided via `Layer.succeed` in Step 3 until Task 8 deletes them. Optional Tags (`ReadQueryTag`, `ClaudeEventPersistTag`, `ProviderStateServiceTag`, `InstanceMgmtTag`, `ProjectMgmtTag`, `ScanDepsTag`) are also absent from the NOTE. | Plan lines 728-731; `layers.ts:179-248` | Expand the NOTE to list ALL bridge Tags that remain externally provided. Add: `SessionManagerTag`, `WebSocketHandlerTag`, `SessionOverridesTag`, `PtyManagerTag`, `StatusPollerTag`, `SessionRegistryTag`, `PollerManagerTag`, and the optional persistence/daemon Tags. |
| 5 | Implicit Assumptions | **Accept** | Task 5 Step 5 test (plan lines 806-810) uses bridge Tags (`WebSocketHandlerTag`, `SessionManagerTag`, `SessionRegistryTag`) and calls `wsHandler.getClientCount` as if it's a property on `WebSocketHandlerShape`. In the Option B world, `getClientCount` is a standalone Effect in `ws-handler-service.ts:359`. This is acceptable as a transition-period smoke test (bridge Tags must still work), but implementers should be aware the test validates bridge wiring, not the new state Tag architecture. | Plan lines 806-810 | -- |
| 6 | Implicit Assumptions | **Accept** | `SessionManagerServiceLive` is `Layer.succeed`, meaning its Layer type is `Layer<SessionManagerServiceTag, never, never>` -- it has zero declared requirements. The actual runtime requirements (`OpenCodeAPITag`, `SessionManagerStateTag`) are deferred to the call site. This is correct Effect behavior but may mislead implementers into thinking `SessionManagerServiceLive` alone is sufficient. The plan's current text (finding #1) actively causes this confusion. Fixing finding #1 and #3 resolves this. | `session-manager-service.ts:173` | -- |

---

## Verification of Round 2 fixes

| Fix | Status | Notes |
|-----|--------|-------|
| AP-R2-6 (Option B: state Tags only) | Partially applied | Task 4 correctly describes state Tags, but the `SessionManagerStateTag` entry (line 541) is wrong (finding #1). |
| AP-R2-7 (SessionManagerServiceLive name collision) | Applied | Plan correctly distinguishes `SessionManagerServiceTag` vs `SessionManagerStateTag` everywhere except line 541. |
| AP-R2-8 (RelayStateLive renamed, Tags enumerated) | Partially applied | Layer name correct. Tag list is missing `makeSessionManagerStateLive()` (finding #3). |
| AP-R2-9 (OpenCodeAPILive reference removed) | Applied | `OpenCodeAPITag` now provided via `Layer.succeed` in Step 3. |
| AP-R2-10 (Poller state/pubsub added) | Applied | `makePollerStateLive()` and `makePollerPubSubLive()` present in `RelayStateLive`. |
| AP-R2-11 (runtime.runSync -> runPromise) | Applied | Step 4 uses `runtime.runPromise`. |
| AP-R2-15 (HashMap example replaced) | Applied | Task 4 verifies existing modules instead. |
| AP-R2-16 (SessionManager deps clarified) | Partially applied | But the clarification itself is wrong (finding #1). |
| AU-R2-1 (Option B chosen) | Applied | Consistent throughout. |

## Verified correct (actual module exports match plan claims)

These modules were verified on the branch and match what the plan claims:

- `session-registry-state.ts` -- exports `makeSessionRegistryStateLive` (line 29)
- `session-overrides-state.ts` -- exports `makeOverridesStateLive` (line 46)
- `message-poller.ts` -- exports `makePollerManagerStateLive` (line 19)
- `session-status-poller.ts` -- exports `makePollerStateLive` (line 109) and `makePollerPubSubLive` (line 117)
- `ws-handler-service.ts` -- exports `makeWsHandlerStateLive` (line 85)
- `instance-manager-service.ts` -- exports `makeInstanceManagerStateLive` (line 76)
- `ManagedRuntime.runPromise` -- confirmed in `effect/dist/dts/ManagedRuntime.d.ts:86`

## No issues found in

- State Issues (no new shared-state race conditions introduced)
- Non-Strict Typing (plan code uses proper Tag/Layer types)
- Fragile Code (no hardcoded values beyond the rate limiter config which mirrors existing code)
