@tangle-network/sdk-provider-cli-base 0.13.206 → 0.13.207

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.
@@ -71,8 +71,9 @@ interface PrimeTurn extends AcpTurn<PrimeConfig> {
71
71
  * Kernel death closes the session with a typed failure.
72
72
  *
73
73
  * prime hosts one session per connection and has no `session/load`: a new
74
- * process opens a new ACP session, and the conversation resumes from prime's
75
- * own session files in the per-session agent dir.
74
+ * process opens a new ACP session and a new prime-agent session. It reads an
75
+ * earlier session's file only when the caller's flags name one (`--continue`,
76
+ * `--resume`), which prime-agent looks up in its session directory.
76
77
  */
77
78
  declare class PrimeProviderAdapter extends AcpProviderAdapterBase<PrimeConfig, PrimeTurn> {
78
79
  readonly provider = "prime";
@@ -85,6 +86,13 @@ declare class PrimeProviderAdapter extends AcpProviderAdapterBase<PrimeConfig, P
85
86
  sessionId: string;
86
87
  workspaceRoot: string;
87
88
  }): Promise<AcpProcessSpec>;
89
+ /**
90
+ * Where prime-agent keeps this session's own records. An agent dir of the
91
+ * session's own holds them where prime-agent puts them by default. A shared
92
+ * agent dir (continual mode) gives each session a directory inside it, so
93
+ * the model calls one session made are never read as another's.
94
+ */
95
+ private sessionRecordsDir;
88
96
  protected spawnFailureMessage(failure: {
89
97
  message: string;
90
98
  missingBinary: boolean;
@@ -92,9 +100,10 @@ declare class PrimeProviderAdapter extends AcpProviderAdapterBase<PrimeConfig, P
92
100
  /**
93
101
  * prime-agent's `session/prompt` response reports no token usage, so each
94
102
  * response is completed with the usage prime wrote to its session files for
95
- * that prompt (see prompt-usage.ts). The files are read in the agent dir the
96
- * process was started on, which the agent environment can set over the
97
- * spec's.
103
+ * that prompt (see prompt-usage.ts). The files are the session's own: those
104
+ * in the session directory the process was started with, and its subagents'
105
+ * in the agent dir the process was started on, which the agent environment
106
+ * can set over the spec's.
98
107
  */
99
108
  protected spawnBridge(request: AcpBridgeSpawn): RpcBridge;
100
109
  /**
@@ -148,22 +148,31 @@ function materializePrimeModelsConfig(agentDir, input, io = { write: writePrimeA
148
148
  * calls the prompt made.
149
149
  */
150
150
  /**
151
- * Prints, as a JSON array, the usage of every model call the session files of
152
- * an agent dir recorded at or after a time (argv: agent dir, epoch ms).
151
+ * Prints, as a JSON array, the usage of every model call one session's files
152
+ * recorded at or after a time (argv: agent dir, session dir, epoch ms).
153
153
  *
154
- * A root session's file is `sessions/<id>.jsonl`; a subagent's is below
155
- * `session-artifacts/` and its calls are in no root file's message rows, so
156
- * both trees are read and each call is counted once. Rows are appended in
157
- * time order and stamped when appended, so a prompt's rows are a file's tail:
158
- * each file is read backwards and left at the first message row older than
159
- * the time, which keeps a long session's history unread. A file last written
160
- * well before the time is not opened; the margin covers filesystems that
161
- * store modification times to the second.
154
+ * Nothing prime-agent writes names the ACP session a file belongs to, so the
155
+ * adapter starts each process with a session directory of the session's own
156
+ * (`--session-dir`). A root session's file is `<session dir>/<id>.jsonl`, one
157
+ * for each process the session has run. A subagent's file is below
158
+ * `<agent dir>/session-artifacts/<root id>/`, however deep it was spawned,
159
+ * and its calls are in no root file's message rows. So the session's calls
160
+ * are those in its root files and in the trees its root ids name, each
161
+ * counted once; another session's root files, and the trees their ids name,
162
+ * are never opened. prime-agent keeps a root session's other artifacts in
163
+ * `session-artifacts/<root id>/` beside the session directory, which is the
164
+ * same tree only when the session directory is the agent dir's default.
165
+ *
166
+ * Rows are appended in time order and stamped when appended, so a prompt's
167
+ * rows are a file's tail: each file is read backwards and left at the first
168
+ * message row older than the time, which keeps a long session's history
169
+ * unread. A file last written well before the time is not opened; the margin
170
+ * covers filesystems that store modification times to the second.
162
171
  */
163
172
  const MODEL_CALLS_SCRIPT = `
164
173
  const fs = require("node:fs");
165
174
  const path = require("node:path");
166
- const [agentDir, sinceArg] = process.argv.slice(1);
175
+ const [agentDir, sessionDir, sinceArg] = process.argv.slice(1);
167
176
  const since = Number(sinceArg);
168
177
  const CHUNK = 64 * 1024;
169
178
  const MTIME_MARGIN_MS = 60 * 1000;
@@ -210,15 +219,16 @@ function scan(file) {
210
219
  fs.closeSync(fd);
211
220
  }
212
221
  }
213
- function walk(dir, depth) {
214
- let entries;
222
+ function list(dir) {
215
223
  try {
216
- entries = fs.readdirSync(dir, { withFileTypes: true });
224
+ return fs.readdirSync(dir, { withFileTypes: true });
217
225
  } catch (error) {
218
- if (error.code === "ENOENT" || error.code === "ENOTDIR") return;
226
+ if (error.code === "ENOENT" || error.code === "ENOTDIR") return [];
219
227
  throw error;
220
228
  }
221
- for (const entry of entries) {
229
+ }
230
+ function walk(dir, depth) {
231
+ for (const entry of list(dir)) {
222
232
  const file = path.join(dir, entry.name);
223
233
  if (entry.isDirectory()) {
224
234
  if (depth < 16) walk(file, depth + 1);
@@ -227,8 +237,16 @@ function walk(dir, depth) {
227
237
  }
228
238
  }
229
239
  }
230
- walk(path.join(agentDir, "sessions"), 0);
231
- walk(path.join(agentDir, "session-artifacts"), 0);
240
+ const artifactRoots = new Set([
241
+ path.resolve(agentDir, "session-artifacts"),
242
+ path.resolve(sessionDir, "..", "session-artifacts"),
243
+ ]);
244
+ for (const entry of list(sessionDir)) {
245
+ if (!entry.isFile() || !entry.name.endsWith(".jsonl")) continue;
246
+ scan(path.join(sessionDir, entry.name));
247
+ const rootId = entry.name.slice(0, -".jsonl".length);
248
+ for (const root of artifactRoots) walk(path.join(root, rootId), 0);
249
+ }
232
250
  process.stdout.write(JSON.stringify(calls));
233
251
  `;
234
252
  const COUNTS = [
@@ -258,21 +276,27 @@ function sumModelCalls(calls) {
258
276
  return total;
259
277
  }
260
278
  /**
261
- * The usage of the model calls `agentDir`'s session files recorded at or
262
- * after `sinceMs`. `runAsOwner` runs a Node script as the agent dir's owner
263
- * (the adapter's `runNodeAsSubprocess`), like every other read in that dir.
279
+ * The usage of the model calls one session's files recorded at or after
280
+ * `sinceMs`: the root files in `sessionDir` and its subagents' files in
281
+ * `agentDir`. `runAsOwner` runs a Node script as the agent dir's owner (the
282
+ * adapter's `runNodeAsSubprocess`), like every other read in that dir.
264
283
  */
265
- async function readPromptUsage(agentDir, sinceMs, runAsOwner) {
266
- const calls = JSON.parse(await runAsOwner("prime: cannot read the prompt's usage from the agent dir as its owner", MODEL_CALLS_SCRIPT, [agentDir, String(sinceMs)]));
284
+ async function readPromptUsage(session, sinceMs, runAsOwner) {
285
+ const calls = JSON.parse(await runAsOwner("prime: cannot read the prompt's usage from the agent dir as its owner", MODEL_CALLS_SCRIPT, [
286
+ session.agentDir,
287
+ session.sessionDir,
288
+ String(sinceMs)
289
+ ]));
267
290
  return Array.isArray(calls) ? sumModelCalls(calls) : void 0;
268
291
  }
269
292
  /**
270
293
  * `bridge` with each `session/prompt` response carrying the usage of the
271
294
  * model calls that prompt made, as `tokenUsage`.
272
295
  *
273
- * The window opens when the prompt is sent, which the ACP session adapter
274
- * does only after the session's previous prompt has answered, so one prompt's
275
- * calls are never read into another's. A prompt that fails leaves the window
296
+ * `readUsage` reads one session's files, and the window opens when the prompt
297
+ * is sent, which the ACP session adapter does only after the session's
298
+ * previous prompt has answered, so one prompt's calls are never read into
299
+ * another's. A prompt that fails leaves the window
276
300
  * open: the retry on the same process then reports the calls of both. A usage
277
301
  * read that fails is reported to `onUnreadable` and the response is returned
278
302
  * without usage, which stays distinct from a measured zero.
@@ -335,6 +359,8 @@ var PrimeKernelDeadError = class extends Error {
335
359
  //#endregion
336
360
  //#region src/adapters/prime/adapter.ts
337
361
  const DEFAULT_MODEL_PROVIDER = "tangle";
362
+ /** The prime-agent flag that names the directory a session's own file is written in. */
363
+ const SESSION_DIR_FLAG = "--session-dir";
338
364
  /**
339
365
  * Env vars the adapter sets on the spawned prime-agent. The bridge lays this
340
366
  * env over the sidecar's inherited one (sdk-cli-runner mergeSpawnEnv), so the
@@ -414,8 +440,9 @@ function __setPrimeBridgeFactoryForTesting(factory) {
414
440
  * Kernel death closes the session with a typed failure.
415
441
  *
416
442
  * prime hosts one session per connection and has no `session/load`: a new
417
- * process opens a new ACP session, and the conversation resumes from prime's
418
- * own session files in the per-session agent dir.
443
+ * process opens a new ACP session and a new prime-agent session. It reads an
444
+ * earlier session's file only when the caller's flags name one (`--continue`,
445
+ * `--resume`), which prime-agent looks up in its session directory.
419
446
  */
420
447
  var PrimeProviderAdapter = class extends AcpProviderAdapterBase {
421
448
  provider = "prime";
@@ -477,10 +504,12 @@ var PrimeProviderAdapter = class extends AcpProviderAdapterBase {
477
504
  }
478
505
  });
479
506
  const env = this.buildSpawnEnv(agentDir);
507
+ const args = this.buildSpawnArgs(config, modelId, profileArgs);
508
+ if (!args.includes(SESSION_DIR_FLAG)) args.push(SESSION_DIR_FLAG, this.sessionRecordsDir(agentDir, config, context.sessionId));
480
509
  return {
481
510
  command: this.getCliPath(),
482
511
  args: [
483
- ...this.buildSpawnArgs(config, modelId, profileArgs),
512
+ ...args,
484
513
  "--daemon-socket",
485
514
  env.PRIME_AGENT_DAEMON_SOCKET
486
515
  ],
@@ -488,21 +517,36 @@ var PrimeProviderAdapter = class extends AcpProviderAdapterBase {
488
517
  statusDetail: "Spawning prime-agent (ACP mode)"
489
518
  };
490
519
  }
520
+ /**
521
+ * Where prime-agent keeps this session's own records. An agent dir of the
522
+ * session's own holds them where prime-agent puts them by default. A shared
523
+ * agent dir (continual mode) gives each session a directory inside it, so
524
+ * the model calls one session made are never read as another's.
525
+ */
526
+ sessionRecordsDir(agentDir, config, sessionId) {
527
+ return config.continualHarnessState ? join(agentDir, "session-records", sessionId, "sessions") : join(agentDir, "sessions");
528
+ }
491
529
  spawnFailureMessage(failure) {
492
530
  return failure.missingBinary ? `prime: "${this.getCliPath()}" not found on PATH. The container image must ship the pinned Prime native CLI and prepared Python kernel — see packages/sdk-provider-cli-base/docs/prime.md.` : `prime: failed to spawn prime-agent: ${failure.message}`;
493
531
  }
494
532
  /**
495
533
  * prime-agent's `session/prompt` response reports no token usage, so each
496
534
  * response is completed with the usage prime wrote to its session files for
497
- * that prompt (see prompt-usage.ts). The files are read in the agent dir the
498
- * process was started on, which the agent environment can set over the
499
- * spec's.
535
+ * that prompt (see prompt-usage.ts). The files are the session's own: those
536
+ * in the session directory the process was started with, and its subagents'
537
+ * in the agent dir the process was started on, which the agent environment
538
+ * can set over the spec's.
500
539
  */
501
540
  spawnBridge(request) {
502
541
  const bridge = (bridgeFactoryOverride ?? spawnAcpBridge)(request);
503
542
  const agentDir = request.env?.[PRIME_AGENT_DIR_ENV_VAR$1];
504
- if (!agentDir) return bridge;
505
- return withPromptUsage(bridge, (sinceMs) => readPromptUsage(agentDir, sinceMs, (operation, script, args) => this.runNodeAsSubprocess(operation, script, args)), (error) => this.logger.warn("prime: the turn's token usage is not reported", { error: error instanceof Error ? error.message : String(error) }));
543
+ const flag = request.args.lastIndexOf(SESSION_DIR_FLAG);
544
+ const sessionDir = flag < 0 ? void 0 : request.args[flag + 1];
545
+ if (!agentDir || !sessionDir) return bridge;
546
+ return withPromptUsage(bridge, (sinceMs) => readPromptUsage({
547
+ agentDir,
548
+ sessionDir
549
+ }, sinceMs, (operation, script, args) => this.runNodeAsSubprocess(operation, script, args)), (error) => this.logger.warn("prime: the turn's token usage is not reported", { error: error instanceof Error ? error.message : String(error) }));
506
550
  }
507
551
  /**
508
552
  * A failed tool whose output carries a kernel-death marker (the ipython
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/sdk-provider-cli-base",
3
- "version": "0.13.206",
3
+ "version": "0.13.207",
4
4
  "description": "Base adapter for CLI-based AI agent providers",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
@@ -79,8 +79,8 @@
79
79
  "registry": "https://registry.npmjs.org"
80
80
  },
81
81
  "tangleRelease": {
82
- "sourceSha": "4731feee617625a5bacad23efc422de2319cceb4",
83
- "mergedAt": "2026-11-24T11:49:38-08:00"
82
+ "sourceSha": "35b13c72e9b97ee18ae7d61182a30a872cf552b9",
83
+ "mergedAt": "2026-11-24T12:34:25-08:00"
84
84
  },
85
85
  "scripts": {
86
86
  "build": "tsdown",