ycodium-acp-executor 0.2.5542 → 0.2.5751

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 (251) hide show
  1. package/README.md +5 -4
  2. package/dist/_private/ycodium-jsonl-stream/appendedJsonl.d.ts +47 -0
  3. package/dist/_private/ycodium-jsonl-stream/appendedJsonl.js +172 -0
  4. package/dist/_private/ycodium-jsonl-stream/claudeSessions.d.ts +63 -0
  5. package/dist/_private/ycodium-jsonl-stream/claudeSessions.js +220 -0
  6. package/dist/_private/ycodium-jsonl-stream/codexSessions.d.ts +41 -0
  7. package/dist/_private/ycodium-jsonl-stream/codexSessions.js +197 -0
  8. package/dist/_private/ycodium-jsonl-stream/history.d.ts +6 -0
  9. package/dist/_private/ycodium-jsonl-stream/history.js +389 -0
  10. package/dist/_private/ycodium-jsonl-stream/historyErrors.d.ts +6 -0
  11. package/dist/_private/ycodium-jsonl-stream/historyErrors.js +6 -0
  12. package/dist/_private/ycodium-jsonl-stream/historyFile.d.ts +19 -0
  13. package/dist/_private/ycodium-jsonl-stream/historyFile.js +117 -0
  14. package/dist/_private/ycodium-jsonl-stream/historyRecord.d.ts +31 -0
  15. package/dist/_private/ycodium-jsonl-stream/historyRecord.js +144 -0
  16. package/dist/_private/ycodium-jsonl-stream/historyWaiting.d.ts +8 -0
  17. package/dist/_private/ycodium-jsonl-stream/historyWaiting.js +142 -0
  18. package/dist/_private/ycodium-jsonl-stream/jsonObjectLine.d.ts +5 -0
  19. package/dist/_private/ycodium-jsonl-stream/jsonObjectLine.js +18 -0
  20. package/dist/_private/ycodium-jsonl-stream/jsonlStream.d.ts +79 -0
  21. package/dist/_private/ycodium-jsonl-stream/jsonlStream.js +603 -0
  22. package/dist/_private/ycodium-jsonl-stream/scanCache.d.ts +69 -0
  23. package/dist/_private/ycodium-jsonl-stream/scanCache.js +106 -0
  24. package/dist/_private/ycodium-jsonl-stream/sessionDirectory.d.ts +5 -0
  25. package/dist/_private/ycodium-jsonl-stream/sessionDirectory.js +32 -0
  26. package/dist/_private/ycodium-jsonl-stream/sessionTranscript.d.ts +3 -1
  27. package/dist/_private/ycodium-jsonl-stream/sessionTranscript.js +13 -0
  28. package/dist/_private/ycodium-jsonl-stream/usageFields.d.ts +5 -0
  29. package/dist/_private/ycodium-jsonl-stream/usageFields.js +11 -0
  30. package/dist/_private/ycodium-workspace-path/directoryScope.d.ts +5 -0
  31. package/dist/_private/ycodium-workspace-path/directoryScope.js +28 -0
  32. package/dist/_private/ycodium-workspace-path/workspacePath.d.ts +19 -0
  33. package/dist/_private/ycodium-workspace-path/workspacePath.js +90 -0
  34. package/dist/adapter-support.d.ts.map +1 -1
  35. package/dist/adapter-support.js.map +1 -1
  36. package/dist/apply-effort.d.ts.map +1 -1
  37. package/dist/apply-effort.js.map +1 -1
  38. package/dist/approvals.d.ts.map +1 -1
  39. package/dist/approvals.js.map +1 -1
  40. package/dist/artifacts.d.ts.map +1 -1
  41. package/dist/artifacts.js.map +1 -1
  42. package/dist/card-defaults.d.ts.map +1 -1
  43. package/dist/card-defaults.js.map +1 -1
  44. package/dist/config-option-wiring.d.ts.map +1 -1
  45. package/dist/config-option-wiring.js.map +1 -1
  46. package/dist/config-options.d.ts.map +1 -1
  47. package/dist/config-options.js.map +1 -1
  48. package/dist/connection.d.ts.map +1 -1
  49. package/dist/connection.js.map +1 -1
  50. package/dist/errors.d.ts.map +1 -1
  51. package/dist/errors.js.map +1 -1
  52. package/dist/event-stream.d.ts.map +1 -1
  53. package/dist/event-stream.js.map +1 -1
  54. package/dist/events.d.ts.map +1 -1
  55. package/dist/events.js.map +1 -1
  56. package/dist/executor/child-sessions.d.ts.map +1 -1
  57. package/dist/executor/child-sessions.js.map +1 -1
  58. package/dist/executor/lifecycle.d.ts.map +1 -1
  59. package/dist/executor/lifecycle.js.map +1 -1
  60. package/dist/executor/prompt-results.d.ts.map +1 -1
  61. package/dist/executor/prompt-results.js.map +1 -1
  62. package/dist/executor/prompt.d.ts.map +1 -1
  63. package/dist/executor/prompt.js.map +1 -1
  64. package/dist/executor/requests.d.ts.map +1 -1
  65. package/dist/executor/requests.js.map +1 -1
  66. package/dist/executor/runtime.d.ts.map +1 -1
  67. package/dist/executor/runtime.js.map +1 -1
  68. package/dist/executor/session.d.ts.map +1 -1
  69. package/dist/executor/session.js +17 -1
  70. package/dist/executor/session.js.map +1 -1
  71. package/dist/executor/settlement.d.ts.map +1 -1
  72. package/dist/executor/settlement.js.map +1 -1
  73. package/dist/executor/subagent-sessions.d.ts.map +1 -1
  74. package/dist/executor/subagent-sessions.js.map +1 -1
  75. package/dist/executor/support.d.ts.map +1 -1
  76. package/dist/executor/support.js.map +1 -1
  77. package/dist/executor/updates.d.ts.map +1 -1
  78. package/dist/executor/updates.js +11 -0
  79. package/dist/executor/updates.js.map +1 -1
  80. package/dist/executor.d.ts.map +1 -1
  81. package/dist/executor.js.map +1 -1
  82. package/dist/failureCategory.d.ts.map +1 -1
  83. package/dist/failureCategory.js.map +1 -1
  84. package/dist/handshake.d.ts.map +1 -1
  85. package/dist/handshake.js.map +1 -1
  86. package/dist/index.d.ts +2 -2
  87. package/dist/index.d.ts.map +1 -1
  88. package/dist/index.js +2 -2
  89. package/dist/index.js.map +1 -1
  90. package/dist/instance-kit.d.ts.map +1 -1
  91. package/dist/instance-kit.js.map +1 -1
  92. package/dist/jsonrpc.d.ts.map +1 -1
  93. package/dist/jsonrpc.js.map +1 -1
  94. package/dist/line-buffer.d.ts.map +1 -1
  95. package/dist/line-buffer.js.map +1 -1
  96. package/dist/mcp-servers.d.ts +26 -4
  97. package/dist/mcp-servers.d.ts.map +1 -1
  98. package/dist/mcp-servers.js +25 -4
  99. package/dist/mcp-servers.js.map +1 -1
  100. package/dist/model-selection.d.ts.map +1 -1
  101. package/dist/model-selection.js.map +1 -1
  102. package/dist/model-send.d.ts.map +1 -1
  103. package/dist/model-send.js.map +1 -1
  104. package/dist/mutex.d.ts.map +1 -1
  105. package/dist/mutex.js.map +1 -1
  106. package/dist/native-session-transcript.d.ts.map +1 -1
  107. package/dist/native-session-transcript.js.map +1 -1
  108. package/dist/native-sessions.d.ts.map +1 -1
  109. package/dist/native-sessions.js.map +1 -1
  110. package/dist/openCommands.d.ts.map +1 -1
  111. package/dist/openCommands.js.map +1 -1
  112. package/dist/permission-statement.d.ts.map +1 -1
  113. package/dist/permission-statement.js.map +1 -1
  114. package/dist/probe-process.d.ts +9 -5
  115. package/dist/probe-process.d.ts.map +1 -1
  116. package/dist/probe-process.js +4 -0
  117. package/dist/probe-process.js.map +1 -1
  118. package/dist/probe-session.d.ts.map +1 -1
  119. package/dist/probe-session.js.map +1 -1
  120. package/dist/probe.d.ts.map +1 -1
  121. package/dist/probe.js.map +1 -1
  122. package/dist/prompt-dispatch.d.ts.map +1 -1
  123. package/dist/prompt-dispatch.js.map +1 -1
  124. package/dist/protocol-log.d.ts.map +1 -1
  125. package/dist/protocol-log.js.map +1 -1
  126. package/dist/protocol.d.ts.map +1 -1
  127. package/dist/protocol.js.map +1 -1
  128. package/dist/refreshDeadline.d.ts.map +1 -1
  129. package/dist/refreshDeadline.js.map +1 -1
  130. package/dist/runtime-mode.d.ts.map +1 -1
  131. package/dist/runtime-mode.js.map +1 -1
  132. package/dist/runtime-model.d.ts +4 -0
  133. package/dist/runtime-model.d.ts.map +1 -1
  134. package/dist/runtime-model.js +13 -0
  135. package/dist/runtime-model.js.map +1 -1
  136. package/dist/session-cleanup.d.ts.map +1 -1
  137. package/dist/session-cleanup.js.map +1 -1
  138. package/dist/session-registry.d.ts.map +1 -1
  139. package/dist/session-registry.js.map +1 -1
  140. package/dist/session-update.d.ts.map +1 -1
  141. package/dist/session-update.js.map +1 -1
  142. package/dist/sessionProcess.d.ts.map +1 -1
  143. package/dist/sessionProcess.js.map +1 -1
  144. package/dist/source/_private/ycodium-executor-common/catalogModel.ts +40 -0
  145. package/dist/source/_private/ycodium-executor-common/eventTypes.ts +97 -0
  146. package/dist/source/_private/ycodium-executor-common/modelSelection.ts +24 -0
  147. package/dist/source/_private/ycodium-executor-common/nodeStreams.ts +212 -0
  148. package/dist/source/_private/ycodium-executor-common/raceAbort.ts +25 -0
  149. package/dist/source/_private/ycodium-executor-common/reasoningLevelFallback.ts +28 -0
  150. package/dist/source/_private/ycodium-executor-common/stderrTail.ts +27 -0
  151. package/dist/source/_private/ycodium-executor-common/toolPresentation.ts +268 -0
  152. package/dist/source/_private/ycodium-jsonl-stream/appendedJsonl.ts +222 -0
  153. package/dist/source/_private/ycodium-jsonl-stream/claudeSessions.ts +263 -0
  154. package/dist/source/_private/ycodium-jsonl-stream/codexSessions.ts +254 -0
  155. package/dist/source/_private/ycodium-jsonl-stream/history.ts +492 -0
  156. package/dist/source/_private/ycodium-jsonl-stream/historyErrors.ts +6 -0
  157. package/dist/source/_private/ycodium-jsonl-stream/historyFile.ts +139 -0
  158. package/dist/source/_private/ycodium-jsonl-stream/historyRecord.ts +180 -0
  159. package/dist/source/_private/ycodium-jsonl-stream/historyWaiting.ts +140 -0
  160. package/dist/source/_private/ycodium-jsonl-stream/jsonObjectLine.ts +15 -0
  161. package/dist/source/_private/ycodium-jsonl-stream/jsonlStream.ts +650 -0
  162. package/dist/source/_private/ycodium-jsonl-stream/scanCache.ts +163 -0
  163. package/dist/source/_private/ycodium-jsonl-stream/sessionDirectory.ts +39 -0
  164. package/dist/source/_private/ycodium-jsonl-stream/sessionTranscript.ts +136 -0
  165. package/dist/source/_private/ycodium-jsonl-stream/usageFields.ts +13 -0
  166. package/dist/source/_private/ycodium-workspace-path/directoryScope.ts +34 -0
  167. package/dist/source/_private/ycodium-workspace-path/workspacePath.ts +110 -0
  168. package/dist/source/adapter-support.ts +115 -0
  169. package/dist/source/apply-effort.ts +66 -0
  170. package/dist/source/approvals.ts +94 -0
  171. package/dist/source/artifacts.ts +88 -0
  172. package/dist/source/card-defaults.ts +25 -0
  173. package/dist/source/config-option-wiring.ts +119 -0
  174. package/dist/source/config-options.ts +270 -0
  175. package/dist/source/connection.ts +553 -0
  176. package/dist/source/errors.ts +207 -0
  177. package/dist/source/event-stream.ts +91 -0
  178. package/dist/source/events.ts +341 -0
  179. package/dist/source/executor/child-sessions.ts +87 -0
  180. package/dist/source/executor/lifecycle.ts +282 -0
  181. package/dist/source/executor/prompt-results.ts +123 -0
  182. package/dist/source/executor/prompt.ts +606 -0
  183. package/dist/source/executor/requests.ts +323 -0
  184. package/dist/source/executor/runtime.ts +137 -0
  185. package/dist/source/executor/session.ts +553 -0
  186. package/dist/source/executor/settlement.ts +92 -0
  187. package/dist/source/executor/subagent-sessions.ts +183 -0
  188. package/dist/source/executor/support.ts +129 -0
  189. package/dist/source/executor/updates.ts +427 -0
  190. package/dist/source/executor.ts +537 -0
  191. package/dist/source/failureCategory.ts +74 -0
  192. package/dist/source/handshake.ts +66 -0
  193. package/dist/source/index.ts +210 -0
  194. package/dist/source/instance-kit.ts +478 -0
  195. package/dist/source/jsonrpc.ts +59 -0
  196. package/dist/source/line-buffer.ts +54 -0
  197. package/dist/source/mcp-servers.ts +83 -0
  198. package/dist/source/model-selection.ts +138 -0
  199. package/dist/source/model-send.ts +80 -0
  200. package/dist/source/mutex.ts +47 -0
  201. package/dist/source/native-session-transcript.ts +167 -0
  202. package/dist/source/native-sessions.ts +271 -0
  203. package/dist/source/openCommands.ts +39 -0
  204. package/dist/source/permission-statement.ts +49 -0
  205. package/dist/source/probe-process.ts +95 -0
  206. package/dist/source/probe-session.ts +81 -0
  207. package/dist/source/probe.ts +261 -0
  208. package/dist/source/prompt-dispatch.ts +42 -0
  209. package/dist/source/protocol-log.ts +143 -0
  210. package/dist/source/protocol.ts +36 -0
  211. package/dist/source/refreshDeadline.ts +2 -0
  212. package/dist/source/runtime-mode.ts +125 -0
  213. package/dist/source/runtime-model.ts +598 -0
  214. package/dist/source/session-cleanup.ts +82 -0
  215. package/dist/source/session-registry.ts +62 -0
  216. package/dist/source/session-update.ts +88 -0
  217. package/dist/source/sessionProcess.ts +29 -0
  218. package/dist/source/start-abort.ts +19 -0
  219. package/dist/source/thread-operations.ts +145 -0
  220. package/dist/source/tool-call-state.ts +322 -0
  221. package/dist/source/tool-presentation.ts +20 -0
  222. package/dist/source/tool-subagents.ts +491 -0
  223. package/dist/source/turn-usage.ts +78 -0
  224. package/dist/source/types.ts +146 -0
  225. package/dist/source/user-input-pending.ts +31 -0
  226. package/dist/source/util.ts +38 -0
  227. package/dist/source/vendor-hooks.ts +394 -0
  228. package/dist/start-abort.d.ts.map +1 -1
  229. package/dist/start-abort.js.map +1 -1
  230. package/dist/thread-operations.d.ts.map +1 -1
  231. package/dist/thread-operations.js.map +1 -1
  232. package/dist/tool-call-state.d.ts.map +1 -1
  233. package/dist/tool-call-state.js.map +1 -1
  234. package/dist/tool-presentation.d.ts.map +1 -1
  235. package/dist/tool-presentation.js.map +1 -1
  236. package/dist/tool-subagents.d.ts +1 -0
  237. package/dist/tool-subagents.d.ts.map +1 -1
  238. package/dist/tool-subagents.js +2 -0
  239. package/dist/tool-subagents.js.map +1 -1
  240. package/dist/turn-usage.d.ts.map +1 -1
  241. package/dist/turn-usage.js.map +1 -1
  242. package/dist/types.d.ts.map +1 -1
  243. package/dist/types.js.map +1 -1
  244. package/dist/user-input-pending.d.ts.map +1 -1
  245. package/dist/user-input-pending.js.map +1 -1
  246. package/dist/util.d.ts.map +1 -1
  247. package/dist/util.js.map +1 -1
  248. package/dist/vendor-hooks.d.ts +11 -3
  249. package/dist/vendor-hooks.d.ts.map +1 -1
  250. package/dist/vendor-hooks.js.map +1 -1
  251. package/package.json +5 -2
