@north-light/cli 0.3.726 → 0.3.727

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.
@@ -1,4 +1,4 @@
1
1
  var N=Object.defineProperty;var o=(e,t)=>N(e,"name",{value:t,configurable:!0});import{AsyncLocalStorage as k}from"node:async_hooks";import{randomBytes as B}from"node:crypto";import{appendFileSync as I,mkdirSync as U,readFileSync as q}from"node:fs";import{join as G}from"node:path";import y from"pg";import{resolveDatabaseUrl as K}from"../../daemon/postgres/supervisor.js";import{publishFileExclusive as X}from"../fs-utils.js";import{initializeEmptyLayout as F,runtimeRoot as d}from"../layout.js";import{translatePlaceholders as H,translateOnce as J}from"../statements.js";import{ensureHome as V}from"./paths.js";import{addStepTiming as w,isCollectingStepTimings as W,statementLabel as Z}from"../step-timings.js";import{emitEvent as z}from"../events/emit.js";import{ensureSchema as j}from"./schema-chain.js";import{canvasDatabaseSchema as Y,canvasSchemaDatabaseUrl as Q,canvasSearchPathOptions as O}from"./schema-version.js";const tt="crtr_test_template";class et extends Error{static{o(this,"CanvasConstraintError")}constraint;code="23505";constructor(t,n,a){super(t,a),this.name="CanvasConstraintError",this.constraint=n}}function L(e){const t=process.env.CRTR_DB_OPEN_LEDGER;if(t)try{const n=`${new Date().toISOString()} ${process.pid} ${process.argv[1]??"<unknown>"} ${e.replace(/\/\/([^:@/]*):[^@/]*@/,"//$1@")}
2
- `;I(t,n)}catch{}}o(L,"recordOpenLedger");const nt=6,rt=12,R=2;function x(e=process.env){const t=e.CRTR_DATABASE_POOL_MAX?.trim();if(t){const n=Number(t);if(!Number.isInteger(n)||n<R)throw new Error(`CRTR_DATABASE_POOL_MAX must be an integer of at least ${R}; got ${JSON.stringify(t)}`);return n}return e.CRTR_DATABASE_URL?rt:nt}o(x,"canvasPoolSize");const C=3e4,ot=1e3,E=new k,at=new Set([114,3802]),st={getTypeParser:o(((e,t)=>at.has(e)?n=>n:e===20||e===1700?n=>{const a=Number(n);if(e===20&&!Number.isSafeInteger(a))throw new Error(`canvas db: bigint ${n} does not fit a JS number`);return a}:y.types.getTypeParser(e,t)),"getTypeParser")};class ct{static{o(this,"Handle")}url;db;pool=null;schema=null;constructor(t){this.url=t;const n=this;this.db={prepare:o(a=>this.statement(a,!0),"prepare"),prepareUncached:o(a=>this.statement(a,!1),"prepareUncached"),exec:o(a=>this.run({text:a}).then(()=>{}),"exec"),get inTransaction(){return M(n)}}}ready(){return this.schema??=ut(this.url).catch(t=>{throw this.schema=null,t}),this.schema}connections(){if(this.pool!==null)return this.pool;const t=new y.Pool({connectionString:this.url,types:st,application_name:`crtr-${process.pid}`,connectionTimeoutMillis:C,options:O(),max:x(),idleTimeoutMillis:1e4,allowExitOnIdle:!0});return t.on("connect",()=>L(this.url)),t.on("error",n=>console.warn(`[canvas db] pooled connection error: ${n.message}`)),this.pool=t}async checkout(){try{return await this.connections().connect()}catch(t){throw this.mapError(t)}}async run(t){const n=E.getStore();try{if(n!==void 0&&n.open&&n.handle===this){const a=performance.now();try{return await n.client.query(t)}finally{const s=performance.now()-a;n.statementMs+=s,n.statements?.push({text:t.text,ms:s})}}return await this.connections().query(t)}catch(a){throw this.mapError(a)}}mapError(t){const n=t;return n.code==="23505"?new et(n.message??"unique constraint violation",n.constraint,{cause:t}):/timeout exceeded when trying to connect/i.test(n.message??"")?new Error(`canvas db: no pooled connection became free within ${C} ms (pool of ${x()}); a transaction body is holding connections across a slow await`,{cause:t}):t}statement(t,n){const a=n?(()=>{const r=H(t);return{text:r.text,name:r.name}})():{text:J(t).text},s=o(r=>this.run({...a,values:r.map(it)}),"query");return{get:o(async(...r)=>(await s(r)).rows[0],"get"),all:o(async(...r)=>(await s(r)).rows,"all"),run:o(async(...r)=>({changes:(await s(r)).rowCount??0}),"run")}}async close(){const t=this.pool;this.pool=null,await t?.end().catch(()=>{})}}function it(e){return ArrayBuffer.isView(e)&&!Buffer.isBuffer(e)?Buffer.from(e.buffer,e.byteOffset,e.byteLength):e}o(it,"bindable");function M(e){const t=E.getStore();return t!==void 0&&t.open&&t.handle===e}o(M,"ownsTransaction");async function ut(e){const t=o((f,l,h)=>{z({level:f,event:l,fields:h})},"emit"),n=Y(),a=Q(),s=a??e;L(s);const r=new y.Client({connectionString:s,application_name:`crtr-schema-${process.pid}`,connectionTimeoutMillis:1e4,...a===void 0?{options:O()}:{}});r.on("error",()=>{}),await r.connect();try{await j(r,{schema:n,emit:t})}finally{await r.end().catch(()=>{})}}o(ut,"applySchema");function lt(){return process.env.NODE_TEST_CONTEXT!==void 0||process.execArgv.includes("--test")}o(lt,"runningUnderNodeTest");function S(e,t){const n=/^(postgres(?:ql)?:\/\/[^/?#]*)(\/[^?#]*)?(.*)$/s.exec(e);if(n===null)throw new Error(`not a postgres URL: ${e.replace(/\/\/[^@/]*@/,"//")}`);return`${n[1]}/${t}${n[3]}`}o(S,"withDatabase");async function mt(e){const t=G(d(),"test-db"),n=o(()=>{try{return q(t,"utf8").trim()||void 0}catch(f){if(f.code==="ENOENT")return;throw f}},"recorded"),a=n();if(a!==void 0)return S(e,a);U(d(),{recursive:!0});const s=`t_${B(8).toString("hex")}`,r=new y.Client({connectionString:S(e,"postgres")});r.on("error",()=>{}),await r.connect();try{if(await r.query(`CREATE DATABASE ${s} TEMPLATE ${tt} STRATEGY file_copy`),!X(t,`${s}
2
+ `;I(t,n)}catch{}}o(L,"recordOpenLedger");const nt=6,rt=12,R=2;function x(e=process.env){const t=e.CRTR_DATABASE_POOL_MAX?.trim();if(t){const n=Number(t);if(!Number.isInteger(n)||n<R)throw new Error(`CRTR_DATABASE_POOL_MAX must be an integer of at least ${R}; got ${JSON.stringify(t)}`);return n}return e.CRTR_DATABASE_URL?rt:nt}o(x,"canvasPoolSize");const C=3e4,ot=1e3,E=new k,at=new Set([114,3802]),st={getTypeParser:o(((e,t)=>at.has(e)?n=>n:e===20||e===1700?n=>{const a=Number(n);if(e===20&&!Number.isSafeInteger(a))throw new Error(`canvas db: bigint ${n} does not fit a JS number`);return a}:y.types.getTypeParser(e,t)),"getTypeParser")};class ct{static{o(this,"Handle")}url;db;pool=null;schema=null;constructor(t){this.url=t;const n=this;this.db={prepare:o(a=>this.statement(a,!0),"prepare"),prepareUncached:o(a=>this.statement(a,!1),"prepareUncached"),exec:o(a=>this.run({text:a}).then(()=>{}),"exec"),get inTransaction(){return M(n)}}}ready(){return this.schema??=ut(this.url).catch(t=>{throw this.schema=null,t}),this.schema}connections(){if(this.pool!==null)return this.pool;const t=new y.Pool({connectionString:this.url,types:st,application_name:`crtr-${process.pid}`,connectionTimeoutMillis:C,options:O(),max:x(),idleTimeoutMillis:1e4,allowExitOnIdle:!0});return t.on("connect",()=>L(this.url)),t.on("error",n=>console.warn(`[canvas db] pooled connection error: ${n.message}`)),this.pool=t}async checkout(){try{return await this.connections().connect()}catch(t){throw this.mapError(t)}}async run(t){const n=E.getStore();try{if(n!==void 0&&n.open&&n.handle.url===this.url){const a=performance.now();try{return await n.client.query(t)}finally{const s=performance.now()-a;n.statementMs+=s,n.statements?.push({text:t.text,ms:s})}}return await this.connections().query(t)}catch(a){throw this.mapError(a)}}mapError(t){const n=t;return n.code==="23505"?new et(n.message??"unique constraint violation",n.constraint,{cause:t}):/timeout exceeded when trying to connect/i.test(n.message??"")?new Error(`canvas db: no pooled connection became free within ${C} ms (pool of ${x()}); a transaction body is holding connections across a slow await`,{cause:t}):t}statement(t,n){const a=n?(()=>{const r=H(t);return{text:r.text,name:r.name}})():{text:J(t).text},s=o(r=>this.run({...a,values:r.map(it)}),"query");return{get:o(async(...r)=>(await s(r)).rows[0],"get"),all:o(async(...r)=>(await s(r)).rows,"all"),run:o(async(...r)=>({changes:(await s(r)).rowCount??0}),"run")}}async close(){const t=this.pool;this.pool=null,await t?.end().catch(()=>{})}}function it(e){return ArrayBuffer.isView(e)&&!Buffer.isBuffer(e)?Buffer.from(e.buffer,e.byteOffset,e.byteLength):e}o(it,"bindable");function M(e){const t=E.getStore();return t!==void 0&&t.open&&t.handle.url===e.url}o(M,"ownsTransaction");async function ut(e){const t=o((f,l,h)=>{z({level:f,event:l,fields:h})},"emit"),n=Y(),a=Q(),s=a??e;L(s);const r=new y.Client({connectionString:s,application_name:`crtr-schema-${process.pid}`,connectionTimeoutMillis:1e4,...a===void 0?{options:O()}:{}});r.on("error",()=>{}),await r.connect();try{await j(r,{schema:n,emit:t})}finally{await r.end().catch(()=>{})}}o(ut,"applySchema");function lt(){return process.env.NODE_TEST_CONTEXT!==void 0||process.execArgv.includes("--test")}o(lt,"runningUnderNodeTest");function S(e,t){const n=/^(postgres(?:ql)?:\/\/[^/?#]*)(\/[^?#]*)?(.*)$/s.exec(e);if(n===null)throw new Error(`not a postgres URL: ${e.replace(/\/\/[^@/]*@/,"//")}`);return`${n[1]}/${t}${n[3]}`}o(S,"withDatabase");async function mt(e){const t=G(d(),"test-db"),n=o(()=>{try{return q(t,"utf8").trim()||void 0}catch(f){if(f.code==="ENOENT")return;throw f}},"recorded"),a=n();if(a!==void 0)return S(e,a);U(d(),{recursive:!0});const s=`t_${B(8).toString("hex")}`,r=new y.Client({connectionString:S(e,"postgres")});r.on("error",()=>{}),await r.connect();try{if(await r.query(`CREATE DATABASE ${s} TEMPLATE ${tt} STRATEGY file_copy`),!X(t,`${s}
3
3
  `,384))return await r.query(`DROP DATABASE ${s}`),S(e,n())}finally{await r.end()}return S(e,s)}o(mt,"testDatabaseUrl");async function ft(){const e=process.env.CRTR_TEST_PG_URL;if(e)return mt(e);if(lt())throw new Error("CRTR_TEST_PG_URL is not set: a test must reach its own Postgres (run the suite through the lane runner); refusing to open a real canvas database");return K(d())}o(ft,"canvasDatabaseUrl");const p=new Map,_=new Map;async function v(){F(),V();const e=await ft();_.set(d(),e);let t=p.get(e);return t===void 0&&p.set(e,t=new ct(e)),await t.ready(),t}o(v,"openHandle");async function xt(){return(await v()).db}o(xt,"openDb");async function Ct(){const e=_.get(d());if(e===void 0)return;_.delete(d());const t=p.get(e);t!==void 0&&(p.delete(e),await t.close())}o(Ct,"closeDb");async function Mt(){const e=[...p.values()];p.clear(),_.clear(),await Promise.all(e.map(t=>t.close()))}o(Mt,"closeAllDbs");function vt(e){const t=E.getStore();if(t===void 0||!t.open){queueMicrotask(e);return}t.afterCommit.push(e)}o(vt,"afterCommit");let dt=0;async function Pt(e,t={}){const n=await v(),a=n.db;if(M(n)){if(!t.savepoint&&!t.rollback)return e(a);const i=E.getStore(),c=`canvas_write_${++dt}`;await n.run({text:`SAVEPOINT ${c}`});const m=i.afterCommit,A=m.length;let $;try{$=await e(a)}catch(D){throw await n.run({text:`ROLLBACK TO ${c}; RELEASE ${c}`}),m.splice(A),D}return await n.run({text:t.rollback?`ROLLBACK TO ${c}; RELEASE ${c}`:`RELEASE ${c}`}),t.rollback&&m.splice(A),$}const s=new Error("canvas write transaction entered here"),r=W()?t.timingName:void 0,f=performance.now(),l=await n.checkout();r!==void 0&&w(`${r}_checkout_wait`,performance.now()-f);let h;const g=o(i=>{h??=i},"onConnectionError");l.on("error",g);const P=performance.now();let T=!1,b=!1;const u={handle:n,client:l,open:!0,afterCommit:[],statementMs:0,...r===void 0?{}:{statements:[]}};try{await l.query("BEGIN");try{const i=await E.run(u,()=>e(a));return await l.query(t.rollback?"ROLLBACK":"COMMIT"),T=!t.rollback,i}catch(i){b=!0;try{await l.query("ROLLBACK")}catch(c){h??=c}throw n.mapError(i)}}finally{u.open=!1;const i=performance.now()-P;if(r!==void 0&&u.statements!==void 0&&(w(`${r}_statements_count`,u.statements.length),w(`${r}_statements_ms`,u.statementMs),w(`${r}_other_ms`,i-u.statementMs),[...u.statements].sort((c,m)=>m.ms-c.ms).slice(0,3).forEach((c,m)=>w(`${r}_slow${m+1}_${Z(c.text)}`,c.ms))),!b&&i-u.statementMs>ot&&console.warn(`[canvas db] write transaction held its connection ${Math.round(i)}ms (${Math.round(u.statementMs)}ms in its statements); its body did non-db work while holding a pooled connection and its row locks
4
4
  ${s.stack}`),l.removeListener("error",g),l.release(h),T)for(const c of u.afterCommit)queueMicrotask(c)}}o(Pt,"withCanvasWrite");export{et as CanvasConstraintError,vt as afterCommit,ft as canvasDatabaseUrl,x as canvasPoolSize,Mt as closeAllDbs,Ct as closeDb,xt as openDb,Pt as withCanvasWrite};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@north-light/cli",
3
- "version": "0.3.726",
3
+ "version": "0.3.727",
4
4
  "description": "crtr — agent runtime with memory, plugins, and marketplaces",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -158,8 +158,8 @@
158
158
  },
159
159
  "license": "UNLICENSED",
160
160
  "dependencies": {
161
- "@north-light/identity": "0.3.726",
162
- "@north-light/plugin": "0.3.726",
161
+ "@north-light/identity": "0.3.727",
162
+ "@north-light/plugin": "0.3.727",
163
163
  "@earendil-works/chord": "1.5.2",
164
164
  "@earendil-works/pi-agent-core": "1.5.2",
165
165
  "@earendil-works/pi-ai": "1.5.2",
@@ -160,7 +160,7 @@ class Handle {
160
160
  async run(config) {
161
161
  const transaction = transactionContext.getStore();
162
162
  try {
163
- if (transaction !== undefined && transaction.open && transaction.handle === this) {
163
+ if (transaction !== undefined && transaction.open && transaction.handle.url === this.url) {
164
164
  const began = performance.now();
165
165
  try {
166
166
  return await transaction.client.query(config);
@@ -212,7 +212,7 @@ function bindable(value) {
212
212
  }
213
213
  function ownsTransaction(handle) {
214
214
  const owned = transactionContext.getStore();
215
- return owned !== undefined && owned.open && owned.handle === handle;
215
+ return owned !== undefined && owned.open && owned.handle.url === handle.url;
216
216
  }
217
217
  // ---------------------------------------------------------------------------
218
218
  // Schema
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@north-light/api",
3
- "version": "0.3.726",
3
+ "version": "0.3.727",
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/identity",
3
- "version": "0.3.726",
3
+ "version": "0.3.727",
4
4
  "description": "Northlight access-token verification and scope rules",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@north-light/plugin",
3
- "version": "0.3.726",
3
+ "version": "0.3.727",
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.726",
31
- "@north-light/identity": "0.3.726"
30
+ "@north-light/api": "0.3.727",
31
+ "@north-light/identity": "0.3.727"
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.726",
3
+ "version": "0.3.727",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@north-light/cli",
9
- "version": "0.3.726",
9
+ "version": "0.3.727",
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.726",
32
- "@north-light/plugin": "0.3.726",
31
+ "@north-light/identity": "0.3.727",
32
+ "@north-light/plugin": "0.3.727",
33
33
  "cron-parser": "^5.6.0",
34
34
  "esbuild": "0.28.2",
35
35
  "jose": "^6.2.12",
@@ -11902,17 +11902,17 @@
11902
11902
  },
11903
11903
  "packages/crouter-api": {
11904
11904
  "name": "@north-light/api",
11905
- "version": "0.3.726",
11905
+ "version": "0.3.727",
11906
11906
  "license": "UNLICENSED"
11907
11907
  },
11908
11908
  "packages/crouter-env-docker": {
11909
11909
  "name": "@north-light/env-docker",
11910
- "version": "0.3.726",
11910
+ "version": "0.3.727",
11911
11911
  "license": "UNLICENSED"
11912
11912
  },
11913
11913
  "packages/crouter-identity": {
11914
11914
  "name": "@north-light/identity",
11915
- "version": "0.3.726",
11915
+ "version": "0.3.727",
11916
11916
  "license": "UNLICENSED",
11917
11917
  "dependencies": {
11918
11918
  "jose": "^6.2.1"
@@ -11920,21 +11920,21 @@
11920
11920
  },
11921
11921
  "packages/crouter-plugin": {
11922
11922
  "name": "@north-light/plugin",
11923
- "version": "0.3.726",
11923
+ "version": "0.3.727",
11924
11924
  "license": "UNLICENSED",
11925
11925
  "dependencies": {
11926
- "@north-light/api": "0.3.726",
11927
- "@north-light/identity": "0.3.726"
11926
+ "@north-light/api": "0.3.727",
11927
+ "@north-light/identity": "0.3.727"
11928
11928
  }
11929
11929
  },
11930
11930
  "packages/crouter-sdk": {
11931
11931
  "name": "@north-light/sdk",
11932
- "version": "0.3.726",
11932
+ "version": "0.3.727",
11933
11933
  "license": "UNLICENSED",
11934
11934
  "dependencies": {
11935
- "@north-light/api": "0.3.726",
11936
- "@north-light/identity": "0.3.726",
11937
- "@north-light/plugin": "0.3.726",
11935
+ "@north-light/api": "0.3.727",
11936
+ "@north-light/identity": "0.3.727",
11937
+ "@north-light/plugin": "0.3.727",
11938
11938
  "jose": "^6.2.1"
11939
11939
  },
11940
11940
  "bin": {