@@ -4,7 +4,8 @@ export type AttentionRecord = {
44 id : string
55 editspace : string
66 revision : number
7- status : "open" | "snoozed" | "decided" | "resolved" | "withdrawn"
7+ status : "open" | "snoozed" | "decided" | "answered" | "resolved" | "withdrawn"
8+ lane ?: string | null
89 kind : "decision" | "blocker"
910 title : string
1011 context : string
@@ -15,26 +16,77 @@ export type AttentionRecord = {
1516 gate_key : string
1617 source_ref : { session_id : string ; directory : string }
1718 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
1934 created_at : number
2035 updated_at : number
2136 snoozed_until ?: number | null
2237}
38+ export type DeliveryState = "not-requested" | "pending" | "blocked" | "queued" | "admitted" | "presented" | "handled" | "closed"
2339export type AttentionList = {
2440 records : AttentionRecord [ ]
2541 projects : { editspace : string ; available : boolean ; error ?: string } [ ]
2642 partial : boolean
2743 truncated ?: boolean
2844}
2945export type AttentionAction = {
30- action : "choose" | "snooze" | "wake" | "resolve"
46+ action : "choose" | "answer" | " snooze" | "wake" | "resolve"
3147 choice_id ?: string
48+ text ?: string
3249 snoozed_until ?: number
3350 reason ?: string
3451}
3552
3653export const attentionPath = ( path = "" , editspace ?: string ) => `/es/api/attention${ path } ${ editspace ? `?es=${ encodeURIComponent ( editspace ) } ` : "" } `
3754
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+
3890export const decisionLabel = ( record : AttentionRecord ) => {
3991 const choice = typeof record . decision === "string" ? record . decision : record . decision ?. choice_id
4092 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) => {
62114 const [ error , setError ] = createSignal ( "" )
63115 const [ loading , setLoading ] = createSignal ( false )
64116 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 > ( )
66118 const [ writing , setWriting ] = createSignal ( false )
67119 const [ notice , setNotice ] = createSignal ( "" )
68120 const [ now , setNow ] = createSignal ( Date . now ( ) )
@@ -110,11 +162,26 @@ export const createAttention = (project: () => string | undefined) => {
110162 }
111163 const checkOperator = async ( ) => {
112164 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+ }
114170 const bootstrap = await fetch ( attentionPath ( "/session" ) , { method : "POST" , credentials : "include" , headers : { "content-type" : "application/json" , "X-Attention-CSRF" : "1" } , body : "{}" } )
115171 setOperator ( bootstrap . ok && ( await bootstrap . json ( ) ) . operator === true )
116172 }
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+ }
117183 onMount ( ( ) => {
184+ resume ( )
118185 // Only explicitly allowlisted local UI origins can acquire the operator cookie.
119186 void checkOperator ( ) . catch ( ( ) => { setOperator ( false ) } )
120187 const timer = setInterval ( ( ) => { setNow ( Date . now ( ) ) ; void refresh ( ) } , 30_000 )
@@ -145,13 +212,19 @@ export const createAttention = (project: () => string | undefined) => {
145212 }
146213 if ( response . status === 403 ) setOperator ( false )
147214 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 )
149216 await refresh ( )
217+ resume ( )
150218 return false
151219 }
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." )
154226 await refresh ( )
227+ resume ( )
155228 return true
156229 } catch {
157230 setNotice ( "Response lost. Outcome unknown; retry the same action identity or refresh before proceeding." )
@@ -160,10 +233,35 @@ export const createAttention = (project: () => string | undefined) => {
160233 }
161234 const act = ( record : AttentionRecord , action : AttentionAction ) => {
162235 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 )
164245 return retry ( )
165246 }
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+ }
166263 return {
264+ retryDelivery,
167265 snapshot,
168266 error, loading, operator, pending, writing, notice, now, refresh, checkOperator, act, retry,
169267 stale : ( ) => ! snapshot ( ) || ! ! error ( ) || now ( ) - snapshot ( ) ! . checked > 45_000 ,
0 commit comments