@sema-agent/cli 1.0.150 → 1.0.152

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 (4) hide show
  1. package/npm-shrinkwrap.json +37 -34
  2. package/package.json +7 -7
  3. package/sema-main.js +237852 -217477
  4. package/sema.js +144 -45
package/sema.js CHANGED
@@ -4,19 +4,10 @@ const require = __semaCreateRequire(import.meta.url);
4
4
  // Sema prelude — micro-entry (signal tail + --version fast path); body lives in sema-main.js
5
5
 
6
6
 
7
- // build-src/src/utils/bootSignalTail.ts
8
- import { randomUUID } from "node:crypto";
9
- import { writeSync } from "node:fs";
10
-
11
- // build-src/src/sema/synthesizedResultDenials.ts
12
- function synthesizedResultDenials() {
13
- return { permission_denials: [], _sema_permission_denials_absent: true };
14
- }
15
-
16
7
  // build-src/src/utils/signalExitClaim.ts
17
8
  var CLAIM = /* @__PURE__ */ Symbol.for("sema.signalExitClaim");
18
9
  function claimSignalExit(code) {
19
- if (!Number.isInteger(code) || code <= 0) return;
10
+ if (!Number.isInteger(code) || code < 0) return;
20
11
  const g = globalThis;
21
12
  if (g[CLAIM] === void 0) g[CLAIM] = code;
22
13
  }
@@ -963,56 +954,164 @@ function earlyFacts(argv = processArgvTokens()) {
963
954
  return {
964
955
  print: lexicalKeyPresent(walk, "print"),
965
956
  outputFormat: formats[formats.length - 1],
957
+ verbose: lexicalKeyPresent(walk, "verbose"),
966
958
  versionOnly: argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")
967
959
  };
968
960
  }
