chamba 0.9.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +10 -4
  2. package/dist/commands/dev.js +11 -11
  3. package/dist/commands/menu.js +1 -1
  4. package/dist/commands/settings.js +9 -6
  5. package/dist/lib/awake-holder.js +3 -0
  6. package/dist/lib/awake-marker.js +4 -0
  7. package/dist/lib/awake.js +1 -0
  8. package/dist/lib/constants.js +1 -1
  9. package/dist/lib/global-config.js +3 -3
  10. package/dist/lib/skills.js +2 -2
  11. package/package.json +1 -1
  12. package/templates/pane-apps/client/assets/api-Cyoc3E6K.css +1 -0
  13. package/templates/pane-apps/client/assets/home-CLFnmg7u.css +1 -0
  14. package/templates/pane-apps/client/assets/home-CrhBkqDQ.js +1 -0
  15. package/templates/pane-apps/client/assets/reviews-CKk1bus6.js +153 -0
  16. package/templates/pane-apps/client/assets/reviews-DzHtA35Q.css +1 -0
  17. package/templates/pane-apps/client/assets/{files-BkmyervY.js → search-DgFnnu-J.js} +1 -1
  18. package/templates/pane-apps/client/assets/specs-JvdNFtXR.js +13 -0
  19. package/templates/pane-apps/client/assets/{useNow-BHFKFraS.js → useNow-Ww-Gffnk.js} +1 -1
  20. package/templates/pane-apps/client/home/index.html +5 -5
  21. package/templates/pane-apps/client/reviews/index.html +6 -6
  22. package/templates/pane-apps/client/specs/index.html +4 -4
  23. package/templates/pane-apps/server/home.mjs +11 -12
  24. package/templates/pane-apps/server/reviews.mjs +18 -18
  25. package/templates/pane-apps/server/specs.mjs +4 -4
  26. package/templates/skills/dx-spec/SKILL.md +4 -16
  27. package/templates/skills/dx-spec/references/review-guide.md +13 -7
  28. package/templates/skills/dx-spec/references/spec-guide.md +11 -4
  29. package/templates/skills/dx-spec/references/stages.md +38 -22
  30. package/templates/skills/dx-spec-execute/SKILL.md +5 -21
  31. package/templates/webterm/README.md +11 -8
  32. package/templates/webterm/awake.js +27 -0
  33. package/templates/webterm/config.js +10 -0
  34. package/templates/webterm/public/app/state.js +4 -0
  35. package/templates/webterm/public/app/tabs.js +37 -3
  36. package/templates/webterm/public/styles.css +13 -0
  37. package/templates/webterm/server.js +24 -1
  38. package/templates/webterm/tools/home.js +25 -3
  39. package/templates/pane-apps/client/assets/api-DNLbSBpK.css +0 -1
  40. package/templates/pane-apps/client/assets/home-3SeGzySM.css +0 -1
  41. package/templates/pane-apps/client/assets/home-Cu9xOd2i.js +0 -1
  42. package/templates/pane-apps/client/assets/reviews-DMrGB-kG.js +0 -153
  43. package/templates/pane-apps/client/assets/reviews-nK_yRav9.css +0 -1
  44. package/templates/pane-apps/client/assets/specs-Cabb15F0.js +0 -13
  45. package/templates/skills/dx-spec/references/principles-template.md +0 -2
  46. /package/templates/pane-apps/client/assets/{api-BBDf_rzz.js → api-CguEsvNp.js} +0 -0
