@lobu/runtime-device 24.4.0 → 24.6.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 (113) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +77 -64
  3. package/dist/verify.mjs +1 -1
  4. package/npm-shrinkwrap.json +176 -141
  5. package/package.json +3 -2
  6. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +65 -11
  7. package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +20 -13
  8. package/vendor/cli/dist/commands/_lib/apply/client.d.ts +48 -100
  9. package/vendor/cli/dist/commands/_lib/apply/client.js +76 -27
  10. package/vendor/cli/dist/commands/_lib/apply/deployment.js +1 -1
  11. package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +7 -11
  12. package/vendor/cli/dist/commands/_lib/apply/desired-state.js +9 -40
  13. package/vendor/cli/dist/commands/_lib/apply/diff.d.ts +4 -4
  14. package/vendor/cli/dist/commands/_lib/apply/diff.js +37 -33
  15. package/vendor/cli/dist/commands/_lib/apply/map-config.d.ts +1 -1
  16. package/vendor/cli/dist/commands/_lib/apply/map-config.js +79 -92
  17. package/vendor/cli/dist/commands/_lib/apply/shared.d.ts +3 -6
  18. package/vendor/cli/dist/commands/_lib/apply/shared.js +8 -19
  19. package/vendor/cli/dist/commands/_lib/view-bundler.js +9 -1
  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/linkedin-identity.ts +43 -0
  26. package/vendor/cli/dist/connectors/linkedin.ts +154 -3
  27. package/vendor/cli/dist/connectors/postgres.ts +77 -48
  28. package/vendor/cli/dist/connectors/x-like-follow.ts +329 -0
  29. package/vendor/cli/dist/connectors/x.ts +709 -80
  30. package/vendor/cli/dist/internal/api-client.d.ts +2 -0
  31. package/vendor/cli/dist/internal/api-client.js +6 -1
  32. package/vendor/cli/dist/internal/connector-compiler.js +3 -1
  33. package/vendor/cli/dist/internal/node-binary.d.ts +22 -0
  34. package/vendor/cli/dist/internal/node-binary.js +73 -0
  35. package/vendor/cli/dist/internal/node-version.d.ts +7 -12
  36. package/vendor/cli/dist/internal/node-version.js +16 -20
  37. package/vendor/cli/dist/runtime-components.json +4 -4
  38. package/vendor/cli/dist/templates/AGENTS.md.tmpl +2 -2
  39. package/vendor/cli/package.json +3 -2
  40. package/vendor/connector-sdk/dist/reaction-client-types.d.ts +8 -2
  41. package/vendor/connector-sdk/dist/reaction-sdk.d.ts +5 -1
  42. package/vendor/connector-sdk/package.json +1 -1
  43. package/vendor/connector-worker/dist/compile/index.d.ts +4 -3
  44. package/vendor/connector-worker/dist/compile/index.js +6 -3
  45. package/vendor/connector-worker/dist/daemon/acp-adapters/claude.js +1 -1
  46. package/vendor/connector-worker/dist/daemon/agent-turn.js +21 -10
  47. package/vendor/connector-worker/dist/daemon/client.d.ts +25 -8
  48. package/vendor/connector-worker/dist/daemon/client.js +25 -3
  49. package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +7 -1
  50. package/vendor/connector-worker/dist/daemon/poll-loop.d.ts +17 -0
  51. package/vendor/connector-worker/dist/daemon/poll-loop.js +75 -6
  52. package/vendor/connector-worker/dist/daemon/resource-admission.d.ts +19 -0
  53. package/vendor/connector-worker/dist/daemon/resource-admission.js +59 -0
  54. package/vendor/connector-worker/dist/daemon/resources.d.ts +20 -0
  55. package/vendor/connector-worker/dist/daemon/resources.js +163 -0
  56. package/vendor/connector-worker/dist/daemon/start.js +29 -4
  57. package/vendor/connector-worker/dist/daemon/worker.d.ts +2 -0
  58. package/vendor/connector-worker/dist/daemon/worker.js +4 -0
  59. package/vendor/connector-worker/dist/executor/select.d.ts +1 -1
  60. package/vendor/connector-worker/dist/executor/select.js +1 -1
  61. package/vendor/connector-worker/dist/isolate/ivm-types.d.ts +1 -2
  62. package/vendor/connector-worker/dist/isolate/ivm-types.js +1 -2
  63. package/vendor/connector-worker/dist/isolate/load.d.ts +10 -12
  64. package/vendor/connector-worker/dist/isolate/load.js +23 -27
  65. package/vendor/connector-worker/dist/self-check/index.d.ts +2 -2
  66. package/vendor/connector-worker/dist/self-check/index.js +2 -2
  67. package/vendor/connector-worker/package.json +3 -4
  68. package/vendor/core/dist/contracts/agent-settings.d.ts +61 -6
  69. package/vendor/core/dist/contracts/agent-settings.js +10 -7
  70. package/vendor/core/dist/contracts/tools/manage-agents.d.ts +473 -98
  71. package/vendor/core/dist/contracts/tools/manage-agents.js +111 -2
  72. package/vendor/core/dist/contracts/tools/manage-automations.d.ts +21 -18
  73. package/vendor/core/dist/contracts/tools/manage-automations.js +15 -15
  74. package/vendor/core/dist/contracts/tools/manage-entity-schema.js +2 -2
  75. package/vendor/core/dist/contracts/tools/manage-inference-providers.d.ts +445 -0
  76. package/vendor/core/dist/contracts/tools/manage-inference-providers.js +133 -0
  77. package/vendor/core/dist/contracts/tools/manage-views.d.ts +4 -4
  78. package/vendor/core/dist/contracts/tools/manage-views.js +5 -3
  79. package/vendor/core/dist/contracts/tools/view-host.d.ts +7 -0
  80. package/vendor/core/dist/contracts/tools/view-host.js +28 -0
  81. package/vendor/core/dist/contracts/tools/view-runtime.d.ts +2 -4
  82. package/vendor/core/dist/contracts/tools/view-runtime.js +19 -7
  83. package/vendor/core/dist/contracts/worker/device-automation.js +4 -2
  84. package/vendor/core/dist/error-report.d.ts +73 -0
  85. package/vendor/core/dist/error-report.js +346 -0
  86. package/vendor/core/dist/index.d.ts +3 -1
  87. package/vendor/core/dist/index.js +4 -1
  88. package/vendor/core/dist/logger.js +7 -4
  89. package/vendor/core/dist/otel-fetch.d.ts +19 -0
  90. package/vendor/core/dist/otel-fetch.js +96 -0
  91. package/vendor/core/dist/otel.d.ts +9 -0
  92. package/vendor/core/dist/otel.js +28 -7
  93. package/vendor/core/dist/reserved.d.ts +1 -1
  94. package/vendor/core/dist/reserved.js +6 -4
  95. package/vendor/core/dist/tool-result.d.ts +8 -0
  96. package/vendor/core/dist/tool-result.js +61 -0
  97. package/vendor/core/dist/utils/diagnostic-scrubber.js +1 -1
  98. package/vendor/core/dist/worker/auth.js +4 -0
  99. package/vendor/core/package.json +16 -1
  100. package/vendor/views/dist/api.d.ts +290 -0
  101. package/vendor/views/dist/api.js +714 -0
  102. package/vendor/views/dist/bridge.d.ts +157 -0
  103. package/vendor/views/dist/bridge.js +372 -0
  104. package/vendor/views/dist/components.d.ts +172 -0
  105. package/vendor/views/dist/components.js +376 -0
  106. package/vendor/views/dist/index.d.ts +13 -0
  107. package/vendor/views/dist/index.js +3 -0
  108. package/vendor/views/dist/native.d.ts +327 -0
  109. package/vendor/views/dist/native.js +57 -0
  110. package/vendor/views/package.json +53 -0
  111. /package/vendor/cli/dist/vendor/ora/{.bun-tag-fde20e9a6df212fa → .bun-tag-77bb9b258a474a18} +0 -0
  112. /package/vendor/ora/{.bun-tag-fde20e9a6df212fa → .bun-tag-77bb9b258a474a18} +0 -0
  113. /package/vendor/postgres/{.bun-tag-22d644c2873790db → .bun-tag-c81dacd34e959a7a} +0 -0
