@lobu/runtime-device 20.1.0 → 21.0.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.
Files changed (121) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +46 -40
  3. package/npm-shrinkwrap.json +283 -237
  4. package/package.json +1 -1
  5. package/vendor/cli/dist/commands/_lib/apply/apply-cmd.d.ts +7 -2
  6. package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +77 -75
  7. package/vendor/cli/dist/commands/_lib/apply/client.d.ts +40 -25
  8. package/vendor/cli/dist/commands/_lib/apply/client.js +31 -39
  9. package/vendor/cli/dist/commands/_lib/apply/deployment.d.ts +1 -1
  10. package/vendor/cli/dist/commands/_lib/apply/deployment.js +2 -2
  11. package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +47 -11
  12. package/vendor/cli/dist/commands/_lib/apply/desired-state.js +106 -7
  13. package/vendor/cli/dist/commands/_lib/apply/diff.d.ts +14 -6
  14. package/vendor/cli/dist/commands/_lib/apply/diff.js +42 -23
  15. package/vendor/cli/dist/commands/_lib/apply/map-config.js +2 -3
  16. package/vendor/cli/dist/commands/_lib/apply/render.js +2 -0
  17. package/vendor/cli/dist/commands/_lib/apply/rollback-cmd.js +3 -3
  18. package/vendor/cli/dist/commands/_lib/connector-loader.d.ts +5 -1
  19. package/vendor/cli/dist/commands/_lib/connector-loader.js +1 -1
  20. package/vendor/cli/dist/commands/_lib/ensure-deps-installed.d.ts +15 -26
  21. package/vendor/cli/dist/commands/_lib/ensure-deps-installed.js +227 -136
  22. package/vendor/cli/dist/commands/_lib/view-bundler.d.ts +33 -0
  23. package/vendor/cli/dist/commands/_lib/view-bundler.js +207 -0
  24. package/vendor/cli/dist/commands/_lib/view-reapply.d.ts +60 -0
  25. package/vendor/cli/dist/commands/_lib/view-reapply.js +182 -0
  26. package/vendor/cli/dist/commands/dev.d.ts +14 -5
  27. package/vendor/cli/dist/commands/dev.js +56 -8
  28. package/vendor/cli/dist/commands/init.d.ts +1 -1
  29. package/vendor/cli/dist/commands/init.js +4 -4
  30. package/vendor/cli/dist/commands/login.js +3 -0
  31. package/vendor/cli/dist/config/define.d.ts +25 -8
  32. package/vendor/cli/dist/config/define.js +24 -0
  33. package/vendor/cli/dist/connectors/README.md +15 -16
  34. package/vendor/cli/dist/connectors/chat-user-identity.ts +41 -0
  35. package/vendor/cli/dist/connectors/github.ts +15 -9
  36. package/vendor/cli/dist/connectors/google-identity.ts +11 -0
  37. package/vendor/cli/dist/connectors/google_calendar.ts +21 -31
  38. package/vendor/cli/dist/connectors/google_drive.ts +106 -131
  39. package/vendor/cli/dist/connectors/google_gmail.ts +53 -30
  40. package/vendor/cli/dist/connectors/hackernews.ts +2 -5
  41. package/vendor/cli/dist/connectors/jira.ts +62 -27
  42. package/vendor/cli/dist/connectors/linear.ts +55 -21
  43. package/vendor/cli/dist/connectors/microsoft_outlook.ts +151 -110
  44. package/vendor/cli/dist/connectors/postgres.ts +19 -11
  45. package/vendor/cli/dist/connectors/producthunt.ts +8 -6
  46. package/vendor/cli/dist/connectors/reddit.ts +2 -2
  47. package/vendor/cli/dist/connectors/rss.ts +2 -2
  48. package/vendor/cli/dist/connectors/slack-identity.ts +19 -0
  49. package/vendor/cli/dist/connectors/whatsapp_web.ts +5 -4
  50. package/vendor/cli/dist/connectors/x.ts +32 -17
  51. package/vendor/cli/dist/connectors/youtube.ts +18 -5
  52. package/vendor/cli/dist/internal/connector-compiler.d.ts +1 -1
  53. package/vendor/cli/dist/internal/connector-compiler.js +216 -17
  54. package/vendor/cli/dist/internal/credentials.js +5 -1
  55. package/vendor/cli/dist/internal/oauth.d.ts +15 -1
  56. package/vendor/cli/dist/internal/oauth.js +9 -1
  57. package/vendor/cli/dist/runtime-components.json +4 -4
  58. package/vendor/cli/package.json +2 -1
  59. package/vendor/connector-sdk/dist/connector-runtime.d.ts +4 -6
  60. package/vendor/connector-sdk/dist/connector-runtime.js +3 -5
  61. package/vendor/connector-sdk/dist/connector-types.d.ts +28 -13
  62. package/vendor/connector-sdk/dist/define-connector.d.ts +4 -1
  63. package/vendor/connector-sdk/dist/define-connector.js +4 -1
  64. package/vendor/connector-sdk/dist/http-client.d.ts +18 -7
  65. package/vendor/connector-sdk/dist/http-client.js +15 -13
  66. package/vendor/connector-sdk/dist/index.d.ts +1 -1
  67. package/vendor/connector-sdk/dist/retry.d.ts +15 -3
  68. package/vendor/connector-sdk/dist/retry.js +40 -64
  69. package/vendor/connector-sdk/package.json +1 -1
  70. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +122 -10
  71. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +78 -10
  72. package/vendor/connector-worker/dist/agent-turn/types.d.ts +34 -4
  73. package/vendor/connector-worker/dist/compile/index.d.ts +21 -2
  74. package/vendor/connector-worker/dist/compile/index.js +41 -8
  75. package/vendor/connector-worker/dist/compile/source-capture.d.ts +10 -0
  76. package/vendor/connector-worker/dist/compile/source-capture.js +108 -0
  77. package/vendor/connector-worker/dist/daemon/agent-turn.js +97 -10
  78. package/vendor/connector-worker/dist/daemon/automation-process.d.ts +32 -0
  79. package/vendor/connector-worker/dist/daemon/automation-process.js +102 -1
  80. package/vendor/connector-worker/dist/daemon/automation.d.ts +1 -1
  81. package/vendor/connector-worker/dist/daemon/builtins/index.js +8 -0
  82. package/vendor/connector-worker/dist/daemon/builtins/os-shell.d.ts +13 -1
  83. package/vendor/connector-worker/dist/daemon/builtins/os-shell.js +28 -2
  84. package/vendor/connector-worker/dist/daemon/executor.d.ts +0 -1
  85. package/vendor/connector-worker/dist/daemon/executor.js +42 -51
  86. package/vendor/connector-worker/dist/daemon/start.js +5 -1
  87. package/vendor/connector-worker/dist/executor/interface.d.ts +12 -8
  88. package/vendor/connector-worker/dist/executor/isolate.js +45 -28
  89. package/vendor/connector-worker/dist/self-check/index.js +15 -13
  90. package/vendor/connector-worker/package.json +1 -1
  91. package/vendor/core/dist/agent-policy.js +3 -0
  92. package/vendor/core/dist/connector-query-errors.d.ts +3 -3
  93. package/vendor/core/dist/connector-query-errors.js +8 -3
  94. package/vendor/core/dist/contracts/tools/manage-connections.d.ts +24 -0
  95. package/vendor/core/dist/contracts/tools/manage-connections.js +13 -0
  96. package/vendor/core/dist/contracts/tools/manage-entity-schema.d.ts +15 -25
  97. package/vendor/core/dist/contracts/tools/manage-entity-schema.js +3 -5
  98. package/vendor/core/dist/contracts/tools/manage-feeds.d.ts +11 -0
  99. package/vendor/core/dist/contracts/tools/manage-feeds.js +12 -1
  100. package/vendor/core/dist/contracts/tools/manage-operations.d.ts +47 -6
  101. package/vendor/core/dist/contracts/tools/manage-operations.js +36 -1
  102. package/vendor/core/dist/contracts/tools/manage-views.d.ts +293 -0
  103. package/vendor/core/dist/contracts/tools/manage-views.js +203 -0
  104. package/vendor/core/dist/contracts/tools/source-files-schema.d.ts +7 -0
  105. package/vendor/core/dist/contracts/tools/source-files-schema.js +19 -0
  106. package/vendor/core/dist/contracts/tools/source-files.d.ts +14 -0
  107. package/vendor/core/dist/contracts/tools/source-files.js +55 -0
  108. package/vendor/core/dist/contracts/tools/view-content-hash.d.ts +37 -0
  109. package/vendor/core/dist/contracts/tools/view-content-hash.js +58 -0
  110. package/vendor/core/dist/contracts/tools/view-path.d.ts +18 -0
  111. package/vendor/core/dist/contracts/tools/view-path.js +24 -0
  112. package/vendor/core/dist/contracts/worker/protocol.d.ts +73 -8
  113. package/vendor/core/dist/contracts/worker/protocol.js +58 -9
  114. package/vendor/core/dist/credentials.d.ts +16 -1
  115. package/vendor/core/dist/credentials.js +10 -1
  116. package/vendor/core/dist/reserved.d.ts +3 -3
  117. package/vendor/core/dist/reserved.js +9 -1
  118. package/vendor/core/dist/utils/secret-redaction.js +2 -0
  119. package/vendor/core/package.json +25 -5
  120. package/vendor/core/dist/contracts/tools/manage-view-templates.d.ts +0 -165
  121. package/vendor/core/dist/contracts/tools/manage-view-templates.js +0 -130
