Files
stack/packages/webui/src/autonomy-http.mjs
T
jason.woltje 7ffe932250 feat(console): dev-local-owner Console writes autonomy and restarts when detached (#1873)
A --dev-local-owner Console's organization pages read writable:false because
the per-org options had no autonomy writer and the route fell back to the
request transport, which org options never set. The org options now carry an
autonomy writer on the organization's console transport, fenced to the four
owner autonomy verbs. The route prefers it and keeps requestWrite otherwise.

mosaic console start --dev-local-owner --detach now runs the Console under a
detached supervisor, so Restart Console (exit 75) relaunches it on the same
address. A non-dev detached Console is unchanged. A deployed --sso --send
Console has no organization routes and keeps its root autonomy transport;
a test pins both.
2026-11-11 14:16:09 -06:00

32 lines
3.3 KiB
JavaScript

// AU: owner Console transport only. Identity and organization never come from a JSON body.
export const AUTONOMY_ROUTE=/^\/api\/autonomy(?:\/(confirm|reverse|review))?$/;
// D1 (#1873): the owner writes this route makes, and a fence that passes only those to a console transport.
export const AUTONOMY_WRITE_VERBS=Object.freeze(['autonomy.set.console','autonomy.review.console','decision.confirm.console','decision.reverse.console']);
export const autonomyCaller=call=>async(verb,args,options)=>{if(!AUTONOMY_WRITE_VERBS.includes(verb))throw new Error(`the autonomy writer does not call ${verb}`);return call(verb,args,options);};
const one=(req,k)=>req.headersDistinct[k]?.length===1?req.headersDistinct[k][0]:null;
const json=(res,status,value)=>{res.writeHead(status,{'content-type':'application/json','cache-control':'no-store'});res.end(JSON.stringify(value));};
export async function autonomyHTTP(req,res,{path,read,write,session,live,origin}) {
if(!session)return json(res,401,{error:'sign-in-required'});
if(!read)return json(res,503,{error:'not-configured'});
if(!['GET','POST'].includes(req.method))return json(res,405,{error:'method-not-allowed'});
const step=AUTONOMY_ROUTE.exec(path)?.[1]??null;
if(step && req.method!=='POST')return json(res,405,{error:'method-not-allowed'});
const post=req.method==='POST';
if(post && !write)return json(res,403,{error:'read-only'});
if(post && (one(req,'origin')!==origin || one(req,'sec-fetch-site')!=='same-origin'))return json(res,403,{error:'cross-origin'});
let args={};
if(post) {
if(one(req,'content-type')?.split(';')[0].trim()!=='application/json')return json(res,400,{error:'invalid-request'});
const chunks=[];let size=0;for await(const chunk of req){size+=chunk.length;if(size>4096)return json(res,413,{error:'invalid-request'});chunks.push(chunk);}
try{args=JSON.parse(Buffer.concat(chunks).toString('utf8'));}catch{return json(res,400,{error:'invalid-request'});}
if(!args || typeof args!=='object' || Array.isArray(args))return json(res,400,{error:'invalid-request'});
const keys=step==='confirm'?['id','confirm','reason']:step==='reverse'?['id']:step==='review'?['decisionClass']:['revision','autonomy'];
if(Object.keys(args).some(k=>!keys.includes(k)))return json(res,400,{error:'invalid-request'});
}
if(live()!==session)return json(res,401,{error:'sign-in-required'});
const subject={provider:session.provider,iss:session.iss,sub:session.sub};
const verb=step==='confirm'?'decision.confirm.console':step==='reverse'?'decision.reverse.console':step==='review'?'autonomy.review.console':post?'autonomy.set.console':'autonomy.get.console';
try{return json(res,200,{...await(post?write:read)(verb,args,{subject}),writable:!!write});}
catch(e){const allowed=['authority-limits-unavailable','decision-closed','confirmation-required','not-found','storage-refused','human-required','invalid-request','invalid-autonomy','policy-reverse-unavailable','autonomy-precondition','revision-conflict','decision-not-eligible','decision-stale','decision-reversed','reverse-window-closed','reversal-unavailable','not-resolver','gate-missing','gate-failed','base-conflict','not-configured'];const code=allowed.includes(e?.code)?e.code:'outcome-unknown';return json(res,code==='human-required'?403:code==='invalid-request'||code==='invalid-autonomy'?400:409,{error:code});}
}