@@ -4,7 +4,8 @@ export type AttentionRecord = { |
4 | 4 | id: string |
5 | 5 | editspace: string |
6 | 6 | revision: number |
7 | | - status: "open" | "snoozed" | "decided" | "resolved" | "withdrawn" |
| 7 | + status: "open" | "snoozed" | "decided" | "answered" | "resolved" | "withdrawn" |
| 8 | + lane?: string | null |
8 | 9 | kind: "decision" | "blocker" |
9 | 10 | title: string |
10 | 11 | context: string |
@@ -15,26 +16,77 @@ export type AttentionRecord = { |
15 | 16 | gate_key: string |
16 | 17 | source_ref: { session_id: string; directory: string } |
17 | 18 | decision: { choice_id: string; label?: string } | string | null |
18 | | - delivery_state: "not-requested" |
| 19 | + delivery_state: DeliveryState |
| 20 | + answer?: { id: string; text: string; choice_id: string | null; answered_at: number } | null |
| 21 | + delivery?: { |
| 22 | + state: DeliveryState |
| 23 | + reason: string | null |
| 24 | + lane: string | null |
| 25 | + session: string | null |
| 26 | + session_directory?: string | null |
| 27 | + message_id: string | null |
| 28 | + error: string | null |
| 29 | + updated_at: number |
| 30 | + presented_at: string | null |
| 31 | + handled_at: string | null |
| 32 | + copies?: { session: string; directory?: string | null; state: string; error: string | null; presented_at: string | null; handled_at: string | null }[] |
| 33 | + } | null |
19 | 34 | created_at: number |
20 | 35 | updated_at: number |
21 | 36 | snoozed_until?: number | null |
22 | 37 | } |
| 38 | +export type DeliveryState = "not-requested" | "pending" | "blocked" | "queued" | "admitted" | "presented" | "handled" | "closed" |
23 | 39 | export type AttentionList = { |
24 | 40 | records: AttentionRecord[] |
25 | 41 | projects: { editspace: string; available: boolean; error?: string }[] |
26 | 42 | partial: boolean |
27 | 43 | truncated?: boolean |
28 | 44 | } |
29 | 45 | export type AttentionAction = { |
30 | | - action: "choose" | "snooze" | "wake" | "resolve" |
| 46 | + action: "choose" | "answer" | "snooze" | "wake" | "resolve" |
31 | 47 | choice_id?: string |
| 48 | + text?: string |
32 | 49 | snoozed_until?: number |
33 | 50 | reason?: string |
34 | 51 | } |
35 | 52 |
|
36 | 53 | export const attentionPath = (path = "", editspace?: string) => `/es/api/attention${path}${editspace ? `?es=${encodeURIComponent(editspace)}` : ""}` |
37 | 54 |
|
| 55 | +// Evidence wording follows runtime receipts only; status or idleness never advances it. |
| 56 | +export const deliveryLabel = (record: AttentionRecord) => ({ |
| 57 | + "not-requested": "Not sent", |
| 58 | + pending: "Saved · delivery not yet attempted", |
| 59 | + blocked: "Saved · delivery blocked", |
| 60 | + queued: "Saved · mail recorded, admission not confirmed", |
| 61 | + admitted: "Delivered to the agent's input queue · not yet read", |
| 62 | + presented: "Presented to agent · awaiting acknowledgement", |
| 63 | + handled: "Acknowledged by the agent", |
| 64 | + closed: "Not sent · record closed before delivery", |
| 65 | +})[record.delivery_state] ?? `Delivery ${record.delivery_state}` |
| 66 | + |
| 67 | +export const blockedReason = (reason: string | null | undefined) => ({ |
| 68 | + "handoff-pending": "A handoff for this lane is in progress; delivery waits for it to complete.", |
| 69 | + "owner-unverified": "The lane's current owner is not on this request's verified handoff chain. Nothing was sent to it.", |
| 70 | + "no-owner": "The lane has no current agent owner.", |
| 71 | + "source-unverified": "The requesting session no longer exists in its published directory. Nothing was sent.", |
| 72 | + "source-unavailable": "The agent runtime is unreachable; delivery is retried automatically.", |
| 73 | + "source-superseded": "The requesting session was replaced. Nothing is forwarded to its replacement automatically.", |
| 74 | + "handoff-lineage-unavailable": "Handoff records are unreadable right now.", |
| 75 | + "no-actionable-recipient": "The lane's mail subscription is muted for this message.", |
| 76 | +} as Record<string, string>)[reason ?? ""] ?? reason ?? "" |
| 77 | + |
| 78 | +export const answerDraftKey = (record: AttentionRecord) => `es-attention-answer:${record.editspace}:${record.id}` |
| 79 | +// An answer command whose outcome is unknown survives reload with its exact identity and payload. |
| 80 | +const pendingPrefix = "es-attention-pending:" |
| 81 | +type PendingCommand = { record: AttentionRecord; body: AttentionAction & { command_id: string; expected_revision: number } } |
| 82 | +const pendingKey = (record: AttentionRecord) => `${pendingPrefix}${record.editspace}:${record.id}` |
| 83 | +const storedPending = (): PendingCommand[] => Object.keys(localStorage).filter(key => key.startsWith(pendingPrefix)).sort().flatMap(key => { |
| 84 | + try { |
| 85 | + const value = JSON.parse(localStorage.getItem(key) ?? "") |
| 86 | + return value?.record?.id && value?.body?.command_id && value.body.action === "answer" ? [value as PendingCommand] : [] |
| 87 | + } catch { return [] } |
| 88 | +}) |
| 89 | + |
38 | 90 | export const decisionLabel = (record: AttentionRecord) => { |
39 | 91 | const choice = typeof record.decision === "string" ? record.decision : record.decision?.choice_id |
40 | 92 | return record.options.find(option => option.id === choice)?.label ?? (typeof record.decision === "object" ? record.decision?.label : record.decision) ?? "Choice recorded" |
@@ -62,7 +114,7 @@ export const createAttention = (project: () => string | undefined) => { |
62 | 114 | const [error, setError] = createSignal("") |
63 | 115 | const [loading, setLoading] = createSignal(false) |
64 | 116 | const [operator, setOperator] = createSignal(false) |
65 | | - const [pending, setPending] = createSignal<{ record: AttentionRecord; body: AttentionAction & { command_id: string; expected_revision: number } }>() |
| 117 | + const [pending, setPending] = createSignal<PendingCommand>() |
66 | 118 | const [writing, setWriting] = createSignal(false) |
67 | 119 | const [notice, setNotice] = createSignal("") |
68 | 120 | const [now, setNow] = createSignal(Date.now()) |
@@ -110,11 +162,26 @@ export const createAttention = (project: () => string | undefined) => { |
110 | 162 | } |
111 | 163 | const checkOperator = async () => { |
112 | 164 | const response = await fetch(attentionPath("/session"), { credentials: "include" }) |
113 | | - if (response.ok && (await response.json()).operator === true) { setOperator(true); return } |
| 165 | + // An operator cookie minted before written answers existed cannot send them; refresh it. |
| 166 | + if (response.ok) { |
| 167 | + const session = await response.json() |
| 168 | + if (session.operator === true && session.answer !== false) { setOperator(true); return } |
| 169 | + } |
114 | 170 | const bootstrap = await fetch(attentionPath("/session"), { method: "POST", credentials: "include", headers: { "content-type": "application/json", "X-Attention-CSRF": "1" }, body: "{}" }) |
115 | 171 | setOperator(bootstrap.ok && (await bootstrap.json()).operator === true) |
116 | 172 | } |
| 173 | + const settle = (command: PendingCommand) => { |
| 174 | + if (command.body.action === "answer") localStorage.removeItem(pendingKey(command.record)) |
| 175 | + setPending(undefined) |
| 176 | + } |
| 177 | + const resume = () => { |
| 178 | + const stored = storedPending()[0] |
| 179 | + if (!stored || pending()) return |
| 180 | + setPending(stored) |
| 181 | + setNotice("An answer sent before reload has an unknown outcome. Retry the same action identity; no new answer is created.") |
| 182 | + } |
117 | 183 | onMount(() => { |
| 184 | + resume() |
118 | 185 | // Only explicitly allowlisted local UI origins can acquire the operator cookie. |
119 | 186 | void checkOperator().catch(() => { setOperator(false) }) |
120 | 187 | const timer = setInterval(() => { setNow(Date.now()); void refresh() }, 30_000) |
@@ -145,13 +212,19 @@ export const createAttention = (project: () => string | undefined) => { |
145 | 212 | } |
146 | 213 | if (response.status === 403) setOperator(false) |
147 | 214 | setNotice(response.status === 409 ? "This record changed. Review the current revision before choosing again." : `Action refused (HTTP ${response.status}); no local change was assumed.`) |
148 | | - setPending(undefined) |
| 215 | + settle(command) |
149 | 216 | await refresh() |
| 217 | + resume() |
150 | 218 | return false |
151 | 219 | } |
152 | | - setPending(undefined) |
153 | | - setNotice(command.body.action === "choose" ? "Decision saved · not sent. No agent was resumed and no tool was approved." : "Saved to the attention ledger.") |
| 220 | + settle(command) |
| 221 | + // Keep any newer draft typed while this older submission was in flight. |
| 222 | + if (command.body.action === "answer" && localStorage.getItem(answerDraftKey(command.record)) === command.body.text) localStorage.removeItem(answerDraftKey(command.record)) |
| 223 | + setNotice(command.body.action === "choose" ? "Decision saved · not sent. No agent was resumed and no tool was approved." |
| 224 | + : command.body.action === "answer" ? "Answer saved. It is delivered only to the verified current owner of this request; evidence appears under Answered." |
| 225 | + : "Saved to the attention ledger.") |
154 | 226 | await refresh() |
| 227 | + resume() |
155 | 228 | return true |
156 | 229 | } catch { |
157 | 230 | setNotice("Response lost. Outcome unknown; retry the same action identity or refresh before proceeding.") |
@@ -160,10 +233,35 @@ export const createAttention = (project: () => string | undefined) => { |
160 | 233 | } |
161 | 234 | const act = (record: AttentionRecord, action: AttentionAction) => { |
162 | 235 | if (pending() || writing()) return Promise.resolve(false) |
163 | | - setPending({ record, body: { ...action, expected_revision: record.revision, command_id: crypto.randomUUID() } }) |
| 236 | + const command = { record, body: { ...action, expected_revision: record.revision, command_id: crypto.randomUUID() } } |
| 237 | + if (action.action === "answer") { |
| 238 | + // Persist before POST so a lost response can be retried with this identity after reload. |
| 239 | + try { localStorage.setItem(pendingKey(record), JSON.stringify(command)) } catch { |
| 240 | + setNotice("Could not save this answer's identity locally, so it was not sent. Free browser storage and try again.") |
| 241 | + return Promise.resolve(false) |
| 242 | + } |
| 243 | + } |
| 244 | + setPending(command) |
164 | 245 | return retry() |
165 | 246 | } |
| 247 | + // Re-evaluates delivery now; no new answer or revision, so it is safe to repeat. |
| 248 | + const retryDelivery = async (record: AttentionRecord) => { |
| 249 | + if (writing()) return |
| 250 | + setWriting(true) |
| 251 | + try { |
| 252 | + const response = await fetch(attentionPath(`/${encodeURIComponent(record.id)}/delivery`, record.editspace), { |
| 253 | + method: "POST", credentials: "include", headers: { "content-type": "application/json", "X-Attention-CSRF": "1" }, body: "{}", |
| 254 | + }) |
| 255 | + setNotice(response.ok ? "Delivery re-checked." : `Delivery check refused (HTTP ${response.status}). The answer remains saved.`) |
| 256 | + } catch { |
| 257 | + setNotice("Delivery check response lost. The answer remains saved; check again.") |
| 258 | + } finally { |
| 259 | + setWriting(false) |
| 260 | + await refresh() |
| 261 | + } |
| 262 | + } |
166 | 263 | return { |
| 264 | + retryDelivery, |
167 | 265 | snapshot, |
168 | 266 | error, loading, operator, pending, writing, notice, now, refresh, checkOperator, act, retry, |
169 | 267 | stale: () => !snapshot() || !!error() || now() - snapshot()!.checked > 45_000, |
|