@@ -30,6 +30,16 @@ interface HttpClientRetryOptions {
30
30
  context?: Record<string, any>;
31
31
  onRetry?: (error: Error, attempt: number) => void;
32
32
  }
33
+ /** `RequestInit` plus the one hint retrying needs from the caller. */
34
+ export interface HttpRequestInit extends RequestInit {
35
+ /**
36
+ * The request is safe to repeat although its method is not idempotent — a
37
+ * GraphQL query or a search sent as POST. Without it a POST/PATCH is retried
38
+ * only on a 429, never on a 5xx or a dropped connection, because those do not
39
+ * say whether the server already acted on it (a sent email, a created issue).
40
+ */
41
+ idempotent?: boolean;
42
+ }
33
43
  export interface CreateHttpClientOptions {
34
44
  /**
35
45
  * Static bearer token shorthand: sent as `Authorization: Bearer <token>`
@@ -56,18 +66,19 @@ export interface HttpClient {
56
66
  /**
57
67
  * Fetch with auth + retry, resolving with the raw `Response` for non-2xx
58
68
  * statuses so callers can keep their own `response.ok` handling. Transient
59
- * statuses (429/5xx) still throw to drive the retry loop, so they surface
60
- * as a thrown `HttpStatusError` once retries are exhausted.
69
+ * statuses (408, 429, 5xx — see `classifyToolError`) still throw to drive the
70
+ * retry loop, so they surface as a thrown `HttpStatusError` once retries are
71
+ * exhausted.
61
72
  */
62
- raw(url: string, init?: RequestInit): Promise<Response>;
73
+ raw(url: string, init?: HttpRequestInit): Promise<Response>;
63
74
  /** Fetch with auth + retry; throws `HttpStatusError` on any non-2xx status. */
64
- request(url: string, init?: RequestInit): Promise<Response>;
75
+ request(url: string, init?: HttpRequestInit): Promise<Response>;
65
76
  /** `request()` + parse the response body as JSON (any method via `init`). */
66
- json<T>(url: string, init?: RequestInit): Promise<T>;
77
+ json<T>(url: string, init?: HttpRequestInit): Promise<T>;
67
78
  /** GET returning parsed JSON. */
68
- get<T>(url: string, init?: RequestInit): Promise<T>;
79
+ get<T>(url: string, init?: HttpRequestInit): Promise<T>;
69
80
  /** POST `body` as JSON (unless a Content-Type is supplied) returning parsed JSON. */
70
- post<T>(url: string, body?: unknown, init?: RequestInit): Promise<T>;
81
+ post<T>(url: string, body?: unknown, init?: HttpRequestInit): Promise<T>;
71
82
  }
