chamba 0.7.0 → 0.9.0
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.
- package/LICENSE +53 -14
- package/README.md +34 -8
- package/dist/commands/advanced.js +7 -278
- package/dist/commands/dev.js +11 -612
- package/dist/commands/doctor.js +1 -29
- package/dist/commands/menu.js +1 -80
- package/dist/commands/onboard.js +6 -230
- package/dist/commands/settings.js +15 -349
- package/dist/lib/agent-commands.js +2 -0
- package/dist/lib/agent-context.js +6 -210
- package/dist/lib/browser.js +1 -40
- package/dist/lib/chamba-yaml.js +10 -191
- package/dist/lib/constants.js +1 -125
- package/dist/lib/dockerfile-builder.js +41 -261
- package/dist/lib/env.js +2 -78
- package/dist/lib/git-env.js +1 -21
- package/dist/lib/global-config.js +4 -66
- package/dist/lib/pnpm-store.js +1 -19
- package/dist/lib/ports.js +1 -210
- package/dist/lib/safe-rm.js +1 -36
- package/dist/lib/sessions.js +2 -34
- package/dist/lib/shadows.js +1 -176
- package/dist/lib/skills.js +2 -0
- package/dist/lib/templates.js +1 -0
- package/dist/lib/webterm.js +1 -305
- package/dist/lib/workspace-identity.js +4 -260
- package/package.json +4 -4
- package/templates/Dockerfile +15 -22
- package/templates/claude-statusline.sh +5 -7
- package/templates/context/baseline.md +4 -2
- package/templates/context/web-pane-craft.md +1 -1
- package/templates/pane-apps/client/assets/api-BBDf_rzz.js +12 -0
- package/templates/pane-apps/client/assets/api-DNLbSBpK.css +1 -0
- package/templates/pane-apps/client/assets/bee-B7t97l5B.webp +0 -0
- package/templates/pane-apps/client/assets/buzz-CUUsbnXH.mp3 +0 -0
- package/templates/pane-apps/client/assets/c-BNa-DeUl.json +3552 -0
- package/templates/pane-apps/client/assets/cpp-B-T4NRoH.json +20239 -0
- package/templates/pane-apps/client/assets/csharp-BbxHChhA.json +6010 -0
- package/templates/pane-apps/client/assets/css-CWCxuwmz.json +1860 -0
- package/templates/pane-apps/client/assets/diff-B-2N89g8.json +154 -0
- package/templates/pane-apps/client/assets/docker-DWTvYW26.json +103 -0
- package/templates/pane-apps/client/assets/files-BkmyervY.js +1 -0
- package/templates/pane-apps/client/assets/go-B2hyYeNg.json +3156 -0
- package/templates/pane-apps/client/assets/graphql-Z6Id2aNO.json +1292 -0
- package/templates/pane-apps/client/assets/home-3SeGzySM.css +1 -0
- package/templates/pane-apps/client/assets/home-Cu9xOd2i.js +1 -0
- package/templates/pane-apps/client/assets/html-BtzvlIfu.json +2627 -0
- package/templates/pane-apps/client/assets/ini-Bn3Twap1.json +108 -0
- package/templates/pane-apps/client/assets/java-C7t2dwLO.json +1885 -0
- package/templates/pane-apps/client/assets/javascript-DuC2v0C0.json +5996 -0
- package/templates/pane-apps/client/assets/json-CwdkWeV5.json +206 -0
- package/templates/pane-apps/client/assets/jsonc-MYWDbTU3.json +206 -0
- package/templates/pane-apps/client/assets/jsx-Cn8EZuIC.json +5996 -0
- package/templates/pane-apps/client/assets/kotlin-CFNOz1gw.json +580 -0
- package/templates/pane-apps/client/assets/make-BVnlFCcl.json +629 -0
- package/templates/pane-apps/client/assets/markdown-B4Xk2WM_.json +3263 -0
- package/templates/pane-apps/client/assets/php-CAQzQmmI.json +4188 -0
- package/templates/pane-apps/client/assets/pop-D5ZANBC3.mp3 +0 -0
- package/templates/pane-apps/client/assets/python-CYQee8m4.json +4174 -0
- package/templates/pane-apps/client/assets/reviews-DMrGB-kG.js +153 -0
- package/templates/pane-apps/client/assets/reviews-nK_yRav9.css +1 -0
- package/templates/pane-apps/client/assets/ruby-D7R58tus.json +3114 -0
- package/templates/pane-apps/client/assets/rust-BFBbFiei.json +1103 -0
- package/templates/pane-apps/client/assets/scss-TQRhBkPG.json +1874 -0
- package/templates/pane-apps/client/assets/shellscript-DieAkC4W.json +2375 -0
- package/templates/pane-apps/client/assets/specs-Cabb15F0.js +13 -0
- package/templates/pane-apps/client/assets/sql-Bxd030LV.json +634 -0
- package/templates/pane-apps/client/assets/toml-ChrKGzsV.json +430 -0
- package/templates/pane-apps/client/assets/tsx-D2e90MFw.json +5996 -0
- package/templates/pane-apps/client/assets/typescript-DonXQ_n1.json +5747 -0
- package/templates/pane-apps/client/assets/useNow-BHFKFraS.js +1 -0
- package/templates/pane-apps/client/assets/xml-B17r2RXF.json +382 -0
- package/templates/pane-apps/client/assets/yaml-CKn4h1zy.json +627 -0
- package/templates/pane-apps/client/home/index.html +16 -0
- package/templates/pane-apps/client/reviews/index.html +17 -0
- package/templates/pane-apps/client/specs/index.html +4 -2
- package/templates/pane-apps/server/home.mjs +12 -0
- package/templates/pane-apps/server/reviews.mjs +21 -0
- package/templates/pane-apps/server/specs.mjs +5 -1588
- package/templates/skills/chamba-statusline/SKILL.md +7 -19
- package/templates/skills/dx-review/SKILL.md +136 -0
- package/templates/skills/dx-review/references/acts.md +233 -0
- package/templates/skills/dx-spec/SKILL.md +230 -30
- package/templates/skills/dx-spec/references/principles-template.md +2 -0
- package/templates/skills/dx-spec/references/review-guide.md +6 -52
- package/templates/skills/dx-spec/references/spec-guide.md +25 -5
- package/templates/skills/dx-spec/references/stages.md +149 -60
- package/templates/skills/dx-spec-execute/SKILL.md +217 -38
- package/templates/startup.mjs +22 -16
- package/templates/tool-helper.sh +168 -0
- package/templates/webterm/README.md +82 -27
- package/templates/webterm/color.js +61 -0
- package/templates/webterm/config.js +55 -4
- package/templates/webterm/context/claude.md +0 -1
- package/templates/webterm/public/app/alerts.js +95 -14
- package/templates/webterm/public/app/anchored.js +81 -0
- package/templates/webterm/public/app/composer.js +37 -3
- package/templates/webterm/public/app/connection.js +23 -0
- package/templates/webterm/public/app/dom.js +3 -0
- package/templates/webterm/public/app/frames.js +52 -18
- package/templates/webterm/public/app/home-host.js +16 -0
- package/templates/webterm/public/app/main.js +24 -8
- package/templates/webterm/public/app/new-session.js +13 -41
- package/templates/webterm/public/app/palette.js +58 -0
- package/templates/webterm/public/app/pane-shape.js +169 -0
- package/templates/webterm/public/app/pane-shell.js +240 -135
- package/templates/webterm/public/app/pane.js +39 -24
- package/templates/webterm/public/app/reviews-host.js +19 -0
- package/templates/webterm/public/app/specs-host.js +12 -218
- package/templates/webterm/public/app/state.js +5 -0
- package/templates/webterm/public/app/tabs.js +6 -2
- package/templates/webterm/public/app/theme.js +75 -27
- package/templates/webterm/public/app/tool-host.js +318 -0
- package/templates/webterm/public/app/workspace-color.js +192 -0
- package/templates/webterm/public/index.html +22 -4
- package/templates/webterm/public/styles.css +195 -21
- package/templates/webterm/server.js +198 -150
- package/templates/webterm/tool-document.js +3 -2
- package/templates/webterm/tools/commands.js +61 -0
- package/templates/webterm/tools/home.js +58 -0
- package/templates/webterm/tools/index.js +26 -0
- package/templates/webterm/tools/mount.js +337 -0
- package/templates/webterm/tools/paths.js +34 -0
- package/templates/webterm/tools/reviews.js +137 -0
- package/templates/webterm/tools/specs.js +154 -0
- package/templates/webterm/typed-line.js +70 -7
- package/templates/context/context-usage.md +0 -1
- package/templates/context-usage.sh +0 -266
- package/templates/pane-apps/client/assets/specs-B1970L17.css +0 -1
- package/templates/pane-apps/client/assets/specs-cEee_SPn.js +0 -23
- package/templates/skills/dx-spec-config/SKILL.md +0 -313
- package/templates/skills/dx-spec-config/references/principles-template.md +0 -12
- package/templates/specs.sh +0 -106
- package/templates/webterm/specs.js +0 -358
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import{existsSync as e,mkdirSync as t,readFileSync as n,realpathSync as r,renameSync as i,rmSync as a,writeFileSync as o}from"node:fs";import{basename as s,dirname as c,isAbsolute as l,join as u,relative as d,resolve as f,sep as p}from"node:path";import{execFile as ee}from"node:child_process";import{createHash as te}from"node:crypto";import{lstat as ne,readdir as re}from"node:fs/promises";function ie(e){let t=e?.code;return typeof t==`string`&&ae.has(t)?t:null}var ae=new Set([`path-refused`,`no-entry`,`no-file`,`bad-request`,`too-large`,`upstream-failed`,`unreadable-answer`]),m=class extends Error{requested;code=`path-refused`;constructor(e,t){super(`${e}: refused path ${t}`),this.requested=t,this.name=`PathRefused`}},oe=class extends Error{requested;code=`no-file`;constructor(e,t,n=`not a file`){super(`${e}: ${n} - ${t}`),this.requested=t,this.name=`NoSuchFile`}};function h(e){switch(ie(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 g(e)?404:500}function g(e){let t=e?.code;return t===`ENOENT`||t===`ENOTDIR`}var se=`/usr/bin/git`,ce=3e4,le=64*1024*1024,ue=[`core.fsmonitor=false`,`core.hooksPath=/dev/null`],_=[`--no-color`,`--no-ext-diff`,`--no-textconv`],de={GIT_CONFIG_NOSYSTEM:`1`,GIT_CONFIG_GLOBAL:`/dev/null`,GIT_OPTIONAL_LOCKS:`0`,LC_ALL:`C`,PATH:`/usr/bin:/bin`},v=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`}},y=class extends Error{code=`bad-request`;constructor(e,t){super(`${e}: ${t}`),this.name=`NotAllowed`}};function b(e){return/^[0-9a-f]{40}$|^[0-9a-f]{64}$/.test(e)}function fe(e){let t=t=>new y(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(!b(e))throw t(`${e} is not an object id`);return e},asCommit:e=>{if(!b(e))throw t(`${e} is not a resolved commit`);return e},refuse:t}}function pe(e){let{tool:t,table:n,workspace:r}=e,i=[...e.config??[],...ue].flatMap(e=>[`-c`,e]),a=new Set(e.answersWithStatus??[]),o=(e,...r)=>{let a=n[e];if(!a)throw new y(t,`no git operation named ${String(e)}`);return[...i,...a(...r)]},s=(e,n)=>new Promise((i,o)=>{ee(se,n,{cwd:r,env:{...de},encoding:`buffer`,maxBuffer:le,timeout:ce,shell:!1,windowsHide:!0},(n,r,s)=>{let c=Buffer.isBuffer(r)?r:Buffer.from(String(r)),l=x(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 v(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 v(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||!b(i))throw new y(t,`there is no commit at ${e}`);return i}}}function x(e){return(Buffer.isBuffer(e)?e.toString(`utf8`):String(e)).split(`
|
|
2
|
+
`).map(e=>e.trim()).filter(Boolean)[0]??``}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 me(e){return e.startsWith(`.`)}function S(e){let t=e,n=[];for(;;)try{return u(r(t),...n)}catch{let r=c(t);if(r===t)return e;n.unshift(s(t)),t=r}}function C(e){return S(f(e))}function w(e){return e.split(`/`).some(e=>e!==``&&me(e))}function he(e,t,n,r){let i=n.replace(/^\/+/,``);if(l(n)||i.includes(`\0`))throw new m(e,n);let a=r===`require`;if(w(i)!==a)throw new m(e,n);let o=S(f(t)),s=f(o,i);if(!E(o,s)||!E(o,S(s))||w(d(o,s).split(p).join(`/`))!==a)throw new m(e,n);return s}function T(e,t,n){return he(e,t,n,`require`)}function E(e,t){return t===e||t.startsWith(e+p)}var ge=[`main`,`master`],_e=7;async function D(e,t,n){if(!await e.ok(`rev-parse-git-dir`))return null;let r=await e.text(`rev-parse-top`).catch(()=>null);if(r===null||C(r)!==C(t))return null;let i=await e.text(`symbolic-ref-head`).catch(()=>null),a=i===null||i===``,o=a?await e.text(`rev-parse`,`HEAD`).catch(()=>null):null;return{name:n===void 0||n===``?s(C(t)):n,branch:a?o?.slice(0,_e)??`HEAD`:i,detached:a}}async function ve(e,t){let n=await e.text(`symbolic-ref`).catch(()=>null),r=n?.startsWith(`refs/remotes/origin/`)?n.slice(20):null;if(r!==null&&r!==``&&await O(e,r)!==null)return r;for(let t of ge)if(await O(e,t)!==null)return t;return t!==null&&await O(e,t)!==null?t:null}async function O(e,t){let n=await e.text(`rev-parse`,`refs/heads/${t}`).catch(()=>null);return n===null||n===``?null:n}async function ye(e){return be(await e.text(`status`))}function be(e){return k(e).length}function k(e){let t=e.split(`\0`),n=[];for(let e=0;e<t.length;e++){let r=t[e];if(r===void 0||r===``)continue;let i=r.slice(0,2);n.push({status:i,path:r.slice(3)}),(i.startsWith(`R`)||i.startsWith(`C`))&&(e+=1)}return n}async function A(e){return await e.text(`rev-parse-shallow`).catch(()=>`false`)===`true`}var{mustBeValue:j,mustBeOid:M,refuse:N}=fe(`home`),xe=[`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`],Se=`4b825dc642cb6eb9a060e54bf8d69288fbee4904`,Ce=`%x00%H%x09%aI`;function we(e){let t=`%(refname:short)%09%(objectname)%09%(authordate:iso-strict)%09%(tree)`;return e===null?t:`${t}%09%(ahead-behind:${M(e)})`}function P(e,t){let n=j(`a ref`,t);return e===null?[n]:[`${j(`a ref`,e)}..${n}`]}function F(e){if(!Number.isInteger(e)||e<=0)throw N(`${e} is not a count`);return String(e)}function I(e){if(!/^\d{4}-\d{2}-\d{2}$/.test(e))throw N(`${e} is not a date`);return e}var L={"rev-parse":e=>[`rev-parse`,`--verify`,`--quiet`,`--end-of-options`,`${j(`a ref`,e)}^{commit}`],"rev-parse-git-dir":()=>[`rev-parse`,`--git-dir`],"rev-parse-top":()=>[`rev-parse`,`--show-toplevel`],"symbolic-ref-head":()=>[`symbolic-ref`,`--quiet`,`--short`,`--end-of-options`,`HEAD`],"rev-parse-shallow":()=>[`rev-parse`,`--is-shallow-repository`],"rev-parse-tree":()=>[`rev-parse`,`--verify`,`--quiet`,`HEAD^{tree}`],"symbolic-ref":()=>[`symbolic-ref`,`--quiet`,`--end-of-options`,`refs/remotes/origin/HEAD`],"for-each-ref":e=>[`for-each-ref`,`--format=${we(e)}`,`--`,`refs/heads/`],"rev-list-distance":(e,t)=>[`rev-list`,`--left-right`,`--count`,`--end-of-options`,`${M(e)}...${M(t)}`],"for-each-ref-tag":e=>[`for-each-ref`,`--format=%(creatordate:iso-strict)`,`--`,`refs/tags/${j(`a tag`,e)}`],"stash-list":()=>[`stash`,`list`,`--format=%H`],log:(e,t,n)=>[`log`,..._,`--format=${Ce}`,`--shortstat`,...n===null?[]:[`--since=${I(n)}`],`--end-of-options`,...P(e,t)],"log-numstat":e=>[`log`,..._,`--format=%x00%H%x09%aI`,`--numstat`,`--find-renames`,`--since=${I(e)}`,`--end-of-options`,`HEAD`],"log-patch":(e,t,n)=>[`log`,..._,`--diff-algorithm=histogram`,`--patch`,`--format=%x00%H`,`--find-renames`,`--unified=3`,`--max-count=${F(n)}`,`--end-of-options`,...P(e,t)],"rev-list-count":(e,t)=>[`rev-list`,`--count`,`--end-of-options`,...P(e,t)],"rev-list-trees":(e,t,n)=>[`rev-list`,`--format=%T`,`--max-count=${F(n)}`,`--end-of-options`,...P(e,t)],status:()=>[`status`,`--porcelain`,`-z`],"status-ignored":()=>[`status`,`--ignored`,`--porcelain`,`-z`],"ls-tree":()=>[`ls-tree`,`-r`,`-l`,`-z`,`--end-of-options`,`HEAD`],grep:()=>[`grep`,`--no-color`,`-I`,`-c`,`-z`,`-e`,``,`--end-of-options`,`HEAD`],"grep-plain":()=>[`grep`,`--no-color`,`-I`,`-c`,`-e`,``,`HEAD`],describe:()=>[`describe`,`--tags`,`--abbrev=0`],"merge-base":(e,t)=>[`merge-base`,`--end-of-options`,j(`a ref`,e),j(`a ref`,t)],"merge-base-ancestor":(e,t)=>[`merge-base`,`--is-ancestor`,`--end-of-options`,j(`a ref`,e),j(`a ref`,t)],"diff-tree-patch":(e,t)=>[`diff-tree`,..._,`--diff-algorithm=histogram`,`--patch`,`--find-renames`,`--unified=3`,`-r`,`--end-of-options`,M(e),M(t)]};Object.keys(L);function Te(e){return pe({tool:`home`,table:L,workspace:e,config:xe,answersWithStatus:[`grep`,`grep-plain`]})}var R=2e3;function z(e){let t=[];for(let n of e.split(`
|
|
3
|
+
`)){if(n===``)continue;let[e,r,i,a,o]=n.split(` `);if(e===void 0||r===void 0||i===void 0||a===void 0)continue;let[s,c]=(o??``).split(` `);t.push({name:e,tip:r,lastCommit:i,tree:a,ahead:Number(s??0)||0,behind:Number(c??0)||0})}return t}var Ee=[`index `,`old mode `,`new mode `,`new file mode `,`deleted file mode `,`similarity index `,`dissimilarity index `,`\\ No newline`];function B(e){let t=e.split(`
|
|
4
|
+
`).filter(e=>!Ee.some(t=>e.startsWith(t))).map(e=>e.startsWith(`@@`)?`@@`:e).join(`
|
|
5
|
+
`).trim();return te(`sha256`).update(t).digest(`hex`)}function De(){return{trees:new Map,patches:new Map}}function Oe(e){return e.split(`
|
|
6
|
+
`).filter(e=>e!==``&&!e.startsWith(`commit `))}function ke(e){let t=[];for(let n of e.split(`\0`)){if(n===``)continue;let e=n.indexOf(`
|
|
7
|
+
`);if(e===-1)continue;let r=n.slice(e+1).trim();r!==``&&t.push(r)}return t}async function Ae(e,t,n,r,i,a){let o=`${r.tip}:${i}`,s=t.verdicts[o]??n[o]??await je(e,r,i,a);return n[o]=s,s}async function je(e,t,n,r){let i=await e.text(`merge-base`,t.tip,n).catch(()=>null);if(i===null||i===``)return!1;if((await Me(e,i,n,r)).includes(t.tree))return!0;let a=await e.text(`diff-tree-patch`,i===t.tip?Se:i,t.tip).catch(()=>null);return a===null?!1:a.trim()===``||(await Ne(e,i,n,r)).includes(B(a))}async function Me(e,t,n,r){let i=r.trees.get(t);if(i!==void 0)return i;let a=Oe(await e.text(`rev-list-trees`,t,n,R).catch(()=>``));return r.trees.set(t,a),a}async function Ne(e,t,n,r){let i=r.patches.get(t);if(i!==void 0)return i;let a=Number(await e.text(`rev-list-count`,t,n).catch(()=>`0`));if(!Number.isFinite(a)||a>2e3)return r.patches.set(t,[]),[];let o=ke(await e.text(`log-patch`,t,n,R).catch(()=>``)).map(B);return r.patches.set(t,o),o}function V(e,t){let n=Date.parse(e);return!Number.isNaN(n)&&t-n>30*864e5}async function Pe(e,t){if(t===null)return z(await e.text(`for-each-ref`,null));let n=await e.text(`for-each-ref`,t).catch(()=>null);if(n!==null)return z(n);let r=z(await e.text(`for-each-ref`,null));for(let n of r){let r=await e.text(`rev-list-distance`,n.tip,t).catch(()=>null);if(r===null)continue;let[i,a]=r.trim().split(/\s+/);n.ahead=Number(i??0)||0,n.behind=Number(a??0)||0}return r}async function Fe(e,t,n){let r=n.now??Date.now(),i=n.defaultBranch===null?null:await O(e,n.defaultBranch),a=await Pe(e,i),o=t.read(`squash`,4)??{version:4,verdicts:{}},s={},c=De(),l=[];for(let t of a){let a=t.name===n.current;l.push({name:t.name,current:a,lastCommit:t.lastCommit,ahead:t.ahead,state:await Ie(e,o,s,t,{current:a,isDefault:t.name===n.defaultBranch,defaultTip:i,bases:c,now:r})})}return t.write(`squash`,{version:4,verdicts:s}),l.sort((e,t)=>e.current===t.current?Date.parse(t.lastCommit)-Date.parse(e.lastCommit):e.current?-1:1),{default:n.defaultBranch,branches:l}}async function Ie(e,t,n,r,i){return i.current?`current`:i.isDefault?`default`:i.defaultTip===null?V(r.lastCommit,i.now)?`stale`:`ahead`:r.ahead===0?`merged`:await Ae(e,t,n,r,i.defaultTip,i.bases)?`squash`:V(r.lastCommit,i.now)?`stale`:`ahead`}var Le=`.tmp-`;function Re(e,n){t(c(e),{recursive:!0});let r=`${e}${Le}${process.pid}-${Math.random().toString(36).slice(2)}`;try{o(r,n),i(r,e)}finally{a(r,{force:!0})}}function ze(e,t,n,r){Re(T(e,t,n),r)}function Be(e,t,r){let i=T(e,t,r);try{return n(i,`utf8`)}catch{return null}}var H=`home`,U=`.home`;function W(e){let t=C(e.cacheDir);if(E(C(e.workspace),t))throw new y(H,`the cache directory ${t} is inside the workspace`);let n=!1;return{directory:t,read(e,n){try{let r=Be(H,t,`${U}/${e}.json`);if(r===null)return null;let i=JSON.parse(r);return i?.version===n?i:null}catch{return null}},write(e,r){try{return ze(H,t,`${U}/${e}.json`,JSON.stringify(r)),!0}catch(e){if(e?.code===`path-refused`)throw e;return n||(n=!0,console.error(`home: the cache directory ${t} cannot be written - ${String(e)}`)),!1}}}}var Ve=new Set([`packages`,`apps`,`libs`,`services`,`crates`,`modules`]),He=`(root)`,Ue=`other`;function We(e){let t=/^(.*)\{(.*) => (.*)\}(.*)$/.exec(e);if(t!==null){let[,e=``,,n=``,r=``]=t;return`${e}${n}${r}`.replace(/\/\//g,`/`)}let n=/ => /.exec(e);return n===null?e:e.slice(n.index+4)}function Ge(e){let t=e.split(`/`).filter(e=>e!==``),[n,r]=t;return n===void 0||t.length===1?He:Ve.has(n)&&r!==void 0&&t.length>2?`${n}/${r}`:n}function Ke(e){let t=[];for(let n of e.split(`\0`)){if(n===``)continue;let e=n.indexOf(`
|
|
8
|
+
`);if(e===-1)continue;let[r,i]=n.slice(0,e).split(` `);if(!(r===void 0||i===void 0||i===``))for(let a of n.slice(e+1).split(`
|
|
9
|
+
`)){let[e,n,o]=a.split(` `);e===void 0||n===void 0||o===void 0||o===``||t.push({hash:r,date:i,path:We(o),added:e===`-`?0:Number(e)||0,deleted:n===`-`?0:Number(n)||0})}}return t}function qe(){return{commits:new Set,lines:0}}function Je(e,t){let n=Math.floor(t/1e3)-30*86400,r=n-30*86400,i=new Map,a=new Map;for(let t of e){let e=K(t.date),o=e>=n?i:e>=r?a:null;if(o===null)continue;let s=Ge(t.path),c=o.get(s)??qe();c.commits.add(t.hash),c.lines+=t.added+t.deleted,o.set(s,c)}let o=[...new Set([...i.keys(),...a.keys()])].map(e=>({area:e,commits:i.get(e)?.commits.size??0,lines:i.get(e)?.lines??0,before:{commits:a.get(e)?.commits.size??0,lines:a.get(e)?.lines??0}})).sort((e,t)=>t.lines-e.lines||t.before.lines-e.before.lines||e.area.localeCompare(t.area));if(o.length<=8)return o;let s=o.slice(0,8),c=o.slice(8);return s.push({area:Ue,commits:c.reduce((e,t)=>e+t.commits,0),lines:c.reduce((e,t)=>e+t.lines,0),before:{commits:c.reduce((e,t)=>e+t.before.commits,0),lines:c.reduce((e,t)=>e+t.before.lines,0)}}),s}async function Ye(e,t=Date.now()){let n=new Date(t-74*864e5).toISOString().slice(0,10);return Je(Ke(await e.text(`log-numstat`,n).catch(()=>``)),t)}var Xe=2;function G(e){return e.slice(0,10)}function Ze(e){return Number(e.slice(11,13))}function Qe(e){let t=Date.UTC(Number(e.slice(0,4)),Number(e.slice(5,7))-1,Number(e.slice(8,10)),12);return(new Date(t).getUTCDay()+6)%7}function K(e){let t=Date.parse(e);return Number.isNaN(t)?0:Math.round(t/1e3)}function q(e){let t=[];for(let n of e.split(`\0`)){if(n===``)continue;let e=n.indexOf(`
|
|
10
|
+
`),[r,i]=(e===-1?n:n.slice(0,e)).split(` `);if(r===void 0||i===void 0)continue;let a=/(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?/.exec(e===-1?``:n.slice(e));t.push({hash:r,date:i,added:Number(a?.[2]??0),deleted:Number(a?.[3]??0)})}return t}function J(e){return{version:4,tip:e,commits:0,firstCommit:null,lastCommit:null,days:{},hours:Array.from({length:24},()=>0),weekdays:Array.from({length:7},()=>0),cut:!1}}function Y(e,t){let n={...e.days},r=[...e.hours],i=[...e.weekdays];for(let e of t){let t=G(e.date),[a=0,o=0,s=0]=n[t]??[0,0,0];n[t]=[a+1,o+e.added,s+e.deleted];let c=Ze(e.date);c>=0&&c<24&&(r[c]=(r[c]??0)+1);let l=Qe(e.date);l>=0&&l<7&&(i[l]=(i[l]??0)+1)}return{...e,commits:e.commits+t.length,firstCommit:$e(e.firstCommit,t),lastCommit:et(e.lastCommit,t),days:n,hours:r,weekdays:i}}function $e(e,t){let n=e;for(let e of t)G(e.date)<`1990-01-01`||(n===null||K(e.date)<K(n))&&(n=e.date);if(n!==null)return n;for(let e of t)(n===null||K(e.date)<K(n))&&(n=e.date);return n}function et(e,t){let n=e;for(let e of t)(n===null||K(e.date)>K(n))&&(n=e.date);return n}async function tt(e,t,n){let r=t.read(`history`,4);if(r?.tip===n)return r;let i=r&&!r.cut&&await e.ok(`merge-base-ancestor`,r.tip,n)?{...Y(r,q(await e.text(`log`,r.tip,n,null))),tip:n}:{...await nt(e,n),tip:n};return t.write(`history`,i),i}async function nt(e,t){try{return Y(J(t),q(await e.text(`log`,null,t,null)))}catch(n){let r=n;if(r?.code!==`upstream-failed`||r.overLimit!==!0)throw n;let i=new Date(Date.now()-Xe*365*864e5).toISOString().slice(0,10),a=await e.text(`log`,null,t,i);return{...Y(J(t),q(a)),cut:!0}}}function rt(e,t){let n=X(t),r=Object.keys(e).filter(e=>e>=`1990-01-01`&&e<=n).sort()[0]??t,i=new Map;for(let[t,a]of Object.entries(e)){let e=t>n?n:t<`1990-01-01`?r:t,[o=0,s=0,c=0]=i.get(e)??[0,0,0];i.set(e,[o+a[0],s+a[1],c+a[2]])}return[...i.entries()].sort(([e],[t])=>e.localeCompare(t)).map(([e,[t,n,r]])=>({date:e,commits:t,added:n,deleted:r}))}function it(e){if(e.length===0)return null;let t=[...e].sort(),n={days:1,from:t[0]??``,to:t[0]??``},r=t[0]??``,i=1;for(let e=1;e<t.length;e++){let a=t[e]??``;X(t[e-1]??``)===a?i+=1:(r=a,i=1),i>n.days&&(n={days:i,from:r,to:a})}return n}function X(e){let t=Date.parse(`${e}T00:00:00Z`);return new Date(t+864e5).toISOString().slice(0,10)}function at(e,t){let n=Date.parse(`${t}T00:00:00Z`)-Date.parse(`${e}T00:00:00Z`);return Math.max(1,Math.round(n/864e5)+1)}async function ot(e){let t=await e.text(`describe`).catch(()=>null);if(t===null||t===``)return null;let n=await e.text(`for-each-ref-tag`,t).catch(()=>null);return{name:t,date:n===null||n===``?``:n}}async function Z(e){let t=await e.text(`stash-list`).catch(()=>``);return t===``?0:t.split(`
|
|
11
|
+
`).filter(e=>e!==``).length}async function st(e,t,n,r=Date.now()){let i=await e.text(`rev-parse`,`HEAD`).catch(()=>null),a=await ye(e).catch(()=>0),o=await A(e);if(i===null){let t=J(``);return{repository:{...n,changedFiles:a,firstCommit:null,lastCommit:null,commits:0,shallow:o,cut:!1},days:[],hours:t.hours,weekdays:t.weekdays,areas:[],milestones:{firstCommit:null,commitsPerDay:0,longestStreak:null,biggestDay:null,lastTag:null,stashes:await Z(e)}}}let s=await tt(e,t,i),c=new Date(r).toISOString().slice(0,10),l=rt(s.days,c),u=l.reduce((e,t)=>e===null||t.commits>e.commits?{date:t.date,commits:t.commits}:e,null),d=l[0]?.date??null;return{repository:{...n,changedFiles:a,firstCommit:s.firstCommit,lastCommit:s.lastCommit,commits:s.commits,shallow:o,cut:s.cut},days:l,hours:s.hours,weekdays:s.weekdays,areas:await Ye(e,r),milestones:{firstCommit:s.firstCommit,commitsPerDay:d===null?0:Math.round(s.commits/at(d,c)*100)/100,longestStreak:it(l.map(e=>e.date)),biggestDay:u,lastTag:await ot(e),stashes:await Z(e)}}}var Q=`other`,ct={dockerfile:`Dockerfile`,makefile:`Makefile`,license:`License`,"license.md":`License`,licence:`License`,readme:`Markdown`,".gitignore":`Config`,".gitattributes":`Config`,".dockerignore":`Config`,".editorconfig":`Config`,".npmrc":`Config`,".nvmrc":`Config`,".env":`Config`},lt={ts:`TypeScript`,tsx:`TypeScript`,mts:`TypeScript`,cts:`TypeScript`,js:`JavaScript`,jsx:`JavaScript`,mjs:`JavaScript`,cjs:`JavaScript`,py:`Python`,rb:`Ruby`,go:`Go`,rs:`Rust`,java:`Java`,kt:`Kotlin`,swift:`Swift`,c:`C`,h:`C`,cc:`C++`,cpp:`C++`,cxx:`C++`,hpp:`C++`,cs:`C#`,php:`PHP`,lua:`Lua`,sh:`Shell`,bash:`Shell`,zsh:`Shell`,fish:`Shell`,ps1:`Shell`,sql:`SQL`,html:`HTML`,htm:`HTML`,css:`CSS`,scss:`CSS`,sass:`CSS`,less:`CSS`,md:`Markdown`,mdx:`Markdown`,rst:`Markdown`,txt:`Text`,json:`JSON`,jsonc:`JSON`,yaml:`YAML`,yml:`YAML`,toml:`Config`,ini:`Config`,cfg:`Config`,conf:`Config`,xml:`XML`,svg:`Image`,png:`Image`,jpg:`Image`,jpeg:`Image`,gif:`Image`,webp:`Image`,avif:`Image`,ico:`Image`,mp3:`Audio`,wav:`Audio`,ogg:`Audio`,mp4:`Video`,webm:`Video`,mov:`Video`,woff:`Font`,woff2:`Font`,ttf:`Font`,otf:`Font`,pdf:`Document`,zip:`Archive`,gz:`Archive`,tar:`Archive`,lock:`Lockfile`};function ut(e){let t=e.slice(e.lastIndexOf(`/`)+1),n=ct[t.toLowerCase()];if(n!==void 0)return n;let r=t.lastIndexOf(`.`);return r<=0?Q:lt[t.slice(r+1).toLowerCase()]??`other`}function dt(e){let t=[];for(let n of e.split(`\0`)){if(n===``)continue;let e=n.indexOf(` `);if(e===-1)continue;let r=n.slice(0,e).split(/\s+/);if(r[1]!==`blob`)continue;let i=Number(r[3]);t.push({path:n.slice(e+1),bytes:Number.isFinite(i)?i:0})}return t}function ft(e){let t=0;for(let n of e.split(`
|
|
12
|
+
`)){if(n===``)continue;let e=Math.max(n.lastIndexOf(`\0`),n.lastIndexOf(`:`));e!==-1&&(t+=Number(n.slice(e+1))||0)}return t}function pt(e){let t=e.reduce((e,t)=>e+t,0);if(t<=0)return e.map(()=>0);let n=e.map(e=>e/t*100),r=n.map(e=>Math.floor(e)),i=100-r.reduce((e,t)=>e+t,0),a=n.map((e,t)=>({at:t,remainder:e-Math.floor(e)})).sort((e,t)=>t.remainder-e.remainder);for(let{at:e}of a){if(i<=0)break;r[e]=(r[e]??0)+1,--i}return r}function mt(e){let t=Object.entries(e).filter(([,e])=>e>0).sort((e,t)=>t[1]-e[1]||e[0].localeCompare(t[0])),n=t.filter(([e])=>e!==Q).slice(0,6),r=t.filter(([e])=>!n.some(([t])=>t===e)).reduce((e,[,t])=>e+t,0),i=r>0?[...n,[Q,r]]:n,a=pt(i.map(([,e])=>e));return i.map(([e,t],n)=>({kind:e,bytes:t,share:a[n]??0}))}async function ht(e){let t=await e.text(`grep`).catch(()=>null);return t===null?e.text(`grep-plain`).catch(()=>null):t}async function gt(e,t){let n=await e.text(`rev-parse-tree`).catch(()=>null);if(n===null||n===``)return null;let r=t.read(`tree`,4);if(r?.tree===n)return r;let i=dt(await e.text(`ls-tree`)),a={},o=0;for(let e of i){let t=ut(e.path);a[t]=(a[t]??0)+e.bytes,o+=e.bytes}let s=await ht(e),c={version:4,tree:n,bytesByKind:a,files:i.length,lines:s===null?0:ft(s),bytes:o};return s!==null&&t.write(`tree`,c),c}async function _t(e,t,n){let r=0,i=[e];for(;i.length>0;){if(n()||Date.now()>t)return{bytes:r,whole:!1};let e=i.pop();if(e===void 0)continue;let a;try{a=await ne(e)}catch{continue}if(a.isSymbolicLink()){r+=a.size;continue}if(a.isFile()){r+=a.size;continue}if(a.isDirectory())try{for(let t of await re(e))i.push(u(e,t))}catch{}}return{bytes:r,whole:!0}}function vt(e){let t=e.limitMs??1e4,n=e.cache.read(`ignored`,4),r=null,i=!1;async function a(){let r=Date.now()+t,a=await e.git.text(`status-ignored`).catch(()=>``);if(i)return;let o=k(a).filter(e=>e.status===`!!`).map(e=>e.path),s=[],c=0,l=!0;for(let t of o){if(i)return;if(Date.now()>r){l=!1;break}let n=await _t(f(e.workspace,t),r,()=>i);l&&=n.whole,c+=n.bytes,s.push({path:t,bytes:n.bytes})}if(i)return;let u={version:4,bytes:c,atLeast:!l,countedAt:new Date().toISOString(),largest:s.sort((e,t)=>t.bytes-e.bytes).slice(0,2)};n=u,e.cache.write(`ignored`,u)}return{last:()=>n===null?null:{...n},start(){return i?Promise.resolve():(r??=a().finally(()=>{r=null}),r)},close(){i=!0}}}var $=`home`;function yt(e){let t=f(e.root),n=W({cacheDir:e.cacheDir,workspace:t}),r=Te(t),i=vt({git:r,cache:n,workspace:t,...e.walkLimitMs===void 0?{}:{limitMs:e.walkLimitMs}}),a=e.name??``,o=()=>new Date().toISOString();return{root:t,async history(){let e=await D(r,t,a);if(e===null)return{readAt:o(),repository:null};let i=await st(r,n,e);return{readAt:o(),...i}},async branches(){let e=await D(r,t,a);if(e===null)return{readAt:o(),repository:null};let i=e.detached?null:e.branch,s=await Fe(r,n,{defaultBranch:await ve(r,i),current:i});return{readAt:o(),repository:e,...s}},async tree(){let e=await D(r,t,a);if(e===null)return{readAt:o(),repository:null};let s=i.last();i.start().catch(()=>{});let c=await gt(r,n);return{readAt:o(),repository:e,kinds:c===null?[]:mt(c.bytesByKind),tracked:{files:c?.files??0,lines:c?.lines??0,bytes:c?.bytes??0},ignored:s}},toolPath(e){throw new oe($,e,`no entry`)},close(){i.close()}}}function bt(e){throw new y($,`home serves no workspace file: ${e}`)}function xt(t){return e(f(t))}export{yt as createHomeModule,xt as hasRoot,bt as refuseRaw,h as statusFor};
|
|
@@ -0,0 +1,21 @@
|
|
|
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(`
|
|
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(`
|
|
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};
|