@north-light/cli 0.3.634 → 0.3.636
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.
|
@@ -7,9 +7,17 @@ export declare class ImportCloud {
|
|
|
7
7
|
private readonly token;
|
|
8
8
|
private readonly retryBaseMs;
|
|
9
9
|
private readonly putIdleMs;
|
|
10
|
+
private readonly retryBudgetMs;
|
|
10
11
|
/** `token` is a fixed import token, or a getter for a person bearer (5-minute tokens, so a long import asks per call). */
|
|
11
|
-
constructor(apiUrl: string, token: string | (() => Promise<string>), retryBaseMs?: number, putIdleMs?: number);
|
|
12
|
+
constructor(apiUrl: string, token: string | (() => Promise<string>), retryBaseMs?: number, putIdleMs?: number, retryBudgetMs?: number);
|
|
13
|
+
/**
|
|
14
|
+
* After a transient failure, wait the backoff for try `attempt` and answer true, or answer false when `retryBudgetMs` has passed since the first failure (`since`).
|
|
15
|
+
* Shared by `put` and `call`, so the rule is one rule.
|
|
16
|
+
*/
|
|
17
|
+
private backoff;
|
|
18
|
+
/** One API call. With `retry` (an idempotent call on the upload path), a network failure, a stalled connection, a 408, 429 or 5xx answer, or a person bearer that could not be fetched because Northlight was unreachable is retried until the budget is spent. */
|
|
12
19
|
private call;
|
|
20
|
+
private callOnce;
|
|
13
21
|
start(request: ImportStartRequest): Promise<ImportStartedDTO>;
|
|
14
22
|
uploadUrls(run: string, keys: string[]): Promise<ImportUploadUrlsDTO>;
|
|
15
23
|
putStore(run: string, name: ImportStoreNameDTO, body: unknown): Promise<void>;
|
|
@@ -22,8 +30,8 @@ export declare class ImportCloud {
|
|
|
22
30
|
}>;
|
|
23
31
|
/**
|
|
24
32
|
* PUT bytes to a signed URL, retrying what is transient: a failed, dropped or stalled connection (socket closed or reset, no progress within the attempt's deadline)
|
|
25
|
-
* and a 408, 429 or 5xx answer, with doubling backoff,
|
|
26
|
-
* is given the URL is replaced by one from `uploadUrls`, once. Any other answer, or running out of
|
|
33
|
+
* and a 408, 429 or 5xx answer, with doubling backoff capped at 30 s, until `retryBudgetMs` (10 minutes) has passed since the first failure. A 403 is a signed URL that has expired (or been refused): when `freshUrl`
|
|
34
|
+
* is given the URL is replaced by one from `uploadUrls`, once. Any other answer, or running out of budget, throws naming `key`.
|
|
27
35
|
*/
|
|
28
36
|
put(url: string, data: Buffer, key: string, freshUrl?: () => Promise<string>): Promise<void>;
|
|
29
37
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var _=Object.defineProperty;var f=(o,t)=>_(o,"name",{value:t,configurable:!0});import{CrtrError as T,general as d}from"../../errors.js";import{NORTHLIGHT_UNREACHABLE as O}from"../../directory/reach.js";const x="stores.json",I=["auth","credentials","env","settings","plugins"],B=10*6e4,R=3e4,U=6e4,y=64*1024,S=f(o=>o===408||o===429||o>=500,"isTransientStatus");function M(o){const t=o.cause;return`failed: ${o instanceof Error?o.message:String(o)}${typeof t?.code=="string"?` (${t.code})`:""}`}f(M,"failureText");class C{static{f(this,"ImportCloud")}apiUrl;token;retryBaseMs;putIdleMs;retryBudgetMs;constructor(t,e,r=500,l=U,a=B){this.apiUrl=t,this.token=e,this.retryBaseMs=r,this.putIdleMs=l,this.retryBudgetMs=a}async backoff(t,e){return Date.now()-e>=this.retryBudgetMs?!1:(await new Promise(r=>setTimeout(r,Math.min(this.retryBaseMs*2**(t-1),R)*(.5+Math.random()/2))),!0)}async call(t,e,r,l=!1){let a;for(let i=1;;i+=1)try{return await this.callOnce(t,e,r,l)}catch(n){const s=n instanceof T?n.details?.status:void 0,u=n instanceof T&&(n.code===O||n.details?.network===!0||typeof s=="number"&&S(s));if(!l||!u||(a??=Date.now(),!await this.backoff(i,a)))throw n}}async callOnce(t,e,r,l){let a,i;const n=`Bearer ${typeof this.token=="string"?this.token:await this.token()}`;try{i=await fetch(new URL(e,this.apiUrl),{method:t,headers:{authorization:n,...r===void 0?{}:{"content-type":"application/json"}},...r===void 0?{}:{body:JSON.stringify(r)},...l?{signal:AbortSignal.timeout(this.putIdleMs)}:{}}),a=await i.text()}catch(s){throw d(`${t} ${e} ${M(s)}`,{network:!0})}if(!i.ok){let s;try{s=JSON.parse(a).error?.code}catch{s=void 0}throw d(`${t} ${e} answered ${i.status}: ${a.slice(0,500)}`,{status:i.status,...typeof s=="string"?{code:s}:{}})}return a===""?void 0:JSON.parse(a)}start(t){return this.call("POST","/v1/imports",t)}uploadUrls(t,e){return this.call("POST",`/v1/imports/${t}/upload-urls`,{keys:e},!0)}putStore(t,e,r){return this.call("PUT",`/v1/imports/${t}/stores/${e}`,r,!0)}load(t){return this.call("POST",`/v1/imports/${t}/load`,{})}get(t){return this.call("GET",`/v1/imports/${t}`)}machineCredential(t,e=!1){return this.call("POST",`/v1/imports/${t}/machine-credential${e?"?rotate=1":""}`,{})}async put(t,e,r,l){let a=!1,i;for(let n=1;;n+=1){let s,u;const $=new AbortController;let p;const w=f(()=>{clearTimeout(p),p=setTimeout(()=>$.abort(new Error(`no progress for ${Math.round(this.putIdleMs/1e3)}s`)),this.putIdleMs)},"arm");let h=0;const E=new ReadableStream({pull(c){if(w(),h>=e.length){c.close();return}c.enqueue(e.subarray(h,h+y)),h+=y}},{highWaterMark:0});try{w();const c=await fetch(t,{method:"PUT",headers:{"content-length":String(e.length)},body:E,duplex:"half",signal:$.signal});if(w(),c.ok){await c.arrayBuffer();return}const m=c.status;let g="";try{g=(await c.text()).slice(0,300)}catch{}if(s=`answered ${m}: ${g}`,m===403&&l!==void 0&&!a){a=!0,t=await l();continue}u=S(m)}catch(c){s=M(c),u=!0}finally{clearTimeout(p)}if(!u)throw d(`upload of ${r} ${s}`,{key:r});if(i??=Date.now(),!await this.backoff(n,i))throw d(`upload of ${r} ${s} after ${n} tries`,{key:r})}}}export{C as ImportCloud,x as STORE_FILE,I as STORE_ORDER};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var v=Object.defineProperty;var a=(t,n)=>v(t,"name",{value:n,configurable:!0});import{randomBytes as $}from"node:crypto";import{ApiError as o}from"../../../api/errors.js";import{blobStore as c}from"../../../core/blobs/index.js";import{presignPutUrl as O,r2ConfigFromEnv as T}from"../../../core/blobs/r2.js";import{openDb as m}from"../../../core/canvas/db.js";import{emitEvent as R}from"../../../core/events/emit.js";import{bundleKeys as f}from"../../../core/import/bundle/layout.js";import{deleteRunPrefix as D}from"../../../core/import/load/blobs.js";import{putStore as N,StoreBodyError as A,STORE_NAMES as P}from"../../../core/import/load/stores.js";import{rollbackRun as j}from"../../../core/import/load/rollback.js";import{crtrVersion as y}from"../../../shared/crtr-version.js";import{importTokenFor as I}from"../import-auth.js";const g=3600;function b(t){if(t.principal.class!=="person"||t.principal.sub===void 0)throw new o(403,"person_only","an import is made by a signed-in person");return t.principal.sub}a(b,"personOf");async function _(t,n=!1){const e=t.params.run_id??"",r=await(await m()).prepare("SELECT * FROM import_runs WHERE run_id = ?").get(e),s=t.principal;if(s.class==="import"){if(s.run===null&&!n)throw new o(403,"permission_error","the service credential starts and reads runs; the import token uploads and loads");if(r===void 0||(s.run===null?r.source_kind!=="hosted":s.run!==r.run_id))throw new o(404,"import_not_found",`no import run ${e}`);if(s.run!==null&&!n&&r.state!=="uploading"&&r.state!=="loading")throw new o(403,"import_token_expired",`import run ${r.run_id} is ${r.state}: its import token no longer works`);return r}if(r===void 0||r.person!==b(t))throw new o(404,"import_not_found",`no import run ${e}`);return r}a(_,"ownRun");function U(t,n,e){const r=t.principal;if(r.class!=="import")return b(t);if(r.run!==null)throw new o(403,"permission_error","an import token reaches only its own run");const s=n.person;if(typeof s!="string"||!/^user:.+/.test(s))throw new o(400,"invalid_body","person (user:<id>) is required");if(e!=="hosted")throw new o(400,"invalid_body","the service credential starts hosted runs only: source_kind must be hosted");return s}a(U,"startPerson");const S=a(t=>{if(t===null||typeof t!="object"||Array.isArray(t))throw new o(400,"invalid_body","the body must be a JSON object");return t},"objectBody");async function L(t){const n=S(t.body),e=U(t,n,n.source_kind),r=n.source_install_id,s=n.source_kind,d=n.release;if(typeof r!="string"||r===""||s!=="machine"&&s!=="hosted"||typeof d!="string")throw new o(400,"invalid_body","source_install_id, source_kind (machine | hosted) and release are required");if(d!==y())throw new o(409,"release_mismatch",`the source runs ${d}, the control plane accepts ${y()}`);const p=await m();let i=await p.prepare("SELECT run_id, state FROM import_runs WHERE source_install_id = ? AND state IN ('uploading', 'loading', 'committed', 'done')").get(r);if(i?.state==="uploading"){const k={step:"uploading",code:"superseded",message:`a new import run was started for install ${r} before this one asked for the load`};await p.prepare("UPDATE import_runs SET state = 'failed', failure_json = ?, finished_at = ? WHERE run_id = ? AND state = 'uploading'").run(JSON.stringify(k),new Date().toISOString(),i.run_id);const h=i.run_id;c().then(w=>D(w,h,{bestEffort:!0})).catch(w=>R({level:"warn",event:"import.prefix_delete_failed",error:w,fields:{run_id:h}})),i=await p.prepare("SELECT run_id, state FROM import_runs WHERE source_install_id = ? AND state IN ('uploading', 'loading', 'committed', 'done')").get(r)}if(i?.state==="done")throw new o(409,"already_imported",`install ${r} was imported by run ${i.run_id}`);if(i!==void 0)throw new o(409,"import_running",`install ${r} has an open run ${i.run_id} (${i.state})`);const u=`imp_${$(8).toString("hex")}`;await p.prepare("INSERT INTO import_runs (run_id, source_install_id, source_kind, person, release, state, created_at) VALUES (?, ?, ?, ?, ?, 'uploading', ?)").run(u,r,s,e,d,new Date().toISOString());const l=t.principal.class==="import"?I(u):null;return{status:201,body:{run_id:u,upload:{rows_prefix:f.prefix(u),url_template:`POST /v1/imports/${u}/upload-urls {"keys":[...]}`},...l===null?{}:{import_token:l}}}}a(L,"handleStart");async function W(t){const n=await _(t);if(n.state!=="uploading")throw new o(409,"import_not_uploading",`import run ${n.run_id} is ${n.state}`);const e=S(t.body).keys;if(!Array.isArray(e)||e.some(i=>typeof i!="string"))throw new o(400,"invalid_body","keys must be a list of blob keys");const r=f.prefix(n.run_id);for(const i of e)if(!(i.startsWith(r)&&!i.startsWith(`${r}stores/`)&&i!==f.manifest(n.run_id))||i.split("/").some(l=>l===""||l==="."||l===".."))throw new o(400,"invalid_key",`${i} is not a key this run may upload`);const s=T();if(s===null)throw new o(501,"upload_not_supported","this control plane keeps blobs in a directory: there is no URL to upload to");const d={};for(const i of e)d[i]=O(s,i,g);return{status:200,body:{urls:d,expires_at:new Date(Date.now()+g*1e3).toISOString()}}}a(W,"handleUploadUrls");async function C(t){const n=await _(t);if(n.state!=="uploading")throw new o(409,"import_not_uploading",`import run ${n.run_id} is ${n.state}`);const e=t.params.name;if(!P.includes(e))throw new o(404,"unknown_store",`no file store ${e}`);try{await N(await c(),n.run_id,e,t.body)}catch(r){throw r instanceof A?new o(400,"invalid_body",r.message):r}return{status:204}}a(C,"handleStore");async function F(t){const n=await _(t),e=await m();if(n.state==="uploading"){if(await(await c()).get(f.source(n.run_id))===null)throw new o(409,"bundle_invalid","source.json is not uploaded");await e.prepare("UPDATE import_runs SET state = 'loading', step = 'queued', loaded_at = ? WHERE run_id = ? AND state = 'uploading'").run(new Date().toISOString(),n.run_id)}else if(n.state!=="loading"&&n.state!=="committed"&&n.state!=="done")throw new o(409,"import_not_uploading",`import run ${n.run_id} is ${n.state}`);return{status:202,body:await E(n.run_id)}}a(F,"handleLoad");async function E(t){const n=await(await m()).prepare("SELECT * FROM import_runs WHERE run_id = ?").get(t),e=n.counts_json===null?{}:JSON.parse(n.counts_json),{unresolved_plugins:r,refused_plugins:s,...d}=e;return{run_id:n.run_id,source_install_id:n.source_install_id,source_kind:n.source_kind,person:n.person,release:n.release,state:n.state,step:n.step,counts:d,verify:n.verify_json===null?null:JSON.parse(n.verify_json),failure:n.failure_json===null?null:JSON.parse(n.failure_json),unresolved_plugins:r??[],refused_plugins:s??[],created:n.created_at,loaded:n.loaded_at,finished:n.finished_at}}a(E,"runDto");async function H(t){const n=await _(t,!0);return{status:200,body:await E(n.run_id)}}a(H,"handleGet");async function J(t){const n=await _(t),e=await j(n.run_id,await c());if(!e.rolled_back)throw new o(409,"rollback_has_dependents",`later rows depend on import run ${n.run_id}`,e.dependents);return{status:200,body:e}}a(J,"handleRollback");const rn=[{method:"POST",pattern:"/v1/imports",handler:L},{method:"POST",pattern:"/v1/imports/:run_id/upload-urls",handler:W},{method:"PUT",pattern:"/v1/imports/:run_id/stores/:name",handler:C},{method:"POST",pattern:"/v1/imports/:run_id/load",handler:F},{method:"GET",pattern:"/v1/imports/:run_id",handler:H},{method:"POST",pattern:"/v1/imports/:run_id/rollback",handler:J}];export{rn as importRoutes};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@north-light/cli",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.636",
|
|
4
4
|
"description": "crtr — agent runtime with memory, plugins, and marketplaces",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -145,7 +145,7 @@
|
|
|
145
145
|
},
|
|
146
146
|
"license": "UNLICENSED",
|
|
147
147
|
"dependencies": {
|
|
148
|
-
"@north-light/identity": "0.3.
|
|
148
|
+
"@north-light/identity": "0.3.636",
|
|
149
149
|
"@earendil-works/chord": "1.1.0",
|
|
150
150
|
"@earendil-works/pi-agent-core": "1.1.0",
|
|
151
151
|
"@earendil-works/pi-ai": "1.1.0",
|
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.636",
|
|
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.636",
|
|
10
10
|
"hasInstallScript": true,
|
|
11
11
|
"license": "UNLICENSED",
|
|
12
12
|
"workspaces": [
|
|
@@ -28,7 +28,7 @@
|
|
|
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.
|
|
31
|
+
"@north-light/identity": "0.3.636",
|
|
32
32
|
"cron-parser": "^5.6.0",
|
|
33
33
|
"esbuild": "^0.27.7",
|
|
34
34
|
"jose": "^6.2.12",
|
|
@@ -11956,17 +11956,17 @@
|
|
|
11956
11956
|
},
|
|
11957
11957
|
"packages/crouter-api": {
|
|
11958
11958
|
"name": "@north-light/api",
|
|
11959
|
-
"version": "0.3.
|
|
11959
|
+
"version": "0.3.636",
|
|
11960
11960
|
"license": "UNLICENSED"
|
|
11961
11961
|
},
|
|
11962
11962
|
"packages/crouter-env-docker": {
|
|
11963
11963
|
"name": "@north-light/env-docker",
|
|
11964
|
-
"version": "0.3.
|
|
11964
|
+
"version": "0.3.636",
|
|
11965
11965
|
"license": "UNLICENSED"
|
|
11966
11966
|
},
|
|
11967
11967
|
"packages/crouter-identity": {
|
|
11968
11968
|
"name": "@north-light/identity",
|
|
11969
|
-
"version": "0.3.
|
|
11969
|
+
"version": "0.3.636",
|
|
11970
11970
|
"license": "UNLICENSED",
|
|
11971
11971
|
"dependencies": {
|
|
11972
11972
|
"jose": "^6.2.1"
|
|
@@ -11974,21 +11974,21 @@
|
|
|
11974
11974
|
},
|
|
11975
11975
|
"packages/crouter-plugin": {
|
|
11976
11976
|
"name": "@north-light/plugin",
|
|
11977
|
-
"version": "0.3.
|
|
11977
|
+
"version": "0.3.636",
|
|
11978
11978
|
"license": "UNLICENSED",
|
|
11979
11979
|
"dependencies": {
|
|
11980
|
-
"@north-light/api": "0.3.
|
|
11981
|
-
"@north-light/identity": "0.3.
|
|
11980
|
+
"@north-light/api": "0.3.636",
|
|
11981
|
+
"@north-light/identity": "0.3.636"
|
|
11982
11982
|
}
|
|
11983
11983
|
},
|
|
11984
11984
|
"packages/crouter-sdk": {
|
|
11985
11985
|
"name": "@north-light/sdk",
|
|
11986
|
-
"version": "0.3.
|
|
11986
|
+
"version": "0.3.636",
|
|
11987
11987
|
"license": "UNLICENSED",
|
|
11988
11988
|
"dependencies": {
|
|
11989
|
-
"@north-light/api": "0.3.
|
|
11990
|
-
"@north-light/identity": "0.3.
|
|
11991
|
-
"@north-light/plugin": "0.3.
|
|
11989
|
+
"@north-light/api": "0.3.636",
|
|
11990
|
+
"@north-light/identity": "0.3.636",
|
|
11991
|
+
"@north-light/plugin": "0.3.636",
|
|
11992
11992
|
"jose": "^6.2.1"
|
|
11993
11993
|
},
|
|
11994
11994
|
"bin": {
|