@lobu/runtime-device 24.3.0 → 24.5.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 (96) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +66 -68
  3. package/dist/verify.mjs +1 -1
  4. package/npm-shrinkwrap.json +165 -181
  5. package/package.json +2 -2
  6. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +23 -5
  7. package/vendor/cli/dist/bundled-skills/lobu-connect/SKILL.md +6 -0
  8. package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +20 -13
  9. package/vendor/cli/dist/commands/_lib/apply/client.d.ts +48 -100
  10. package/vendor/cli/dist/commands/_lib/apply/client.js +76 -27
  11. package/vendor/cli/dist/commands/_lib/apply/deployment.js +1 -1
  12. package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +7 -11
  13. package/vendor/cli/dist/commands/_lib/apply/desired-state.js +9 -40
  14. package/vendor/cli/dist/commands/_lib/apply/diff.d.ts +4 -4
  15. package/vendor/cli/dist/commands/_lib/apply/diff.js +37 -33
  16. package/vendor/cli/dist/commands/_lib/apply/map-config.d.ts +1 -1
  17. package/vendor/cli/dist/commands/_lib/apply/map-config.js +79 -92
  18. package/vendor/cli/dist/commands/_lib/apply/shared.d.ts +3 -6
  19. package/vendor/cli/dist/commands/_lib/apply/shared.js +8 -19
  20. package/vendor/cli/dist/commands/dev.js +21 -7
  21. package/vendor/cli/dist/commands/doctor.js +1 -8
  22. package/vendor/cli/dist/commands/init.js +4 -5
  23. package/vendor/cli/dist/config/define.d.ts +27 -28
  24. package/vendor/cli/dist/config/define.js +4 -5
  25. package/vendor/cli/dist/connectors/postgres.ts +77 -48
  26. package/vendor/cli/dist/internal/api-client.d.ts +2 -0
  27. package/vendor/cli/dist/internal/api-client.js +6 -1
  28. package/vendor/cli/dist/internal/node-binary.d.ts +22 -0
  29. package/vendor/cli/dist/internal/node-binary.js +73 -0
  30. package/vendor/cli/dist/internal/node-version.d.ts +7 -12
  31. package/vendor/cli/dist/internal/node-version.js +16 -20
  32. package/vendor/cli/dist/runtime-components.json +4 -4
  33. package/vendor/cli/dist/templates/AGENTS.md.tmpl +2 -2
  34. package/vendor/cli/package.json +2 -2
  35. package/vendor/connector-sdk/dist/reaction-client-types.d.ts +8 -2
  36. package/vendor/connector-sdk/dist/reaction-sdk.d.ts +5 -1
  37. package/vendor/connector-sdk/package.json +1 -1
  38. package/vendor/connector-worker/dist/daemon/acp-adapters/claude.js +1 -1
  39. package/vendor/connector-worker/dist/daemon/agent-turn.js +69 -3
  40. package/vendor/connector-worker/dist/daemon/client.d.ts +25 -8
  41. package/vendor/connector-worker/dist/daemon/client.js +32 -4
  42. package/vendor/connector-worker/dist/daemon/log.js +2 -1
  43. package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +7 -1
  44. package/vendor/connector-worker/dist/daemon/poll-loop.d.ts +17 -0
  45. package/vendor/connector-worker/dist/daemon/poll-loop.js +101 -13
  46. package/vendor/connector-worker/dist/daemon/resource-admission.d.ts +19 -0
  47. package/vendor/connector-worker/dist/daemon/resource-admission.js +59 -0
  48. package/vendor/connector-worker/dist/daemon/resources.d.ts +20 -0
  49. package/vendor/connector-worker/dist/daemon/resources.js +163 -0
  50. package/vendor/connector-worker/dist/daemon/start.js +29 -4
  51. package/vendor/connector-worker/dist/daemon/worker.d.ts +2 -0
  52. package/vendor/connector-worker/dist/daemon/worker.js +4 -0
  53. package/vendor/connector-worker/dist/executor/select.d.ts +1 -1
  54. package/vendor/connector-worker/dist/executor/select.js +1 -1
  55. package/vendor/connector-worker/dist/isolate/ivm-types.d.ts +1 -2
  56. package/vendor/connector-worker/dist/isolate/ivm-types.js +1 -2
  57. package/vendor/connector-worker/dist/isolate/load.d.ts +10 -12
  58. package/vendor/connector-worker/dist/isolate/load.js +23 -27
  59. package/vendor/connector-worker/dist/self-check/index.d.ts +2 -2
  60. package/vendor/connector-worker/dist/self-check/index.js +2 -2
  61. package/vendor/connector-worker/package.json +3 -4
  62. package/vendor/core/dist/contracts/agent-settings.d.ts +61 -6
  63. package/vendor/core/dist/contracts/agent-settings.js +10 -7
  64. package/vendor/core/dist/contracts/tools/manage-agents.d.ts +473 -98
  65. package/vendor/core/dist/contracts/tools/manage-agents.js +111 -2
  66. package/vendor/core/dist/contracts/tools/manage-automations.d.ts +21 -18
  67. package/vendor/core/dist/contracts/tools/manage-automations.js +15 -15
  68. package/vendor/core/dist/contracts/tools/manage-entity-schema.js +2 -2
  69. package/vendor/core/dist/contracts/tools/manage-entity.d.ts +45 -20
  70. package/vendor/core/dist/contracts/tools/manage-entity.js +17 -5
  71. package/vendor/core/dist/contracts/tools/manage-inference-providers.d.ts +445 -0
  72. package/vendor/core/dist/contracts/tools/manage-inference-providers.js +133 -0
  73. package/vendor/core/dist/contracts/tools/manage-views.d.ts +4 -4
  74. package/vendor/core/dist/contracts/tools/manage-views.js +5 -3
  75. package/vendor/core/dist/contracts/worker/device-automation.js +4 -2
  76. package/vendor/core/dist/error-report.d.ts +73 -0
  77. package/vendor/core/dist/error-report.js +346 -0
  78. package/vendor/core/dist/index.d.ts +4 -1
  79. package/vendor/core/dist/index.js +7 -1
  80. package/vendor/core/dist/logger.js +15 -6
  81. package/vendor/core/dist/otel-fetch.d.ts +19 -0
  82. package/vendor/core/dist/otel-fetch.js +96 -0
  83. package/vendor/core/dist/otel.d.ts +22 -2
  84. package/vendor/core/dist/otel.js +95 -48
  85. package/vendor/core/dist/reserved.d.ts +1 -1
  86. package/vendor/core/dist/reserved.js +6 -4
  87. package/vendor/core/dist/tool-result.d.ts +8 -0
  88. package/vendor/core/dist/tool-result.js +61 -0
  89. package/vendor/core/dist/trace-context.d.ts +12 -0
  90. package/vendor/core/dist/trace-context.js +50 -0
  91. package/vendor/core/dist/utils/diagnostic-scrubber.js +1 -1
  92. package/vendor/core/dist/worker/auth.js +4 -0
  93. package/vendor/core/package.json +6 -1
  94. /package/vendor/cli/dist/vendor/ora/{.bun-tag-fde20e9a6df212fa → .bun-tag-77bb9b258a474a18} +0 -0
  95. /package/vendor/ora/{.bun-tag-fde20e9a6df212fa → .bun-tag-77bb9b258a474a18} +0 -0
  96. /package/vendor/postgres/{.bun-tag-22d644c2873790db → .bun-tag-c81dacd34e959a7a} +0 -0
