(req: DaemonRequest)
| 91 | const { sessionStore, leaseRegistry } = deps; |
| 92 | |
| 93 | async function handleRequest(req: DaemonRequest): Promise<DaemonResponse> { |
| 94 | const start = Date.now(); |
| 95 | const debug = Boolean(req.meta?.debug || req.flags?.verbose); |
| 96 | return await withDiagnosticsScope( |
| 97 | { |
| 98 | session: req.session, |
| 99 | requestId: req.meta?.requestId, |
| 100 | command: req.command, |
| 101 | debug, |
| 102 | logPath, |
| 103 | }, |
| 104 | async () => { |
| 105 | const response = await runRequestWithinScope(req); |
| 106 | // Phase 2 (typed errors) graft: enrich error responses with additive, |
| 107 | // machine-readable signals — `supportedOn` for platform mismatches and |
| 108 | // `retriable` for transient failures — so an agent self-corrects without a |
| 109 | // wasted round-trip. Returned unchanged when neither applies, so the |
| 110 | // default error wire shape is preserved. |
| 111 | if (!response.ok) { |
| 112 | return { ok: false, error: enrichDaemonError(req.command, response.error) }; |
| 113 | } |
| 114 | // Phase 4 (agent-cost) grafts on the success path. Runs inside the |
| 115 | // diagnostics scope so cost can read this request's runner-round-trip tally. |
| 116 | return applyAgentCostGrafts(req, response, start); |
| 117 | }, |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | async function runRequestWithinScope(req: DaemonRequest): Promise<DaemonResponse> { |
| 122 | if (!timingSafeStringEqual(req.token, token)) { |
no test coverage detected