Re-check finding 1 (P2): the wording for refusals that can come after the decision is consumed told the user to look for the proposal "as answered" in the inbox and otherwise read "nothing was applied". The bus inbox lists no closed decision, so a consumed one drops out of it and the user would read the false claim. Those codes now say only that part of it may already be applied and to check the Agents page. decision-http tests assert no "nothing was applied" in any case and no inbox pointer for those codes. Re-check P3-b: the autonomy controls (Save, Discard, notices, review summary and buttons) carry data-focus-key, so a focused button in a clean form gets its focus back after the poll repaints the section. Re-check P3-c: a failed autonomy read keeps the last read only while the page holds the form (dirty, or focus inside it). A clean form shows the read error. org-live-edit-browser test 4 covers both. Re-check P3-a (harness-off after the consume through a corrupt prior settings file) is deferred: wording it exactly needs the bus to mark the phase on the refusal.
79 lines
10 KiB
JavaScript
79 lines
10 KiB
JavaScript
// Owner settings and decision notices. Server and broker establish identity and eligibility.
|
|
(() => {
|
|
'use strict';
|
|
const esc=x=>String(x??'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
|
const classes=['cross-role','merge','policy','authority'],levels=['in the loop','near the loop','out of the loop'];
|
|
const labels={'cross-role':'Cross-role work',merge:'Merge',policy:'Policy',authority:'Plain hire, fire and role change'};
|
|
const states=new Map(),path=(org,step='')=>`${org?`/api/orgs/${encodeURIComponent(org)}`:'/api'}/autonomy${step?`/${step}`:''}`;
|
|
const guidance={
|
|
'policy-reverse-unavailable':'Policy has no effect executor or reverse yet. Choose in the loop or out of the loop.',
|
|
'autonomy-precondition':'This move needs a separate resolver, a tested reverse, owner notices and an enforced budget covering new agents for authority. Out of the loop needs an actual near-loop decision followed by your review of its trail. If a reverse stopped an agent, inspect the trail before relaunching it.',
|
|
'authority-limits-unavailable':'Choose near the loop with a budget covering new agents. Authority out of the loop waits for per-definition agent limits.',
|
|
'decision-closed':'This decision is closed. Refresh the inbox; raise a new decision if needed.',
|
|
'confirmation-required':'Confirm the arbiter resolution in the Console before using the decision.',
|
|
'not-found':'This decision could not be found. Refresh the inbox.',
|
|
'storage-refused':'The trail write was refused. Inspect the decision trail before retrying.',
|
|
'reverse-window-closed':'The reverse window has closed. Raise an ordinary new decision.',
|
|
'revision-conflict':'The organization changed. Reload before saving.',
|
|
'not-resolver':'The resolver no longer holds the required right. Withdraw and raise a fresh decision under current policy.',
|
|
'reversal-unavailable':'This effect cannot be reversed through this path. Use an ordinary new decision.',
|
|
};
|
|
async function call(org,step,body) {
|
|
const r=await fetch(path(org,step),body===undefined?{cache:'no-store'}:{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify(body)});
|
|
const value=await r.json();if(!r.ok)throw Error(guidance[value.error]??`The bus refused: ${value.error??'outcome unknown'}. Refresh before retrying.`);return value;
|
|
}
|
|
// E2E6 P1-1: a poll reloads this state; the last save's receipt and the open review list stay.
|
|
// A failed poll keeps the last good read only while the page holds the form (dirty, or a field has
|
|
// focus), so the held form is not dropped (review P3). A clean form shows the read error (re-check P3).
|
|
const held=org=>{const form=[...document.querySelectorAll('[data-autonomy-org]')].find(n=>n.dataset.autonomyOrg===org)?.querySelector('[data-autonomy-form]');return !!form&&(form.hasAttribute('data-live-dirty')||form.contains(document.activeElement));};
|
|
async function load(org) {const prev=states.get(org),keep={status:prev?.status??'',reviewOpen:prev?.reviewOpen===true};try{states.set(org,{...await call(org,''),...keep});}catch(error){states.set(org,prev&&!prev.error&&held(org)?{...prev,...keep}:{error:error.message,...keep});}}
|
|
function section(org) {
|
|
const s=states.get(org);
|
|
if(!s || s.error)return `<section class="panel" data-autonomy><h3>Autonomy</h3><p>${esc(s?.error??'Autonomy could not be read.')}</p></section>`;
|
|
const v=s.autonomy??{},fk=k=>` data-focus-key="${esc(`autonomy-${k}:${org}`)}"`;
|
|
// The form carries the revision it was drawn from: a form a poll kept (workspace holdLive) saves
|
|
// against that revision, so it cannot overwrite a change it never showed.
|
|
return `<section class="panel" data-autonomy data-autonomy-org="${esc(org)}" data-live-key="${esc(`autonomy:${org}`)}"><h3>Autonomy</h3><p>Choose how closely you follow each class of decision. Hard waits, flagged grants and credentials always need a person.</p><form data-autonomy-form data-revision="${esc(JSON.stringify(s.revision??null))}">
|
|
<div class="autonomy-fields">${classes.map(k=>`<label>${labels[k]}<select name="${k}"${s.writable?'':' disabled'}>${levels.map(l=>`<option${s.levels[k]===l?' selected':''}>${l}</option>`).join('')}</select></label>`).join('')}</div>
|
|
<div class="autonomy-fields"><label>Reverse window (hours)<input type="number" name="reverseWindowHours" min="1" max="168" value="${v.reverseWindowHours??24}"${s.writable?'':' disabled'}></label><label>Authority resolver<input name="authorityResolver" maxlength="64" value="${esc(v.authorityResolver??'')}" autocomplete="off"${s.writable?'':' disabled'}></label></div>
|
|
<label class="autonomy-checkbox"><input name="notices" type="checkbox"${fk('notices')}${v.notices===true?' checked':''}${s.writable?'':' disabled'}> Console notices for decisions near the loop</label>
|
|
<p>In the loop, the arbiter resolves and you confirm before work takes effect. Near the loop, work takes effect and you can reverse it within the window. Out of the loop, the trail records it without a notice.</p>
|
|
${s.writable?`<p><button class="btn" type="submit"${fk('save')}>Save autonomy</button> <button class="btn" type="button" data-autonomy-discard${fk('discard')}>Discard changes</button></p>`:'<p>Only the owner can change autonomy.</p>'}
|
|
<p data-autonomy-status role="status">${esc(s.status)}</p></form>
|
|
${s.writable?`<details data-autonomy-review-list${s.reviewOpen?' open':''}><summary${fk('review-list')}>Review reversals before moving out of the loop</summary><p>Read the decision trail and its reversals before recording your review.</p>${classes.map(k=>`<button class="btn" type="button" data-autonomy-review="${k}"${fk(`review-${k}`)}>Record review: ${labels[k]}</button>`).join('')}</details>`:''}</section>`;
|
|
}
|
|
function notice(d) {
|
|
if(!d?.autonomy)return '';
|
|
const a=d.autonomy,org=d.business??window.MosaicOrg?.id;
|
|
return `<section class="panel" data-autonomy-decision="${esc(d.id)}" data-autonomy-org="${esc(org??'')}"><h2>${a.pending?'Confirm the arbiter resolution':'Decision near the loop'}</h2>
|
|
${a.pending?'<p>The arbiter resolved this decision. It has no effect until you confirm.</p><label>Reason for returning to the arbiter<input data-autonomy-reason maxlength="500"></label><p><button class="btn" data-autonomy-confirm="yes">Confirm resolution</button> <button class="btn" data-autonomy-confirm="no">Return to arbiter</button></p>':`<p>The agent resolved this decision. Reverse is available until ${esc(a.reverseUntil)}. A reverse cannot undo messages, work already done or code already shipped.</p><button class="btn" data-autonomy-reverse>Reverse decision</button>`}
|
|
<p role="status" data-autonomy-status></p></section>`;
|
|
}
|
|
document.addEventListener('submit',async e=>{
|
|
if(!e.target.matches('[data-autonomy-form]'))return;e.preventDefault();
|
|
const form=e.target,section=form.closest('[data-autonomy-org]'),org=section.dataset.autonomyOrg,s=states.get(org),fields=new FormData(form);
|
|
const autonomy=Object.fromEntries(classes.map(k=>[k,fields.get(k)]));autonomy.notices=fields.has('notices');autonomy.reverseWindowHours=Number(fields.get('reverseWindowHours'));
|
|
if(fields.get('authorityResolver'))autonomy.authorityResolver=fields.get('authorityResolver');
|
|
const button=form.querySelector('button[type="submit"]'),status=form.querySelector('[data-autonomy-status]');button.disabled=true;
|
|
let base=s.revision;try{if(form.dataset.revision)base=JSON.parse(form.dataset.revision);}catch{/* drawn without one */}
|
|
try{const r=await call(org,'',{revision:base,autonomy});s.revision=r.revision;s.autonomy=autonomy;s.levels=Object.fromEntries(classes.map(k=>[k,autonomy[k]]));s.status=`Saved. Receipt ${r.transaction}.`;form.dataset.revision=JSON.stringify(r.revision);delete form.dataset.liveDirty;status.textContent=s.status;}
|
|
catch(error){s.status=error.message;status.textContent=s.status;}finally{button.disabled=false;}
|
|
});
|
|
// An edited form is dirty until it saves: holdLive keeps a dirty form through every poll.
|
|
const dirty=e=>{const form=e.target.closest?.('[data-autonomy-form]');if(form)form.dataset.liveDirty='1';};
|
|
document.addEventListener('input',dirty);document.addEventListener('change',dirty);
|
|
// Review P3: Discard drops the held form and draws the last read in its place.
|
|
document.addEventListener('click',e=>{const b=e.target.closest?.('[data-autonomy-discard]');const el=b?.closest('[data-autonomy-org]');if(!el||!states.get(el.dataset.autonomyOrg))return;const org=el.dataset.autonomyOrg;el.outerHTML=section(org);[...document.querySelectorAll('[data-autonomy-org]')].find(n=>n.dataset.autonomyOrg===org)?.querySelector('[data-autonomy-discard]')?.focus();});
|
|
document.addEventListener('toggle',e=>{if(!e.target.matches?.('[data-autonomy-review-list]'))return;const s=states.get(e.target.closest('[data-autonomy-org]')?.dataset.autonomyOrg);if(s)s.reviewOpen=e.target.open;},true);
|
|
document.addEventListener('click',async e=>{
|
|
const button=e.target.closest('[data-autonomy-confirm],[data-autonomy-reverse],[data-autonomy-review]');if(!button)return;
|
|
const section=button.closest('[data-autonomy-org]'),org=section.dataset.autonomyOrg,status=section.querySelector('[data-autonomy-status]');
|
|
const review=button.dataset.autonomyReview,reverse=button.hasAttribute('data-autonomy-reverse');
|
|
const body=review?{decisionClass:review}:reverse?{id:section.dataset.autonomyDecision}:{id:section.dataset.autonomyDecision,confirm:button.dataset.autonomyConfirm==='yes',...(button.dataset.autonomyConfirm==='no'?{reason:section.querySelector('[data-autonomy-reason]').value}:{})};
|
|
button.disabled=true;
|
|
try{const r=await call(org,review?'review':reverse?'reverse':'confirm',body);status.textContent=review?'Trail review recorded.':reverse?(r.restart||r.trailPending?'Reversal committed. Completion needs recovery: inspect the decision trail and the stopped agent before requesting relaunch.':'Reversal recorded.'):(body.confirm?'Confirmation recorded. Refresh the inbox for current state.':'Returned to arbiter. Refresh the inbox for current state.');}
|
|
catch(error){status.textContent=error.message;}finally{button.disabled=false;}
|
|
});
|
|
window.MosaicAutonomy={load,section,notice};
|
|
})();
|