@@ -1,43 +1,39 @@
1
1
  /**
2
2
  * Single source of truth for the CLI's Node-version policy.
3
3
  *
4
- * Lobu's SDK sandbox (query_sdk / run_sdk) uses isolated-vm, whose native addon
5
- * is tied to each Node line's V8 ABI: isolated-vm@6 covers Node 22–24, the
6
- * aliased isolated-vm@7 covers Node 26+. Node 25 is an EOL non-LTS line upstream
7
- * skipped, so it boots but has no sandbox build. Below 22 nothing works — the
8
- * bundled server's deps (undici) reference globals absent on
9
- * old Node and crash on load.
4
+ * Lobu's gateway and worker load isolated-vm, whose native addon is tied to a
5
+ * single Node major's V8 ABI. This repo ships one isolated-vm 7 build and runs
6
+ * it on Node 26 only; Node 27 and later need a matching addon build before they
7
+ * can be supported.
10
8
  *
11
- * The `bin/lobu.js` entrypoint carries an inline copy of the MIN threshold
12
- * because it must run with zero imports (before the bundle loads); keep it and
9
+ * The `bin/lobu.js` entrypoint carries an inline copy of the same major because
10
+ * it must run with zero imports (before the bundle loads); keep it and
13
11
  * packages/server/src/utils/assert-node-version.ts in sync with this file.
14
12
  */