@@ -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;
@@ -9,6 +9,7 @@ import { shutdownTracing } from '@lobu/core';
9
9
  import { WORKER_POLL_MAX_WAIT_SECONDS } from '@lobu/core/contracts/worker/protocol';
10
10
  import { WorkerHttpError, WorkerUpgradeRequiredError, } from './client.js';
11
11
  import { log } from './log.js';
12
+ import { RESOURCE_ADMISSION_INTERVAL_MS } from './resource-admission.js';
12
13
  /** Drain budget for a daemon nobody told how long it has (devices, local runs). */
13
14
  const DEFAULT_SHUTDOWN_DRAIN_MS = 30_000;
14
15
  /**
@@ -42,6 +43,8 @@ export class WorkerPollLoop {
42
43
  pollIntervalMs;
43
44
  maxIdleDelayMs;
44
45
  maxConcurrentJobs;
46
+ maxConcurrentAgentTurns;
47
+ resourceAdmission;
45
48
  execute;
46
49
  beforeIdlePoll;
47
50
  failClosedOnPollAuthError;
@@ -49,9 +52,12 @@ export class WorkerPollLoop {
49
52
  running = false;
50
53
  admittingJobs = true;
51
54
  activeJobs = 0;
55
+ /** Runs charged to the agent-turn budget; only counted when it is set. */
56
+ activeAgentTurns = 0;
52
57
  capacityVersion = 0;
53
58
  wakePoll;
54
59
  pendingPoll;
