acp-kernel 0.0.40 → 0.0.41-pr.135.2

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.
@@ -919,15 +919,194 @@ function mirrorResponsesToCore(view, systemText) {
919
919
  });
920
920
  return msgs;
921
921
  }
922
+
923
+ // src/wire/compress-detect.ts
924
+ import { createHash as createHash2 } from "crypto";
925
+ function compressToolArgs(call) {
926
+ let args = call.arguments;
927
+ if (typeof args === "string") {
928
+ try {
929
+ args = JSON.parse(args);
930
+ } catch {
931
+ return null;
932
+ }
933
+ }
934
+ if (!args || typeof args !== "object" || Array.isArray(args)) return null;
935
+ const a = args;
936
+ if (call.name === "compress") {
937
+ return Array.isArray(a.content) ? { content: a.content, topic: a.topic, summaryMaxChars: a.summaryMaxChars } : null;
938
+ }
939
+ if (call.name !== "write") return null;
940
+ const path = typeof a.path === "string" ? a.path.split("?")[0].replace(/\/+$/, "") : "";
941
+ if (path !== "xd://compress") return null;
942
+ let inner = a.content;
943
+ if (typeof inner === "string") {
944
+ try {
945
+ inner = JSON.parse(inner);
946
+ } catch {
947
+ return null;
948
+ }
949
+ }
950
+ if (!inner || typeof inner !== "object") return null;
951
+ if (Array.isArray(inner)) return { content: inner };
952
+ const ia = inner;
953
+ return Array.isArray(ia.content) ? { content: ia.content, topic: ia.topic, summaryMaxChars: ia.summaryMaxChars } : { content: [ia] };
954
+ }
955
+ function toolCallNames(msgs) {
956
+ const names = /* @__PURE__ */ new Map();
957
+ for (const m of msgs) {
958
+ if (m.contentType === "tool-call" && m.toolCallId && m.toolName) names.set(m.toolCallId, m.toolName);
959
+ }
960
+ return names;
961
+ }
962
+ function toolResultTextsCore(msgs) {
963
+ const results = /* @__PURE__ */ new Map();
964
+ for (const m of msgs) {
965
+ if (m.contentType !== "tool-result" || !m.toolCallId) continue;
966
+ results.set(m.toolCallId, m.text ?? "");
967
+ }
968
+ return results;
969
+ }
970
+ function findCompressCallsCore(msg) {
971
+ if (msg.contentType !== "tool-call" || !msg.toolName) return [];
972
+ const args = compressToolArgs({ name: msg.toolName, arguments: msg.text });
973
+ if (!args) return [];
974
+ const content = args.content;
975
+ if (!Array.isArray(content)) return [];
976
+ const ranges = [];
977
+ const callTopic = typeof args.topic === "string" ? args.topic : void 0;
978
+ for (const item of content) {
979
+ const r = item;
980
+ if (typeof r.startId !== "string" || typeof r.endId !== "string" || typeof r.summary !== "string" || r.summary.length === 0) continue;
981
+ ranges.push({
982
+ startRef: r.startId,
983
+ endRef: r.endId,
984
+ summary: r.summary,
985
+ topic: typeof r.topic === "string" ? r.topic : callTopic,
986
+ summaryMaxChars: typeof args.summaryMaxChars === "number" ? args.summaryMaxChars : void 0,
987
+ compressCallId: msg.toolCallId ?? ""
988
+ });
989
+ }
990
+ return ranges.length > 0 ? [{ id: msg.toolCallId ?? "", ranges }] : [];
991
+ }
992
+ function corePieceKey(cm) {
993
+ return `${cm.role}|${cm.contentType}|${cm.toolName ?? ""}|${(cm.text ?? "").slice(0, 4096)}`;
994
+ }
995
+ function spanFingerprintCore(coreMessages, startId, endId) {
996
+ const find = (id) => coreMessages.find((cm) => cm.id === id);
997
+ const first = find(startId);
998
+ const last = find(endId);
999
+ if (!first || !last) return "";
1000
+ return createHash2("sha1").update(`${corePieceKey(first)}\0${corePieceKey(last)}`).digest("hex").slice(0, 8);
1001
+ }
1002
+ function spanFingerprintCoreIdx(coreMessages, startIdx, endIdx) {
1003
+ const first = coreMessages[startIdx];
1004
+ const last = coreMessages[endIdx];
1005
+ if (!first || !last) return "";
1006
+ return createHash2("sha1").update(`${corePieceKey(first)}\0${corePieceKey(last)}`).digest("hex").slice(0, 8);
1007
+ }
1008
+ function boundaryRawCore(ref, byRef, blocks, coreMessages, pick) {
1009
+ const raw = byRef[ref];
1010
+ if (raw) return raw;
1011
+ const m = /^b(\d+)$/i.exec(ref.trim());
1012
+ if (!m) return "";
1013
+ const block = blocks.find((b) => b.blockId.toLowerCase() === `b${m[1]}`);
1014
+ if (!block) return "";
1015
+ const idx = (id) => coreMessages.findIndex((cm) => cm.id === (byRef[id] ?? id));
1016
+ let best = -1;
1017
+ for (const id of block.effectiveMessageIds) {
1018
+ const i = idx(id);
1019
+ if (i < 0) continue;
1020
+ if (best < 0 || (pick === "min" ? i < best : i > best)) best = i;
1021
+ }
1022
+ return best < 0 ? "" : coreMessages[best]?.id ?? "";
1023
+ }
1024
+ function boundaryIndexCore(ref, byRef, blocks, coreMessages, pick, fallbackIdx = -1) {
1025
+ const id = boundaryRawCore(ref, byRef, blocks, coreMessages, pick);
1026
+ if (id) {
1027
+ const i = coreMessages.findIndex((cm) => cm.id === id);
1028
+ if (i >= 0) return i;
1029
+ }
1030
+ return fallbackIdx >= 0 && fallbackIdx < coreMessages.length ? fallbackIdx : -1;
1031
+ }
1032
+ function refOfPieceCore(coreMessages, idx, byRef) {
1033
+ const id = coreMessages[idx]?.id;
1034
+ if (!id) return "";
1035
+ for (const [ref, mapped] of Object.entries(byRef)) if (mapped === id) return ref;
1036
+ return "";
1037
+ }
1038
+ function staleRangeCore(r, rangeIndex, resultText, coreMessages, callIndex, byRef, blocks) {
1039
+ const pm = resultText.match(/\[pos=([0-9,-]+)\]/);
1040
+ const pair = pm ? pm[1].split(",")[rangeIndex] ?? "-" : "-";
1041
+ const hinted = pair !== "-";
1042
+ const [ps, pe] = pair === "-" ? ["", ""] : pair.split("-");
1043
+ const fbStart = ps && ps !== "" ? Number.parseInt(ps, 10) : -1;
1044
+ const fbEnd = pe && pe !== "" ? Number.parseInt(pe, 10) : -1;
1045
+ const startRaw = boundaryRawCore(r.startRef, byRef, blocks, coreMessages, "min");
1046
+ const endRaw = boundaryRawCore(r.endRef, byRef, blocks, coreMessages, "max");
1047
+ const rawStartIdx = startRaw ? coreMessages.findIndex((cm) => cm.id === startRaw) : -1;
1048
+ const rawEndIdx = endRaw ? coreMessages.findIndex((cm) => cm.id === endRaw) : -1;
1049
+ const startIdx = rawStartIdx >= 0 ? rawStartIdx : fbStart >= 0 && fbStart < coreMessages.length ? fbStart : -1;
1050
+ const endIdx = rawEndIdx >= 0 ? rawEndIdx : fbEnd >= 0 && fbEnd < coreMessages.length ? fbEnd : -1;
1051
+ if (startIdx < 0 || endIdx < 0) {
1052
+ if (!/^b\d+$/i.test(r.startRef.trim()) && !/^b\d+$/i.test(r.endRef.trim()))
1053
+ return { reject: `unresolved ${r.startRef}..${r.endRef} -> ${startIdx}..${endIdx}`, ...hinted ? { hint: true } : {} };
1054
+ return {};
1055
+ }
1056
+ if (endIdx > callIndex) return { reject: `end idx ${endIdx} > callIndex ${callIndex}`, ...hinted ? { hint: true } : {} };
1057
+ const m = resultText.match(/\[fp=([0-9a-f,-]+)\]/);
1058
+ if (m) {
1059
+ const want = m[1].split(",")[rangeIndex];
1060
+ if (want !== void 0 && want !== "-") {
1061
+ const got = spanFingerprintCoreIdx(coreMessages, startIdx, endIdx);
1062
+ if (want !== got) return { reject: `fp ${r.startRef}..${r.endRef} want ${want} got ${got} @${startIdx}..${endIdx}`, ...hinted ? { hint: true } : {} };
1063
+ }
1064
+ }
1065
+ const remap = {};
1066
+ if (/^m\d+$/i.test(r.startRef.trim()) && rawStartIdx < 0) {
1067
+ const ref = refOfPieceCore(coreMessages, startIdx, byRef);
1068
+ if (!ref) return { reject: `recovered ${r.startRef} @${startIdx} has no ref (protected piece)`, ...hinted ? { hint: true } : {} };
1069
+ remap.startRef = ref;
1070
+ }
1071
+ if (/^m\d+$/i.test(r.endRef.trim()) && rawEndIdx < 0) {
1072
+ const ref = refOfPieceCore(coreMessages, endIdx, byRef);
1073
+ if (!ref) return { reject: `recovered ${r.endRef} @${endIdx} has no ref (protected piece)`, ...hinted ? { hint: true } : {} };
1074
+ remap.endRef = ref;
1075
+ }
1076
+ if (!remap.startRef && !remap.endRef) return {};
1077
+ return { remap, recovered: { pos: pair, startIdx, endIdx } };
1078
+ }
1079
+ function rangeFingerprintsCore(ranges, coreMessages, byRef, blocks) {
1080
+ return ranges.map((r) => {
1081
+ const start = boundaryRawCore(r.startRef, byRef, blocks, coreMessages, "min");
1082
+ const end = start ? boundaryRawCore(r.endRef, byRef, blocks, coreMessages, "max") : "";
1083
+ if (start && end) {
1084
+ const fp = spanFingerprintCore(coreMessages, start, end);
1085
+ if (fp.length > 0) return fp;
1086
+ }
1087
+ return "-";
1088
+ });
1089
+ }
1090
+ function rangePositionsCore(ranges, coreMessages, byRef, blocks) {
1091
+ return ranges.map((r) => {
1092
+ const s = boundaryIndexCore(r.startRef, byRef, blocks, coreMessages, "min");
1093
+ const e = s >= 0 ? boundaryIndexCore(r.endRef, byRef, blocks, coreMessages, "max") : -1;
1094
+ return s >= 0 && e >= 0 ? `${s}-${e}` : "-";
1095
+ });
1096
+ }
922
1097
  export {
923
1098
  ClusterCounter,
924
1099
  WIRE_FORMATS,
925
1100
  anthropicToCore,
1101
+ boundaryIndexCore,
1102
+ boundaryRawCore,
926
1103
  buildSystem,
1104
+ compressToolArgs,
927
1105
  conversationIdentityResponses,
928
1106
  conversationSignalAnthropic,
929
1107
  conversationSignalOpenai,
930
1108
  conversationSignalResponses,
1109
+ corePieceKey,
931
1110
  coreToAnthropic,
932
1111
  coreToOpenai,
933
1112
  coreToResponses,
@@ -935,6 +1114,7 @@ export {
935
1114
  deriveMessageId,
936
1115
  detectWireFormat,
937
1116
  extractSystem,
1117
+ findCompressCallsCore,
938
1118
  hashId,
939
1119
  injectOpenaiSystem,
940
1120
  injectResponsesDeveloperMessage,
@@ -948,9 +1128,17 @@ export {
948
1128
  openaiToCore,
949
1129
  parseDataUrl,
950
1130
  patchResponsesInput,
1131
+ rangeFingerprintsCore,
1132
+ rangePositionsCore,
1133
+ refOfPieceCore,
951
1134
  responsesToCore,
1135
+ spanFingerprintCore,
1136
+ spanFingerprintCoreIdx,
952
1137
  splitDemotedThinking,
1138
+ staleRangeCore,
953
1139
  subagentNamespace,
954
- toCoreMessages
1140
+ toCoreMessages,
1141
+ toolCallNames,
1142
+ toolResultTextsCore
955
1143
  };
956
1144
  //# sourceMappingURL=index.js.map