961
+ function printLaneNow(argv) {
962
+ if (argv !== void 0) return earlyFacts(argv).print;
963
+ if (active !== void 0) return active.route === "print";
964
+ return earlyFacts().print;
965
+ }
966
+
967
+ // build-src/src/sema/terminalFrame.ts
968
+ import { randomUUID } from "node:crypto";
969
+ import { writeSync } from "node:fs";
970
+ function printLaneOfFormat(format) {
971
+ return format === "stream-json" || format === "json" ? format : "text";
972
+ }
973
+ function synthesizedResultDenials() {
974
+ return { permission_denials: [], _sema_permission_denials_absent: true };
975
+ }
976
+ function zeroUsage() {
977
+ return {
978
+ input_tokens: 0,
979
+ cache_creation_input_tokens: 0,
980
+ cache_read_input_tokens: 0,
981
+ output_tokens: 0,
982
+ server_tool_use: { web_search_requests: 0, web_fetch_requests: 0 },
983
+ service_tier: "standard",
984
+ cache_creation: {
985
+ ephemeral_1h_input_tokens: 0,
986
+ ephemeral_5m_input_tokens: 0
987
+ },
988
+ inference_geo: "",
989
+ iterations: [],
990
+ speed: "standard"
991
+ };
992
+ }
993
+ function causeErrors(cause) {
994
+ switch (cause.kind) {
995
+ case "signal":
996
+ return [`terminated by ${cause.signal}`];
997
+ case "run_error":
998
+ return [...cause.errors];
999
+ case "load_failure":
1000
+ return [cause.message];
1001
+ case "engine_fatal":
1002
+ return [cause.line];
1003
+ }
1004
+ }
1005
+ function shellTerminalFrame(cause, ctx) {
1006
+ const durationMs = ctx.durationMs ?? ledgerDurationMs(Date.now());
1007
+ const reason = cause.kind !== "signal" ? void 0 : ctx.terminalReason === null ? void 0 : ctx.terminalReason ?? ledgerTerminalReason();
1008
+ const frame = {
1009
+ type: "result",
1010
+ subtype: "error_during_execution",
1011
+ duration_ms: durationMs,
1012
+ duration_api_ms: 0,
1013
+ is_error: true,
1014
+ num_turns: 0,
1015
+ stop_reason: null,
1016
+ session_id: ctx.sessionId,
1017
+ total_cost_usd: 0,
1018
+ usage: zeroUsage(),
1019
+ modelUsage: {},
1020
+ ...synthesizedResultDenials(),
1021
+ ...reason !== void 0 ? { terminal_reason: reason } : {},
1022
+ ...cause.kind === "engine_fatal" && typeof cause.errorCode === "string" && cause.errorCode !== "" ? { _sema_error_code: cause.errorCode } : {},
1023
+ uuid: randomUUID(),
1024
+ errors: causeErrors(cause)
1025
+ };
1026
+ return ctx.extra === void 0 ? frame : Object.assign(frame, ctx.extra);
1027
+ }
1028
+ var LEDGER = /* @__PURE__ */ Symbol.for("sema.terminalLedger");
1029
+ function ledger() {
1030
+ const g = globalThis;
1031
+ let l = g[LEDGER];
1032
+ if (l === void 0) {
1033
+ l = { terminating: false, fullHandler: false, turnOpen: false, workStarted: 0, resultsSettled: 0, resultsCommitted: 0, inFlight: /* @__PURE__ */ new Set() };
1034
+ g[LEDGER] = l;
1035
+ }
1036
+ return l;
1037
+ }
1038
+ function claimExitFrame() {
1039
+ const l = ledger();
1040
+ if (l.terminating) return { kind: "none" };
1041
+ l.terminating = true;
1042
+ if (l.held !== void 0 && l.heldAtWork === l.workStarted) {
1043
+ const write = l.held;
1044
+ l.held = void 0;
1045
+ l.resultsSettled = l.workStarted;
1046
+ l.resultsCommitted++;
1047
+ return { kind: "held", write };
1048
+ }
1049
+ l.held = void 0;
1050
+ const outstanding = l.workStarted > l.resultsSettled;
1051
+ const nothingYet = l.workStarted === 0 && l.resultsSettled === 0;
1052
+ if (l.turnOpen || outstanding || nothingYet) return { kind: "synthetic" };
1053
+ return { kind: "none" };
1054
+ }
1055
+ function fullSignalHandlerInstalled() {
1056
+ return ledger().fullHandler;
1057
+ }
1058
+ function ledgerTerminalReason() {
1059
+ const l = ledger();
1060
+ if (!l.turnOpen) return void 0;
1061
+ return l.inFlight.size > 0 ? "aborted_tools" : "aborted_streaming";
1062
+ }
1063
+ function ledgerDurationMs(nowMs) {
1064
+ const l = ledger();
1065
+ const start = l.turnOpen ? l.turnStartedAtMs : l.runStartedAtMs;
1066
+ return start === void 0 ? 0 : Math.max(0, Math.round(nowMs - start));
1067
+ }
1068
+ var TEXT_SIGINT_LINE = "Execution error";
1069
+ var terminalCloseoutSource = null;
1070
+ function takeTerminalCloseout() {
1071
+ return terminalCloseoutSource !== null ? terminalCloseoutSource() : null;
1072
+ }
1073
+ function shellTerminalOutput(cause, lane, frame, verboseBuffer, closeout) {
1074
+ if (lane === "stream-json") {
1075
+ return closeout !== null ? [[1, JSON.stringify(closeout) + "\n"], [1, JSON.stringify(frame) + "\n"]] : [[1, JSON.stringify(frame) + "\n"]];
1076
+ }
1077
+ if (lane === "json") {
1078
+ const doc = verboseBuffer !== void 0 ? [...verboseBuffer, ...closeout !== null ? [closeout] : [], frame] : frame;
1079
+ return [[1, JSON.stringify(doc) + "\n"]];
1080
+ }
1081
+ if (cause.kind === "signal" && cause.signal === "SIGINT") return [[1, TEXT_SIGINT_LINE]];
1082
+ return [[2, `Error: ${frame.errors[0] ?? ""}
1083
+ `]];
1084
+ }
1085
+ function writeShellTerminalSync(cause, lane, frame, verboseBuffer) {
1086
+ try {
1087
+ const closeout = takeTerminalCloseout();
1088
+ for (const [fd, text] of shellTerminalOutput(cause, lane, frame, verboseBuffer, closeout)) writeSync(fd, text);
1089
+ } catch {
1090
+ }
1091
+ }
969
1092
 
970
1093
  // build-src/src/utils/bootSignalTail.ts
971
1094
  var INSTALLED_MARKER = /* @__PURE__ */ Symbol.for("sema.bootSignalTailInstalled");