15
- export const MIN_NODE_MAJOR = 22;
16
- const SANDBOX_GAP_NODE_MAJOR = 25;
13
+ export const MIN_NODE_MAJOR = 26;
14
+ const MAX_NODE_MAJOR_EXCLUSIVE = 27;
17
15
  function currentNodeMajor() {
18
16
  return Number.parseInt(process.versions.node.split(".")[0] ?? "", 10);
19
17
  }
20
- /** True when isolated-vm has a build for this Node major (sandbox works). */
21
- export function sandboxAvailableForNode(major) {
22
- return ((major >= MIN_NODE_MAJOR && major < SANDBOX_GAP_NODE_MAJOR) || major >= 26);
23
- }
24
18
  /**
25
- * One-line explanation of the floor, so user-facing messages say WHY 22 —
19
+ * One-line explanation of the floor, so user-facing messages say WHY 26 —
26
20
  * not a bare "unsupported" that reads as arbitrary.
27
21
  */
28
- const NODE_FLOOR_REASON = `Lobu runs agent code in an isolated-vm sandbox whose native builds only ` +
29
- `cover Node ${MIN_NODE_MAJOR}–24 and 26+, so ${MIN_NODE_MAJOR} is the minimum.`;
22
+ const NODE_FLOOR_REASON = `Lobu runs agent code in an isolated-vm sandbox whose native build covers ` +
23
+ `Node ${MIN_NODE_MAJOR}.x only.`;
30
24
  /** Classify the running Node against Lobu's support policy. */
31
25
  export function checkNodeSupport() {
32
26
  const current = process.versions.node;
33
27
  const major = currentNodeMajor();
34
- if (!Number.isFinite(major) || major < MIN_NODE_MAJOR) {
28
+ if (!Number.isFinite(major) ||
29
+ major < MIN_NODE_MAJOR ||
30
+ major >= MAX_NODE_MAJOR_EXCLUSIVE) {
35
31
  return {
36
32
  ok: false,
37
- reason: `Lobu needs Node.js ${MIN_NODE_MAJOR} or newer — you're on Node ${current}.`,
33
+ reason: `Lobu needs Node.js ${MIN_NODE_MAJOR}.x — you're on Node ${current}.`,
38
34
  why: NODE_FLOOR_REASON,
39
35
  };
40
36
  }
41
- return { ok: true, sandbox: sandboxAvailableForNode(major) };
37
+ return { ok: true };
42
38
  }
43
39
  //# sourceMappingURL=node-version.js.map
@@ -5,7 +5,7 @@
5
5
  "server": "dist/server.bundle.mjs",
6
6
  "embeddingsServer": "dist/embeddings-server.mjs"
7
7
  },
8
- "version": "24.3.0",
8
+ "version": "24.5.0",
9
9
  "verify": "dist/verify.mjs"
10
10
  },
11
11
  "device": {
@@ -16,7 +16,7 @@
16
16
  "connector": "node_modules/@lobu/cli/dist/commands/connector.js",
17
17
  "worker": "node_modules/@lobu/connector-worker/dist/bin.js"
18
18
  },
19
- "version": "24.3.0",
19
+ "version": "24.5.0",
20
20
  "verify": "dist/verify.mjs"
21
21
  },
22
22
  "postgres": {
@@ -24,7 +24,7 @@
24
24
  "entries": {
25
25
  "postgres": "dist/index.mjs"
26
26
  },
27
- "version": "24.3.0",
27
+ "version": "24.5.0",
28
28
  "verify": "dist/verify.mjs"
29
29
  },