72
83
  export declare function createHttpClient(options?: CreateHttpClientOptions): HttpClient;
73
84
  export interface RequireBearerClientOptions {
@@ -7,14 +7,10 @@
7
7
  * `Retry-After`, and descriptive errors that include the status code and a
8
8
  * truncated response body.
9
9
  */
10
- import { withHttpRetry } from './retry.js';
10
+ import { isTransientStatus, withHttpRetry } from './retry.js';
11
11
  import { sleep } from './sleep.js';
12
- /**
13
- * Statuses treated as transient: the client throws an `HttpStatusError` for
14
- * these so `withHttpRetry` can retry them (its keyword classifier recognizes
15
- * the status code embedded in the message).
16
- */
17
- const TRANSIENT_STATUSES = new Set([429, 500, 502, 503, 504]);
12
+ /** Methods RFC 9110 §9.2.2 defines as idempotent: repeating one has the effect of sending it once. */
13
+ const IDEMPOTENT_METHODS = new Set(['GET', 'HEAD', 'OPTIONS', 'TRACE', 'PUT', 'DELETE']);
18
14
  const BODY_PREVIEW_CHARS = 500;
19
15
  const DEFAULT_MAX_RETRY_AFTER_MS = 30_000;
20
16
  /** Error thrown for non-2xx responses. Message: `<prefix> <METHOD> <url> failed (<status>): <truncated body>`. */
@@ -85,10 +81,11 @@ export function createHttpClient(options = {}) {
85
81
  async function attempt(url, init, throwOnAnyError) {
86
82
  const method = (init?.method ?? 'GET').toUpperCase();
87
83
  const headers = await buildHeaders(init);
88
- const response = await fetch(url, { ...init, headers });
84
+ const { idempotent: _idempotent, ...fetchInit } = init ?? {};
85
+ const response = await fetch(url, { ...fetchInit, headers });
89
86
  if (response.ok)
90
87
  return response;
91
- if (TRANSIENT_STATUSES.has(response.status)) {
88
+ if (isTransientStatus(response.status)) {
92
89
  const error = await statusError(response, method, url);
93
90
  if (response.status === 429 && retryEnabled) {
94
91
  const retryAfterMs = parseRetryAfterMs(response.headers.get('Retry-After'));
@@ -101,13 +98,18 @@ export function createHttpClient(options = {}) {
101
98
  throw await statusError(response, method, url);
102
99
  return response;
103
100
  }
104
- function withRetry(fn) {
101
+ function withRetry(init, fn) {
105
102
  if (!retryEnabled)
106
103
  return fn();
107
- return withHttpRetry(fn, { operation: `${errorPrefix} request`, ...retryOptions });
104
+ const method = (init?.method ?? 'GET').toUpperCase();
105
+ return withHttpRetry(fn, {
106
+ operation: `${errorPrefix} request`,
107
+ ...retryOptions,
108
+ repeatable: IDEMPOTENT_METHODS.has(method) || init?.idempotent === true,
109
+ });
108
110
  }
109
- const raw = (url, init) => withRetry(() => attempt(url, init, false));
110
- const request = (url, init) => withRetry(() => attempt(url, init, true));
111
+ const raw = (url, init) => withRetry(init, () => attempt(url, init, false));
112
+ const request = (url, init) => withRetry(init, () => attempt(url, init, true));
111
113
  const json = async (url, init) => {
112
114
  const response = await request(url, init);
113
115
  return (await response.json());
@@ -23,7 +23,7 @@ export type { AccessIdentitySpec, AccessMember, AccessResource, AclSourceDef, Ch
23
23
  export { ACL_RESOURCE_TYPE, ACL_RESOURCE_TYPE_SLUG, } from './acl-source.js';
24
24
  import { EVENT_RECALL_IDENTITY_NAMESPACES, getIdentityNamespaceDefinition, IDENTITY, IDENTITY_NAMESPACE_REGISTRY, isEventRecallIdentityNamespace } from './identity-namespaces.js';
25
25
  export { EVENT_RECALL_IDENTITY_NAMESPACES, getIdentityNamespaceDefinition, IDENTITY, IDENTITY_NAMESPACE_REGISTRY, isEventRecallIdentityNamespace, };
26
- export type { CreateHttpClientOptions, HttpClient, RequireBearerClientOptions, } from './http-client.js';
26
+ export type { CreateHttpClientOptions, HttpClient, HttpRequestInit, RequireBearerClientOptions, } from './http-client.js';
27
27
  export { createHttpClient, HttpStatusError, requireBearerClient } from './http-client.js';
28
28
  export { sdkLogger, sdkLogger as logger } from './logger.js';
29
29
  export type { CursorPage, OffsetPage, PaginateByCursorOptions, PaginateByOffsetOptions, } from './pagination.js';
@@ -1,9 +1,12 @@
1
1
  /**
2
2
  * HTTP retry helper for connector SDK.
3
3
  *
4
- * Exponential backoff with full jitter (5 retries, 1s → 16s), retry on
5
- * transient network/rate-limit/server errors, abort on permanent client errors
6
- * (401/403/404/etc.).
4
+ * Exponential backoff with full jitter (5 retries, 1s → 16s). Whether an error
5
+ * is worth retrying is decided by `classifyToolError`, the one catalog shared
6
+ * with the server: an HTTP status or SQLSTATE the error carries always wins,
7
+ * and message text is consulted only when it carries neither. A response
8
+ * body or URL is never evidence — a 503 whose body says "invalid" is still a
9
+ * 503.
7
10
  *
8
11
  * The backoff loop lives here rather than being imported from `@lobu/core`:
9
12
  * core's root entry drags winston, Sentry and OpenTelemetry into every
@@ -11,10 +14,19 @@
11
14
  * isolate. It keeps the semantics of core's `retryWithBackoff` for the one
12
15
  * configuration `withHttpRetry` uses: exponential, capped, full jitter.
13
16
  */
17
+ /** Whether a response with this HTTP status may succeed if the request is repeated. */
18
+ export declare function isTransientStatus(status: number): boolean;
14
19
  interface RetryOptions {
15
20
  operation?: string;
16
21
  context?: Record<string, any>;
17
22
  onRetry?: (error: Error, attempt: number) => void;
23
+ /**
24
+ * Whether repeating the call is safe when an earlier attempt may already have
25
+ * taken effect. Default `true`. Pass `false` for a write (a send, a create): it
26
+ * is then retried only on a 429, which proves the server did not act on it —
27
+ * never on a 5xx or a dropped connection (RFC 9110 §9.2.2).
28
+ */
29
+ repeatable?: boolean;
18
30
  }
19
31
  /**
20
32
  * HTTP retry strategy
@@ -1,9 +1,12 @@
1
1
  /**
2
2
  * HTTP retry helper for connector SDK.
3
3
  *
4
- * Exponential backoff with full jitter (5 retries, 1s → 16s), retry on
5
- * transient network/rate-limit/server errors, abort on permanent client errors
6
- * (401/403/404/etc.).
4
+ * Exponential backoff with full jitter (5 retries, 1s → 16s). Whether an error
5
+ * is worth retrying is decided by `classifyToolError`, the one catalog shared
6
+ * with the server: an HTTP status or SQLSTATE the error carries always wins,
7
+ * and message text is consulted only when it carries neither. A response
8
+ * body or URL is never evidence — a 503 whose body says "invalid" is still a
9
+ * 503.
7
10
  *
8
11
  * The backoff loop lives here rather than being imported from `@lobu/core`:
9
12
  * core's root entry drags winston, Sentry and OpenTelemetry into every
@@ -11,6 +14,7 @@
11
14
  * isolate. It keeps the semantics of core's `retryWithBackoff` for the one
12
15
  * configuration `withHttpRetry` uses: exponential, capped, full jitter.
13
16
  */
17
+ import { classifyToolError, isRetryable, } from '@lobu/core/connector-query-errors';
14
18
  import { sdkLogger } from './logger.js';
15
19
  async function retryWithBackoff(fn, options) {
16
20
  const { maxRetries, baseDelay, maxDelay, shouldRetry, onRetry } = options;
@@ -41,61 +45,38 @@ async function retryWithBackoff(fn, options) {
41
45
  }
42
46
  throw lastError;
43
47
  }
44
- const TRANSIENT_KEYWORDS = [
45
- // network
46
- 'network',
47
- 'econnrefused',
48
- 'etimedout',
49
- 'enotfound',
50
- 'econnreset',
51
- 'fetch failed',
52
- 'socket',
53
- 'dns',
54
- // database
55
- 'connection pool',
56
- 'too many connections',
57
- 'connection limit',
58
- 'connection reset',
59
- 'connection refused',
60
- 'server closed',
61
- 'connection terminated',
62
- 'connection timeout',
63
- 'deadlock',
64
- 'lock timeout',
65
- 'query timeout',
66
- 'statement timeout',
67
- 'transaction',
68
- 'postgres',
69
- 'postgresql',
70
- 'pg_',
71
- 'relation does not exist',
72
- 'syntax error',
73
- // rate limit
74
- 'rate limit',
75
- '429',
76
- 'too many requests',
77
- // server
78
- '500',
79
- '502',
80
- '503',
81
- '504',
82
- 'server error',
83
- 'service unavailable',
84
- 'gateway timeout',
85
- ];
86
- const PERMANENT_KEYWORDS = [
87
- 'not found',
88
- '404',
89
- 'unauthorized',
90
- '401',
91
- 'forbidden',
92
- '403',
93
- 'invalid',
94
- 'bad request',
95
- '400',
96
- ];
97
- function errorMessage(error) {
98
- return (error instanceof Error ? error.message : String(error)).toLowerCase();
48
+ /**
49
+ * The structured signal an error carries, read off the error itself. An
50
+ * `HttpStatusError` carries `status`; a postgres.js error carries a SQLSTATE
51
+ * `code`. The message rides along only as the catalog's last resort, for
52
+ * errors that arrive as text alone (a failed fetch inside the isolate is a
53
+ * bare `TypeError('fetch failed: …')`).
54
+ */
55
+ function errorSignal(error) {
56
+ if (!(error instanceof Error))
57
+ return { message: String(error) };
58
+ const fields = error;
59
+ return {
60
+ httpStatus: typeof fields.status === 'number' ? fields.status : undefined,
61
+ pgCode: typeof fields.code === 'string' ? fields.code : undefined,
62
+ message: error.message,
63
+ };
64
+ }
65
+ /** Whether a response with this HTTP status may succeed if the request is repeated. */
66
+ export function isTransientStatus(status) {
67
+ return isRetryable(classifyToolError({ httpStatus: status }));
68
+ }
69
+ /** Whether `error` may succeed if the identical call is repeated. */
70
+ function isTransientError(error) {
71
+ return isRetryable(classifyToolError(errorSignal(error)));
72
+ }
73
+ /**
74
+ * Whether `error` proves the server refused the request without acting on it.
75
+ * Only a 429 says so; a 5xx or a dropped connection says nothing about whether
76
+ * a write landed.
77
+ */
78
+ function isUnprocessed(error) {
79
+ return classifyToolError(errorSignal(error)) === 'RATE_LIMITED';
99
80
  }
100
81
  /**
101
82
  * HTTP retry strategy
@@ -110,12 +91,7 @@ export async function withHttpRetry(fn, options) {
110
91
  maxRetries: totalRetries,
111
92
  baseDelay: 1000,
112
93
  maxDelay: 16000,
113
- shouldRetry: (error) => {
114
- const msg = errorMessage(error);
115
- if (PERMANENT_KEYWORDS.some((k) => msg.includes(k)))
116
- return false;
117
- return TRANSIENT_KEYWORDS.some((k) => msg.includes(k));
118
- },
94
+ shouldRetry: options?.repeatable === false ? isUnprocessed : isTransientError,
119
95
  onRetry: (attempt, error) => {
120
96
  options?.onRetry?.(error, attempt);
121
97
  sdkLogger.debug({
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobu/connector-sdk",
3
- "version": "20.1.0",
3
+ "version": "21.0.0",
4
4
  "description": "Lobu memory SDK — build pluggable connectors for Lobu memory",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -341,6 +341,53 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
341
341
  let toolCalls = 0;
342
342
  /** Insertion-ordered, so the ledger reads in first-call order. */
343
343
  const toolsUsed = new Set();
344
+ /**
345
+ * Per-input attribution for the tools above. The owner input's own calls
346
+ * stay in `toolsUsed`; each steered input run id owns its own ledger. A
347
+ * terminal reply is stamped with its input's ledger only, never the
348
+ * execution-wide union — otherwise a sibling input's calls would be reported
349
+ * as this reply's evidence (#3662).
350
+ */
351
+ const steeredToolsUsed = new Map();
352
+ /**
353
+ * First tool error per input, quoted exactly as the tool returned it. Bound
354
+ * at call START to the input the call was made for, so a result that lands
355
+ * after a mid-turn follow-up stays with its initiating message.
356
+ */
357
+ let ownerFirstError;
358
+ const steeredFirstErrors = new Map();
359
+ /** Accepted input run id the active tool call was started for (`undefined` = owner). */
360
+ let activeInputRunId;
361
+ /**
362
+ * The input that emitted the current tool batch: captured on every assistant
363
+ * `message_end` (the message that carries the calls), because a steered
364
+ * follow-up can be appended — moving `activeInputRunId` — between the model
365
+ * emitting a call and the worker starting or finishing it. The result stays
366
+ * with the message that initiated it, never with whichever input is active
367
+ * when it lands.
368
+ */
369
+ let emitInputRunId;
370
+ const toolInputRun = new Map();
371
+ const toolsFor = (runId) => {
372
+ if (runId === undefined)
373
+ return toolsUsed;
374
+ let set = steeredToolsUsed.get(runId);
375
+ if (!set) {
376
+ set = new Set();
377
+ steeredToolsUsed.set(runId, set);
378
+ }
379
+ return set;
380
+ };
381
+ const noteToolError = (runId, output) => {
382
+ if (!output)
383
+ return;
384
+ if (runId === undefined) {
385
+ ownerFirstError ??= output;
386
+ return;
387
+ }
388
+ if (!steeredFirstErrors.has(runId))
389
+ steeredFirstErrors.set(runId, output);
390
+ };
344
391
  // `ask_user` hands the conversation back to the human: the question is posted
345
392
  // as buttons and the click returns as a NEW inbound message, which is a new
346
393
  // turn. Stop the session at that point, or the model keeps calling tools
@@ -449,6 +496,9 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
449
496
  // turn, in order. It drives the live typing indicator and is the fallback
450
497
  // answer for a turn that never settles a message.
451
498
  let text = '';
499
+ // Owner-only fallback stream. The aggregate `text` includes steered
500
+ // replies too, so it cannot safely stand in for an unsettled owner answer.
501
+ let ownerText = '';
452
502
  let stopReason = null;
453
503
  let usage = null;
454
504
  // While the pre-compaction memory flush runs, nothing it produces is the
@@ -458,12 +508,29 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
458
508
  // arrived at exactly those points — after an assistant message, after a tool
459
509
  // result — and queue it as the user message it is.
460
510
  const steeredMessages = new Map();
511
+ // Steered input run ids in arrival order. A user `message_end` closes the
512
+ // answer to the message before it: the first one closes the owner's, each
513
+ // later one hands the active input to the next steered run. FIFO, not
514
+ // identity: pi rewrites messages on the way to the provider, so the event
515
+ // object is not reliably the one `agent.steer` was handed (see the
516
+ // `messageContext` note above).
517
+ const steeredRunOrder = [];
461
518
  // The turn's answer(s), one per user message the model replied to. Tracked
462
519
  // from events rather than read back out of `agent.state.messages` at the
463
520
  // end: compaction replaces that array mid-turn, and the newest message is
464
521
  // the wrong one to read anyway — see the `text` field of the result.
465
- const answers = [];
522
+ let ownerAnswer = null;
523
+ const inputAnswers = new Map();
524
+ let sawInitialUser = false;
466
525
  let answer = null;
526
+ const settleAnswer = () => {
527
+ if (!answer)
528
+ return;
529
+ if (activeInputRunId === undefined)
530
+ ownerAnswer = answer;
531
+ else
532
+ inputAnswers.set(activeInputRunId, answer);
533
+ };
467
534
  const steer = () => {
468
535
  if (flushing)
469
536
  return;
@@ -472,6 +539,7 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
472
539
  role: 'user', content: [{ type: 'text', text: message.text }], timestamp: Date.now(),
473
540
  };
474
541
  steeredMessages.set(nativeMessage, message.runId);
542
+ steeredRunOrder.push(message.runId);
475
543
  // Its own block, beside its own message. Recorded BEFORE the steer so
476
544
  // the loader can never see the message without its context.
477
545
  // Last writer wins on identical text. Two follow-ups with the SAME
@@ -501,6 +569,8 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
501
569
  const partial = event.assistantMessageEvent;
502
570
  if (partial.type === 'text_delta' && typeof partial.delta === 'string') {
503
571
  text += partial.delta;
572
+ if (activeInputRunId === undefined)
573
+ ownerText += partial.delta;
504
574
  emit({ type: 'text_delta', delta: partial.delta });
505
575
  }
506
576
  else if (partial.type === 'thinking_delta' && typeof partial.delta === 'string') {
@@ -513,8 +583,11 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
513
583
  // A user message — the prompt, or a steered follow-up Pi injected —
514
584
  // closes the answer to the message before it.
515
585
  if (message.role === 'user') {
516
- if (answer)
517
- answers.push(answer);
586
+ settleAnswer();
587
+ if (sawInitialUser)
588
+ activeInputRunId = steeredRunOrder.shift();
589
+ else
590
+ sawInitialUser = true;
518
591
  answer = null;
519
592
  return;
520
593
  }
@@ -522,6 +595,11 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
522
595
  // turn's outcome.
523
596
  if (message.role !== 'assistant')
524
597
  return;
598
+ // The calls this message emitted belong to the input it is answering,
599
+ // whatever steers the turn before they run. Captured here rather than
600
+ // at call start: `steer()` above may already have queued a follow-up
601
+ // whose user message lands before the first call starts.
602
+ emitInputRunId = activeInputRunId;
525
603
  // The LAST assistant message with text answers its user message: a
526
604
  // narration before a tool call is superseded by the message that
527
605
  // follows the result. An EMPTY one (an aborted or errored request)
@@ -541,6 +619,13 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
541
619
  return;
542
620
  }
543
621
  if (event.type === 'tool_execution_start') {
622
+ // Bound now, read at END: a call emitted for one input may start and
623
+ // finish after a follow-up steered the turn elsewhere, and the result
624
+ // belongs to the message that initiated it. The binding is the input
625
+ // of the assistant message that emitted the call, not the live input.
626
+ // `undefined` is a real owner binding, not a missing value. Preserve
627
+ // it so a later steer cannot claim an owner tool that starts late.
628
+ toolInputRun.set(event.toolCallId, emitInputRunId);
544
629
  emit({ type: 'tool_call_start', toolCallId: event.toolCallId, name: event.toolName, args: event.args });
545
630
  return;
546
631
  }
@@ -549,18 +634,29 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
549
634
  // matching what the retired lane stamped (`recordToolUsed`, from its
550
635
  // own `tool_use` event). A failed call still counts as attempted; the
551
636
  // guardrail asks whether the tool was reached, not whether it worked.
552
- toolsUsed.add(event.toolName);
637
+ const inputRunId = toolInputRun.has(event.toolCallId)
638
+ ? toolInputRun.get(event.toolCallId)
639
+ : activeInputRunId;
640
+ toolInputRun.delete(event.toolCallId);
641
+ toolsFor(inputRunId).add(event.toolName);
553
642
  const result = event.result;
554
643
  // Summarised BEFORE the clip, from the result as the tool returned it:
555
644
  // a retrieval body over the display cap would otherwise parse to
556
645
  // nothing and the turn would carry no evidence for its own answer.
557
646
  const resultSummary = event.isError ? null : summarizeToolTrace(event.toolName, event.result);
647
+ const output = clip(joinText(result?.content));
648
+ // The first failure of each input, verbatim: the durable record of
649
+ // what actually went wrong, independent of how the model paraphrases
650
+ // it in prose.
651
+ if (event.isError)
652
+ noteToolError(inputRunId, output);
558
653
  emit({
559
654
  type: 'tool_call_end',
560
655
  toolCallId: event.toolCallId,
561
656
  name: event.toolName,
562
657
  isError: event.isError,
563
- output: clip(joinText(result?.content)),
658
+ output,
659
+ ...(inputRunId !== undefined ? { inputRunId } : {}),
564
660
  ...(resultSummary ? { resultSummary } : {}),
565
661
  });
566
662
  }
@@ -633,8 +729,7 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
633
729
  }
634
730
  if (ended)
635
731
  throw new Error(ended);
636
- if (answer)
637
- answers.push(answer);
732
+ settleAnswer();
638
733
  return {
639
734
  // The answer is the last assistant message WITH TEXT after each user
640
735
  // message, not the accumulated stream: a model that narrates before a
@@ -654,17 +749,34 @@ export async function runAgentTurn(input, emit, takeSteering = () => [], runtime
654
749
  // turn on one — so the answer that did settle is still delivered.
655
750
  //
656
751
  // Falls back to the stream when no assistant message settled: a turn
657
- // aborted mid-answer still owes the user what it managed to say.
658
- text: answers.length > 0 ? answers.join('\n\n') : text,
752
+ // aborted mid-answer still owes the user what it managed to say. The
753
+ // fallback is the owner's only: a steered input without its own settled
754
+ // answer fails below rather than borrowing a sibling's text.
755
+ text: ownerAnswer ?? ownerText,
659
756
  stopReason,
660
757
  usage,
661
758
  sessionJsonl: nativeSessionJsonl(session),
662
759
  toolsUsed: [...toolsUsed],
760
+ ...(ownerFirstError ? { firstError: ownerFirstError } : {}),
663
761
  // Pi emits message events before persistence. Receipt identity comes from
664
762
  // the finished native branch after retries/compaction have drained.
665
763
  consumedInputs: session.sessionManager.getBranch().flatMap((entry) => {
666
764
  const runId = entry.type === 'message' ? steeredMessages.get(entry.message) : undefined;
667
- return runId === undefined ? [] : [{ runId, sessionEntryId: entry.id }];
765
+ if (runId === undefined)
766
+ return [];
767
+ // Fail closed: a steered input the turn never answered must not be
768
+ // completed with another input's text. The server turns this throw
769
+ // into a failed turn, which the reaper then terminalizes honestly.
770
+ const responseText = inputAnswers.get(runId);
771
+ if (!responseText?.trim())
772
+ throw new Error(`steered input ${runId} completed without its own answer`);
773
+ return [{
774
+ runId,
775
+ sessionEntryId: entry.id,
776
+ responseText,
777
+ toolsUsed: [...(steeredToolsUsed.get(runId) ?? [])],
778
+ ...(steeredFirstErrors.get(runId) ? { firstError: steeredFirstErrors.get(runId) } : {}),
779
+ }];
668
780
  }),
669
781
  ...(repliedInBand ? { repliedInBand: true } : {}),
670
782
  };