@@ -1,21 +1,21 @@
1
1
  import{closeSync as e,constants as t,existsSync as n,fstatSync as r,lstatSync as i,mkdirSync as a,openSync as o,readFileSync as s,readdirSync as c,readlinkSync as l,realpathSync as u,renameSync as d,rmSync as f,statSync as p,watch as m,writeFileSync as h}from"node:fs";import{basename as g,dirname as _,isAbsolute as v,join as y,relative as ee,resolve as b,sep as x}from"node:path";import{createHash as S}from"node:crypto";import{execFile as te}from"node:child_process";function ne(e){return e.toLowerCase().replace(/`/g,``).replace(/[^a-z0-9]+/g,`-`).replace(/^-+|-+$/g,``)||``}function re(e){return e.open?e.messages[e.messages.length-1]?.from===`agent`:!1}var ie=[`undo`,`explain`,`comment`,`group-comment`,`fix`],ae={undo:`Undo the change`,explain:`Explain in a comment`,comment:`Comment`,"group-comment":`Comment on group`,fix:`Fix`};function oe(e){return e!==`undo`}function se(e){if(e.length===0)return``;let t=[...e].sort((e,t)=>e-t),n=t[0],r=t[t.length-1];return n===void 0||r===void 0?``:n===r?`L${n}`:`L${n}-${r}`}var ce=[`info`,`attention`,`issue`];function le(e,t,n){let r=`${e}\n${(t??[]).join(`,`)}\n${n}`,i=3735928559,a=1103547991;for(let e=0;e<r.length;e+=1){let t=r.charCodeAt(e);i=Math.imul(i^t,2654435761),a=Math.imul(a^t,1597334677)}return i=Math.imul(i^i>>>16,2246822507)^Math.imul(a^a>>>13,3266489909),a=Math.imul(a^a>>>16,2246822507)^Math.imul(i^i>>>13,3266489909),(a>>>0).toString(16).padStart(8,`0`)+(i>>>0).toString(16).padStart(8,`0`)}function ue(e,t){return`${e}\n${t}`}var de=`ungrouped`;function fe(e){return e.lines===null||e.lines.length===0?null:Math.min(...e.lines)}function pe(e){return[...e].sort((e,t)=>(fe(e)??-1)-(fe(t)??-1))}function me(e){return e.unseen+e.issues+e.attention+e.threads+e.pending>0}function he(e){return e.unseen+e.issues+e.attention+e.threads+e.pending}function ge(e){let t=[],n=(e,t,n)=>e===1?t:n;if(e.unseen>0&&t.push({kind:`unseen`,count:e.unseen,text:`${n(e.unseen,`hunk`,`hunks`)} not Seen`}),e.attention>0&&t.push({kind:`attention`,count:e.attention,text:`attention ${n(e.attention,`note`,`notes`)} open`}),e.issues>0&&t.push({kind:`issues`,count:e.issues,text:`issue ${n(e.issues,`note`,`notes`)} open`}),e.threads>0){let r=e.unreadReplies>0?`, with ${e.unreadReplies} ${n(e.unreadReplies,`reply`,`replies`)} you have not read`:``;t.push({kind:`threads`,count:e.threads,text:`${n(e.threads,`conversation`,`conversations`)} open${r}`})}return e.pending>0&&t.push({kind:`pending`,count:e.pending,text:`feedback ${n(e.pending,`item`,`items`)} pending, not sent`}),t}var C=e=>typeof e==`object`&&!!e&&!Array.isArray(e),w=(e,t=``)=>typeof e==`string`?e:t,T=e=>typeof e==`string`?e:null,E=(e,t=0)=>typeof e==`number`&&Number.isFinite(e)?e:t,D=e=>Array.isArray(e)?e:[],_e=e=>D(e).filter(e=>typeof e==`string`);function O(e){if(e==null||e.trim()===``)return null;try{let t=JSON.parse(e);return C(t)?t:null}catch{return null}}function ve(e=``){return{version:1,fingerprint:e,marks:[],acts:[],changed:[]}}function ye(e){let t=O(e);if(t===null)return ve();let n=[`hunk`,`file`,`group`,`unseen`];return{version:E(t.version,1),fingerprint:w(t.fingerprint),marks:D(t.marks).filter(C).map(e=>({hunk:w(e.hunk),at:w(e.at),act:w(e.act),content:w(e.content)})).filter(e=>e.hunk!==``),acts:D(t.acts).filter(C).map(e=>{let t=w(e.kind);return{id:w(e.id),at:w(e.at),kind:n.includes(t)?t:`hunk`,container:T(e.container),hunks:_e(e.hunks)}}).filter(e=>e.id!==``),changed:D(t.changed).filter(C).map(e=>({hunk:w(e.hunk),at:w(e.at),content:w(e.content)})).filter(e=>e.hunk!==``)}}function be(e=``){return{version:1,fingerprint:e,threads:[]}}function xe(e){let t=O(e);if(t===null)return be();let n=[`you`,`agent`,`tool`];return{version:E(t.version,1),fingerprint:w(t.fingerprint),threads:D(t.threads).filter(C).map(e=>({id:w(e.id),kind:e.kind===`group`?`group`:`hunk`,anchor:w(e.anchor),open:e.open!==!1,label:e.label===`hunk-removed`?`hunk-removed`:null,closedAt:T(e.closedAt),messages:D(e.messages).filter(C).map(e=>{let t=w(e.from);return{id:w(e.id),from:n.includes(t)?t:`tool`,at:w(e.at),text:w(e.text),readAt:T(e.readAt)}}).filter(e=>e.id!==``)})).filter(e=>e.id!==``&&e.anchor!==``)}}function Se(e=``){return{version:1,fingerprint:e,items:[]}}function Ce(e){let t=O(e);return t===null?Se():{version:E(t.version,1),fingerprint:w(t.fingerprint),items:D(t.items).filter(C).map(e=>{let t=w(e.act);return{id:w(e.id),at:w(e.at),act:ie.includes(t)?t:`comment`,hunk:T(e.hunk),group:T(e.group),lines:Te(e.lines),text:w(e.text),label:e.label===`hunk-removed`?`hunk-removed`:null}}).filter(e=>e.id!==``&&(e.hunk!==null||e.group!==null))}}function we(e){let t=O(e);return t===null?null:{version:E(t.version,1),sentAt:w(t.sentAt),fingerprint:w(t.fingerprint),items:D(t.items).filter(C).map(e=>{let t=w(e.act);return{hunk:T(e.hunk),act:ie.includes(t)?t:`comment`,group:T(e.group),lines:Te(e.lines),text:w(e.text)}})}}function Te(e){if(!Array.isArray(e))return null;let t=e.filter(e=>Number.isInteger(e)&&e>0);return t.length===0?null:t}function Ee(e=``){return{version:1,fingerprint:e,items:[]}}function De(e){let t=O(e);return t===null?Ee():{version:E(t.version,1),fingerprint:w(t.fingerprint),items:D(t.items).filter(C).map(e=>({hunk:w(e.hunk),key:w(e.key),at:w(e.at)})).filter(e=>e.hunk!==``&&e.key!==``)}}function Oe(e=``){return{version:1,fingerprint:e,groups:[],notes:[]}}function ke(e){let t=O(e);return t===null?Oe():{version:E(t.version,1),fingerprint:w(t.fingerprint),groups:D(t.groups).filter(C).map(e=>({id:w(e.id),name:w(e.name),narrative:w(e.narrative),facts:_e(e.facts),hunks:_e(e.hunks)})).filter(e=>e.id!==``),notes:D(t.notes).filter(C).map(e=>({hunk:w(e.hunk),kind:w(e.kind),lines:Array.isArray(e.lines)?e.lines.filter(e=>typeof e==`number`):null,text:w(e.text),key:w(e.key)})).filter(e=>e.hunk!==``&&e.key!==``&&ce.includes(e.kind))}}function Ae(){return{version:1,scroll:0,expanded:[],group:null,drafts:[],at:``}}function je(e){let t=O(e);return t===null?Ae():{version:E(t.version,1),scroll:E(t.scroll),expanded:_e(t.expanded),group:T(t.group),drafts:Me(t.drafts),at:w(t.at)}}function Me(e){let t=[];for(let n of D(e)){if(!C(n))continue;let e=w(n.anchor),r=w(n.act);e===``||!(r in ae)||t.push({anchor:e,act:r,lines:D(n.lines).filter(e=>typeof e==`number`&&Number.isInteger(e)&&e>0),text:w(n.text),note:w(n.note)===``?null:w(n.note)})}return t}function Ne(e){let t=O(e);return t===null?null:{version:E(t.version,1),approved:t.approved===!0,at:w(t.at),name:w(t.name),signature:T(t.signature),reason:w(t.reason),loops:E(t.loops),fingerprint:w(t.fingerprint),commit:w(t.commit),uncommitted:t.uncommitted===!0,leftAsIs:Pe(t.leftAsIs)}}function Pe(e){return C(e)?{unseen:E(e.unseen),issues:E(e.issues),attention:E(e.attention),threads:E(e.threads),unreadReplies:E(e.unreadReplies),pending:E(e.pending)}:null}function Fe(e,t){return`${e}\n${t}`}function Ie(e,t=new Set){let n=(e.baseline?.entries??[]).map(e=>e.id),r=new Map(n.map((e,t)=>[e,t])),i=new Set(e.marks.marks.map(e=>e.hunk)),a=new Set(e.marks.changed.map(e=>e.hunk).filter(e=>r.has(e))),o=new Set([...e.metadata.groups.map(e=>e.id),de]),s=new Map;for(let t of e.metadata.notes)s.set(t.hunk,[...s.get(t.hunk)??[],t]);let c=new Map;for(let t of e.dismissals.items)c.set(t.hunk,(c.get(t.hunk)??new Set).add(t.key));let l=new Map,u=new Map;for(let[e,t]of s){let n=pe(t);l.set(e,n);let r=c.get(e);u.set(e,n.filter(e=>r===void 0||!r.has(e.key)))}let d=new Map,f=new Set,p=[];for(let n of e.threads.threads){let e=n.kind===`hunk`&&n.open&&n.label===`hunk-removed`,i=n.kind===`group`?o.has(n.anchor):r.has(n.anchor)||e;if(e&&p.push(n),!i)continue;let a=Fe(n.kind,n.anchor);d.set(a,[...d.get(a)??[],n]);for(let e of n.messages)e.from!==`agent`||e.readAt!==null||t.has(e.id)||f.add(e.id)}return{side:e.state.side,order:n,places:r,seen:i,changed:a,open:u,notes:l,aside:c,threads:d,unread:f,gone:p}}function Le(e){let t=0;for(let n of e.threads.values())for(let e of n)e.open&&(t+=1);return t}function Re(e,t){if(t===null)return[...e.order];let n=new Map;for(let r of t.hunks){let t=e.places.get(r);t!==void 0&&n.set(r,t)}return[...n.keys()].sort((e,t)=>(n.get(e)??0)-(n.get(t)??0))}function ze(e,t){let n=Re(e,t),r=0,i=0,a=0,o=0,s=0;for(let c of n){e.seen.has(c)&&(r+=1),e.changed.has(c)&&(s+=1);for(let t of e.open.get(c)??[])t.kind===`issue`?i+=1:t.kind===`attention`&&(a+=1);t!==null&&(o+=Ve(e,Fe(`hunk`,c)))}return t===null?o=e.unread.size:o+=Ve(e,Fe(`group`,t.id)),{hunks:n.length,seen:r,unseen:n.length-r,issues:i,attention:a,replies:o,changed:s,waiting:Be(e,t,n)}}function Be(e,t,n){if(e.side===`agent`)return 0;let r=t===null?[...e.threads.keys()]:[...n.map(e=>Fe(`hunk`,e)),Fe(`group`,t.id)],i=0;for(let t of r)for(let n of e.threads.get(t)??[])re(n)&&(i+=1);return i}function Ve(e,t){let n=0;for(let r of e.threads.get(t)??[])for(let t of r.messages)e.unread.has(t.id)&&(n+=1);return n}var k=`.reviews`,A=`archive`,He=`state.json`,Ue=`marks.json`,We=`threads.json`,Ge=`pending.json`,Ke=`dismissals.json`,qe=`reader-state.json`,Je=`approval.json`,Ye=`signature.png`,Xe=`sends`,j=`baseline`,Ze=`baseline.next`,Qe=`baseline.gone`,$e=`baseline.json`,et=`diff.patch`,tt=`blobs`,nt=`metadata.json`;function rt(e){let t=e.indexOf(`/${k}/`);return!(t===-1||e.slice(t+8+2)===`reader-state.json`)}var it=[`# Written by the Code Reviews tool when this root was created.`,`# Git keeps the record of a review. What is named here is a snapshot of the`,`# code, the agent labels on it, or one reader place, and none of it is history.`,`*/${k}/${Ze}/`,`*/${k}/${Qe}/`,`*/${k}/${j}/${et}`,`*/${k}/${j}/${tt}/`,`*/${k}/${j}/${nt}`,`*/${k}/${Ge}`,`*/${k}/${qe}`,`${A}/*/${k}/${Ze}/`,`${A}/*/${k}/${Qe}/`,`${A}/*/${k}/${j}/${et}`,`${A}/*/${k}/${j}/${tt}/`,`${A}/*/${k}/${j}/${nt}`,`${A}/*/${k}/${Ge}`,`${A}/*/${k}/${qe}`],at=12;function ot(e){let t=e=>e.slice(0,at);if(e.followed!==null)return`commit ${t(e.followed)}`;if(e.kind===`branch`||e.kind===`other-branch`){let n=e.baseRef??t(e.base),r=`${e.head} against ${n}`;return e.uncommitted?`${r} - includes uncommitted changes`:r}return e.kind===`range`?`${t(e.base)}..${t(e.head)}`:e.kind===`last-commit`?`last commit ${t(e.head)}`:`commit ${t(e.head)}`}function st(e){let t=new Date(e);if(!/^\d{4}-\d{2}-\d{2}/.test(e)||Number.isNaN(t.getTime()))return``;let n=e=>String(e).padStart(2,`0`);return`${t.getFullYear()}-${n(t.getMonth()+1)}-${n(t.getDate())}`}function ct(e){let t=e.state,n=[`# ${t.name||t.slug||`A review`}`,``],r=st(t.created);if(r&&n.push(`- Filed: ${r} from the ${t.filedBy}`),n.push(`- Status: ${t.status}`),n.push(`- Reviewing: ${ot(t.selection)}`),n.push(`- Seen: ${e.seen} of ${e.total} hunks`),n.push(`- Feedback loops: ${e.loops}`),e.threads.total>0&&n.push(`- Threads: ${e.threads.total}, ${e.threads.open} open`),t.side===`agent`){let e=t.agent.instruction.replace(/\s*\n\s*/g,` `).trim(),r=t.agent.activity||t.agent.act||`working`;n.push(`- Awaits the agent: ${r}${e===``?``:` - asked: ${e}`}`)}if(e.commit&&n.push(`- Commit: ${e.commit}`),e.fingerprint&&n.push(`- Fingerprint: ${e.fingerprint}`),e.metadata.groups.length>0){n.push(``,`## Groups`,``);for(let t of e.metadata.groups){n.push(`### ${t.name||t.id}`,``),t.narrative&&n.push(t.narrative,``);for(let e of t.facts)n.push(`- ${e}`);t.facts.length>0&&n.push(``),n.push(`${t.hunks.length} ${t.hunks.length===1?`hunk`:`hunks`}`,``)}for(;n[n.length-1]===``;)n.pop()}let i=e.approval;if(i!==null){n.push(``,`## The ending`,``);let e=st(i.at);if(i.approved){let t=i.name||`the reviewer`;if(n.push(`Approved by ${t}${e?` on ${e}`:``}.`),i.leftAsIs!==null&&me(i.leftAsIs)){let e=he(i.leftAsIs),t=ge(i.leftAsIs).map(e=>`${e.count} ${e.text}`);n.push(``,`Approved with ${e} ${e===1?`thing`:`things`} left as ${e===1?`it was`:`they were`}: ${t.join(`, `)}.`)}i.signature&&n.push(``,`Signed by hand.`)}else n.push(`Closed without approval${e?` on ${e}`:``}.`),i.reason&&n.push(``,i.reason);i.uncommitted&&n.push(``,`Uncommitted changes were part of what was reviewed.`)}if(t.log.length>0){n.push(``,`## Log`,``);for(let e of t.log){let t=st(e.at);n.push(t?`- ${t}: ${e.said}`:`- ${e.said}`)}}return`${n.join(`
2
- `)}\n`}var lt=2e3,ut=600*1e3;function dt(e,t){if(e===null)return!1;let n=new Date(e).getTime();return!Number.isNaN(n)&&t.getTime()-n>ut}var ft=[`reviewing`,`approved`,`closed`,`archived`],pt=[`prepare`,`process`,`sync`],mt=e=>typeof e==`object`&&!!e&&!Array.isArray(e),M=(e,t=``)=>typeof e==`string`?e:t,ht=e=>typeof e==`string`?e:null,gt=e=>typeof e==`number`&&Number.isInteger(e)&&e>=0?e:null;function _t(e,t,n,r,i){return{version:1,name:e,slug:t,selection:n,status:`reviewing`,side:`agent`,agent:{act:`prepare`,activity:`Preparing the review`,groups:null,lastPostAt:i,instruction:``},filedBy:r,created:i,log:[{at:i,said:`filed from the ${r===`form`?`form`:`terminal`}`}]}}function vt(e,t){if(e==null||e.trim()===``)return null;let n;try{n=JSON.parse(e)}catch{return null}if(!mt(n))return null;let r=mt(n.selection)?n.selection:null;if(r===null)return null;let i=mt(n.agent)?n.agent:{},a=M(n.status),o=M(i.act);return{version:typeof n.version==`number`?n.version:1,name:M(n.name,t),slug:M(n.slug,t),selection:r,status:ft.includes(a)?a:`reviewing`,side:n.side===`agent`?`agent`:`you`,agent:{act:pt.includes(o)?o:null,activity:M(i.activity),groups:gt(i.groups),lastPostAt:ht(i.lastPostAt),instruction:M(i.instruction)},filedBy:n.filedBy===`terminal`?`terminal`:`form`,created:M(n.created),log:Array.isArray(n.log)?n.log.filter(mt).map(e=>({at:M(e.at),said:M(e.said)})).filter(e=>e.said!==``):[]}}function yt(e){let t=e?.code;return typeof t==`string`&&bt.has(t)?t:null}var bt=new Set([`path-refused`,`no-entry`,`no-file`,`bad-request`,`too-large`,`upstream-failed`,`unreadable-answer`]),N=class extends Error{requested;code=`path-refused`;constructor(e,t){super(`${e}: refused path ${t}`),this.requested=t,this.name=`PathRefused`}},xt=class extends Error{requested;code=`no-entry`;constructor(e,t){super(`${e}: no directory at ${t}`),this.requested=t,this.name=`NoSuchEntry`}},St=class extends Error{requested;code=`no-file`;constructor(e,t,n=`not a file`){super(`${e}: ${n} - ${t}`),this.requested=t,this.name=`NoSuchFile`}},Ct=class extends Error{requested;size;code=`too-large`;constructor(e,t,n){super(`${e}: too large to serve - ${t}`),this.requested=t,this.size=n,this.name=`FileTooLarge`}};function wt(e){switch(yt(e)){case`path-refused`:return 403;case`no-entry`:case`no-file`:return 404;case`bad-request`:return 400;case`too-large`:return 413;case`upstream-failed`:case`unreadable-answer`:return 502}return Tt(e)?404:500}function Tt(e){let t=e?.code;return t===`ENOENT`||t===`ENOTDIR`}var Et=32*1024*1024;new Set(`txt text log json yaml yml toml ini cfg conf csv tsv xml css scss js jsx mjs cjs ts tsx py rb go rs java kt swift c h cpp hpp sh bash zsh fish sql graphql gql env diff patch lock gitignore editorconfig`.split(` `));function Dt(e){let t=e.slice(e.lastIndexOf(`/`)+1).slice(1).match(/\.([^.]+)$/);return t?t[1].toLowerCase():``}function Ot(e){return e.startsWith(`.`)}function kt(e){let t=e,n=[];for(;;)try{return y(u(t),...n)}catch{let r=_(t);if(r===t)return e;n.unshift(g(t)),t=r}}function At(e){return kt(b(e))}function jt(e){return e.split(`/`).some(e=>e!==``&&Ot(e))}function Mt(e,t,n,r){let i=n.replace(/^\/+/,``);if(v(n)||i.includes(`\0`))throw new N(e,n);let a=r===`require`;if(jt(i)!==a)throw new N(e,n);let o=kt(b(t)),s=b(o,i);if(!Ft(o,s)||!Ft(o,kt(s))||jt(ee(o,s).split(x).join(`/`))!==a)throw new N(e,n);return s}function Nt(e,t,n){return Mt(e,t,n,`refuse`)}function Pt(e,t,n){return Mt(e,t,n,`require`)}function Ft(e,t){return t===e||t.startsWith(e+x)}function It(e,t=`children`){if(t===`self`)return``;let n=e.split(`/`).filter(Boolean);return n.length<2?``:n[0]===`archive`?n.length>2?`archive/${n[1]}`:``:n[0]}var Lt={md:`text/markdown; charset=utf-8`,markdown:`text/markdown; charset=utf-8`,html:`text/html; charset=utf-8`,htm:`text/html; charset=utf-8`,css:`text/css; charset=utf-8`,js:`text/javascript; charset=utf-8`,json:`application/json; charset=utf-8`,svg:`image/svg+xml`,png:`image/png`,jpg:`image/jpeg`,jpeg:`image/jpeg`,gif:`image/gif`,webp:`image/webp`,avif:`image/avif`,bmp:`image/bmp`,ico:`image/x-icon`,pdf:`application/pdf`,txt:`text/plain; charset=utf-8`};function Rt(e){return Lt[Dt(e)]??`application/octet-stream`}var zt=Et;function Bt(e,t,n){return Ht(e,Nt(e,t,n),n)}function Vt(e,t,n,r){let i=n.replace(/^\/+/,``);if(!r(i))throw new N(e,n);return Ht(e,Pt(e,t,i),n)}function Ht(n,i,a){let c=o(i,t.O_RDONLY|t.O_NOFOLLOW);try{let e=r(c);if(!e.isFile())throw new St(n,a);if(e.size>zt)throw new Ct(n,a,e.size);return{bytes:s(c),mediaType:Rt(a),size:e.size,modified:new Date(e.mtimeMs).toISOString()}}finally{e(c)}}var Ut=`.tmp-`;function Wt(e){return e.slice(e.lastIndexOf(`/`)+1).includes(Ut)}function P(e,t){a(_(e),{recursive:!0});let n=`${e}${Ut}${process.pid}-${Math.random().toString(36).slice(2)}`;try{h(n,t),d(n,e)}finally{f(n,{force:!0})}}function Gt(e,t,n,r){P(Pt(e,t,n),r)}function Kt(e,t,n){let r=Pt(e,t,n);try{return s(r,`utf8`)}catch{return null}}function qt(e){try{let t=i(e);return{directory:t.isDirectory(),id:t.ino}}catch{return{directory:!1,id:0}}}function Jt(e,t,r={}){let i=r.entries??`children`,a=r.coalesce??`per-path`,o=r.debounceMs??120,s=r.shared??(()=>!1),l=new Map,u=null,d=new Map,f=!1,p=t=>t===``?e:`${e}/${t}`,h=()=>{u=null;let e=[...l.values()];l.clear();for(let n of e)try{t(n)}catch{}},_=e=>{if(e===``||Wt(e)||jt(e)&&!s(e))return;let t=It(e,i);if(a===`per-entry`)l.set(t,{entry:t,path:t,kind:`changed`});else{let r=n(p(e))?`changed`:`gone`;l.set(e,{entry:t,path:e,kind:r})}u||=setTimeout(h,o)},v=e=>{for(let[t,n]of d)(t===e||t.startsWith(`${e}/`))&&(n.watcher.close(),d.delete(t))},y=t=>{if(f||d.has(t))return;let r;try{r=m(p(t),(r,i)=>{if(!i)return;let a=i.toString();if(a===``)return;let o=t===``?a:`${t}/${a}`;if(a===(t===``?g(e):t.split(`/`).at(-1))&&!n(p(o)))return;let s=d.get(o),c=qt(p(o));if(s&&c.directory){if(s.id===c.id)return;v(o),y(o),_(o);return}_(o),s?v(o):c.directory&&y(o)})}catch{return}r.on(`error`,()=>v(t)),d.set(t,{watcher:r,id:qt(p(t)).id});for(let e of ee(t))y(e)},ee=e=>{try{return c(p(e),{withFileTypes:!0}).filter(e=>e.isDirectory()).map(t=>e===``?t.name:`${e}/${t.name}`)}catch{return[]}};return y(``),{refollow(){f||y(``)},close(){f=!0,u&&clearTimeout(u),u=null;for(let e of d.values())e.watcher.close();d.clear()}}}var Yt={A:`added`,C:`copied`,D:`deleted`,M:`modified`,R:`renamed`,T:`type-changed`,U:`unmerged`,X:`unknown`},F=class extends Error{code=`unreadable-answer`;constructor(e){super(`reviews: ${e}`),this.name=`UnreadableDiff`}},Xt=new WeakMap;function Zt(e){return Xt.get(e)??null}function I(e){return Buffer.from(e,`latin1`).toString(`utf8`)}function L(e){let t=(Buffer.isBuffer(e)?e:Buffer.from(e,`utf8`)).toString(`latin1`);if(t.trim()===``)return{files:[]};let n=[],r=t;for(;r!==``;){let e=Qt(r);n.push(...e.files),r=e.rest}return{files:n}}function Qt(e){let{raw:t,patch:n,rest:r}=en(e),i=t.map(e=>({path:e.path,oldPath:e.oldPath,status:e.status,similarity:e.similarity,oldMode:e.oldMode,newMode:e.newMode,oldOid:e.oldOid,newOid:e.newOid,binary:!1,hunks:[]}));rn(n,i,t);for(let e of i)e.path=I(e.path),e.oldPath=e.oldPath===null?null:I(e.oldPath);return{files:i,rest:r}}var $t=/^:\d{6} \d{6} [0-9a-f]+ [0-9a-f]+ [A-Z]\d*$/;function en(e){let t=[],n=t=>{let n=e.indexOf(`\0`,t);return n===-1?{value:e.slice(t),next:e.length}:{value:e.slice(t,n),next:n+1}},r=0;for(;;){let i=n(r);if(!i.value.startsWith(`:`))break;let a=nn(i.value),o=a.status===`renamed`||a.status===`copied`?2:1,s=[],c=i.next;for(let t=0;t<o;t+=1){if(c>=e.length)throw new F(`a raw record names no path: ${i.value}`);let t=n(c);s.push(t.value),c=t.next}t.push(o===2?{...a,oldPath:s[0],path:s[1]}:{...a,oldPath:null,path:s[0]}),r=c}for(;e[r]===`\0`;)r+=1;let i=tn(e,r);return{raw:t,patch:e.slice(r,i),rest:e.slice(i)}}function tn(e,t){let n=t;for(;n<e.length;){let r=e.indexOf(`:`,n);if(r===-1)return e.length;if(r===t||e[r-1]===`
3
- `){let t=e.indexOf(`\0`,r),n=t===-1?e.slice(r):e.slice(r,t);if($t.test(n))return r}n=r+1}return e.length}function nn(e){let t=e.slice(1).split(` `);if(t.length!==5)throw new F(`a raw record reads wrong: ${e}`);let[n,r,i,a,o]=t,s=Yt[o.slice(0,1)];if(!s)throw new F(`a raw record carries no status: ${e}`);let c=o.slice(1);return{oldMode:n,newMode:r,oldOid:i,newOid:a,status:s,similarity:c===``?null:Number(c)}}function rn(e,t,n){if(e===``)return;let r=e.split(`
4
- `);r.at(-1)===``&&r.pop();let i=0,a=-1;for(;i<r.length;){let e=r[i];if(!e.startsWith(`diff --git `))throw new F(`the patch does not start a file at line ${i+1}`);a+=1;let o=t[a],s=n[a];if(!o||!s)throw new F(`the patch holds more files than the raw record`);an(e,s),i=sn(r,i+1,o)}if(a+1!==t.length)throw new F(`the raw record names ${t.length} files and the patch holds ${a+1}`)}function an(e,t){let n=t.oldPath??t.path,r=t.path,i=`diff --git a/${n} b/${r}`,a=`diff --git ${on(`a/${n}`)} ${on(`b/${r}`)}`;if(e!==i&&e!==a)throw new F(`the patch and the raw record disagree about ${r}`)}function on(e){let t={"\x07":`\\a`,"\b":`\\b`,"\f":`\\f`,"\n":`\\n`,"\r":`\\r`," ":`\\t`,"\v":`\\v`,'"':`\\"`,"\\":`\\\\`},n=``;for(let r of e){let e=t[r];if(e){n+=e;continue}let i=r.codePointAt(0)??0;n+=i<32||i===127?`\\${i.toString(8).padStart(3,`0`)}`:r}return`"${n}"`}function sn(e,t,n){let r=t;for(;r<e.length;){let t=e[r];if(t.startsWith(`diff --git `))return r;if(t.startsWith(`@@`)){r=ln(e,r,n);continue}(t.startsWith(`Binary files `)||t===`GIT binary patch`)&&(n.binary=!0),r+=1}return r}var cn=/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(?: (.*))?$/;function ln(e,t,n){let r=e[t],i=cn.exec(r);if(!i)throw new F(`a hunk header reads wrong: ${r}`);let a=Number(i[1]),o=i[2]===void 0?1:Number(i[2]),s=Number(i[3]),c=i[4]===void 0?1:Number(i[4]),l={header:I(r),heading:I(i[5]??``),oldStart:a,oldLines:o,newStart:s,newLines:c,lines:[]},u=[],d=a,f=s,p=0,m=0,h=t+1;for(;p<o||m<c;){if(h>=e.length)throw new F(`a hunk ends early: ${r}`);let t=e[h];if(h+=1,t.startsWith(`\\`)){let e=l.lines.at(-1);e&&(e.noNewline=!0);continue}let n=t.slice(0,1),i=t.slice(1);if(n===`+`){u.push(`+${i}`),l.lines.push({kind:`add`,text:I(i),oldLine:null,newLine:f,noNewline:!1}),f+=1,m+=1;continue}if(n===`-`){u.push(`-${i}`),l.lines.push({kind:`remove`,text:I(i),oldLine:d,newLine:null,noNewline:!1}),d+=1,p+=1;continue}if(n===` `||t===``){l.lines.push({kind:`context`,text:I(i),oldLine:d,newLine:f,noNewline:!1}),d+=1,f+=1,p+=1,m+=1;continue}throw new F(`a hunk holds a line that is not one: ${t.slice(0,40)}`)}if(Xt.set(l,u.join(`
5
- `)),n.hunks.push(l),(e[h]??``).startsWith(`\\`)){let e=l.lines.at(-1);e&&(e.noNewline=!0),h+=1}return h}function un(e){let t=0,n=0;for(let r of e.hunks)for(let e of r.lines)e.kind===`add`&&(t+=1),e.kind===`remove`&&(n+=1);return{added:t,deleted:n}}var dn=`/usr/bin/git`,fn=3e4,pn=64*1024*1024,mn=[`core.fsmonitor=false`,`core.hooksPath=/dev/null`],hn=[`--no-color`,`--no-ext-diff`,`--no-textconv`],gn={GIT_CONFIG_NOSYSTEM:`1`,GIT_CONFIG_GLOBAL:`/dev/null`,GIT_OPTIONAL_LOCKS:`0`,LC_ALL:`C`,PATH:`/usr/bin:/bin`},_n=class extends Error{operation;said;tooMuch;overLimit;code=`upstream-failed`;constructor(e,t,n,r=!1,i=!1){super(`${e}: git ${t} failed - ${n}`),this.operation=t,this.said=n,this.tooMuch=r,this.overLimit=i,this.name=`GitFailed`}},R=class extends Error{code=`bad-request`;constructor(e,t){super(`${e}: ${t}`),this.name=`NotAllowed`}};function vn(e){return/^[0-9a-f]{40}$|^[0-9a-f]{64}$/.test(e)}function yn(e){let t=t=>new R(e,t),n=(e,n)=>{if(n===``)throw t(`${e} is empty`);if(n.startsWith(`-`))throw t(`${e} is spelled as an option: ${n}`);if(/[\0\n\r]/.test(n))throw t(`${e} holds a line break or a null byte`);return n};return{mustBeValue:n,mustBeInsideWorkspace:(e,r)=>{if(n(e,r),r.startsWith(`/`))throw t(`${e} is absolute: ${r}`);if(r.split(`/`).includes(`..`))throw t(`${e} climbs out of the workspace: ${r}`);return r},mustBePath:(e,n)=>{if(n===``)throw t(`${e} is empty`);if(n.includes(`\0`))throw t(`${e} holds a null byte`);if(n.startsWith(`/`))throw t(`${e} is absolute: ${n}`);if(n.split(`/`).includes(`..`))throw t(`${e} climbs out of the workspace: ${n}`);return n},mustBeOid:e=>{if(!vn(e))throw t(`${e} is not an object id`);return e},asCommit:e=>{if(!vn(e))throw t(`${e} is not a resolved commit`);return e},refuse:t}}function bn(e){let{tool:t,table:n,workspace:r}=e,i=[...e.config??[],...mn].flatMap(e=>[`-c`,e]),a=new Set(e.answersWithStatus??[]),o=(e,...r)=>{let a=n[e];if(!a)throw new R(t,`no git operation named ${String(e)}`);return[...i,...a(...r)]},s=(e,n)=>new Promise((i,o)=>{te(dn,n,{cwd:r,env:{...gn},encoding:`buffer`,maxBuffer:pn,timeout:fn,shell:!1,windowsHide:!0},(n,r,s)=>{let c=Buffer.isBuffer(r)?r:Buffer.from(String(r)),l=xn(s);if(!n)return i({ok:!0,out:c,said:l});let u=n,d=u.code===`ERR_CHILD_PROCESS_STDIO_MAXBUFFER`;if(u.killed||u.code===`ENOENT`||d){let n=u.killed===!0||d;return o(new _n(t,String(e),l||u.message,d,n))}i({ok:(typeof u.code==`number`?u.code:null)===1&&a.has(String(e))&&l.trim()===``,out:c,said:l})})}),c=(e,n)=>new _n(t,String(e),n||`it said nothing`);return{vector:o,async text(e,...t){let n=await s(e,o(e,...t));if(!n.ok)throw c(e,n.said);return n.out.toString(`utf8`).replace(/\n$/,``)},async bytes(e,...t){let n=await s(e,o(e,...t));if(!n.ok)throw c(e,n.said);return n.out},async ok(e,...t){return(await s(e,o(e,...t))).ok},async commit(e){let n=o(`rev-parse`,e),r=await s(`rev-parse`,n),i=r.out.toString(`utf8`).trim();if(!r.ok||!vn(i))throw new R(t,`there is no commit at ${e}`);return i}}}function xn(e){return(Buffer.isBuffer(e)?e.toString(`utf8`):String(e)).split(`
6
- `).map(e=>e.trim()).filter(Boolean)[0]??``}var{mustBeValue:z,mustBeInsideWorkspace:Sn,mustBePath:Cn,mustBeOid:wn,asCommit:Tn,refuse:En}=yn(`reviews`),Dn=[`diff.algorithm=histogram`,`diff.context=3`,`diff.interHunkContext=0`,`diff.indentHeuristic=true`,`diff.renames=true`,`diff.renameLimit=1000`,`diff.relative=false`,`diff.srcPrefix=a/`,`diff.dstPrefix=b/`,`core.quotepath=false`,`diff.noprefix=false`,`diff.mnemonicPrefix=false`,`core.autocrlf=false`,`core.bigFileThreshold=512m`,`diff.suppressBlankEmpty=false`];[...Dn,...mn];function On(e){return e.map(e=>`:(exclude,top)${Sn(`an excluded root`,e).replace(/\/+$/,``)}/`)}function kn(e,t){let n=b(t);return e.map(e=>{if(!v(e))return e;let t=ee(n,b(e));if(t===``||t.startsWith(`..`)||v(t))throw En(`the excluded root ${e} is not inside ${n}`);return t.split(x).join(`/`)})}function An(e){let t=On(e),n=(e,t)=>e===null?[t]:[`${z(`a ref`,e)}..${t}`];return{"rev-parse":e=>[`rev-parse`,`--verify`,`--quiet`,`--end-of-options`,`${z(`a ref`,e)}^{commit}`],"rev-parse-abbrev":()=>[`rev-parse`,`--abbrev-ref`,`HEAD`],"rev-parse-git-dir":()=>[`rev-parse`,`--git-dir`],"rev-parse-top":()=>[`rev-parse`,`--show-toplevel`],"for-each-ref":()=>[`for-each-ref`,`--format=%(refname:short)%09%(objectname)%09%(upstream:short)`,`--`,`refs/heads/`],"reflog-show":e=>[`reflog`,`show`,`--format=%gd%x09%gs`,`--end-of-options`,z(`a ref`,e)],"reflog-head":()=>[`reflog`,`show`,`--format=%gd%x09%gs`,`--end-of-options`,`HEAD`],"merge-base":(e,t)=>[`merge-base`,`--end-of-options`,z(`a ref`,e),z(`a ref`,t)],"merge-base-ancestor":(e,t)=>[`merge-base`,`--is-ancestor`,`--end-of-options`,z(`a ref`,e),z(`a ref`,t)],log:(e,r)=>[`log`,`--format=%H%x09%P%x09%an%x09%aI%x09%s`,`--end-of-options`,...n(e,z(`a ref`,r)),`--`,...t],"log-first-parent":(e,t)=>[`log`,`--first-parent`,`--format=%H%x09%P%x09%an%x09%aI%x09%s`,`--end-of-options`,...n(e,z(`a ref`,t))],diff:(e,n,r,i=[])=>[`diff`,...hn,`--find-renames`,`--unified=3`,`--raw`,`--patch`,`-z`,`--no-abbrev`,`--end-of-options`,...jn(e,n,r),`--`,...t,...i.map(e=>`:(exclude,top)${Cn(`an excluded path`,e)}`)],"diff-text":(e,t,n,r)=>[`diff`,...hn,`--find-renames`,`--text`,`--unified=3`,`--raw`,`--patch`,`-z`,`--no-abbrev`,`--end-of-options`,...jn(e,t,n),`--`,...Mn(r).map(e=>`:(top,literal)${Cn(`a path`,e)}`)],numstat:(e,n,r)=>[`diff`,...hn,`--find-renames`,`--numstat`,`-z`,`--end-of-options`,...jn(e,n,r),`--`,...t],"diff-no-index":e=>[`diff`,...hn,`--no-index`,`--unified=3`,`--raw`,`--patch`,`-z`,`--no-abbrev`,`--`,`/dev/null`,Cn(`a path`,e)],"ls-files-others":()=>[`ls-files`,`--others`,`--exclude-standard`,`-z`,`--`,...t],"ls-files":()=>[`ls-files`,`-z`,`--`,...t],"diff-tree":e=>[`diff-tree`,...hn,`--no-commit-id`,`--numstat`,`-r`,`--find-renames`,`--end-of-options`,z(`a commit`,e),`--`,...t],show:e=>[`show`,...hn,`--no-patch`,`--format=%H%x09%P%x09%an%x09%aI%x09%s`,`--end-of-options`,z(`a commit`,e)],status:()=>[`status`,`--porcelain`,`-z`,`--`,...t],"check-attr":e=>[`check-attr`,`-z`,`linguist-generated`,`--`,...e.map(e=>Cn(`a path`,e))],"cat-file-blob":e=>[`cat-file`,`blob`,`--end-of-options`,wn(e)],"cat-file-type":e=>[`cat-file`,`-t`,`--end-of-options`,wn(e)],"config-user-name":()=>[`config`,`--get`,`--end-of-options`,`user.name`]}}function jn(e,t,n){let r=z(`a ref`,e);if(n)return[r];if(t===null)throw En(`a diff of committed code names both ends`);return[r,z(`a ref`,t)]}function Mn(e){if(e.length===0)throw En(`a text diff names no path to read`);return e}Object.keys(An([]));function Nn(e){return bn({tool:`reviews`,table:An(kn(e.exclude,e.workspace)),workspace:e.workspace,config:Dn,answersWithStatus:[`diff-no-index`]})}function Pn(e){return/^0+$/.test(e)}function Fn(e){return!/^0+$/.test(e)}var In=`#`;function Ln(e){return e.path}function Rn(e){let t=[];for(let n of e.lines)n.kind===`add`?t.push(`+${n.text}`):n.kind===`remove`&&t.push(`-${n.text}`);return t.join(`
7
- `)}function zn(e){return Zt(e)??Rn(e)}function Bn(e,t={}){return[`old-oid ${Vn(e,`old`,t)}`,`new-oid ${Vn(e,`new`,t)}`,`old-mode ${e.oldMode}`,`new-mode ${e.newMode}`,`rename-from ${e.oldPath??``}`,`rename-to ${e.oldPath===null?``:e.path}`,`binary ${e.binary?`yes`:`no`}`].join(`
8
- `)}function Vn(e,t,n){let r=t===`old`?e.oldOid:e.newOid,i=t===`old`?e.oldMode:e.newMode;if(!Pn(r)||!Fn(i))return r;let a=n.resolveOid?.(e,t)??null;return a===null?r:`content:${a}`}function Hn(e){return S(`sha256`).update(e,`utf8`).digest(`hex`)}function Un(e,t={}){let n=[],r=[];return e.files.forEach((e,i)=>{let a=Ln(e);e.oldPath!==null&&e.oldPath!==e.path&&r.push({from:e.oldPath,to:e.path});let o=new Map,s=(e,t,r)=>{let s=(o.get(e)??0)+1;o.set(e,s);let c=`${a}${In}${e}`;n.push({id:`${c}${In}${s}`,fileKey:a,contentHash:e,ordinal:s,file:i,hunk:r,content:t,nonTextual:r===null})};if(e.hunks.length===0){let n=Bn(e,t);s(Hn(n),n,null);return}e.hunks.forEach((e,t)=>{let n=zn(e);s(Hn(n),n,t)})}),{entries:n,renames:r,fingerprint:Wn(n.map(e=>e.id))}}function Wn(e){return`sha256:${Hn([...e].sort().join(`
9
- `))}`}function Gn(e,t,n){return Kn(t,Kn(e.map(e=>({from:e.to,to:e.from})),n))}function Kn(e,t){let n=new Map(e.map(e=>[e.from,e.to])),r=new Set([t]),i=t;for(;;){let e=n.get(i);if(e===void 0||r.has(e))return i;r.add(e),i=e}}function qn(e){let t=e.lastIndexOf(In);if(t<=0)return null;let n=e.lastIndexOf(In,t-1);if(n<=0)return null;let r=Number(e.slice(t+1));return!Number.isInteger(r)||r<1?null:{fileKey:e.slice(0,n),contentHash:e.slice(n+1,t),ordinal:r}}function Jn(e,t,n){return`${e}#${t}#${n}`}function Yn(e,t){let n=e===``?[]:e.split(`
2
+ `)}\n`}var lt=2e3,ut=600*1e3;function dt(e,t){if(e===null)return!1;let n=new Date(e).getTime();return!Number.isNaN(n)&&t.getTime()-n>ut}var ft=[`reviewing`,`approved`,`closed`,`archived`],pt=[`prepare`,`process`,`sync`],mt=e=>typeof e==`object`&&!!e&&!Array.isArray(e),M=(e,t=``)=>typeof e==`string`?e:t,ht=e=>typeof e==`string`?e:null,gt=e=>typeof e==`number`&&Number.isInteger(e)&&e>=0?e:null;function _t(e,t,n,r,i){return{version:1,name:e,slug:t,selection:n,status:`reviewing`,side:`agent`,agent:{act:`prepare`,activity:`Preparing the review`,groups:null,lastPostAt:i,instruction:``},filedBy:r,created:i,log:[{at:i,said:`filed from the ${r===`form`?`form`:`terminal`}`}]}}function vt(e,t){if(e==null||e.trim()===``)return null;let n;try{n=JSON.parse(e)}catch{return null}if(!mt(n))return null;let r=mt(n.selection)?n.selection:null;if(r===null)return null;let i=mt(n.agent)?n.agent:{},a=M(n.status),o=M(i.act);return{version:typeof n.version==`number`?n.version:1,name:M(n.name,t),slug:M(n.slug,t),selection:r,status:ft.includes(a)?a:`reviewing`,side:n.side===`agent`?`agent`:`you`,agent:{act:pt.includes(o)?o:null,activity:M(i.activity),groups:gt(i.groups),lastPostAt:ht(i.lastPostAt),instruction:M(i.instruction)},filedBy:n.filedBy===`terminal`?`terminal`:`form`,created:M(n.created),log:Array.isArray(n.log)?n.log.filter(mt).map(e=>({at:M(e.at),said:M(e.said)})).filter(e=>e.said!==``):[]}}function yt(e){let t=e?.code;return typeof t==`string`&&bt.has(t)?t:null}var bt=new Set([`path-refused`,`no-entry`,`no-file`,`bad-request`,`too-large`,`upstream-failed`,`unreadable-answer`]),N=class extends Error{requested;code=`path-refused`;constructor(e,t){super(`${e}: refused path ${t}`),this.requested=t,this.name=`PathRefused`}},xt=class extends Error{requested;code=`no-entry`;constructor(e,t){super(`${e}: no directory at ${t}`),this.requested=t,this.name=`NoSuchEntry`}},St=class extends Error{requested;code=`no-file`;constructor(e,t,n=`not a file`){super(`${e}: ${n} - ${t}`),this.requested=t,this.name=`NoSuchFile`}},Ct=class extends Error{requested;size;code=`too-large`;constructor(e,t,n){super(`${e}: too large to serve - ${t}`),this.requested=t,this.size=n,this.name=`FileTooLarge`}};function wt(e){switch(yt(e)){case`path-refused`:return 403;case`no-entry`:case`no-file`:return 404;case`bad-request`:return 400;case`too-large`:return 413;case`upstream-failed`:case`unreadable-answer`:return 502}return Tt(e)?404:500}function Tt(e){let t=e?.code;return t===`ENOENT`||t===`ENOTDIR`}var Et=32*1024*1024;new Set(`txt text log json yaml yml toml ini cfg conf csv tsv xml css scss js jsx mjs cjs ts tsx py rb go rs java kt swift c h cpp hpp sh bash zsh fish sql graphql gql env diff patch lock gitignore editorconfig`.split(` `));function Dt(e){let t=e.slice(e.lastIndexOf(`/`)+1).slice(1).match(/\.([^.]+)$/);return t?t[1].toLowerCase():``}function Ot(e){return e.startsWith(`.`)}function kt(e){let t=e,n=[];for(;;)try{return y(u(t),...n)}catch{let r=_(t);if(r===t)return e;n.unshift(g(t)),t=r}}function At(e){return kt(b(e))}function jt(e){return e.split(`/`).some(e=>e!==``&&Ot(e))}function Mt(e,t,n,r){let i=n.replace(/^\/+/,``);if(v(n)||i.includes(`\0`))throw new N(e,n);let a=r===`require`;if(jt(i)!==a)throw new N(e,n);let o=kt(b(t)),s=b(o,i);if(!Ft(o,s)||!Ft(o,kt(s))||jt(ee(o,s).split(x).join(`/`))!==a)throw new N(e,n);return s}function Nt(e,t,n){return Mt(e,t,n,`refuse`)}function Pt(e,t,n){return Mt(e,t,n,`require`)}function Ft(e,t){return t===e||t.startsWith(e+x)}function It(e,t=`children`){if(t===`self`)return``;let n=e.split(`/`).filter(Boolean);return n.length<2?``:n[0]===`archive`?n.length>2?`archive/${n[1]}`:``:n[0]}var Lt={md:`text/markdown; charset=utf-8`,markdown:`text/markdown; charset=utf-8`,html:`text/html; charset=utf-8`,htm:`text/html; charset=utf-8`,css:`text/css; charset=utf-8`,js:`text/javascript; charset=utf-8`,json:`application/json; charset=utf-8`,svg:`image/svg+xml`,png:`image/png`,jpg:`image/jpeg`,jpeg:`image/jpeg`,gif:`image/gif`,webp:`image/webp`,avif:`image/avif`,bmp:`image/bmp`,ico:`image/x-icon`,pdf:`application/pdf`,txt:`text/plain; charset=utf-8`};function Rt(e){return Lt[Dt(e)]??`application/octet-stream`}var zt=Et;function Bt(e,t,n){return Ht(e,Nt(e,t,n),n)}function Vt(e,t,n,r){let i=n.replace(/^\/+/,``);if(!r(i))throw new N(e,n);return Ht(e,Pt(e,t,i),n)}function Ht(n,i,a){let c=o(i,t.O_RDONLY|t.O_NOFOLLOW);try{let e=r(c);if(!e.isFile())throw new St(n,a);if(e.size>zt)throw new Ct(n,a,e.size);return{bytes:s(c),mediaType:Rt(a),size:e.size,modified:new Date(e.mtimeMs).toISOString()}}finally{e(c)}}var Ut=`.tmp-`;function Wt(e){return e.slice(e.lastIndexOf(`/`)+1).includes(Ut)}function P(e,t){a(_(e),{recursive:!0});let n=`${e}${Ut}${process.pid}-${Math.random().toString(36).slice(2)}`;try{h(n,t),d(n,e)}finally{f(n,{force:!0})}}function Gt(e,t,n,r){P(Pt(e,t,n),r)}function Kt(e,t,n){let r=Pt(e,t,n);try{return s(r,`utf8`)}catch{return null}}function qt(e){try{let t=i(e);return{directory:t.isDirectory(),id:t.ino}}catch{return{directory:!1,id:0}}}function Jt(e,t,r={}){let i=r.entries??`children`,a=r.coalesce??`per-path`,o=r.debounceMs??120,s=r.shared??(()=>!1),l=new Map,u=null,d=new Map,f=!1,p=t=>t===``?e:`${e}/${t}`,h=()=>{u=null;let e=[...l.values()];l.clear();for(let n of e)try{t(n)}catch{}},_=e=>{if(e===``||Wt(e)||jt(e)&&!s(e))return;let t=It(e,i);if(a===`per-entry`)l.set(t,{entry:t,path:t,kind:`changed`});else{let r=n(p(e))?`changed`:`gone`;l.set(e,{entry:t,path:e,kind:r})}u||=setTimeout(h,o)},v=e=>{for(let[t,n]of d)(t===e||t.startsWith(`${e}/`))&&(n.watcher.close(),d.delete(t))},y=t=>{if(f||d.has(t))return;let r;try{r=m(p(t),(r,i)=>{if(!i)return;let a=i.toString();if(a===``)return;let o=t===``?a:`${t}/${a}`;if(a===(t===``?g(e):t.split(`/`).at(-1))&&!n(p(o)))return;let s=d.get(o),c=qt(p(o));if(s&&c.directory){if(s.id===c.id)return;v(o),y(o),_(o);return}_(o),s?v(o):c.directory&&y(o)})}catch{return}r.on(`error`,()=>v(t)),d.set(t,{watcher:r,id:qt(p(t)).id});for(let e of ee(t))y(e)},ee=e=>{try{return c(p(e),{withFileTypes:!0}).filter(e=>e.isDirectory()).map(t=>e===``?t.name:`${e}/${t.name}`)}catch{return[]}};return y(``),{refollow(){f||y(``)},close(){f=!0,u&&clearTimeout(u),u=null;for(let e of d.values())e.watcher.close();d.clear()}}}var Yt={A:`added`,C:`copied`,D:`deleted`,M:`modified`,R:`renamed`,T:`type-changed`,U:`unmerged`,X:`unknown`},F=class extends Error{code=`unreadable-answer`;constructor(e){super(`reviews: ${e}`),this.name=`UnreadableDiff`}},Xt=new WeakMap;function Zt(e){return Xt.get(e)??null}function I(e){return Buffer.from(e,`latin1`).toString(`utf8`)}function Qt(e){let t=(Buffer.isBuffer(e)?e:Buffer.from(e,`utf8`)).toString(`latin1`);if(t.trim()===``)return{files:[]};let n=[],r=t;for(;r!==``;){let e=$t(r);n.push(...e.files),r=e.rest}return{files:n}}function $t(e){let{raw:t,patch:n,rest:r}=tn(e),i=t.map(e=>({path:e.path,oldPath:e.oldPath,status:e.status,similarity:e.similarity,oldMode:e.oldMode,newMode:e.newMode,oldOid:e.oldOid,newOid:e.newOid,binary:!1,hunks:[]}));an(n,i,t);for(let e of i)e.path=I(e.path),e.oldPath=e.oldPath===null?null:I(e.oldPath);return{files:i,rest:r}}var en=/^:\d{6} \d{6} [0-9a-f]+ [0-9a-f]+ [A-Z]\d*$/;function tn(e){let t=[],n=t=>{let n=e.indexOf(`\0`,t);return n===-1?{value:e.slice(t),next:e.length}:{value:e.slice(t,n),next:n+1}},r=0;for(;;){let i=n(r);if(!i.value.startsWith(`:`))break;let a=rn(i.value),o=a.status===`renamed`||a.status===`copied`?2:1,s=[],c=i.next;for(let t=0;t<o;t+=1){if(c>=e.length)throw new F(`a raw record names no path: ${i.value}`);let t=n(c);s.push(t.value),c=t.next}t.push(o===2?{...a,oldPath:s[0],path:s[1]}:{...a,oldPath:null,path:s[0]}),r=c}for(;e[r]===`\0`;)r+=1;let i=nn(e,r);return{raw:t,patch:e.slice(r,i),rest:e.slice(i)}}function nn(e,t){let n=t;for(;n<e.length;){let r=e.indexOf(`:`,n);if(r===-1)return e.length;if(r===t||e[r-1]===`
3
+ `){let t=e.indexOf(`\0`,r),n=t===-1?e.slice(r):e.slice(r,t);if(en.test(n))return r}n=r+1}return e.length}function rn(e){let t=e.slice(1).split(` `);if(t.length!==5)throw new F(`a raw record reads wrong: ${e}`);let[n,r,i,a,o]=t,s=Yt[o.slice(0,1)];if(!s)throw new F(`a raw record carries no status: ${e}`);let c=o.slice(1);return{oldMode:n,newMode:r,oldOid:i,newOid:a,status:s,similarity:c===``?null:Number(c)}}function an(e,t,n){if(e===``)return;let r=e.split(`
4
+ `);r.at(-1)===``&&r.pop();let i=0,a=-1;for(;i<r.length;){let e=r[i];if(!e.startsWith(`diff --git `))throw new F(`the patch does not start a file at line ${i+1}`);a+=1;let o=t[a],s=n[a];if(!o||!s)throw new F(`the patch holds more files than the raw record`);on(e,s),i=cn(r,i+1,o)}if(a+1!==t.length)throw new F(`the raw record names ${t.length} files and the patch holds ${a+1}`)}function on(e,t){let n=t.oldPath??t.path,r=t.path,i=`diff --git a/${n} b/${r}`,a=`diff --git ${sn(`a/${n}`)} ${sn(`b/${r}`)}`;if(e!==i&&e!==a)throw new F(`the patch and the raw record disagree about ${r}`)}function sn(e){let t={"\x07":`\\a`,"\b":`\\b`,"\f":`\\f`,"\n":`\\n`,"\r":`\\r`," ":`\\t`,"\v":`\\v`,'"':`\\"`,"\\":`\\\\`},n=``;for(let r of e){let e=t[r];if(e){n+=e;continue}let i=r.codePointAt(0)??0;n+=i<32||i===127?`\\${i.toString(8).padStart(3,`0`)}`:r}return`"${n}"`}function cn(e,t,n){let r=t;for(;r<e.length;){let t=e[r];if(t.startsWith(`diff --git `))return r;if(t.startsWith(`@@`)){r=un(e,r,n);continue}(t.startsWith(`Binary files `)||t===`GIT binary patch`)&&(n.binary=!0),r+=1}return r}var ln=/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(?: (.*))?$/;function un(e,t,n){let r=e[t],i=ln.exec(r);if(!i)throw new F(`a hunk header reads wrong: ${r}`);let a=Number(i[1]),o=i[2]===void 0?1:Number(i[2]),s=Number(i[3]),c=i[4]===void 0?1:Number(i[4]),l={header:I(r),heading:I(i[5]??``),oldStart:a,oldLines:o,newStart:s,newLines:c,lines:[]},u=[],d=a,f=s,p=0,m=0,h=t+1;for(;p<o||m<c;){if(h>=e.length)throw new F(`a hunk ends early: ${r}`);let t=e[h];if(h+=1,t.startsWith(`\\`)){let e=l.lines.at(-1);e&&(e.noNewline=!0);continue}let n=t.slice(0,1),i=t.slice(1);if(n===`+`){u.push(`+${i}`),l.lines.push({kind:`add`,text:I(i),oldLine:null,newLine:f,noNewline:!1}),f+=1,m+=1;continue}if(n===`-`){u.push(`-${i}`),l.lines.push({kind:`remove`,text:I(i),oldLine:d,newLine:null,noNewline:!1}),d+=1,p+=1;continue}if(n===` `||t===``){l.lines.push({kind:`context`,text:I(i),oldLine:d,newLine:f,noNewline:!1}),d+=1,f+=1,p+=1,m+=1;continue}throw new F(`a hunk holds a line that is not one: ${t.slice(0,40)}`)}if(Xt.set(l,u.join(`
5
+ `)),n.hunks.push(l),(e[h]??``).startsWith(`\\`)){let e=l.lines.at(-1);e&&(e.noNewline=!0),h+=1}return h}function dn(e){let t=0,n=0;for(let r of e.hunks)for(let e of r.lines)e.kind===`add`&&(t+=1),e.kind===`remove`&&(n+=1);return{added:t,deleted:n}}var fn=`/usr/bin/git`,pn=3e4,mn=64*1024*1024,hn=[`core.fsmonitor=false`,`core.hooksPath=/dev/null`],gn=[`--no-color`,`--no-ext-diff`,`--no-textconv`],_n={GIT_CONFIG_NOSYSTEM:`1`,GIT_CONFIG_GLOBAL:`/dev/null`,GIT_OPTIONAL_LOCKS:`0`,LC_ALL:`C`,PATH:`/usr/bin:/bin`},vn=class extends Error{operation;said;tooMuch;overLimit;code=`upstream-failed`;constructor(e,t,n,r=!1,i=!1){super(`${e}: git ${t} failed - ${n}`),this.operation=t,this.said=n,this.tooMuch=r,this.overLimit=i,this.name=`GitFailed`}},L=class extends Error{code=`bad-request`;constructor(e,t){super(`${e}: ${t}`),this.name=`NotAllowed`}};function yn(e){return/^[0-9a-f]{40}$|^[0-9a-f]{64}$/.test(e)}function bn(e){let t=t=>new L(e,t),n=(e,n)=>{if(n===``)throw t(`${e} is empty`);if(n.startsWith(`-`))throw t(`${e} is spelled as an option: ${n}`);if(/[\0\n\r]/.test(n))throw t(`${e} holds a line break or a null byte`);return n};return{mustBeValue:n,mustBeInsideWorkspace:(e,r)=>{if(n(e,r),r.startsWith(`/`))throw t(`${e} is absolute: ${r}`);if(r.split(`/`).includes(`..`))throw t(`${e} climbs out of the workspace: ${r}`);return r},mustBePath:(e,n)=>{if(n===``)throw t(`${e} is empty`);if(n.includes(`\0`))throw t(`${e} holds a null byte`);if(n.startsWith(`/`))throw t(`${e} is absolute: ${n}`);if(n.split(`/`).includes(`..`))throw t(`${e} climbs out of the workspace: ${n}`);return n},mustBeOid:e=>{if(!yn(e))throw t(`${e} is not an object id`);return e},asCommit:e=>{if(!yn(e))throw t(`${e} is not a resolved commit`);return e},refuse:t}}function xn(e){let{tool:t,table:n,workspace:r}=e,i=[...e.config??[],...hn].flatMap(e=>[`-c`,e]),a=new Set(e.answersWithStatus??[]),o=(e,...r)=>{let a=n[e];if(!a)throw new L(t,`no git operation named ${String(e)}`);return[...i,...a(...r)]},s=(e,n)=>new Promise((i,o)=>{te(fn,n,{cwd:r,env:{..._n},encoding:`buffer`,maxBuffer:mn,timeout:pn,shell:!1,windowsHide:!0},(n,r,s)=>{let c=Buffer.isBuffer(r)?r:Buffer.from(String(r)),l=Sn(s);if(!n)return i({ok:!0,out:c,said:l});let u=n,d=u.code===`ERR_CHILD_PROCESS_STDIO_MAXBUFFER`;if(u.killed||u.code===`ENOENT`||d){let n=u.killed===!0||d;return o(new vn(t,String(e),l||u.message,d,n))}i({ok:(typeof u.code==`number`?u.code:null)===1&&a.has(String(e))&&l.trim()===``,out:c,said:l})})}),c=(e,n)=>new vn(t,String(e),n||`it said nothing`);return{vector:o,async text(e,...t){let n=await s(e,o(e,...t));if(!n.ok)throw c(e,n.said);return n.out.toString(`utf8`).replace(/\n$/,``)},async bytes(e,...t){let n=await s(e,o(e,...t));if(!n.ok)throw c(e,n.said);return n.out},async ok(e,...t){return(await s(e,o(e,...t))).ok},async commit(e){let n=o(`rev-parse`,e),r=await s(`rev-parse`,n),i=r.out.toString(`utf8`).trim();if(!r.ok||!yn(i))throw new L(t,`there is no commit at ${e}`);return i}}}function Sn(e){return(Buffer.isBuffer(e)?e.toString(`utf8`):String(e)).split(`
6
+ `).map(e=>e.trim()).filter(Boolean)[0]??``}var{mustBeValue:R,mustBeInsideWorkspace:Cn,mustBePath:wn,mustBeOid:Tn,asCommit:En,refuse:Dn}=bn(`reviews`),On=[`diff.algorithm=histogram`,`diff.context=3`,`diff.interHunkContext=0`,`diff.indentHeuristic=true`,`diff.renames=true`,`diff.renameLimit=1000`,`diff.relative=false`,`diff.srcPrefix=a/`,`diff.dstPrefix=b/`,`core.quotepath=false`,`diff.noprefix=false`,`diff.mnemonicPrefix=false`,`core.autocrlf=false`,`core.bigFileThreshold=512m`,`diff.suppressBlankEmpty=false`];[...On,...hn];function kn(e){return e.map(e=>`:(exclude,top)${Cn(`an excluded root`,e).replace(/\/+$/,``)}/`)}function An(e,t){let n=b(t);return e.map(e=>{if(!v(e))return e;let t=ee(n,b(e));if(t===``||t.startsWith(`..`)||v(t))throw Dn(`the excluded root ${e} is not inside ${n}`);return t.split(x).join(`/`)})}function jn(e){let t=kn(e),n=(e,t)=>e===null?[t]:[`${R(`a ref`,e)}..${t}`];return{"rev-parse":e=>[`rev-parse`,`--verify`,`--quiet`,`--end-of-options`,`${R(`a ref`,e)}^{commit}`],"rev-parse-abbrev":()=>[`rev-parse`,`--abbrev-ref`,`HEAD`],"rev-parse-git-dir":()=>[`rev-parse`,`--git-dir`],"rev-parse-top":()=>[`rev-parse`,`--show-toplevel`],"for-each-ref":()=>[`for-each-ref`,`--format=%(refname:short)%09%(objectname)%09%(upstream:short)`,`--`,`refs/heads/`],"reflog-show":e=>[`reflog`,`show`,`--format=%gd%x09%gs`,`--end-of-options`,R(`a ref`,e)],"reflog-head":()=>[`reflog`,`show`,`--format=%gd%x09%gs`,`--end-of-options`,`HEAD`],"merge-base":(e,t)=>[`merge-base`,`--end-of-options`,R(`a ref`,e),R(`a ref`,t)],"merge-base-ancestor":(e,t)=>[`merge-base`,`--is-ancestor`,`--end-of-options`,R(`a ref`,e),R(`a ref`,t)],log:(e,r)=>[`log`,`--format=%H%x09%P%x09%an%x09%aI%x09%s`,`--end-of-options`,...n(e,R(`a ref`,r)),`--`,...t],"log-first-parent":(e,t)=>[`log`,`--first-parent`,`--format=%H%x09%P%x09%an%x09%aI%x09%s`,`--end-of-options`,...n(e,R(`a ref`,t))],diff:(e,n,r,i=[])=>[`diff`,...gn,`--find-renames`,`--unified=3`,`--raw`,`--patch`,`-z`,`--no-abbrev`,`--end-of-options`,...Mn(e,n,r),`--`,...t,...i.map(e=>`:(exclude,top)${wn(`an excluded path`,e)}`)],"diff-text":(e,t,n,r)=>[`diff`,...gn,`--find-renames`,`--text`,`--unified=3`,`--raw`,`--patch`,`-z`,`--no-abbrev`,`--end-of-options`,...Mn(e,t,n),`--`,...Nn(r).map(e=>`:(top,literal)${wn(`a path`,e)}`)],numstat:(e,n,r)=>[`diff`,...gn,`--find-renames`,`--numstat`,`-z`,`--end-of-options`,...Mn(e,n,r),`--`,...t],"diff-no-index":e=>[`diff`,...gn,`--no-index`,`--unified=3`,`--raw`,`--patch`,`-z`,`--no-abbrev`,`--`,`/dev/null`,wn(`a path`,e)],"ls-files-others":()=>[`ls-files`,`--others`,`--exclude-standard`,`-z`,`--`,...t],"ls-files":()=>[`ls-files`,`-z`,`--`,...t],"diff-tree":e=>[`diff-tree`,...gn,`--no-commit-id`,`--numstat`,`-r`,`--find-renames`,`--end-of-options`,R(`a commit`,e),`--`,...t],show:e=>[`show`,...gn,`--no-patch`,`--format=%H%x09%P%x09%an%x09%aI%x09%s`,`--end-of-options`,R(`a commit`,e)],status:()=>[`status`,`--porcelain`,`-z`,`--`,...t],"check-attr":e=>[`check-attr`,`-z`,`linguist-generated`,`--`,...e.map(e=>wn(`a path`,e))],"cat-file-blob":e=>[`cat-file`,`blob`,`--end-of-options`,Tn(e)],"cat-file-type":e=>[`cat-file`,`-t`,`--end-of-options`,Tn(e)],"config-user-name":()=>[`config`,`--get`,`--end-of-options`,`user.name`]}}function Mn(e,t,n){let r=R(`a ref`,e);if(n)return[r];if(t===null)throw Dn(`a diff of committed code names both ends`);return[r,R(`a ref`,t)]}function Nn(e){if(e.length===0)throw Dn(`a text diff names no path to read`);return e}Object.keys(jn([]));function Pn(e){return xn({tool:`reviews`,table:jn(An(e.exclude,e.workspace)),workspace:e.workspace,config:On,answersWithStatus:[`diff-no-index`]})}function Fn(e){return/^0+$/.test(e)}function In(e){return!/^0+$/.test(e)}var Ln=`#`;function Rn(e){return e.path}function zn(e){let t=[];for(let n of e.lines)n.kind===`add`?t.push(`+${n.text}`):n.kind===`remove`&&t.push(`-${n.text}`);return t.join(`
7
+ `)}function Bn(e){return Zt(e)??zn(e)}function Vn(e,t={}){return[`old-oid ${Hn(e,`old`,t)}`,`new-oid ${Hn(e,`new`,t)}`,`old-mode ${e.oldMode}`,`new-mode ${e.newMode}`,`rename-from ${e.oldPath??``}`,`rename-to ${e.oldPath===null?``:e.path}`,`binary ${e.binary?`yes`:`no`}`].join(`
8
+ `)}function Hn(e,t,n){let r=t===`old`?e.oldOid:e.newOid,i=t===`old`?e.oldMode:e.newMode;if(!Fn(r)||!In(i))return r;let a=n.resolveOid?.(e,t)??null;return a===null?r:`content:${a}`}function Un(e){return S(`sha256`).update(e,`utf8`).digest(`hex`)}function Wn(e,t={}){let n=[],r=[];return e.files.forEach((e,i)=>{let a=Rn(e);e.oldPath!==null&&e.oldPath!==e.path&&r.push({from:e.oldPath,to:e.path});let o=new Map,s=(e,t,r)=>{let s=(o.get(e)??0)+1;o.set(e,s);let c=`${a}${Ln}${e}`;n.push({id:`${c}${Ln}${s}`,fileKey:a,contentHash:e,ordinal:s,file:i,hunk:r,content:t,nonTextual:r===null})};if(e.hunks.length===0){let n=Vn(e,t);s(Un(n),n,null);return}e.hunks.forEach((e,t)=>{let n=Bn(e);s(Un(n),n,t)})}),{entries:n,renames:r,fingerprint:Gn(n.map(e=>e.id))}}function Gn(e){return`sha256:${Un([...e].sort().join(`
9
+ `))}`}function Kn(e,t,n){return qn(t,qn(e.map(e=>({from:e.to,to:e.from})),n))}function qn(e,t){let n=new Map(e.map(e=>[e.from,e.to])),r=new Set([t]),i=t;for(;;){let e=n.get(i);if(e===void 0||r.has(e))return i;r.add(e),i=e}}function Jn(e){let t=e.lastIndexOf(Ln);if(t<=0)return null;let n=e.lastIndexOf(Ln,t-1);if(n<=0)return null;let r=Number(e.slice(t+1));return!Number.isInteger(r)||r<1?null:{fileKey:e.slice(0,n),contentHash:e.slice(n+1,t),ordinal:r}}function Yn(e,t,n){return`${e}#${t}#${n}`}function Xn(e,t){let n=e===``?[]:e.split(`
10
10
  `),r=t===``?[]:t.split(`
11
- `);if(n.length===0&&r.length===0)return 0;let i=new Map;for(let e of n)i.set(e,(i.get(e)??0)+1);let a=0;for(let e of r){let t=i.get(e)??0;t>0&&(i.set(e,t-1),a+=1)}return 1-a/Math.max(n.length,r.length)}function Xn(e,t,n=e=>e){let r=new Set(t.map(e=>e.id)),i=e.filter(e=>!r.has(Jn(n(e.fileKey),e.contentHash,e.ordinal))),a=new Set(e.map(e=>Jn(n(e.fileKey),e.contentHash,e.ordinal))),o=t.filter(e=>!a.has(e.id));if(i.length===0||o.length===0)return[];let s=new Map,c=new Map,l=new Map,u=(e,t,n)=>{let r=e.get(t);r===void 0?e.set(t,[n]):r.push(n)};for(let e of o){if(e.content===``){l.set(e,0),u(c,e.fileKey,e);continue}let t=e.content.split(`
11
+ `);if(n.length===0&&r.length===0)return 0;let i=new Map;for(let e of n)i.set(e,(i.get(e)??0)+1);let a=0;for(let e of r){let t=i.get(e)??0;t>0&&(i.set(e,t-1),a+=1)}return 1-a/Math.max(n.length,r.length)}function Zn(e,t,n=e=>e){let r=new Set(t.map(e=>e.id)),i=e.filter(e=>!r.has(Yn(n(e.fileKey),e.contentHash,e.ordinal))),a=new Set(e.map(e=>Yn(n(e.fileKey),e.contentHash,e.ordinal))),o=t.filter(e=>!a.has(e.id));if(i.length===0||o.length===0)return[];let s=new Map,c=new Map,l=new Map,u=(e,t,n)=>{let r=e.get(t);r===void 0?e.set(t,[n]):r.push(n)};for(let e of o){if(e.content===``){l.set(e,0),u(c,e.fileKey,e);continue}let t=e.content.split(`
12
12
  `);l.set(e,t.length);for(let n of new Set(t))u(s,`${e.fileKey}\n${n}`,e)}let d=[],f=(e,t)=>Math.abs(e.ordinal-t.ordinal)<=3;for(let e of i){let t=n(e.fileKey);if(e.content===``){for(let n of c.get(t)??[])f(e,n)&&d.push({from:e.id,to:n.id,cost:0});continue}let r=e.content.split(`
13
- `),i=new Map;for(let e of r)for(let n of s.get(`${t}\n${e}`)??[])i.set(n,(i.get(n)??0)+1);for(let[t,n]of i){if(!f(e,t)||n*2<=Math.max(r.length,l.get(t)??0))continue;let i=Yn(e.content,t.content);i>=.5||d.push({from:e.id,to:t.id,cost:i})}}d.sort((e,t)=>e.cost-t.cost||e.from.localeCompare(t.from)||e.to.localeCompare(t.to));let p=new Set,m=new Set,h=[];for(let e of d)p.has(e.from)||m.has(e.to)||(p.add(e.from),m.add(e.to),h.push(e));return h}function Zn(e){return!vn(e.head)}var Qn=[`main`,`master`];function $n(e){return Qn.includes(e)}var er=[`4b825dc642cb6eb9a060e54bf8d69288fbee4904`,`6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321`];async function tr(e){for(let t of er)if(await e.text(`cat-file-type`,t).catch(()=>``)===`tree`)return t;throw new R(`reviews`,`this repository has no empty tree to diff a first commit against`)}async function nr(e){return e.ok(`rev-parse-git-dir`)}async function rr(e){let t=await e.text(`rev-parse-abbrev`).catch(()=>``);return t===``||t===`HEAD`?null:t}async function ir(e){let t=await e.text(`for-each-ref`).catch(()=>``),n=[];for(let e of t.split(`
14
- `)){if(e===``)continue;let[t=``,r=``,i=``]=e.split(` `);t===``||r===``||n.push({name:t,tip:r,upstream:i===``?null:i})}return n}function ar(e){let t=[];for(let n of e.split(`
15
- `)){if(n===``)continue;let e=n.indexOf(` `);e!==-1&&t.push({at:n.slice(0,e),said:n.slice(e+1)})}return t}async function or(e,t){if($n(t))return null;let n=[],r=await ir(e),i=r.find(e=>e.name===t)?.upstream??null,a=e=>sr(e,t,i,new Set(r.map(e=>e.name))),o=ar(await e.text(`reflog-show`,t).catch(()=>``)).at(-1)?.said??``,s=/^branch: Created from (.+)$/.exec(o)?.[1];s!==void 0&&s!==`HEAD`&&n.push({ref:s,foundBy:`branch-reflog`});let c=ar(await e.text(`reflog-head`).catch(()=>``));for(let e of[...c].reverse()){let r=/^checkout: moving from (.+) to (.+)$/.exec(e.said);if(r&&r[2]===t&&r[1]!==void 0&&r[1]!==t){n.push({ref:r[1],foundBy:`head-reflog`});break}}i!==null&&n.push({ref:i,foundBy:`upstream`});for(let e of Qn)n.push({ref:e,foundBy:`merge-base`});for(let r of n){if(a(r.ref))continue;let n=await cr(e,r.ref,t);if(n!==null)return{ref:r.ref,hash:n,foundBy:r.foundBy}}return null}function sr(e,t,n,r){return e===t||e===`refs/heads/${t}`?!0:e.endsWith(`/${t}`)?e.startsWith(`refs/remotes/`)?!0:e===n&&!r.has(e):!1}async function cr(e,t,n){let r=(await e.text(`merge-base`,t,n).catch(()=>``)).trim();return vn(r)?r:null}async function lr(e,t,n){return e.ok(`merge-base-ancestor`,t,n)}function ur(e){let t=(Buffer.isBuffer(e)?e.toString(`utf8`):e).split(`\0`),n=[];for(let e=0;e<t.length;e+=1){let r=t[e];if(r===void 0||r===``)continue;let i=r.slice(0,2),a=r.slice(3);if(a===``)continue;let o=i.includes(`R`)||i.includes(`C`),s=o?t[e+1]??null:null;o&&(e+=1),n.push({code:i,path:a,oldPath:s,untracked:i===`??`})}return n}async function dr(e){return ur(await e.bytes(`status`))}async function fr(e){return(await e.bytes(`ls-files-others`)).toString(`utf8`).split(`\0`).filter(e=>e!==``)}function pr(e){let t=[];for(let n of e.split(`
16
- `)){if(n===``)continue;let e=n.split(` `);e.length<5||t.push({hash:e[0]??``,parents:(e[1]??``).split(` `).filter(e=>e!==``),author:e[2]??``,date:e[3]??``,subject:e.slice(4).join(` `)})}return t}async function mr(e,t,n){let r=hr(n.base)?null:n.base,i=Zn(t)?`log`:`log-first-parent`;return pr(await e.text(i,r,n.head).catch(()=>``))}function hr(e){return er.includes(e)}async function gr(e,t){let n=await vr(e,t);if(Zn(t))return{base:n,head:await e.commit(t.head),tree:t.uncommitted,extended:!1,rewritten:!1};if(t.followed!==null)return{base:n,head:t.followed,tree:!1,extended:!1,rewritten:!1};let r=await e.commit(`HEAD`).catch(()=>null);if(!(r!==null&&await lr(e,t.head,r)))return{base:n,head:t.head,tree:t.uncommitted,extended:!1,rewritten:r!==null};let i=await _r(e,t.head,r),a=i!==t.head;return{base:n,head:i,tree:t.uncommitted,extended:a,rewritten:!1}}async function _r(e,t,n){if(t===n)return n;let r=pr(await e.text(`log-first-parent`,t,n).catch(()=>``)),i=t;for(let e of[...r].reverse()){if(e.parents.length>1)return i;i=e.hash}return i}async function vr(e,t){return!((t.kind===`branch`||t.kind===`other-branch`)&&t.baseRef!==null&&t.baseFoundBy!==`chosen`)||t.baseRef===null?t.base:await cr(e,t.baseRef,t.head)??t.base}async function yr(e,t){let n=t.base===void 0?null:await e.commit(t.base);if(t.kind===`branch`||t.kind===`other-branch`){let r=t.branch??await rr(e);if(r===null)throw new R(`reviews`,`there is no branch checked out`);let i=await e.commit(r),a=n===null?await or(e,r):{ref:t.base??null,hash:n,foundBy:`chosen`};if(a===null)throw new R(`reviews`,$n(r)?`${r} is the main line and has no base`:`the base of ${r} could not be found`);if(a.hash===i)throw new R(`reviews`,`${r} holds nothing its base does not`);return{kind:t.kind,base:a.hash,baseRef:a.ref,baseFoundBy:a.foundBy,head:r,uncommitted:t.kind===`branch`?t.uncommitted??!1:!1,followed:null}}if(t.kind===`range`){if(t.from===void 0||t.to===void 0)throw new R(`reviews`,`a range names both of its ends`);let r=n??await e.commit(t.from),i=await e.commit(t.to);if(r===i)throw new R(`reviews`,`the range is empty`);return{kind:`range`,base:r,baseRef:n===null?t.from:t.base??null,baseFoundBy:n===null?`range`:`chosen`,head:i,uncommitted:t.uncommitted??!1,followed:null}}let r=t.kind===`last-commit`?`HEAD`:t.commit??``;if(r===``)throw new R(`reviews`,`no commit was named`);let i=await e.commit(r);if(n!==null)return{kind:t.kind,base:n,baseRef:t.base??null,baseFoundBy:`chosen`,head:i,uncommitted:t.uncommitted??!1,followed:null};let a=await e.commit(`${i}^`).catch(()=>null);return{kind:t.kind,base:a??await tr(e),baseRef:null,baseFoundBy:a===null?`root`:`parent`,head:i,uncommitted:t.uncommitted??!1,followed:null}}async function br(e,t){let n=await e.commit(t),r=await e.commit(`${n}^`).catch(()=>null);return{kind:`commit`,base:r??await tr(e),baseRef:null,baseFoundBy:r===null?`root`:`parent`,head:n,uncommitted:!1,followed:n}}async function xr(e,t){let n=await gr(e,t),r=L(await e.bytes(`diff`,n.base,n.tree?null:n.head,n.tree)),i=r.files.length,a=0,o=0,s=0;for(let e of r.files){a+=e.hunks.length===0?1:e.hunks.length;let t=un(e);o+=t.added,s+=t.deleted}if(n.tree)for(let t of await fr(e)){let n=L(await e.bytes(`diff-no-index`,t));for(let e of n.files){let t=un(e);a+=e.hunks.length===0?1:e.hunks.length,o+=t.added,s+=t.deleted}i+=n.files.length}return{files:i,hunks:a,added:o,deleted:s,commits:await mr(e,t,n),uncommittedFiles:t.uncommitted?(await dr(e)).length:0}}async function Sr(e){let t={options:[],notOffered:null};if(!await nr(e)||await e.commit(`HEAD`).catch(()=>null)===null)return t;let n=[],r=(await dr(e)).length,i=await rr(e),a=async t=>{try{return n.push(await wr(e,t,r)),null}catch(e){return e instanceof Error?e:Error(String(e))}},o=null;if(i!==null){let e=await a({kind:`branch`,uncommitted:r>0});e!==null&&(o={branch:i,reason:e.message})}await a({kind:`last-commit`});for(let t of await ir(e))t.name!==i&&await a({kind:`other-branch`,branch:t.name});return{options:n,notOffered:o}}async function Cr(e,t){return wr(e,t,(await dr(e)).length)}async function wr(e,t,n){let r=await yr(e,t);return{kind:r.kind,label:Tr(r),selection:r,summary:await xr(e,r),uncommitted:{offered:r.kind===`branch`,files:n}}}function Tr(e){let t=e=>e.slice(0,12);return e.kind===`branch`||e.kind===`other-branch`?`${e.head} against ${e.baseRef??t(e.base)}`:e.kind===`last-commit`?`the last commit, ${t(e.head)}`:e.kind===`range`?`${t(e.base)}..${t(e.head)}`:`commit ${t(e.head)}`}var Er=[`package-lock.json`,`pnpm-lock.yaml`,`yarn.lock`,`Cargo.lock`,`go.sum`,`poetry.lock`,`Gemfile.lock`,`composer.lock`],Dr=200;async function Or(e,t){let n=new Set;for(let r=0;r<t.length;r+=Dr){let i=(await e.text(`check-attr`,t.slice(r,r+Dr))).split(`\0`);for(let e=0;e+2<i.length;e+=3){let t=i[e],r=i[e+2];t!==void 0&&t!==``&&r===`set`&&n.add(t)}}return n}function kr(e,t,n){if(e.binary)return`binary`;let r=e.path.split(`/`).pop()??e.path;return Er.includes(r)||t.has(e.path)?`generated`:n.added+n.deleted>5e3?`oversize`:`text`}async function Ar(e,t){let n=t.filter(e=>!e.binary).map(e=>e.path),r=n.length===0?new Set:await Or(e,n),i=new Map;for(let e of t){let t=un(e),n=kr(e,r,t);i.set(e,{category:n,added:t.added,deleted:t.deleted}),n!==`text`&&(e.hunks=[])}return i}var jr={hunks:2e3,patchBytes:20*1024*1024,blobBytes:100*1024*1024},Mr=class extends Error{code=`too-large`;constructor(e){super(`reviews: ${e}`),this.name=`TooLarge`}};function Nr(e){return S(`sha256`).update(e).digest(`hex`)}function Pr(e,t){if(t===``||t.startsWith(`/`)||t.split(`/`).includes(`..`))throw new R(`reviews`,`${t} is not a path inside the workspace`);return y(e,t)}function Fr(e,t){try{return i(y(e,t)).isFile()}catch{return!1}}async function Ir(e,t,n){let r={...jr,...n.limits},i=await gr(e,t),a=await Lr(e,i,n.workspace,r.blobBytes).catch(e=>{throw e instanceof _n&&e.tooMuch?new Mr(`this selection holds more than ${B(pn)} of diff; a narrower selection will open`):e}),o=a.parsed,s=[...a.patches],c=new Set;if(i.tree)for(let t of(await fr(e)).sort()){if(!Fr(n.workspace,t))continue;let r=await e.bytes(`diff-no-index`,t);s.push(r);for(let e of L(r).files)o.files.push(e),c.add(e.path)}let l=s.reduce((e,t)=>e+t.length,0);if(l>r.patchBytes)throw new Mr(`this selection holds ${B(l)} of diff, past the limit of ${B(r.patchBytes)}; a narrower selection will open`);let u=await Ar(e,o.files),d=await Hr(e,o,n.workspace,r.blobBytes,i.tree),f=Un(o,{resolveOid:(e,t)=>d.hashes.get(e)?.[t]??null});if(f.entries.length>r.hunks)throw new Mr(`this selection holds ${f.entries.length} hunks, past the limit of ${r.hunks}; a narrower selection will open`);let p=o.files.map(e=>{let t=u.get(e)??{category:`text`,...un(e)},n=d.stored.get(e)??{old:null,new:null},r=e=>e===null?null:d.blobs.get(e)?.length??null;return{path:e.path,oldPath:e.oldPath,status:e.status,similarity:e.similarity,oldMode:e.oldMode,newMode:e.newMode,oldOid:e.oldOid,newOid:e.newOid,binary:e.binary,category:t.category,oldBlob:n.old,newBlob:n.new,oldBytes:r(n.old),newBytes:r(n.new),added:t.added,deleted:t.deleted,hunks:e.hunks,untracked:c.has(e.path)}}),m=f.entries.map(e=>({id:e.id,fileKey:e.fileKey,contentHash:e.contentHash,ordinal:e.ordinal,file:e.file,hunk:e.hunk,facts:e.nonTextual?e.content:null}));return{record:{version:1,computedAt:new Date().toISOString(),selection:t,resolved:i,commits:await mr(e,t,i),tree:i.tree?await Wr(e,o,n.workspace,r.blobBytes):null,files:p,entries:m,renames:f.renames,fingerprint:f.fingerprint,totals:{files:p.length,hunks:m.length,added:p.reduce((e,t)=>e+t.added,0),deleted:p.reduce((e,t)=>e+t.deleted,0),patchBytes:l,blobBytes:d.bytes}},patch:Buffer.concat(s),blobs:d.blobs}}function B(e){return`${(e/(1024*1024)).toFixed(1)} MB`}async function Lr(e,t,n,r){let i=t.tree?null:t.head,a=await e.bytes(`diff`,t.base,i,t.tree),o=L(a),s=[];for(let i of o.files)i.binary&&await Rr(e,i,n,r,t.tree)&&(i.oldPath!==null&&s.push(i.oldPath),s.push(i.path));if(s.length===0)return{parsed:o,patches:[a]};let c=await e.bytes(`diff`,t.base,i,t.tree,s),l=await e.bytes(`diff-text`,t.base,i,t.tree,s);return{parsed:{files:[...L(c).files,...L(l).files]},patches:[c,l]}}async function Rr(e,t,n,r,i){for(let a of[`old`,`new`]){let o=a===`old`?t.oldMode:t.newMode;if(/^0+$/.test(o)||o===`160000`)continue;let s=await zr(e,t,a,n,r,i).catch(()=>null);if(s===null||s.subarray(0,Vr).includes(0))return!1}return!0}async function zr(e,t,n,r,i,a){let o=n===`old`?t.oldOid:t.newOid;return Br(n,o,a)?Ur(r,n===`old`?t.oldPath??t.path:t.path,i):e.bytes(`cat-file-blob`,o)}function Br(e,t,n){return n&&e===`new`||/^0+$/.test(t)}var Vr=8e3;async function Hr(e,t,n,r,i){let a={blobs:new Map,stored:new Map,hashes:new Map,bytes:0};for(let o of t.files){let t={old:null,new:null},s={old:null,new:null};for(let c of[`old`,`new`]){let l=c===`old`?o.oldOid:o.newOid,u=c===`old`?o.oldMode:o.newMode;if(/^0+$/.test(u)||u===`160000`)continue;let d=Br(c,l,i),f=await zr(e,o,c,n,r,i);if(f===null)continue;let p=d?Nr(f):l;if(d&&(s[c]=p),t[c]=p,!a.blobs.has(p)){if(a.bytes+=f.length,a.bytes>r)throw new Mr(`this selection holds ${B(a.bytes)} of file content, past the limit of ${B(r)}; a narrower selection will open`);a.blobs.set(p,f)}}a.stored.set(o,t),a.hashes.set(o,s)}return a}function Ur(e,t,n){let r=Pr(e,t),a;try{a=i(r)}catch{return null}if(a.isSymbolicLink())return Buffer.from(l(r),`utf8`);if(!a.isFile())return null;if(a.size>n)throw new Mr(`${t} is ${B(a.size)}, past the limit of ${B(n)} for one file; a narrower selection will open`);return s(r)}async function Wr(e,t,n,r){let a=(await e.bytes(`status`)).toString(`utf8`),o=[],s=new Set;for(let e of t.files){if(s.has(e.path)||/^0+$/.test(e.newMode)||e.newMode===`160000`)continue;s.add(e.path);let t=Pr(n,e.path),a=Ur(n,e.path,r);if(a===null)continue;let c;try{c=i(t)}catch{continue}o.push({path:e.path,size:a.length,mtimeMs:c.mtimeMs,hash:Nr(a)})}return{porcelain:a,files:o}}function Gr(e){return y(e,k)}function Kr(e){return y(Gr(e),j)}function qr(e){return y(Kr(e),nt)}function Jr(e,t){let n=Gr(e),r=y(n,Ze);f(r,{recursive:!0,force:!0}),a(y(r,tt),{recursive:!0});for(let[e,n]of t.blobs)P(ei(r,e),n);P(y(r,et),t.patch),P(y(r,$e),`${JSON.stringify(t.record,null,2)}\n`),Yr(n)}function Yr(e){let t=y(e,j),r=y(e,Ze),i=y(e,Qe);n(t)&&(f(i,{recursive:!0,force:!0}),d(t,i)),d(r,t),f(i,{recursive:!0,force:!0})}function Xr(e){let t=Gr(e),r=y(t,j),i=y(t,Ze),a=y(t,Qe),o=n(y(i,$e));if(n(r)){f(a,{recursive:!0,force:!0}),o||f(i,{recursive:!0,force:!0});return}if(o){Yr(t);return}f(i,{recursive:!0,force:!0}),n(a)&&d(a,r)}function Zr(e){let t=y(Gr(e),j,$e);if(!n(t))return null;let r;try{r=JSON.parse(s(t,`utf8`))}catch{return null}if(!Qr(r))return null;try{Tn(r.resolved.base),Tn(r.resolved.head)}catch{return null}return r}function Qr(e){if(typeof e!=`object`||!e||!Array.isArray(e.files)||!Array.isArray(e.entries)||!Array.isArray(e.commits))return!1;let t=e.resolved;return typeof t==`object`&&!!t&&typeof t.base==`string`&&typeof t.head==`string`&&typeof e.fingerprint==`string`&&typeof e.selection==`object`&&e.selection!==null}function $r(e){if(!/^[0-9a-f]{40}$|^[0-9a-f]{64}$/.test(e))throw new N(`reviews`,e);return`${k}/${j}/${tt}/${e}`}function ei(e,t){if(!/^[0-9a-f]{40}$|^[0-9a-f]{64}$/.test(t))throw new N(`reviews`,t);return y(e,tt,t)}function ti(e){return e.entries.map(t=>({id:t.id,fileKey:t.fileKey,contentHash:t.contentHash,ordinal:t.ordinal,content:ni(e,t)}))}function ni(e,t){if(t.facts!==null)return t.facts;let n=t.hunk===null?void 0:e.files[t.file]?.hunks[t.hunk];return n===void 0?``:Rn(n)}function ri(e,t){let n=new Set(t.entries.map(e=>e.id)),r=ai(e,t),i=new Map,a=new Set;for(let t of e.entries){let e=ii(r(t.fileKey),t.contentHash,t.ordinal);n.has(e)&&(i.set(t.id,e),a.add(e))}let o=Xn(ti(e),ti(t),r),s=new Map;for(let e of o)i.set(e.from,e.to),s.set(e.to,e.from);let c=new Set(i.values()),l=new Set;for(let n of oi(e,t,r)){let e=i.get(n);e!==void 0&&l.add(e)}return{lineage:i,same:a,paired:s,vanished:e.entries.filter(e=>!i.has(e.id)).map(e=>e.id),arrived:t.entries.filter(e=>!c.has(e.id)).map(e=>e.id),reset:l}}function ii(e,t,n){return`${e}#${t}#${n}`}function ai(e,t){let n=new Map;return r=>{let i=n.get(r);if(i!==void 0)return i;let a=Gn(e.renames,t.renames,r);return n.set(r,a),a}}function oi(e,t,n){let r=(e,t)=>{let n=new Map;for(let r of e.entries){let e=`${t(r.fileKey)}#${r.contentHash}`;n.set(e,(n.get(e)??0)+1)}return n},i=e=>e,a=r(e,n),o=r(t,i),s=new Set;for(let t of e.entries){let e=`${n(t.fileKey)}#${t.contentHash}`;(a.get(e)??0)!==(o.get(e)??0)&&s.add(t.id)}return s}var si={done:`Agent done`,sync:`Synced`,stop:`Stopped`,follow:`Followed`};function ci(e,t){let n=[];return t.changedSeen>0&&n.push(t.changedSeen===1?`1 hunk changed since you saw it`:`${t.changedSeen} hunks changed since you saw them`),t.threadsAnswered>0&&n.push(t.threadsAnswered===1?`1 thread answered`:`${t.threadsAnswered} threads answered`),t.newCommits>0&&n.push(t.newCommits===1?`1 new commit`:`${t.newCommits} new commits`),n.length===0?null:`${si[e]}: ${n.join(`, `)}`}function li(e,t){if(e===null)return 0;let n=new Set(e.commits.map(e=>e.hash));return t.commits.filter(e=>!n.has(e.hash)).length}var V=`reviews`;function ui(e,t){return Nt(V,e,t)}function di(e,t){return Pt(V,e,t)}var H=class extends Error{code=`bad-request`;constructor(e){super(`reviews: ${e}`),this.name=`Refused`}},fi=new Map;function pi(e,t){let n=(fi.get(e)??Promise.resolve()).then(t,t),r=n.then(()=>void 0,()=>void 0);return fi.set(e,r),r.then(()=>{fi.get(e)===r&&fi.delete(e)}),n}function mi(e){let t=e.split(`/`);return t.length===2&&t[0]===`archive`}function hi(e){let t=e.split(`/`);return t.some(e=>e===``||e===`.`||e===`..`)?!1:t.length===1?t[0]!==A:mi(e)}function U(e,t){let n=t.trim();if(!hi(n))throw new N(V,t);return ui(e,n)}function gi(e,t,n){return di(e,`${W(t)}/${k}/${n}`)}function W(e){let t=e.trim();if(!hi(t))throw new N(V,e);return t}function G(e,t,n){return Kt(V,e,`${W(t)}/${k}/${n}`)}function K(e,t,n,r){Gt(V,e,`${W(t)}/${k}/${n}`,r)}function q(e){return`${JSON.stringify(e,null,2)}\n`}function _i(e){a(e,{recursive:!0});let t=di(e,`.gitignore`);n(t)||P(t,`${it.join(`
17
- `)}\n`)}function vi(e,t,r){let i=r(t)||`review`;if(!n(ui(e,i)))return i;let a=2;for(;n(ui(e,`${i}-${a}`));)a++;return`${i}-${a}`}function yi(e,t,r=new Date().toISOString()){let i=U(e,t);if(!n(i)||!p(i).isDirectory())throw new xt(V,t);let a=W(t),o=a.split(`/`).pop()??a,s=Zr(i),c=vt(G(e,t,He),o);if(c===null)throw new xt(V,`${t}/${k}/${He}`);let l=G(e,t,Ue),u=G(e,t,We),d=G(e,t,Ge),f=G(e,t,Ke),m=xi(e,a,[[Ue,l],[We,u],[Ge,d],[Ke,f]]),h=ye(l),g=xe(u),_=Ce(d),v=De(f),y=!1,ee=Ti(i),b=s!==null&&[h.fingerprint,g.fingerprint,_.fingerprint,v.fingerprint].some(e=>e!==s.fingerprint);if(s!==null&&b){let e=Ui(s,{marks:h,threads:g,pending:_,dismissals:v},r);h=e.marks,g=e.threads,_=e.pending,v=e.dismissals,y=!0}let x=c,S=ji(e,a),te=Ci(x,S,s,r);return{slug:a,dir:i,state:c,marks:h,threads:g,pending:_,dismissals:v,metadata:ee,approval:Ne(G(e,t,Je)),baseline:s,loops:S.length,repaired:y||te,broken:m}}function bi(e){if(e===null||e.trim()===``)return!1;try{let t=JSON.parse(e);return typeof t!=`object`||!t||Array.isArray(t)}catch{return!0}}function xi(e,t,n){let r=n.filter(([,e])=>bi(e)).map(([e])=>e);if(r.length===0||mi(t))return r;for(let n of r)d(gi(e,t,n),gi(e,t,Si(e,t,n)));return r}function Si(e,t,r){let i=r.replace(/\.json$/,``),a=`${i}.broken.json`,o=2;for(;n(gi(e,t,a));)a=`${i}.broken-${o}.json`,o++;return a}function Ci(e,t,n,r){let i=t[t.length-1]??null,a=t=>e.log.some(e=>e.said===t),o=!1;return i!==null&&e.status===`reviewing`&&e.side===`you`&&n!==null&&i.fingerprint===n.fingerprint&&!a(wi(i.items.length,t.length))&&(e.side=`agent`,e.agent={act:`process`,activity:`Working on your feedback`,groups:null,lastPostAt:r,instruction:e.agent.instruction},e.log.push({at:r,said:wi(i.items.length,t.length)}),o=!0),o}function wi(e,t){return`Sent to agent: ${e} ${e===1?`item`:`items`} - loop ${t}.`}function Ti(e){try{return ke(s(qr(e),`utf8`))}catch{return Oe()}}function Ei(e,t,n=[]){if(mi(t.slug)&&t.state.status===`archived`)throw new H(`${t.slug} is archived, so nothing is written into it`);let r=t.baseline?.fingerprint??``,i=new Set(n);i.has(`marks`)&&(t.marks.fingerprint=r,K(e,t.slug,Ue,q(t.marks))),i.has(`threads`)&&(t.threads.fingerprint=r,K(e,t.slug,We,q(t.threads))),i.has(`pending`)&&(t.pending.fingerprint=r,K(e,t.slug,Ge,q(t.pending))),i.has(`dismissals`)&&(t.dismissals.fingerprint=r,K(e,t.slug,Ke,q(t.dismissals))),i.has(`metadata`)&&(a(Kr(t.dir),{recursive:!0}),P(qr(t.dir),q(t.metadata))),i.has(`approval`)&&t.approval!==null&&K(e,t.slug,Je,q(t.approval)),K(e,t.slug,He,q(t.state)),P(ui(e,`${t.slug}/README.md`),Di(t))}function Di(e){let t=Pi(e);return ct({state:e.state,seen:t.seen,total:t.total,loops:e.loops,fingerprint:e.baseline?.fingerprint??``,commit:e.baseline?.resolved.head??``,threads:{total:e.threads.threads.length,open:e.threads.threads.filter(e=>e.open).length},metadata:e.metadata,approval:e.approval})}function Oi(e,t){return je(G(e,t,qe))}function ki(e,t,n){K(e,t,qe,q(n))}function Ai(e,t){return gi(e,t,Ye)}function ji(e,t){let r=y(U(e,t),k,Xe);if(!n(r))return[];let i=c(r).filter(e=>/^\d+\.json$/.test(e)).sort(),a=[];for(let n of i){let r=we(G(e,t,`${Xe}/${n}`));r!==null&&a.push(r)}return a}function Mi(e,t,r){let i=y(U(e,t),k,Xe),a=(n(i)?c(i).filter(e=>/^\d+\.json$/.test(e)):[]).reduce((e,t)=>Math.max(e,Number.parseInt(t,10)),0),o=`${String(a+1).padStart(2,`0`)}.json`;return K(e,t,`${Xe}/${o}`,q(r)),`${Xe}/${o}`}function Ni(e,t,r){let i=ui(e,r);if(n(i))throw new H(`${r} is already there`);a(_(i),{recursive:!0}),d(U(e,t),i)}function Pi(e,t){let n=ze(t??Ie(e),null);return{seen:n.seen,total:n.hunks,left:n.unseen}}function Fi(e,t){let n=t??Ie(e),r=ze(n,null);return{unreadReplies:r.replies,issues:r.issues,attention:r.attention,changed:r.changed,removed:n.gone.length,waiting:r.waiting}}function Ii(e,t){let n=t??Ie(e),r=Fi(e,n);return{unseen:Pi(e,n).left,issues:r.issues,attention:r.attention,threads:Le(n),unreadReplies:r.unreadReplies,pending:e.pending.items.length}}function Li(e,t){if(e.state.status!==`reviewing`||e.state.side!==`you`||e.baseline===null)return!1;let n=t??Ie(e);return Pi(e,n).total!==0&&!me(Ii(e,n))}function Ri(e,t){if(e===null)return[];let n=new Set;for(let e of t.groups)if(e.id!==`ungrouped`)for(let t of e.hunks)n.add(t);return e.entries.map(e=>e.id).filter(e=>!n.has(e))}function zi(e,t,n){let r=new Set([de,...n.groups.map(e=>e.id)]),i=e.filter(e=>e.kind!==`group`||r.has(e.anchor)),a=[],o=[];for(let e of t)e.group===null||r.has(e.group)?a.push(e):o.push(e.id);return{threads:i,items:a,gone:o}}function Bi(e,t,n,r,i){let a=Vi(ri(e,t),t,n,r,i),o=r.fingerprint===e.fingerprint?zi(a.threads.threads,a.pending.items,a.metadata):{threads:a.threads.threads,items:a.pending.items,gone:[]};return{...a,threads:{...a.threads,threads:o.threads},pending:{...a.pending,items:o.items},gone:o.gone}}function Vi(e,t,n,r,i){let a=new Set(t.entries.map(e=>e.id)),o=t=>{let n=e.lineage.get(t);return n!==void 0&&a.has(n)?n:null},s=[],c=[];for(let t of n.marks.marks){let n=o(t.hunk);if(n!==null){if(e.same.has(n)&&!e.reset.has(n)){s.push({...t,hunk:n});continue}e.paired.has(n)&&c.push({hunk:n,at:i,content:t.content})}}let l=c.length,u=new Set(c.map(e=>e.hunk));for(let e of n.marks.changed){let t=o(e.hunk);t===null||u.has(t)||c.push({...e,hunk:t})}let d=n.threads.threads.map(e=>{if(e.kind===`group`)return{...e};let t=o(e.anchor);return t===null?{...e,label:`hunk-removed`}:{...e,anchor:t,label:null}}),f=n.pending.items.map(e=>{if(e.hunk===null)return{...e};let t=o(e.hunk);return t===null?{...e,label:`hunk-removed`}:{...e,hunk:t,label:null}}),p=Hi(e,a,r,t.fingerprint),m=d,h=f,g=new Set(p.notes.map(e=>ue(e.hunk,e.key))),_=[];for(let e of n.dismissals.items){let t=o(e.hunk);t!==null&&g.has(ue(t,e.key))&&_.push({...e,hunk:t})}return{marks:{...n.marks,marks:s,changed:c,fingerprint:t.fingerprint},threads:{...n.threads,threads:m,fingerprint:t.fingerprint},pending:{...n.pending,items:h,fingerprint:t.fingerprint},dismissals:{...n.dismissals,items:_,fingerprint:t.fingerprint},metadata:p,changedSeen:l,gone:[]}}function Hi(e,t,n,r){let i=n=>{let r=e.lineage.get(n);return r!==void 0&&t.has(r)?r:null};return{...n,fingerprint:r,groups:n.groups.map(e=>({...e,hunks:e.hunks.map(e=>i(e)).filter(e=>e!==null)})),notes:n.notes.map(t=>{let n=i(t.hunk);return n!==null&&e.same.has(n)&&!e.reset.has(n)?{...t,hunk:n}:null}).filter(e=>e!==null)}}function Ui(e,t,n){let r=[];for(let e of t.marks.marks){let t=qn(e.hunk);t!==null&&r.push({id:e.hunk,fileKey:t.fileKey,contentHash:t.contentHash,ordinal:t.ordinal,content:e.content})}let i=new Set(e.entries.map(e=>e.id)),a=ti(e),o=new Set,s=new Map;for(let e of r)i.has(e.id)&&(o.add(e.id),s.set(e.id,e.id));let c=new Map;for(let e of Xn(r,a))s.set(e.from,e.to),c.set(e.to,e.from);for(let e of i)s.has(e)||s.set(e,e);for(let e of o)s.set(e,e);return Vi({lineage:s,same:o,paired:c,vanished:[],arrived:[],reset:new Set},e,t,Oe(),n)}function Wi(e,t){let n=0;for(let r of t){if(!r.startsWith(e))continue;let t=Number(r.slice(e.length));Number.isInteger(t)&&t>n&&(n=t)}return`${e}${n+1}`}function Gi(e,t,n){let r=e.threads.find(e=>e.open&&e.kind===t&&e.anchor===n);if(r!==void 0)return{thread:r,added:!1};let i={id:Wi(`t`,e.threads.map(e=>e.id)),kind:t,anchor:n,open:!0,label:null,closedAt:null,messages:[]};return e.threads.push(i),{thread:i,added:!0}}function Ki(e,t,n,r,i){let a={id:Wi(`m`,e.threads.flatMap(e=>e.messages.map(e=>e.id))),from:n,at:i,text:r,readAt:n===`agent`?null:i};return t.messages.push(a),a}function qi(e){return{marks:ve(e),threads:be(e),pending:Se(e),dismissals:Ee(e)}}var J=(e=[])=>({wrote:e,typed:[],command:!1});function Y(e,t){if(e.state.status!==`reviewing`)throw new H(`this review is ${e.state.status}, so ${t} is not possible`);if(e.state.side!==`you`)throw new H(`this review awaits the agent, so ${t} is not possible`)}function Ji(e,t){let n=e.baseline;if(n===null)throw new H(`this review has no baseline`);let r=n.entries.find(e=>e.id===t);if(r===void 0)throw new H(`this review holds no hunk ${t}`);return{baseline:n,entry:r}}function Yi(e,t,n){Y(e,`marking a hunk`);let{baseline:r,entry:i}=Ji(e,t.hunk),a=e.marks;if(!t.seen)return a.marks.some(e=>e.hunk===t.hunk)?(Qi(e,`unseen`,null,[t.hunk],n),a.marks=a.marks.filter(e=>e.hunk!==t.hunk),a.changed=a.changed.filter(e=>e.hunk!==t.hunk),J([`marks`])):J([]);let o=Qi(e,`hunk`,null,[t.hunk],n),s={hunk:t.hunk,at:n,act:o,content:ni(r,i)};return a.marks=[...a.marks.filter(e=>e.hunk!==t.hunk),s],a.changed=a.changed.filter(e=>e.hunk!==t.hunk),J([`marks`])}function Xi(e,t,n){Y(e,`marking a group`);let r=e.baseline;if(r===null)throw new H(`this review has no baseline`);let i=Zi(e,t),a=new Set(e.marks.marks.map(e=>e.hunk)),o=i.filter(e=>!a.has(e));if(o.length===0)throw new H(`nothing is left to mark in ${t.id}`);let s=Qi(e,t.kind,t.id,o,n),c=new Map(r.entries.map(e=>[e.id,e]));for(let t of o){let i=c.get(t);i!==void 0&&e.marks.marks.push({hunk:t,at:n,act:s,content:ni(r,i)})}let l=new Set(o);return e.marks.changed=e.marks.changed.filter(e=>!l.has(e.hunk)),J([`marks`])}function Zi(e,t){let n=e.baseline;if(n===null)return[];if(t.kind===`file`)return n.entries.filter(e=>e.fileKey===t.id).map(e=>e.id);if(t.id===`ungrouped`)return Ri(n,e.metadata);let r=e.metadata.groups.find(e=>e.id===t.id);if(r===void 0)throw new H(`this review holds no group ${t.id}`);let i=new Set(n.entries.map(e=>e.id));return r.hunks.filter(e=>i.has(e))}function Qi(e,t,n,r,i){let a=Wi(`a`,e.marks.acts.map(e=>e.id));return e.marks.acts.push({id:a,at:i,kind:t,container:n,hunks:r}),a}function $i(e,t,n){Y(e,`commenting`);let{baseline:r,entry:i}=Ji(e,t.hunk);if(t.kind===`talk`){let{thread:a}=Gi(e.threads,`hunk`,t.hunk);Ki(e.threads,a,`tool`,`Conversation request sent to the agent in the terminal.`,n);let o=r.files[i.file],s=i.hunk===null?null:o?.hunks[i.hunk]??null,c=t.lines===null?``:se(t.lines);return{wrote:[`threads`],typed:[{event:`talk`,detail:[e.state.name,o?.path??i.fileKey,s?.header??`the whole file`,c].filter(e=>e!==``).join(` - `),path:`${e.slug}/${k}/${We}`}],command:!1}}if(t.kind===`group-comment`)throw new H(`a group comment names its group, so it is posted as one`);return ta(e,t.kind,t.hunk,null,t.lines,t.text,n),J([`pending`])}function ea(e,t,n){if(Y(e,`commenting`),!(t.group===`ungrouped`||e.metadata.groups.some(e=>e.id===t.group)))throw new H(`this review holds no group ${t.group}`);return ta(e,`group-comment`,null,t.group,null,t.text,n),J([`pending`])}function ta(e,t,n,r,i,a,o){let s=a.trim();if(s===``&&oe(t))throw new H("say something in `text`");let c={id:Wi(`p`,e.pending.items.map(e=>e.id)),at:o,act:t,hunk:n,group:r,lines:i!==null&&i.length>0?[...i]:null,text:s,label:null};return e.pending.items.push(c),c}function na(e,t,n){Y(e,`changing the pending feedback`);let r=e.baseline;if(r===null)throw new H(`this review has no baseline`);let i=new Set(r.entries.map(e=>e.id)),a=new Set([de,...e.metadata.groups.map(e=>e.id)]),o=[];for(let e of t){if(e.hunk!==null&&!i.has(e.hunk)){o.push({...e,label:`hunk-removed`});continue}if(e.group!==null&&!a.has(e.group))throw new H(`this review holds no group ${e.group}`);o.push({...e,at:e.at||n,label:null})}return e.pending.items=o,e.pending.version=1,J([`pending`])}function ra(e,t,n){if(Y(e,`dismissing a note`),!e.metadata.notes.some(e=>e.hunk===t.hunk&&e.key===t.key))throw new H(`this review holds no such note`);let r=e.dismissals.items.filter(e=>!(e.hunk===t.hunk&&e.key===t.key));return e.dismissals.items=t.on?[...r,{hunk:t.hunk,key:t.key,at:n}]:r,J([`dismissals`])}function ia(e,t,n){Y(e,`closing a thread`);let r=e.threads.threads.find(e=>e.id===t);if(r===void 0)throw new H(`there is no thread called ${t}`);r.open=!1,r.closedAt=n;for(let e of r.messages)e.readAt===null&&(e.readAt=n);return J([`threads`])}function aa(e,t,n){if(t.length===0)return J();let r=new Set(t),i=!1;for(let t of e.threads.threads)for(let e of t.messages)e.readAt!==null||!r.has(e.id)||(e.readAt=n,i=!0);return J(i?[`threads`]:[])}function oa(e,t,n){Y(t,`sending`);let r=t.pending.items;if(r.length===0)throw new H(`there is nothing to send`);let i=t.baseline?.fingerprint??``,a=r.map(e=>({hunk:e.hunk,act:e.act,group:e.group,lines:e.lines,text:e.text})),o={version:1,sentAt:n,fingerprint:i,items:a};Mi(e.root,t.slug,o),t.loops+=1;for(let e of r){let r=e.hunk??e.group;if(r===null)continue;let{thread:i}=Gi(t.threads,e.hunk===null?`group`:`hunk`,r),a=e.lines===null?``:` ${se(e.lines)}`,o=`${ae[e.act]}${a}`;Ki(t.threads,i,`you`,e.text?`${o}: ${e.text}`:o,n)}return t.pending.items=[],sa(t,`process`,`Working on your feedback`,n),t.state.log.push({at:n,said:wi(a.length,t.loops)}),{wrote:[`pending`,`threads`],typed:[],command:!0}}function sa(e,t,n,r){e.state.side=`agent`,e.state.agent={act:t,activity:n,groups:null,lastPostAt:r,instruction:e.state.agent.instruction}}async function ca(e,t,n,r){let i=t.baseline,a=await Ir(e.git,t.state.selection,{workspace:e.workspace,limits:e.limits}),o=0;if(i!==null){let n=Bi(i,a.record,{marks:t.marks,threads:t.threads,pending:t.pending,dismissals:t.dismissals},t.metadata,r);t.marks=n.marks,t.threads=n.threads,t.pending=n.pending,t.dismissals=n.dismissals,t.metadata=n.metadata,o=n.changedSeen,ha(e,t,n.gone)}Jr(t.dir,a),t.baseline=a.record;let s=ci(n,{changedSeen:o,threadsAnswered:la(t,i),newCommits:li(i,a.record)});return s!==null&&t.state.log.push({at:r,said:s}),{wrote:[`marks`,`threads`,`pending`,`dismissals`,`metadata`],typed:[],command:!1,line:s}}function la(e,t){if(t===null)return 0;let n=Date.parse(t.computedAt);return Number.isNaN(n)?0:e.threads.threads.filter(e=>e.messages.some(e=>{if(e.from!==`agent`)return!1;let t=Date.parse(e.at);return!Number.isNaN(t)&&t>n})).length}async function ua(e,t,n){Y(t,`syncing`);let r=await ca(e,t,`sync`,n);return sa(t,`sync`,`Syncing`,n),{...r,typed:[],command:!0}}async function da(e,t,n){if(t.state.status!==`reviewing`)throw new H(`this review is ${t.state.status}, so there is nothing to stop`);if(t.state.side!==`agent`)throw new H(`this review is already yours`);let r=await ca(e,t,`stop`,n);return t.state.side=`you`,t.state.agent={act:null,activity:``,groups:null,lastPostAt:t.state.agent.lastPostAt,instruction:t.state.agent.instruction},t.state.log.push({at:n,said:`stopped the agent`}),{...r,typed:[{event:`stopped`,detail:t.state.name,path:va(t)}]}}async function fa(e,t,n){pa(t);let r=await ca(e,t,`done`,n);return t.state.side=`you`,t.state.agent={act:null,activity:``,groups:null,lastPostAt:n,instruction:``},r}function pa(e){let t=e.state.agent.act;if(t!==`prepare`&&t!==`sync`)return;let n=e.baseline;if(n===null)return;let r=Ri(n,e.metadata);if(r.length>0){let e=r.slice(0,3).join(`, `),t=r.length>3?` and ${r.length-3} more`:``;throw new H(`${r.length} hunk${r.length===1?` is`:`s are`} in no group: ${e}${t}. Group them, then post done again.`)}let i=e.state.agent.groups,a=e.metadata.groups.length;if(i!==null&&a<i)throw new H(`${a} of the ${i} groups you said were coming are posted. Post the rest, or say the new count with activity { groups }, then post done again.`)}function ma(e){let t=e.trim();if(t.length>2e3)throw new H(`an instruction to the agent is at most ${lt} characters, and this one is ${t.length}`);return t}function ha(e,t,n){if(n.length===0)return;let r=new Set(n.map(e=>`pending:${e}`)),i=Oi(e.root,t.slug),a=i.drafts.filter(e=>!r.has(e.anchor));a.length!==i.drafts.length&&ki(e.root,t.slug,{...i,drafts:a})}function ga(e,t,n,r){Y(t,`resetting the review`);let i=t.baseline;if(i===null)throw new H(`this review has no baseline to prepare`);let a=ma(n);t.metadata=Oe(i.fingerprint),t.dismissals.items=[];let o=zi(t.threads.threads,t.pending.items,t.metadata);t.threads.threads=o.threads,t.pending.items=o.items,ha(e,t,o.gone),t.state.agent.instruction=a,sa(t,`prepare`,`Preparing from nothing`,r);let s=a.split(`
18
- `)[0]?.trim()??``;return t.state.log.push({at:r,said:s===``?`reset the review`:`reset the review: ${s}`}),{wrote:[`metadata`,`threads`,`pending`,`dismissals`],typed:[],command:!0}}async function _a(e,t,n){Y(t,`following a merge`);let r=t.baseline;if(r===null)throw new H(`this review has no baseline`);let i=await e.relations.mergeOf(r);if(i===null)throw new H(`no commit on the base holds this change`);return t.state.selection=await br(e.git,i),t.state.log.push({at:n,said:`followed the merge to ${i}`}),{...await ca(e,t,`follow`,n),typed:[{event:`followed`,detail:i,path:va(t)}]}}function va(e){return`${e.slug}/${k}/baseline/baseline.json`}function ya(e,t,n){Y(e,`approving`);let r=e.baseline;if(r===null)throw new H(`this review has no baseline`);let i=t.name.trim();if(i===``)throw new H(`an approval carries the name that signed it`);let a=Ii(e),o=he(a);if(o>0&&!t.acknowledged)throw new H(`this approval leaves ${o} ${o===1?`thing`:`things`} as ${o===1?`it is`:`they are`}; acknowledge them to sign`);let s=t.signature===null?null:ba(t.signature);return e.approval={version:1,approved:!0,at:n,name:i,signature:s===null?null:Ye,reason:``,loops:e.loops,fingerprint:r.fingerprint,commit:r.resolved.head,uncommitted:r.resolved.tree,leftAsIs:o>0?a:null},e.state.status=`approved`,e.state.log.push({at:n,said:o>0?`approved by ${i} with ${o} ${o===1?`thing`:`things`} left as ${o===1?`it was`:`they were`}`:`approved by ${i}`}),{...J([`approval`]),signature:s}}function ba(e){let t=/^data:image\/png;base64,([A-Za-z0-9+/=]+)$/.exec(e.trim());if(t===null||t[1]===void 0)throw new H(`a signature is a PNG data URL drawn by the client`);return Buffer.from(t[1],`base64`)}function xa(e,t,n){Y(e,`closing without approval`);let r=t.trim();if(r===``)throw new H(`closing without approval carries a reason`);let i=e.baseline,a=Pi(e);return e.approval={version:1,approved:!1,at:n,name:``,signature:null,reason:r,loops:e.loops,fingerprint:i?.fingerprint??``,commit:i?.resolved.head??``,uncommitted:i?.resolved.tree??!1,leftAsIs:null},e.state.status=`closed`,e.state.side=`you`,e.state.agent={act:null,activity:``,groups:null,lastPostAt:e.state.agent.lastPostAt,instruction:e.state.agent.instruction},e.state.log.push({at:n,said:`closed without approval with ${a.seen} of ${a.total} hunks Seen`}),J([`approval`])}function Sa(e,t){let n=e=>String(e).padStart(2,`0`);return`${A}/${`${t.getFullYear()}-${n(t.getMonth()+1)}-${n(t.getDate())}`}-${e}`}function Ca(e,t){if(e.state.status!==`approved`&&e.state.status!==`closed`)throw new H(`a review is archived once it is approved or closed`);return e.state.status=`archived`,e.state.log.push({at:t,said:`archived`}),J()}var wa=.8;function Ta(e,t){let n=new Map,r=async t=>{let r=await Ea(e,t);if(r===null)return null;let i=`${r}\n${t.fingerprint}`,a=n.get(i);if(a!==void 0)return a;let o=await Oa(e,t,r);return n.set(i,o),o};return{of:async n=>{let i=Zn(n.selection)?n.selection.head:null,a=await rr(e),o=await ir(e),s=i===null?null:o.find(e=>e.name===i)??null;if(i!==null){let t=await Da(e,n,s?.tip??n.resolved.head);if(s===null||t){let e=await r(n);return e===null?t?{relation:`merged`,branch:i,mergedAs:null,why:`the base holds the branch`}:{relation:`history-rewritten`,branch:i,mergedAs:null,why:`the branch is gone and no commit on the base holds its change`}:{relation:`merged`,branch:i,mergedAs:e,why:`merged as ${e}`}}if(n.selection.kind===`branch`&&a!==i)return{relation:`different-branch`,branch:i,mergedAs:null,why:`${a??`no branch`} is checked out`}}if(n.selection.baseRef!==null&&await Ea(e,n)===null)return{relation:`history-rewritten`,branch:i,mergedAs:null,why:`${n.selection.baseRef} is gone`};let c=await gr(e,n.selection).catch(()=>null);if(c===null)return{relation:`history-rewritten`,branch:i,mergedAs:null,why:`the selection no longer resolves`};if(c.rewritten)return{relation:`history-rewritten`,branch:i,mergedAs:null,why:`HEAD no longer descends from the head of this review`};if(i!==null&&!await lr(e,n.resolved.head,c.head))return{relation:`history-rewritten`,branch:i,mergedAs:null,why:`${i} no longer holds the commits this review was of`};if(c.base!==n.resolved.base)return{relation:`out-of-sync`,branch:i,mergedAs:null,why:`the base moved`};if(c.head!==n.resolved.head)return{relation:`out-of-sync`,branch:i,mergedAs:null,why:`new commits`};let l=await ja(e,n,t.workspace);return l===null?{relation:`current`,branch:i,mergedAs:null,why:`the tree is the baseline`}:{relation:`out-of-sync`,branch:i,mergedAs:null,why:l}},mergeOf:r}}async function Ea(e,t){let n=t.selection.baseRef;return n===null?null:e.commit(n).catch(()=>null)}async function Da(e,t,n){let r=await Ea(e,t);return r!==null&&lr(e,n,r)}async function Oa(e,t,n){if(t.entries.length===0)return null;let r=t.resolved.base,i=(await e.text(`log-first-parent`,r,n).catch(()=>``)).split(`
19
- `).filter(e=>e!==``).map(e=>e.split(` `)[0]??``).filter(e=>e!==``).slice(0,200),a=new Set(t.entries.map(e=>e.id)),o=t.entries.filter(e=>e.hunk!==null).length;for(let n of i){if(!ka(await e.text(`diff-tree`,n).catch(()=>``),o))continue;let r=await e.commit(`${n}^`).catch(()=>null);if(r===null)continue;let i=Un(L(await e.bytes(`diff`,r,n,!1)));if(i.fingerprint===t.fingerprint||i.entries.filter(e=>a.has(e.id)).length/a.size>=.8)return n}return null}function ka(e,t){let n=0,r=0;for(let t of e.split(`
20
- `)){if(t===``)continue;let e=t.split(` `);n+=Aa(e[0])+Aa(e[1]),r+=1}return r!==0&&n>=Math.floor(t*wa)}function Aa(e){let t=Number(e);return Number.isFinite(t)?t:0}async function ja(e,t,n){if(t.tree===null)return null;if((await e.bytes(`status`)).toString(`utf8`)!==t.tree.porcelain)return`the working tree changed`;let r=b(n);for(let e of t.tree.files){let t=Ma(r,e);if(t!==null)return t}return null}function Ma(e,t){let n=e;try{n=u(e)}catch{}let r=b(n,t.path);if(!Ft(n,r))return`${t.path} is not in the workspace`;let a;try{if(!Ft(n,u(_(r))))return`${t.path} is not in the workspace`;a=i(r)}catch{return`${t.path} is gone`}let o=a.isSymbolicLink();if(!o&&!a.isFile())return`${t.path} is not a file any more`;try{let e=o?Buffer.byteLength(l(r)):a.size;if(e!==t.size)return`${t.path} was edited`;if(a.mtimeMs===t.mtimeMs)return null;if(e>jr.blobBytes)return`${t.path} is too large to check`;let n=o?Buffer.from(l(r),`utf8`):s(r);return S(`sha256`).update(n).digest(`hex`)===t.hash?null:`${t.path} was edited`}catch{return`${t.path} is gone`}}var Na=[`stages`,`state`,`start`,`metadata`,`group`,`ungroup`,`reply`,`activity`,`done`,`log`],Pa=[`metadata`,`group`,`ungroup`,`reply`,`activity`,`done`],Fa=[...Pa,`log`];function Ia(e,t){if(Fa.includes(t)){if(e.state.status!==`reviewing`)throw new H(`this review is ${e.state.status}, so it takes no more work`);if(Pa.includes(t)&&e.state.side!==`agent`)throw new H(`this review awaits the reviewer, so there is nothing to post`)}}var La=e=>typeof e==`object`&&!!e&&!Array.isArray(e);function X(e,t){if(!La(e))throw new H(`${t} is an object`);return e}var Z=(e,t=``)=>typeof e==`string`?e:t,Ra=e=>Array.isArray(e)?e:[];function za(e){let t=Z(X(e,`this`).said).trim();if(t===``)throw new H("say something in `said`");return t}function Ba(e){let t=za(e),n=X(e,`this`).groups;if(n==null)return{said:t,groups:null};if(typeof n!=`number`||!Number.isInteger(n)||n<0)throw new H("`groups` is how many groups there will be, as a whole count");return{said:t,groups:n}}function Va(e){let t=X(e,`a reply`),n=Z(t.thread).trim(),r=Z(t.text).trim();if(n===``)throw new H("a reply names the thread it is on in `thread`");if(r===``)throw new H("a reply carries its words in `text`");return{thread:n,text:r}}function Ha(e){let t=X(e,`metadata`),n=Ua(t,`metadata`),r=[],i=new Set;for(let e of Ra(t.groups)){let t=Wa(e);if(i.has(t.id))throw new H(`two groups are called ${t.id}`);i.add(t.id),r.push(t)}return{fingerprint:n,groups:r,notes:Ra(t.notes).map(Ka)}}function Ua(e,t){let n=Z(e.fingerprint).trim();if(n===``)throw new H(`${t} names the baseline it was written for in \`fingerprint\``);return n}function Wa(e){let t=X(e,`a group`),n=Z(t.id).trim();if(n===``)throw new H("a group names itself in `id`");if(n===`ungrouped`)throw new H(`${de} is the tool's own group and is not posted`);return{id:n,name:Ga(t,n),narrative:Z(t.narrative),facts:Ra(t.facts).filter(e=>typeof e==`string`),hunks:Ra(t.hunks).filter(e=>typeof e==`string`)}}function Ga(e,t){if(e.name===void 0||e.name===null)return t;let n=Z(e.name).trim();if(n===``||n.length>60)throw new H(`the name of group \`${t}\` is ${n.length} characters; a name is 60 at most`);return n}function Ka(e){let t=X(e,`a note`),n=Z(t.hunk).trim();if(n===``)throw new H("a note names the hunk it is on in `hunk`");let r=Z(t.kind);if(!ce.includes(r))throw new H("a note is `info`, `attention` or `issue`");let i=Z(t.text).trim();if(i===``)throw new H("a note carries its words in `text`");let a=t.lines===void 0||t.lines===null?null:Ra(t.lines).filter(e=>Number.isInteger(e));if(a!==null&&a.length===0)throw new H(`a note that names lines names at least one`);return{hunk:n,kind:r,lines:a,text:i,key:le(r,a,i)}}function qa(e){let t=X(e,`a group post`),n=Ua(t,`a group post`),r=Wa(t.group),i=Ra(t.notes).map(Ka),a=new Set(r.hunks);for(let e of i)if(!a.has(e.hunk))throw new H(`a note posted with a group is on one of its hunks; ${e.hunk} is not`);return{fingerprint:n,group:r,notes:i}}function Ja(e){let t=X(e,`an ungroup post`),n=Ua(t,`an ungroup post`),r=Z(t.id).trim();if(r===``)throw new H("name the group to remove in `id`");return{fingerprint:n,id:r}}function Ya(e,t){let n=Qa(e,t.fingerprint);return $a(n,t.groups,t.notes),eo(e,n,t.groups,t.notes)}function Xa(e,t){let n=Qa(e,t.fingerprint);$a(n,[t.group],t.notes);let r=e.metadata.groups.some(e=>e.id===t.group.id)?e.metadata.groups.map(e=>e.id===t.group.id?t.group:e):[...e.metadata.groups,t.group],i=new Set(t.group.hunks);return eo(e,n,r,[...e.metadata.notes.filter(e=>!i.has(e.hunk)),...t.notes])}function Za(e,t){let n=Qa(e,t.fingerprint);if(!e.metadata.groups.some(e=>e.id===t.id))throw new H(`this review holds no group called ${t.id}`);return eo(e,n,e.metadata.groups.filter(e=>e.id!==t.id),e.metadata.notes)}function Qa(e,t){let n=e.baseline;if(n===null)throw new H(`this review has no baseline to group`);if(t!==n.fingerprint)throw new H(`the baseline changed; the review awaits you`);return n}function $a(e,t,n){let r=new Map(e.entries.map(e=>[e.id,e]));for(let e of t)for(let t of e.hunks)if(!r.has(t))throw new H(`${e.id} names ${t}, which this baseline does not hold`);for(let t of n){let n=r.get(t.hunk);if(n===void 0)throw new H(`a note names ${t.hunk}, which this baseline does not hold`);to(e,n,t)}}function eo(e,t,n,r){let i={version:1,fingerprint:t.fingerprint,groups:n,notes:r};return e.metadata=i,{ungrouped:Ri(t,i),groups:n.length,notes:r.length}}function to(e,t,n){if(n.lines===null)return;let r=t.hunk===null?null:e.files[t.file]?.hunks[t.hunk]??null;if(r===null)throw new H(`${n.hunk} changes no line, so a note on it names no lines`);let i=r.lines.map(e=>e.newLine).filter(e=>e!==null),a=i[0],o=i[i.length-1];if(a===void 0||o===void 0)throw new H(`${n.hunk} shows no new-side line, so a note on it names no lines`);for(let e of n.lines)if(e<a||e>o)throw new H(`a note names line ${e}, which is outside ${n.hunk}`)}function no(e,t,n){let r=e.threads.threads.find(e=>e.id===t.thread);if(r===void 0)throw new H(`there is no thread called ${t.thread}`);if(!r.open)throw new H(`${t.thread} is closed`);return Ki(e.threads,r,`agent`,t.text,n),{thread:r.id}}function ro(e,t,n,r=null){e.state.agent.activity=t,r!==null&&(e.state.agent.groups=r),e.state.agent.lastPostAt=n}function io(e,t,n){e.state.log.push({at:n,said:t}),e.state.side===`agent`&&(e.state.agent.lastPostAt=n)}var ao=200,oo=2e3,so=e=>typeof e==`object`&&!!e&&!Array.isArray(e);function Q(e,t){if(!so(e))throw new H(`${t} is an object`);return e}var $=(e,t=``)=>typeof e==`string`?e:t,co=e=>Array.isArray(e)?e:[],lo=(e,t=0)=>typeof e==`number`&&Number.isFinite(e)?e:t,uo=e=>co(e).filter(e=>typeof e==`string`);function fo(e){if(e==null)return null;let t=co(e).map(e=>lo(e)).filter(e=>Number.isInteger(e)&&e>0);return t.length===0?null:t}function po(e){let t=$(e.hunk).trim();if(t===``)throw new H("name the hunk in `hunk`");return t}function mo(e){let t=Q(e,`a selection`),n=$(t.kind),r=[`branch`,`last-commit`,`commit`,`range`,`other-branch`];if(!r.includes(n))throw new H(`a selection is one of ${r.join(`, `)}`);let i={kind:n,uncommitted:t.uncommitted===!0};return typeof t.branch==`string`&&(i.branch=t.branch),typeof t.commit==`string`&&(i.commit=t.commit),typeof t.from==`string`&&(i.from=t.from),typeof t.to==`string`&&(i.to=t.to),typeof t.base==`string`&&(i.base=t.base),i}function ho(e){let t=Q(e,`a review`);return{name:$(t.name).trim(),selection:mo(t.selection)}}function go(e){let t=Q(e,`the pending feedback`),n=[];for(let e of co(t.items)){let t=Q(e,`a pending item`),r=$(t.act);if(!ie.includes(r))throw new H(`an item is one of ${ie.join(`, `)}`);let i=typeof t.hunk==`string`?t.hunk:null,a=typeof t.group==`string`?t.group:null;if(i===null&&a===null)throw new H(`an item names a hunk or a group`);let o=$(t.text).trim();if(o===``&&r!==`undo`)throw new H("an item says something in `text`");let s=fo(t.lines);n.push({id:$(t.id),at:$(t.at),act:r,hunk:i,group:a,lines:s,text:o,label:null})}return n}function _o(e){let t=At(e.root),r=At(e.workspace),i=e.clock??(()=>new Date),a=Nn({workspace:r,exclude:[t]}),o=Ta(a,{workspace:r}),s=e.onChange,c=s?Jt(t,s,{shared:rt,entries:`children`,coalesce:`per-entry`}):null,l=n=>({root:t,git:a,relations:o,workspace:r,limits:e.limits,at:n}),d=(e,n)=>vo(t,e,$r(n)),f=e=>vo(t,e,`${k}/${Ye}`),p=null,m=async()=>(p===null&&(p=await Eo(a)),p),h=e=>`${t}\n${W(e)}`,g,_=async()=>{if(g!==void 0)return g;if(!await a.ok(`rev-parse-git-dir`))return g=`This workspace is not a git repository.`,g;let e=(await a.text(`rev-parse-top`).catch(()=>``)).trim();return g=e!==``&&u(e)===u(r)?null:`This workspace is inside the repository at ${e}, not the top of it, so a review of it would be a review of the wrong paths.`,g},v=(e,n)=>(Xr(U(t,W(e))),yi(t,e,n)),y=(e,t)=>pi(h(e),async()=>{let n=i().toISOString();return t(v(e,n),n)}),ee=[`marks`,`threads`,`pending`,`dismissals`],b=async(e,n)=>pi(h(e),async()=>{let r=i().toISOString(),a=v(e,r),o=await n(a,r),s=a.repaired?[...new Set([...o.wrote,...ee])]:o.wrote;return Ei(t,a,s),{review:await S(a),typed:o.typed,command:o.command,line:o.line??null}}),x=async(e,n,r,a)=>pi(h(e),async()=>{let o=i().toISOString(),s=v(e,o);Ia(s,n);let c=await r(s,o);return Ei(t,s,a(c)),c}),S=async e=>{let n=i(),r=Ie(e);return{slug:e.slug,state:e.state,baseline:e.baseline===null?null:Do(e.baseline),marks:wo(e.marks),threads:e.threads,pending:e.pending,dismissals:e.dismissals,metadata:e.metadata,ungrouped:Ri(e.baseline,e.metadata),relation:e.baseline===null?null:await o.of(e.baseline),loops:e.loops,progress:Pi(e,r),attention:Fi(e,r),leftAsIs:Ii(e,r),ready:Li(e,r),stale:e.state.side===`agent`&&dt(e.state.agent.lastPostAt,n),approval:e.approval,place:Oi(t,e.slug),configuredName:await m(),error:To(e)}};return{root:t,workspace:r,async list(){let e=await _(),n=[],a=[];for(let e of So(t)){let r=await pi(h(e),()=>(Xr(U(t,W(e))),Po(t,e,o,i())));(mi(e)?a:n).push(r)}let s=(e,t)=>e.slug.localeCompare(t.slug);return n.sort(s),a.sort((e,t)=>s(t,e)),{root:xo(t,r),active:n,archived:a,awaitingYou:n.filter(e=>!e.missing&&e.status===`reviewing`&&e.side===`you`).length,any:n.length+a.length>0,repository:e===null,error:e}},async review(e){return y(e,e=>S(e))},hunks(e,n,r){return No(yi(t,e,i().toISOString()),n,r)},blobLines(e,t,n,r){return Mo(d(e,t),t,n,r)},options:async e=>{if(e==null)return Sr(a);try{return{options:[await Cr(a,mo(e))],notOffered:null}}catch(e){throw e instanceof H?e:new H(e instanceof Error?e.message:`that is not a selection`)}},async start(n,o=`form`){let s=await _();if(s!==null)throw new H(s);let l=ho(n);return pi(`${t}\n`,async()=>{let n=await yr(a,l.selection),s=await Ir(a,n,{workspace:r,limits:e.limits});if(s.record.entries.length===0)throw new H(`Nothing to review in this selection.`);let u=i().toISOString(),d=l.name||ot(n);_i(t);let f=vi(t,d,ne),p=U(t,f),m=qi(s.record.fingerprint),h={slug:f,dir:p,state:_t(d,f,n,o,u),marks:m.marks,threads:m.threads,pending:m.pending,dismissals:m.dismissals,metadata:{version:1,fingerprint:s.record.fingerprint,groups:[],notes:[]},approval:null,baseline:s.record,loops:0,repaired:!1,broken:[]};return Ei(t,h,[`marks`,`threads`,`pending`,`dismissals`]),Jr(p,s),c?.refollow(),{slug:f,path:`${f}/README.md`,state:h.state,command:!0}})},async act(e,n,r){switch(n){case`mark`:return b(e,(e,t)=>{let n=Q(r,`a mark`);return Yi(e,{hunk:po(n),seen:n.seen!==!1},t)});case`bulk`:return b(e,(e,t)=>{let n=Q(r,`a bulk act`),i=$(n.kind);if(i!==`file`&&i!==`group`)throw new H("a bulk act covers a `file` or a `group`");let a=$(n.id).trim();if(a===``)throw new H("a bulk act names what it covers in `id`");return Xi(e,{kind:i,id:a},t)});case`comment`:return b(e,(e,t)=>{let n=Q(r,`a comment`),i=$(n.kind),a=[`undo`,`explain`,`comment`,`fix`,`talk`];if(!a.includes(i))throw new H(`a comment is one of ${a.join(`, `)}`);let o=fo(n.lines);return $i(e,{hunk:po(n),kind:i,text:$(n.text),lines:o},t)});case`dismiss`:return b(e,(e,t)=>{let n=Q(r,`a dismissal`),i=$(n.key).trim();if(i===``)throw new H("name the note in `key`");return ra(e,{hunk:po(n),key:i,on:n.on!==!1},t)});case`group-comment`:return b(e,(e,t)=>{let n=Q(r,`a group comment`),i=$(n.group).trim();if(i===``)throw new H("a group comment names its group in `group`");return ea(e,{group:i,text:$(n.text)},t)});case`close-thread`:return b(e,(e,t)=>{let n=$(Q(r,`a thread`).thread).trim();if(n===``)throw new H("name the thread in `thread`");return ia(e,n,t)});case`pending`:return b(e,(e,t)=>na(e,go(r),t));case`send`:return b(e,(e,t)=>oa(l(t),e,t));case`sync`:return b(e,(e,t)=>ua(l(t),e,t));case`reset`:return b(e,(e,t)=>{let n=so(r)?r:{};return ga(l(t),e,$(n.instruction),t)});case`stop`:return b(e,(e,t)=>da(l(t),e,t));case`follow`:return b(e,(e,t)=>_a(l(t),e,t));case`approve`:return b(e,(e,n)=>{let i=Q(r,`an approval`),a=typeof i.signature==`string`?i.signature:null,o=ya(e,{name:$(i.name),signature:a,acknowledged:i.acknowledged===!0},n);return o.signature!==null&&Fo(t,e.slug,o.signature),o});case`close`:return b(e,(e,t)=>xa(e,$(Q(r,`a closing`).reason),t));case`archive`:return pi(h(e),async()=>{let n=i().toISOString(),r=v(e,n),a=r.state.status===`archived`&&!mi(r.slug)?{wrote:[],typed:[],command:!1}:Ca(r,n);Ei(t,r,r.repaired?[`marks`,`threads`,`pending`]:[]);let o=Sa(r.slug,i());return Ni(t,r.slug,o),r.slug=o,r.dir=U(t,o),{review:await S(r),typed:a.typed,command:!1,line:null}});case`place`:return pi(h(e),async()=>{let n=i().toISOString(),a=W(e);if(mi(a))throw new H(`this review is filed away, so it keeps no reading place`);let o=Q(r,`a place`),s={version:1,scroll:lo(o.scroll),expanded:uo(o.expanded),group:typeof o.group==`string`?o.group:null,drafts:Me(o.drafts),at:n};ki(t,a,s);let c=uo(o.read);if(c.length===0)return{review:null,typed:[],command:!1,line:null};let l=v(a,n),u=aa(l,c,n),d=l.repaired?[...new Set([...u.wrote,...ee])]:u.wrote;return d.length>0&&Ei(t,l,d),{review:await S(l),typed:[],command:!1,line:null}});default:throw new H(`there is no act called ${n}`)}},stages:()=>({root:t,limits:{...jr,...e.limits},verbs:Na,reviews:So(t)}),async agentState(e){return y(e,e=>{let n=ji(t,e.slug);return{slug:e.slug,name:e.state.name,status:e.state.status,side:e.state.side,act:e.state.agent.act,activity:e.state.agent.activity,instruction:e.state.agent.instruction,selection:ot(e.state.selection),filedBy:e.state.filedBy,loops:e.loops,baseline:e.baseline===null?null:Ao(e.baseline),metadata:e.metadata,ungrouped:Ri(e.baseline,e.metadata),threads:e.threads.threads,send:n[n.length-1]??null,awaits:jo(e)}})},metadata:(e,t)=>x(e,`metadata`,e=>Ya(e,Ha(t)),()=>[`metadata`]),group:(e,t)=>x(e,`group`,e=>Xa(e,qa(t)),()=>[`metadata`]),ungroup:(e,t)=>x(e,`ungroup`,e=>Za(e,Ja(t)),()=>[`metadata`]),reply:(e,t)=>x(e,`reply`,(e,n)=>{let r=no(e,Va(t),n);return ro(e,e.state.agent.activity||`Answering`,n),r},()=>[`threads`]),activity:(e,t)=>x(e,`activity`,(e,n)=>{let r=Ba(t);return ro(e,r.said,n,r.groups),{said:r.said}},()=>[]),done:e=>x(e,`done`,async(e,t)=>{let n=await fa(l(t),e,t);return{fingerprint:e.baseline?.fingerprint??``,line:n.line??null,wrote:n.wrote}},e=>e.wrote).then(e=>({fingerprint:e.fingerprint,line:e.line})),log:(e,t)=>x(e,`log`,(e,n)=>{let r=za(t);return io(e,r,n),{said:r}},()=>[]),blob:d,signature:f,raw(e){let t=e.split(`/`),n=t.pop()??``;if(n===`signature.png`)return f(t.join(`/`));if(t.pop()===`blobs`)return d(t.join(`/`),n);throw new N(V,e)},toolPath(e){if(!rt(e))throw new N(V,e);let r=di(t,e);if(!n(r))throw new St(V,e);return r},file:e=>Bt(V,t,e),close:()=>c?.close()}}function vo(e,t,n){return Vt(V,e,`${W(t)}/${n}`,bo)}var yo=RegExp(`^${j}/${tt}/([0-9a-f]{40}|[0-9a-f]{64})$`);function bo(e){let t=e.indexOf(`/${k}/`);if(t===-1)return!1;let n=e.slice(t+k.length+2);return n===`signature.png`||yo.test(n)}function xo(e,t){return e===t?`.`:`${e.startsWith(`${t}/`)?e.slice(t.length+1):e}/`}function So(e){let t=[];for(let n of Co(e)){if(n===`archive`){for(let r of Co(ui(e,n)))t.push(`archive/${r}`);continue}t.push(n)}return t.sort()}function Co(e){try{return c(e,{withFileTypes:!0}).filter(e=>e.isDirectory()&&!e.name.startsWith(`.`)).map(e=>e.name)}catch{return[]}}function wo(e){return{version:e.version,fingerprint:e.fingerprint,marks:e.marks.map(({hunk:e,at:t,act:n})=>({hunk:e,at:t,act:n})),acts:e.acts,changed:e.changed.map(({hunk:e,at:t})=>({hunk:e,at:t}))}}function To(e){if(e.baseline===null)return`${k}/${j}/baseline.json is not there to be read, so there is nothing left to review; closing this review without approval is what is left`;if(e.broken.length===0)return null;let t=e.broken.map(e=>e.replace(/\.json$/,`.broken.json`));return`${e.broken.join(` and `)} could not be read and started again; what was there is kept as ${t.join(` and `)}`}async function Eo(e){return(await e.text(`config-user-name`).catch(()=>``)).trim()}function Do(e){let t=new Map;for(let n of e.entries)t.has(n.file)||t.set(n.file,n.fileKey);return{fingerprint:e.fingerprint,computedAt:e.computedAt,resolved:e.resolved,commits:e.commits,files:e.files.map((e,n)=>Oo(e,t.get(n)??e.path)),entries:e.entries.map(t=>ko(e,t)),totals:e.totals}}function Oo(e,t){return{key:t,path:e.path,oldPath:e.oldPath,status:e.status,similarity:e.similarity,binary:e.binary,category:e.category,untracked:e.untracked,added:e.added,deleted:e.deleted,oldBlob:e.oldBlob,newBlob:e.newBlob,oldBytes:e.oldBytes,newBytes:e.newBytes}}function ko(e,t){let n=e.files[t.file],r=t.hunk===null?null:n?.hunks[t.hunk]??null;return{id:t.id,file:t.file,header:r?.header??``}}function Ao(e){return{fingerprint:e.fingerprint,computedAt:e.computedAt,totals:e.totals,files:e.files.map((t,n)=>({path:t.path,oldPath:t.oldPath,hunks:e.entries.filter(e=>e.file===n).map(e=>({id:e.id,header:e.hunk===null?``:t.hunks[e.hunk]?.header??``}))}))}}function jo(e){let t=e.state;if(t.status!==`reviewing`)return{side:t.side,act:null,reason:`this review is ${t.status}`};if(t.side===`you`)return{side:`you`,act:null,reason:`the reviewer has this review`};let n=t.agent.act===`prepare`?`group the baseline and write its narratives`:t.agent.act===`process`?`work through the newest send`:`group what is new and revise the narratives the new code made wrong`;return{side:`agent`,act:t.agent.act,reason:n}}function Mo(e,t,n,r){let i=e.bytes.toString(`utf8`).split(`
21
- `);i[i.length-1]===``&&i.pop();let a=Math.max(1,Math.trunc(n)),o=Math.min(oo,Math.max(1,Math.trunc(r)));return{blob:t,from:a,total:i.length,lines:i.slice(a-1,a-1+o)}}function No(e,t,n){let r=e.baseline;if(r===null)throw new H(`this review has no baseline`);let i=Math.max(0,Math.trunc(t)),a=Math.min(200,Math.max(1,Math.trunc(n))),o=r.entries.slice(i,i+a),s=[];for(let e of o){let t=r.files[e.file];if(t===void 0)continue;let n=e.hunk===null?null:t.hunks[e.hunk]??null;s.push({id:e.id,file:e.file,path:t.path,oldPath:t.oldPath,status:t.status,binary:t.binary,untracked:t.untracked,oldBlob:t.oldBlob,newBlob:t.newBlob,header:n?.header??``,heading:n?.heading??``,oldStart:n?.oldStart??0,oldLines:n?.oldLines??0,newStart:n?.newStart??0,newLines:n?.newLines??0,lines:n?.lines??[],nonTextual:e.hunk===null,facts:e.facts})}return{from:i,count:s.length,total:r.entries.length,fingerprint:r.fingerprint,hunks:s}}async function Po(e,t,n,r){let i;try{i=yi(e,t,r.toISOString())}catch{return{slug:t,name:t.split(`/`).pop()??t,status:`reviewing`,side:`you`,filedBy:`form`,progress:{seen:0,total:0,left:0},relation:null,activity:``,lastPostAt:null,stale:!1,selection:``,branch:null,created:``,endedAt:null,approved:null,leftAsIs:null,missing:!0}}let a=i.state,o=a.selection.kind===`branch`||a.selection.kind===`other-branch`?a.selection.head:null;return{slug:i.slug,name:a.name,status:a.status,side:a.side,filedBy:a.filedBy,progress:Pi(i),relation:i.baseline===null||a.status!==`reviewing`?null:await n.of(i.baseline),activity:a.agent.activity,lastPostAt:a.agent.lastPostAt,stale:a.side===`agent`&&dt(a.agent.lastPostAt,r),selection:ot(a.selection),branch:o,created:a.created,endedAt:i.approval?.at??null,approved:i.approval===null?null:i.approval.approved,leftAsIs:i.approval?.leftAsIs??null,missing:!1}}function Fo(e,t,n){P(Ai(e,t),n)}export{oo as LINES_MAX,ao as PAGE_MAX,H as Refused,_o as createReviewsModule,wt as statusFor};
13
+ `),i=new Map;for(let e of r)for(let n of s.get(`${t}\n${e}`)??[])i.set(n,(i.get(n)??0)+1);for(let[t,n]of i){if(!f(e,t)||n*2<=Math.max(r.length,l.get(t)??0))continue;let i=Xn(e.content,t.content);i>=.5||d.push({from:e.id,to:t.id,cost:i})}}d.sort((e,t)=>e.cost-t.cost||e.from.localeCompare(t.from)||e.to.localeCompare(t.to));let p=new Set,m=new Set,h=[];for(let e of d)p.has(e.from)||m.has(e.to)||(p.add(e.from),m.add(e.to),h.push(e));return h}function Qn(e){return!yn(e.head)}var $n=[`main`,`master`];function er(e){return $n.includes(e)}var tr=[`4b825dc642cb6eb9a060e54bf8d69288fbee4904`,`6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321`];async function nr(e){for(let t of tr)if(await e.text(`cat-file-type`,t).catch(()=>``)===`tree`)return t;throw new L(`reviews`,`this repository has no empty tree to diff a first commit against`)}async function rr(e){return e.ok(`rev-parse-git-dir`)}async function ir(e){let t=await e.text(`rev-parse-abbrev`).catch(()=>``);return t===``||t===`HEAD`?null:t}async function ar(e){let t=await e.text(`for-each-ref`).catch(()=>``),n=[];for(let e of t.split(`
14
+ `)){if(e===``)continue;let[t=``,r=``,i=``]=e.split(` `);t===``||r===``||n.push({name:t,tip:r,upstream:i===``?null:i})}return n}function or(e){let t=[];for(let n of e.split(`
15
+ `)){if(n===``)continue;let e=n.indexOf(` `);e!==-1&&t.push({at:n.slice(0,e),said:n.slice(e+1)})}return t}async function sr(e,t){if(er(t))return null;let n=[],r=await ar(e),i=r.find(e=>e.name===t)?.upstream??null,a=e=>cr(e,t,i,new Set(r.map(e=>e.name))),o=or(await e.text(`reflog-show`,t).catch(()=>``)).at(-1)?.said??``,s=/^branch: Created from (.+)$/.exec(o)?.[1];s!==void 0&&s!==`HEAD`&&n.push({ref:s,foundBy:`branch-reflog`});let c=or(await e.text(`reflog-head`).catch(()=>``));for(let e of[...c].reverse()){let r=/^checkout: moving from (.+) to (.+)$/.exec(e.said);if(r&&r[2]===t&&r[1]!==void 0&&r[1]!==t){n.push({ref:r[1],foundBy:`head-reflog`});break}}i!==null&&n.push({ref:i,foundBy:`upstream`});for(let e of $n)n.push({ref:e,foundBy:`merge-base`});for(let r of n){if(a(r.ref))continue;let n=await lr(e,r.ref,t);if(n!==null)return{ref:r.ref,hash:n,foundBy:r.foundBy}}return null}function cr(e,t,n,r){return e===t||e===`refs/heads/${t}`?!0:e.endsWith(`/${t}`)?e.startsWith(`refs/remotes/`)?!0:e===n&&!r.has(e):!1}async function lr(e,t,n){let r=(await e.text(`merge-base`,t,n).catch(()=>``)).trim();return yn(r)?r:null}async function ur(e,t,n){return e.ok(`merge-base-ancestor`,t,n)}function dr(e){let t=(Buffer.isBuffer(e)?e.toString(`utf8`):e).split(`\0`),n=[];for(let e=0;e<t.length;e+=1){let r=t[e];if(r===void 0||r===``)continue;let i=r.slice(0,2),a=r.slice(3);if(a===``)continue;let o=i.includes(`R`)||i.includes(`C`),s=o?t[e+1]??null:null;o&&(e+=1),n.push({code:i,path:a,oldPath:s,untracked:i===`??`})}return n}async function fr(e){return dr(await e.bytes(`status`))}async function pr(e){return(await e.bytes(`ls-files-others`)).toString(`utf8`).split(`\0`).filter(e=>e!==``)}function mr(e){let t=[];for(let n of e.split(`
16
+ `)){if(n===``)continue;let e=n.split(` `);e.length<5||t.push({hash:e[0]??``,parents:(e[1]??``).split(` `).filter(e=>e!==``),author:e[2]??``,date:e[3]??``,subject:e.slice(4).join(` `)})}return t}async function hr(e,t,n){let r=gr(n.base)?null:n.base,i=Qn(t)?`log`:`log-first-parent`;return mr(await e.text(i,r,n.head).catch(()=>``))}function gr(e){return tr.includes(e)}async function _r(e,t){let n=await yr(e,t);if(Qn(t))return{base:n,head:await e.commit(t.head),tree:t.uncommitted,extended:!1,rewritten:!1};if(t.followed!==null)return{base:n,head:t.followed,tree:!1,extended:!1,rewritten:!1};let r=await e.commit(`HEAD`).catch(()=>null);if(!(r!==null&&await ur(e,t.head,r)))return{base:n,head:t.head,tree:t.uncommitted,extended:!1,rewritten:r!==null};if(!t.extends)return{base:n,head:t.head,tree:t.uncommitted,extended:!1,rewritten:!1};let i=await vr(e,t.head,r),a=i!==t.head;return{base:n,head:i,tree:t.uncommitted,extended:a,rewritten:!1}}async function vr(e,t,n){if(t===n)return n;let r=mr(await e.text(`log-first-parent`,t,n).catch(()=>``)),i=t;for(let e of[...r].reverse()){if(e.parents.length>1)return i;i=e.hash}return i}async function yr(e,t){return!((t.kind===`branch`||t.kind===`other-branch`)&&t.baseRef!==null&&t.baseFoundBy!==`chosen`)||t.baseRef===null?t.base:await lr(e,t.baseRef,t.head)??t.base}async function br(e,t){let n=t.base===void 0?null:await e.commit(t.base);if(t.kind===`branch`||t.kind===`other-branch`){let r=t.branch??await ir(e);if(r===null)throw new L(`reviews`,`there is no branch checked out`);let i=await e.commit(r),a=n===null?await sr(e,r):{ref:t.base??null,hash:n,foundBy:`chosen`};if(a===null)throw new L(`reviews`,er(r)?`${r} is the main line and has no base`:`the base of ${r} could not be found`);if(a.hash===i)throw new L(`reviews`,`${r} holds nothing its base does not`);return{kind:t.kind,base:a.hash,baseRef:a.ref,baseFoundBy:a.foundBy,head:r,uncommitted:t.kind===`branch`?t.uncommitted??!1:!1,followed:null,extends:!1}}if(t.kind===`range`){if(t.from===void 0||t.to===void 0)throw new L(`reviews`,`a range names both of its ends`);let r=n??await e.commit(t.from),i=await e.commit(t.to);if(r===i){let e=i.slice(0,12);throw new L(`reviews`,`the range is empty: from is excluded, so name its parent as from - ${e}^..${e} reviews that one commit`)}return{kind:`range`,base:r,baseRef:n===null?t.from:t.base??null,baseFoundBy:n===null?`range`:`chosen`,head:i,uncommitted:t.uncommitted??!1,followed:null,extends:await Sr(e,t,i)}}let r=t.kind===`last-commit`?`HEAD`:t.commit??``;if(r===``)throw new L(`reviews`,`no commit was named`);let i=await e.commit(r),a=await Sr(e,t,i);if(n!==null)return{kind:t.kind,base:n,baseRef:t.base??null,baseFoundBy:`chosen`,head:i,uncommitted:t.uncommitted??!1,followed:null,extends:a};let o=await e.commit(`${i}^`).catch(()=>null);return{kind:t.kind,base:o??await nr(e),baseRef:null,baseFoundBy:o===null?`root`:`parent`,head:i,uncommitted:t.uncommitted??!1,followed:null,extends:a}}async function xr(e,t){return await e.commit(`HEAD`).catch(()=>null)===t}async function Sr(e,t,n){if(t.extend!==!0)return!1;if(!await xr(e,n))throw new L(`reviews`,`only a selection that ends at HEAD can include newer commits`);return!0}async function Cr(e,t){let n=await e.commit(t),r=await e.commit(`${n}^`).catch(()=>null);return{kind:`commit`,base:r??await nr(e),baseRef:null,baseFoundBy:r===null?`root`:`parent`,head:n,uncommitted:!1,followed:n,extends:!1}}async function wr(e,t){let n=await _r(e,t),r=Qt(await e.bytes(`diff`,n.base,n.tree?null:n.head,n.tree)),i=r.files.length,a=0,o=0,s=0;for(let e of r.files){a+=e.hunks.length===0?1:e.hunks.length;let t=dn(e);o+=t.added,s+=t.deleted}if(n.tree)for(let t of await pr(e)){let n=Qt(await e.bytes(`diff-no-index`,t));for(let e of n.files){let t=dn(e);a+=e.hunks.length===0?1:e.hunks.length,o+=t.added,s+=t.deleted}i+=n.files.length}return{files:i,hunks:a,added:o,deleted:s,commits:await hr(e,t,n),uncommittedFiles:t.uncommitted?(await fr(e)).length:0}}async function Tr(e){let t={options:[],notOffered:null};if(!await rr(e)||await e.commit(`HEAD`).catch(()=>null)===null)return t;let n=[],r=(await fr(e)).length,i=await ir(e),a=async t=>{try{return n.push(await Dr(e,t,r)),null}catch(e){return e instanceof Error?e:Error(String(e))}},o=null;if(i!==null){let e=await a({kind:`branch`,uncommitted:r>0});e!==null&&(o={branch:i,reason:e.message})}await a({kind:`last-commit`});for(let t of await ar(e))t.name!==i&&await a({kind:`other-branch`,branch:t.name});return{options:n,notOffered:o}}async function Er(e,t){return Dr(e,t,(await fr(e)).length)}async function Dr(e,t,n){let r=await br(e,t);return{kind:r.kind,label:Or(r),selection:r,summary:await wr(e,r),uncommitted:{offered:r.kind===`branch`,files:n},extendable:!Qn(r)&&await xr(e,r.head)}}function Or(e){let t=e=>e.slice(0,12);return e.kind===`branch`||e.kind===`other-branch`?`${e.head} against ${e.baseRef??t(e.base)}`:e.kind===`last-commit`?`the last commit, ${t(e.head)}`:e.kind===`range`?`${t(e.base)}..${t(e.head)}`:`commit ${t(e.head)}`}var kr=[`package-lock.json`,`pnpm-lock.yaml`,`yarn.lock`,`Cargo.lock`,`go.sum`,`poetry.lock`,`Gemfile.lock`,`composer.lock`],Ar=200;async function jr(e,t){let n=new Set;for(let r=0;r<t.length;r+=Ar){let i=(await e.text(`check-attr`,t.slice(r,r+Ar))).split(`\0`);for(let e=0;e+2<i.length;e+=3){let t=i[e],r=i[e+2];t!==void 0&&t!==``&&r===`set`&&n.add(t)}}return n}function Mr(e,t,n){if(e.binary)return`binary`;let r=e.path.split(`/`).pop()??e.path;return kr.includes(r)||t.has(e.path)?`generated`:n.added+n.deleted>5e3?`oversize`:`text`}async function Nr(e,t){let n=t.filter(e=>!e.binary).map(e=>e.path),r=n.length===0?new Set:await jr(e,n),i=new Map;for(let e of t){let t=dn(e),n=Mr(e,r,t);i.set(e,{category:n,added:t.added,deleted:t.deleted}),n!==`text`&&(e.hunks=[])}return i}var Pr={hunks:2e3,patchBytes:20*1024*1024,blobBytes:100*1024*1024},Fr=class extends Error{code=`too-large`;constructor(e){super(`reviews: ${e}`),this.name=`TooLarge`}};function Ir(e){return S(`sha256`).update(e).digest(`hex`)}function Lr(e,t){if(t===``||t.startsWith(`/`)||t.split(`/`).includes(`..`))throw new L(`reviews`,`${t} is not a path inside the workspace`);return y(e,t)}function Rr(e,t){try{return i(y(e,t)).isFile()}catch{return!1}}async function zr(e,t,n){let r={...Pr,...n.limits},i=await _r(e,t),a=await Br(e,i,n.workspace,r.blobBytes).catch(e=>{throw e instanceof vn&&e.tooMuch?new Fr(`this selection holds more than ${z(mn)} of diff; a narrower selection will open`):e}),o=a.parsed,s=[...a.patches],c=new Set;if(i.tree)for(let t of(await pr(e)).sort()){if(!Rr(n.workspace,t))continue;let r=await e.bytes(`diff-no-index`,t);s.push(r);for(let e of Qt(r).files)o.files.push(e),c.add(e.path)}let l=s.reduce((e,t)=>e+t.length,0);if(l>r.patchBytes)throw new Fr(`this selection holds ${z(l)} of diff, past the limit of ${z(r.patchBytes)}; a narrower selection will open`);let u=await Nr(e,o.files),d=await Gr(e,o,n.workspace,r.blobBytes,i.tree),f=Wn(o,{resolveOid:(e,t)=>d.hashes.get(e)?.[t]??null});if(f.entries.length>r.hunks)throw new Fr(`this selection holds ${f.entries.length} hunks, past the limit of ${r.hunks}; a narrower selection will open`);let p=o.files.map(e=>{let t=u.get(e)??{category:`text`,...dn(e)},n=d.stored.get(e)??{old:null,new:null},r=e=>e===null?null:d.blobs.get(e)?.length??null;return{path:e.path,oldPath:e.oldPath,status:e.status,similarity:e.similarity,oldMode:e.oldMode,newMode:e.newMode,oldOid:e.oldOid,newOid:e.newOid,binary:e.binary,category:t.category,oldBlob:n.old,newBlob:n.new,oldBytes:r(n.old),newBytes:r(n.new),added:t.added,deleted:t.deleted,hunks:e.hunks,untracked:c.has(e.path)}}),m=f.entries.map(e=>({id:e.id,fileKey:e.fileKey,contentHash:e.contentHash,ordinal:e.ordinal,file:e.file,hunk:e.hunk,facts:e.nonTextual?e.content:null}));return{record:{version:1,computedAt:new Date().toISOString(),selection:t,resolved:i,commits:await hr(e,t,i),tree:i.tree?await qr(e,o,n.workspace,r.blobBytes):null,files:p,entries:m,renames:f.renames,fingerprint:f.fingerprint,totals:{files:p.length,hunks:m.length,added:p.reduce((e,t)=>e+t.added,0),deleted:p.reduce((e,t)=>e+t.deleted,0),patchBytes:l,blobBytes:d.bytes}},patch:Buffer.concat(s),blobs:d.blobs}}function z(e){return`${(e/(1024*1024)).toFixed(1)} MB`}async function Br(e,t,n,r){let i=t.tree?null:t.head,a=await e.bytes(`diff`,t.base,i,t.tree),o=Qt(a),s=[];for(let i of o.files)i.binary&&await Vr(e,i,n,r,t.tree)&&(i.oldPath!==null&&s.push(i.oldPath),s.push(i.path));if(s.length===0)return{parsed:o,patches:[a]};let c=await e.bytes(`diff`,t.base,i,t.tree,s),l=await e.bytes(`diff-text`,t.base,i,t.tree,s);return{parsed:{files:[...Qt(c).files,...Qt(l).files]},patches:[c,l]}}async function Vr(e,t,n,r,i){for(let a of[`old`,`new`]){let o=a===`old`?t.oldMode:t.newMode;if(/^0+$/.test(o)||o===`160000`)continue;let s=await Hr(e,t,a,n,r,i).catch(()=>null);if(s===null||s.subarray(0,Wr).includes(0))return!1}return!0}async function Hr(e,t,n,r,i,a){let o=n===`old`?t.oldOid:t.newOid;return Ur(n,o,a)?Kr(r,n===`old`?t.oldPath??t.path:t.path,i):e.bytes(`cat-file-blob`,o)}function Ur(e,t,n){return n&&e===`new`||/^0+$/.test(t)}var Wr=8e3;async function Gr(e,t,n,r,i){let a={blobs:new Map,stored:new Map,hashes:new Map,bytes:0};for(let o of t.files){let t={old:null,new:null},s={old:null,new:null};for(let c of[`old`,`new`]){let l=c===`old`?o.oldOid:o.newOid,u=c===`old`?o.oldMode:o.newMode;if(/^0+$/.test(u)||u===`160000`)continue;let d=Ur(c,l,i),f=await Hr(e,o,c,n,r,i);if(f===null)continue;let p=d?Ir(f):l;if(d&&(s[c]=p),t[c]=p,!a.blobs.has(p)){if(a.bytes+=f.length,a.bytes>r)throw new Fr(`this selection holds ${z(a.bytes)} of file content, past the limit of ${z(r)}; a narrower selection will open`);a.blobs.set(p,f)}}a.stored.set(o,t),a.hashes.set(o,s)}return a}function Kr(e,t,n){let r=Lr(e,t),a;try{a=i(r)}catch{return null}if(a.isSymbolicLink())return Buffer.from(l(r),`utf8`);if(!a.isFile())return null;if(a.size>n)throw new Fr(`${t} is ${z(a.size)}, past the limit of ${z(n)} for one file; a narrower selection will open`);return s(r)}async function qr(e,t,n,r){let a=(await e.bytes(`status`)).toString(`utf8`),o=[],s=new Set;for(let e of t.files){if(s.has(e.path)||/^0+$/.test(e.newMode)||e.newMode===`160000`)continue;s.add(e.path);let t=Lr(n,e.path),a=Kr(n,e.path,r);if(a===null)continue;let c;try{c=i(t)}catch{continue}o.push({path:e.path,size:a.length,mtimeMs:c.mtimeMs,hash:Ir(a)})}return{porcelain:a,files:o}}function Jr(e){return y(e,k)}function Yr(e){return y(Jr(e),j)}function Xr(e){return y(Yr(e),nt)}function Zr(e,t){let n=Jr(e),r=y(n,Ze);f(r,{recursive:!0,force:!0}),a(y(r,tt),{recursive:!0});for(let[e,n]of t.blobs)P(ri(r,e),n);P(y(r,et),t.patch),P(y(r,$e),`${JSON.stringify(t.record,null,2)}\n`),Qr(n)}function Qr(e){let t=y(e,j),r=y(e,Ze),i=y(e,Qe);n(t)&&(f(i,{recursive:!0,force:!0}),d(t,i)),d(r,t),f(i,{recursive:!0,force:!0})}function $r(e){let t=Jr(e),r=y(t,j),i=y(t,Ze),a=y(t,Qe),o=n(y(i,$e));if(n(r)){f(a,{recursive:!0,force:!0}),o||f(i,{recursive:!0,force:!0});return}if(o){Qr(t);return}f(i,{recursive:!0,force:!0}),n(a)&&d(a,r)}function ei(e){let t=y(Jr(e),j,$e);if(!n(t))return null;let r;try{r=JSON.parse(s(t,`utf8`))}catch{return null}if(!ti(r))return null;try{En(r.resolved.base),En(r.resolved.head)}catch{return null}return r.selection.extends=r.selection.extends===!0,r}function ti(e){if(typeof e!=`object`||!e||!Array.isArray(e.files)||!Array.isArray(e.entries)||!Array.isArray(e.commits))return!1;let t=e.resolved;return typeof t==`object`&&!!t&&typeof t.base==`string`&&typeof t.head==`string`&&typeof e.fingerprint==`string`&&typeof e.selection==`object`&&e.selection!==null}function ni(e){if(!/^[0-9a-f]{40}$|^[0-9a-f]{64}$/.test(e))throw new N(`reviews`,e);return`${k}/${j}/${tt}/${e}`}function ri(e,t){if(!/^[0-9a-f]{40}$|^[0-9a-f]{64}$/.test(t))throw new N(`reviews`,t);return y(e,tt,t)}function ii(e){return e.entries.map(t=>({id:t.id,fileKey:t.fileKey,contentHash:t.contentHash,ordinal:t.ordinal,content:ai(e,t)}))}function ai(e,t){if(t.facts!==null)return t.facts;let n=t.hunk===null?void 0:e.files[t.file]?.hunks[t.hunk];return n===void 0?``:zn(n)}function oi(e,t){let n=new Set(t.entries.map(e=>e.id)),r=ci(e,t),i=new Map,a=new Set;for(let t of e.entries){let e=si(r(t.fileKey),t.contentHash,t.ordinal);n.has(e)&&(i.set(t.id,e),a.add(e))}let o=Zn(ii(e),ii(t),r),s=new Map;for(let e of o)i.set(e.from,e.to),s.set(e.to,e.from);let c=new Set(i.values()),l=new Set;for(let n of li(e,t,r)){let e=i.get(n);e!==void 0&&l.add(e)}return{lineage:i,same:a,paired:s,vanished:e.entries.filter(e=>!i.has(e.id)).map(e=>e.id),arrived:t.entries.filter(e=>!c.has(e.id)).map(e=>e.id),reset:l}}function si(e,t,n){return`${e}#${t}#${n}`}function ci(e,t){let n=new Map;return r=>{let i=n.get(r);if(i!==void 0)return i;let a=Kn(e.renames,t.renames,r);return n.set(r,a),a}}function li(e,t,n){let r=(e,t)=>{let n=new Map;for(let r of e.entries){let e=`${t(r.fileKey)}#${r.contentHash}`;n.set(e,(n.get(e)??0)+1)}return n},i=e=>e,a=r(e,n),o=r(t,i),s=new Set;for(let t of e.entries){let e=`${n(t.fileKey)}#${t.contentHash}`;(a.get(e)??0)!==(o.get(e)??0)&&s.add(t.id)}return s}var ui={done:`Agent done`,sync:`Synced`,stop:`Stopped`,follow:`Followed`};function di(e,t){let n=[];return t.changedSeen>0&&n.push(t.changedSeen===1?`1 hunk changed since you saw it`:`${t.changedSeen} hunks changed since you saw them`),t.threadsAnswered>0&&n.push(t.threadsAnswered===1?`1 thread answered`:`${t.threadsAnswered} threads answered`),t.newCommits>0&&n.push(t.newCommits===1?`1 new commit`:`${t.newCommits} new commits`),n.length===0?null:`${ui[e]}: ${n.join(`, `)}`}function fi(e,t){if(e===null)return 0;let n=new Set(e.commits.map(e=>e.hash));return t.commits.filter(e=>!n.has(e.hash)).length}var B=`reviews`;function pi(e,t){return Nt(B,e,t)}function mi(e,t){return Pt(B,e,t)}var V=class extends Error{code=`bad-request`;constructor(e){super(`reviews: ${e}`),this.name=`Refused`}},hi=new Map;function H(e,t){let n=(hi.get(e)??Promise.resolve()).then(t,t),r=n.then(()=>void 0,()=>void 0);return hi.set(e,r),r.then(()=>{hi.get(e)===r&&hi.delete(e)}),n}function gi(e){let t=e.split(`/`);return t.length===2&&t[0]===`archive`}function _i(e){let t=e.split(`/`);return t.some(e=>e===``||e===`.`||e===`..`)?!1:t.length===1?t[0]!==A:gi(e)}function U(e,t){let n=t.trim();if(!_i(n))throw new N(B,t);return pi(e,n)}function vi(e,t,n){return mi(e,`${W(t)}/${k}/${n}`)}function W(e){let t=e.trim();if(!_i(t))throw new N(B,e);return t}function G(e,t,n){return Kt(B,e,`${W(t)}/${k}/${n}`)}function K(e,t,n,r){Gt(B,e,`${W(t)}/${k}/${n}`,r)}function q(e){return`${JSON.stringify(e,null,2)}\n`}function yi(e){a(e,{recursive:!0});let t=mi(e,`.gitignore`);n(t)||P(t,`${it.join(`
17
+ `)}\n`)}function bi(e,t,r){let i=r(t)||`review`;if(!n(pi(e,i)))return i;let a=2;for(;n(pi(e,`${i}-${a}`));)a++;return`${i}-${a}`}function xi(e,t,r=new Date().toISOString()){let i=U(e,t);if(!n(i)||!p(i).isDirectory())throw new xt(B,t);let a=W(t),o=a.split(`/`).pop()??a,s=ei(i),c=vt(G(e,t,He),o);if(c===null)throw new xt(B,`${t}/${k}/${He}`);let l=G(e,t,Ue),u=G(e,t,We),d=G(e,t,Ge),f=G(e,t,Ke),m=Ci(e,a,[[Ue,l],[We,u],[Ge,d],[Ke,f]]),h=ye(l),g=xe(u),_=Ce(d),v=De(f),y=!1,ee=Di(i),b=s!==null&&[h.fingerprint,g.fingerprint,_.fingerprint,v.fingerprint].some(e=>e!==s.fingerprint);if(s!==null&&b){let e=Gi(s,{marks:h,threads:g,pending:_,dismissals:v},r);h=e.marks,g=e.threads,_=e.pending,v=e.dismissals,y=!0}let x=c,S=Ni(e,a),te=Ti(x,S,s,r);return{slug:a,dir:i,state:c,marks:h,threads:g,pending:_,dismissals:v,metadata:ee,approval:Ne(G(e,t,Je)),baseline:s,loops:S.length,repaired:y||te,broken:m}}function Si(e){if(e===null||e.trim()===``)return!1;try{let t=JSON.parse(e);return typeof t!=`object`||!t||Array.isArray(t)}catch{return!0}}function Ci(e,t,n){let r=n.filter(([,e])=>Si(e)).map(([e])=>e);if(r.length===0||gi(t))return r;for(let n of r)d(vi(e,t,n),vi(e,t,wi(e,t,n)));return r}function wi(e,t,r){let i=r.replace(/\.json$/,``),a=`${i}.broken.json`,o=2;for(;n(vi(e,t,a));)a=`${i}.broken-${o}.json`,o++;return a}function Ti(e,t,n,r){let i=t[t.length-1]??null,a=t=>e.log.some(e=>e.said===t),o=!1;return i!==null&&e.status===`reviewing`&&e.side===`you`&&n!==null&&i.fingerprint===n.fingerprint&&!a(Ei(i.items.length,t.length))&&(e.side=`agent`,e.agent={act:`process`,activity:`Working on your feedback`,groups:null,lastPostAt:r,instruction:e.agent.instruction},e.log.push({at:r,said:Ei(i.items.length,t.length)}),o=!0),o}function Ei(e,t){return`Sent to agent: ${e} ${e===1?`item`:`items`} - loop ${t}.`}function Di(e){try{return ke(s(Xr(e),`utf8`))}catch{return Oe()}}function Oi(e,t,n=[]){if(gi(t.slug)&&t.state.status===`archived`)throw new V(`${t.slug} is archived, so nothing is written into it`);let r=t.baseline?.fingerprint??``,i=new Set(n);i.has(`marks`)&&(t.marks.fingerprint=r,K(e,t.slug,Ue,q(t.marks))),i.has(`threads`)&&(t.threads.fingerprint=r,K(e,t.slug,We,q(t.threads))),i.has(`pending`)&&(t.pending.fingerprint=r,K(e,t.slug,Ge,q(t.pending))),i.has(`dismissals`)&&(t.dismissals.fingerprint=r,K(e,t.slug,Ke,q(t.dismissals))),i.has(`metadata`)&&(a(Yr(t.dir),{recursive:!0}),P(Xr(t.dir),q(t.metadata))),i.has(`approval`)&&t.approval!==null&&K(e,t.slug,Je,q(t.approval)),K(e,t.slug,He,q(t.state)),P(pi(e,`${t.slug}/README.md`),ki(t))}function ki(e){let t=Ii(e);return ct({state:e.state,seen:t.seen,total:t.total,loops:e.loops,fingerprint:e.baseline?.fingerprint??``,commit:e.baseline?.resolved.head??``,threads:{total:e.threads.threads.length,open:e.threads.threads.filter(e=>e.open).length},metadata:e.metadata,approval:e.approval})}function Ai(e,t){return je(G(e,t,qe))}function ji(e,t,n){K(e,t,qe,q(n))}function Mi(e,t){return vi(e,t,Ye)}function Ni(e,t){let r=y(U(e,t),k,Xe);if(!n(r))return[];let i=c(r).filter(e=>/^\d+\.json$/.test(e)).sort(),a=[];for(let n of i){let r=we(G(e,t,`${Xe}/${n}`));r!==null&&a.push(r)}return a}function Pi(e,t,r){let i=y(U(e,t),k,Xe),a=(n(i)?c(i).filter(e=>/^\d+\.json$/.test(e)):[]).reduce((e,t)=>Math.max(e,Number.parseInt(t,10)),0),o=`${String(a+1).padStart(2,`0`)}.json`;return K(e,t,`${Xe}/${o}`,q(r)),`${Xe}/${o}`}function Fi(e,t,r){let i=pi(e,r);if(n(i))throw new V(`${r} is already there`);a(_(i),{recursive:!0}),d(U(e,t),i)}function Ii(e,t){let n=ze(t??Ie(e),null);return{seen:n.seen,total:n.hunks,left:n.unseen}}function Li(e,t){let n=t??Ie(e),r=ze(n,null);return{unreadReplies:r.replies,issues:r.issues,attention:r.attention,changed:r.changed,removed:n.gone.length,waiting:r.waiting}}function Ri(e,t){let n=t??Ie(e),r=Li(e,n);return{unseen:Ii(e,n).left,issues:r.issues,attention:r.attention,threads:Le(n),unreadReplies:r.unreadReplies,pending:e.pending.items.length}}function zi(e,t){if(e.state.status!==`reviewing`||e.state.side!==`you`||e.baseline===null)return!1;let n=t??Ie(e);return Ii(e,n).total!==0&&!me(Ri(e,n))}function Bi(e,t){if(e===null)return[];let n=new Set;for(let e of t.groups)if(e.id!==`ungrouped`)for(let t of e.hunks)n.add(t);return e.entries.map(e=>e.id).filter(e=>!n.has(e))}function Vi(e,t,n){let r=new Set([de,...n.groups.map(e=>e.id)]),i=e.filter(e=>e.kind!==`group`||r.has(e.anchor)),a=[],o=[];for(let e of t)e.group===null||r.has(e.group)?a.push(e):o.push(e.id);return{threads:i,items:a,gone:o}}function Hi(e,t,n,r,i){let a=Ui(oi(e,t),t,n,r,i),o=r.fingerprint===e.fingerprint?Vi(a.threads.threads,a.pending.items,a.metadata):{threads:a.threads.threads,items:a.pending.items,gone:[]};return{...a,threads:{...a.threads,threads:o.threads},pending:{...a.pending,items:o.items},gone:o.gone}}function Ui(e,t,n,r,i){let a=new Set(t.entries.map(e=>e.id)),o=t=>{let n=e.lineage.get(t);return n!==void 0&&a.has(n)?n:null},s=[],c=[];for(let t of n.marks.marks){let n=o(t.hunk);if(n!==null){if(e.same.has(n)&&!e.reset.has(n)){s.push({...t,hunk:n});continue}e.paired.has(n)&&c.push({hunk:n,at:i,content:t.content})}}let l=c.length,u=new Set(c.map(e=>e.hunk));for(let e of n.marks.changed){let t=o(e.hunk);t===null||u.has(t)||c.push({...e,hunk:t})}let d=n.threads.threads.map(e=>{if(e.kind===`group`)return{...e};let t=o(e.anchor);return t===null?{...e,label:`hunk-removed`}:{...e,anchor:t,label:null}}),f=n.pending.items.map(e=>{if(e.hunk===null)return{...e};let t=o(e.hunk);return t===null?{...e,label:`hunk-removed`}:{...e,hunk:t,label:null}}),p=Wi(e,a,r,t.fingerprint),m=d,h=f,g=new Set(p.notes.map(e=>ue(e.hunk,e.key))),_=[];for(let e of n.dismissals.items){let t=o(e.hunk);t!==null&&g.has(ue(t,e.key))&&_.push({...e,hunk:t})}return{marks:{...n.marks,marks:s,changed:c,fingerprint:t.fingerprint},threads:{...n.threads,threads:m,fingerprint:t.fingerprint},pending:{...n.pending,items:h,fingerprint:t.fingerprint},dismissals:{...n.dismissals,items:_,fingerprint:t.fingerprint},metadata:p,changedSeen:l,gone:[]}}function Wi(e,t,n,r){let i=n=>{let r=e.lineage.get(n);return r!==void 0&&t.has(r)?r:null};return{...n,fingerprint:r,groups:n.groups.map(e=>({...e,hunks:e.hunks.map(e=>i(e)).filter(e=>e!==null)})),notes:n.notes.map(t=>{let n=i(t.hunk);return n!==null&&e.same.has(n)&&!e.reset.has(n)?{...t,hunk:n}:null}).filter(e=>e!==null)}}function Gi(e,t,n){let r=[];for(let e of t.marks.marks){let t=Jn(e.hunk);t!==null&&r.push({id:e.hunk,fileKey:t.fileKey,contentHash:t.contentHash,ordinal:t.ordinal,content:e.content})}let i=new Set(e.entries.map(e=>e.id)),a=ii(e),o=new Set,s=new Map;for(let e of r)i.has(e.id)&&(o.add(e.id),s.set(e.id,e.id));let c=new Map;for(let e of Zn(r,a))s.set(e.from,e.to),c.set(e.to,e.from);for(let e of i)s.has(e)||s.set(e,e);for(let e of o)s.set(e,e);return Ui({lineage:s,same:o,paired:c,vanished:[],arrived:[],reset:new Set},e,t,Oe(),n)}function Ki(e,t){let n=0;for(let r of t){if(!r.startsWith(e))continue;let t=Number(r.slice(e.length));Number.isInteger(t)&&t>n&&(n=t)}return`${e}${n+1}`}function qi(e,t,n){let r=e.threads.find(e=>e.open&&e.kind===t&&e.anchor===n);if(r!==void 0)return{thread:r,added:!1};let i={id:Ki(`t`,e.threads.map(e=>e.id)),kind:t,anchor:n,open:!0,label:null,closedAt:null,messages:[]};return e.threads.push(i),{thread:i,added:!0}}function Ji(e,t,n,r,i){let a={id:Ki(`m`,e.threads.flatMap(e=>e.messages.map(e=>e.id))),from:n,at:i,text:r,readAt:n===`agent`?null:i};return t.messages.push(a),a}function Yi(e){return{marks:ve(e),threads:be(e),pending:Se(e),dismissals:Ee(e)}}var J=(e=[])=>({wrote:e,typed:[],command:!1});function Y(e,t){if(e.state.status!==`reviewing`)throw new V(`this review is ${e.state.status}, so ${t} is not possible`);if(e.state.side!==`you`)throw new V(`this review awaits the agent, so ${t} is not possible`)}function Xi(e,t){let n=e.baseline;if(n===null)throw new V(`this review has no baseline`);let r=n.entries.find(e=>e.id===t);if(r===void 0)throw new V(`this review holds no hunk ${t}`);return{baseline:n,entry:r}}function Zi(e,t,n){Y(e,`marking a hunk`);let{baseline:r,entry:i}=Xi(e,t.hunk),a=e.marks;if(!t.seen)return a.marks.some(e=>e.hunk===t.hunk)?(ea(e,`unseen`,null,[t.hunk],n),a.marks=a.marks.filter(e=>e.hunk!==t.hunk),a.changed=a.changed.filter(e=>e.hunk!==t.hunk),J([`marks`])):J([]);let o=ea(e,`hunk`,null,[t.hunk],n),s={hunk:t.hunk,at:n,act:o,content:ai(r,i)};return a.marks=[...a.marks.filter(e=>e.hunk!==t.hunk),s],a.changed=a.changed.filter(e=>e.hunk!==t.hunk),J([`marks`])}function Qi(e,t,n){Y(e,`marking a group`);let r=e.baseline;if(r===null)throw new V(`this review has no baseline`);let i=$i(e,t),a=new Set(e.marks.marks.map(e=>e.hunk)),o=i.filter(e=>!a.has(e));if(o.length===0)throw new V(`nothing is left to mark in ${t.id}`);let s=ea(e,t.kind,t.id,o,n),c=new Map(r.entries.map(e=>[e.id,e]));for(let t of o){let i=c.get(t);i!==void 0&&e.marks.marks.push({hunk:t,at:n,act:s,content:ai(r,i)})}let l=new Set(o);return e.marks.changed=e.marks.changed.filter(e=>!l.has(e.hunk)),J([`marks`])}function $i(e,t){let n=e.baseline;if(n===null)return[];if(t.kind===`file`)return n.entries.filter(e=>e.fileKey===t.id).map(e=>e.id);if(t.id===`ungrouped`)return Bi(n,e.metadata);let r=e.metadata.groups.find(e=>e.id===t.id);if(r===void 0)throw new V(`this review holds no group ${t.id}`);let i=new Set(n.entries.map(e=>e.id));return r.hunks.filter(e=>i.has(e))}function ea(e,t,n,r,i){let a=Ki(`a`,e.marks.acts.map(e=>e.id));return e.marks.acts.push({id:a,at:i,kind:t,container:n,hunks:r}),a}function ta(e,t,n){Y(e,`commenting`);let{baseline:r,entry:i}=Xi(e,t.hunk);if(t.kind===`talk`){let{thread:a}=qi(e.threads,`hunk`,t.hunk);Ji(e.threads,a,`tool`,`Conversation request sent to the agent in the terminal.`,n);let o=r.files[i.file],s=i.hunk===null?null:o?.hunks[i.hunk]??null,c=t.lines===null?``:se(t.lines);return{wrote:[`threads`],typed:[{event:`talk`,detail:[e.state.name,o?.path??i.fileKey,s?.header??`the whole file`,c].filter(e=>e!==``).join(` - `),path:`${e.slug}/${k}/${We}`}],command:!1}}if(t.kind===`group-comment`)throw new V(`a group comment names its group, so it is posted as one`);return ra(e,t.kind,t.hunk,null,t.lines,t.text,n),J([`pending`])}function na(e,t,n){if(Y(e,`commenting`),!(t.group===`ungrouped`||e.metadata.groups.some(e=>e.id===t.group)))throw new V(`this review holds no group ${t.group}`);return ra(e,`group-comment`,null,t.group,null,t.text,n),J([`pending`])}function ra(e,t,n,r,i,a,o){let s=a.trim();if(s===``&&oe(t))throw new V("say something in `text`");let c={id:Ki(`p`,e.pending.items.map(e=>e.id)),at:o,act:t,hunk:n,group:r,lines:i!==null&&i.length>0?[...i]:null,text:s,label:null};return e.pending.items.push(c),c}function ia(e,t,n){Y(e,`changing the pending feedback`);let r=e.baseline;if(r===null)throw new V(`this review has no baseline`);let i=new Set(r.entries.map(e=>e.id)),a=new Set([de,...e.metadata.groups.map(e=>e.id)]),o=[];for(let e of t){if(e.hunk!==null&&!i.has(e.hunk)){o.push({...e,label:`hunk-removed`});continue}if(e.group!==null&&!a.has(e.group))throw new V(`this review holds no group ${e.group}`);o.push({...e,at:e.at||n,label:null})}return e.pending.items=o,e.pending.version=1,J([`pending`])}function aa(e,t,n){if(Y(e,`dismissing a note`),!e.metadata.notes.some(e=>e.hunk===t.hunk&&e.key===t.key))throw new V(`this review holds no such note`);let r=e.dismissals.items.filter(e=>!(e.hunk===t.hunk&&e.key===t.key));return e.dismissals.items=t.on?[...r,{hunk:t.hunk,key:t.key,at:n}]:r,J([`dismissals`])}function oa(e,t,n){Y(e,`closing a thread`);let r=e.threads.threads.find(e=>e.id===t);if(r===void 0)throw new V(`there is no thread called ${t}`);r.open=!1,r.closedAt=n;for(let e of r.messages)e.readAt===null&&(e.readAt=n);return J([`threads`])}function sa(e,t,n){if(t.length===0)return J();let r=new Set(t),i=!1;for(let t of e.threads.threads)for(let e of t.messages)e.readAt!==null||!r.has(e.id)||(e.readAt=n,i=!0);return J(i?[`threads`]:[])}function ca(e,t,n){Y(t,`sending`);let r=t.pending.items;if(r.length===0)throw new V(`there is nothing to send`);let i=t.baseline?.fingerprint??``,a=r.map(e=>({hunk:e.hunk,act:e.act,group:e.group,lines:e.lines,text:e.text})),o={version:1,sentAt:n,fingerprint:i,items:a};Pi(e.root,t.slug,o),t.loops+=1;for(let e of r){let r=e.hunk??e.group;if(r===null)continue;let{thread:i}=qi(t.threads,e.hunk===null?`group`:`hunk`,r),a=e.lines===null?``:` ${se(e.lines)}`,o=`${ae[e.act]}${a}`;Ji(t.threads,i,`you`,e.text?`${o}: ${e.text}`:o,n)}return t.pending.items=[],la(t,`process`,`Working on your feedback`,n),t.state.log.push({at:n,said:Ei(a.length,t.loops)}),{wrote:[`pending`,`threads`],typed:[],command:!0}}function la(e,t,n,r){e.state.side=`agent`,e.state.agent={act:t,activity:n,groups:null,lastPostAt:r,instruction:e.state.agent.instruction}}async function ua(e,t,n,r){let i=t.baseline,a=await zr(e.git,t.state.selection,{workspace:e.workspace,limits:e.limits}),o=0;if(i!==null){let n=Hi(i,a.record,{marks:t.marks,threads:t.threads,pending:t.pending,dismissals:t.dismissals},t.metadata,r);t.marks=n.marks,t.threads=n.threads,t.pending=n.pending,t.dismissals=n.dismissals,t.metadata=n.metadata,o=n.changedSeen,_a(e,t,n.gone)}Zr(t.dir,a),t.baseline=a.record;let s=di(n,{changedSeen:o,threadsAnswered:da(t,i),newCommits:fi(i,a.record)});return s!==null&&t.state.log.push({at:r,said:s}),{wrote:[`marks`,`threads`,`pending`,`dismissals`,`metadata`],typed:[],command:!1,line:s}}function da(e,t){if(t===null)return 0;let n=Date.parse(t.computedAt);return Number.isNaN(n)?0:e.threads.threads.filter(e=>e.messages.some(e=>{if(e.from!==`agent`)return!1;let t=Date.parse(e.at);return!Number.isNaN(t)&&t>n})).length}async function fa(e,t,n){Y(t,`syncing`);let r=await ua(e,t,`sync`,n);return la(t,`sync`,`Syncing`,n),{...r,typed:[],command:!0}}async function pa(e,t,n){if(t.state.status!==`reviewing`)throw new V(`this review is ${t.state.status}, so there is nothing to stop`);if(t.state.side!==`agent`)throw new V(`this review is already yours`);let r=await ua(e,t,`stop`,n);return t.state.side=`you`,t.state.agent={act:null,activity:``,groups:null,lastPostAt:t.state.agent.lastPostAt,instruction:t.state.agent.instruction},t.state.log.push({at:n,said:`stopped the agent`}),{...r,typed:[{event:`stopped`,detail:t.state.name,path:ba(t)}]}}async function ma(e,t,n){ha(t);let r=await ua(e,t,`done`,n);return t.state.side=`you`,t.state.agent={act:null,activity:``,groups:null,lastPostAt:n,instruction:``},r}function ha(e){let t=e.state.agent.act;if(t!==`prepare`&&t!==`sync`)return;let n=e.baseline;if(n===null)return;let r=Bi(n,e.metadata);if(r.length>0){let e=r.slice(0,3).join(`, `),t=r.length>3?` and ${r.length-3} more`:``;throw new V(`${r.length} hunk${r.length===1?` is`:`s are`} in no group: ${e}${t}. Group them, then post done again.`)}let i=e.state.agent.groups,a=e.metadata.groups.length;if(i!==null&&a<i)throw new V(`${a} of the ${i} groups you said were coming are posted. Post the rest, or say the new count with activity { groups }, then post done again.`)}function ga(e){let t=e.trim();if(t.length>2e3)throw new V(`an instruction to the agent is at most ${lt} characters, and this one is ${t.length}`);return t}function _a(e,t,n){if(n.length===0)return;let r=new Set(n.map(e=>`pending:${e}`)),i=Ai(e.root,t.slug),a=i.drafts.filter(e=>!r.has(e.anchor));a.length!==i.drafts.length&&ji(e.root,t.slug,{...i,drafts:a})}function va(e,t,n,r){Y(t,`resetting the review`);let i=t.baseline;if(i===null)throw new V(`this review has no baseline to prepare`);let a=ga(n);t.metadata=Oe(i.fingerprint),t.dismissals.items=[];let o=Vi(t.threads.threads,t.pending.items,t.metadata);t.threads.threads=o.threads,t.pending.items=o.items,_a(e,t,o.gone),t.state.agent.instruction=a,la(t,`prepare`,`Preparing from nothing`,r);let s=a.split(`
18
+ `)[0]?.trim()??``;return t.state.log.push({at:r,said:s===``?`reset the review`:`reset the review: ${s}`}),{wrote:[`metadata`,`threads`,`pending`,`dismissals`],typed:[],command:!0}}async function ya(e,t,n){Y(t,`following a merge`);let r=t.baseline;if(r===null)throw new V(`this review has no baseline`);let i=await e.relations.mergeOf(r);if(i===null)throw new V(`no commit on the base holds this change`);return t.state.selection=await Cr(e.git,i),t.state.log.push({at:n,said:`followed the merge to ${i}`}),{...await ua(e,t,`follow`,n),typed:[{event:`followed`,detail:i,path:ba(t)}]}}function ba(e){return`${e.slug}/${k}/baseline/baseline.json`}function xa(e,t,n){Y(e,`approving`);let r=e.baseline;if(r===null)throw new V(`this review has no baseline`);let i=t.name.trim();if(i===``)throw new V(`an approval carries the name that signed it`);let a=Ri(e),o=he(a);if(o>0&&!t.acknowledged)throw new V(`this approval leaves ${o} ${o===1?`thing`:`things`} as ${o===1?`it is`:`they are`}; acknowledge them to sign`);let s=t.signature===null?null:Sa(t.signature);return e.approval={version:1,approved:!0,at:n,name:i,signature:s===null?null:Ye,reason:``,loops:e.loops,fingerprint:r.fingerprint,commit:r.resolved.head,uncommitted:r.resolved.tree,leftAsIs:o>0?a:null},e.state.status=`approved`,e.state.log.push({at:n,said:o>0?`approved by ${i} with ${o} ${o===1?`thing`:`things`} left as ${o===1?`it was`:`they were`}`:`approved by ${i}`}),{...J([`approval`]),signature:s}}function Sa(e){let t=/^data:image\/png;base64,([A-Za-z0-9+/=]+)$/.exec(e.trim());if(t===null||t[1]===void 0)throw new V(`a signature is a PNG data URL drawn by the client`);return Buffer.from(t[1],`base64`)}function Ca(e,t,n){Y(e,`closing without approval`);let r=t.trim();if(r===``)throw new V(`closing without approval carries a reason`);let i=e.baseline,a=Ii(e);return e.approval={version:1,approved:!1,at:n,name:``,signature:null,reason:r,loops:e.loops,fingerprint:i?.fingerprint??``,commit:i?.resolved.head??``,uncommitted:i?.resolved.tree??!1,leftAsIs:null},e.state.status=`closed`,e.state.side=`you`,e.state.agent={act:null,activity:``,groups:null,lastPostAt:e.state.agent.lastPostAt,instruction:e.state.agent.instruction},e.state.log.push({at:n,said:`closed without approval with ${a.seen} of ${a.total} hunks Seen`}),J([`approval`])}function wa(e,t){let n=e=>String(e).padStart(2,`0`);return`${A}/${`${t.getFullYear()}-${n(t.getMonth()+1)}-${n(t.getDate())}`}-${e}`}function Ta(e,t){if(e.state.status!==`approved`&&e.state.status!==`closed`)throw new V(`a review is archived once it is approved or closed`);return e.state.status=`archived`,e.state.log.push({at:t,said:`archived`}),J()}var Ea=.8;function Da(e,t){let n=new Map,r=async t=>{let r=await Oa(e,t);if(r===null)return null;let i=`${r}\n${t.fingerprint}`,a=n.get(i);if(a!==void 0)return a;let o=await Aa(e,t,r);return n.set(i,o),o};return{of:async n=>{let i=Qn(n.selection)?n.selection.head:null,a=await ir(e),o=await ar(e),s=i===null?null:o.find(e=>e.name===i)??null;if(i!==null){let t=await ka(e,n,s?.tip??n.resolved.head);if(s===null||t){let e=await r(n);return e===null?t?{relation:`merged`,branch:i,mergedAs:null,why:`the base holds the branch`}:{relation:`history-rewritten`,branch:i,mergedAs:null,why:`the branch is gone and no commit on the base holds its change`}:{relation:`merged`,branch:i,mergedAs:e,why:`merged as ${e}`}}if(n.selection.kind===`branch`&&a!==i)return{relation:`different-branch`,branch:i,mergedAs:null,why:`${a??`no branch`} is checked out`}}if(n.selection.baseRef!==null&&await Oa(e,n)===null)return{relation:`history-rewritten`,branch:i,mergedAs:null,why:`${n.selection.baseRef} is gone`};let c=await _r(e,n.selection).catch(()=>null);if(c===null)return{relation:`history-rewritten`,branch:i,mergedAs:null,why:`the selection no longer resolves`};if(c.rewritten)return{relation:`history-rewritten`,branch:i,mergedAs:null,why:`HEAD no longer descends from the head of this review`};if(i!==null&&!await ur(e,n.resolved.head,c.head))return{relation:`history-rewritten`,branch:i,mergedAs:null,why:`${i} no longer holds the commits this review was of`};if(c.base!==n.resolved.base)return{relation:`out-of-sync`,branch:i,mergedAs:null,why:`the base moved`};if(c.head!==n.resolved.head)return{relation:`out-of-sync`,branch:i,mergedAs:null,why:`new commits`};let l=await Na(e,n,t.workspace);return l===null?{relation:`current`,branch:i,mergedAs:null,why:`the tree is the baseline`}:{relation:`out-of-sync`,branch:i,mergedAs:null,why:l}},mergeOf:r}}async function Oa(e,t){let n=t.selection.baseRef;return n===null?null:e.commit(n).catch(()=>null)}async function ka(e,t,n){let r=await Oa(e,t);return r!==null&&ur(e,n,r)}async function Aa(e,t,n){if(t.entries.length===0)return null;let r=t.resolved.base,i=(await e.text(`log-first-parent`,r,n).catch(()=>``)).split(`
19
+ `).filter(e=>e!==``).map(e=>e.split(` `)[0]??``).filter(e=>e!==``).slice(0,200),a=new Set(t.entries.map(e=>e.id)),o=t.entries.filter(e=>e.hunk!==null).length;for(let n of i){if(!ja(await e.text(`diff-tree`,n).catch(()=>``),o))continue;let r=await e.commit(`${n}^`).catch(()=>null);if(r===null)continue;let i=Wn(Qt(await e.bytes(`diff`,r,n,!1)));if(i.fingerprint===t.fingerprint||i.entries.filter(e=>a.has(e.id)).length/a.size>=.8)return n}return null}function ja(e,t){let n=0,r=0;for(let t of e.split(`
20
+ `)){if(t===``)continue;let e=t.split(` `);n+=Ma(e[0])+Ma(e[1]),r+=1}return r!==0&&n>=Math.floor(t*Ea)}function Ma(e){let t=Number(e);return Number.isFinite(t)?t:0}async function Na(e,t,n){if(t.tree===null)return null;if((await e.bytes(`status`)).toString(`utf8`)!==t.tree.porcelain)return`the working tree changed`;let r=b(n);for(let e of t.tree.files){let t=Pa(r,e);if(t!==null)return t}return null}function Pa(e,t){let n=e;try{n=u(e)}catch{}let r=b(n,t.path);if(!Ft(n,r))return`${t.path} is not in the workspace`;let a;try{if(!Ft(n,u(_(r))))return`${t.path} is not in the workspace`;a=i(r)}catch{return`${t.path} is gone`}let o=a.isSymbolicLink();if(!o&&!a.isFile())return`${t.path} is not a file any more`;try{let e=o?Buffer.byteLength(l(r)):a.size;if(e!==t.size)return`${t.path} was edited`;if(a.mtimeMs===t.mtimeMs)return null;if(e>Pr.blobBytes)return`${t.path} is too large to check`;let n=o?Buffer.from(l(r),`utf8`):s(r);return S(`sha256`).update(n).digest(`hex`)===t.hash?null:`${t.path} was edited`}catch{return`${t.path} is gone`}}var Fa=[`stages`,`state`,`start`,`metadata`,`group`,`ungroup`,`reply`,`activity`,`done`,`log`],Ia=[`metadata`,`group`,`ungroup`,`reply`,`activity`,`done`],La=[...Ia,`log`];function Ra(e,t){if(La.includes(t)){if(e.state.status!==`reviewing`)throw new V(`this review is ${e.state.status}, so it takes no more work`);if(Ia.includes(t)&&e.state.side!==`agent`)throw new V(`this review awaits the reviewer, so there is nothing to post`)}}var za=e=>typeof e==`object`&&!!e&&!Array.isArray(e);function X(e,t){if(!za(e))throw new V(`${t} is an object`);return e}var Z=(e,t=``)=>typeof e==`string`?e:t,Ba=e=>Array.isArray(e)?e:[];function Va(e){let t=Z(X(e,`this`).said).trim();if(t===``)throw new V("say something in `said`");return t}function Ha(e){let t=Va(e),n=X(e,`this`).groups;if(n==null)return{said:t,groups:null};if(typeof n!=`number`||!Number.isInteger(n)||n<0)throw new V("`groups` is how many groups there will be, as a whole count");return{said:t,groups:n}}function Ua(e){let t=X(e,`a reply`),n=Z(t.thread).trim(),r=Z(t.text).trim();if(n===``)throw new V("a reply names the thread it is on in `thread`");if(r===``)throw new V("a reply carries its words in `text`");return{thread:n,text:r}}function Wa(e){let t=X(e,`metadata`),n=Ga(t,`metadata`),r=[],i=new Set;for(let e of Ba(t.groups)){let t=Ka(e);if(i.has(t.id))throw new V(`two groups are called ${t.id}`);i.add(t.id),r.push(t)}return{fingerprint:n,groups:r,notes:Ba(t.notes).map(Ja)}}function Ga(e,t){let n=Z(e.fingerprint).trim();if(n===``)throw new V(`${t} names the baseline it was written for in \`fingerprint\``);return n}function Ka(e){let t=X(e,`a group`),n=Z(t.id).trim();if(n===``)throw new V("a group names itself in `id`");if(n===`ungrouped`)throw new V(`${de} is the tool's own group and is not posted`);return{id:n,name:qa(t,n),narrative:Z(t.narrative),facts:Ba(t.facts).filter(e=>typeof e==`string`),hunks:Ba(t.hunks).filter(e=>typeof e==`string`)}}function qa(e,t){if(e.name===void 0||e.name===null)return t;let n=Z(e.name).trim();if(n===``||n.length>60)throw new V(`the name of group \`${t}\` is ${n.length} characters; a name is 60 at most`);return n}function Ja(e){let t=X(e,`a note`),n=Z(t.hunk).trim();if(n===``)throw new V("a note names the hunk it is on in `hunk`");let r=Z(t.kind);if(!ce.includes(r))throw new V("a note is `info`, `attention` or `issue`");let i=Z(t.text).trim();if(i===``)throw new V("a note carries its words in `text`");let a=t.lines===void 0||t.lines===null?null:Ba(t.lines).filter(e=>Number.isInteger(e));if(a!==null&&a.length===0)throw new V(`a note that names lines names at least one`);return{hunk:n,kind:r,lines:a,text:i,key:le(r,a,i)}}function Ya(e){let t=X(e,`a group post`),n=Ga(t,`a group post`),r=Ka(t.group),i=Ba(t.notes).map(Ja),a=new Set(r.hunks);for(let e of i)if(!a.has(e.hunk))throw new V(`a note posted with a group is on one of its hunks; ${e.hunk} is not`);return{fingerprint:n,group:r,notes:i}}function Xa(e){let t=X(e,`an ungroup post`),n=Ga(t,`an ungroup post`),r=Z(t.id).trim();if(r===``)throw new V("name the group to remove in `id`");return{fingerprint:n,id:r}}function Za(e,t){let n=eo(e,t.fingerprint);return to(n,t.groups,t.notes),no(e,n,t.groups,t.notes)}function Qa(e,t){let n=eo(e,t.fingerprint);to(n,[t.group],t.notes);let r=e.metadata.groups.some(e=>e.id===t.group.id)?e.metadata.groups.map(e=>e.id===t.group.id?t.group:e):[...e.metadata.groups,t.group],i=new Set(t.group.hunks);return no(e,n,r,[...e.metadata.notes.filter(e=>!i.has(e.hunk)),...t.notes])}function $a(e,t){let n=eo(e,t.fingerprint);if(!e.metadata.groups.some(e=>e.id===t.id))throw new V(`this review holds no group called ${t.id}`);return no(e,n,e.metadata.groups.filter(e=>e.id!==t.id),e.metadata.notes)}function eo(e,t){let n=e.baseline;if(n===null)throw new V(`this review has no baseline to group`);if(t!==n.fingerprint)throw new V(`the baseline changed; the review awaits you`);return n}function to(e,t,n){let r=new Map(e.entries.map(e=>[e.id,e]));for(let e of t)for(let t of e.hunks)if(!r.has(t))throw new V(`${e.id} names ${t}, which this baseline does not hold`);for(let t of n){let n=r.get(t.hunk);if(n===void 0)throw new V(`a note names ${t.hunk}, which this baseline does not hold`);ro(e,n,t)}}function no(e,t,n,r){let i={version:1,fingerprint:t.fingerprint,groups:n,notes:r};return e.metadata=i,{ungrouped:Bi(t,i),groups:n.length,notes:r.length}}function ro(e,t,n){if(n.lines===null)return;let r=t.hunk===null?null:e.files[t.file]?.hunks[t.hunk]??null;if(r===null)throw new V(`${n.hunk} changes no line, so a note on it names no lines`);let i=r.lines.map(e=>e.newLine).filter(e=>e!==null),a=i[0],o=i[i.length-1];if(a===void 0||o===void 0)throw new V(`${n.hunk} shows no new-side line, so a note on it names no lines`);for(let e of n.lines)if(e<a||e>o)throw new V(`a note names line ${e}, which is outside ${n.hunk}`)}function io(e,t,n){let r=e.threads.threads.find(e=>e.id===t.thread);if(r===void 0)throw new V(`there is no thread called ${t.thread}`);if(!r.open)throw new V(`${t.thread} is closed`);return Ji(e.threads,r,`agent`,t.text,n),{thread:r.id}}function ao(e,t,n,r=null){e.state.agent.activity=t,r!==null&&(e.state.agent.groups=r),e.state.agent.lastPostAt=n}function oo(e,t,n){e.state.log.push({at:n,said:t}),e.state.side===`agent`&&(e.state.agent.lastPostAt=n)}var so=200,co=2e3,lo=e=>typeof e==`object`&&!!e&&!Array.isArray(e);function Q(e,t){if(!lo(e))throw new V(`${t} is an object`);return e}var $=(e,t=``)=>typeof e==`string`?e:t,uo=e=>Array.isArray(e)?e:[],fo=(e,t=0)=>typeof e==`number`&&Number.isFinite(e)?e:t,po=e=>uo(e).filter(e=>typeof e==`string`);function mo(e){if(e==null)return null;let t=uo(e).map(e=>fo(e)).filter(e=>Number.isInteger(e)&&e>0);return t.length===0?null:t}function ho(e){let t=$(e.hunk).trim();if(t===``)throw new V("name the hunk in `hunk`");return t}function go(e){let t=Q(e,`a selection`),n=$(t.kind),r=[`branch`,`last-commit`,`commit`,`range`,`other-branch`];if(!r.includes(n))throw new V(`a selection is one of ${r.join(`, `)}`);let i={kind:n,uncommitted:t.uncommitted===!0};return typeof t.branch==`string`&&(i.branch=t.branch),typeof t.commit==`string`&&(i.commit=t.commit),typeof t.from==`string`&&(i.from=t.from),typeof t.to==`string`&&(i.to=t.to),typeof t.base==`string`&&(i.base=t.base),t.extend===!0&&(i.extend=!0),i}function _o(e){let t=Q(e,`a review`);return{name:$(t.name).trim(),selection:go(t.selection)}}function vo(e){let t=Q(e,`the pending feedback`),n=[];for(let e of uo(t.items)){let t=Q(e,`a pending item`),r=$(t.act);if(!ie.includes(r))throw new V(`an item is one of ${ie.join(`, `)}`);let i=typeof t.hunk==`string`?t.hunk:null,a=typeof t.group==`string`?t.group:null;if(i===null&&a===null)throw new V(`an item names a hunk or a group`);let o=$(t.text).trim();if(o===``&&r!==`undo`)throw new V("an item says something in `text`");let s=mo(t.lines);n.push({id:$(t.id),at:$(t.at),act:r,hunk:i,group:a,lines:s,text:o,label:null})}return n}function yo(e){let t=At(e.root),r=At(e.workspace),i=e.clock??(()=>new Date),a=Pn({workspace:r,exclude:[t]}),o=Da(a,{workspace:r}),s=e.onChange,c=s?Jt(t,s,{shared:rt,entries:`children`,coalesce:`per-entry`}):null,l=n=>({root:t,git:a,relations:o,workspace:r,limits:e.limits,at:n}),d=(e,n)=>bo(t,e,ni(n)),f=e=>bo(t,e,`${k}/${Ye}`),p=null,m=async()=>(p===null&&(p=await Oo(a)),p),h=e=>`${t}\n${W(e)}`,g,_=async()=>{if(g!==void 0)return g;if(!await a.ok(`rev-parse-git-dir`))return g=`This workspace is not a git repository.`,g;let e=(await a.text(`rev-parse-top`).catch(()=>``)).trim();return g=e!==``&&u(e)===u(r)?null:`This workspace is inside the repository at ${e}, not the top of it, so a review of it would be a review of the wrong paths.`,g},v=(e,n)=>($r(U(t,W(e))),xi(t,e,n)),y=(e,t)=>H(h(e),async()=>{let n=i().toISOString();return t(v(e,n),n)}),ee=[`marks`,`threads`,`pending`,`dismissals`],b=async(e,n)=>H(h(e),async()=>{let r=i().toISOString(),a=v(e,r),o=await n(a,r),s=a.repaired?[...new Set([...o.wrote,...ee])]:o.wrote;return Oi(t,a,s),{review:await S(a),typed:o.typed,command:o.command,line:o.line??null}}),x=async(e,n,r,a)=>H(h(e),async()=>{let o=i().toISOString(),s=v(e,o);Ra(s,n);let c=await r(s,o);return Oi(t,s,a(c)),c}),S=async e=>{let n=i(),r=Ie(e);return{slug:e.slug,state:e.state,baseline:e.baseline===null?null:ko(e.baseline),marks:Eo(e.marks),threads:e.threads,pending:e.pending,dismissals:e.dismissals,metadata:e.metadata,ungrouped:Bi(e.baseline,e.metadata),relation:e.baseline===null?null:await o.of(e.baseline),loops:e.loops,progress:Ii(e,r),attention:Li(e,r),leftAsIs:Ri(e,r),ready:zi(e,r),stale:e.state.side===`agent`&&dt(e.state.agent.lastPostAt,n),approval:e.approval,place:Ai(t,e.slug),configuredName:await m(),error:Do(e)}};return{root:t,workspace:r,async list(){let e=await _(),n=[],a=[];for(let e of wo(t)){let r=await H(h(e),()=>($r(U(t,W(e))),Io(t,e,o,i())));(gi(e)?a:n).push(r)}let s=(e,t)=>e.slug.localeCompare(t.slug);return n.sort(s),a.sort((e,t)=>s(t,e)),{root:Co(t,r),active:n,archived:a,awaitingYou:n.filter(e=>!e.missing&&e.status===`reviewing`&&e.side===`you`).length,any:n.length+a.length>0,repository:e===null,error:e}},async review(e){return y(e,e=>S(e))},hunks(e,n,r){return Fo(xi(t,e,i().toISOString()),n,r)},blobLines(e,t,n,r){return Po(d(e,t),t,n,r)},options:async e=>{if(e==null)return Tr(a);try{return{options:[await Er(a,go(e))],notOffered:null}}catch(e){throw e instanceof V||e instanceof L?e:new V(e instanceof Error?e.message:`that is not a selection`)}},async start(n,o=`form`){let s=await _();if(s!==null)throw new V(s);let l=_o(n);return H(`${t}\n`,async()=>{let n=await br(a,l.selection),s=await zr(a,n,{workspace:r,limits:e.limits});if(s.record.entries.length===0)throw new V(`Nothing to review in this selection.`);let u=i().toISOString(),d=l.name||ot(n);yi(t);let f=bi(t,d,ne),p=U(t,f),m=Yi(s.record.fingerprint),h={slug:f,dir:p,state:_t(d,f,n,o,u),marks:m.marks,threads:m.threads,pending:m.pending,dismissals:m.dismissals,metadata:{version:1,fingerprint:s.record.fingerprint,groups:[],notes:[]},approval:null,baseline:s.record,loops:0,repaired:!1,broken:[]};return Oi(t,h,[`marks`,`threads`,`pending`,`dismissals`]),Zr(p,s),c?.refollow(),{slug:f,path:`${f}/README.md`,state:h.state,command:!0}})},async act(e,n,r){switch(n){case`mark`:return b(e,(e,t)=>{let n=Q(r,`a mark`);return Zi(e,{hunk:ho(n),seen:n.seen!==!1},t)});case`bulk`:return b(e,(e,t)=>{let n=Q(r,`a bulk act`),i=$(n.kind);if(i!==`file`&&i!==`group`)throw new V("a bulk act covers a `file` or a `group`");let a=$(n.id).trim();if(a===``)throw new V("a bulk act names what it covers in `id`");return Qi(e,{kind:i,id:a},t)});case`comment`:return b(e,(e,t)=>{let n=Q(r,`a comment`),i=$(n.kind),a=[`undo`,`explain`,`comment`,`fix`,`talk`];if(!a.includes(i))throw new V(`a comment is one of ${a.join(`, `)}`);let o=mo(n.lines);return ta(e,{hunk:ho(n),kind:i,text:$(n.text),lines:o},t)});case`dismiss`:return b(e,(e,t)=>{let n=Q(r,`a dismissal`),i=$(n.key).trim();if(i===``)throw new V("name the note in `key`");return aa(e,{hunk:ho(n),key:i,on:n.on!==!1},t)});case`group-comment`:return b(e,(e,t)=>{let n=Q(r,`a group comment`),i=$(n.group).trim();if(i===``)throw new V("a group comment names its group in `group`");return na(e,{group:i,text:$(n.text)},t)});case`close-thread`:return b(e,(e,t)=>{let n=$(Q(r,`a thread`).thread).trim();if(n===``)throw new V("name the thread in `thread`");return oa(e,n,t)});case`pending`:return b(e,(e,t)=>ia(e,vo(r),t));case`send`:return b(e,(e,t)=>ca(l(t),e,t));case`sync`:return b(e,(e,t)=>fa(l(t),e,t));case`reset`:return b(e,(e,t)=>{let n=lo(r)?r:{};return va(l(t),e,$(n.instruction),t)});case`stop`:return b(e,(e,t)=>pa(l(t),e,t));case`follow`:return b(e,(e,t)=>ya(l(t),e,t));case`approve`:return b(e,(e,n)=>{let i=Q(r,`an approval`),a=typeof i.signature==`string`?i.signature:null,o=xa(e,{name:$(i.name),signature:a,acknowledged:i.acknowledged===!0},n);return o.signature!==null&&Lo(t,e.slug,o.signature),o});case`close`:return b(e,(e,t)=>Ca(e,$(Q(r,`a closing`).reason),t));case`archive`:return H(h(e),async()=>{let n=i().toISOString(),r=v(e,n),a=r.state.status===`archived`&&!gi(r.slug)?{wrote:[],typed:[],command:!1}:Ta(r,n);Oi(t,r,r.repaired?[`marks`,`threads`,`pending`]:[]);let o=wa(r.slug,i());return Fi(t,r.slug,o),r.slug=o,r.dir=U(t,o),{review:await S(r),typed:a.typed,command:!1,line:null}});case`place`:return H(h(e),async()=>{let n=i().toISOString(),a=W(e);if(gi(a))throw new V(`this review is filed away, so it keeps no reading place`);let o=Q(r,`a place`),s={version:1,scroll:fo(o.scroll),expanded:po(o.expanded),group:typeof o.group==`string`?o.group:null,drafts:Me(o.drafts),at:n};ji(t,a,s);let c=po(o.read);if(c.length===0)return{review:null,typed:[],command:!1,line:null};let l=v(a,n),u=sa(l,c,n),d=l.repaired?[...new Set([...u.wrote,...ee])]:u.wrote;return d.length>0&&Oi(t,l,d),{review:await S(l),typed:[],command:!1,line:null}});default:throw new V(`there is no act called ${n}`)}},stages:()=>({root:t,limits:{...Pr,...e.limits},verbs:Fa,reviews:wo(t)}),async agentState(e){return y(e,e=>{let n=Ni(t,e.slug);return{slug:e.slug,name:e.state.name,status:e.state.status,side:e.state.side,act:e.state.agent.act,activity:e.state.agent.activity,instruction:e.state.agent.instruction,selection:ot(e.state.selection),filedBy:e.state.filedBy,loops:e.loops,baseline:e.baseline===null?null:Mo(e.baseline),metadata:e.metadata,ungrouped:Bi(e.baseline,e.metadata),threads:e.threads.threads,send:n[n.length-1]??null,awaits:No(e)}})},metadata:(e,t)=>x(e,`metadata`,e=>Za(e,Wa(t)),()=>[`metadata`]),group:(e,t)=>x(e,`group`,e=>Qa(e,Ya(t)),()=>[`metadata`]),ungroup:(e,t)=>x(e,`ungroup`,e=>$a(e,Xa(t)),()=>[`metadata`]),reply:(e,t)=>x(e,`reply`,(e,n)=>{let r=io(e,Ua(t),n);return ao(e,e.state.agent.activity||`Answering`,n),r},()=>[`threads`]),activity:(e,t)=>x(e,`activity`,(e,n)=>{let r=Ha(t);return ao(e,r.said,n,r.groups),{said:r.said}},()=>[]),done:e=>x(e,`done`,async(e,t)=>{let n=await ma(l(t),e,t);return{fingerprint:e.baseline?.fingerprint??``,line:n.line??null,wrote:n.wrote}},e=>e.wrote).then(e=>({fingerprint:e.fingerprint,line:e.line})),log:(e,t)=>x(e,`log`,(e,n)=>{let r=Va(t);return oo(e,r,n),{said:r}},()=>[]),blob:d,signature:f,raw(e){let t=e.split(`/`),n=t.pop()??``;if(n===`signature.png`)return f(t.join(`/`));if(t.pop()===`blobs`)return d(t.join(`/`),n);throw new N(B,e)},toolPath(e){if(!rt(e))throw new N(B,e);let r=mi(t,e);if(!n(r))throw new St(B,e);return r},file:e=>Bt(B,t,e),close:()=>c?.close()}}function bo(e,t,n){return Vt(B,e,`${W(t)}/${n}`,So)}var xo=RegExp(`^${j}/${tt}/([0-9a-f]{40}|[0-9a-f]{64})$`);function So(e){let t=e.indexOf(`/${k}/`);if(t===-1)return!1;let n=e.slice(t+k.length+2);return n===`signature.png`||xo.test(n)}function Co(e,t){return e===t?`.`:`${e.startsWith(`${t}/`)?e.slice(t.length+1):e}/`}function wo(e){let t=[];for(let n of To(e)){if(n===`archive`){for(let r of To(pi(e,n)))t.push(`archive/${r}`);continue}t.push(n)}return t.sort()}function To(e){try{return c(e,{withFileTypes:!0}).filter(e=>e.isDirectory()&&!e.name.startsWith(`.`)).map(e=>e.name)}catch{return[]}}function Eo(e){return{version:e.version,fingerprint:e.fingerprint,marks:e.marks.map(({hunk:e,at:t,act:n})=>({hunk:e,at:t,act:n})),acts:e.acts,changed:e.changed.map(({hunk:e,at:t})=>({hunk:e,at:t}))}}function Do(e){if(e.baseline===null)return`${k}/${j}/baseline.json is not there to be read, so there is nothing left to review; closing this review without approval is what is left`;if(e.broken.length===0)return null;let t=e.broken.map(e=>e.replace(/\.json$/,`.broken.json`));return`${e.broken.join(` and `)} could not be read and started again; what was there is kept as ${t.join(` and `)}`}async function Oo(e){return(await e.text(`config-user-name`).catch(()=>``)).trim()}function ko(e){let t=new Map;for(let n of e.entries)t.has(n.file)||t.set(n.file,n.fileKey);return{fingerprint:e.fingerprint,computedAt:e.computedAt,resolved:e.resolved,commits:e.commits,files:e.files.map((e,n)=>Ao(e,t.get(n)??e.path)),entries:e.entries.map(t=>jo(e,t)),totals:e.totals}}function Ao(e,t){return{key:t,path:e.path,oldPath:e.oldPath,status:e.status,similarity:e.similarity,binary:e.binary,category:e.category,untracked:e.untracked,added:e.added,deleted:e.deleted,oldBlob:e.oldBlob,newBlob:e.newBlob,oldBytes:e.oldBytes,newBytes:e.newBytes}}function jo(e,t){let n=e.files[t.file],r=t.hunk===null?null:n?.hunks[t.hunk]??null;return{id:t.id,file:t.file,header:r?.header??``}}function Mo(e){return{fingerprint:e.fingerprint,computedAt:e.computedAt,totals:e.totals,files:e.files.map((t,n)=>({path:t.path,oldPath:t.oldPath,hunks:e.entries.filter(e=>e.file===n).map(e=>({id:e.id,header:e.hunk===null?``:t.hunks[e.hunk]?.header??``}))}))}}function No(e){let t=e.state;if(t.status!==`reviewing`)return{side:t.side,act:null,reason:`this review is ${t.status}`};if(t.side===`you`)return{side:`you`,act:null,reason:`the reviewer has this review`};let n=t.agent.act===`prepare`?`group the baseline and write its narratives`:t.agent.act===`process`?`work through the newest send`:`group what is new and revise the narratives the new code made wrong`;return{side:`agent`,act:t.agent.act,reason:n}}function Po(e,t,n,r){let i=e.bytes.toString(`utf8`).split(`
21
+ `);i[i.length-1]===``&&i.pop();let a=Math.max(1,Math.trunc(n)),o=Math.min(co,Math.max(1,Math.trunc(r)));return{blob:t,from:a,total:i.length,lines:i.slice(a-1,a-1+o)}}function Fo(e,t,n){let r=e.baseline;if(r===null)throw new V(`this review has no baseline`);let i=Math.max(0,Math.trunc(t)),a=Math.min(200,Math.max(1,Math.trunc(n))),o=r.entries.slice(i,i+a),s=[];for(let e of o){let t=r.files[e.file];if(t===void 0)continue;let n=e.hunk===null?null:t.hunks[e.hunk]??null;s.push({id:e.id,file:e.file,path:t.path,oldPath:t.oldPath,status:t.status,binary:t.binary,untracked:t.untracked,oldBlob:t.oldBlob,newBlob:t.newBlob,header:n?.header??``,heading:n?.heading??``,oldStart:n?.oldStart??0,oldLines:n?.oldLines??0,newStart:n?.newStart??0,newLines:n?.newLines??0,lines:n?.lines??[],nonTextual:e.hunk===null,facts:e.facts})}return{from:i,count:s.length,total:r.entries.length,fingerprint:r.fingerprint,hunks:s}}async function Io(e,t,n,r){let i;try{i=xi(e,t,r.toISOString())}catch{return{slug:t,name:t.split(`/`).pop()??t,status:`reviewing`,side:`you`,filedBy:`form`,progress:{seen:0,total:0,left:0},relation:null,activity:``,lastPostAt:null,stale:!1,selection:``,branch:null,created:``,endedAt:null,approved:null,leftAsIs:null,missing:!0}}let a=i.state,o=a.selection.kind===`branch`||a.selection.kind===`other-branch`?a.selection.head:null;return{slug:i.slug,name:a.name,status:a.status,side:a.side,filedBy:a.filedBy,progress:Ii(i),relation:i.baseline===null||a.status!==`reviewing`?null:await n.of(i.baseline),activity:a.agent.activity,lastPostAt:a.agent.lastPostAt,stale:a.side===`agent`&&dt(a.agent.lastPostAt,r),selection:ot(a.selection),branch:o,created:a.created,endedAt:i.approval?.at??null,approved:i.approval===null?null:i.approval.approved,leftAsIs:i.approval?.leftAsIs??null,missing:!1}}function Lo(e,t,n){P(Mi(e,t),n)}export{co as LINES_MAX,so as PAGE_MAX,V as Refused,yo as createReviewsModule,wt as statusFor};