@springbrand/agent-runtime 0.3.0-alpha.2 → 0.3.0-alpha.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@springbrand/agent-runtime",
3
- "version": "0.3.0-alpha.2",
3
+ "version": "0.3.0-alpha.4",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src",
@@ -17,10 +17,10 @@
17
17
  "./models": "./src/lib/model-catalog.ts"
18
18
  },
19
19
  "dependencies": {
20
- "@cloudflare/codemode": "0.5.1",
20
+ "@cloudflare/codemode": "0.5.2",
21
21
  "@cloudflare/sandbox": "0.12.4",
22
22
  "@cloudflare/shell": "0.4.3",
23
- "@cloudflare/think": "0.17.0",
23
+ "@cloudflare/think": "0.18.0",
24
24
  "@earendil-works/pi-agent-core": "0.85.1",
25
25
  "@earendil-works/pi-ai": "0.85.1",
26
26
  "@types/lodash-es": "^4.17.12",
@@ -30,11 +30,11 @@
30
30
  "zod": "^4.4.3"
31
31
  },
32
32
  "peerDependencies": {
33
- "agents": "^0.22.0",
33
+ "agents": "^0.23.0",
34
34
  "ai": "^7.0.0"
35
35
  },
36
36
  "devDependencies": {
37
- "agents": "0.22.0",
37
+ "agents": "0.23.0",
38
38
  "ai": "7.0.56"
39
39
  }
40
40
  }
@@ -114,7 +114,7 @@ interface RuntimeAgentDefinitionBase<
114
114
  /** Prepare the optional telemetry consumer for this concrete Agent facet. */
115
115
  readonly telemetry?: (
116
116
  context: RuntimeAgentPlanningContext<Env, Command, Change>,
117
- ) => AgentTelemetryBinding | undefined;
117
+ ) => AgentTelemetryBinding | undefined | Promise<AgentTelemetryBinding | undefined>;
118
118
  }
119
119
 
120
120
  export interface ReadonlyRuntimeAgentDefinition<
@@ -406,14 +406,14 @@ export function defineRuntimeAgent<
406
406
  | MutableRuntimeAgentDefinition<Env, Config, Command, Change>
407
407
  | ReadonlyRuntimeAgentDefinition<Env, Config>,