30
30
  "embeddings": {
@@ -32,7 +32,7 @@
32
32
  "entries": {
33
33
  "embeddings": "dist/embeddings.js"
34
34
  },
35
- "version": "24.3.0",
35
+ "version": "24.5.0",
36
36
  "verify": "dist/verify.mjs"
37
37
  }
38
38
  }
@@ -44,8 +44,8 @@ or public domain from a personal email address.
44
44
 
45
45
  ## Prerequisites
46
46
 
47
- Before `lobu run`, the host needs Node 22-24 or 26+ (only Node 25 is unsupported —
48
- `isolated-vm` skips that EOL non-LTS line) and one LLM provider credential in
47
+ Before `lobu run`, the host needs Node 26.x — the gateway and worker load the
48
+ Node-26-only `isolated-vm` native build — and one LLM provider credential in
49
49
  `.env`.
50
50
  Postgres is built in: `lobu run`
51
51
  starts an embedded PostgreSQL (with `pgvector`); set `DATABASE_URL` only to use an
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobu/cli",
3
- "version": "24.3.0",
3
+ "version": "24.5.0",
4
4
  "description": "CLI for deploying and managing AI agents on Lobu",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -56,6 +56,6 @@
56
56
  "dist"
57
57
  ],
58
58
  "engines": {
59
- "node": ">=22 <25 || >=26"
59
+ "node": ">=26 <27"
60
60
  }
61
61
  }
@@ -195,8 +195,14 @@ export interface ReactionClient {
195
195
  run_id?: number;
196
196
  }>;
197
197
  };
198
- /** Run a read-only SQL query against the org's Postgres. */
199
- query(sql: string): Promise<unknown[]>;
198
+ /**
199
+ * Run a read-only SQL query against the org's Postgres, or live against a
200
+ * connection's source when `options.connection` names one.
201
+ */
202
+ query(sql: string, options?: {
203
+ window_token?: string;
204
+ connection?: string;
205
+ }): Promise<unknown[]>;
200
206
  /** Structured log — appears in the Automation run output. */
201
207
  log(message: string, data?: Record<string, unknown>): void;
202
208
  }