972
1095
  function installBootSignalTailFrame(argv) {
973
1096
  if (process.platform === "win32") return false;
1097
+ if (!printLaneNow(argv)) return false;
974
1098
  const early2 = earlyFacts(argv);
975
- if (!early2.print || early2.outputFormat !== "stream-json") return false;
976
1099
  const g = globalThis;
977
1100
  if (g[INSTALLED_MARKER] === true) return false;
978
1101
  g[INSTALLED_MARKER] = true;
1102
+ const lane = printLaneOfFormat(early2.outputFormat);
979
1103
  const bootTail = (signal, exitCode) => {
980
1104
  claimSignalExit(exitCode);
981
- if (g[/* @__PURE__ */ Symbol.for("sema.streamJsonSignalTailFrame")] === true) return;
982
- if (g[/* @__PURE__ */ Symbol.for("sema.streamJsonTailEmitted")] !== true) {
983
- g[/* @__PURE__ */ Symbol.for("sema.streamJsonTailEmitted")] = true;
984
- try {
985
- writeSync(
986
- 1,
987
- JSON.stringify({
988
- type: "result",
989
- subtype: "error_during_execution",
990
- duration_ms: 0,
991
- duration_api_ms: 0,
992
- is_error: true,
993
- num_turns: 0,
994
- stop_reason: null,
995
- session_id: "",
996
- total_cost_usd: 0,
997
- usage: {
998
- input_tokens: 0,
999
- output_tokens: 0,
1000
- cache_read_input_tokens: 0,
1001
- cache_creation_input_tokens: 0
1002
- },
1003
- modelUsage: {},
1004
- // L-192① / §31b:开机窗尾帧在引擎回话之前发出 —— 结构上没有拒绝账本可读,
1005
- // 裸 `[]` = 把「没有这本账」说成「零拒绝」。两键合读,单一铸点见 sema/synthesizedResultDenials.ts。
1006
- ...synthesizedResultDenials(),
1007
- uuid: randomUUID(),
1008
- errors: [`terminated by ${signal}`]
1009
- }) + "\n"
1010
- );
1011
- } catch {
1012
- }
1105
+ if (fullSignalHandlerInstalled()) return;
1106
+ const claim = claimExitFrame();
1107
+ if (claim.kind !== "none") {
1108
+ const cause = { kind: "signal", signal };
1109
+ const frame = shellTerminalFrame(cause, { sessionId: "", durationMs: 0, terminalReason: null });
1110
+ writeShellTerminalSync(cause, lane, frame, lane === "json" && early2.verbose ? [] : void 0);
1013
1111
  }
1014
1112
  process.exit(exitCode);
1015
1113
  };
1114
+ process.on("SIGINT", () => bootTail("SIGINT", 0));
1016
1115
  process.on("SIGTERM", () => bootTail("SIGTERM", 143));
1017
1116
  process.on("SIGHUP", () => bootTail("SIGHUP", 129));
1018
1117
  return true;
@@ -1073,7 +1172,7 @@ function nodeFloorRefusalLine(version, range) {
1073
1172
  }
1074
1173
 
1075
1174
  // build-src/src/sema/preludeEntry.ts
1076
- var versionLine = "sema 1.0.150" ? "sema 1.0.150" : "";
1175
+ var versionLine = "sema 1.0.152" ? "sema 1.0.152" : "";
1077
1176
  var early = earlyFacts();
1078
1177
  var wantsVersionFastPath = early.versionOnly;
1079
1178
  var nodeEnginesRange = "^20.19.0 || >=22.12.0" ? "^20.19.0 || >=22.12.0" : "";
@@ -1082,7 +1181,7 @@ if (!wantsVersionFastPath && nodeEnginesRange && nodeVersionSatisfies(process.ve
1082
1181
  process.exit(1);
1083
1182
  }
1084
1183
  var EARLY_INPUT_PRELUDE_SLOT = /* @__PURE__ */ Symbol.for("sema.earlyInputPrelude");
1085
- if (!wantsVersionFastPath && process.stdin.isTTY && !early.print && typeof process.stdin.setRawMode === "function") {
1184
+ if (!wantsVersionFastPath && process.stdin.isTTY && !printLaneNow() && typeof process.stdin.setRawMode === "function") {
1086
1185
  try {
1087
1186
  const chunks = [];
1088
1187
  let stopped = false;