|
1 | 1 | import { |
2 | 2 | AppError, |
3 | | - type DispatchDisclosure, |
| 3 | + discloseDispatch, |
4 | 4 | discloseUnclassifiedDispatch, |
5 | 5 | } from '@agent-device/kernel/errors'; |
6 | 6 | import { resolveCommandRecordingEffect } from '@agent-device/command-registry/registry'; |
7 | 7 | import type { DaemonRequest, DaemonResponse } from '../../daemon-request.ts'; |
8 | 8 |
|
9 | 9 | /** |
10 | | - * The verdict for an interaction failure no producer classified. A request the registry declares |
11 | | - * read-only (`recordingEffect: 'observes-app'`) never dispatches a mutation, so it is `no`. |
12 | | - * Otherwise `unknown`: only a producer that refuses before dispatch may say `no`, and only one that |
13 | | - * proved execution may say `yes`, and this layer cannot tell either from where the failure surfaced. |
14 | | - * A producer's own verdict is always kept. |
| 10 | + * The daemon's verdict around interaction dispatch. A request the registry declares read-only |
| 11 | + * (`recordingEffect: 'observes-app'`) is `no` over any producer verdict: a read has no side effect, |
| 12 | + * so it is always safe to resend. Otherwise `unknown` fills only a failure no producer classified. |
15 | 13 | */ |
16 | | -export async function discloseUnclassifiedInteractionDispatch( |
| 14 | +export async function discloseInteractionDispatch( |
17 | 15 | req: DaemonRequest, |
18 | 16 | dispatch: () => Promise<DaemonResponse | null>, |
19 | 17 | ): Promise<DaemonResponse | null> { |
20 | | - const verdict: DispatchDisclosure = |
21 | | - resolveCommandRecordingEffect(req) === 'observes-app' ? 'no' : 'unknown'; |
| 18 | + const readOnly = resolveCommandRecordingEffect(req) === 'observes-app'; |
22 | 19 | try { |
23 | 20 | const response = await dispatch(); |
24 | | - if (!response || response.ok || response.error.details?.dispatched !== undefined) { |
25 | | - return response; |
26 | | - } |
| 21 | + if (!response || response.ok) return response; |
| 22 | + const producerVerdict = response.error.details?.dispatched; |
| 23 | + const dispatched = readOnly ? 'no' : (producerVerdict ?? 'unknown'); |
| 24 | + if (dispatched === producerVerdict) return response; |
27 | 25 | return { |
28 | 26 | ok: false, |
29 | | - error: { |
30 | | - ...response.error, |
31 | | - details: { ...response.error.details, dispatched: verdict }, |
32 | | - }, |
| 27 | + error: { ...response.error, details: { ...response.error.details, dispatched } }, |
33 | 28 | }; |
34 | 29 | } catch (error) { |
35 | | - if (error instanceof AppError) throw discloseUnclassifiedDispatch(error, verdict); |
36 | | - throw error; |
| 30 | + if (!(error instanceof AppError)) throw error; |
| 31 | + throw readOnly ? discloseDispatch(error, 'no') : discloseUnclassifiedDispatch(error, 'unknown'); |
37 | 32 | } |
38 | 33 | } |
39 | 34 |
|
|
0 commit comments