@@ -14,9 +14,13 @@ export interface ReactionEntity {
14
14
  /**
15
15
  * Context passed to reaction scripts containing the analysis results
16
16
  * and metadata about the completed Automation run. Reaction scripts have the
17
- * shape `default async (ctx: ReactionContext, client, params?)`.
17
+ * shape `default async (ctx: ReactionContext, client, params?)`. The same
18
+ * module may export `prepare(ctx: AutomationScriptContext, client)` returning
19
+ * named arrays of records, saved before the run's agent is dispatched.
18
20
  */
19
21
  export interface ReactionContext {
22
+ /** Named sources prepared and saved before this run's agent execution. */
23
+ sources?: Record<string, unknown[]>;
20
24
  /** The extracted analysis data from the completed run */
21
25
  extracted_data: Record<string, unknown>;
22
26
  /** All entities the Automation is attached to */
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobu/connector-sdk",
3
- "version": "24.3.0",
3
+ "version": "24.5.0",
4
4
  "description": "Lobu memory SDK — build pluggable connectors for Lobu memory",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -5,7 +5,7 @@ var __export = (target, all) => {
5
5
  __defProp(target, name, { get: all[name], enumerable: true });
6
6
  };
7
7
 
8
- // ../../node_modules/@anthropic-ai/claude-agent-sdk/sdk.mjs
8
+ // ../../node_modules/@agentclientprotocol/claude-agent-acp/node_modules/@anthropic-ai/claude-agent-sdk/sdk.mjs
9
9
  import { createRequire as j5 } from "node:module";
10
10
  import * as nr from "node:fs/promises";
11
11
  import * as bn from "node:path";
@@ -7,6 +7,7 @@
7
7
  * What this module adds is only the envelope: the guest bundle, the allowlist,
8
8
  * and reporting the result.
9
9
  */
10
+ import { classifyErrorMessage, createChildSpan, initTracing, recordError, runInSpanContext, SpanStatusCode, traceparentHeader, } from '@lobu/core';
10
11
  import { AGENT_TURN_BRIDGE_BYTES, AGENT_TURN_INPUT_MAX, TURN_DELTA_MAX_CHARS, TURN_TOOL_EVENT_QUEUE_MAX, } from '@lobu/core/contracts/worker/protocol';
11
12
  import { agentGuestBundle } from '../agent-turn/bundle.js';
12
13
  import { selectExecutor } from '../executor/select.js';
@@ -24,6 +25,13 @@ import { completeAgentTurnOnce } from './terminal-delivery.js';
24
25
  * the same request the liveness beat would have made.
25
26
  */
26
27
  const TURN_DELTA_FLUSH_MS = 400;
28
+ /**
29
+ * Heap cap for one agent turn's isolate. The fleet runs many turns beside each
30
+ * other, so the connector default (512 MB) would let a few runaway turns take
31
+ * the whole pod. The worst measured turn (history at its 4 MB cap) peaked at a
32
+ * 63 MB heap; a light one at 15 MB. 256 MB is four times that worst case.
33
+ */
34
+ const AGENT_TURN_ISOLATE_MEMORY_MB = 256;
27
35
  /**
28
36
  * How many batches the pre-completion drain will push before giving up.
29
37
  *
@@ -36,7 +44,63 @@ const TURN_DELTA_DRAIN_BATCHES = 4;
36
44
  function isAgentTurnPayload(value) {
37
45
  return !!value && typeof value === 'object' && 'turn' in value;
38
46
  }
47
+ function ensureWorkerTracing() {
48
+ const otlpEndpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT?.trim();
49
+ if (!otlpEndpoint)
50
+ return;
51
+ initTracing({
52
+ serviceName: 'lobu-worker',
53
+ serviceVersion: process.env.APP_GIT_SHA,
54
+ otlpEndpoint,
55
+ });
56
+ }
39
57
  export async function executeAgentTurnRun(client, job, env, cfg) {
58
+ let span = null;
59
+ try {
60
+ ensureWorkerTracing();
61
+ const traceparent = traceparentHeader(job.traceparent);
62
+ if (traceparent) {
63
+ span = createChildSpan('agent_turn', traceparent, {
64
+ 'lobu.run_id': String(job.run_id ?? ''),
65
+ });
66
+ }
67
+ }
68
+ catch (error) {
69
+ // Telemetry is optional. A missing exporter must not fail a claimed turn.
70
+ log.info('[agent-turn] Tracing unavailable; continuing without a span:', error);
71
+ }
72
+ try {
73
+ const result = await runInSpanContext(span, () => runAgentTurn(client, job, env, cfg));
74
+ // The gateway's typed receipt is authoritative, including a cancellation
75
+ // that raced the worker's completion. Keep this internal to tracing.
76
+ const { terminalStatus, ...outcome } = result;
77
+ const status = terminalStatus ?? (outcome.error ? 'failed' : 'completed');
78
+ span?.setAttribute('lobu.run_status', status);
79
+ if (status === 'failed') {
80
+ // Classify the failure text once, here, so it groups and routes by its
81
+ // catalog code rather than by prose.
82
+ const message = outcome.error ?? 'Agent turn failed';
83
+ recordError(span, {
84
+ name: 'AgentTurnError',
85
+ message,
86
+ code: classifyErrorMessage(message),
87
+ });
88
+ }
89
+ else if (status !== 'cancelled') {
90
+ span?.setStatus({ code: SpanStatusCode.OK });
91
+ }
92
+ return outcome;
93
+ }
94
+ catch (error) {
95
+ span?.setAttribute('lobu.run_status', 'failed');
96
+ recordError(span, error);
97
+ throw error;
98
+ }
99
+ finally {
100
+ span?.end();
101
+ }
102
+ }
103
+ async function runAgentTurn(client, job, env, cfg) {
40
104
  const runId = job.run_id;
41
105
  if (!runId)
42
106
  return { itemsCollected: 0, error: 'agent turn run missing its run id' };
@@ -58,7 +122,7 @@ export async function executeAgentTurnRun(client, job, env, cfg) {
58
122
  ...(trailing?.length ? { turn_tool_events: trailing } : {}),
59
123
  exit_reason: 'error_message',
60
124
  });
61
- return { itemsCollected: 0, ...(receipt.status === 'completed' ? {} : { error }) };
125
+ return { itemsCollected: 0, terminalStatus: receipt.status, ...(receipt.status === 'completed' ? {} : { error }) };
62
126
  }
63
127
  catch (deliveryError) {
64
128
  // The outer executor must not replace this failure's evidence with a
@@ -343,6 +407,7 @@ export async function executeAgentTurnRun(client, job, env, cfg) {
343
407
  // attachment payload and stored history separately; the connector
344
408
  // default is sized for a connector payload, not for that.
345
409
  messageBytes: AGENT_TURN_BRIDGE_BYTES,
410
+ memoryMb: AGENT_TURN_ISOLATE_MEMORY_MB,
346
411
  }));
347
412
  const result = await executor.execute(guestCode, {
348
413
  mode: 'agent_turn',
@@ -609,8 +674,9 @@ export async function executeAgentTurnRun(client, job, env, cfg) {
609
674
  ...(result.turn.suggestions ? { suggestions: result.turn.suggestions } : {}),
610
675
  exit_reason: 'ok',
611
676
  });
612
- if (receipt.status !== 'completed')
613
- return { itemsCollected: 0, error: `agent turn ${receipt.status}` };
677
+ if (receipt.status !== 'completed') {
678
+ return { itemsCollected: 0, error: `agent turn ${receipt.status}`, terminalStatus: receipt.status };
679
+ }
614
680
  log.info(`[agent-turn] run ${runId} completed after ${deltaSequence} delta batches and ${toolCalls} tool calls`);
615
681
  return { itemsCollected: 0 };
616
682
  }
@@ -12,10 +12,7 @@ export declare function trimTrailingSlashes(value: string): string;
12
12
  */
13
13
  export interface ExecutorClient {
14
14
  readonly id: string;
15
- poll(capacityAvailable?: number, options?: {
16
- waitSeconds?: number;
17
- signal?: AbortSignal;
18
- }): Promise<PollResponse>;
15
+ poll(capacityAvailable?: number, options?: WorkerPollOptions): Promise<PollResponse>;
19
16
  /**
20
17
  * Beat, and read what the gateway says back. The response is the only channel
21
18
  * into a run this worker already holds: `continue: false` means stop.
@@ -75,6 +72,22 @@ import type { ConnectorOperationResult } from "@lobu/core/contracts/tools/manage
75
72
  import type { AgentKind } from "@lobu/core/contracts/worker/device-automation";
76
73
  /** Capability strings the worker advertises, keyed by name (e.g. `browser.debugger`). */
77
74
  export type WorkerCapabilities = Record<string, boolean>;
75
+ /**
76
+ * Free slots per budget, for a worker that budgets agent turns apart from
77
+ * connector work. The poll body already says this without a new field:
78
+ * `agent_turn` is withdrawn while no turn slot is free, and the free connector
79
+ * slots are the compiled-connector backend capacity, which is what every fleet
80
+ * sync, action and auth claim requires.
81
+ */
82
+ export interface PollLaneCapacity {
83
+ agentTurns: number;
84
+ connectorJobs: number;
85
+ }
86
+ export interface WorkerPollOptions {
87
+ waitSeconds?: number;
88
+ signal?: AbortSignal;
89
+ lanes?: PollLaneCapacity;
90
+ }
78
91
  export interface WorkerAdvertisementSnapshot {
79
92
  capabilities: WorkerCapabilities;
80
93
  manifests: unknown[];
@@ -179,13 +192,17 @@ export declare class WorkerClient implements ExecutorClient {
179
192
  private post;
180
193
  private requestJson;
181
194
  private requestVoid;
195
+ /**
196
+ * Report free connector slots as compiled-connector capacity, but only for a
197
+ * backend that is ready: a closed backend stays at zero. A worker that sends
198
+ * no map is read by the gateway as `defaultBackendCapacity()`, so start from
199
+ * that.
200
+ */
201
+ private laneBackendCapacity;
182
202
  /**
183
203
  * Poll for available runs
184
204
  */
185
- poll(capacityAvailable?: number, options?: {
186
- waitSeconds?: number;
187
- signal?: AbortSignal;
188
- }): Promise<PollResponse>;
205
+ poll(capacityAvailable?: number, options?: WorkerPollOptions): Promise<PollResponse>;
189
206
  /**
190
207
  * Send heartbeat for active run
191
208
  */
@@ -10,6 +10,8 @@ export function trimTrailingSlashes(value) {
10
10
  end--;
11
11
  return end === value.length ? value : value.slice(0, end);
12
12
  }
13
+ import { traceparentHeader } from "@lobu/core";
14
+ import { defaultBackendCapacity, EXECUTION_BACKENDS, } from "@lobu/core/contracts/worker/protocol";
13
15
  import { resolveRunnableAgentKinds } from "./agent-binaries.js";
14
16
  export class MutableWorkerAdvertisementProvider {
15
17
  current;
@@ -204,14 +206,35 @@ export class WorkerClient {
204
206
  async requestVoid(path, body) {
205
207
  await this.post(path, body);
206
208
  }
209
+ /**
210
+ * Report free connector slots as compiled-connector capacity, but only for a
211
+ * backend that is ready: a closed backend stays at zero. A worker that sends
212
+ * no map is read by the gateway as `defaultBackendCapacity()`, so start from
213
+ * that.
214
+ */
215
+ laneBackendCapacity(connectorJobs) {
216
+ const compiled = EXECUTION_BACKENDS.compiledConnector;
217
+ const base = Object.keys(this.backendCapacity).length > 0
218
+ ? this.backendCapacity
219
+ : defaultBackendCapacity();
220
+ return { ...base, [compiled]: (base[compiled] ?? 0) > 0 ? connectorJobs : 0 };
221
+ }
207
222
  /**
208
223
  * Poll for available runs
209
224
  */
210
225
  async poll(capacityAvailable, options) {
211
226
  const advertisement = this.advertisementProvider?.snapshot();
212
- const capabilities = advertisement
227
+ const advertised = advertisement
213
228
  ? { ...this.advertisedCapabilities(), ...advertisement.capabilities }
214
229
  : this.advertisedCapabilities();
230
+ const lanes = options?.lanes;
231
+ // Budgets only ever withdraw what was advertised; they never open a lane.
232
+ const capabilities = lanes && advertised.agent_turn === true && lanes.agentTurns === 0
233
+ ? { ...advertised, agent_turn: false }
234
+ : advertised;
235
+ const backendCapacity = lanes
236
+ ? this.laneBackendCapacity(lanes.connectorJobs)
237
+ : this.backendCapacity;
215
238
  const manifests = advertisement?.manifests ?? this.manifests;
216
239
  const body = {
217
240
  worker_id: this.workerId,
@@ -221,8 +244,8 @@ export class WorkerClient {
221
244
  ...(capacityAvailable === undefined
222
245
  ? {}
223
246
  : { capacity_available: capacityAvailable }),
224
- ...(Object.keys(this.backendCapacity).length > 0
225
- ? { backend_capacity: this.backendCapacity }
247
+ ...(Object.keys(backendCapacity).length > 0
248
+ ? { backend_capacity: backendCapacity }
226
249
  : {}),
227
250
  // app_version belongs to the device registration fields, so omit both for
228
251
  // fleet workers rather than sending empty values. A device worker also
@@ -240,7 +263,12 @@ export class WorkerClient {
240
263
  : {}),
241
264
  };
242
265
  try {
243
- return await this.requestJson('/api/workers/poll', body, options?.signal);
266
+ const response = await this.post('/api/workers/poll', body, options?.signal);
267
+ const job = (await response.json());
268
+ const traceparent = traceparentHeader(response.headers.get('traceparent'));
269
+ if (traceparent)
270
+ job.traceparent = traceparent;
271
+ return job;
244
272
  }
245
273
  catch (error) {
246
274
  const description = error instanceof WorkerHttpError ? upgradeRequiredDescription(error) : null;
@@ -6,7 +6,7 @@
6
6
  * the run's start and its terminal result — plus startup/shutdown and hard
7
7
  * failures. Pass `--debug` to enable the poll/heartbeat/retry chatter.
8
8
  */
9
- import { scrubDiagnosticValue } from '@lobu/core';
9
+ import { currentTraceLogFields, scrubDiagnosticValue } from '@lobu/core';
10
10
  let debugEnabled = false;
11
11
  function write(level, parts) {
12
12
  let line;
@@ -14,6 +14,7 @@ function write(level, parts) {
14
14
  const record = scrubDiagnosticValue({
15
15
  timestamp: new Date().toISOString(), level, service: 'lobu-worker',
16
16
  release: process.env.APP_GIT_SHA,
17
+ ...currentTraceLogFields(),
17
18
  message: parts.filter((part) => typeof part === 'string').join(' '),
18
19
  data: parts.filter((part) => typeof part !== 'string'),
19
20
  });
@@ -268,7 +268,13 @@ export function createMacDeviceDaemon(options, dependencies = {}) {
268
268
  sessionMeta: {
269
269
  claudeCode: {
270
270
  options: {
271
- allowedTools: ['mcp__lobu__query_sdk', 'mcp__lobu__run_sdk'],
271
+ // Discovery is read-only and must work before a run can use the SDK.
272
+ // Keep in sync with the headless `--allowedTools` list in core's DEVICE_AGENT_SPECS.
273
+ allowedTools: [
274
+ 'mcp__lobu__search_sdk',
275
+ 'mcp__lobu__query_sdk',
276
+ 'mcp__lobu__run_sdk',
277
+ ],
272
278
  settingSources: [],
273
279
  },
274
280
  },
@@ -6,6 +6,7 @@
6
6
  * injected so the device build does not import fleet connector machinery.
7
7
  */
8
8
  import { type PollResponse, type WorkerClient } from './client.js';
9
+ import { type ResourceAdmission } from './resource-admission.js';
9
10
  export interface WorkerPollLoopOptions {
10
11
  client: WorkerClient;
11
12
  pollIntervalMs?: number;
@@ -16,6 +17,14 @@ export interface WorkerPollLoopOptions {
16
17
  */
17
18
  maxIdleDelayMs?: number;
18
19
  maxConcurrentJobs?: number;
20
+ /**
21
+ * Give agent turns their own budget. `maxConcurrentJobs` then budgets only
22
+ * connector work (syncs, actions, auth, embedding backfills). Unset keeps
23
+ * the single shared budget that device daemons and the embedded worker use.
24
+ */
25
+ maxConcurrentAgentTurns?: number;
26
+ /** Fleet-local resource gate; omitted for device and embedded workers. */
27
+ resourceAdmission?: ResourceAdmission;
19
28
  execute: (job: PollResponse) => Promise<unknown>;
20
29
  beforeIdlePoll?: () => Promise<void>;
21
30
  /** Persisted device credentials treat poll-time revocation as terminal. */
@@ -34,6 +43,8 @@ export declare class WorkerPollLoop {
34
43
  private readonly pollIntervalMs;
35
44
  private readonly maxIdleDelayMs?;
36
45
  private readonly maxConcurrentJobs;
46
+ private readonly maxConcurrentAgentTurns?;
47
+ private readonly resourceAdmission?;
37
48
  private readonly execute;
38
49
  private readonly beforeIdlePoll?;
39
50
  private readonly failClosedOnPollAuthError;
@@ -41,13 +52,19 @@ export declare class WorkerPollLoop {
41
52
  private running;
42
53
  private admittingJobs;
43
54
  private activeJobs;
55
+ /** Runs charged to the agent-turn budget; only counted when it is set. */
56
+ private activeAgentTurns;
44
57
  private capacityVersion;
45
58
  private wakePoll?;
46
59
  private pendingPoll?;
60
+ private lastPollAt;
47
61
  constructor(options: WorkerPollLoopOptions);
48
62
  start(): Promise<void>;
49
63
  stop(): void;
50
64
  waitForActiveJobs(timeoutMs?: number, pollMs?: number): Promise<boolean>;
65
+ /** Free slots per budget, or undefined while every run shares one budget. */
66
+ private laneCapacity;
67
+ private freeSlots;
51
68
  private pollAndExecute;
52
69
  private waitForNextPoll;
53
70
  private sleep;