Skip to content

Commit 36de767

Browse files
committed
chore(es-app): integrate tool runs/prompt nav (anomalyco#228) with written attention answers (anomalyco#227)
Merge-only fix: drop import aliases in components/tool-run.tsx (ToolRun data type renamed ToolRunGroup).
2 parents 5c64911 + 53056f1 commit 36de767

8 files changed

Lines changed: 623 additions & 32 deletions

File tree

‎packages/es-app/src/attention.ts‎

Lines changed: 107 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
2339
export type AttentionList = {
2440
records: AttentionRecord[]
2541
projects: { editspace: string; available: boolean; error?: string }[]
2642
partial: boolean
2743
truncated?: boolean
2844
}
2945
export 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

3653
export 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+
3890
export 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,

‎packages/es-app/src/components/tool-run.tsx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { createMemo, For, Show, type JSX } from "solid-js"
2-
import type { Message as MessageInfo, Part, ToolPart } from "@opencode-ai/sdk/v2"
1+
import { createMemo, For, Show, type ComponentProps, type JSX } from "solid-js"
2+
import type { Part, ToolPart } from "@opencode-ai/sdk/v2"
33
import { Message, type ToolRenderers } from "@opencode-ai/session-ui/message-part"
4-
import type { ToolRun as Run } from "../transcript-groups"
4+
import type { ToolRunGroup } from "../transcript-groups"
55
import { SIZE_LEGEND, toolMagnitude } from "../tool-magnitude"
66
import { rangeView } from "./tool-size"
77

88
// Collapsed by default. Members mount only while open, as their original chips.
99
// "Short" describes measured text size only, never safety or cost.
1010
export const ToolRun = (props: {
11-
run: Run
12-
message: (id: string) => MessageInfo | undefined
11+
run: ToolRunGroup
12+
message: (id: string) => ComponentProps<typeof Message>["message"] | undefined
1313
parts: (messageID: string) => readonly Part[]
1414
open: boolean
1515
onToggle: (open: boolean) => void

‎packages/es-app/src/pages/home.css‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@
5555
.home-confirm { position: fixed; margin: auto; inline-size: min(550px, calc(100vw - 40px)); padding: 24px; background: var(--surface0); color: var(--text); border: 1px solid var(--peach); }
5656
.home-confirm::backdrop { background: #000a; }
5757
@media (max-width: 650px) { .attention-home { padding-inline: 14px; } .home-agent, .home-request { grid-template-columns: minmax(0,1fr); gap: 9px; } .home-section { gap: 4px; } .home-tabs button { padding: 7px 8px; } .home-header-end { display: none; } }
58+
.home-answer { margin-block-start: 14px; display: grid; gap: 6px; max-width: 90ch; }
59+
.home-answer > label { font-size: 11px; color: var(--subtext0); cursor: default; }
60+
.home-answer textarea { inline-size: 100%; box-sizing: border-box; min-block-size: 64px; resize: vertical; padding: 8px 10px; font: inherit; line-height: 1.6; color: var(--text); background: var(--base); border: 1px solid var(--surface1); border-radius: 0; text-align: start; }
61+
.home-answer textarea:focus-visible { outline: 2px solid var(--lavender); outline-offset: 1px; }
62+
.home-answer .home-actions { margin-block: 2px 0; }
63+
.attention-home pre.home-answer-text { font: inherit; font-size: 13px; line-height: 1.6; padding: 8px 10px; margin-block: 6px; background: var(--base); border-inline-start: 2px solid var(--green); text-align: start; }
64+
.home-blocked { color: var(--yellow); }
65+
.home-copies { margin: 4px 0; padding-inline-start: 18px; font-size: 11px; color: var(--subtext0); }

0 commit comments

Comments
 (0)