408
408
  ): RuntimeAgentClass<Env, Command, Change> {
409
- const resolveDefinitionTelemetry = (
409
+ const resolveDefinitionTelemetry = async (
410
410
  context: RuntimeAgentPlanningContext<Env, Command, Change>,
411
- ): AgentTelemetryBinding | undefined => {
411
+ ): Promise<AgentTelemetryBinding | undefined> => {
412
412
  const telemetry = definition.telemetry;
413
413
  if (!telemetry) return undefined;
414
- return (telemetry as (
414
+ return await (telemetry as (
415
415
  context: RuntimeAgentPlanningContext<Env, Command, Change>,
416
- ) => AgentTelemetryBinding | undefined)(context);
416
+ ) => AgentTelemetryBinding | undefined | Promise<AgentTelemetryBinding | undefined>)(context);
417
417
  };
418
418
 
419
419
  const GeneratedRuntimeAgent = {
@@ -635,7 +635,7 @@ export function defineRuntimeAgent<
635
635
  let resources = loaded.resources;
636
636
  let commitGuard = loaded.commitGuard;
637
637
  if (!this.telemetryResolved) {
638
- this.resolvedTelemetry = resolveDefinitionTelemetry(context);
638
+ this.resolvedTelemetry = await resolveDefinitionTelemetry(context);
639
639
  this.telemetryResolved = true;
640
640
  }
641
641
  const telemetry = this.resolvedTelemetry;
@@ -1,4 +1,4 @@
1
- import { estimateStringTokens } from "agents/experimental/memory/utils";
1
+ import { estimateStringTokens } from "../lib/token-estimate";
2
2
  import type { RuntimeMemoryPort } from "../config/ports";
3
3
  import type { RuntimeDegradation } from "../config/degradation";
4
4
  import type { RuntimeMemoryProfile } from "../config/profile";
@@ -0,0 +1,8 @@
1
+ /** Conservative token estimate for prompt budgeting, not provider billing. */
2
+ export function estimateStringTokens(text: string): number {
3
+ if (!text) return 0;
4
+ return Math.ceil(Math.max(
5
+ text.length / 4,
6
+ text.split(/\s+/u).filter(Boolean).length * 1.3,
7
+ ));
8
+ }
@@ -43,6 +43,13 @@ const RETIRED_SCHEDULE_CALLBACKS = [
43
43
  "_chatRecoveryRetry",
44
44
  ] as const;
45
45
 
46
+ const LEGACY_TERMINAL_SUBMISSION_STATUSES = new Set([
47
+ "completed",
48
+ "failed",
49
+ "aborted",
50
+ "skipped",
51
+ ]);
52
+
46
53
  type LegacyRecord = Record<string, unknown> & {
47
54
  id: string;
48
55
  parentId: string | null;
@@ -310,6 +317,34 @@ function hasToolExecution(value: ToolExecution | undefined): boolean {
310
317
  typeof value.args === "object" && !Array.isArray(value.args));
311
318
  }
312
319
 
320
+ /** A terminal Submission cannot resume, so its stale lane pointer is not active work. */
321
+ function clearTerminalOperationPointers(sql: SqlStorage): void {
322
+ if (!hasTable(sql, "pi_submissions")) return;
323
+ const lanes = sql.exec<{ key: string; body: string }>(
324
+ "SELECT key, body FROM pi_values WHERE namespace = ?",
325
+ laneState("").namespace,
326
+ ).toArray();
327
+ for (const lane of lanes) {
328
+ const state = JSON.parse(lane.body) as LaneState;
329
+ if (state.currentOperationId === null) continue;
330
+ const terminal = sql.exec(
331
+ `SELECT 1 FROM pi_submissions
332
+ WHERE submission_id = ?
333
+ AND status IN ('completed', 'failed', 'aborted', 'skipped')
334
+ LIMIT 1`,
335
+ state.currentOperationId,
336
+ ).toArray().length === 1;
337
+ if (!terminal) continue;
338
+ sql.exec(
339
+ `UPDATE pi_values
340
+ SET body = json_set(body, '$.currentOperationId', json('null'))
341
+ WHERE namespace = ? AND key = ?`,
342
+ laneState("").namespace,
343
+ lane.key,
344
+ );
345
+ }
346
+ }
347
+
313
348
  /** Old active tool outcomes cannot be reconstructed from error text or approval facts. */
314
349
  function assertActiveToolProvenance(sql: SqlStorage): void {
315
350
  const read = <T>(address: Value<T>): T | undefined => {
@@ -379,6 +414,7 @@ export function migrateCurrentPiSession(
379
414
  if (version !== 1 && version !== 2) {
380
415
  throw new Error(`Unsupported Cloudflare Pi storage version: ${version}`);
381
416
  }
417
+ clearTerminalOperationPointers(sql);
382
418
  assertActiveToolProvenance(sql);
383
419
  if (version === 1) removeRetiredRuntimeArtifacts(sql);
384
420
  repairDuplicatedUserSidecars(sql);
@@ -761,6 +797,9 @@ export function migrateLegacyPiSession(
761
797
  const knownSubmissions = new Set(
762
798
  submissions.map((row) => row.submission_id),
763
799
  );
800
+ const submissionStatuses = new Map(
801
+ submissions.map((row) => [row.submission_id, row.status]),
802
+ );
764
803
  for (const submission of submissions) {
765
804
  if (submission.status !== "pending" || !submission.queued_input_json)
766
805
  continue;
@@ -809,8 +848,12 @@ export function migrateLegacyPiSession(
809
848
  if (!knownSubmissions.has(row.submission_id))
810
849
  throw new Error("Dangling recovery submission");
811
850
  const record = decodePiToolRecoveryMilestone(row.body);
812
- if (!record || record.turnId !== row.submission_id)
851
+ if (!record || record.turnId !== row.submission_id) {
852
+ if (LEGACY_TERMINAL_SUBMISSION_STATUSES.has(
853
+ submissionStatuses.get(row.submission_id) ?? "",
854
+ )) continue;
813
855
  throw new Error("Invalid legacy recovery milestone");
856
+ }
814
857
  const bodies = bySubmission.get(row.submission_id) ?? [];
815
858
  bodies.push(row.body);
816
859
  bySubmission.set(row.submission_id, bodies);
package/src/tools/base.ts CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  type Static,
9
9
  type TSchema,
10
10
  } from "@earendil-works/pi-ai";
11
- import { estimateStringTokens } from "agents/experimental/memory/utils";
11
+ import { estimateStringTokens } from "../lib/token-estimate";
12
12
  import type { RuntimeMemoryPort } from "../config/ports";
13
13
  import type { RuntimeMemoryProfile } from "../config/profile";
14
14
  import { serializeOutput } from "../lib/artifacts";