@lotics/cli 0.175.0 → 0.177.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/dist/src/cli.js CHANGED
@@ -44183,6 +44183,7 @@ function transportErrorMessage(status, parsed) {
44183
44183
  }
44184
44184
 
44185
44185
  // src/client.ts
44186
+ import crypto from "node:crypto";
44186
44187
  import fs2 from "node:fs";
44187
44188
  import path2 from "node:path";
44188
44189
 
@@ -44321,6 +44322,9 @@ async function fetchOfficialStarters() {
44321
44322
  return await response.json();
44322
44323
  }
44323
44324
  var SHELF_FETCH_TIMEOUT_MS = 1e4;
44325
+ function newRequestId() {
44326
+ return crypto.randomUUID().replace(/-/g, "").slice(0, 12);
44327
+ }
44324
44328
  var LoticsClient = class {
44325
44329
  apiKey;
44326
44330
  workspaceId;
@@ -44337,7 +44341,18 @@ var LoticsClient = class {
44337
44341
  this.viewAsMemberId = options.viewAsMemberId;
44338
44342
  this.baseUrl = API_BASE_URL;
44339
44343
  }
44340
- async throwResponseError(response) {
44344
+ /**
44345
+ * The id is appended to the MESSAGE rather than carried on a field, because
44346
+ * the only thing that reliably reaches a person is what got printed: the CLI's
44347
+ * top-level handler writes `error.message` to stderr, and the telemetry tail
44348
+ * captures the same bytes. A field would have to be read by every one of ~60
44349
+ * exit sites to be worth anything.
44350
+ *
44351
+ * `requestId` is optional because one caller legitimately has none — a
44352
+ * presigned CDN download is not a request of ours and appears in none of our
44353
+ * logs. Every call against the API passes the id its own headers carry.
44354
+ */
44355
+ async throwResponseError(response, requestId) {
44341
44356
  const text = await response.text();
44342
44357
  let message2;
44343
44358
  try {
@@ -44346,15 +44361,23 @@ var LoticsClient = class {
44346
44361
  } catch {
44347
44362
  message2 = text;
44348
44363
  }
44349
- throw new Error(`${response.status}: ${message2}`);
44364
+ const trace = requestId === void 0 ? "" : `
44365
+
44366
+ Request id: ${requestId} \u2014 quote this to Lotics support.`;
44367
+ throw new Error(`${response.status}: ${message2}${trace}`);
44350
44368
  }
44351
44369
  /**
44352
- * The backend's `log()` middleware registers `user-agent` and
44353
- * `x-posthog-session-id` onto the per-request Logger, so they ride EVERY log
44354
- * line that request emits — the validation 400, the tool error, the timing.
44355
- * Sending them is therefore the whole of the correlation work: it turns an
44356
- * anonymous API-key request into "`app workflow set`, from cli 0.117.0, the
44357
- * fourth command of this session".
44370
+ * The backend's `log()` middleware registers `user-agent`,
44371
+ * `x-posthog-session-id` and `x-request-id` onto the per-request Logger, so
44372
+ * they ride EVERY log line that request emits — the validation 400, the tool
44373
+ * error, the timing. Sending them is therefore the whole of the correlation
44374
+ * work: it turns an anonymous API-key request into "`app workflow set`, from
44375
+ * cli 0.117.0, the fourth command of this session".
44376
+ *
44377
+ * The request id is minted HERE, with the other headers, so it cannot reach
44378
+ * some paths and not others: several commands build their own transport around
44379
+ * these headers, and an id threaded only through `request` would leave
44380
+ * `app deploy`, `uploadFiles` and every workflow call unfindable.
44358
44381
  */
44359
44382
  buildHeaders() {
44360
44383
  const invocation2 = getInvocation();
@@ -44376,6 +44399,7 @@ var LoticsClient = class {
44376
44399
  headers["x-posthog-session-id"] = invocation2.session;
44377
44400
  }
44378
44401
  }
44402
+ headers["x-request-id"] = newRequestId();
44379
44403
  return headers;
44380
44404
  }
44381
44405
  async request(method, path13, body) {
@@ -44387,7 +44411,7 @@ var LoticsClient = class {
44387
44411
  init.body = JSON.stringify(body);
44388
44412
  }
44389
44413
  const response = await fetch(url2, init);
44390
- if (!response.ok) await this.throwResponseError(response);
44414
+ if (!response.ok) await this.throwResponseError(response, headers["x-request-id"]);
44391
44415
  return response.json();
44392
44416
  }
44393
44417
  async whoami() {
@@ -44438,13 +44462,14 @@ var LoticsClient = class {
44438
44462
  const timeout = setTimeout(() => controller.abort(), options.timeoutMs);
44439
44463
  try {
44440
44464
  const url2 = `${this.baseUrl}/v1/tools/execute`;
44465
+ const headers = { ...this.buildHeaders(), "Content-Type": "application/json" };
44441
44466
  const response = await fetch(url2, {
44442
44467
  method: "POST",
44443
- headers: { ...this.buildHeaders(), "Content-Type": "application/json" },
44468
+ headers,
44444
44469
  body: JSON.stringify(body),
44445
44470
  signal: controller.signal
44446
44471
  });
44447
- if (!response.ok) await this.throwResponseError(response);
44472
+ if (!response.ok) await this.throwResponseError(response, headers["x-request-id"]);
44448
44473
  return response.json();
44449
44474
  } catch (error52) {
44450
44475
  if (error52 instanceof Error && error52.name === "AbortError") {
@@ -44862,16 +44887,12 @@ var LoticsClient = class {
44862
44887
  * iframe. Mirrors POST /v1/apps/{app_id}/agents/{alias}/runs.
44863
44888
  */
44864
44889
  async appAgentRunStream(app_id, alias, body, signal) {
44890
+ const headers = { ...this.buildHeaders(), "Content-Type": "application/json" };
44865
44891
  const res = await fetch(
44866
44892
  `${this.baseUrl}/v1/apps/${encodeURIComponent(app_id)}/agents/${encodeURIComponent(alias)}/runs`,
44867
- {
44868
- method: "POST",
44869
- headers: { ...this.buildHeaders(), "Content-Type": "application/json" },
44870
- body: JSON.stringify(body),
44871
- signal
44872
- }
44893
+ { method: "POST", headers, body: JSON.stringify(body), signal }
44873
44894
  );
44874
- if (!res.ok) await this.throwResponseError(res);
44895
+ if (!res.ok) await this.throwResponseError(res, headers["x-request-id"]);
44875
44896
  return res;
44876
44897
  }
44877
44898
  /**
@@ -44881,16 +44902,12 @@ var LoticsClient = class {
44881
44902
  * POST /v1/apps/{app_id}/agent-runs/{run_id}/continue.
44882
44903
  */
44883
44904
  async appAgentRunContinueStream(app_id, run_id, body, signal) {
44905
+ const headers = { ...this.buildHeaders(), "Content-Type": "application/json" };
44884
44906
  const res = await fetch(
44885
44907
  `${this.baseUrl}/v1/apps/${encodeURIComponent(app_id)}/agent-runs/${encodeURIComponent(run_id)}/continue`,
44886
- {
44887
- method: "POST",
44888
- headers: { ...this.buildHeaders(), "Content-Type": "application/json" },
44889
- body: JSON.stringify(body),
44890
- signal
44891
- }
44908
+ { method: "POST", headers, body: JSON.stringify(body), signal }
44892
44909
  );
44893
- if (!res.ok) await this.throwResponseError(res);
44910
+ if (!res.ok) await this.throwResponseError(res, headers["x-request-id"]);
44894
44911
  return res;
44895
44912
  }
44896
44913
  /**
@@ -45009,12 +45026,8 @@ var LoticsClient = class {
45009
45026
  formData.append("agent_aliases", JSON.stringify(args.agent_aliases ?? []));
45010
45027
  formData.append("query_aliases", JSON.stringify(args.query_aliases ?? []));
45011
45028
  const url2 = `${this.baseUrl}/v1/apps/${encodeURIComponent(args.app_id)}/versions`;
45012
- const response = await fetch(url2, {
45013
- method: "POST",
45014
- headers: this.buildHeaders(),
45015
- // no Content-Type — fetch sets multipart boundary
45016
- body: formData
45017
- });
45029
+ const headers = this.buildHeaders();
45030
+ const response = await fetch(url2, { method: "POST", headers, body: formData });
45018
45031
  if (response.status === 409) {
45019
45032
  const conflict = await response.json();
45020
45033
  const err2 = new Error(conflict.message ?? "Deploy conflict \u2014 pull required");
@@ -45023,7 +45036,7 @@ var LoticsClient = class {
45023
45036
  throw err2;
45024
45037
  }
45025
45038
  if (!response.ok) {
45026
- await this.throwResponseError(response);
45039
+ await this.throwResponseError(response, headers["x-request-id"]);
45027
45040
  }
45028
45041
  return response.json();
45029
45042
  }
@@ -45039,11 +45052,14 @@ var LoticsClient = class {
45039
45052
  return absolutePath;
45040
45053
  }
45041
45054
  async downloadFileById(fileId, outputDir, options) {
45055
+ const signedHeaders = this.buildHeaders();
45042
45056
  const signedUrlRes = await fetch(
45043
45057
  `${this.baseUrl}/v1/files/${encodeURIComponent(fileId)}/signed_url`,
45044
- { headers: this.buildHeaders() }
45058
+ { headers: signedHeaders }
45045
45059
  );
45046
- if (!signedUrlRes.ok) await this.throwResponseError(signedUrlRes);
45060
+ if (!signedUrlRes.ok) {
45061
+ await this.throwResponseError(signedUrlRes, signedHeaders["x-request-id"]);
45062
+ }
45047
45063
  const { url: url2 } = await signedUrlRes.json();
45048
45064
  const response = await fetch(url2);
45049
45065
  if (!response.ok) await this.throwResponseError(response);
@@ -45103,12 +45119,9 @@ var LoticsClient = class {
45103
45119
  formData.append("file", new Blob([item.bytes], { type: mimeType }), item.filename);
45104
45120
  }
45105
45121
  const url2 = `${this.baseUrl}/v1/files`;
45106
- const response = await fetch(url2, {
45107
- method: "POST",
45108
- headers: this.buildHeaders(),
45109
- body: formData
45110
- });
45111
- if (!response.ok) await this.throwResponseError(response);
45122
+ const headers = this.buildHeaders();
45123
+ const response = await fetch(url2, { method: "POST", headers, body: formData });
45124
+ if (!response.ok) await this.throwResponseError(response, headers["x-request-id"]);
45112
45125
  return response.json();
45113
45126
  }
45114
45127
  };
@@ -45200,7 +45213,7 @@ async function resolveUploadSource(sourceFlag, flags) {
45200
45213
  }
45201
45214
 
45202
45215
  // src/telemetry.ts
45203
- import crypto from "node:crypto";
45216
+ import crypto2 from "node:crypto";
45204
45217
  import fs3 from "node:fs";
45205
45218
  import os2 from "node:os";
45206
45219
  import path3 from "node:path";
@@ -45276,7 +45289,7 @@ function buildEvent(exitCode, durationMs, now) {
45276
45289
  if (invocation2 === null || invocation2.session === null) return null;
45277
45290
  const message2 = exitCode === 0 ? "" : stripNarration(stderrTail);
45278
45291
  return {
45279
- event_id: crypto.randomUUID(),
45292
+ event_id: crypto2.randomUUID(),
45280
45293
  ts: now.toISOString(),
45281
45294
  cli_session_id: invocation2.session,
45282
45295
  cli_command: invocation2.command,
@@ -45343,8 +45356,8 @@ function flushSpool(post, minEvents = 5) {
45343
45356
  const batch = events.slice(0, MAX_FLUSH_EVENTS);
45344
45357
  const sent = `${lines.slice(0, batch.length).join("\n")}
45345
45358
  `;
45346
- void post(batch).then(() => {
45347
- clearSpool(sent);
45359
+ void post(batch).then((outcome) => {
45360
+ if (outcome !== "retry") clearSpool(sent);
45348
45361
  }).catch(() => {
45349
45362
  });
45350
45363
  }
@@ -45358,7 +45371,9 @@ async function postEvents(baseUrl, apiKey, events, timeoutMs = 5e3) {
45358
45371
  body: JSON.stringify({ events }),
45359
45372
  signal: controller.signal
45360
45373
  });
45361
- if (!res.ok) throw new Error(`telemetry flush: ${res.status}`);
45374
+ if (res.ok) return "delivered";
45375
+ const permanent = res.status >= 400 && res.status < 500 && res.status !== 408 && res.status !== 429;
45376
+ return permanent ? "rejected" : "retry";
45362
45377
  } finally {
45363
45378
  clearTimeout(timer2);
45364
45379
  }
@@ -45696,7 +45711,7 @@ function resultSideEffects(result) {
45696
45711
  }
45697
45712
 
45698
45713
  // src/version.ts
45699
- var VERSION = "0.175.0";
45714
+ var VERSION = "0.177.0";
45700
45715
 
45701
45716
  // src/timezone.ts
45702
45717
  function machineTimezone() {
@@ -48192,6 +48207,22 @@ ${inputLines.join("\n")}
48192
48207
  }
48193
48208
  `;
48194
48209
  }
48210
+ var LINK_TSCONFIG_PATH = ".lotics/tsconfig.link.json";
48211
+ function generateLinkTsconfig(paths) {
48212
+ const header = Object.keys(paths).length > 0 ? `// GENERATED by \`lotics app codegen\` \u2014 do not edit.
48213
+ // LOTICS_UI_SRC is set, so @lotics/ui resolves to your working copy for tsc,
48214
+ // vitest, eslint and your editor \u2014 the same copy Vite is bundling. The peer
48215
+ // pins keep ONE react / react-native in the program; without them the kit's
48216
+ // source resolves its own copies and every shared type stops matching.
48217
+ // Unset LOTICS_UI_SRC and re-run to go back to the published kit.
48218
+ ` : `// GENERATED by \`lotics app codegen\` \u2014 do not edit.
48219
+ // @lotics/ui resolves from node_modules as normal, so this is inert. It still
48220
+ // has to exist: tsconfig.json extends it, and a missing extends target fails
48221
+ // the build outright.
48222
+ `;
48223
+ return `${header}${JSON.stringify({ compilerOptions: { paths } }, null, 2)}
48224
+ `;
48225
+ }
48195
48226
  var CAPABILITY_GATED_CALLS = {
48196
48227
  comments: ["useComments", "createComment", "updateComment", "deleteComment"]
48197
48228
  };
@@ -71905,18 +71936,17 @@ function ensureAppTsconfig(projectDir) {
71905
71936
  }
71906
71937
  const existingExtends = parsed.extends;
71907
71938
  if (existingExtends === void 0) {
71908
- parsed.extends = `./${LINK_TSCONFIG}`;
71909
- changes.push(`added "extends": "./${LINK_TSCONFIG}" (so tsc resolves the same @lotics/ui as Vite)`);
71910
- } else if (existingExtends !== `./${LINK_TSCONFIG}`) {
71939
+ parsed.extends = `./${LINK_TSCONFIG_PATH}`;
71940
+ changes.push(`added "extends": "./${LINK_TSCONFIG_PATH}" (so tsc resolves the same @lotics/ui as Vite)`);
71941
+ } else if (existingExtends !== `./${LINK_TSCONFIG_PATH}`) {
71911
71942
  console.error(
71912
- `\u26A0 tsconfig.json already extends ${JSON.stringify(existingExtends)}, so the dev-link config was not added \u2014 \`tsc\` will keep resolving @lotics/ui from node_modules even under LOTICS_UI_SRC. Add "./${LINK_TSCONFIG}" to "extends" (it accepts an array) to fix it.`
71943
+ `\u26A0 tsconfig.json already extends ${JSON.stringify(existingExtends)}, so the dev-link config was not added \u2014 \`tsc\` will keep resolving @lotics/ui from node_modules even under LOTICS_UI_SRC. Add "./${LINK_TSCONFIG_PATH}" to "extends" (it accepts an array) to fix it.`
71913
71944
  );
71914
71945
  }
71915
71946
  if (changes.length === 0) return;
71916
71947
  fs8.writeFileSync(tsconfigPath, JSON.stringify(parsed, null, 2) + "\n");
71917
71948
  console.error(`Patched tsconfig.json: ${changes.join("; ")}.`);
71918
71949
  }
71919
- var LINK_TSCONFIG = ".lotics/tsconfig.link.json";
71920
71950
  function writeDevLinkTsconfig(projectDir) {
71921
71951
  const uiSrc = process.env.LOTICS_UI_SRC;
71922
71952
  const paths = {};
@@ -71931,19 +71961,8 @@ function writeDevLinkTsconfig(projectDir) {
71931
71961
  paths[`${peer}/*`] = [`${target}/*`];
71932
71962
  }
71933
71963
  }
71934
- const file2 = path9.join(projectDir, ".lotics", "tsconfig.link.json");
71935
- const header = uiSrc ? `// GENERATED by \`lotics app codegen\` \u2014 do not edit.
71936
- // LOTICS_UI_SRC is set, so @lotics/ui resolves to your working copy for tsc,
71937
- // vitest, eslint and your editor \u2014 the same copy Vite is bundling. The peer
71938
- // pins keep ONE react / react-native in the program; without them the kit's
71939
- // source resolves its own copies and every shared type stops matching.
71940
- // Unset LOTICS_UI_SRC and re-run to go back to the published kit.
71941
- ` : `// GENERATED by \`lotics app codegen\` \u2014 do not edit.
71942
- // LOTICS_UI_SRC is not set, so this is inert and @lotics/ui resolves from
71943
- // node_modules as normal.
71944
- `;
71945
- fs8.writeFileSync(file2, `${header}${JSON.stringify({ compilerOptions: { paths } }, null, 2)}
71946
- `);
71964
+ const file2 = path9.join(projectDir, LINK_TSCONFIG_PATH);
71965
+ fs8.writeFileSync(file2, generateLinkTsconfig(paths));
71947
71966
  return file2;
71948
71967
  }
71949
71968
  function writeKitTypeAugmentation(projectDir) {
@@ -72475,6 +72494,8 @@ async function hydrateAppProject(client, targetPath, app, opts) {
72475
72494
  }
72476
72495
  reportUnseenLive("prompt", unseenLiveProse, app.id);
72477
72496
  }
72497
+ }
72498
+ async function prepareProjectToolchain(targetPath) {
72478
72499
  await runNpmInstall(targetPath);
72479
72500
  writeAppDtsKitLinks(targetPath);
72480
72501
  }
@@ -72521,6 +72542,7 @@ async function appPull(client, args) {
72521
72542
  ...args.force !== void 0 ? { force: args.force } : {},
72522
72543
  kept: keptLocal
72523
72544
  });
72545
+ await prepareProjectToolchain(targetPath);
72524
72546
  reportRestoredFiles(restoredFromArchive, prior !== null);
72525
72547
  reportKeptLocalFiles(keptLocal, app.id, {
72526
72548
  // `prior` is non-null whenever the stamp was held — `stampAfterPull` cannot
@@ -74125,11 +74147,21 @@ Building ${starter.name} \u2014 this runs its build on your machine (its package
74125
74147
  } else if (serverDeployed === null) {
74126
74148
  note(`Building and deploying\u2026`);
74127
74149
  }
74150
+ if (serverDeployed === null && result.build_error) {
74151
+ warn(
74152
+ `
74153
+ Lotics could not build ${starter.name} \u2014 building here instead.
74154
+ ${result.build_error.split("\n")[0]}
74155
+ The copy is fine: the tables, the records and the app all landed. Do NOT
74156
+ copy again, and do not write a replacement app \u2014 only the build is missing.`
74157
+ );
74158
+ }
74128
74159
  const resumeDir = path10.relative(process.cwd(), targetPath) || ".";
74129
74160
  try {
74130
74161
  if (serverDeployed !== null) {
74131
74162
  note(`Deployed v${serverDeployed.version_number} \u2014 built on Lotics, no Node needed here.`);
74132
74163
  } else {
74164
+ await prepareProjectToolchain(targetPath);
74133
74165
  await appDeploy(client, {
74134
74166
  projectDir: targetPath,
74135
74167
  message: `Copied from starter ${starter.name}`
@@ -163,12 +163,17 @@ export declare class LoticsClient {
163
163
  constructor(options: LoticsClientOptions);
164
164
  private throwResponseError;
165
165
  /**
166
- * The backend's `log()` middleware registers `user-agent` and
167
- * `x-posthog-session-id` onto the per-request Logger, so they ride EVERY log
168
- * line that request emits — the validation 400, the tool error, the timing.
169
- * Sending them is therefore the whole of the correlation work: it turns an
170
- * anonymous API-key request into "`app workflow set`, from cli 0.117.0, the
171
- * fourth command of this session".
166
+ * The backend's `log()` middleware registers `user-agent`,
167
+ * `x-posthog-session-id` and `x-request-id` onto the per-request Logger, so
168
+ * they ride EVERY log line that request emits — the validation 400, the tool
169
+ * error, the timing. Sending them is therefore the whole of the correlation
170
+ * work: it turns an anonymous API-key request into "`app workflow set`, from
171
+ * cli 0.117.0, the fourth command of this session".
172
+ *
173
+ * The request id is minted HERE, with the other headers, so it cannot reach
174
+ * some paths and not others: several commands build their own transport around
175
+ * these headers, and an id threaded only through `request` would leave
176
+ * `app deploy`, `uploadFiles` and every workflow call unfindable.
172
177
  */
173
178
  private buildHeaders;
174
179
  private request;
@@ -422,6 +427,11 @@ export declare class LoticsClient {
422
427
  version_id: string;
423
428
  version_number: number;
424
429
  } | null;
430
+ /**
431
+ * Why the server built nothing, when it was asked to and `deployed` is null.
432
+ * Absent from an older server's response for the same reason as `deployed`.
433
+ */
434
+ build_error?: string | null;
425
435
  binding: Record<string, Record<string, string>>;
426
436
  sample_record_ids: Record<string, string[]>;
427
437
  knowledge_warnings: {
@@ -1,4 +1,5 @@
1
1
  import { transportErrorMessage } from "@lotics/shared/transport_error";
2
+ import crypto from "node:crypto";
2
3
  import fs from "node:fs";
3
4
  import path from "node:path";
4
5
  import { getInvocation } from "./invocation.js";
@@ -115,6 +116,18 @@ export async function fetchOfficialStarters() {
115
116
  }
116
117
  /** Long enough for a slow link, short enough that a dead one still says so. */
117
118
  const SHELF_FETCH_TIMEOUT_MS = 10_000;
119
+ /**
120
+ * A short id for one request, short because a person has to read it back to us.
121
+ *
122
+ * Not `generateLocalId` from `@lotics/shared`: this file is the UNBUNDLED
123
+ * library entry, so every bare specifier it imports has to resolve from a
124
+ * package whose `dependencies` are empty. `node:crypto` is a builtin and costs
125
+ * nothing, and the server treats the header as opaque — it logs whatever
126
+ * arrives, so matching its id ALPHABET buys nothing.
127
+ */
128
+ function newRequestId() {
129
+ return crypto.randomUUID().replace(/-/g, "").slice(0, 12);
130
+ }
118
131
  export class LoticsClient {
119
132
  apiKey;
120
133
  workspaceId;
@@ -131,7 +144,18 @@ export class LoticsClient {
131
144
  this.viewAsMemberId = options.viewAsMemberId;
132
145
  this.baseUrl = API_BASE_URL;
133
146
  }
134
- async throwResponseError(response) {
147
+ /**
148
+ * The id is appended to the MESSAGE rather than carried on a field, because
149
+ * the only thing that reliably reaches a person is what got printed: the CLI's
150
+ * top-level handler writes `error.message` to stderr, and the telemetry tail
151
+ * captures the same bytes. A field would have to be read by every one of ~60
152
+ * exit sites to be worth anything.
153
+ *
154
+ * `requestId` is optional because one caller legitimately has none — a
155
+ * presigned CDN download is not a request of ours and appears in none of our
156
+ * logs. Every call against the API passes the id its own headers carry.
157
+ */
158
+ async throwResponseError(response, requestId) {
135
159
  const text = await response.text();
136
160
  let message;
137
161
  try {
@@ -141,15 +165,21 @@ export class LoticsClient {
141
165
  catch {
142
166
  message = text;
143
167
  }
144
- throw new Error(`${response.status}: ${message}`);
168
+ const trace = requestId === undefined ? "" : `\n\n Request id: ${requestId} — quote this to Lotics support.`;
169
+ throw new Error(`${response.status}: ${message}${trace}`);
145
170
  }
146
171
  /**
147
- * The backend's `log()` middleware registers `user-agent` and
148
- * `x-posthog-session-id` onto the per-request Logger, so they ride EVERY log
149
- * line that request emits — the validation 400, the tool error, the timing.
150
- * Sending them is therefore the whole of the correlation work: it turns an
151
- * anonymous API-key request into "`app workflow set`, from cli 0.117.0, the
152
- * fourth command of this session".
172
+ * The backend's `log()` middleware registers `user-agent`,
173
+ * `x-posthog-session-id` and `x-request-id` onto the per-request Logger, so
174
+ * they ride EVERY log line that request emits — the validation 400, the tool
175
+ * error, the timing. Sending them is therefore the whole of the correlation
176
+ * work: it turns an anonymous API-key request into "`app workflow set`, from
177
+ * cli 0.117.0, the fourth command of this session".
178
+ *
179
+ * The request id is minted HERE, with the other headers, so it cannot reach
180
+ * some paths and not others: several commands build their own transport around
181
+ * these headers, and an id threaded only through `request` would leave
182
+ * `app deploy`, `uploadFiles` and every workflow call unfindable.
153
183
  */
154
184
  buildHeaders() {
155
185
  const invocation = getInvocation();
@@ -171,6 +201,9 @@ export class LoticsClient {
171
201
  headers["x-posthog-session-id"] = invocation.session;
172
202
  }
173
203
  }
204
+ // Unconditional, unlike the session header: this describes the request
205
+ // rather than tracking anyone, and it is what makes a failure findable.
206
+ headers["x-request-id"] = newRequestId();
174
207
  return headers;
175
208
  }
176
209
  async request(method, path, body) {
@@ -183,7 +216,7 @@ export class LoticsClient {
183
216
  }
184
217
  const response = await fetch(url, init);
185
218
  if (!response.ok)
186
- await this.throwResponseError(response);
219
+ await this.throwResponseError(response, headers["x-request-id"]);
187
220
  return response.json();
188
221
  }
189
222
  async whoami() {
@@ -234,14 +267,15 @@ export class LoticsClient {
234
267
  const timeout = setTimeout(() => controller.abort(), options.timeoutMs);
235
268
  try {
236
269
  const url = `${this.baseUrl}/v1/tools/execute`;
270
+ const headers = { ...this.buildHeaders(), "Content-Type": "application/json" };
237
271
  const response = await fetch(url, {
238
272
  method: "POST",
239
- headers: { ...this.buildHeaders(), "Content-Type": "application/json" },
273
+ headers,
240
274
  body: JSON.stringify(body),
241
275
  signal: controller.signal,
242
276
  });
243
277
  if (!response.ok)
244
- await this.throwResponseError(response);
278
+ await this.throwResponseError(response, headers["x-request-id"]);
245
279
  return response.json();
246
280
  }
247
281
  catch (error) {
@@ -662,14 +696,10 @@ export class LoticsClient {
662
696
  * iframe. Mirrors POST /v1/apps/{app_id}/agents/{alias}/runs.
663
697
  */
664
698
  async appAgentRunStream(app_id, alias, body, signal) {
665
- const res = await fetch(`${this.baseUrl}/v1/apps/${encodeURIComponent(app_id)}/agents/${encodeURIComponent(alias)}/runs`, {
666
- method: "POST",
667
- headers: { ...this.buildHeaders(), "Content-Type": "application/json" },
668
- body: JSON.stringify(body),
669
- signal,
670
- });
699
+ const headers = { ...this.buildHeaders(), "Content-Type": "application/json" };
700
+ const res = await fetch(`${this.baseUrl}/v1/apps/${encodeURIComponent(app_id)}/agents/${encodeURIComponent(alias)}/runs`, { method: "POST", headers, body: JSON.stringify(body), signal });
671
701
  if (!res.ok)
672
- await this.throwResponseError(res);
702
+ await this.throwResponseError(res, headers["x-request-id"]);
673
703
  return res;
674
704
  }
675
705
  /**
@@ -679,14 +709,10 @@ export class LoticsClient {
679
709
  * POST /v1/apps/{app_id}/agent-runs/{run_id}/continue.
680
710
  */
681
711
  async appAgentRunContinueStream(app_id, run_id, body, signal) {
682
- const res = await fetch(`${this.baseUrl}/v1/apps/${encodeURIComponent(app_id)}/agent-runs/${encodeURIComponent(run_id)}/continue`, {
683
- method: "POST",
684
- headers: { ...this.buildHeaders(), "Content-Type": "application/json" },
685
- body: JSON.stringify(body),
686
- signal,
687
- });
712
+ const headers = { ...this.buildHeaders(), "Content-Type": "application/json" };
713
+ const res = await fetch(`${this.baseUrl}/v1/apps/${encodeURIComponent(app_id)}/agent-runs/${encodeURIComponent(run_id)}/continue`, { method: "POST", headers, body: JSON.stringify(body), signal });
688
714
  if (!res.ok)
689
- await this.throwResponseError(res);
715
+ await this.throwResponseError(res, headers["x-request-id"]);
690
716
  return res;
691
717
  }
692
718
  /**
@@ -775,11 +801,8 @@ export class LoticsClient {
775
801
  formData.append("agent_aliases", JSON.stringify(args.agent_aliases ?? []));
776
802
  formData.append("query_aliases", JSON.stringify(args.query_aliases ?? []));
777
803
  const url = `${this.baseUrl}/v1/apps/${encodeURIComponent(args.app_id)}/versions`;
778
- const response = await fetch(url, {
779
- method: "POST",
780
- headers: this.buildHeaders(), // no Content-Type — fetch sets multipart boundary
781
- body: formData,
782
- });
804
+ const headers = this.buildHeaders(); // no Content-Type — fetch sets multipart boundary
805
+ const response = await fetch(url, { method: "POST", headers, body: formData });
783
806
  if (response.status === 409) {
784
807
  const conflict = (await response.json());
785
808
  const err = new Error(conflict.message ?? "Deploy conflict — pull required");
@@ -789,7 +812,7 @@ export class LoticsClient {
789
812
  throw err;
790
813
  }
791
814
  if (!response.ok) {
792
- await this.throwResponseError(response);
815
+ await this.throwResponseError(response, headers["x-request-id"]);
793
816
  }
794
817
  return response.json();
795
818
  }
@@ -809,9 +832,11 @@ export class LoticsClient {
809
832
  // edge Worker that doesn't accept the API key). The signed_url endpoint
810
833
  // takes the API key and returns a short-lived, directly-fetchable URL
811
834
  // whose Content-Disposition carries the original filename.
812
- const signedUrlRes = await fetch(`${this.baseUrl}/v1/files/${encodeURIComponent(fileId)}/signed_url`, { headers: this.buildHeaders() });
813
- if (!signedUrlRes.ok)
814
- await this.throwResponseError(signedUrlRes);
835
+ const signedHeaders = this.buildHeaders();
836
+ const signedUrlRes = await fetch(`${this.baseUrl}/v1/files/${encodeURIComponent(fileId)}/signed_url`, { headers: signedHeaders });
837
+ if (!signedUrlRes.ok) {
838
+ await this.throwResponseError(signedUrlRes, signedHeaders["x-request-id"]);
839
+ }
815
840
  const { url } = (await signedUrlRes.json());
816
841
  const response = await fetch(url);
817
842
  if (!response.ok)
@@ -890,13 +915,10 @@ export class LoticsClient {
890
915
  formData.append("file", new Blob([item.bytes], { type: mimeType }), item.filename);
891
916
  }
892
917
  const url = `${this.baseUrl}/v1/files`;
893
- const response = await fetch(url, {
894
- method: "POST",
895
- headers: this.buildHeaders(),
896
- body: formData,
897
- });
918
+ const headers = this.buildHeaders();
919
+ const response = await fetch(url, { method: "POST", headers, body: formData });
898
920
  if (!response.ok)
899
- await this.throwResponseError(response);
921
+ await this.throwResponseError(response, headers["x-request-id"]);
900
922
  return response.json();
901
923
  }
902
924
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lotics/cli",
3
- "version": "0.175.0",
3
+ "version": "0.177.0",
4
4
  "description": "Lotics SDK and CLI for AI agents",
5
5
  "type": "module",
6
6
  "bin": {