@agentsky/sky 3.39.0 → 3.42.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.
@@ -2,7 +2,7 @@ import {
2
2
  TRACE_CONTENT_SCOPES,
3
3
  USER_AGENT,
4
4
  redactTraceText
5
- } from "./chunk-SC5RAJVJ.js";
5
+ } from "./chunk-YY2ZSZDH.js";
6
6
  import {
7
7
  configPath
8
8
  } from "./chunk-XHZJG3QT.js";
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  ApiError,
3
3
  uploadLargeFile
4
- } from "./chunk-7A3BLBKU.js";
4
+ } from "./chunk-ALJRHTMX.js";
5
5
  import {
6
6
  SessionSourceCollector,
7
7
  USER_AGENT,
@@ -9,7 +9,7 @@ import {
9
9
  publicRemote,
10
10
  redactLine,
11
11
  redactTraceText
12
- } from "./chunk-SC5RAJVJ.js";
12
+ } from "./chunk-YY2ZSZDH.js";
13
13
  import {
14
14
  apiBase,
15
15
  apiToken
@@ -388,6 +388,88 @@ function removePendingSource(pending) {
388
388
  rmSync2(completed, { recursive: true });
389
389
  }
390
390
 
391
+ // src/trace-session.ts
392
+ var SESSION_HARNESSES = ["claude_code", "codex"];
393
+ var UPLOADED_MESSAGE_PREFIX = "trace:";
394
+ var SESSION_BACKFILL_DAYS = 30;
395
+ var BATCH_MESSAGES = 200;
396
+ var BATCH_BYTES = 3 * 1024 * 1024;
397
+ var MESSAGE_BYTES = 384 * 1024;
398
+ var HARNESS_BLOCKS = /<(system-reminder|local-command-stdout|local-command-stderr|command-message|command-args|task-notification)>[\s\S]*?<\/\1>/g;
399
+ function sessionHarness(harness) {
400
+ return SESSION_HARNESSES.includes(harness);
401
+ }
402
+ var bytesOf = (value) => Buffer.byteLength(JSON.stringify(value));
403
+ function transcriptMessages(rows, startedAt) {
404
+ const messages = [];
405
+ let at = startedAt && Number.isFinite(Date.parse(startedAt)) ? new Date(startedAt).toISOString() : (/* @__PURE__ */ new Date(0)).toISOString();
406
+ let current = null;
407
+ rows.forEach((row, index) => {
408
+ if (row.at && Number.isFinite(Date.parse(row.at))) at = new Date(row.at).toISOString();
409
+ if (row.role === "tool") return;
410
+ const role = row.role === "user" ? "user" : "agent";
411
+ const text = role === "user" ? row.text.replace(HARNESS_BLOCKS, "").trim() : row.text;
412
+ if (!text) return;
413
+ if (!current || current.role !== role) {
414
+ current = { id: `${UPLOADED_MESSAGE_PREFIX}${index}`, role, created_at: at, parts: [], bytes: 0 };
415
+ messages.push(current);
416
+ }
417
+ const part = { type: "text", index: current.parts.length, text };
418
+ const size = bytesOf(part);
419
+ if (current.bytes + size > MESSAGE_BYTES) return;
420
+ current.parts.push(part);
421
+ current.bytes += size;
422
+ });
423
+ return messages.filter((message) => message.parts.length).map(({ id, role, created_at, parts }) => ({ id, role, created_at, parts }));
424
+ }
425
+ function messageBatches(messages) {
426
+ const batches = [];
427
+ let batch = [];
428
+ let bytes = 0;
429
+ for (const message of messages) {
430
+ const size = bytesOf(message);
431
+ if (batch.length && (batch.length >= BATCH_MESSAGES || bytes + size > BATCH_BYTES)) {
432
+ batches.push(batch);
433
+ batch = [];
434
+ bytes = 0;
435
+ }
436
+ batch.push(message);
437
+ bytes += size;
438
+ }
439
+ if (batch.length) batches.push(batch);
440
+ return batches;
441
+ }
442
+ var refusal = (error) => error instanceof ApiError && error.status !== 429 && (error.status >= 400 && error.status < 500 || error.status === 503);
443
+ async function saveTraceSession(request, input, now = Date.now()) {
444
+ const { harness, sessionId, workspace, body } = input;
445
+ const last = Date.parse(body.ended_at ?? body.started_at ?? "");
446
+ if (Number.isFinite(last) && last < now - SESSION_BACKFILL_DAYS * 864e5) return { status: "older" };
447
+ try {
448
+ const opened = await request("POST", "/threads/local", workspace, {
449
+ harness,
450
+ sourceSessionId: sessionId,
451
+ origin: "terminal",
452
+ ...body.model ? { llm: body.model } : {},
453
+ ...body.title ? { title: body.title.slice(0, 120) } : {},
454
+ ...body.cwd ? { folder: body.cwd } : {},
455
+ machine: body.device.slice(0, 120)
456
+ });
457
+ if (opened.workspace) return { status: "elsewhere", sessionId: opened.sessionId, workspace: opened.workspace };
458
+ let stored = 0;
459
+ for (const [index, messages] of messageBatches(transcriptMessages(body.messages, body.started_at)).entries()) {
460
+ const answer = await request("PUT", `/sessions/${encodeURIComponent(opened.sessionId)}/mirror`, workspace, {
461
+ messages,
462
+ ...index === 0 && body.title ? { title: body.title.slice(0, 120) } : {}
463
+ });
464
+ stored += answer.stored;
465
+ }
466
+ return { status: "saved", sessionId: opened.sessionId, stored };
467
+ } catch (error) {
468
+ if (refusal(error)) return { status: "refused", code: error.code };
469
+ throw error;
470
+ }
471
+ }
472
+
391
473
  // src/trace-library.ts
392
474
  var MESSAGE_MAX_BYTES = 8 * 1024;
393
475
  var TOOL_MAX_BYTES = 2 * 1024;
@@ -628,9 +710,10 @@ function stringify(value) {
628
710
  }
629
711
  var Collector = class {
630
712
  messages = [];
713
+ conversation = [];
631
714
  partial = false;
632
715
  bytes = 0;
633
- push(role, text, at) {
716
+ push(role, text, at, inConversation = true) {
634
717
  const trimmed = text.replace(/\u0000/g, "").trim();
635
718
  if (!trimmed) return;
636
719
  if (this.messages.length >= MESSAGES_MAX) {
@@ -641,6 +724,7 @@ var Collector = class {
641
724
  if (safe.truncated) this.partial = true;
642
725
  this.bytes += Buffer.byteLength(safe.text) + 80;
643
726
  this.messages.push({ role, text: safe.text, ...at ? { at } : {} });
727
+ this.conversation.push(inConversation);
644
728
  }
645
729
  };
646
730
  function isoOrUndefined(value) {
@@ -655,7 +739,7 @@ async function collectorBody(trace, device, scrubber) {
655
739
  return sink.toLibrary({ ...scrubbed, cwd: trace.cwd, stats: trace.stats }, device);
656
740
  }
657
741
  async function parseClaudeSession(session) {
658
- const out = { harness: "claude_code", sessionId: session.sessionId, userTurns: 0, toolCalls: 0, partial: false, messages: [] };
742
+ const out = { harness: "claude_code", sessionId: session.sessionId, userTurns: 0, toolCalls: 0, partial: false, messages: [], conversation: [] };
659
743
  const collector = new Collector();
660
744
  const source = new SessionSourceCollector();
661
745
  const references = new TraceReferences();
@@ -683,10 +767,12 @@ async function parseClaudeSession(session) {
683
767
  }
684
768
  const message = asRecord(record2.message);
685
769
  if (!message || record2.isMeta === true) continue;
770
+ const own = index === 0 && record2.isSidechain !== true && record2.parent_tool_use_id == null;
686
771
  if (record2.type === "user") {
772
+ const spoken = own && asRecord(record2.origin)?.kind !== "task-notification" && record2.promptSource !== "system" && typeof record2.wakeupSource !== "string";
687
773
  const content = message.content;
688
774
  if (typeof content === "string") {
689
- collector.push("user", content, at);
775
+ collector.push("user", content, at, spoken);
690
776
  if (index === 0) out.userTurns += 1;
691
777
  if (index === 0) out.title ??= titleOf(content);
692
778
  continue;
@@ -695,7 +781,7 @@ async function parseClaudeSession(session) {
695
781
  let typed = false;
696
782
  for (const part of content.map(asRecord)) {
697
783
  if (part?.type === "text" && str(part.text)) {
698
- collector.push("user", part.text, at);
784
+ collector.push("user", part.text, at, spoken);
699
785
  typed = true;
700
786
  if (index === 0) out.title ??= titleOf(part.text);
701
787
  } else if (part?.type === "tool_result") {
@@ -706,7 +792,7 @@ async function parseClaudeSession(session) {
706
792
  } else if (record2.type === "assistant") {
707
793
  const content = Array.isArray(message.content) ? message.content.map(asRecord) : [];
708
794
  for (const part of content) {
709
- if (part?.type === "text") collector.push("assistant", str(part.text) ?? "", at);
795
+ if (part?.type === "text") collector.push("assistant", str(part.text) ?? "", at, own);
710
796
  else if (part?.type === "tool_use") {
711
797
  out.toolCalls += 1;
712
798
  collector.push("tool", `${str(part.name) ?? "tool"} ${stringify(part.input)}`, at);
@@ -718,7 +804,7 @@ async function parseClaudeSession(session) {
718
804
  return withWorkLinks(finish(withSource(out, source.result()), collector), references);
719
805
  }
720
806
  async function parseCodexSession(session) {
721
- const out = { harness: "codex", sessionId: session.sessionId, userTurns: 0, toolCalls: 0, partial: false, messages: [] };
807
+ const out = { harness: "codex", sessionId: session.sessionId, userTurns: 0, toolCalls: 0, partial: false, messages: [], conversation: [] };
722
808
  const collector = new Collector();
723
809
  const source = new SessionSourceCollector();
724
810
  const references = new TraceReferences();
@@ -787,6 +873,7 @@ function withSource(out, meta) {
787
873
  }
788
874
  function finish(out, collector) {
789
875
  let messages = collector.messages;
876
+ const conversation = [...collector.conversation];
790
877
  let partial = out.partial || collector.partial;
791
878
  let bytes = collector.bytes;
792
879
  if (bytes > BODY_MAX_BYTES) {
@@ -795,10 +882,12 @@ function finish(out, collector) {
795
882
  partial = true;
796
883
  }
797
884
  while (bytes > BODY_MAX_BYTES && messages.length) {
798
- const dropped = messages.splice(Math.floor(messages.length / 2), Math.max(1, Math.floor(messages.length / 10)));
885
+ const from = Math.floor(messages.length / 2), count = Math.max(1, Math.floor(messages.length / 10));
886
+ const dropped = messages.splice(from, count);
887
+ conversation.splice(from, count);
799
888
  bytes -= dropped.reduce((sum, message) => sum + Buffer.byteLength(message.text) + 80, 0);
800
889
  }
801
- return { ...out, title: out.title ? redactTraceText(out.title, 300).text : void 0, partial, messages };
890
+ return { ...out, title: out.title ? redactTraceText(out.title, 300).text : void 0, partial, messages, conversation };
802
891
  }
803
892
  function isTrivial(session) {
804
893
  if (!session.messages.length) return true;
@@ -952,7 +1041,8 @@ function uploadBody(session, sha256, device) {
952
1041
  user_turns: session.userTurns,
953
1042
  tool_calls: session.toolCalls,
954
1043
  partial: session.partial,
955
- messages: session.messages
1044
+ messages: session.messages,
1045
+ outside_conversation: session.conversation.flatMap((inside, index) => inside ? [] : [index])
956
1046
  };
957
1047
  }
958
1048
  function lockPath() {
@@ -1035,7 +1125,7 @@ async function runSync(config, options) {
1035
1125
  const state = readJson(statePath(), { sessions: {} });
1036
1126
  const full = options.full || !state.lastFullAt || Date.now() - Date.parse(state.lastFullAt) > FULL_SWEEP_MS;
1037
1127
  const device = config.device ?? hostname();
1038
- const report = { scanned: 0, changed: 0, uploaded: 0, unchanged: 0, local: 0, owned: 0, empty: 0, failed: 0, legacySources: 0, byWorkspace: {}, errors: [] };
1128
+ const report = { scanned: 0, changed: 0, uploaded: 0, unchanged: 0, local: 0, owned: 0, empty: 0, sessions: 0, failed: 0, legacySources: 0, byWorkspace: {}, errors: [] };
1039
1129
  const key = (s) => `${s.harness}:${s.sessionId}`;
1040
1130
  const selected = (s) => !options.only?.length || options.only.includes(s.sessionId) || s.files.some((file) => options.only.includes(file));
1041
1131
  const record2 = (s, value) => {
@@ -1099,7 +1189,19 @@ async function runSync(config, options) {
1099
1189
  }, workspace);
1100
1190
  savePendingSource(item, { fileId: file.id });
1101
1191
  }
1102
- const { work_objects: workObjects, ...previewBody } = item.manifest.body;
1192
+ const { work_objects: workObjects, outside_conversation: outside, ...previewBody } = item.manifest.body;
1193
+ if (config.sessions !== false && sessionHarness(local.harness)) {
1194
+ const body = previewBody;
1195
+ const left = new Set(outside ?? []);
1196
+ const saved2 = await saveTraceSession(libraryRequest, {
1197
+ harness: local.harness,
1198
+ sessionId: local.sessionId,
1199
+ workspace,
1200
+ body: { ...body, messages: body.messages.filter((_, index) => !left.has(index)) }
1201
+ });
1202
+ if (saved2.status === "saved") report.sessions++;
1203
+ else if (saved2.status === "refused") options.onProgress?.(`${key(local)}: kept in the trace library only (${saved2.code})`);
1204
+ }
1103
1205
  const saved = await libraryRequest("PUT", `/trace-library/${local.harness}/${encodeURIComponent(local.sessionId)}`, workspace, { ...previewBody, sha256: source.sha256 });
1104
1206
  savePendingSource(item, { previewSaved: true, link: saved.linked_task ?? null });
1105
1207
  const link = item.manifest.link;
@@ -1233,11 +1335,12 @@ function hookSessionRef(payload) {
1233
1335
  }
1234
1336
  }
1235
1337
  var LAUNCHD_LABEL = "dev.agentsky.trace-sync";
1236
- function installLaunchd(command, logPath) {
1237
- if (process.platform !== "darwin") throw new Error("the schedule installer covers macOS; on Linux add `*/15 * * * * sky trace sync --quiet` to your crontab");
1238
- const plist = join3(homedir2(), "Library", "LaunchAgents", `${LAUNCHD_LABEL}.plist`);
1338
+ function launchdPlistPath(home = homedir2()) {
1339
+ return join3(home, "Library", "LaunchAgents", `${LAUNCHD_LABEL}.plist`);
1340
+ }
1341
+ function launchdPlist(command, logPath) {
1239
1342
  const escape = (value) => value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
1240
- writeFileSync2(plist, `<?xml version="1.0" encoding="UTF-8"?>
1343
+ return `<?xml version="1.0" encoding="UTF-8"?>
1241
1344
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
1242
1345
  <plist version="1.0">
1243
1346
  <dict>
@@ -1253,9 +1356,18 @@ ${command.map((part) => ` <string>${escape(part)}</string>`).join("\n")}
1253
1356
  <key>StandardErrorPath</key><string>${escape(logPath)}</string>
1254
1357
  </dict>
1255
1358
  </plist>
1256
- `);
1359
+ `;
1360
+ }
1361
+ function installLaunchd(command, logPath) {
1362
+ if (process.platform !== "darwin") throw new Error("the schedule installer covers macOS; on Linux add `*/15 * * * * sky trace sync --quiet` to your crontab");
1363
+ const plist = launchdPlistPath();
1364
+ mkdirSync3(dirname4(plist), { recursive: true });
1365
+ writeFileSync2(plist, launchdPlist(command, logPath));
1257
1366
  return plist;
1258
1367
  }
1368
+ function syncDir() {
1369
+ return skyDir();
1370
+ }
1259
1371
  export {
1260
1372
  LAUNCHD_LABEL,
1261
1373
  collectorBody,
@@ -1265,6 +1377,8 @@ export {
1265
1377
  importExport,
1266
1378
  installLaunchd,
1267
1379
  isTrivial,
1380
+ launchdPlist,
1381
+ launchdPlistPath,
1268
1382
  libraryRequest,
1269
1383
  loadSyncConfig,
1270
1384
  parseClaudeSession,
@@ -1277,5 +1391,6 @@ export {
1277
1391
  sourceFolders,
1278
1392
  supportedAgents,
1279
1393
  syncConfigPath,
1394
+ syncDir,
1280
1395
  syncTraces
1281
1396
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentsky/sky",
3
- "version": "3.39.0",
3
+ "version": "3.42.0",
4
4
  "description": "sky \u2014 the AgentSky developer CLI: launch and talk to long-lived agents from your terminal.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",