60
+ lastPollAt = -Infinity;
55
61
  constructor(options) {
56
62
  this.client = options.client;
57
63
  this.pollIntervalMs = options.pollIntervalMs ?? 10000;
@@ -59,7 +65,11 @@ export class WorkerPollLoop {
59
65
  ? undefined
60
66
  : Math.max(1, options.maxIdleDelayMs);
61
67
  this.maxConcurrentJobs = Math.max(1, options.maxConcurrentJobs ?? 1);
68
+ this.maxConcurrentAgentTurns = options.maxConcurrentAgentTurns === undefined
69
+ ? undefined
70
+ : Math.max(1, options.maxConcurrentAgentTurns);
62
71
  this.execute = options.execute;
72
+ this.resourceAdmission = options.resourceAdmission;
63
73
  this.beforeIdlePoll = options.beforeIdlePoll;
64
74
  this.failClosedOnPollAuthError = options.failClosedOnPollAuthError === true;
65
75
  this.onActiveJobsChange = options.onActiveJobsChange;
@@ -74,7 +84,9 @@ export class WorkerPollLoop {
74
84
  }
75
85
  if (!this.admittingJobs)
76
86
  return;
77
- log.info(`[daemon] Starting worker daemon (max concurrent jobs: ${this.maxConcurrentJobs})...`);
87
+ log.info(this.maxConcurrentAgentTurns === undefined
88
+ ? `[daemon] Starting worker daemon (max concurrent jobs: ${this.maxConcurrentJobs})...`
89
+ : `[daemon] Starting worker daemon (max concurrent connector jobs: ${this.maxConcurrentJobs}, agent turns: ${this.maxConcurrentAgentTurns})...`);
78
90
  this.running = true;
79
91
  while (this.running) {
80
92
  if (this.activeJobs === 0)
@@ -135,19 +147,55 @@ export class WorkerPollLoop {
135
147
  log.debug('[daemon] All active jobs completed');
136
148
  return true;
137
149
  }
150
+ /** Free slots per budget, or undefined while every run shares one budget. */
151
+ laneCapacity() {
152
+ if (this.maxConcurrentAgentTurns === undefined)
153
+ return undefined;
154
+ return {
155
+ agentTurns: Math.max(0, this.maxConcurrentAgentTurns - this.activeAgentTurns),
156
+ connectorJobs: Math.max(0, this.maxConcurrentJobs - (this.activeJobs - this.activeAgentTurns)),
157
+ };
158
+ }
159
+ freeSlots(lanes) {
160
+ return lanes
161
+ ? lanes.agentTurns + lanes.connectorJobs
162
+ : Math.max(0, this.maxConcurrentJobs - this.activeJobs);
163
+ }
138
164
  async pollAndExecute() {
139
165
  if (!this.admittingJobs)
140
166
  return undefined;
141
- const capacityAvailable = Math.max(0, this.maxConcurrentJobs - this.activeJobs);
167
+ const budgetLanes = this.laneCapacity();
168
+ const resourceCapacity = this.resourceAdmission?.capacity();
169
+ // Recheck local pressure frequently without turning that cadence into
170
+ // gateway heartbeats. A recovered worker can claim immediately; a paused
171
+ // one still reports liveness at the normal polling interval.
172
+ if (resourceCapacity === 0 && performance.now() - this.lastPollAt < this.pollIntervalMs) {
173
+ return RESOURCE_ADMISSION_INTERVAL_MS;
174
+ }
175
+ const lanes = resourceCapacity === 0 && budgetLanes
176
+ ? { agentTurns: 0, connectorJobs: 0 }
177
+ : budgetLanes;
178
+ const capacityAvailable = Math.min(this.freeSlots(lanes), resourceCapacity ?? Infinity);
142
179
  const controller = new AbortController();
143
180
  this.pendingPoll = controller;
144
181
  let job;
145
182
  try {
183
+ this.lastPollAt = performance.now();
146
184
  job = await this.client.poll(capacityAvailable, {
147
185
  // A worker with no free slot must not hold the request: it cannot take
148
- // the run the hold would return.
149
- waitSeconds: capacityAvailable > 0 ? WORKER_POLL_MAX_WAIT_SECONDS : 0,
186
+ // the run the hold would return. Nor may one whose turn budget is full:
187
+ // the hold cannot learn that a turn slot reopened, so a queued chat
188
+ // turn would wait out the whole hold. Polling without one re-polls as
189
+ // soon as any run finishes. A full connector budget still holds: a
190
+ // connector slot freed mid-hold is taken only when the hold returns,
191
+ // at most WORKER_POLL_MAX_WAIT_SECONDS later, which a minutes-long sync
192
+ // can bear; not holding would instead cost every chat turn the idle
193
+ // poll interval for as long as the connector slots stay busy.
194
+ waitSeconds: capacityAvailable > 0 && (lanes === undefined || lanes.agentTurns > 0)
195
+ ? WORKER_POLL_MAX_WAIT_SECONDS
196
+ : 0,
150
197
  signal: controller.signal,
198
+ ...(lanes ? { lanes } : {}),
151
199
  });
152
200
  }
153
201
  finally {
@@ -158,7 +206,10 @@ export class WorkerPollLoop {
158
206
  return undefined;
159
207
  const nextPoll = job.next_poll_seconds ?? 30;
160
208
  log.debug(`[daemon] No runs available, next poll in ${nextPoll}s`);
161
- return Number.isFinite(nextPoll) && nextPoll >= 0 ? nextPoll * 1000 : 1000;
209
+ const delay = Number.isFinite(nextPoll) && nextPoll >= 0 ? nextPoll * 1000 : 1000;
210
+ // Resource pressure can recover while every turn is still waiting on a
211
+ // provider. Recheck locally instead of sleeping through the server hint.
212
+ return resourceCapacity === 0 ? Math.min(delay, RESOURCE_ADMISSION_INTERVAL_MS) : delay;
162
213
  }
163
214
  // A zero-capacity response should be impossible: the server must not enter
164
215
  // a claim lane when the worker truthfully reports no free slot. Do not
@@ -167,7 +218,21 @@ export class WorkerPollLoop {
167
218
  log.info(`[daemon] Server returned run ${job.run_id} while worker is at capacity; leaving it untouched`);
168
219
  return undefined;
169
220
  }
221
+ // An agent turn takes a turn slot; everything else is connector work. The
222
+ // gateway gates turns on `agent_turn` and connector runs on compiled
223
+ // capacity, but an embedding backfill has no gate besides free capacity,
224
+ // so it can arrive while only turn slots are free. It then borrows a turn
225
+ // slot: total concurrency stays within both budgets, and backfills are few
226
+ // (one per organization, a handful per scheduler tick).
227
+ const chargedToTurns = lanes !== undefined && (job.run_type === 'agent_turn'
228
+ ? lanes.agentTurns > 0
229
+ : lanes.connectorJobs === 0);
170
230
  this.activeJobs++;
231
+ // Honour an admitted held claim even if pressure rose during the HTTP
232
+ // request: the server may already own its lease. Only future claims pause.
233
+ this.resourceAdmission?.started();
234
+ if (chargedToTurns)
235
+ this.activeAgentTurns++;
171
236
  this.onActiveJobsChange?.(this.activeJobs);
172
237
  Promise.resolve()
173
238
  .then(() => this.execute(job))
@@ -176,13 +241,17 @@ export class WorkerPollLoop {
176
241
  })
177
242
  .finally(() => {
178
243
  this.activeJobs--;
244
+ if (chargedToTurns)
245
+ this.activeAgentTurns--;
179
246
  this.onActiveJobsChange?.(this.activeJobs);
180
247
  this.capacityVersion++;
181
248
  this.wakePoll?.(true);
182
249
  });
183
250
  // Fill remaining slots while the queue has work, then wait for capacity
184
251
  // or the normal heartbeat instead of delaying every successful claim.
185
- return this.activeJobs < this.maxConcurrentJobs ? 0 : undefined;
252
+ return this.resourceAdmission
253
+ ? RESOURCE_ADMISSION_INTERVAL_MS
254
+ : this.freeSlots(this.laneCapacity()) > 0 ? 0 : undefined;
186
255
  }
187
256
  waitForNextPoll(ms, capacityVersion) {
188
257
  if (!this.running ||
@@ -0,0 +1,19 @@
1
+ import { type ResourceSample } from './resources.js';
2
+ /** Give a newly admitted isolate time to expose its startup cost. */
3
+ export declare const RESOURCE_ADMISSION_INTERVAL_MS = 500;
4
+ /**
5
+ * Local resource admission, underneath the configured per-kind ceilings.
6
+ * This owns no run state: the gateway still owns claims, fairness and leases.
7
+ */
8
+ export declare class ResourceAdmission {
9
+ private readonly sample;
10
+ private readonly now;
11
+ private previous?;
12
+ private nextStartAt;
13
+ private paused?;
14
+ constructor(sample?: () => ResourceSample | null, now?: () => number);
15
+ capacity(): number;
16
+ started(): void;
17
+ private transition;
18
+ }
19
+ //# sourceMappingURL=resource-admission.d.ts.map
@@ -0,0 +1,59 @@
1
+ import { log } from './log.js';
2
+ import { createResourceSampler } from './resources.js';
3
+ /** Give a newly admitted isolate time to expose its startup cost. */
4
+ export const RESOURCE_ADMISSION_INTERVAL_MS = 500;
5
+ const MIB = 1024 * 1024;
6
+ /**
7
+ * Local resource admission, underneath the configured per-kind ceilings.
8
+ * This owns no run state: the gateway still owns claims, fairness and leases.
9
+ */
10
+ export class ResourceAdmission {
11
+ sample;
12
+ now;
13
+ previous;
14
+ nextStartAt = 0;
15
+ paused;
16
+ constructor(sample = createResourceSampler(), now = () => performance.now()) {
17
+ this.sample = sample;
18
+ this.now = now;
19
+ }
20
+ capacity() {
21
+ const at = this.now();
22
+ if (at < this.nextStartAt)
23
+ return 0;
24
+ if (this.previous && at - this.previous.at < RESOURCE_ADMISSION_INTERVAL_MS)
25
+ return this.paused ? 0 : 1;
26
+ const current = this.sample();
27
+ if (!current || current.memory.length === 0 || current.memory.some(m => !Number.isFinite(m.usedBytes) || m.usedBytes < 0 || !Number.isFinite(m.limitBytes) || m.limitBytes <= 0)) {
28
+ this.transition('sample');
29
+ return 0;
30
+ }
31
+ const memoryTarget = this.paused === 'memory' ? 0.7 : 0.8;
32
+ // Keep room for one more large isolate. On small installations reserve
33
+ // 20% instead of requiring half a GiB before admitting the first run.
34
+ const memoryPressure = current.memory.some(m => m.usedBytes + Math.min(512 * MIB, m.limitBytes * 0.2) > m.limitBytes * memoryTarget);
35
+ let cpuLoad = 0;
36
+ if (this.previous && at > this.previous.at) {
37
+ const elapsedMicros = (at - this.previous.at) * 1000;
38
+ for (const c of current.cpu) {
39
+ const before = this.previous.sample.cpu.find(p => p.key === c.key);
40
+ if (before && c.cores > 0)
41
+ cpuLoad = Math.max(cpuLoad, (c.usageMicros - before.usageMicros) / elapsedMicros / c.cores);
42
+ }
43
+ }
44
+ this.previous = { sample: current, at };
45
+ const cpuPressure = cpuLoad > (this.paused === 'cpu' ? 0.6 : 0.8);
46
+ this.transition(memoryPressure ? 'memory' : cpuPressure ? 'cpu' : undefined, { memory: current.memory, cpuLoad });
47
+ return this.paused ? 0 : 1;
48
+ }
49
+ started() {
50
+ this.nextStartAt = this.now() + RESOURCE_ADMISSION_INTERVAL_MS;
51
+ }
52
+ transition(reason, resources) {
53
+ if (reason === this.paused)
54
+ return;
55
+ this.paused = reason;
56
+ log.info(`[daemon] Resource admission ${reason ? `paused (${reason})` : 'resumed'}`, resources);
57
+ }
58
+ }
59
+ //# sourceMappingURL=resource-admission.js.map
@@ -0,0 +1,20 @@
1
+ export interface ResourceSample {
2
+ memory: Array<{
3
+ usedBytes: number;
4
+ limitBytes: number;
5
+ }>;
6
+ cpu: Array<{
7
+ key: string;
8
+ usageMicros: number;
9
+ cores: number;
10
+ }>;
11
+ }
12
+ type Read = (path: string) => string;
13
+ declare function hostMemory(): {
14
+ limitBytes: number;
15
+ availableBytes: number;
16
+ };
17
+ /** Container-wide counters include isolate threads and spawned tool children. */
18
+ export declare function createResourceSampler(read?: Read, platform?: string, readHostMemory?: typeof hostMemory): () => ResourceSample | null;
19
+ export {};
20
+ //# sourceMappingURL=resources.d.ts.map
@@ -0,0 +1,163 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { availableParallelism, freemem, totalmem } from 'node:os';
3
+ import { dirname, join, relative } from 'node:path';
4
+ function hostMemory() {
5
+ const runtime = process;
6
+ // Available memory includes reclaimable host cache; free memory alone can
7
+ // leave an otherwise healthy bare-metal worker permanently paused.
8
+ return { limitBytes: totalmem(), availableBytes: runtime.availableMemory?.() ?? freemem() };
9
+ }
10
+ function optional(read, path) {
11
+ try {
12
+ return read(path).trim();
13
+ }
14
+ catch {
15
+ return undefined;
16
+ }
17
+ }
18
+ function positive(value) {
19
+ const n = value ? Number(value) : Number.NaN;
20
+ return Number.isFinite(n) && n > 0 ? n : undefined;
21
+ }
22
+ function unescapePath(value) {
23
+ return value.replace(/\\([0-7]{3})/g, (_, octal) => String.fromCharCode(Number.parseInt(octal, 8)));
24
+ }
25
+ /** Resolve both host cgroup paths and a container's cgroup-namespace root. */
26
+ function groups(read) {
27
+ const membership = optional(read, '/proc/self/cgroup') ?? '';
28
+ const mounts = optional(read, '/proc/self/mountinfo') ?? '';
29
+ const result = {};
30
+ for (const line of mounts.split('\n')) {
31
+ const [left, right] = line.split(' - ');
32
+ if (!left || !right)
33
+ continue;
34
+ const fields = left.split(' ');
35
+ const [kind, , options = ''] = right.split(' ');
36
+ if (kind !== 'cgroup' && kind !== 'cgroup2')
37
+ continue;
38
+ const root = unescapePath(fields[4] ?? '');
39
+ const mountedFrom = unescapePath(fields[3] ?? '/');
40
+ if (!root.startsWith('/'))
41
+ continue;
42
+ for (const controller of ['memory', 'cpu', 'cpuacct']) {
43
+ if (kind === 'cgroup' && !options.split(',').includes(controller))
44
+ continue;
45
+ // On hybrid hosts a controller belongs to only one hierarchy. A v2
46
+ // mount must not hide its v1 membership, regardless of mount order.
47
+ const v1Entry = membership.split('\n').find(row => row.split(':')[1]?.split(',').includes(controller));
48
+ if (kind === 'cgroup2' && v1Entry)
49
+ continue;
50
+ const entry = membership.split('\n').find(row => {
51
+ const parts = row.split(':');
52
+ return kind === 'cgroup2' ? parts[0] === '0' && parts[1] === '' : parts[1]?.split(',').includes(controller);
53
+ });
54
+ if (!entry)
55
+ continue;
56
+ const member = entry.slice(entry.indexOf(':', entry.indexOf(':') + 1) + 1);
57
+ const suffix = mountedFrom === '/' ? member.slice(1)
58
+ : member === mountedFrom ? ''
59
+ : member.startsWith(`${mountedFrom}/`) ? member.slice(mountedFrom.length + 1)
60
+ : member === '/' ? '' : member.slice(1);
61
+ const candidate = join(root, suffix);
62
+ // A namespace may report a path outside the visible mount. In that
63
+ // case the mounted root is the only scope we can measure faithfully.
64
+ const leaf = relative(root, candidate).startsWith('..') ? root : candidate;
65
+ result[controller] = { root, leaf, v2: kind === 'cgroup2' };
66
+ }
67
+ }
68
+ return result;
69
+ }
70
+ function ancestors(group) {
71
+ const paths = [group.leaf];
72
+ while (paths[paths.length - 1] !== group.root) {
73
+ const parent = dirname(paths[paths.length - 1]);
74
+ if (parent === paths[paths.length - 1])
75
+ break;
76
+ paths.push(parent);
77
+ }
78
+ return paths;
79
+ }
80
+ /** Container-wide counters include isolate threads and spawned tool children. */
81
+ export function createResourceSampler(read = path => readFileSync(path, 'utf8'), platform = process.platform, readHostMemory = hostMemory) {
82
+ const discovered = platform === 'linux' ? groups(read) : {};
83
+ return () => {
84
+ const memory = [];
85
+ const cpu = [];
86
+ const host = readHostMemory();
87
+ if (!Number.isFinite(host.limitBytes) || host.limitBytes <= 0 || !Number.isFinite(host.availableBytes) || host.availableBytes < 0)
88
+ return null;
89
+ if (discovered.memory) {
90
+ for (const path of ancestors(discovered.memory)) {
91
+ let raw;
92
+ try {
93
+ raw = read(join(path, discovered.memory.v2 ? 'memory.max' : 'memory.limit_in_bytes')).trim();
94
+ }
95
+ catch (error) {
96
+ if (error?.code !== 'ENOENT')
97
+ return null;
98
+ continue;
99
+ }
100
+ const limit = positive(raw);
101
+ if (raw !== 'max' && limit === undefined)
102
+ return null;
103
+ // v2 `max` and v1's huge unlimited sentinel use the host bound.
104
+ if (!limit || limit >= host.limitBytes)
105
+ continue;
106
+ const usage = optional(read, join(path, discovered.memory.v2 ? 'memory.current' : 'memory.usage_in_bytes'));
107
+ const used = usage === undefined ? Number.NaN : Number(usage);
108
+ if (!Number.isFinite(used) || used < 0)
109
+ return null;
110
+ const stats = optional(read, join(path, 'memory.stat')) ?? '';
111
+ const inactiveKey = discovered.memory.v2 ? 'inactive_file' : 'total_inactive_file';
112
+ const inactive = Number(stats.match(new RegExp(`^${inactiveKey} (\\d+)$`, 'm'))?.[1] ?? 0);
113
+ // Match cAdvisor's working set: inactive file cache is reclaimable.
114
+ // Keeping it in usage can permanently pause a worker after a build or
115
+ // large source read, even when resident job memory is small.
116
+ memory.push({ usedBytes: Math.max(0, used - inactive), limitBytes: limit });
117
+ }
118
+ }
119
+ // The unified root (or a hierarchy without the memory controller) has
120
+ // no limit file. Only a discovered bound requires a readable usage counter.
121
+ if (memory.length === 0)
122
+ memory.push({ usedBytes: Math.max(0, host.limitBytes - host.availableBytes), limitBytes: host.limitBytes });
123
+ if (discovered.cpu) {
124
+ for (const path of ancestors(discovered.cpu)) {
125
+ let quota;
126
+ let period;
127
+ if (discovered.cpu.v2) {
128
+ const parts = optional(read, join(path, 'cpu.max'))?.split(/\s+/);
129
+ quota = positive(parts?.[0]);
130
+ period = positive(parts?.[1]);
131
+ if (parts && (parts.length !== 2 || !period || (parts[0] !== 'max' && !quota)))
132
+ return null;
133
+ }
134
+ else {
135
+ const raw = optional(read, join(path, 'cpu.cfs_quota_us'));
136
+ quota = positive(raw);
137
+ period = positive(optional(read, join(path, 'cpu.cfs_period_us')));
138
+ if (raw !== undefined && (!period || (raw !== '-1' && !quota)))
139
+ return null;
140
+ }
141
+ if (!quota || !period)
142
+ continue;
143
+ const accounting = discovered.cpuacct;
144
+ const usagePath = accounting ? join(accounting.leaf, relative(discovered.cpu.leaf, path)) : path;
145
+ const raw = discovered.cpu.v2
146
+ ? optional(read, join(path, 'cpu.stat'))?.match(/^usage_usec (\d+)$/m)?.[1]
147
+ : optional(read, join(usagePath, 'cpuacct.usage'));
148
+ const used = raw === undefined ? Number.NaN : Number(raw);
149
+ if (!Number.isFinite(used) || used < 0)
150
+ return null;
151
+ cpu.push({ key: path, usageMicros: discovered.cpu.v2 ? used : used / 1000, cores: Math.min(availableParallelism(), quota / period) });
152
+ }
153
+ }
154
+ // With no quota, process CPU still includes all isolated-vm threads. On
155
+ // devices this sampler is unused; the existing device budget is preserved.
156
+ if (cpu.length === 0) {
157
+ const usage = process.cpuUsage();
158
+ cpu.push({ key: 'process', usageMicros: usage.user + usage.system, cores: availableParallelism() });
159
+ }
160
+ return { memory, cpu };
161
+ };
162
+ }
163
+ //# sourceMappingURL=resources.js.map
@@ -45,14 +45,37 @@ export function resolveDaemonLaunchContext(opts, env = process.env, sessionsDir)
45
45
  interactiveSession: detected,
46
46
  };
47
47
  }
48
+ function parseSlotCount(raw) {
49
+ const configured = raw ? Math.max(1, Number.parseInt(raw, 10)) : undefined;
50
+ return configured !== undefined && Number.isFinite(configured) ? configured : undefined;
51
+ }
52
+ /**
53
+ * A device's one shared budget, or a fleet worker's connector-work budget
54
+ * (syncs, actions, auth, embedding backfills).
55
+ */
48
56
  function resolveDaemonMaxConcurrentJobs(platform, env = process.env, hostThreads) {
49
- const configured = env.WORKER_MAX_CONCURRENT_JOBS
50
- ? Math.max(1, Number.parseInt(env.WORKER_MAX_CONCURRENT_JOBS, 10))
51
- : undefined;
52
- if (configured !== undefined && Number.isFinite(configured))
57
+ const configured = parseSlotCount(env.WORKER_MAX_CONCURRENT_JOBS);
58
+ if (configured !== undefined)
53
59
  return configured;
54
60
  return platform ? defaultDeviceMaxConcurrentJobs(hostThreads) : undefined;
55
61
  }
62
+ /**
63
+ * Agent-turn slots for an unconfigured fleet worker. A turn mostly waits on
64
+ * the model provider; the worst measured one (4 MB history, the cap) peaked at
65
+ * 133 MB worker RSS, so four stay near half a gigabyte beside the connector
66
+ * slot. The Helm chart sizes its own value from the pod's memory.
67
+ */
68
+ const DEFAULT_FLEET_MAX_CONCURRENT_AGENT_TURNS = 4;
69
+ /**
70
+ * A fleet worker budgets agent turns apart from connector work.
71
+ * Devices never run agent turns and keep their one shared budget.
72
+ */
73
+ function resolveDaemonMaxConcurrentAgentTurns(platform, env = process.env) {
74
+ if (platform)
75
+ return undefined;
76
+ return parseSlotCount(env.WORKER_MAX_CONCURRENT_AGENT_TURNS)
77
+ ?? DEFAULT_FLEET_MAX_CONCURRENT_AGENT_TURNS;
78
+ }
56
79
  /**
57
80
  * Validate the options and boot the daemon. Throws a user-facing `Error` on
58
81
  * bad input; the caller prints it and exits non-zero.
@@ -136,6 +159,7 @@ export async function startDaemonCommand(opts) {
136
159
  log.info(`[cli] Starting worker daemon (ID: ${workerId}, API: ${opts.apiUrl})`);
137
160
  const env = buildConnectorWorkerEnv();
138
161
  const maxConcurrentJobs = resolveDaemonMaxConcurrentJobs(platform);
162
+ const maxConcurrentAgentTurns = resolveDaemonMaxConcurrentAgentTurns(platform);
139
163
  if (platform) {
140
164
  log.info(`[cli] device mode: platform=${platform} capabilities=${capabilities.join(',') || '(none)'} max_concurrent_jobs=${maxConcurrentJobs}`);
141
165
  if (opts.activeOrg) {
@@ -156,6 +180,7 @@ export async function startDaemonCommand(opts) {
156
180
  ...(label ? { label } : {}),
157
181
  ...(platform ? { manifests: DEVICE_MANIFESTS_BY_PLATFORM[platform] ?? [] } : {}),
158
182
  ...(maxConcurrentJobs ? { maxConcurrentJobs } : {}),
183
+ ...(maxConcurrentAgentTurns ? { maxConcurrentAgentTurns } : {}),
159
184
  backendCapacity,
160
185
  ...(opts.workerCredentialMaintenance
161
186
  ? { workerCredentialMaintenance: opts.workerCredentialMaintenance }
@@ -14,6 +14,8 @@ export interface DaemonConfig {
14
14
  capabilities?: WorkerCapabilities;
15
15
  pollIntervalMs?: number;
16
16
  maxConcurrentJobs?: number;
17
+ /** Separate agent-turn budget; see `WorkerPollLoopOptions`. */
18
+ maxConcurrentAgentTurns?: number;
17
19
  executor?: Partial<ExecutorConfig>;
18
20
  version?: string;
19
21
  /** Set only for a device worker; cloud-fleet daemons leave this undefined. */
@@ -7,6 +7,7 @@ import { WorkerClient } from './client.js';
7
7
  import { executeRun } from './executor.js';
8
8
  import { attachedInteractiveSession, attachInteractiveSession, } from './interactive-session.js';
9
9
  import { installWorkerPollLoopSignals, WorkerPollLoop } from './poll-loop.js';
10
+ import { ResourceAdmission } from './resource-admission.js';
10
11
  const DEFAULT_CAPABILITIES = {};
11
12
  const DEFAULT_EXECUTOR_TIMEOUT_MS = 600000;
12
13
  /**
@@ -56,6 +57,9 @@ export class WorkerDaemon {
56
57
  client: this.client,
57
58
  pollIntervalMs: daemonConfig.pollIntervalMs,
58
59
  maxConcurrentJobs: daemonConfig.maxConcurrentJobs,
60
+ maxConcurrentAgentTurns: daemonConfig.maxConcurrentAgentTurns,
61
+ resourceAdmission: !daemonConfig.platform && daemonConfig.maxConcurrentAgentTurns !== undefined
62
+ ? new ResourceAdmission() : undefined,
59
63
  execute: (job) => executeRun(this.client, job, this.env, this.config.executor),
60
64
  ...(credentialMaintenance
61
65
  ? {
@@ -26,7 +26,7 @@ export interface ExecutorSelection {
26
26
  * Build the executor for a job. There is exactly one: connector code runs in a
27
27
  * hardened V8 isolate, which IS the security boundary for organization-supplied
28
28
  * code. Nothing about the job selects it, so a host that cannot load
29
- * `isolated-vm` (Bun, Node 25, or a failed native build) rejects with
29
+ * `isolated-vm` (Bun, a non-26 Node, or a failed native build) rejects with
30
30
  * `IsolateRuntimeUnavailableError` rather than falling back to anything.
31
31
  */
32
32
  export declare function selectExecutor(selection?: ExecutorSelection): Promise<SyncExecutor>;
@@ -7,7 +7,7 @@ import { IsolateExecutor, IsolateRuntimeUnavailableError } from './isolate.js';
7
7
  * Build the executor for a job. There is exactly one: connector code runs in a
8
8
  * hardened V8 isolate, which IS the security boundary for organization-supplied
9
9
  * code. Nothing about the job selects it, so a host that cannot load
10
- * `isolated-vm` (Bun, Node 25, or a failed native build) rejects with
10
+ * `isolated-vm` (Bun, a non-26 Node, or a failed native build) rejects with
11
11
  * `IsolateRuntimeUnavailableError` rather than falling back to anything.
12
12
  */
13
13
  export async function selectExecutor(selection = {}) {
@@ -3,8 +3,7 @@
3
3
  *
4
4
  * Kept separate from the module's own typings so no public worker type
5
5
  * depends on `isolated-vm` being installed: `loadIsolatedVm()` narrows the
6
- * dynamically imported module to this shape at the load boundary, and both
7
- * `isolated-vm@6` and `isolated-vm@7` satisfy it.
6
+ * dynamically imported module to this shape at the load boundary.
8
7
  */
9
8
  export interface IvmReferenceApplyOptions {
10
9
  timeout?: number;
@@ -3,8 +3,7 @@
3
3
  *
4
4
  * Kept separate from the module's own typings so no public worker type
5
5
  * depends on `isolated-vm` being installed: `loadIsolatedVm()` narrows the
6
- * dynamically imported module to this shape at the load boundary, and both
7
- * `isolated-vm@6` and `isolated-vm@7` satisfy it.
6
+ * dynamically imported module to this shape at the load boundary.
8
7
  */
9
8
  export {};
10
9
  //# sourceMappingURL=ivm-types.js.map
@@ -1,20 +1,18 @@
1
1
  /**
2
- * Load `isolated-vm` for the Node line this worker runs on.
2
+ * Load `isolated-vm` for the worker's Node runtime.
3
3
  *
4
- * The native addon is ABI-bound per Node major, so two builds ship as
5
- * optionalDependencies: `isolated-vm@6` for Node 22–24 and the aliased
6
- * `isolated-vm-next` (= `isolated-vm@7`) for Node 26+. Node 25 is an EOL
7
- * non-LTS line upstream skipped, and Bun cannot dlopen a V8 addon at all.
8
- * Both return `null` so the caller fails closed: the executor selector fails
9
- * an `isolate` run instead of forking a child for it. Mirrors
10
- * `packages/server/src/sandbox/run-script.ts`; the gateway adopting this
11
- * module is a follow-up.
4
+ * The native addon is ABI-bound to the Node major: Lobu ships one
5
+ * `isolated-vm@7` build and runs it on Node 26 only. Bun cannot dlopen a V8
6
+ * addon, and Node majors other than 26 have no matching ABI build, so both
7
+ * return `null` and the executor selector fails an `isolate` run instead of
8
+ * forking a child for it. Mirrors
9
+ * `packages/server/src/sandbox/run-script.ts`.
12
10
  */
13
11
  import type { IsolatedVm } from './ivm-types.js';
14
12
  /**
15
- * Why this host cannot run an isolate, or `null` when the Node line has a
16
- * build. A load failure on an eligible line (native build skipped on this
17
- * platform) is reported by `loadIsolatedVm()` returning null; this only
13
+ * Why this host cannot run an isolate, or `null` when it is the supported
14
+ * Node 26 line. A load failure on an eligible host (native build skipped on
15
+ * this platform) is reported by `loadIsolatedVm()` returning null; this only
18
16
  * classifies the runtime.
19
17
  */
20
18
  export declare function isolatedVmUnavailableReason(): string | null;