Skip to content

fix(daemon): report failed close/disconnect, cancel shutdown timers, scope daemon diagnostics - #4357

Merged
thymikee merged 19 commits into
mainfrom
claude/audit-findings-remediation-62d6ky-daemon-lifecycle
Nov 10, 2026
Merged

thymikee merged 19 commits into
mainfrom
claude/audit-findings-remediation-62d6ky-daemon-lifecycle

test: name the moved lease and connection cases for the modules they …

b8cf1bb
Select commit
Loading
Failed to load commit list.
apex-sentinel-staging / Apex Sentinel succeeded Nov 10, 2026 in 8m 56s

4 finding(s) · approve

Apex Sentinel

Verdict: approve

4 finding(s): 4 minor.

🟡 Minor (4)

Where Finding
packages/daemon/src/sessions/session-lifecycle/internal/session-close-script.ts:108 Close script failure now drops its actionable hint whenever the inner error has one
packages/daemon/src/core/session-script-writer.ts:186 Ordinary close no longer fails loud: callers that ignore result.error report success
packages/daemon/src/server/provider-lease-expiry.ts:120 writeJournal now runs detached in a diagnostics scope; its rejection is unhandled
packages/remote/src/remote-connection-state.ts:288 isRemoteConfigReferenced treats an unreadable record as unreferenced, so a secret-bearing profile can be deleted while in use

Reviewed with this repository's own Sentinel configuration (093341a6040c).

Feedback goes in the review comment threads, as a reply under the inline comment of a finding:

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Comment @apex-sentinel-staging review on this conversation to run a full review.

Annotations

Check warning on line 118 in packages/daemon/src/sessions/session-lifecycle/internal/session-close-script.ts

See this annotation in the file changed.

@apex-sentinel-staging apex-sentinel-staging / Apex Sentinel

minor: Close script failure now drops its actionable hint whenever the inner error has one

A `close --save-script` refused by the no-clobber guard shows the inner `AppError`'s hint instead of the recovery hint this function exists to add; any failure carrying its own `details.hint` (the no-clobber refusal, `script_write_failed`) suppresses it. The spread order in `toOrdinaryCloseSaveScriptFailure` is inverted: `{ hint, ...error.details, retriable: false }` lets `error.details.hint` overwrite `hint`, whereas the base pattern `{ ...error.details, ...overrides }` would keep the override.

### Also raised here

Also raised at this location by persona `maintainability`: Hint now loses to error.details, reversing the previous override order — `toOrdinaryCloseSaveScriptFailure` spreads `error.details` after the literal `hint`, so a wrapped error carrying its own `details.hint` replaces the close-specific guidance about `--force`/`--overwrite`; the previous version applied its overrides last and the hint was always the one shown at close. Put `hint` (or a `hint ?? fallback` resolution) after the spread so the close-path guidance stays unless it is deliberately specialized.

Also raised at this location by persona `senior-developer`: Ordinary-close failure hint is now silently replaced by the inner error's hint — No AppError raised on this path currently sets a hint, so nothing breaks today; if one starts doing so (e.g. the active-publication failure adds one), the close hint for the same target silently changes. Keeping the override on the two keys this function owns (`hint`, `retriable`) states the intent.

Check warning on line 232 in packages/daemon/src/core/session-script-writer.ts

See this annotation in the file changed.

@apex-sentinel-staging apex-sentinel-staging / Apex Sentinel

minor: Ordinary close no longer fails loud: callers that ignore result.error report success

`handleSessionScriptWriteFailure` now returns `{ written:false, error }` instead of throwing, but per-action/active publication in `session-script-publication.ts` and `finalizeRepairTeardown` (non-repair sessions) only inspect `written`, so a no-clobber refusal or fs error is silently recorded as a successful close. No test pins this behavior for those paths.

### Also raised here

Also raised at this location by persona `javascript`: Ordinary script-write failures are now returned, so callers that check only `written` swallow them — `handleSessionScriptWriteFailure` used to rethrow an `AppError` and, for a non-AppError filesystem error, return a bare `{ written: false }`; every non-repair, non-active-publication failure now comes back as `{ written: false, error }` instead of throwing. That is correct only for callers that read `result.error`: `SessionStore.writeSessionLog` has at least one internal call site (packages/daemon/src/core/session-store.ts:307) that I could not confirm inspects `result.error`, and any such caller now turns a refused or failed `.ad` write into a silent skip where the base aborted the caller with a thrown error. The PR description already flags this as a risk on the teardown path.

Check warning on line 123 in packages/daemon/src/server/provider-lease-expiry.ts

See this annotation in the file changed.

@apex-sentinel-staging apex-sentinel-staging / Apex Sentinel

minor: writeJournal now runs detached in a diagnostics scope; its rejection is unhandled

When `withDaemonDiagnosticsScope` rejects (daemon.log open/write fails) or `writeJournal` throws outside its try block, the `void` in `runScoped` leaves the rejection unobserved, aborting the daemon during shutdown. `writeJournal` also mutates `journalRetryTimer` after a fire, racing `shutdown()` if the scope callback defers a microtask. Report the rejection (e.g. `void runScoped(...).catch(...)`) so a diagnostics failure doesn't kill the process.

### Also raised here

Also raised at this location by persona `maintainability`: runScoped swallows the scoped rejection and its result — `runScoped` accepts `() => Promise<void> | void` and discards the promise returned by `withinDiagnosticsScope`, so a rejection from a scheduled journal write or lease retry surfaces as an unhandled rejection rather than a reported failure, and any returned value is thrown away (it type-checks today only because `retryPending` resolves to void). Narrow the parameter to `() => Promise<void>` and attach a catch that reports the failure.

Check warning on line 314 in packages/remote/src/remote-connection-state.ts

See this annotation in the file changed.

@apex-sentinel-staging apex-sentinel-staging / Apex Sentinel

minor: isRemoteConfigReferenced treats an unreadable record as unreferenced, so a secret-bearing profile can be deleted while in use

If a session record is momentarily unreadable, mid-write, or malformed, `isRemoteConfigReferenced` returns `false` and `removeGeneratedRemoteConfigIfUnused` deletes a profile an active session still connects through, causing later commands to fail with a missing-profile error. Treat an unreadable record as a reference or report the failure so the file is kept.

### Also raised here

Also raised at this location by persona `maintainability`: Unreadable records dir reads as "unreferenced" and deletes a used profile — If `fs.readdirSync` on `remote-connections` fails — EMFILE/ENOTEMPTY under load, or EACCES on a state dir with odd permissions — `isRemoteConfigReferenced` returns `false`, which `removeGeneratedRemoteConfigIfUnused` reads as "nobody uses this profile" and deletes it while other sessions still point at it. An unreadable directory is not an empty one: return `true` (or rethrow) from the `catch` so the caller leaves the profile alone, and keep "directory does not exist" as the only case that means "no references".