2026-11-26 08:44:15 +00:00
let inboxDoneOpen = false ;
2026-11-14 21:21:07 +00:00
const GONE_NOTICE = "already done or removed elsewhere" ;
2026-11-26 08:44:15 +00:00
const doneItem = ( item ) => Boolean ( item . held || ( item . job && ! jobOpen ( item . job )));
2026-11-28 01:47:41 +00:00
const REQUEST_CHIPS = [[ "open" , "Open" , "bi-play-circle" ], [ "expired" , "Expired" , "bi-hourglass-bottom" ], [ "stale" , "Stale" , "bi-exclamation-triangle" ], [ "failed" , "Failed" , "bi-x-octagon" ], [ "all" , "All" , "bi-list-check" ]];
const REQUEST_TONES = { open : "info" , expired : "neutral" , stale : "warn" , failed : "bad" };
const TRIAGE_FOOTER = "footer" ;
const triage = { chip : "all" , picked : new Map (), footer : false };
/**
* @param {Object} item a request of requestItems().
* @returns {boolean} whether its hook stopped waiting, so the hub takes no answer for it: an expired ask or decision,
* or one whose dialog is open in Claude (claude), which counts as open.
*/
const dialogLapsed = ( item ) => item . request === "expired" || Boolean ( item . claude );
/**
* @returns {Object[]} every request the snapshot lists: the inbox and the lapsed asks and decisions, each with its
* request kind (open, expired, stale or failed); one open in Claude is open; a decision row already answered is none.
*/
const requestItems = () => [... state . inbox , ...( state . lapsed || [])]. filter (( item ) => ! item . answered );
const requestsOf = ( chip ) => requestItems (). filter (( item ) => chip === "all" || item . request === chip );
const listedInbox = () => requestsOf ( triage . chip ). filter (( item ) => ui . snoozed || ! item . until );
/**
* @param {string} scope the chip the items are listed under, or TRIAGE_FOOTER for the Stream's stale requests.
* @param {Object[]} items the requests listed there.
* @returns {HTMLElement} the select-all checkbox and the "Delete N" button over items. The tick keeps the keys it saw,
* so a request that arrives later is never deleted with them; the box shows indeterminate until it is ticked again.
*/
function triagePicker ( scope , items ) {
const keys = items . map ( keyOf );
const picked = ( triage . picked . get ( scope ) || []). filter (( key ) => keys . includes ( key ));
const chosen = items . filter (( item ) => picked . includes ( keyOf ( item )));
const redraw = () => ( scope === TRIAGE_FOOTER ? drawStream () : drawInbox ());
const all = h ( "input" , { type : "checkbox" , class : "triage-all" , checked : Boolean ( keys . length ) && picked . length === keys . length , disabled : ! keys . length || ui . busy , ... named ( "Select all" , `Select all ${ keys . length } listed here` ), on : { change : ( event ) => {
triage . picked . set ( scope , event . target . checked ? keys : []);
redraw ();
}}});
all . indeterminate = picked . length > 0 && picked . length < keys . length ;
const hint = `Delete ${ chosen . length } : dismissed in the hub for every browser, no agent gets a message; one passkey signature, then ${ undoWords () } ` ;
const remove = h ( "button" , { type : "button" , class : "btn small danger triage-delete" , disabled : ! chosen . length || ui . busy , ... named ( `Delete ${ chosen . length } ` , hint ), on : { click : () => deleteRequests ( scope , chosen )}}, icon ( "bi-trash" ), `Delete ${ chosen . length } ` );
return h ( "div" , { class : "triage-pick" , "data-scope" : scope }, h ( "label" , { class : "check" }, all , "Select all" ), remove );
}
/**
* @param {string} scope the chip the items were picked under, or TRIAGE_FOOTER; the hub refuses a request of another
* kind than the chip, so an open one is deleted only from Open or All.
* @param {Object[]} items the requests to dismiss.
* @returns {Promise<boolean>} whether the hub took the dismissal; its undo toast then runs, every browser drops them at
* its next poll and the undo brings them back.
*/
async function deleteRequests ( scope , items ) {
const entries = items . map (( item ) => ({ kind : "dismiss" , target : item . kind , id : item . id , scope : scope === TRIAGE_FOOTER ? item . request : scope }));
const label = `Delete ${ items . length } ${ items . length === 1 ? "request" : "requests" } ` ;
const names = new Map ( items . map (( item ) => [ item . id , short ( item . title )]));
ui . busy = true ;
const signed = await proofFor ( entries , label , ( result ) => namedRefusal ( result , label , names ));
if ( signed === null ) {
ui . busy = false ;
await load ();
return false ;
}
const result = await sendJson ( "/api/requests/dismiss" , { items : entries , proof : signed . proof });
ui . busy = false ;
if ( ! result . ok || ! result . data ) {
if ( ! namedRefusal ( result , label , names )) {
toast ( ` ${ label } : not possible` , fail ( result ), false );
}
await load ();
return false ;
}
triage . picked . delete ( scope );
known . batches . add ( result . data . id );
undoToast ( result . data , label );
await load ();
return true ;
}
/**
* @returns {HTMLElement} the request chips of Tasks → Operator, Open · Expired · Stale · Failed · All, each with its
* count and listing exactly its kind, and the select all and Delete N of the chosen chip.
*/
function triageBar () {
const chips = REQUEST_CHIPS . map (([ id , label , name ]) => {
const total = requestsOf ( id ). length ;
return h ( "button" , { type : "button" , class : "chip" , "data-request" : id , "aria-pressed" : String ( triage . chip === id ), ... named ( ` ${ label } ${ total } ` , ` ${ label } : ${ total } requests` ), on : { click : () => {
triage . chip = id ;
drawInbox ();
}}}, icon ( name ), label , counter ( total ));
});
return h ( "div" , { class : "triage-bar" }, h ( "div" , { class : "chips" , role : "group" , "aria-label" : "Request kinds" }, chips ), triagePicker ( triage . chip , requestsOf ( triage . chip )));
}
2026-11-14 21:21:07 +00:00
function closeGone ( result , keys , label ) {
if ( result . status !== 404 || ! keys . length ) {
return false ;
}
state . inbox = state . inbox . filter (( item ) => ! keys . includes ( keyOf ( item )));
toast ( ` ${ label } : ${ GONE_NOTICE } ` , [], true );
render ( false );
drawStream ();
return true ;
}
async function reloadGone ( missing , keys , label ) {
await load ();
if ( missing === null ) {
return ;
}
const left = new Set ( state . inbox . map ( keyOf ));
if ( keys . some (( key ) => ! left . has ( key ))) {
toast ( ` ${ label } : ${ GONE_NOTICE } ` , [], true );
} else {
toast ( ` ${ label } : not possible` , fail ( missing ), false );
}
}
2026-11-24 10:39:44 +00:00
const sending = new Set ();
2026-11-27 20:53:57 +00:00
const refusedIds = new Map ();
2026-11-24 10:39:44 +00:00
2026-11-27 20:53:57 +00:00
/**
* @param {object} result a hub answer; a 409 carries {id, status} of the item it refused.
* @param {string} label the batch's label.
* @param {Map<string, string>|undefined} names item id to the title the toast shows instead of the id.
* @returns {boolean} whether it was such a refusal: kept in refusedIds and toasted by title.
*/
function namedRefusal ( result , label , names ) {
const id = result . status === 409 && result . data && typeof result . data . id === "string" ? result . data . id : "" ;
if ( ! id ) {
return false ;
}
const status = typeof result . data . status === "string" ? result . data . status : "refused" ;
refusedIds . set ( id , status );
2026-11-28 01:47:41 +00:00
toast ( ` ${ label } : not possible` , [ ` ${ ( names && names . get ( id )) || id } : ${ status } ` ], false );
2026-11-27 20:53:57 +00:00
return true ;
}
async function submit ( entries , label , names ) {
2026-11-14 21:21:07 +00:00
const keys = entries . map ( keyOf );
2026-11-24 10:39:44 +00:00
keys . forEach (( key ) => sending . add ( key ));
drawInbox ();
try {
2026-11-27 20:53:57 +00:00
return await sendBatch ( entries , label , keys , names );
2026-11-24 10:39:44 +00:00
} finally {
keys . forEach (( key ) => sending . delete ( key ));
drawInbox ();
}
}
2026-11-27 20:53:57 +00:00
async function sendBatch ( entries , label , keys , names ) {
2026-11-24 10:39:44 +00:00
ui . busy = true ;
2026-11-14 21:21:07 +00:00
let missing = null ;
const gone = ( result ) => {
2026-11-27 20:53:57 +00:00
if ( namedRefusal ( result , label , names )) {
return true ;
}
2026-11-14 21:21:07 +00:00
if ( keys . length > 1 && result . status === 404 ) {
missing = result ;
return true ;
}
return closeGone ( result , keys , label );
};
const signed = await proofFor ( entries , label , gone );
2026-11-06 08:28:04 +00:00
if ( signed === null ) {
ui . busy = false ;
2026-11-14 21:21:07 +00:00
await reloadGone ( missing , keys , label );
2026-11-06 08:28:04 +00:00
return false ;
}
const result = await sendJson ( "/api/inbox/approve" , { items : entries , proof : signed . proof });
ui . busy = false ;
if ( ! result . ok || ! result . data ) {
2026-11-14 21:21:07 +00:00
if ( ! gone ( result )) {
toast ( ` ${ label } : not possible` , fail ( result ), false );
}
await reloadGone ( missing , keys , label );
2026-11-06 08:28:04 +00:00
return false ;
}
known . batches . add ( result . data . id );
ui . selected . clear ();
undoToast ( result . data , label );
await load ();
return true ;
}
2026-11-26 08:44:15 +00:00
/**
* @returns {string} what happens after a signature with the undo window the operator set: "3 s remain to undo",
* or "it takes effect at once" for 0.
*/
const undoWords = () => ( settings . undo > 0 ? ` ${ settings . undo } s remain to undo` : "it takes effect at once" );
2026-11-28 01:47:41 +00:00
const undoView = { box : null , live : new Map ()};
/**
* @param {Object} entry a running undo window: {id, label, due, undo, left}.
* @returns {HTMLElement} its Undo entry for the Actions menu of the Tasks toolbar, with the seconds left; entry.left
* keeps that readout so the toast's timer ticks both.
*/
function undoItem ( entry ) {
entry . left = h ( "span" , { class : "undo-left" }, ` ${ Math . max ( 0 , Math . ceil ( entry . due - nowSeconds ())) } s` );
return h ( "button" , { type : "button" , class : "btn small ghost" , "data-key" : `undo: ${ entry . id } ` , ... named ( `Undo: ${ entry . label } ` , `Undo ${ entry . label } : possible until its undo window ends` ), on : { click : entry . undo }},
icon ( "bi-arrow-counterclockwise" ), h ( "span" , { class : "tool-word" }, "Undo" ), h ( "span" , { class : "undo-label" }, short ( entry . label )), entry . left );
}
function drawUndo () {
if ( ! undoView . box ) {
return ;
}
const entries = [... undoView . live . values ()];
fadeSwap ( undoView . box , entries . length ? entries . map ( undoItem ) : [ h ( "p" , { class : "muted" , "data-key" : "undo:none" }, "Nothing to undo" )]);
}
2026-11-26 08:44:15 +00:00
2026-11-06 08:28:04 +00:00
function undoToast ( batch , label ) {
2026-11-26 08:44:15 +00:00
if ( ! ( batch . due > nowSeconds ())) {
toast ( label , [ "Done: no undo window is set." ], true );
load ();
return ;
}
2026-11-06 08:28:04 +00:00
const left = h ( "span" , { class : "muted" });
2026-11-08 17:26:51 +00:00
const button = h ( "button" , { type : "button" , class : "btn small" , ... named ( "Undo" )}, icon ( "bi-arrow-counterclockwise" ), "Undo" );
2026-11-28 01:47:41 +00:00
const glass = h ( "span" , { class : "undo-glass" , "aria-hidden" : "true" }, glassFace ());
glass . style . animationDuration = ` ${ Math . max ( 0 , batch . due - nowSeconds ()) } s` ;
const box = h ( "div" , { class : "toast wait" , role : "status" }, h ( "strong" , {}, label ), h ( "div" , { class : "undo-row" }, left , h ( "span" , { class : "spacer" }), button , glass ));
2026-11-06 08:28:04 +00:00
let timer = 0 ;
2026-11-28 01:47:41 +00:00
const entry = { id : batch . id , label , due : batch . due , undo : null , left : null };
const settle = () => {
window . clearInterval ( timer );
undoView . live . delete ( batch . id );
drawUndo ();
};
2026-11-06 08:28:04 +00:00
const count = () => {
const rest = Math . ceil ( batch . due - nowSeconds ());
if ( rest <= 0 ) {
2026-11-28 01:47:41 +00:00
settle ();
vanish ( box );
2026-11-06 08:28:04 +00:00
load ();
return ;
}
2026-11-08 17:26:51 +00:00
left . textContent = `takes effect in ${ rest } s` ;
2026-11-28 01:47:41 +00:00
if ( entry . left ) {
entry . left . textContent = ` ${ rest } s` ;
}
2026-11-06 08:28:04 +00:00
};
2026-11-28 01:47:41 +00:00
entry . undo = async () => {
settle ();
2026-11-06 08:28:04 +00:00
button . disabled = true ;
const result = await send ( `/api/inbox/undo/ ${ encodeURIComponent ( batch . id ) } ` , "" , "application/json" );
2026-11-28 01:47:41 +00:00
vanish ( box );
2026-11-08 17:26:51 +00:00
toast ( result . ok ? `Undone: ${ label } ` : "Too late: already in effect" , result . ok ? [] : [ ` ${ result . status } ${ result . text } ` . trim ()], result . ok );
2026-11-06 08:28:04 +00:00
await load ();
2026-11-28 01:47:41 +00:00
};
timer = window . setInterval ( count , 250 );
button . addEventListener ( "click" , entry . undo );
undoView . live . set ( batch . id , entry );
2026-11-06 08:28:04 +00:00
document . getElementById ( "toasts" ). append ( box );
2026-11-28 01:47:41 +00:00
drawUndo ();
2026-11-06 08:28:04 +00:00
count ();
}
function approveOne ( item ) {
2026-11-17 19:09:46 +00:00
if ( collecting ( item )) {
collect ( item );
return null ;
}
2026-11-07 18:14:21 +00:00
if ( item . kind === "rule" ) {
return acceptRule ( item );
}
2026-11-06 08:28:04 +00:00
if ( item . kind === "decision" ) {
if ( ! item . recommended ) {
openItem ( item );
return null ;
}
2026-11-22 15:02:23 +00:00
return submit ([ entryOf ( item , "approve" , item . recommended )], `Choice: ${ shownPick ( item . recommended ) } ` );
2026-11-06 08:28:04 +00:00
}
2026-11-25 01:22:03 +00:00
return withGrantNotes ( item , submit ([ entryOf ( item , "approve" , "" )], ` ${ item . kind === "operator" ? "Run" : "Grant" } : ${ short ( item . title ) } ` ));
}
/**
* @param {Object} item the granted inbox item.
* @param {Promise<boolean>|boolean} granted whether its grant went through.
* @returns {Promise<boolean>} granted; once it holds, the item's line comments go to its requester as one post and
* are forgotten.
*/
async function withGrantNotes ( item , granted ) {
const done = await granted ;
const notes = lineNotes ( keyOf ( item ));
if ( ! done || ! notes ) {
return done ;
}
if ( ! item . to ) {
toast ( "Line comments kept" , [ "The card names no agent to post them to." ], false );
} else if ( await radioSend ({ to : item . to , text : `Granted: ${ item . title } . Notes:\n ${ notes } ` , upload : "" , cls : "post" , voice : "" })) {
setLineMarks ( keyOf ( item ), []);
}
return done ;
2026-11-06 08:28:04 +00:00
}
2026-11-22 15:55:43 +00:00
const chooseOne = ( item , label ) => ( picking ( item ) ? collectPick ( item , label ) : submit ([ entryOf ( item , "approve" , label )], `Choice: ${ shownPick ( label ) } ` ));
const answerPick = ( item , label ) => ( picking ( item ) ? collectPick ( item , label ) : answerRow ( item , label ));
2026-11-25 01:22:03 +00:00
const grantSolo = ( item ) => ( item . kind === "rule" ? acceptRule ( item ) : withGrantNotes ( item , submit ([ entryOf ( item , "approve" , "" )], `Grant: ${ short ( item . title ) } ` )));
2026-11-18 19:28:37 +00:00
const asked = ( item ) => item . kind === "operator" && item . check === "decision" ;
async function answerRow ( item , answer ) {
if ( ! item . agent ) {
toast ( "Answer: no agent to send it to" , [ "The row names no adding agent; answer in its session." ], false );
return ;
}
2026-11-24 10:39:44 +00:00
if ( await submit ([ entryOf ( item , "approve" , answer )], `Answer: ${ shownPick ( answer ) } ` )) {
forgetDraft ( item . id );
2026-11-18 19:28:37 +00:00
}
}
2026-11-24 15:42:06 +00:00
const signsFor = ( item ) => ` ${ item . agent } gets this answer as a call once your passkey signs it` ;
2026-11-18 19:28:37 +00:00
2026-11-22 15:02:23 +00:00
/**
2026-11-22 15:10:26 +00:00
* @param {Object} item an inbox item.
* @param {Function} after called before a picked answer is sent.
2026-11-25 01:22:03 +00:00
* @returns {HTMLElement|Array|null} the choiceDialog() of a decision or a decision row with named options, with its recipient line for a row; only
* that line for a Yes/No row and null for a permission ask or a Yes/No decision, which booleanActions() answers; null for every other item.
2026-11-22 15:02:23 +00:00
*/
const choiceOf = ( item , after ) => {
2026-11-25 01:22:03 +00:00
if ( Answers . boolean ( item )) {
return asked ( item ) ? answerTo ( item ) : null ;
}
2026-11-22 15:02:23 +00:00
if ( item . kind === "decision" ) {
2026-11-22 23:28:53 +00:00
return choiceDialog ( item , { pick : ( label ) => { after (); chooseOne ( item , label ); }, toggled : new Set (), chosen : ui . selected . has ( keyOf ( item )) ? grantLabel ( item ) : "" , locked : item . held , hint : signsFor ( item ), group : "" , other : OTHER_PROMPT });
2026-11-22 15:20:11 +00:00
}
2026-11-24 10:39:44 +00:00
const shown = ui . selected . has ( keyOf ( item )) ? grantLabel ( item ) : "" ;
return asked ( item ) ? [ answerTo ( item ), choiceDialog ( item , { pick : ( label ) => { after (); answerPick ( item , label ); }, toggled : new Set (), chosen : shown , locked : item . held , hint : signsFor ( item ), group : "" , other : OTHER_PROMPT })] : null ;
2026-11-22 15:02:23 +00:00
};
2026-11-18 19:28:37 +00:00
/**
* @param {Object} item a decision row of the register.
* @returns {HTMLElement} who receives the answer: the row's first agent with role A, which any agent can set, so the operator sees it before signing.
*/
2026-11-24 15:42:06 +00:00
const UNASSIGNED = "No agent holds role A on this row: answer in its session." ;
const answerTo = ( item ) => h ( "p" , { class : "muted answer-to" }, item . agent ? [ "Your answer goes to " , h ( "strong" , {}, revealed ( item . agent )), " as a passkey-signed call that wakes it at once, with the row id and your answer only." ] : UNASSIGNED );
2026-11-06 08:28:04 +00:00
2026-11-17 19:09:46 +00:00
const deniable = ( item ) => item . deniable || item . kind === "review" ;
2026-11-25 01:22:03 +00:00
const withNotes = ( item , reason ) => [ reason , lineNotes ( keyOf ( item ))]. filter ( Boolean ). join ( LINE_JOIN ). slice ( 0 , DENY_REASON );
const denialOf = ( item , reason ) => ({... entryOf ( item , "reject" , "" ), channel : lineNotes ( keyOf ( item )) ? "call" : "post" , reason : withNotes ( item , reason ), to : item . to });
2026-11-22 15:55:43 +00:00
const pickable = ( item ) => ! item . held && ( collectable ( item ) || deniable ( item ));
2026-11-11 08:32:36 +00:00
2026-11-22 16:41:45 +00:00
const unsettled = ( item ) => item . reviews . filter (( review ) => review . status !== "approved" );
/**
* @param {Object[]} items the entries to grant.
* @returns {Object[]} items after the pending reviews that their commit rows verify and that are not among them yet.
*/
function withReviews ( items ) {
const keys = new Set ( items . map ( keyOf ));
const linked = new Set ( items . flatMap (( item ) => unsettled ( item ). map (( review ) => review . id )));
return [... state . inbox . filter (( entry ) => entry . kind === "review" && linked . has ( entry . id ) && ! entry . held && ! keys . has ( keyOf ( entry ))), ... items ];
}
2026-11-26 08:44:15 +00:00
const denyNote = () => `Every entry and every full command that gets denied; each requester gets its denial and the reason by post. After the click ${ undoWords () } , and one passkey signature covers the whole selection.` ;
2026-11-22 16:41:45 +00:00
function grantNote ( alone , batched ) {
const solos = alone ? ` Each rule and each red-gate review first asks its own signature, one after the other ( ${ alone } ).` : "" ;
const rest = batched ? ` ${ alone ? "Then the" : "The" } passkey signs the rest once, commit rows settling after the reviews they verify.` : "" ;
2026-11-26 08:44:15 +00:00
return `Every entry and every full command that gets granted. ${ solos }${ rest } After each signature ${ undoWords () } .` ;
2026-11-22 16:41:45 +00:00
}
2026-11-11 08:32:36 +00:00
function bulk ( items , label , deny ) {
2026-11-22 15:55:43 +00:00
const usable = items . filter ( deny ? deniable : batchable );
const alone = deny ? [] : items . filter ( solo );
if ( ! usable . length && ! alone . length ) {
2026-11-11 08:32:36 +00:00
toast ( ` ${ label } : nothing to ${ deny ? "deny" : "grant" } ` , [], false );
2026-11-06 08:28:04 +00:00
return ;
}
2026-11-23 11:02:52 +00:00
const labels = new Map ([... usable , ... alone ]. map (( item ) => [ keyOf ( item ), deny ? "" : grantLabel ( item )]));
2026-11-22 15:55:43 +00:00
const rows = [... usable , ... alone ]. map (( item ) => h ( "li" , {},
2026-11-23 11:02:52 +00:00
h ( "div" , { class : "row" }, pill ( ITEM_LABELS [ item . kind ], KIND_TONES [ item . kind ]), labels . get ( keyOf ( item )) ? h ( "span" , { class : "muted" }, revealed ( `→ ${ shownPick ( labels . get ( keyOf ( item ))) } ` )) : null ),
2026-11-13 18:14:17 +00:00
h ( "strong" , {}, revealed ( item . title )),
item . kind === "operator" ? h ( "div" , { class : "command" }, h ( "code" , {}, revealed ( item . command ))) : null ,
item . kind === "operator" ? h ( "span" , { class : "muted" }, revealed ( `in ${ item . directory } ` )) : null ,
item . kind === "ask" ? h ( "div" , { class : "command" }, h ( "code" , {}, revealed ( item . command ))) : null ));
2026-11-22 15:55:43 +00:00
const confirm = h ( "button" , { type : "button" , class : deny ? "btn danger" : "btn primary" }, ` ${ deny ? "Deny" : "Grant" } ${ usable . length + alone . length } ` );
2026-11-25 01:22:03 +00:00
const reason = deny ? h ( "textarea" , { class : "field" , maxlength : DENY_REASON , rows : 3 , placeholder : "e.g. make the gate green first" , "data-skills" : "" }) : null ;
2026-11-22 15:55:43 +00:00
confirm . addEventListener ( "click" , async () => {
2026-11-06 08:28:04 +00:00
closeSheet ();
2026-11-22 15:55:43 +00:00
for ( const item of alone ) {
if ( ! ( await grantSolo ( item ))) {
return ;
}
}
2026-11-25 01:22:03 +00:00
if ( usable . length && ( await submit ( usable . map (( item ) => ( deny ? denialOf ( item , reason . value ) : entryOf ( item , "approve" , labels . get ( keyOf ( item ))))), ` ${ label } : ${ usable . length } entries` ))) {
for ( const item of usable ) {
if ( deny && lineNotes ( keyOf ( item ))) {
setLineMarks ( keyOf ( item ), []);
} else if ( ! deny ) {
await withGrantNotes ( item , true );
}
}
2026-11-22 16:41:45 +00:00
}
2026-11-06 08:28:04 +00:00
});
2026-11-26 08:44:15 +00:00
openSheet ( label , [ h ( "p" , { class : "muted" }, deny ? denyNote () : grantNote ( alone . length , usable . length )), h ( "ul" , { class : "mini" }, rows ), ...( deny ? [ reasonChips ( reason ), h ( "label" , { class : "deny-reason" }, "Reason (optional, every requester reads it)" , reason )] : []),
2026-11-17 19:09:46 +00:00
h ( "div" , { class : "actions" }, h ( "span" , { class : "spacer" }), btn ( "Cancel" , "btn ghost" , closeSheet ), confirm )], false );
( reason || confirm ). focus ();
}
2026-11-18 19:28:37 +00:00
/**
* @returns {object} the picked items as chosen, and the grant, deny and grant-all-green controls of the Grant menu.
*/
function selectionActions ( items ) {
2026-11-17 19:09:46 +00:00
const green = items . filter ( isGreen );
const chosen = items . filter (( item ) => ui . selected . has ( keyOf ( item )));
const denials = chosen . filter ( deniable );
2026-11-22 15:55:43 +00:00
const grantable = chosen . filter ( collectable );
2026-11-18 19:28:37 +00:00
return {
chosen ,
2026-11-22 16:41:45 +00:00
grant : h ( "button" , { type : "button" , class : "btn small primary" , ... named ( `Grant selection ( ${ grantable . length } )` ), disabled : ! grantable . length || ui . busy , on : { click : () => bulk ( withReviews ( chosen ), "Grant selection" , false )}}, icon ( "bi-check2-square" ), `Grant ${ grantable . length } ` ),
2026-11-26 08:44:15 +00:00
deny : h ( "button" , { type : "button" , class : "menu-item danger" , ... named ( `Deny selection ( ${ denials . length } )` , `Deny selection ( ${ denials . length } ): each requester gets its denial and an optional reason by post, one passkey signature, then ${ undoWords () } ` ), disabled : ! denials . length || ui . busy , on : { click : () => bulk ( chosen , "Deny selection" , true )}}, icon ( "bi-x-square" ), `Deny ${ denials . length } ` ),
2026-11-18 19:28:37 +00:00
green : h ( "button" , { type : "button" , class : "menu-item" , ... named ( `Grant all green ( ${ green . length } )` ), disabled : ! green . length || ui . busy , on : { click : () => bulk ( green , "Grant all green" , false )}}, icon ( "bi-check2-all" ), `Grant all green ${ green . length } ` ),
};
2026-11-06 08:28:04 +00:00
}
async function mark ( item , until , pinned ) {
const result = await sendJson ( "/api/inbox/mark" , { kind : item . kind , id : item . id , until , pinned });
if ( ! result . ok ) {
2026-11-08 17:26:51 +00:00
toast ( "Marking failed" , [ ` ${ result . status } ${ result . text } ` . trim ()], false );
2026-11-06 08:28:04 +00:00
}
await load ();
}
function tomorrow () {
const date = new Date ();
date . setDate ( date . getDate () + 1 );
date . setHours ( 8 , 0 , 0 , 0 );
return date . getTime () / 1000 ;
}
2026-11-26 08:44:15 +00:00
function toggle ( item ) {
2026-11-11 08:32:36 +00:00
if ( ! item || ! pickable ( item )) {
2026-11-06 08:28:04 +00:00
return ;
}
const key = keyOf ( item );
2026-11-22 15:55:43 +00:00
flip ( key );
2026-11-28 01:47:41 +00:00
ui . cursor = Math . max ( listedInbox (). findIndex (( entry ) => keyOf ( entry ) === key ), 0 );
2026-11-06 08:28:04 +00:00
drawInbox ();
2026-11-18 19:28:37 +00:00
drawGrant ();
2026-11-06 08:28:04 +00:00
}
2026-11-22 16:41:45 +00:00
function reviewsPill ( item ) {
if ( ! item . reviews . length ) {
return null ;
}
const waiting = unsettled ( item ). length ;
return pill ( `reviews ${ item . reviews . length - waiting } / ${ item . reviews . length } approved` , waiting ? "neutral" : "ok" );
}
function runButton ( item ) {
const waiting = unsettled ( item );
if ( ! waiting . length ) {
return iconBtn ( "bi-play-fill" , "Run" , "btn small primary" , () => approveOne ( item ));
}
const stuck = waiting . some (( review ) => review . status !== "pending" );
const why = stuck
? "A review its commit verifies is rejected or unknown to this hub: the run would fail at its verify step"
: `Approve the ${ waiting . length } reviews its commit verifies, then run it: each red-gate review asks its own signature, the rest goes with the row in one` ;
return h ( "button" , { type : "button" , class : "btn small primary" , ... named ( `Approve ${ waiting . length } and run` , why ), disabled : stuck || ui . busy , on : { click : () => ( collecting ( item ) ? collect ( item ) : bulk ( withReviews ([ item ]), "Approve and run" , false ))}}, icon ( "bi-play-fill" ), `Approve ${ waiting . length } and run` );
}
2026-11-25 01:22:03 +00:00
const blockedButton = ( glyph , word , reason ) => h ( "button" , { type : "button" , class : "btn small" , disabled : true , "data-blocked" : "" , ... named ( word , ` ${ word } : not possible now. ${ reason } ` )}, icon ( glyph ), word );
2026-11-06 08:28:04 +00:00
function actionsFor ( item ) {
if ( item . held ) {
2026-11-08 17:26:51 +00:00
return [ h ( "span" , { class : "muted" }, "takes effect …" )];
2026-11-06 08:28:04 +00:00
}
const buttons = [];
2026-11-17 19:09:46 +00:00
const grant = ( button ) => buttons . push ( grantPress ( button , item ));
2026-11-07 18:14:21 +00:00
if ( item . kind === "review" ) {
2026-11-17 19:09:46 +00:00
grant ( iconBtn ( "bi-check-lg" , "Grant" , "btn small primary" , () => approveOne ( item )));
buttons . push ( denySplit ( item ));
2026-11-25 01:22:03 +00:00
} else if ( item . kind === "ask" ) {
grant ( iconBtn ( "bi-check-lg" , "Grant" , "btn small primary" , () => approveOne ( item ), "Grant: allow this one call" ));
} else if ( Answers . pair ( item )) {
buttons . push (... yesNoButtons ( item , ( label ) => ( asked ( item ) ? answerPick ( item , label ) : chooseOne ( item , label ))));
2026-11-06 08:28:04 +00:00
} else if ( item . kind === "decision" && item . recommended ) {
2026-11-22 15:02:23 +00:00
grant ( h ( "button" , { type : "button" , class : "btn small primary" , ... named ( revealed ( `Recommendation: ${ shownPick ( item . recommended ) } ` )), on : { click : () => approveOne ( item )}}, icon ( "bi-check-lg" ), revealed ( shownPick ( item . recommended ))));
2026-11-06 08:28:04 +00:00
} else if ( item . kind === "operator" && item . runnable ) {
2026-11-22 16:41:45 +00:00
grant ( runButton ( item ));
2026-11-25 01:22:03 +00:00
} else if ( item . kind === "operator" && item . blocked ) {
buttons . push ( blockedButton ( "bi-play-fill" , "Run" , item . blocked ));
2026-11-07 18:14:21 +00:00
} else if ( item . kind === "rule" ) {
2026-11-17 19:09:46 +00:00
grant ( h ( "button" , { type : "button" , class : "btn small primary" , ... named ( "Accept" , "Accept: the passkey signs exactly this rule, then it applies" ), on : { click : () => approveOne ( item )}}, icon ( "bi-shield-check" ), "Accept" ));
2026-11-07 18:14:21 +00:00
}
if ( item . deniable ) {
buttons . push ( denySplit ( item ));
2026-11-25 01:22:03 +00:00
} else if ( item . kind === "operator" && item . blocked ) {
buttons . push ( blockedButton ( "bi-x-lg" , "Deny" , item . blocked ));
2026-11-07 18:14:21 +00:00
}
if ( item . job && ! jobOpen ( item . job )) {
2026-11-08 17:26:51 +00:00
buttons . push ( iconBtn ( "bi-x-circle" , "Close" , "btn small ghost" , () => dismissJob ( item . job ), "Close: hide the console; a done row disappears with it" ));
2026-11-06 08:28:04 +00:00
}
return buttons ;
}
2026-11-14 10:02:43 +00:00
function inboxHeadline ( item ) {
return h ( "button" , { type : "button" , class : "linkish headline" , title : "Open" , on : { click : () => openItem ( item )}}, revealed ( item . kind === "operator" ? item . id : item . kind === "ask" ? item . title . split ( ": " )[ 0 ] : item . title ));
}
2026-11-24 10:39:44 +00:00
/**
* @param {Object} item an inbox item.
* @returns {string} what the item waits for once it was clicked: the passkey, the undo window, or the Grant of a
* Select pick; "" for an item nobody clicked yet.
*/
function waitOf ( item ) {
const to = item . agent ? ` to ${ item . agent } ` : "" ;
if ( sending . has ( keyOf ( item ))) {
2026-11-24 15:42:06 +00:00
return `Clicked: the hub stores the answer and sends it ${ to } once your passkey signs it` ;
2026-11-24 10:39:44 +00:00
}
if ( item . held ) {
return `Signed: waits out the undo window of a few seconds, then the hub delivers it ${ to } and the card closes` ;
}
if ( grantMode () === GRANT_SELECT && ui . selected . has ( keyOf ( item ))) {
const label = chosenLabel ( item );
return ` ${ label === undefined ? "Selected" : `Picked " ${ shownPick ( label ) } "` } : Select mode collects it until you press Grant selection at the top; pick it again to take it back` ;
}
return "" ;
}
2026-11-26 08:44:15 +00:00
/**
* @param {Object} item an inbox item.
* @param {Array} actions its Grant and Deny nodes.
* @returns {HTMLElement|null} one code box with the item's command and diff and the actions in its foot, or null for an
* item with neither.
*/
function itemCode ( item , actions ) {
const commanded = item . kind === "ask" || ( item . kind === "operator" && ! asked ( item ));
const command = commanded ? h ( "div" , { class : "command" , "data-path" : item . directory || "-" }, h ( "code" , {}, revealed ( item . command ))) : null ;
const where = commanded ? h ( "span" , { class : "muted code-where" }, revealed ( `in ${ dash ( item . directory ) } ` )) : null ;
const diff = diffBlocks ( item );
return command || diff ? codeBox ( keyOf ( item ), "preview" , [ command , where , diff ], commanded ? item . command : "" , actions , null ) : null ;
}
2026-11-06 08:28:04 +00:00
function inboxItem ( item , index ) {
const red = item . kind === "review" && item . gate !== "pass" ;
2026-11-24 10:39:44 +00:00
const wait = waitOf ( item );
2026-11-28 01:47:41 +00:00
const actions = dialogLapsed ( item ) ? [] : actionsFor ( item );
const triaged = item . claude ? status ( "claude" ) : item . request && item . request !== "open" ? pill ( item . request , REQUEST_TONES [ item . request ]) : null ;
2026-11-26 08:44:15 +00:00
const code = itemCode ( item , actions );
2026-11-24 10:39:44 +00:00
const classes = [ "item" , "entry" , `k- ${ item . kind } ` , "waiting" , isGreen ( item ) ? "green" : "" , red ? "red" : "" , item . held ? "held" : "" , wait ? "pending" : "" , index === ui . cursor ? "cursor" : "" ]. filter ( Boolean ). join ( " " );
2026-11-26 08:44:15 +00:00
const done = doneItem ( item );
const selected = grantMode () === GRANT_SELECT ? String ( ui . selected . has ( keyOf ( item ))) : null ;
return h ( "li" , { class : classes , "data-index" : index , "data-ref" : keyOf ( item ), "data-to" : item . to || null , "data-blocked" : item . blocked ? "" : null , "data-done" : done ? "" : null , "aria-selected" : selected , "aria-busy" : wait ? "true" : null , title : item . blocked || wait || null },
2026-11-12 16:13:58 +00:00
h ( "div" , { class : "msg-meta" },
when ( item . ts ),
2026-11-24 10:39:44 +00:00
wait ? h ( "span" , { class : "wait-spin" , role : "status" , "aria-label" : wait , title : wait }) : null ,
2026-11-26 08:44:15 +00:00
done ? pill ( "done" , "ok" ) : null ,
2026-11-28 01:47:41 +00:00
triaged ,
2026-11-18 19:28:37 +00:00
asked ( item ) ? pill ( "Decision" , KIND_TONES . decision ) : pill ( ITEM_LABELS [ item . kind ], KIND_TONES [ item . kind ]),
2026-11-12 16:13:58 +00:00
item . kind === "review" ? pill ( item . gate === "pass" ? "Gate green" : "Gate red" , item . gate === "pass" ? "ok" : "bad" ) : null ,
2026-11-22 16:41:45 +00:00
reviewsPill ( item ),
2026-11-12 16:13:58 +00:00
item . warnings . map (( warning ) => pill ( warning , "bad" )),
item . pinned ? pill ( "pinned" , "violet" ) : null ,
item . until ? pill ( `until ${ absolute . format ( stampOf ( item . until )) } ` , "neutral" ) : null ),
h ( "div" , { class : "entry-body" },
h ( "div" , { class : "route" },
2026-11-14 10:02:43 +00:00
inboxHeadline ( item ),
2026-11-26 08:44:15 +00:00
item . agent ? agentLink ( item . agent ) : null ),
2026-11-13 18:14:17 +00:00
item . kind === "review" ? h ( "div" , { class : "muted" }, revealed ( item . detail ), item . score ? revealed ( ` · ${ item . score } ` ) : "" ) : null ,
2026-11-18 19:28:37 +00:00
asked ( item ) ? h ( "p" , { class : "text question" }, revealed ( item . title )) : null ,
2026-11-13 18:14:17 +00:00
item . kind === "operator" && item . detail && item . detail !== "-" ? h ( "p" , { class : "text reason" }, revealed ( item . detail )) : null ,
2026-11-25 01:22:03 +00:00
item . blocked ? h ( "p" , { class : "item-blocked" }, icon ( "bi-slash-circle" ), item . blocked ) : null ,
2026-11-26 08:44:15 +00:00
code ,
2026-11-07 18:14:21 +00:00
item . job ? consoleOf ( item . job ) : null ,
item . kind === "rule" ? ruleRequestBody ( item ) : null ,
2026-11-28 01:47:41 +00:00
dialogLapsed ( item ) ? null : choiceOf ( item , () => {}),
2026-11-12 16:13:58 +00:00
feedbackList ( keyOf ( item ))),
h ( "div" , { class : "item-actions line-actions" },
2026-11-26 08:44:15 +00:00
code ? null : actions ,
2026-11-06 08:28:04 +00:00
h ( "span" , { class : "spacer" }),
2026-11-08 17:26:51 +00:00
iconBtn ( "bi-hourglass-split" , "1 h" , "btn small ghost" , () => mark ( item , nowSeconds () + 3600 , item . pinned ), "1 h: snooze for one hour" ),
iconBtn ( "bi-sunrise" , "tomorrow" , "btn small ghost" , () => mark ( item , tomorrow (), item . pinned ), "tomorrow: snooze until 8 am tomorrow" ),
item . until ? iconBtn ( "bi-alarm" , "wake" , "btn small ghost" , () => mark ( item , 0 , item . pinned ), "wake: show again" ) : null ,
iconBtn ( item . pinned ? "bi-pin-angle-fill" : "bi-pin-angle" , item . pinned ? "unpin" : "pin" , "btn small ghost" , () => mark ( item , item . until , ! item . pinned ))));
2026-11-06 08:28:04 +00:00
}
2026-11-06 22:20:06 +00:00
function inboxScreen () {
2026-11-14 10:02:43 +00:00
inboxDom = h ( "div" , { class : "inbox" , "data-panel" : "tasks/operator" });
2026-11-06 22:20:06 +00:00
return inboxDom ;
}
2026-11-06 08:28:04 +00:00
function drawInbox () {
2026-11-06 22:20:06 +00:00
if ( ! inboxDom ) {
return ;
}
2026-11-28 01:47:41 +00:00
const items = listedInbox ();
2026-11-06 08:28:04 +00:00
ui . cursor = Math . min ( ui . cursor , Math . max ( items . length - 1 , 0 ));
const present = new Set ( items . map ( keyOf ));
for ( const key of [... ui . selected ]) {
if ( ! present . has ( key )) {
ui . selected . delete ( key );
}
}
2026-11-17 19:09:46 +00:00
const mode = grantMode ();
2026-11-28 01:47:41 +00:00
const requests = requestItems (). map (( item ) => [ keyOf ( item ), item . request ]);
const sig = JSON . stringify ([ mode , items . map ( withoutOutput ), [... ui . selected ], [... choices ], [... sending ], ui . cursor , ui . snoozed , ui . busy , Math . floor ( nowSeconds () / 60 ), diffView . version , state . messages . map (( message ) => message . id ), requests , triage . chip , [... triage . picked ]]);
2026-11-06 08:28:04 +00:00
if ( sig === inboxSig ) {
2026-11-07 18:14:21 +00:00
repaintConsoles ( items );
2026-11-06 08:28:04 +00:00
return ;
}
inboxSig = sig ;
2026-11-24 10:39:44 +00:00
const hidden = openInbox (). length - waitingCount ();
2026-11-06 08:28:04 +00:00
const snoozed = h ( "input" , { type : "checkbox" , checked : ui . snoozed , on : { change : ( event ) => {
ui . snoozed = event . target . checked ;
drawInbox ();
}}});
2026-11-28 01:47:41 +00:00
fadeSwap ( inboxDom , [
2026-11-12 16:13:58 +00:00
h ( "div" , { class : "inbox-head" }, h ( "h2" , { class : "group-title" }, "Waits on Operator" , counter ( waitingCount ())), h ( "span" , { class : "spacer" }),
2026-11-26 08:44:15 +00:00
hidden ? h ( "label" , { class : "check" }, snoozed , ` ${ hidden } snoozed` ) : null ),
2026-11-08 17:26:51 +00:00
onLocalhost () ? "" : h ( "p" , { class : "muted" }, "The passkey signs grants only under " , localLink ()),
2026-11-28 01:47:41 +00:00
triageBar (),
... inboxRows ( items )], ( root , swap ) => {
keepTyping ( root , swap );
settleConsoles ( items );
});
2026-11-06 08:28:04 +00:00
}
2026-11-26 08:44:15 +00:00
/**
* @param {Object[]} items the visible inbox items in cursor order.
* @returns {Array} the open rows, then the done rows (signed within their undo window, or their job ended) under a
* Done (n) fold at the end, closed until the operator opens it.
*/
function inboxRows ( items ) {
const rows = items . map (( item , index ) => [ item , index ]);
const live = rows . filter (([ item ]) => ! doneItem ( item ));
const done = rows . filter (([ item ]) => doneItem ( item ));
const fold = done . length ? h ( "details" , { class : "inbox-done" , open : inboxDoneOpen , on : { toggle : ( event ) => {
inboxDoneOpen = event . currentTarget . open ;
}}}, h ( "summary" , {}, `Done ( ${ done . length } )` ), h ( "ol" , { class : "inbox-list" }, done . map (([ item , index ]) => inboxItem ( item , index )))) : null ;
return [ live . length ? h ( "ol" , { class : "inbox-list" }, live . map (([ item , index ]) => inboxItem ( item , index ))) : emptyState ( "All done" , "Nothing is waiting for you right now." ), fold ]. filter ( Boolean );
}
2026-11-06 08:28:04 +00:00
function openItem ( item ) {
if ( ! item ) {
return ;
}
if ( item . kind === "review" ) {
reviewSheet ( item . id );
return ;
}
const body = [
2026-11-26 08:44:15 +00:00
h ( "div" , { class : "who" }, pill ( ITEM_LABELS [ item . kind ], KIND_TONES [ item . kind ]), item . agent ? agentLink ( item . agent ) : null , when ( item . ts )),
2026-11-13 18:14:17 +00:00
h ( "p" , { class : "text" }, revealed ( item . title )),
2026-11-18 19:28:37 +00:00
item . command && ! asked ( item ) ? h ( "div" , { class : "command" }, h ( "code" , {}, revealed ( item . command )), copyButton ( item . command )) : null ,
asked ( item ) && item . detail && item . detail !== "-" ? h ( "p" , { class : "text reason" }, revealed ( item . detail )) : null ,
2026-11-07 18:14:21 +00:00
item . kind === "rule" ? ruleRequestBody ( item ) : null ,
2026-11-08 17:26:51 +00:00
meta ([[ "Directory" , item . directory ], [ "Warnings" , item . warnings . join ( ", " )], [ "Id" , item . id ]]),
2026-11-28 01:47:41 +00:00
dialogLapsed ( item ) ? null : choiceOf ( item , closeSheet ),
h ( "div" , { class : "actions" }, h ( "span" , { class : "spacer" }), ( dialogLapsed ( item ) ? [] : actionsFor ( item )). map (( node ) => {
2026-11-06 08:28:04 +00:00
node . addEventListener ( "click" , closeSheet );
return node ;
})),
];
openSheet ( ITEM_LABELS [ item . kind ], body , false );
}