alineo 0.5.1 → 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 +2 -1
- package/dist/index.mjs +86 -34
- package/package.json +5 -4
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
|
-
*
|
|
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;
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1112
|
+
log.warn("snapshot restore failed, rebuilding", { reason });
|
|
1095
1113
|
await store.delete(spec.name);
|
|
1096
1114
|
}
|
|
1097
1115
|
}
|
|
1098
1116
|
if (!fromSnapshot) {
|
|
1099
|
-
|
|
1117
|
+
log.info("starting sandbox");
|
|
1100
1118
|
const t1 = Date.now();
|
|
1101
1119
|
sb = await client.sandbox({
|
|
1102
1120
|
image: "node:22",
|
|
@@ -1112,21 +1130,27 @@ async function loadAgent(specInput, opts) {
|
|
|
1112
1130
|
resourceId: spec.resourceId ?? spec.name,
|
|
1113
1131
|
teamId: spec.teamId
|
|
1114
1132
|
});
|
|
1115
|
-
|
|
1133
|
+
log.info("sandbox ready", {
|
|
1134
|
+
elapsed: elapsed(t1),
|
|
1135
|
+
sandboxId: sb.sandboxId
|
|
1136
|
+
});
|
|
1116
1137
|
const created = sb;
|
|
1117
1138
|
try {
|
|
1118
|
-
|
|
1139
|
+
log.info("installing Pi CLI");
|
|
1119
1140
|
const t2 = Date.now();
|
|
1120
1141
|
await adapter.install(created, spec);
|
|
1121
|
-
|
|
1142
|
+
log.info("Pi CLI ready", { elapsed: elapsed(t2) });
|
|
1122
1143
|
for (const step of spec.setup ?? []) {
|
|
1123
|
-
|
|
1144
|
+
log.info("setup step", { step: step.name });
|
|
1124
1145
|
const ts = Date.now();
|
|
1125
1146
|
const cmd = step.cwd ? `cd ${step.cwd} && ${step.run}` : step.run;
|
|
1126
1147
|
await created.exec(cmd);
|
|
1127
|
-
|
|
1148
|
+
log.info("setup step done", {
|
|
1149
|
+
step: step.name,
|
|
1150
|
+
elapsed: elapsed(ts)
|
|
1151
|
+
});
|
|
1128
1152
|
}
|
|
1129
|
-
|
|
1153
|
+
log.info("checkpointing");
|
|
1130
1154
|
const t3 = Date.now();
|
|
1131
1155
|
const snapshotId = await created.checkpoint();
|
|
1132
1156
|
await store.save({
|
|
@@ -1135,9 +1159,9 @@ async function loadAgent(specInput, opts) {
|
|
|
1135
1159
|
snapshotId,
|
|
1136
1160
|
createdAt: Date.now()
|
|
1137
1161
|
});
|
|
1138
|
-
|
|
1162
|
+
log.info("checkpoint done", { elapsed: elapsed(t3) });
|
|
1139
1163
|
} catch (err) {
|
|
1140
|
-
await created.close().catch((
|
|
1164
|
+
await created.close().catch(cleanupFailed(log, { sandboxId: created.sandboxId }));
|
|
1141
1165
|
await egressGate?.stop().catch(() => {});
|
|
1142
1166
|
throw err;
|
|
1143
1167
|
}
|
|
@@ -1151,24 +1175,27 @@ async function loadAgent(specInput, opts) {
|
|
|
1151
1175
|
}
|
|
1152
1176
|
resolvedEnv.ALINEO_SANDBOX_ID = sb.sandboxId;
|
|
1153
1177
|
await adapter.configure(sb, spec, resolvedEnv);
|
|
1154
|
-
|
|
1178
|
+
log.info("starting bridge");
|
|
1155
1179
|
const t4 = Date.now();
|
|
1156
1180
|
await adapter.startBridge(sb);
|
|
1157
1181
|
await adapter.waitReady();
|
|
1158
|
-
|
|
1182
|
+
log.info("bridge ready", { elapsed: elapsed(t4) });
|
|
1159
1183
|
} catch (err) {
|
|
1160
|
-
await sb.close().catch((
|
|
1184
|
+
await sb.close().catch(cleanupFailed(log, { sandboxId: sb.sandboxId }));
|
|
1161
1185
|
await egressGate?.stop().catch(() => {});
|
|
1162
1186
|
if (!fromSnapshot) throw err;
|
|
1163
1187
|
const reason = err instanceof Error ? err.message : String(err);
|
|
1164
|
-
|
|
1188
|
+
log.warn("restored snapshot didn't start — discarding it and rebuilding", { reason });
|
|
1165
1189
|
await store.delete(spec.name);
|
|
1166
1190
|
return loadAgent(specInput, {
|
|
1167
1191
|
...opts,
|
|
1168
1192
|
rebuild: true
|
|
1169
1193
|
});
|
|
1170
1194
|
}
|
|
1171
|
-
|
|
1195
|
+
log.info("total", {
|
|
1196
|
+
elapsed: elapsed(t0),
|
|
1197
|
+
fromSnapshot
|
|
1198
|
+
});
|
|
1172
1199
|
return {
|
|
1173
1200
|
sandbox: sb,
|
|
1174
1201
|
spec,
|
|
@@ -1210,8 +1237,12 @@ async function resumeAgent(sandboxId, opts) {
|
|
|
1210
1237
|
resolvedEnv.ALINEO_SPAWN_DEPTH = String(spec.spawnDepth);
|
|
1211
1238
|
}
|
|
1212
1239
|
const runId = opts.runId ?? crypto.randomUUID();
|
|
1240
|
+
const log = agentLog.child(opts.runId ? {
|
|
1241
|
+
name: spec.name,
|
|
1242
|
+
runId
|
|
1243
|
+
} : { name: spec.name });
|
|
1213
1244
|
resolvedEnv.ALINEO_RUN_ID = runId;
|
|
1214
|
-
|
|
1245
|
+
log.info("reconnecting", { sandboxId });
|
|
1215
1246
|
const t1 = Date.now();
|
|
1216
1247
|
const sb = await client.connect(sandboxId, spec.name, {
|
|
1217
1248
|
runId,
|
|
@@ -1219,18 +1250,18 @@ async function resumeAgent(sandboxId, opts) {
|
|
|
1219
1250
|
resourceId: spec.resourceId ?? spec.name,
|
|
1220
1251
|
teamId: spec.teamId
|
|
1221
1252
|
});
|
|
1222
|
-
|
|
1253
|
+
log.info("connected", { elapsed: elapsed(t1) });
|
|
1223
1254
|
await sb.exec("pkill -f 'node /alineo-bridge.js' 2>/dev/null; sleep 0.1; true", { strict: false });
|
|
1224
1255
|
const adapter = new PiAdapter();
|
|
1225
1256
|
resolvedEnv.ALINEO_SANDBOX_ID = sandboxId;
|
|
1226
1257
|
await adapter.configure(sb, spec, resolvedEnv, { resume: true });
|
|
1227
|
-
|
|
1258
|
+
log.info("starting bridge");
|
|
1228
1259
|
const t2 = Date.now();
|
|
1229
1260
|
await adapter.startBridge(sb);
|
|
1230
1261
|
await adapter.waitReady();
|
|
1231
|
-
|
|
1262
|
+
log.info("bridge ready", { elapsed: elapsed(t2) });
|
|
1232
1263
|
await reconcileDroppedPermissions(opts.adapter, sb, spec.name, sandboxId);
|
|
1233
|
-
|
|
1264
|
+
log.info("total", { elapsed: elapsed(t0) });
|
|
1234
1265
|
return {
|
|
1235
1266
|
sandbox: sb,
|
|
1236
1267
|
spec,
|
|
@@ -1292,9 +1323,13 @@ async function reattachAgent(sandboxId, opts) {
|
|
|
1292
1323
|
resolvedEnv.ALINEO_SPAWN_DEPTH = String(spec.spawnDepth);
|
|
1293
1324
|
}
|
|
1294
1325
|
const runId = opts.runId ?? crypto.randomUUID();
|
|
1326
|
+
const log = agentLog.child(opts.runId ? {
|
|
1327
|
+
name: spec.name,
|
|
1328
|
+
runId
|
|
1329
|
+
} : { name: spec.name });
|
|
1295
1330
|
resolvedEnv.ALINEO_RUN_ID = runId;
|
|
1296
1331
|
resolvedEnv.ALINEO_SANDBOX_ID = sandboxId;
|
|
1297
|
-
|
|
1332
|
+
log.info("reattaching", { sandboxId });
|
|
1298
1333
|
const t1 = Date.now();
|
|
1299
1334
|
const sb = await client.connect(sandboxId, spec.name, {
|
|
1300
1335
|
runId,
|
|
@@ -1303,13 +1338,16 @@ async function reattachAgent(sandboxId, opts) {
|
|
|
1303
1338
|
resources: spec.resources ?? config.defaults.resources,
|
|
1304
1339
|
allowPaused: opts.skipReadyCheck === true
|
|
1305
1340
|
});
|
|
1306
|
-
|
|
1341
|
+
log.info("connected", { elapsed: elapsed(t1) });
|
|
1307
1342
|
const adapter = new PiAdapter();
|
|
1308
1343
|
await adapter.reattachBridge(sb);
|
|
1309
1344
|
const t2 = Date.now();
|
|
1310
1345
|
if (!opts.skipReadyCheck) await adapter.waitReady(5e3);
|
|
1311
|
-
|
|
1312
|
-
|
|
1346
|
+
log.info("bridge reattached", {
|
|
1347
|
+
elapsed: elapsed(t2),
|
|
1348
|
+
probed: !opts.skipReadyCheck
|
|
1349
|
+
});
|
|
1350
|
+
log.info("total", { elapsed: elapsed(t0) });
|
|
1313
1351
|
return {
|
|
1314
1352
|
sandbox: sb,
|
|
1315
1353
|
spec,
|
|
@@ -1366,30 +1404,37 @@ async function spawnChild(self, childSpecPath, opts = {}) {
|
|
|
1366
1404
|
childEnv.ALINEO_SPAWN_DEPTH = String(parentDepth - 1);
|
|
1367
1405
|
if (parentMax !== void 0) childEnv.ALINEO_MAX_AGENTS = String(parentMax - 1);
|
|
1368
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 });
|
|
1369
1411
|
childEnv.ALINEO_RUN_ID = runId;
|
|
1370
1412
|
const childCredentialBindings = extractCredentialBindings(childSpec.env ?? {});
|
|
1371
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.`);
|
|
1372
1414
|
const childResourceId = resolveChildResourceId(childSpec);
|
|
1373
|
-
|
|
1415
|
+
log.info("forking sandbox for spawn");
|
|
1374
1416
|
const t0 = Date.now();
|
|
1375
1417
|
const forkedSb = await self.sandbox.fork(childSpec.name, runId, {
|
|
1376
1418
|
credentialProxy: childCredentialBindings.length > 0,
|
|
1377
1419
|
resourceId: childResourceId,
|
|
1378
1420
|
teamId: childSpec.teamId
|
|
1379
1421
|
});
|
|
1380
|
-
|
|
1422
|
+
log.info("fork ready", {
|
|
1423
|
+
elapsed: elapsed(t0),
|
|
1424
|
+
sandboxId: forkedSb.sandboxId
|
|
1425
|
+
});
|
|
1381
1426
|
const adapter = new PiAdapter();
|
|
1382
1427
|
try {
|
|
1383
1428
|
for (const { name, value, binding, source } of childCredentialBindings) await forkedSb.credentials.set(name, value, binding, source);
|
|
1384
1429
|
childEnv.ALINEO_SANDBOX_ID = forkedSb.sandboxId;
|
|
1385
1430
|
await adapter.configure(forkedSb, childSpec, childEnv);
|
|
1386
|
-
|
|
1431
|
+
log.info("starting bridge");
|
|
1387
1432
|
const t1 = Date.now();
|
|
1388
1433
|
await adapter.startBridge(forkedSb, Object.keys(self.env));
|
|
1389
1434
|
await adapter.waitReady();
|
|
1390
|
-
|
|
1435
|
+
log.info("bridge ready", { elapsed: elapsed(t1) });
|
|
1391
1436
|
} catch (err) {
|
|
1392
|
-
await forkedSb.close().catch((
|
|
1437
|
+
await forkedSb.close().catch(cleanupFailed(log, { sandboxId: forkedSb.sandboxId }));
|
|
1393
1438
|
throw err;
|
|
1394
1439
|
}
|
|
1395
1440
|
return {
|
|
@@ -1425,12 +1470,13 @@ async function forkChildMemory(parentMemory, parentRef, child) {
|
|
|
1425
1470
|
try {
|
|
1426
1471
|
await parentMemory.fork(parentRef, child.resourceRef.resourceId);
|
|
1427
1472
|
} catch (err) {
|
|
1428
|
-
await child.close().catch((
|
|
1473
|
+
await child.close().catch(cleanupFailed(agentLog, { resourceId: child.resourceRef.resourceId }));
|
|
1429
1474
|
throw err;
|
|
1430
1475
|
}
|
|
1431
1476
|
}
|
|
1432
1477
|
//#endregion
|
|
1433
1478
|
//#region src/agent/session-control.ts
|
|
1479
|
+
const log = getLogger("agent");
|
|
1434
1480
|
/**
|
|
1435
1481
|
* Wrap a raw adapter stream so that every permission event is mirrored to the ledger
|
|
1436
1482
|
* (`PermissionRequested` / `PermissionResolved`) and, when `onPermission` is given, each
|
|
@@ -1452,7 +1498,12 @@ async function* instrument(a, stream, onPermission) {
|
|
|
1452
1498
|
target: ev.target,
|
|
1453
1499
|
title: ev.title
|
|
1454
1500
|
};
|
|
1455
|
-
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
|
+
});
|
|
1456
1507
|
}
|
|
1457
1508
|
} else if (ev.type === "permission_resolved") a.sandbox.emit(LedgerEvent.PermissionResolved, -1, {
|
|
1458
1509
|
requestId: ev.requestId,
|
|
@@ -1786,7 +1837,8 @@ var Alineo = class Alineo {
|
|
|
1786
1837
|
* `--depth` CLI flag) — standard flag-beats-config precedence. Same for
|
|
1787
1838
|
* `{ maxAgents }` and `--max`.
|
|
1788
1839
|
*
|
|
1789
|
-
*
|
|
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.
|
|
1790
1842
|
*/
|
|
1791
1843
|
static async load(spec, opts) {
|
|
1792
1844
|
const r = await loadAgent(spec, opts);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "alineo",
|
|
3
|
-
"version": "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.
|
|
32
|
-
"@alineo-labs/
|
|
33
|
-
"@alineo-labs/
|
|
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": {
|