@north-light/cli 0.3.686 → 0.3.688
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/dist/clients/attach/viewer.js +527 -527
- package/dist/core/conversation-store/row-store.js +11 -11
- package/dist/core/permissions/ceiling.js +3 -3
- package/package.json +3 -3
- package/packages/crouter-api/dist/core/conversation-store/row-store.js +13 -6
- package/packages/crouter-api/dist/core/permissions/ceiling.js +2 -1
- package/packages/crouter-api/package.json +1 -1
- package/packages/crouter-identity/package.json +1 -1
- package/packages/crouter-plugin/package.json +3 -3
- package/runtime.lock.json +14 -14
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
var
|
|
2
|
-
`).join(""),"encode")
|
|
3
|
-
`).filter(Boolean).map(t=>JSON.parse(t)),"decode"),h=_(()=>new Date().toISOString(),"at");async function g(o,t,r=!1){const
|
|
4
|
-
UNION ALL SELECT 'tail', seq, NULL, body, bytes, NULL, created FROM conversation_entries WHERE conversation_id = ? ORDER BY 1, 2`).all(t,t);return{parts:r.filter(
|
|
5
|
-
`).subarray(
|
|
6
|
-
`).filter(Boolean).length-1),h())||!e?.run_id||e.event_offset===null)return;const f=e.source_session_id?[]:await T(p,e.run_id,e.node_id,
|
|
7
|
-
`);
|
|
8
|
-
`))u&&(yield Buffer.from(u,"latin1").toString("utf8"))}
|
|
9
|
-
`);d.pop();const p=Math.max(0,
|
|
1
|
+
var U=Object.defineProperty;var _=(o,t)=>U(o,"name",{value:t,configurable:!0});import{randomUUID as O}from"node:crypto";import{existsSync as W,readFileSync as q,rmSync as B}from"node:fs";import{join as j}from"node:path";import{SessionManager as J}from"@earendil-works/pi-coding-agent";import{blobStore as E}from"../blobs/index.js";import{openDb as w,withCanvasWrite as y}from"../canvas/db.js";import{appSpace as x}from"../layout.js";import{appendConversationEvents as T,rootRepromptPending as L}from"../../daemon/runs/append-section.js";import{ConversationNotFoundError as R}from"./index.js";const N="s.unbound_at IS NULL DESC, s.created_at DESC, s.seq DESC",D=_((o,t,r)=>`conversations/${o}/${t}/${r}.jsonl`,"keyFor"),C=_(o=>o.map(t=>JSON.stringify(t)+`
|
|
2
|
+
`).join(""),"encode"),H=_(o=>o.split(`
|
|
3
|
+
`).filter(Boolean).map(t=>JSON.parse(t)),"decode"),h=_(()=>new Date().toISOString(),"at");async function g(o,t,r=!1){const a=await o.prepare(`SELECT s.binding_id, s.node_id, s.transcript_path, s.input_ids_json, s.source_session_id, n.run_id FROM sessions s JOIN nodes n ON n.node_id = s.node_id WHERE s.session_id = ? ORDER BY ${N} LIMIT 1${r?" FOR UPDATE OF s":""}`).get(t);if(!a)throw new R(t);return a}_(g,"binding");async function b(o,t){return await o.prepare("SELECT seq, blob_key, bytes, entries, created FROM conversation_parts WHERE conversation_id = ? ORDER BY seq").all(t)}_(b,"parts");async function Y(o,t){return await o.prepare("SELECT seq, body, bytes, created FROM conversation_entries WHERE conversation_id = ? ORDER BY seq").all(t)}_(Y,"tail");async function M(o,t){const r=await o.prepare(`SELECT 'part' AS kind, seq, blob_key, NULL AS body, bytes, entries, created FROM conversation_parts WHERE conversation_id = ?
|
|
4
|
+
UNION ALL SELECT 'tail', seq, NULL, body, bytes, NULL, created FROM conversation_entries WHERE conversation_id = ? ORDER BY 1, 2`).all(t,t);return{parts:r.filter(a=>a.kind==="part").map(({seq:a,blob_key:n,bytes:s,entries:e,created:i})=>({seq:Number(a),blob_key:n,bytes:Number(s),entries:Number(e),created:i})),tail:r.filter(a=>a.kind==="tail").map(({seq:a,body:n,bytes:s,created:e})=>({seq:Number(a),body:n,bytes:Number(s),created:e}))}}_(M,"snapshot");const I=4*1024*1024,V=4,P=16;async function*$(o,t,r){const a=[];let n=0;const s=_(()=>{for(;n<o&&a.length<r;){const e=t(n++);e.catch(()=>{}),a.push(e)}},"fill");for(s();a.length>0;){const e=await a.shift();s(),yield e}}_($,"readAhead");async function S(o,t,r){const a=[];let n=0;const s=await E(),e=[];for(const i of o.parts){const c=Math.max(t,n),d=Math.min(r,n+i.bytes);c<d&&e.push({key:i.blob_key,range:{start:c-n,length:d-c}}),n+=i.bytes}for await(const i of $(e.length,async c=>{const d=await s.get(e[c].key,e[c].range);if(!d)throw new Error(`missing conversation object: ${e[c].key}`);return d},P))a.push(i);for(const i of o.tail){const c=Math.max(t,n),d=Math.min(r,n+i.bytes);c<d&&a.push(Buffer.from(i.body+`
|
|
5
|
+
`).subarray(c-n,d-n)),n+=i.bytes}return Buffer.concat(a)}_(S,"bytesOf");const k=new Map;async function A(o,t,r){const a=await w();if(await a.prepare("SELECT 1 FROM conversation_parts WHERE conversation_id = ? AND seq = 0").get(o)||!W(r))return;let n=q(r);const s=n.lastIndexOf(10);if(s<0)return;n=n.subarray(0,s+1);const e=await a.prepare("SELECT s.binding_id, s.node_id, s.event_offset, s.input_ids_json, s.source_session_id, n.run_id FROM sessions s JOIN nodes n ON n.node_id = s.node_id WHERE s.session_id = ? ORDER BY "+N+" LIMIT 1").get(o);let i=[];if(e?.run_id&&e.event_offset!==null){const p=Number(e.event_offset);if(n.length<p)throw new Error(`conversation shorter than indexed offset: ${o}`);i=H(n.subarray(p).toString("utf8")).filter(u=>u.type!=="session")}const c=e?.run_id?await L(e.run_id,e.node_id,i):!1,d=D(t,o,0);await(await E()).put(d,n),await y(async p=>{if(!await p.prepare("INSERT INTO conversation_parts (conversation_id, seq, blob_key, bytes, entries, created) VALUES (?, 0, ?, ?, ?, ?) ON CONFLICT DO NOTHING RETURNING seq").get(o,d,n.length,Math.max(0,n.toString("utf8").split(`
|
|
6
|
+
`).filter(Boolean).length-1),h())||!e?.run_id||e.event_offset===null)return;const f=e.source_session_id?[]:await T(p,e.run_id,e.node_id,i,JSON.parse(e.input_ids_json),c);await p.prepare("UPDATE sessions SET event_offset = ?, indexed_entry_id = ?, input_ids_json = ? WHERE binding_id = ?").run(n.length,i.at(-1)?.id??null,JSON.stringify(f),e.binding_id)})}_(A,"importOne");async function m(o,t){const r=await t.prepare(`SELECT s.binding_id, s.node_id, s.transcript_path, s.input_ids_json, s.source_session_id, n.run_id FROM sessions s JOIN nodes n ON n.node_id = s.node_id WHERE s.session_id = ? ORDER BY ${N} LIMIT 1`).get(o),a=k.get(o);if(!r&&!a)throw new R(o);let n=(await b(t,o))[0];if(!n){const c=a?.path??r?.transcript_path??void 0;c&&await A(o,a?.node??r.node_id,c),n=(await b(t,o))[0]}if(!n||n.seq!==0)throw new R(o);const s=await(await E()).get(n.blob_key,{start:0,length:Math.min(n.bytes,256*1024)});if(!s)throw new Error(`missing conversation object: ${n.blob_key}`);const e=s.indexOf(10);if(e<0)throw new Error(`invalid conversation header: ${o}`);const i=JSON.parse(s.subarray(0,e).toString("utf8"));if(i.type!=="session"||i.id!==o)throw new Error(`invalid conversation header: ${o}`);return{header:i,path:r?.transcript_path??a?.path??"",binding:r}}_(m,"initial");function at(){const o={async create(t,r,a,n){const e=await(await w()).prepare("SELECT grantee, run_id FROM node_rows WHERE node_id = ?").get(t);if(!e)throw new Error(`missing conversation owner ${t}`);const i=r.id,c=j(x(e.grantee).conversations,t,`${r.timestamp.replace(/[:.]/g,"-")}_${i}.jsonl`),d=C([r,...a]),p=D(t,i,0),u=e.run_id?await L(e.run_id,t,a):!1;return await(await E()).put(p,d),await y(async f=>{if(await f.prepare("SELECT 1 FROM conversation_parts WHERE conversation_id = ?").get(i))throw new Error(`conversation already exists: ${i}`);const l=h(),v=r.parentSession?await f.prepare("SELECT session_id FROM sessions WHERE transcript_path = ? LIMIT 1").get(r.parentSession):void 0;if(await f.prepare("INSERT INTO sessions (binding_id, session_id, node_id, transcript_path, source_session_id, fork_entry_id, created_at, bound_at, unbound_at) VALUES (?, ?, ?, ?, ?, NULL, ?, ?, ?)").run(O(),i,t,c,v?v.session_id:null,l,l,l),await f.prepare("INSERT INTO conversation_parts (conversation_id, seq, blob_key, bytes, entries, created) VALUES (?, 0, ?, ?, ?, ?)").run(i,p,Buffer.byteLength(d),a.length,l),e.run_id&&!v){const F=await T(f,e.run_id,t,a,[],u);await f.prepare("UPDATE sessions SET indexed_entry_id = ?, input_ids_json = ? WHERE session_id = ? AND node_id = ?").run(a.at(-1)?.id??null,JSON.stringify(F),i,t)}}),{conversation_id:i,path:c}},async append(t,r){if(!r.length)return;const a=await g(await w(),t),n=a.run_id?await L(a.run_id,a.node_id,r):!1;await y(async s=>{const e=await g(s,t,!0);if(!(await b(s,t)).length)throw new R(t);const i=[];for(const c of r){const d=JSON.stringify(c);await s.prepare("INSERT INTO conversation_entries (conversation_id, entry_id, body, bytes, execution_id, created) VALUES (?, ?, ?, ?, NULL, ?) ON CONFLICT (conversation_id, entry_id) DO NOTHING RETURNING entry_id").get(t,c.id,d,Buffer.byteLength(d)+1,h())&&i.push(c)}if(e.run_id&&i.length){const c=await T(s,e.run_id,e.node_id,i,JSON.parse(e.input_ids_json??"[]"),n);await s.prepare("UPDATE sessions SET indexed_entry_id = ?, input_ids_json = ? WHERE binding_id = ?").run(i.at(-1).id,JSON.stringify(c),e.binding_id)}})},async readLines(t){const r=await w(),{path:a}=await m(t,r),n=await M(r,t),s=n.parts.reduce((i,c)=>i+c.bytes,0)+n.tail.reduce((i,c)=>i+c.bytes,0);async function*e(){let i="";for await(const c of $(Math.ceil(s/I),d=>S(n,d*I,Math.min(s,(d+1)*I)),V)){const d=i+c.toString("latin1"),p=d.lastIndexOf(`
|
|
7
|
+
`);i=d.slice(p+1);for(const u of d.slice(0,p+1).split(`
|
|
8
|
+
`))u&&(yield Buffer.from(u,"latin1").toString("utf8"))}i&&(yield Buffer.from(i,"latin1").toString("utf8"))}return _(e,"lines"),{path:a,lines:e()}},async read(t,r){const a=await w(),{header:n,path:s}=await m(t,a),e=await M(a,t),i=e.parts.reduce((f,l)=>f+l.bytes,0)+e.tail.reduce((f,l)=>f+l.bytes,0),c=_(f=>H(f).filter(l=>l.type!=="session"),"entriesOf");if(!r||i<=r.head+r.tail)return{header:n,entries:c((await S(e,0,i)).toString("utf8")),path:s};const d=(await S(e,0,Math.max(0,r.head))).toString("utf8").split(`
|
|
9
|
+
`);d.pop();const p=Math.max(0,i-r.tail),u=(await S(e,p>0?p-1:0,i)).toString("utf8").split(`
|
|
10
10
|
`);return p>0&&u.shift(),{header:n,entries:c(d.join(`
|
|
11
11
|
`)),tail:c(u.join(`
|
|
12
|
-
`)),path:s}},async header(t){const r=await w(),{header:
|
|
13
|
-
`).join("");return await(await
|
|
12
|
+
`)),path:s}},async header(t){const r=await w(),{header:a,path:n}=await m(t,r),s=await M(r,t);return{header:a,path:n,mtime:Date.parse(s.tail.at(-1)?.created??s.parts.at(-1).created),size:s.parts.reduce((e,i)=>e+i.bytes,0)+s.tail.reduce((e,i)=>e+i.bytes,0)}},async list(t){const a=await(await w()).prepare(`SELECT DISTINCT ON (s.session_id) s.session_id, s.node_id FROM sessions s JOIN conversation_parts p ON p.conversation_id = s.session_id WHERE s.session_id IS NOT NULL ORDER BY s.session_id, ${N}`).all(),n=[];for(const s of a){if(t&&s.node_id!==t)continue;const e=await o.header(s.session_id);n.push({conversation_id:s.session_id,node_id:s.node_id,...e})}return n.sort((s,e)=>e.mtime-s.mtime)},async replace(t,r,a){if(r.id!==t)throw new Error("replacement header id differs from conversation id");const n=await w(),s=await g(n,t);if(s.run_id)throw new Error(`run conversation ${t} is append-only; replace refused`);const e=`conversations/${s.node_id}/${t}/0-${O()}.jsonl`,i=C([r,...a]);await(await E()).put(e,i);const c=await y(async d=>{if((await g(d,t,!0)).run_id)throw new Error(`run conversation ${t} is append-only; replace refused`);const u=await b(d,t);return await d.prepare("DELETE FROM conversation_parts WHERE conversation_id = ?").run(t),await d.prepare("DELETE FROM conversation_entries WHERE conversation_id = ?").run(t),await d.prepare("INSERT INTO conversation_parts (conversation_id, seq, blob_key, bytes, entries, created) VALUES (?, 0, ?, ?, ?, ?)").run(t,e,Buffer.byteLength(i),a.length,h()),u});for(const d of c)await(await E()).delete(d.blob_key)},async fork(t,r,a,n){const s=await o.read(t);let e=s.entries;if(a!==void 0){const i=J.inMemory(s.header.cwd,void 0,[s.header,...e]);i.createBranchedSession(a),e=i.getEntries()}return(await o.create(r??(await m(t,await w())).binding?.node_id??k.get(t).node,{...s.header,id:O(),timestamp:h(),cwd:n??s.header.cwd,parentSession:s.path},e)).conversation_id},async delete(t,r){const a=await w(),n=await b(a,t);await y(async s=>{await s.prepare("DELETE FROM conversation_entries WHERE conversation_id = ?").run(t),await s.prepare("DELETE FROM conversation_parts WHERE conversation_id = ?").run(t),await s.prepare("DELETE FROM sessions WHERE session_id = ?").run(t)});for(const s of n)await(await E()).delete(s.blob_key);r&&B(r,{force:!0})},async commit(t){return await y(async r=>{const a=await g(r,t,!0),n=await Y(r,t),s=await b(r,t);if(!n.length)return s.at(-1)?.seq??0;const e=(s.at(-1)?.seq??0)+1,i=D(a.node_id,t,e),c=n.map(d=>d.body+`
|
|
13
|
+
`).join("");return await(await E()).put(i,c),process.env.CRTR_FAULT_CONVERSATION==="commit_exit_after_put"&&process.exit(1),await r.prepare("INSERT INTO conversation_parts (conversation_id, seq, blob_key, bytes, entries, created) VALUES (?, ?, ?, ?, ?, ?)").run(t,e,i,Buffer.byteLength(c),n.length,h()),await r.prepare("DELETE FROM conversation_entries WHERE conversation_id = ? AND seq <= ?").run(t,n.at(-1).seq),e})},async importLegacy(t){const a=await(await w()).prepare(`SELECT DISTINCT ON (s.session_id) s.session_id, s.node_id, s.transcript_path FROM sessions s JOIN nodes n ON n.node_id = s.node_id WHERE s.session_id IS NOT NULL AND s.transcript_path IS NOT NULL ${t?"AND n.run_id = ?":""} ORDER BY s.session_id, ${N}`).all(...t?[t]:[]);for(const n of a)await A(n.session_id,n.node_id,n.transcript_path)},recover(t){for(const r of t)r.transcript_path&&k.set(r.session_id,{node:r.node_id,path:r.transcript_path})}};return o}_(at,"createRowStore");function G(o,t,r){const a=o.indexOf(10);if(a<0)throw new Error(`invalid conversation header: ${t}`);const n=JSON.parse(o.subarray(0,a).toString("utf8"));if(n.type!=="session"||n.id!==t)throw new Error(`invalid conversation header: ${t}`);const s=r(n.cwd);return s===n.cwd?null:Buffer.concat([Buffer.from(JSON.stringify({...n,cwd:s})),o.subarray(a)])}_(G,"withHeaderCwd");async function it(o,t,r){const a=await w(),n=await g(a,o),s=(await b(a,o))[0];if(!s||s.seq!==0)return!1;const e=await(await E()).get(s.blob_key);if(!e)throw new Error(`missing conversation object: ${s.blob_key}`);const i=G(e,o,p=>p===t?r:p);if(i===null)return!1;const c=`conversations/${n.node_id}/${o}/0-${O()}.jsonl`;await(await E()).put(c,i);const d=await y(async p=>await p.prepare("UPDATE conversation_parts SET blob_key = ?, bytes = ? WHERE conversation_id = ? AND seq = 0 AND blob_key = ? RETURNING seq").get(c,i.length,o,s.blob_key)!==void 0);if(await(await E()).delete(d?s.blob_key:c).catch(()=>{}),!d)throw new Error(`conversation header changed during relocation: ${o}`);return!0}_(it,"relocateConversationHeader");export{at as createRowStore,$ as readAhead,it as relocateConversationHeader,G as withHeaderCwd};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
var v=Object.defineProperty;var s=(t,e)=>v(t,"name",{value:e,configurable:!0});import{covers as A}from"@north-light/identity";import{isCloudRole as j}from"../canvas/person-state.js";import{eligiblePackageApps as x}from"../packages/eligibility.js";import{subjectSet as F}from"./evaluator.js";import{FALSE as M,TRUE as C,and as U,f as h,or as D,raw as d}from"./sql.js";const k=s(t=>({ownApp:t,roots:[],namespaces:[],place:null,predicates:{allNodes:!1,spaces:!1},authorApps:[],packageApps:null}),"empty");async function $(t,e){return(await t.prepare("SELECT external_id FROM identities WHERE object = ? ORDER BY scheme LIMIT 1").get(e))?.external_id??null}s($,"externalId");async function
|
|
2
|
-
WHERE g.resolved_scopes_json::jsonb @> '[{"scope":"crtr:memory:manage:user","mode":"always"}]'::jsonb`).all()).map(n=>n.id)}s(P,"firstPartyApps");function y(t,e){return X(t).has(e.replace(/^app:/,""))}s(y,"holdsProviderGroup");const g=new WeakMap;function X(t){let e=g.get(t);if(e===void 0){e=new Set;for(const n of t){const o=n.indexOf(":");o>=0&&!n.includes(":",o+1)&&e.add(n.slice(0,o))}g.set(t,e)}return e}s(X,"providerGroupHolders");function z(t){return t.class==="object"?t.repos.map(e=>e.id):t.class!=="node"?[]:[...new Set([...t.repos,...t.projectRepos].map(e=>e.id))]}s(z,"repoRoots");async function Y(t,e){return e===null?[]:(await F(t,e)).groupIds.filter(n=>n.startsWith("group:")).map(n=>n.slice(6))}s(Y,"memberGroups");async function Z(t,e,n){if(e.class==="person")return null;const o=e.scopes;return await x(t,{holdsProvider:s(m=>o!==null&&y(o,m),"holdsProvider"),cloud:j(),ownExt:await $(t,e.app.id),user:n})}s(Z,"readablePackageApps");async function oe(t,e,n,o,m={}){if(e.class==="person")return null;const a=k(e.app.id),r=e.scopes,E=s(i=>r!==null&&r.some(p=>A(p,i)),"has"),l=await $(t,e.app.id),w=e.class==="node"?await R(t,e.node.id):e.place??null;a.place=w,a.authorApps=[e.app.id];const c=o==="write"?"rw":"read",f=e.launched,O=s((i,p,u=!0)=>{a.roots.push({id:i,private:u,match:p})},"own");if(o==="message"||f===void 0||f===null||l===null||f.some(i=>A(i,`crtr:memory:${c}:${l}`)))O(e.app.id,"any"),l!==null&&(a.namespaces=await V(t,l));else{O(e.app.id,"non-document"),o==="read"&&w!==null&&O(e.app.id,"run-report");for(const i of await t.prepare("SELECT o.id, i.external_id FROM objects o JOIN identities i ON i.object = o.id AND i.scheme = 'profile' WHERE o.owner = ? AND o.type = 'profile' AND o.deleted_at IS NULL").all(e.app.id))f.some(p=>A(p,`crtr:memory:${c}:${l}:profile:${i.external_id}`))&&O(i.id,"no-node")}if(o==="read"&&(a.packageApps=await Z(t,e,n)??[]),r===null)return a;a.predicates.spaces=!0;const
|
|
1
|
+
var v=Object.defineProperty;var s=(t,e)=>v(t,"name",{value:e,configurable:!0});import{covers as A}from"@north-light/identity";import{isCloudRole as j}from"../canvas/person-state.js";import{eligiblePackageApps as x}from"../packages/eligibility.js";import{subjectSet as F}from"./evaluator.js";import{FALSE as M,TRUE as C,and as U,f as h,or as D,raw as d}from"./sql.js";const k=s(t=>({ownApp:t,roots:[],namespaces:[],place:null,predicates:{allNodes:!1,spaces:!1},authorApps:[],packageApps:null}),"empty");async function $(t,e){return(await t.prepare("SELECT external_id FROM identities WHERE object = ? ORDER BY scheme LIMIT 1").get(e))?.external_id??null}s($,"externalId");async function _(t,e,n){return(await t.prepare("SELECT o.id FROM identities i JOIN objects o ON o.id = i.object WHERE i.scheme = ? AND i.external_id = ? AND o.deleted_at IS NULL").get(e,n))?.id??null}s(_,"objectOf");async function J(t){return(await t.prepare("SELECT id FROM objects WHERE type = 'person' AND home_machine IS NULL AND deleted_at IS NULL ORDER BY created_at").all()).map(n=>n.id)}s(J,"personObjectIds");async function P(t){return(await t.prepare(`SELECT DISTINCT i.object AS id FROM grants g JOIN identities i ON i.external_id = g.grantee AND i.scheme = 'app'
|
|
2
|
+
WHERE g.resolved_scopes_json::jsonb @> '[{"scope":"crtr:memory:manage:user","mode":"always"}]'::jsonb`).all()).map(n=>n.id)}s(P,"firstPartyApps");function y(t,e){return X(t).has(e.replace(/^app:/,""))}s(y,"holdsProviderGroup");const g=new WeakMap;function X(t){let e=g.get(t);if(e===void 0){e=new Set;for(const n of t){const o=n.indexOf(":");o>=0&&!n.includes(":",o+1)&&e.add(n.slice(0,o))}g.set(t,e)}return e}s(X,"providerGroupHolders");function z(t){return t.class==="object"?t.repos.map(e=>e.id):t.class!=="node"?[]:[...new Set([...t.repos,...t.projectRepos].map(e=>e.id))]}s(z,"repoRoots");async function Y(t,e){return e===null?[]:(await F(t,e)).groupIds.filter(n=>n.startsWith("group:")).map(n=>n.slice(6))}s(Y,"memberGroups");async function Z(t,e,n){if(e.class==="person")return null;const o=e.scopes;return await x(t,{holdsProvider:s(m=>o!==null&&y(o,m),"holdsProvider"),cloud:j(),ownExt:await $(t,e.app.id),user:n})}s(Z,"readablePackageApps");async function oe(t,e,n,o,m={}){if(e.class==="person")return null;const a=k(e.app.id),r=e.scopes,E=s(i=>r!==null&&r.some(p=>A(p,i)),"has"),l=await $(t,e.app.id),w=e.class==="node"?await R(t,e.node.id):e.place??null;a.place=w,a.authorApps=[e.app.id];const c=o==="write"?"rw":"read",f=e.launched,O=s((i,p,u=!0)=>{a.roots.push({id:i,private:u,match:p})},"own");if(o==="message"||f===void 0||f===null||l===null||f.some(i=>A(i,`crtr:memory:${c}:${l}`)))O(e.app.id,"any"),l!==null&&(a.namespaces=await V(t,l));else{O(e.app.id,"non-document"),o==="read"&&w!==null&&O(e.app.id,"run-report");for(const i of await t.prepare("SELECT o.id, i.external_id FROM objects o JOIN identities i ON i.object = o.id AND i.scheme = 'profile' WHERE o.owner = ? AND o.type = 'profile' AND o.deleted_at IS NULL").all(e.app.id))f.some(p=>A(p,`crtr:memory:${c}:${l}:profile:${i.external_id}`))&&O(i.id,"no-node")}if(o==="read"&&(a.packageApps=await Z(t,e,n)??[]),r===null)return a;a.predicates.spaces=!0;const H=await J(t),L=E("crtr:memory:manage:user"),T=L||E("crtr:memory:read_private:user"),N=s((i,p)=>{for(const u of H)u!==e.app.id&&a.roots.push({id:u,private:p,match:i})},"addPerson");if(o==="write")if(L)N("any",!0);else{E("crtr:memory:rw:user")&&(N("self",!0),N("authored:*",!0));for(const i of r){const p=/^crtr:memory:rw:user:(.+)$/.exec(i);p!==null&&N(`authored:${p[1]}`,!0)}E("crtr:objects:write:user")&&N("any",!0),a.roots.some(i=>i.match.startsWith("authored:"))&&a.authorApps.push(...await P(t))}else o==="read"&&(L?N("any",!0):N("direct-doc",T),E(`crtr:objects:${m.verb??"read"}:user`)&&N("any",T));const W=o==="write"?"write":m.verb??"read",b=e.class==="object"&&e.object.type.includes("."),S=s(async(i,p)=>{const u=await _(t,"app",i);u===null||u===e.app.id||a.roots.push({id:u,private:y(r,i),match:p})},"appRoot");for(const i of r){if(!i.startsWith("crtr:"))continue;let p=/^crtr:memory:(read|rw):app:([A-Za-z0-9_-]{1,64})(?::profile:([A-Za-z0-9_-]{1,64}))?$/.exec(i);if(p!==null&&(o==="read"||o==="write"&&p[1]==="rw")){if(p[3]===void 0)await S(`app:${p[2]}`,"no-node");else{const u=await _(t,"profile",p[3]);u!==null&&a.roots.push({id:u,private:!1,match:"no-node"})}continue}if(p=/^crtr:objects:(read|watch|link|write):(user|node|profile:[A-Za-z0-9_-]+|app:[A-Za-z0-9_-]+)$/.exec(i),p!==null&&o!=="message"&&(p[1]===W||o==="read"&&b&&p[1]==="watch")){const u=p[2];if(u==="node")a.predicates.allNodes=!0;else if(u.startsWith("profile:")){const I=await _(t,"profile",u.slice(8));I!==null&&a.roots.push({id:I,private:!1,match:"below"})}else u.startsWith("app:")&&await S(u,"objects-app");continue}if(p=/^crtr:conversations:read:(app:[A-Za-z0-9_-]+)$/.exec(i),p!==null&&o==="read"){await S(p[1],"node-report"),await S(p[1],"run-node");continue}p=/^crtr:objects:message:(app:[A-Za-z0-9_-]+)$/.exec(i),p!==null&&o==="message"&&await S(p[1],"node-tree")}if(o==="read"){for(const i of a.packageApps??[])i!==e.app.id&&a.roots.push({id:i,private:!1,match:"shipped"});for(const i of await t.prepare("SELECT o.id, i.external_id FROM objects o JOIN identities i ON i.object = o.id AND i.scheme = 'app' WHERE o.type = 'app' AND o.deleted_at IS NULL").all())i.id!==e.app.id&&a.roots.push({id:i.id,private:y(r,i.external_id),match:"custom"})}if(o!=="message")for(const i of[...z(e),...await Y(t,n)])a.roots.push({id:i,private:o==="read",match:"any"});return a}s(oe,"resolveCeiling");async function V(t,e){return(await t.prepare("SELECT namespace FROM schemas WHERE app = ?").all(e)).map(n=>n.namespace)}s(V,"ownNamespaces");async function R(t,e){const n=await t.prepare("SELECT run_id, isolated FROM nodes WHERE node_id = ?").get(e);if(n!==void 0)return{run:n.run_id??e,isolated:n.isolated===1};const o=await t.prepare("SELECT run_id FROM deleted_isolated_nodes WHERE node_id = ?").get(e);return o?{run:o.run_id,isolated:!0}:null}s(R,"nodePlace");async function re(t,e){const n=await R(e,t);n?.isolated&&await e.prepare("INSERT INTO deleted_isolated_nodes (node_id, run_id) VALUES (?, ?) ON CONFLICT (node_id) DO UPDATE SET run_id = excluded.run_id").run(t,n.run)}s(re,"rememberDeletedIsolated");function ie(t){return t.class==="person"?t:{...t,launched:null}}s(ie,"asRuntimeWriter");async function ae(t,e,n,o,m=!1){if(e.class==="person"||e.launched===void 0||e.launched===null)return null;const a=await t.prepare(`WITH RECURSIVE c(id, owner, type, kind, depth) AS (
|
|
3
3
|
SELECT id, owner, type, kind, 0 FROM objects WHERE id = ? UNION ALL
|
|
4
4
|
SELECT p.id, p.owner, p.type, p.kind, c.depth + 1 FROM c JOIN objects p ON p.id = c.owner WHERE c.depth < 12)
|
|
5
5
|
SELECT c.id, c.type, c.kind FROM c ORDER BY c.depth`).all(n);if(a[a.length-1]?.id!==e.app.id)return null;const r=await $(t,e.app.id);if(r===null)return null;if(a.length===1)return m?e.launched.some(c=>A(c,`crtr:memory:${o}:${r}`))?null:`crtr:memory:${o}:${r}`:null;if(!m&&a[0].type!=="document")return null;const E=a.slice(0,-1).reverse();if(o==="read"&&!m&&a[0].kind==="report"&&E[0].type==="node"&&e.class!=="object"){const c=e.class==="node"?await R(t,e.node.id):e.place??null,f=await R(t,E[0].id);if(c!==null&&f!==null&&c.run===f.run)return null}const l=E[0],w=l.type==="profile"?`crtr:memory:${o}:${r}:profile:${await $(t,l.id)}`:`crtr:memory:${o}:${r}`;return e.launched.some(c=>A(c,w))?null:w}s(ae,"missingMemoryScope");function pe(t,e){const n=[];t.roots.length>0&&n.push(B(t,e)),t.namespaces.length>0&&n.push(h`split_part(${d(e)}.schema_type, '/', 1) = ANY(${t.namespaces}::text[])`),t.predicates.allNodes&&n.push(d(`(${e}.type = 'node' AND ${e}.privacy = 'public')`)),t.predicates.spaces&&n.push(d(`(${e}.owner IS NULL AND ${e}.type IN ('app','person','repo','group'))`));const o=t.packageApps===null?C:h`(NOT EXISTS (SELECT 1 FROM documents sd WHERE sd.object = ${d(e)}.id AND sd.source_path IS NOT NULL) OR ${d(e)}.owner = ANY(${t.packageApps}::text[]))`;return U([D(n),G(t,e),o])}s(pe,"ceilingFrag");function G(t,e){if(t.place===null)return C;const n=t.place.isolated?1:0;return h`NOT EXISTS (WITH RECURSIVE ca(id, owner, depth, near_node) AS (
|
|
@@ -18,4 +18,4 @@ var v=Object.defineProperty;var s=(t,e)=>v(t,"name",{value:e,configurable:!0});i
|
|
|
18
18
|
SELECT p.id, p.owner, p.type, ca.depth + 1, ca.node_below OR ca.type = 'node', COALESCE(ca.near_node, CASE WHEN p.type = 'node' THEN p.id END)
|
|
19
19
|
FROM ca JOIN objects p ON p.id = ca.owner WHERE ca.depth < 12)
|
|
20
20
|
SELECT 1 FROM ca JOIN unnest(${n}::text[], ${o}::int[], ${m}::text[]) AS r(id, priv, match) ON r.id = ca.id
|
|
21
|
-
WHERE (${d(r("privacy"))} = 'public' OR r.priv = 1) AND ${w})`}s(B,"rootsFrag");export{ie as asRuntimeWriter,pe as ceilingFrag,y as holdsProviderGroup,ae as missingMemoryScope,R as nodePlace,J as personObjectIds,Z as readablePackageApps,re as rememberDeletedIsolated,oe as resolveCeiling};
|
|
21
|
+
WHERE (CASE WHEN ca.id = r.id THEN (${d(r("privacy"))} = 'public' OR r.priv = 1) ELSE FALSE END) AND ${w})`}s(B,"rootsFrag");export{ie as asRuntimeWriter,pe as ceilingFrag,y as holdsProviderGroup,ae as missingMemoryScope,R as nodePlace,J as personObjectIds,Z as readablePackageApps,re as rememberDeletedIsolated,oe as resolveCeiling};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@north-light/cli",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.688",
|
|
4
4
|
"description": "crtr — agent runtime with memory, plugins, and marketplaces",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -156,8 +156,8 @@
|
|
|
156
156
|
},
|
|
157
157
|
"license": "UNLICENSED",
|
|
158
158
|
"dependencies": {
|
|
159
|
-
"@north-light/identity": "0.3.
|
|
160
|
-
"@north-light/plugin": "0.3.
|
|
159
|
+
"@north-light/identity": "0.3.688",
|
|
160
|
+
"@north-light/plugin": "0.3.688",
|
|
161
161
|
"@earendil-works/chord": "1.5.2",
|
|
162
162
|
"@earendil-works/pi-agent-core": "1.5.2",
|
|
163
163
|
"@earendil-works/pi-ai": "1.5.2",
|
|
@@ -38,6 +38,8 @@ const CHUNK = 4 * 1024 * 1024;
|
|
|
38
38
|
/** Ranges a streaming read keeps in flight. Each range of a conversation object is its own R2 request, which costs seconds from the
|
|
39
39
|
* cloud, so one range at a time made a 35 MB conversation take two minutes and a broker starting on it time out. */
|
|
40
40
|
const READ_AHEAD = 4;
|
|
41
|
+
/** Part objects of one range fetched at once. */
|
|
42
|
+
const PART_FETCHES = 16;
|
|
41
43
|
/** `fetch(0)`, `fetch(1)`, … `fetch(count - 1)` in order, keeping up to `depth` of them running ahead of the consumer. */
|
|
42
44
|
export async function* readAhead(count, fetch, depth) {
|
|
43
45
|
const running = [];
|
|
@@ -62,16 +64,21 @@ async function bytesOf(snap, start, end) {
|
|
|
62
64
|
const out = [];
|
|
63
65
|
let offset = 0;
|
|
64
66
|
const blobs = await blobStore();
|
|
67
|
+
const fetches = [];
|
|
65
68
|
for (const part of snap.parts) {
|
|
66
69
|
const from = Math.max(start, offset), to = Math.min(end, offset + part.bytes);
|
|
67
|
-
if (from < to)
|
|
68
|
-
|
|
69
|
-
if (!data)
|
|
70
|
-
throw new Error(`missing conversation object: ${part.blob_key}`);
|
|
71
|
-
out.push(data);
|
|
72
|
-
}
|
|
70
|
+
if (from < to)
|
|
71
|
+
fetches.push({ key: part.blob_key, range: { start: from - offset, length: to - from } });
|
|
73
72
|
offset += part.bytes;
|
|
74
73
|
}
|
|
74
|
+
// A conversation is one small object per commit, and each get is a round trip, so the parts are fetched PART_FETCHES at a time.
|
|
75
|
+
for await (const data of readAhead(fetches.length, async (i) => {
|
|
76
|
+
const got = await blobs.get(fetches[i].key, fetches[i].range);
|
|
77
|
+
if (!got)
|
|
78
|
+
throw new Error(`missing conversation object: ${fetches[i].key}`);
|
|
79
|
+
return got;
|
|
80
|
+
}, PART_FETCHES))
|
|
81
|
+
out.push(data);
|
|
75
82
|
for (const row of snap.tail) {
|
|
76
83
|
const from = Math.max(start, offset), to = Math.min(end, offset + row.bytes);
|
|
77
84
|
if (from < to)
|
|
@@ -288,6 +288,7 @@ function placeFrag(c, o) {
|
|
|
288
288
|
AND NOT (CASE WHEN cn.node_id IS NOT NULL THEN COALESCE(cn.run_id, cn.node_id) = ${c.place.run} OR (cn.isolated = 0 AND ${iso} = 0)
|
|
289
289
|
WHEN cd.node_id IS NOT NULL THEN cd.run_id = ${c.place.run} ELSE ${iso} = 0 END))`;
|
|
290
290
|
}
|
|
291
|
+
/** Only the privacy test sits in the CASE, which repeats the join equality (always true for joined rows) so PostgreSQL cannot push this outer-row-dependent filter into the unnest scan and rebuild the root hash per candidate. The match conditions stay a plain AND. */
|
|
291
292
|
function rootsFrag(c, o) {
|
|
292
293
|
const ids = c.roots.map((r) => r.id);
|
|
293
294
|
const priv = c.roots.map((r) => (r.private ? 1 : 0));
|
|
@@ -328,5 +329,5 @@ function rootsFrag(c, o) {
|
|
|
328
329
|
SELECT p.id, p.owner, p.type, ca.depth + 1, ca.node_below OR ca.type = 'node', COALESCE(ca.near_node, CASE WHEN p.type = 'node' THEN p.id END)
|
|
329
330
|
FROM ca JOIN objects p ON p.id = ca.owner WHERE ca.depth < 12)
|
|
330
331
|
SELECT 1 FROM ca JOIN unnest(${ids}::text[], ${priv}::int[], ${match}::text[]) AS r(id, priv, match) ON r.id = ca.id
|
|
331
|
-
WHERE (${raw(O('privacy'))} = 'public' OR r.priv = 1) AND ${matchSql})`;
|
|
332
|
+
WHERE (CASE WHEN ca.id = r.id THEN (${raw(O('privacy'))} = 'public' OR r.priv = 1) ELSE FALSE END) AND ${matchSql})`;
|
|
332
333
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@north-light/api",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.688",
|
|
4
4
|
"description": "Typed crtrd /v1 API contract — DTOs, route builders, the error contract, the CrtrClient, and the command-plugin manifest format. Zero runtime dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/api/index.js",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@north-light/plugin",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.688",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"postpack": "node ../../scripts/pack-minify.mjs postpack packages/crouter-plugin"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@north-light/api": "0.3.
|
|
31
|
-
"@north-light/identity": "0.3.
|
|
30
|
+
"@north-light/api": "0.3.688",
|
|
31
|
+
"@north-light/identity": "0.3.688"
|
|
32
32
|
},
|
|
33
33
|
"license": "UNLICENSED",
|
|
34
34
|
"publishConfig": {
|
package/runtime.lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@north-light/cli",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.688",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@north-light/cli",
|
|
9
|
-
"version": "0.3.
|
|
9
|
+
"version": "0.3.688",
|
|
10
10
|
"hasInstallScript": true,
|
|
11
11
|
"license": "UNLICENSED",
|
|
12
12
|
"workspaces": [
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"@milkdown/preset-gfm": "^7.22.3",
|
|
29
29
|
"@milkdown/prose": "^7.22.3",
|
|
30
30
|
"@milkdown/utils": "^7.22.3",
|
|
31
|
-
"@north-light/identity": "0.3.
|
|
32
|
-
"@north-light/plugin": "0.3.
|
|
31
|
+
"@north-light/identity": "0.3.688",
|
|
32
|
+
"@north-light/plugin": "0.3.688",
|
|
33
33
|
"cron-parser": "^5.6.0",
|
|
34
34
|
"esbuild": "0.28.2",
|
|
35
35
|
"jose": "^6.2.12",
|
|
@@ -10667,17 +10667,17 @@
|
|
|
10667
10667
|
},
|
|
10668
10668
|
"packages/crouter-api": {
|
|
10669
10669
|
"name": "@north-light/api",
|
|
10670
|
-
"version": "0.3.
|
|
10670
|
+
"version": "0.3.688",
|
|
10671
10671
|
"license": "UNLICENSED"
|
|
10672
10672
|
},
|
|
10673
10673
|
"packages/crouter-env-docker": {
|
|
10674
10674
|
"name": "@north-light/env-docker",
|
|
10675
|
-
"version": "0.3.
|
|
10675
|
+
"version": "0.3.688",
|
|
10676
10676
|
"license": "UNLICENSED"
|
|
10677
10677
|
},
|
|
10678
10678
|
"packages/crouter-identity": {
|
|
10679
10679
|
"name": "@north-light/identity",
|
|
10680
|
-
"version": "0.3.
|
|
10680
|
+
"version": "0.3.688",
|
|
10681
10681
|
"license": "UNLICENSED",
|
|
10682
10682
|
"dependencies": {
|
|
10683
10683
|
"jose": "^6.2.1"
|
|
@@ -10685,21 +10685,21 @@
|
|
|
10685
10685
|
},
|
|
10686
10686
|
"packages/crouter-plugin": {
|
|
10687
10687
|
"name": "@north-light/plugin",
|
|
10688
|
-
"version": "0.3.
|
|
10688
|
+
"version": "0.3.688",
|
|
10689
10689
|
"license": "UNLICENSED",
|
|
10690
10690
|
"dependencies": {
|
|
10691
|
-
"@north-light/api": "0.3.
|
|
10692
|
-
"@north-light/identity": "0.3.
|
|
10691
|
+
"@north-light/api": "0.3.688",
|
|
10692
|
+
"@north-light/identity": "0.3.688"
|
|
10693
10693
|
}
|
|
10694
10694
|
},
|
|
10695
10695
|
"packages/crouter-sdk": {
|
|
10696
10696
|
"name": "@north-light/sdk",
|
|
10697
|
-
"version": "0.3.
|
|
10697
|
+
"version": "0.3.688",
|
|
10698
10698
|
"license": "UNLICENSED",
|
|
10699
10699
|
"dependencies": {
|
|
10700
|
-
"@north-light/api": "0.3.
|
|
10701
|
-
"@north-light/identity": "0.3.
|
|
10702
|
-
"@north-light/plugin": "0.3.
|
|
10700
|
+
"@north-light/api": "0.3.688",
|
|
10701
|
+
"@north-light/identity": "0.3.688",
|
|
10702
|
+
"@north-light/plugin": "0.3.688",
|
|
10703
10703
|
"jose": "^6.2.1"
|
|
10704
10704
|
},
|
|
10705
10705
|
"bin": {
|