@@ -0,0 +1,117 @@
1
+ // @effect-diagnostics nodeBuiltinImport:off - 执行器只读文件适配器,不依赖宿主 Effect。
2
+ import * as NodeFSP from "node:fs/promises";
3
+ import * as NodeCrypto from "node:crypto";
4
+ const HISTORY_READ_BYTES = 65_536;
5
+ export async function* historyBytes(path, start, end, signal) {
6
+ const file = await NodeFSP.open(path, "r");
7
+ try {
8
+ let offset = start;
9
+ while (offset < end) {
10
+ signal?.throwIfAborted();
11
+ const buffer = Buffer.allocUnsafe(Math.min(HISTORY_READ_BYTES, end - offset));
12
+ const { bytesRead } = await file.read(buffer, 0, buffer.length, offset);
13
+ if (bytesRead === 0)
14
+ throw new Error("Native history changed during a read.");
15
+ offset += bytesRead;
16
+ yield buffer.subarray(0, bytesRead);
17
+ }
18
+ }
19
+ finally {
20
+ await file.close();
21
+ }
22
+ }
23
+ export async function* historyText(path, range, signal) {
24
+ const decoder = new TextDecoder("utf-8", { fatal: true });
25
+ for await (const bytes of historyBytes(path, range.start, range.end, signal)) {
26
+ const text = decoder.decode(bytes, { stream: true });
27
+ if (text)
28
+ yield text;
29
+ }
30
+ const last = decoder.decode();
31
+ if (last)
32
+ yield last;
33
+ }
34
+ export async function* historyLines(path, start, end, backwards, signal) {
35
+ if (!backwards) {
36
+ let lineStart = start;
37
+ let offset = start;
38
+ for await (const bytes of historyBytes(path, start, end, signal)) {
39
+ for (let index = 0; index < bytes.length; index += 1) {
40
+ if (bytes[index] !== 10)
41
+ continue;
42
+ const lineEnd = offset + index;
43
+ if (lineEnd > lineStart)
44
+ yield { start: lineStart, end: lineEnd, commit: lineEnd + 1, terminated: true };
45
+ lineStart = lineEnd + 1;
46
+ }
47
+ offset += bytes.length;
48
+ }
49
+ if (lineStart < end)
50
+ yield { start: lineStart, end, commit: end, terminated: false };
51
+ return;
52
+ }
53
+ const file = await NodeFSP.open(path, "r");
54
+ try {
55
+ let scan = end;
56
+ let lineEnd = end;
57
+ let terminated = false;
58
+ while (scan > start) {
59
+ signal?.throwIfAborted();
60
+ const from = Math.max(start, scan - HISTORY_READ_BYTES);
61
+ const buffer = Buffer.allocUnsafe(scan - from);
62
+ const { bytesRead } = await file.read(buffer, 0, buffer.length, from);
63
+ if (bytesRead !== buffer.length)
64
+ throw new Error("Native history changed during a read.");
65
+ for (let index = buffer.length - 1; index >= 0; index -= 1) {
66
+ if (buffer[index] !== 10)
67
+ continue;
68
+ const at = from + index;
69
+ if (at + 1 < lineEnd)
70
+ yield { start: at + 1, end: lineEnd, commit: lineEnd + (terminated ? 1 : 0), terminated };
71
+ lineEnd = at;
72
+ terminated = true;
73
+ }
74
+ scan = from;
75
+ }
76
+ if (lineEnd > start)
77
+ yield { start, end: lineEnd, commit: lineEnd + (terminated ? 1 : 0), terminated };
78
+ }
79
+ finally {
80
+ await file.close();
81
+ }
82
+ }
83
+ export const historyIdentity = async (path) => {
84
+ const stat = await NodeFSP.stat(path);
85
+ return {
86
+ size: stat.size,
87
+ mtimeMs: stat.mtimeMs,
88
+ ctimeMs: stat.ctimeMs,
89
+ identity: NodeCrypto.createHash("sha256")
90
+ .update(`${path}/${stat.dev}/${stat.ino}/${stat.birthtimeMs}`)
91
+ .digest("hex"),
92
+ };
93
+ };
94
+ /** 续接时抽样已提交前缀,覆盖头、中段和尾段;每次文件通知最多读固定大小。 */
95
+ const historyCommittedFingerprint = async (path, sequence, signal) => {
96
+ const hash = NodeCrypto.createHash("sha256");
97
+ const starts = [
98
+ 0,
99
+ Math.max(0, Math.floor(sequence / 3) - 512),
100
+ Math.max(0, Math.floor((sequence * 2) / 3) - 512),
101
+ Math.max(0, sequence - 1_024),
102
+ ];
103
+ for (const start of new Set(starts)) {
104
+ const end = Math.min(sequence, start + 1_024);
105
+ hash.update(`${start}:${end}:`);
106
+ for await (const bytes of historyBytes(path, start, end, signal))
107
+ hash.update(bytes);
108
+ }
109
+ return hash.digest("hex");
110
+ };
111
+ export const historyBoundary = async (path, identity, sequence, signal) => {
112
+ const hash = NodeCrypto.createHash("sha256");
113
+ for await (const bytes of historyBytes(path, Math.max(0, sequence - 128), sequence, signal))
114
+ hash.update(bytes);
115
+ hash.update(await historyCommittedFingerprint(path, sequence, signal));
116
+ return { epoch: `${identity}.${hash.digest("hex")}`, sequence };
117
+ };
@@ -0,0 +1,31 @@
1
+ import type { ExecutorHistoryItem } from "@ycodium-ai/plugin-api/executor";
2
+ import { type RetainedField } from "./jsonlStream.ts";
3
+ import { type LineRange } from "./historyFile.ts";
4
+ type RecordItem = Omit<ExecutorHistoryItem, "id" | "text"> & {
5
+ readonly text: "raw" | "message";
6
+ };
7
+ export type HistoryLayout = () => {
8
+ readonly fields: readonly RetainedField[];
9
+ readonly textPaths: ReadonlyArray<readonly string[]>;
10
+ readonly describe: (metadata: unknown) => ReadonlyArray<RecordItem>;
11
+ readonly verify: (metadata: unknown) => boolean;
12
+ };
13
+ export type RecordInfo = {
14
+ readonly range: LineRange;
15
+ readonly hash: string;
16
+ readonly rawTotal: number;
17
+ readonly messageTotal: number;
18
+ readonly items: ReadonlyArray<RecordItem>;
19
+ };
20
+ export declare const createHistoryRecordAccumulator: (layout: HistoryLayout) => {
21
+ write: (bytes: Uint8Array) => void;
22
+ finish: (range: LineRange) => RecordInfo | undefined;
23
+ };
24
+ export declare const inspectHistoryRecord: (path: string, range: LineRange, layout: HistoryLayout, signal?: AbortSignal) => Promise<RecordInfo | undefined>;
25
+ /** 每次最多留下一个窗口与一个底层读取块;字符串解码跨 JSON 转义和 UTF-8 块继续。 */
26
+ export declare function historyRecordWindows(path: string, info: RecordInfo, mode: "raw" | "message", from: number, until: number, limit: number, layout: HistoryLayout, signal?: AbortSignal): AsyncGenerator<{
27
+ offset: number;
28
+ text: string;
29
+ }, void, unknown>;
30
+ export declare const verifyHistoryRecord: (path: string, range: LineRange, layout: HistoryLayout, signal?: AbortSignal) => Promise<boolean>;
31
+ export {};
@@ -0,0 +1,144 @@
1
+ // @effect-diagnostics nodeBuiltinImport:off - 原生记录只读解析,正文按窗口交付。
2
+ import * as NodeCrypto from "node:crypto";
3
+ import { OversizedRecordParser } from "./jsonlStream.js";
4
+ import { historyBytes, historyText } from "./historyFile.js";
5
+ const textFields = (paths, accept) => {
6
+ let completed = 0;
7
+ let started = false;
8
+ const begin = () => {
9
+ if (started)
10
+ return;
11
+ if (completed > 0)
12
+ accept("\n");
13
+ started = true;
14
+ };
15
+ return paths.map((path) => ({
16
+ path,
17
+ scalarOnly: true,
18
+ onChunk: (text) => {
19
+ begin();
20
+ accept(text);
21
+ },
22
+ onEnd: () => {
23
+ begin();
24
+ completed += 1;
25
+ started = false;
26
+ },
27
+ }));
28
+ };
29
+ export const createHistoryRecordAccumulator = (layout) => {
30
+ const selected = layout();
31
+ let rawTotal = 0;
32
+ let messageTotal = 0;
33
+ const hash = NodeCrypto.createHash("sha256");
34
+ const decoder = new TextDecoder("utf-8", { fatal: true });
35
+ const parser = new OversizedRecordParser([
36
+ ...selected.fields,
37
+ ...textFields(selected.textPaths, (text) => {
38
+ messageTotal += text.length;
39
+ }),
40
+ ], Number.MAX_SAFE_INTEGER, { pruneEmpty: true });
41
+ const accept = (text) => {
42
+ rawTotal += text.length;
43
+ parser.write(text);
44
+ };
45
+ return {
46
+ write: (bytes) => {
47
+ hash.update(bytes);
48
+ accept(decoder.decode(bytes, { stream: true }));
49
+ },
50
+ finish: (range) => {
51
+ accept(decoder.decode());
52
+ const metadata = parser.finish();
53
+ if (metadata === undefined) {
54
+ if (!range.terminated)
55
+ return undefined;
56
+ throw new SyntaxError("Malformed complete native history record.");
57
+ }
58
+ return {
59
+ range,
60
+ hash: hash.digest("hex"),
61
+ rawTotal,
62
+ messageTotal,
63
+ items: selected
64
+ .describe(metadata)
65
+ .map((item) => item.text === "message" && messageTotal === 0
66
+ ? { ...item, text: "raw" }
67
+ : item),
68
+ };
69
+ },
70
+ };
71
+ };
72
+ export const inspectHistoryRecord = async (path, range, layout, signal) => {
73
+ const accumulator = createHistoryRecordAccumulator(layout);
74
+ try {
75
+ for await (const bytes of historyBytes(path, range.start, range.end, signal))
76
+ accumulator.write(bytes);
77
+ return accumulator.finish(range);
78
+ }
79
+ catch (error) {
80
+ if (!range.terminated && error instanceof TypeError)
81
+ return undefined;
82
+ throw error;
83
+ }
84
+ };
85
+ /** 每次最多留下一个窗口与一个底层读取块;字符串解码跨 JSON 转义和 UTF-8 块继续。 */
86
+ export async function* historyRecordWindows(path, info, mode, from, until, limit, layout, signal) {
87
+ let seen = 0;
88
+ let offset = from;
89
+ let pending = "";
90
+ const ready = [];
91
+ const accept = (text) => {
92
+ const begin = Math.max(0, from - seen);
93
+ const end = Math.min(text.length, until - seen);
94
+ seen += text.length;
95
+ if (end > begin)
96
+ pending += text.slice(begin, end);
97
+ while (pending.length >= limit) {
98
+ let size = limit;
99
+ const code = pending.charCodeAt(size - 1);
100
+ if (code >= 0xd800 && code <= 0xdbff)
101
+ size -= 1;
102
+ ready.push({ offset, text: pending.slice(0, size) });
103
+ pending = pending.slice(size);
104
+ offset += size;
105
+ }
106
+ };
107
+ const selected = layout();
108
+ const parser = mode === "raw"
109
+ ? undefined
110
+ : new OversizedRecordParser(textFields(selected.textPaths, accept), Number.MAX_SAFE_INTEGER, {
111
+ pruneEmpty: true,
112
+ });
113
+ for await (const text of historyText(path, info.range, signal)) {
114
+ if (parser === undefined)
115
+ accept(text);
116
+ else
117
+ parser.write(text);
118
+ while (ready.length > 0)
119
+ yield ready.shift();
120
+ if (seen >= until)
121
+ break;
122
+ }
123
+ if (pending)
124
+ yield { offset, text: pending };
125
+ }
126
+ export const verifyHistoryRecord = async (path, range, layout, signal) => {
127
+ const selected = layout();
128
+ const parser = new OversizedRecordParser(selected.fields, Number.MAX_SAFE_INTEGER, {
129
+ pruneEmpty: true,
130
+ });
131
+ try {
132
+ for await (const text of historyText(path, range, signal))
133
+ parser.write(text);
134
+ }
135
+ catch (error) {
136
+ if (!range.terminated && error instanceof TypeError)
137
+ return false;
138
+ throw error;
139
+ }
140
+ const metadata = parser.finish();
141
+ if (metadata === undefined && range.terminated)
142
+ throw new SyntaxError("Malformed complete native history record.");
143
+ return selected.verify(metadata);
144
+ };
@@ -0,0 +1,8 @@
1
+ import type { ExecutorHistorySource } from "@ycodium-ai/plugin-api/executor";
2
+ /** 先取得目录观察,再尝试打开;文件和首行尚未完成时保留订阅,完整坏记录与身份错误不会被吞掉。 */
3
+ export declare const waitForJsonlHistory: (input: {
4
+ readonly root: string;
5
+ readonly accepts: (path: string) => boolean;
6
+ readonly open: (signal: AbortSignal) => Promise<ExecutorHistorySource | undefined>;
7
+ readonly signal?: AbortSignal;
8
+ }) => ExecutorHistorySource;
@@ -0,0 +1,142 @@
1
+ // @effect-diagnostics nodeBuiltinImport:off - 只在订阅期间等待原生文件出现,不使用定时扫描。
2
+ import * as NodeFS from "node:fs";
3
+ import * as NodeFSP from "node:fs/promises";
4
+ import * as NodePath from "node:path";
5
+ import { NativeHistoryPendingError } from "./historyErrors.js";
6
+ const pending = (error) => error instanceof NativeHistoryPendingError ||
7
+ (error !== null && typeof error === "object" && "code" in error && error.code === "ENOENT");
8
+ /** 先取得目录观察,再尝试打开;文件和首行尚未完成时保留订阅,完整坏记录与身份错误不会被吞掉。 */
9
+ export const waitForJsonlHistory = (input) => {
10
+ const controller = new AbortController();
11
+ const sources = new Set();
12
+ const operations = new Set();
13
+ const own = (promise) => {
14
+ operations.add(promise);
15
+ void promise.finally(() => operations.delete(promise)).catch(() => undefined);
16
+ return promise;
17
+ };
18
+ const iterators = new Set();
19
+ const open = () => own((async () => {
20
+ const signal = AbortSignal.any([
21
+ controller.signal,
22
+ ...(input.signal ? [input.signal] : []),
23
+ ]);
24
+ const source = await input.open(signal);
25
+ if (source === undefined)
26
+ throw new NativeHistoryPendingError();
27
+ sources.add(source);
28
+ return source;
29
+ })());
30
+ const release = async (source) => {
31
+ await source.close();
32
+ sources.delete(source);
33
+ };
34
+ return {
35
+ page: (options) => own((async () => {
36
+ const source = await open();
37
+ try {
38
+ return await source.page(options);
39
+ }
40
+ finally {
41
+ await release(source);
42
+ }
43
+ })()),
44
+ follow: (options) => {
45
+ let iterator;
46
+ iterator = (async function* () {
47
+ const signal = AbortSignal.any([
48
+ controller.signal,
49
+ ...(input.signal ? [input.signal] : []),
50
+ ...(options.signal ? [options.signal] : []),
51
+ ]);
52
+ let root = NodePath.resolve(input.root);
53
+ while (true) {
54
+ try {
55
+ if ((await NodeFSP.stat(root)).isDirectory())
56
+ break;
57
+ }
58
+ catch (error) {
59
+ if (!pending(error))
60
+ throw error;
61
+ }
62
+ const parent = NodePath.dirname(root);
63
+ if (parent === root)
64
+ throw new NativeHistoryPendingError();
65
+ root = parent;
66
+ }
67
+ let dirty = true;
68
+ let wake;
69
+ let error;
70
+ const notify = () => {
71
+ dirty = true;
72
+ wake?.();
73
+ wake = undefined;
74
+ };
75
+ const watcher = NodeFS.watch(root, { recursive: true, signal }, (_event, name) => {
76
+ if (name == null || input.accepts(NodePath.resolve(root, String(name))))
77
+ notify();
78
+ });
79
+ watcher.on("error", (cause) => {
80
+ error = cause;
81
+ notify();
82
+ });
83
+ signal.addEventListener("abort", notify, { once: true });
84
+ let announced = false;
85
+ try {
86
+ while (true) {
87
+ signal.throwIfAborted();
88
+ if (error !== undefined)
89
+ throw error;
90
+ if (!dirty)
91
+ await new Promise((resolve) => {
92
+ wake = resolve;
93
+ });
94
+ signal.throwIfAborted();
95
+ dirty = false;
96
+ let source;
97
+ try {
98
+ source = await open();
99
+ }
100
+ catch (cause) {
101
+ if (!pending(cause))
102
+ throw cause;
103
+ if (!announced) {
104
+ announced = true;
105
+ yield { _tag: "unavailable", reason: "native-session-unavailable" };
106
+ yield { _tag: "synchronized" };
107
+ }
108
+ continue;
109
+ }
110
+ announced = false;
111
+ try {
112
+ for await (const frame of source.follow({ ...options, signal }))
113
+ yield frame;
114
+ }
115
+ catch (cause) {
116
+ if (!pending(cause))
117
+ throw cause;
118
+ }
119
+ finally {
120
+ await release(source);
121
+ }
122
+ }
123
+ }
124
+ finally {
125
+ watcher.close();
126
+ signal.removeEventListener("abort", notify);
127
+ wake?.();
128
+ if (iterator !== undefined)
129
+ iterators.delete(iterator);
130
+ }
131
+ })();
132
+ iterators.add(iterator);
133
+ return iterator;
134
+ },
135
+ close: async () => {
136
+ controller.abort();
137
+ await Promise.all([...sources].map(release));
138
+ await Promise.all([...iterators].map((iterator) => iterator.return(undefined)));
139
+ await Promise.allSettled(operations);
140
+ },
141
+ };
142
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 解析一行 JSON 对象。空行、坏 JSON、非对象都得到 undefined。
3
+ * Codex app-server 的按行 JSON-RPC(agent-codex 与 session-import)共用这一份。
4
+ */
5
+ export declare function parseJsonObjectLine(line: string): Record<string, unknown> | undefined;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * 解析一行 JSON 对象。空行、坏 JSON、非对象都得到 undefined。
3
+ * Codex app-server 的按行 JSON-RPC(agent-codex 与 session-import)共用这一份。
4
+ */
5
+ export function parseJsonObjectLine(line) {
6
+ if (line.trim().length === 0)
7
+ return undefined;
8
+ let message;
9
+ try {
10
+ message = JSON.parse(line);
11
+ }
12
+ catch {
13
+ return undefined;
14
+ }
15
+ if (!message || typeof message !== "object")
16
+ return undefined;
17
+ return message;
18
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * 转录 jsonl 的逐行读取,外加超长行的流式字段筛选。
3
+ *
4
+ * 转录里偶尔有一行大到几十 MB(工具输出、整文件写入的 input)。`ctx.fs.readLines`
5
+ * 会把整行读成一个字符串,这样的行既占内存又只能被丢掉。这里改走字节流:
6
+ * - 不超过 `maxLineChars` 的行照旧交回完整字符串,由调用方 `JSON.parse`,行为不变;
7
+ * - 超过的行不再整行落成字符串,而是边读边按字段清单解析,只把清单里的字段装配
8
+ * 成对象,其余值读过即弃;清单内的长字符串只留头尾各 `maxChars` 个字符。
9
+ *
10
+ * 字段清单是调用方真正读取的字段,不是对记录结构的另一份描述;清单外的字段在
11
+ * 超长行里不存在。
12
+ *
13
+ * 官方插件(session-import、agent-claude)共用,各自用 esbuild 打进自己的 bundle:插件之间
14
+ * 不能互相 import,所以这份读取器放在独立的工作区包里。
15
+ *
16
+ * @module ycodium-jsonl-stream
17
+ */
18
+ /** 读字节流的来源:宿主的 `ctx.fs` 和 node:fs 都满足这个形状。 */
19
+ export interface JsonlSource {
20
+ readonly createReadStream: (path: string) => AsyncIterable<Uint8Array>;
21
+ }
22
+ /** 超长行里要留下的一个字段。 */
23
+ export interface RetainedField {
24
+ /** 从记录根开始的键路径;`"*"` 匹配任意对象键或数组下标。 */
25
+ readonly path: readonly string[];
26
+ /**
27
+ * 路径本身只在值是标量时才留;值是对象或数组时只往下走,由更长的清单项挑子字段。
28
+ * 用于 `message.content` 这种既可能是字符串、又可能是块数组的字段。
29
+ */
30
+ readonly scalarOnly?: boolean;
31
+ /** 字符串留头尾各这么多字符,中间换成省略标记。不给就整串保留。 */
32
+ readonly maxChars?: number;
33
+ /** 解码后的字符串片段;提供时不保留该字符串,调用者自行限流或分窗。 */
34
+ readonly onChunk?: (text: string) => void;
35
+ /** 完整字符串的 UTF-16 长度;只在合法闭引号后调用。 */
36
+ readonly onEnd?: (total: number) => void;
37
+ /** 非字符串标量;提供时不保留该值。 */
38
+ readonly onScalar?: (value: boolean | number | null) => void;
39
+ }
40
+ export type JsonlEntry =
41
+ /** 未超长的一行,原文。 */
42
+ {
43
+ readonly line: string;
44
+ }
45
+ /** 超长的一行,按字段清单装配出的记录;写到一半、不是合法 JSON 时为 `undefined`。 */
46
+ | {
47
+ readonly oversized: unknown;
48
+ };
49
+ export interface ReadJsonlOptions {
50
+ /** 超过这个字符数的行走流式筛选。 */
51
+ readonly maxLineChars: number;
52
+ readonly fields: readonly RetainedField[];
53
+ /** 单条超长记录里留下的字符总数上限;超了整条丢弃。默认 8M。 */
54
+ readonly maxRetainedChars?: number;
55
+ }
56
+ /**
57
+ * 一条 JSON 记录的增量解析器:一次一个字符块,行尾 `finish` 取结果。
58
+ * 任何不合法的输入都只把这一条记录标成作废,不抛。
59
+ */
60
+ export declare class OversizedRecordParser {
61
+ #private;
62
+ constructor(fields: readonly RetainedField[], maxRetainedChars?: number, options?: {
63
+ readonly pruneEmpty?: boolean;
64
+ });
65
+ write(chunk: string): void;
66
+ /** 完整且合法才返回记录;其余(含空行)返回 `undefined`。 */
67
+ finish(): unknown;
68
+ }
69
+ /**
70
+ * 一行转成记录:没超长的先过 `accepts` 预筛再 `JSON.parse`,超长的已经是筛好的记录。
71
+ * 解析失败(写到一半的尾行是常态)返回 `undefined`。
72
+ */
73
+ export declare function jsonlEntryRecord(entry: JsonlEntry, accepts: (line: string) => boolean): unknown;
74
+ /**
75
+ * 逐行读一份 jsonl。行以 `\n` 结尾(尾部 `\r` 去掉),空行不吐。
76
+ *
77
+ * 读失败(文件不存在、权限)原样抛出,由调用方决定是空还是错。
78
+ */
79
+ export declare function readJsonlEntries(fs: JsonlSource, path: string, options: ReadJsonlOptions): AsyncGenerator<JsonlEntry>;