alineo 0.5.0 → 0.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.
package/dist/index.d.mts CHANGED
@@ -982,7 +982,8 @@ export declare class Alineo {
982
982
  * `--depth` CLI flag) — standard flag-beats-config precedence. Same for
983
983
  * `{ maxAgents }` and `--max`.
984
984
  *
985
- * Logs timing for each phase to stdout via `[agent]` prefixed lines.
985
+ * Reports timing for each phase through `@alineo-labs/logger` (component `agent`, tagged with
986
+ * the spec `name`). Silent by default — set `ALINEO_LOG_LEVEL=info` to see it.
986
987
  */
987
988
  static load(spec: AgentSpec | Record<string, unknown>, opts: {
988
989
  adapter: IStorageAdapter;
@@ -1082,6 +1083,13 @@ export declare class Alineo {
1082
1083
  runId?: string;
1083
1084
  /** Wire a `Memory` instance onto the returned agent — see `Alineo.memory`. */
1084
1085
  memory?: Memory;
1086
+ /**
1087
+ * Don't probe the bridge before returning, and accept a Paused sandbox. For a paused
1088
+ * sandbox, whose frozen bridge can't answer until `agent.sandbox.resume()` — check it
1089
+ * yourself after resuming (`agent.adapter.waitReady()`), and fall back to
1090
+ * `Alineo.resume()` if it's gone.
1091
+ */
1092
+ skipReadyCheck?: boolean;
1085
1093
  }): Promise<Alineo>;
1086
1094
  /**
1087
1095
  * Connect to an already-running sandbox WITHOUT touching its Pi bridge — unlike
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Sandbox } from "@alineo-labs/sandbox";
2
2
  import { readFileSync } from "node:fs";
3
3
  import { LedgerEvent, WorkflowError } from "@alineo-labs/core";
4
+ import { getLogger } from "@alineo-labs/logger";
4
5
  import { existsSync } from "fs";
5
6
  import * as z$1 from "zod";
6
7
  import { fileURLToPath } from "node:url";
@@ -1010,6 +1011,16 @@ function normalizeHost(host) {
1010
1011
  }
1011
1012
  //#endregion
1012
1013
  //#region src/agent/factory.ts
1014
+ const agentLog = getLogger("agent");
1015
+ /** Best-effort cleanup that failed: say so — the sandbox may now be leaked. */
1016
+ function cleanupFailed(log, which) {
1017
+ return (err) => {
1018
+ log.warn("cleanup failed: could not close sandbox — it may be leaked", {
1019
+ ...which,
1020
+ err
1021
+ });
1022
+ };
1023
+ }
1013
1024
  function elapsed(t) {
1014
1025
  return `${Date.now() - t}ms`;
1015
1026
  }
@@ -1033,6 +1044,10 @@ async function loadAgent(specInput, opts) {
1033
1044
  resolvedEnv.ALINEO_MAX_AGENTS = String(effectiveMaxAgents);
1034
1045
  }
1035
1046
  const runId = opts.runId ?? crypto.randomUUID();
1047
+ const log = agentLog.child(opts.runId ? {
1048
+ name: spec.name,
1049
+ runId
1050
+ } : { name: spec.name });
1036
1051
  resolvedEnv.ALINEO_RUN_ID = runId;
1037
1052
  const resources = {
1038
1053
  ...config.defaults.resources,
@@ -1078,7 +1093,7 @@ async function loadAgent(specInput, opts) {
1078
1093
  if (!opts.rebuild) {
1079
1094
  const record = await store.get(spec.name, setupHash);
1080
1095
  if (record) try {
1081
- console.log(`[agent] restoring from snapshot...`);
1096
+ log.info("restoring from snapshot");
1082
1097
  const t1 = Date.now();
1083
1098
  sb = await client.restoreSnapshot(record.snapshotId, spec.name, resources, runId, {
1084
1099
  networkPolicy,
@@ -1087,16 +1102,19 @@ async function loadAgent(specInput, opts) {
1087
1102
  resourceId: spec.resourceId ?? spec.name,
1088
1103
  teamId: spec.teamId
1089
1104
  });
1090
- console.log(`[agent] snapshot ready ${elapsed(t1)} (${sb.sandboxId})`);
1105
+ log.info("snapshot ready", {
1106
+ elapsed: elapsed(t1),
1107
+ sandboxId: sb.sandboxId
1108
+ });
1091
1109
  fromSnapshot = true;
1092
1110
  } catch (err) {
1093
1111
  const reason = err instanceof Error ? err.message : String(err);
1094
- console.log(`[agent] snapshot restore failed (${reason}), rebuilding...`);
1112
+ log.warn("snapshot restore failed, rebuilding", { reason });
1095
1113
  await store.delete(spec.name);
1096
1114
  }
1097
1115
  }
1098
1116
  if (!fromSnapshot) {
1099
- console.log(`[agent] starting sandbox (${spec.name})...`);
1117
+ log.info("starting sandbox");
1100
1118
  const t1 = Date.now();
1101
1119
  sb = await client.sandbox({
1102
1120
  image: "node:22",
@@ -1112,43 +1130,72 @@ async function loadAgent(specInput, opts) {
1112
1130
  resourceId: spec.resourceId ?? spec.name,
1113
1131
  teamId: spec.teamId
1114
1132
  });
1115
- console.log(`[agent] sandbox ready ${elapsed(t1)} (${sb.sandboxId})`);
1116
- console.log(`[agent] installing Pi CLI...`);
1117
- const t2 = Date.now();
1118
- await adapter.install(sb, spec);
1119
- console.log(`[agent] Pi CLI ready ${elapsed(t2)}`);
1120
- for (const step of spec.setup ?? []) {
1121
- console.log(`[agent] setup: ${step.name}...`);
1122
- const ts = Date.now();
1123
- const cmd = step.cwd ? `cd ${step.cwd} && ${step.run}` : step.run;
1124
- await sb.exec(cmd);
1125
- console.log(`[agent] setup done ${elapsed(ts)} (${step.name})`);
1126
- }
1127
- console.log(`[agent] checkpointing...`);
1128
- const t3 = Date.now();
1129
- const snapshotId = await sb.checkpoint();
1130
- await store.save({
1131
- specName: spec.name,
1132
- setupHash,
1133
- snapshotId,
1134
- createdAt: Date.now()
1133
+ log.info("sandbox ready", {
1134
+ elapsed: elapsed(t1),
1135
+ sandboxId: sb.sandboxId
1135
1136
  });
1136
- console.log(`[agent] checkpoint done ${elapsed(t3)}`);
1137
+ const created = sb;
1138
+ try {
1139
+ log.info("installing Pi CLI");
1140
+ const t2 = Date.now();
1141
+ await adapter.install(created, spec);
1142
+ log.info("Pi CLI ready", { elapsed: elapsed(t2) });
1143
+ for (const step of spec.setup ?? []) {
1144
+ log.info("setup step", { step: step.name });
1145
+ const ts = Date.now();
1146
+ const cmd = step.cwd ? `cd ${step.cwd} && ${step.run}` : step.run;
1147
+ await created.exec(cmd);
1148
+ log.info("setup step done", {
1149
+ step: step.name,
1150
+ elapsed: elapsed(ts)
1151
+ });
1152
+ }
1153
+ log.info("checkpointing");
1154
+ const t3 = Date.now();
1155
+ const snapshotId = await created.checkpoint();
1156
+ await store.save({
1157
+ specName: spec.name,
1158
+ setupHash,
1159
+ snapshotId,
1160
+ createdAt: Date.now()
1161
+ });
1162
+ log.info("checkpoint done", { elapsed: elapsed(t3) });
1163
+ } catch (err) {
1164
+ await created.close().catch(cleanupFailed(log, { sandboxId: created.sandboxId }));
1165
+ await egressGate?.stop().catch(() => {});
1166
+ throw err;
1167
+ }
1137
1168
  }
1138
1169
  if (!sb) throw new Error("internal error: sandbox was neither restored nor created");
1139
1170
  egressGate?.bind(sb);
1140
- for (const cb of credentialBindings) {
1141
- if (cb.approval === "hold") continue;
1142
- await sb.credentials.set(cb.name, cb.value, cb.binding, cb.source);
1143
- }
1144
- resolvedEnv.ALINEO_SANDBOX_ID = sb.sandboxId;
1145
- await adapter.configure(sb, spec, resolvedEnv);
1146
- console.log(`[agent] starting bridge...`);
1147
- const t4 = Date.now();
1148
- await adapter.startBridge(sb);
1149
- await adapter.waitReady();
1150
- console.log(`[agent] bridge ready ${elapsed(t4)}`);
1151
- console.log(`[agent] total ${elapsed(t0)}${fromSnapshot ? " (from snapshot)" : ""}`);
1171
+ try {
1172
+ for (const cb of credentialBindings) {
1173
+ if (cb.approval === "hold") continue;
1174
+ await sb.credentials.set(cb.name, cb.value, cb.binding, cb.source);
1175
+ }
1176
+ resolvedEnv.ALINEO_SANDBOX_ID = sb.sandboxId;
1177
+ await adapter.configure(sb, spec, resolvedEnv);
1178
+ log.info("starting bridge");
1179
+ const t4 = Date.now();
1180
+ await adapter.startBridge(sb);
1181
+ await adapter.waitReady();
1182
+ log.info("bridge ready", { elapsed: elapsed(t4) });
1183
+ } catch (err) {
1184
+ await sb.close().catch(cleanupFailed(log, { sandboxId: sb.sandboxId }));
1185
+ await egressGate?.stop().catch(() => {});
1186
+ if (!fromSnapshot) throw err;
1187
+ const reason = err instanceof Error ? err.message : String(err);
1188
+ log.warn("restored snapshot didn't start — discarding it and rebuilding", { reason });
1189
+ await store.delete(spec.name);
1190
+ return loadAgent(specInput, {
1191
+ ...opts,
1192
+ rebuild: true
1193
+ });
1194
+ }
1195
+ log.info("total", {
1196
+ elapsed: elapsed(t0),
1197
+ fromSnapshot
1198
+ });
1152
1199
  return {
1153
1200
  sandbox: sb,
1154
1201
  spec,
@@ -1190,8 +1237,12 @@ async function resumeAgent(sandboxId, opts) {
1190
1237
  resolvedEnv.ALINEO_SPAWN_DEPTH = String(spec.spawnDepth);
1191
1238
  }
1192
1239
  const runId = opts.runId ?? crypto.randomUUID();
1240
+ const log = agentLog.child(opts.runId ? {
1241
+ name: spec.name,
1242
+ runId
1243
+ } : { name: spec.name });
1193
1244
  resolvedEnv.ALINEO_RUN_ID = runId;
1194
- console.log(`[agent] reconnecting to ${sandboxId}...`);
1245
+ log.info("reconnecting", { sandboxId });
1195
1246
  const t1 = Date.now();
1196
1247
  const sb = await client.connect(sandboxId, spec.name, {
1197
1248
  runId,
@@ -1199,18 +1250,18 @@ async function resumeAgent(sandboxId, opts) {
1199
1250
  resourceId: spec.resourceId ?? spec.name,
1200
1251
  teamId: spec.teamId
1201
1252
  });
1202
- console.log(`[agent] connected ${elapsed(t1)}`);
1253
+ log.info("connected", { elapsed: elapsed(t1) });
1203
1254
  await sb.exec("pkill -f 'node /alineo-bridge.js' 2>/dev/null; sleep 0.1; true", { strict: false });
1204
1255
  const adapter = new PiAdapter();
1205
1256
  resolvedEnv.ALINEO_SANDBOX_ID = sandboxId;
1206
1257
  await adapter.configure(sb, spec, resolvedEnv, { resume: true });
1207
- console.log(`[agent] starting bridge...`);
1258
+ log.info("starting bridge");
1208
1259
  const t2 = Date.now();
1209
1260
  await adapter.startBridge(sb);
1210
1261
  await adapter.waitReady();
1211
- console.log(`[agent] bridge ready ${elapsed(t2)}`);
1262
+ log.info("bridge ready", { elapsed: elapsed(t2) });
1212
1263
  await reconcileDroppedPermissions(opts.adapter, sb, spec.name, sandboxId);
1213
- console.log(`[agent] total ${elapsed(t0)}`);
1264
+ log.info("total", { elapsed: elapsed(t0) });
1214
1265
  return {
1215
1266
  sandbox: sb,
1216
1267
  spec,
@@ -1272,23 +1323,31 @@ async function reattachAgent(sandboxId, opts) {
1272
1323
  resolvedEnv.ALINEO_SPAWN_DEPTH = String(spec.spawnDepth);
1273
1324
  }
1274
1325
  const runId = opts.runId ?? crypto.randomUUID();
1326
+ const log = agentLog.child(opts.runId ? {
1327
+ name: spec.name,
1328
+ runId
1329
+ } : { name: spec.name });
1275
1330
  resolvedEnv.ALINEO_RUN_ID = runId;
1276
1331
  resolvedEnv.ALINEO_SANDBOX_ID = sandboxId;
1277
- console.log(`[agent] reattaching to ${sandboxId}...`);
1332
+ log.info("reattaching", { sandboxId });
1278
1333
  const t1 = Date.now();
1279
1334
  const sb = await client.connect(sandboxId, spec.name, {
1280
1335
  runId,
1281
1336
  resourceId: spec.resourceId ?? spec.name,
1282
1337
  teamId: spec.teamId,
1283
- resources: spec.resources ?? config.defaults.resources
1338
+ resources: spec.resources ?? config.defaults.resources,
1339
+ allowPaused: opts.skipReadyCheck === true
1284
1340
  });
1285
- console.log(`[agent] connected ${elapsed(t1)}`);
1341
+ log.info("connected", { elapsed: elapsed(t1) });
1286
1342
  const adapter = new PiAdapter();
1287
1343
  await adapter.reattachBridge(sb);
1288
1344
  const t2 = Date.now();
1289
- await adapter.waitReady(5e3);
1290
- console.log(`[agent] bridge reattached ${elapsed(t2)}`);
1291
- console.log(`[agent] total ${elapsed(t0)}`);
1345
+ if (!opts.skipReadyCheck) await adapter.waitReady(5e3);
1346
+ log.info("bridge reattached", {
1347
+ elapsed: elapsed(t2),
1348
+ probed: !opts.skipReadyCheck
1349
+ });
1350
+ log.info("total", { elapsed: elapsed(t0) });
1292
1351
  return {
1293
1352
  sandbox: sb,
1294
1353
  spec,
@@ -1345,27 +1404,39 @@ async function spawnChild(self, childSpecPath, opts = {}) {
1345
1404
  childEnv.ALINEO_SPAWN_DEPTH = String(parentDepth - 1);
1346
1405
  if (parentMax !== void 0) childEnv.ALINEO_MAX_AGENTS = String(parentMax - 1);
1347
1406
  const runId = process.env.ALINEO_RUN_ID ?? crypto.randomUUID();
1407
+ const log = agentLog.child(process.env.ALINEO_RUN_ID ? {
1408
+ name: childSpec.name,
1409
+ runId
1410
+ } : { name: childSpec.name });
1348
1411
  childEnv.ALINEO_RUN_ID = runId;
1349
1412
  const childCredentialBindings = extractCredentialBindings(childSpec.env ?? {});
1350
1413
  if (childCredentialBindings.some((b) => b.approval === "hold")) throw new Error(`Spawned agent "${childSpec.name}" has a credential binding with approval: "hold" — egress approval gating is not supported for spawned agents yet (no handler is wired on the spawn path). Remove approval: "hold" from the child spec.`);
1351
1414
  const childResourceId = resolveChildResourceId(childSpec);
1352
- console.log(`[agent] forking sandbox for spawn (${childSpec.name})...`);
1415
+ log.info("forking sandbox for spawn");
1353
1416
  const t0 = Date.now();
1354
1417
  const forkedSb = await self.sandbox.fork(childSpec.name, runId, {
1355
1418
  credentialProxy: childCredentialBindings.length > 0,
1356
1419
  resourceId: childResourceId,
1357
1420
  teamId: childSpec.teamId
1358
1421
  });
1359
- console.log(`[agent] fork ready ${elapsed(t0)} (${forkedSb.sandboxId})`);
1360
- for (const { name, value, binding, source } of childCredentialBindings) await forkedSb.credentials.set(name, value, binding, source);
1422
+ log.info("fork ready", {
1423
+ elapsed: elapsed(t0),
1424
+ sandboxId: forkedSb.sandboxId
1425
+ });
1361
1426
  const adapter = new PiAdapter();
1362
- childEnv.ALINEO_SANDBOX_ID = forkedSb.sandboxId;
1363
- await adapter.configure(forkedSb, childSpec, childEnv);
1364
- console.log(`[agent] starting bridge...`);
1365
- const t1 = Date.now();
1366
- await adapter.startBridge(forkedSb, Object.keys(self.env));
1367
- await adapter.waitReady();
1368
- console.log(`[agent] bridge ready ${elapsed(t1)}`);
1427
+ try {
1428
+ for (const { name, value, binding, source } of childCredentialBindings) await forkedSb.credentials.set(name, value, binding, source);
1429
+ childEnv.ALINEO_SANDBOX_ID = forkedSb.sandboxId;
1430
+ await adapter.configure(forkedSb, childSpec, childEnv);
1431
+ log.info("starting bridge");
1432
+ const t1 = Date.now();
1433
+ await adapter.startBridge(forkedSb, Object.keys(self.env));
1434
+ await adapter.waitReady();
1435
+ log.info("bridge ready", { elapsed: elapsed(t1) });
1436
+ } catch (err) {
1437
+ await forkedSb.close().catch(cleanupFailed(log, { sandboxId: forkedSb.sandboxId }));
1438
+ throw err;
1439
+ }
1369
1440
  return {
1370
1441
  sandbox: forkedSb,
1371
1442
  spec: {
@@ -1399,12 +1470,13 @@ async function forkChildMemory(parentMemory, parentRef, child) {
1399
1470
  try {
1400
1471
  await parentMemory.fork(parentRef, child.resourceRef.resourceId);
1401
1472
  } catch (err) {
1402
- await child.close().catch(() => {});
1473
+ await child.close().catch(cleanupFailed(agentLog, { resourceId: child.resourceRef.resourceId }));
1403
1474
  throw err;
1404
1475
  }
1405
1476
  }
1406
1477
  //#endregion
1407
1478
  //#region src/agent/session-control.ts
1479
+ const log = getLogger("agent");
1408
1480
  /**
1409
1481
  * Wrap a raw adapter stream so that every permission event is mirrored to the ledger
1410
1482
  * (`PermissionRequested` / `PermissionResolved`) and, when `onPermission` is given, each
@@ -1426,7 +1498,12 @@ async function* instrument(a, stream, onPermission) {
1426
1498
  target: ev.target,
1427
1499
  title: ev.title
1428
1500
  };
1429
- Promise.resolve(onPermission(req)).then((decision) => a.adapter.resolvePermission(ev.requestId, decision)).catch(() => {});
1501
+ Promise.resolve(onPermission(req)).then((decision) => a.adapter.resolvePermission(ev.requestId, decision)).catch((err) => {
1502
+ log.warn("onPermission handler failed", {
1503
+ requestId: ev.requestId,
1504
+ err
1505
+ });
1506
+ });
1430
1507
  }
1431
1508
  } else if (ev.type === "permission_resolved") a.sandbox.emit(LedgerEvent.PermissionResolved, -1, {
1432
1509
  requestId: ev.requestId,
@@ -1760,7 +1837,8 @@ var Alineo = class Alineo {
1760
1837
  * `--depth` CLI flag) — standard flag-beats-config precedence. Same for
1761
1838
  * `{ maxAgents }` and `--max`.
1762
1839
  *
1763
- * Logs timing for each phase to stdout via `[agent]` prefixed lines.
1840
+ * Reports timing for each phase through `@alineo-labs/logger` (component `agent`, tagged with
1841
+ * the spec `name`). Silent by default — set `ALINEO_LOG_LEVEL=info` to see it.
1764
1842
  */
1765
1843
  static async load(spec, opts) {
1766
1844
  const r = await loadAgent(spec, opts);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alineo",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "license": "Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,9 +28,10 @@
28
28
  "test": "bun test"
29
29
  },
30
30
  "dependencies": {
31
- "@alineo-labs/core": "0.4.1",
32
- "@alineo-labs/memory": "0.2.2",
33
- "@alineo-labs/sandbox": "0.4.1",
31
+ "@alineo-labs/core": "0.4.2",
32
+ "@alineo-labs/logger": "0.1.0",
33
+ "@alineo-labs/memory": "0.2.3",
34
+ "@alineo-labs/sandbox": "0.4.3",
34
35
  "zod": "^4.4.3"
35
36
  },
36
37
  "devDependencies": {