ycodium-acp-executor 0.2.5634 → 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 (244) hide show
  1. package/README.md +2 -1
  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/usageFields.d.ts +5 -0
  27. package/dist/_private/ycodium-jsonl-stream/usageFields.js +11 -0
  28. package/dist/_private/ycodium-workspace-path/directoryScope.d.ts +5 -0
  29. package/dist/_private/ycodium-workspace-path/directoryScope.js +28 -0
  30. package/dist/_private/ycodium-workspace-path/workspacePath.d.ts +19 -0
  31. package/dist/_private/ycodium-workspace-path/workspacePath.js +90 -0
  32. package/dist/adapter-support.d.ts.map +1 -1
  33. package/dist/adapter-support.js.map +1 -1
  34. package/dist/apply-effort.d.ts.map +1 -1
  35. package/dist/apply-effort.js.map +1 -1
  36. package/dist/approvals.d.ts.map +1 -1
  37. package/dist/approvals.js.map +1 -1
  38. package/dist/artifacts.d.ts.map +1 -1
  39. package/dist/artifacts.js.map +1 -1
  40. package/dist/card-defaults.d.ts.map +1 -1
  41. package/dist/card-defaults.js.map +1 -1
  42. package/dist/config-option-wiring.d.ts.map +1 -1
  43. package/dist/config-option-wiring.js.map +1 -1
  44. package/dist/config-options.d.ts.map +1 -1
  45. package/dist/config-options.js.map +1 -1
  46. package/dist/connection.d.ts.map +1 -1
  47. package/dist/connection.js.map +1 -1
  48. package/dist/errors.d.ts.map +1 -1
  49. package/dist/errors.js.map +1 -1
  50. package/dist/event-stream.d.ts.map +1 -1
  51. package/dist/event-stream.js.map +1 -1
  52. package/dist/events.d.ts.map +1 -1
  53. package/dist/events.js.map +1 -1
  54. package/dist/executor/child-sessions.d.ts.map +1 -1
  55. package/dist/executor/child-sessions.js.map +1 -1
  56. package/dist/executor/lifecycle.d.ts.map +1 -1
  57. package/dist/executor/lifecycle.js.map +1 -1
  58. package/dist/executor/prompt-results.d.ts.map +1 -1
  59. package/dist/executor/prompt-results.js.map +1 -1
  60. package/dist/executor/prompt.d.ts.map +1 -1
  61. package/dist/executor/prompt.js.map +1 -1
  62. package/dist/executor/requests.d.ts.map +1 -1
  63. package/dist/executor/requests.js.map +1 -1
  64. package/dist/executor/runtime.d.ts.map +1 -1
  65. package/dist/executor/runtime.js.map +1 -1
  66. package/dist/executor/session.d.ts.map +1 -1
  67. package/dist/executor/session.js +17 -1
  68. package/dist/executor/session.js.map +1 -1
  69. package/dist/executor/settlement.d.ts.map +1 -1
  70. package/dist/executor/settlement.js.map +1 -1
  71. package/dist/executor/subagent-sessions.d.ts.map +1 -1
  72. package/dist/executor/subagent-sessions.js.map +1 -1
  73. package/dist/executor/support.d.ts.map +1 -1
  74. package/dist/executor/support.js.map +1 -1
  75. package/dist/executor/updates.d.ts.map +1 -1
  76. package/dist/executor/updates.js.map +1 -1
  77. package/dist/executor.d.ts.map +1 -1
  78. package/dist/executor.js.map +1 -1
  79. package/dist/failureCategory.d.ts.map +1 -1
  80. package/dist/failureCategory.js.map +1 -1
  81. package/dist/handshake.d.ts.map +1 -1
  82. package/dist/handshake.js.map +1 -1
  83. package/dist/index.d.ts +1 -1
  84. package/dist/index.d.ts.map +1 -1
  85. package/dist/index.js +1 -1
  86. package/dist/index.js.map +1 -1
  87. package/dist/instance-kit.d.ts.map +1 -1
  88. package/dist/instance-kit.js.map +1 -1
  89. package/dist/jsonrpc.d.ts.map +1 -1
  90. package/dist/jsonrpc.js.map +1 -1
  91. package/dist/line-buffer.d.ts.map +1 -1
  92. package/dist/line-buffer.js.map +1 -1
  93. package/dist/mcp-servers.d.ts +26 -4
  94. package/dist/mcp-servers.d.ts.map +1 -1
  95. package/dist/mcp-servers.js +25 -4
  96. package/dist/mcp-servers.js.map +1 -1
  97. package/dist/model-selection.d.ts.map +1 -1
  98. package/dist/model-selection.js.map +1 -1
  99. package/dist/model-send.d.ts.map +1 -1
  100. package/dist/model-send.js.map +1 -1
  101. package/dist/mutex.d.ts.map +1 -1
  102. package/dist/mutex.js.map +1 -1
  103. package/dist/native-session-transcript.d.ts.map +1 -1
  104. package/dist/native-session-transcript.js.map +1 -1
  105. package/dist/native-sessions.d.ts.map +1 -1
  106. package/dist/native-sessions.js.map +1 -1
  107. package/dist/openCommands.d.ts.map +1 -1
  108. package/dist/openCommands.js.map +1 -1
  109. package/dist/permission-statement.d.ts.map +1 -1
  110. package/dist/permission-statement.js.map +1 -1
  111. package/dist/probe-process.d.ts.map +1 -1
  112. package/dist/probe-process.js.map +1 -1
  113. package/dist/probe-session.d.ts.map +1 -1
  114. package/dist/probe-session.js.map +1 -1
  115. package/dist/probe.d.ts.map +1 -1
  116. package/dist/probe.js.map +1 -1
  117. package/dist/prompt-dispatch.d.ts.map +1 -1
  118. package/dist/prompt-dispatch.js.map +1 -1
  119. package/dist/protocol-log.d.ts.map +1 -1
  120. package/dist/protocol-log.js.map +1 -1
  121. package/dist/protocol.d.ts.map +1 -1
  122. package/dist/protocol.js.map +1 -1
  123. package/dist/refreshDeadline.d.ts.map +1 -1
  124. package/dist/refreshDeadline.js.map +1 -1
  125. package/dist/runtime-mode.d.ts.map +1 -1
  126. package/dist/runtime-mode.js.map +1 -1
  127. package/dist/runtime-model.d.ts.map +1 -1
  128. package/dist/runtime-model.js.map +1 -1
  129. package/dist/session-cleanup.d.ts.map +1 -1
  130. package/dist/session-cleanup.js.map +1 -1
  131. package/dist/session-registry.d.ts.map +1 -1
  132. package/dist/session-registry.js.map +1 -1
  133. package/dist/session-update.d.ts.map +1 -1
  134. package/dist/session-update.js.map +1 -1
  135. package/dist/sessionProcess.d.ts.map +1 -1
  136. package/dist/sessionProcess.js.map +1 -1
  137. package/dist/source/_private/ycodium-executor-common/catalogModel.ts +40 -0
  138. package/dist/source/_private/ycodium-executor-common/eventTypes.ts +97 -0
  139. package/dist/source/_private/ycodium-executor-common/modelSelection.ts +24 -0
  140. package/dist/source/_private/ycodium-executor-common/nodeStreams.ts +212 -0
  141. package/dist/source/_private/ycodium-executor-common/raceAbort.ts +25 -0
  142. package/dist/source/_private/ycodium-executor-common/reasoningLevelFallback.ts +28 -0
  143. package/dist/source/_private/ycodium-executor-common/stderrTail.ts +27 -0
  144. package/dist/source/_private/ycodium-executor-common/toolPresentation.ts +268 -0
  145. package/dist/source/_private/ycodium-jsonl-stream/appendedJsonl.ts +222 -0
  146. package/dist/source/_private/ycodium-jsonl-stream/claudeSessions.ts +263 -0
  147. package/dist/source/_private/ycodium-jsonl-stream/codexSessions.ts +254 -0
  148. package/dist/source/_private/ycodium-jsonl-stream/history.ts +492 -0
  149. package/dist/source/_private/ycodium-jsonl-stream/historyErrors.ts +6 -0
  150. package/dist/source/_private/ycodium-jsonl-stream/historyFile.ts +139 -0
  151. package/dist/source/_private/ycodium-jsonl-stream/historyRecord.ts +180 -0
  152. package/dist/source/_private/ycodium-jsonl-stream/historyWaiting.ts +140 -0
  153. package/dist/source/_private/ycodium-jsonl-stream/jsonObjectLine.ts +15 -0
  154. package/dist/source/_private/ycodium-jsonl-stream/jsonlStream.ts +650 -0
  155. package/dist/source/_private/ycodium-jsonl-stream/scanCache.ts +163 -0
  156. package/dist/source/_private/ycodium-jsonl-stream/sessionDirectory.ts +39 -0
  157. package/dist/source/_private/ycodium-jsonl-stream/sessionTranscript.ts +136 -0
  158. package/dist/source/_private/ycodium-jsonl-stream/usageFields.ts +13 -0
  159. package/dist/source/_private/ycodium-workspace-path/directoryScope.ts +34 -0
  160. package/dist/source/_private/ycodium-workspace-path/workspacePath.ts +110 -0
  161. package/dist/source/adapter-support.ts +115 -0
  162. package/dist/source/apply-effort.ts +66 -0
  163. package/dist/source/approvals.ts +94 -0
  164. package/dist/source/artifacts.ts +88 -0
  165. package/dist/source/card-defaults.ts +25 -0
  166. package/dist/source/config-option-wiring.ts +119 -0
  167. package/dist/source/config-options.ts +270 -0
  168. package/dist/source/connection.ts +553 -0
  169. package/dist/source/errors.ts +207 -0
  170. package/dist/source/event-stream.ts +91 -0
  171. package/dist/source/events.ts +341 -0
  172. package/dist/source/executor/child-sessions.ts +87 -0
  173. package/dist/source/executor/lifecycle.ts +282 -0
  174. package/dist/source/executor/prompt-results.ts +123 -0
  175. package/dist/source/executor/prompt.ts +606 -0
  176. package/dist/source/executor/requests.ts +323 -0
  177. package/dist/source/executor/runtime.ts +137 -0
  178. package/dist/source/executor/session.ts +553 -0
  179. package/dist/source/executor/settlement.ts +92 -0
  180. package/dist/source/executor/subagent-sessions.ts +183 -0
  181. package/dist/source/executor/support.ts +129 -0
  182. package/dist/source/executor/updates.ts +427 -0
  183. package/dist/source/executor.ts +537 -0
  184. package/dist/source/failureCategory.ts +74 -0
  185. package/dist/source/handshake.ts +66 -0
  186. package/dist/source/index.ts +210 -0
  187. package/dist/source/instance-kit.ts +478 -0
  188. package/dist/source/jsonrpc.ts +59 -0
  189. package/dist/source/line-buffer.ts +54 -0
  190. package/dist/source/mcp-servers.ts +83 -0
  191. package/dist/source/model-selection.ts +138 -0
  192. package/dist/source/model-send.ts +80 -0
  193. package/dist/source/mutex.ts +47 -0
  194. package/dist/source/native-session-transcript.ts +167 -0
  195. package/dist/source/native-sessions.ts +271 -0
  196. package/dist/source/openCommands.ts +39 -0
  197. package/dist/source/permission-statement.ts +49 -0
  198. package/dist/source/probe-process.ts +95 -0
  199. package/dist/source/probe-session.ts +81 -0
  200. package/dist/source/probe.ts +261 -0
  201. package/dist/source/prompt-dispatch.ts +42 -0
  202. package/dist/source/protocol-log.ts +143 -0
  203. package/dist/source/protocol.ts +36 -0
  204. package/dist/source/refreshDeadline.ts +2 -0
  205. package/dist/source/runtime-mode.ts +125 -0
  206. package/dist/source/runtime-model.ts +598 -0
  207. package/dist/source/session-cleanup.ts +82 -0
  208. package/dist/source/session-registry.ts +62 -0
  209. package/dist/source/session-update.ts +88 -0
  210. package/dist/source/sessionProcess.ts +29 -0
  211. package/dist/source/start-abort.ts +19 -0
  212. package/dist/source/thread-operations.ts +145 -0
  213. package/dist/source/tool-call-state.ts +322 -0
  214. package/dist/source/tool-presentation.ts +20 -0
  215. package/dist/source/tool-subagents.ts +491 -0
  216. package/dist/source/turn-usage.ts +78 -0
  217. package/dist/source/types.ts +146 -0
  218. package/dist/source/user-input-pending.ts +31 -0
  219. package/dist/source/util.ts +38 -0
  220. package/dist/source/vendor-hooks.ts +394 -0
  221. package/dist/start-abort.d.ts.map +1 -1
  222. package/dist/start-abort.js.map +1 -1
  223. package/dist/thread-operations.d.ts.map +1 -1
  224. package/dist/thread-operations.js.map +1 -1
  225. package/dist/tool-call-state.d.ts.map +1 -1
  226. package/dist/tool-call-state.js.map +1 -1
  227. package/dist/tool-presentation.d.ts.map +1 -1
  228. package/dist/tool-presentation.js.map +1 -1
  229. package/dist/tool-subagents.d.ts +1 -0
  230. package/dist/tool-subagents.d.ts.map +1 -1
  231. package/dist/tool-subagents.js +2 -0
  232. package/dist/tool-subagents.js.map +1 -1
  233. package/dist/turn-usage.d.ts.map +1 -1
  234. package/dist/turn-usage.js.map +1 -1
  235. package/dist/types.d.ts.map +1 -1
  236. package/dist/types.js.map +1 -1
  237. package/dist/user-input-pending.d.ts.map +1 -1
  238. package/dist/user-input-pending.js.map +1 -1
  239. package/dist/util.d.ts.map +1 -1
  240. package/dist/util.js.map +1 -1
  241. package/dist/vendor-hooks.d.ts +9 -0
  242. package/dist/vendor-hooks.d.ts.map +1 -1
  243. package/dist/vendor-hooks.js.map +1 -1
  244. package/package.json +5 -2
@@ -0,0 +1,603 @@
1
+ // @effect-diagnostics globalDate:off nodeBuiltinImport:off - 纯 TS/Promise 工具,被官方插件打进各自的 bundle,不碰 Effect 运行时。
2
+ /**
3
+ * 转录 jsonl 的逐行读取,外加超长行的流式字段筛选。
4
+ *
5
+ * 转录里偶尔有一行大到几十 MB(工具输出、整文件写入的 input)。`ctx.fs.readLines`
6
+ * 会把整行读成一个字符串,这样的行既占内存又只能被丢掉。这里改走字节流:
7
+ * - 不超过 `maxLineChars` 的行照旧交回完整字符串,由调用方 `JSON.parse`,行为不变;
8
+ * - 超过的行不再整行落成字符串,而是边读边按字段清单解析,只把清单里的字段装配
9
+ * 成对象,其余值读过即弃;清单内的长字符串只留头尾各 `maxChars` 个字符。
10
+ *
11
+ * 字段清单是调用方真正读取的字段,不是对记录结构的另一份描述;清单外的字段在
12
+ * 超长行里不存在。
13
+ *
14
+ * 官方插件(session-import、agent-claude)共用,各自用 esbuild 打进自己的 bundle:插件之间
15
+ * 不能互相 import,所以这份读取器放在独立的工作区包里。
16
+ *
17
+ * @module ycodium-jsonl-stream
18
+ */
19
+ const DEFAULT_MAX_RETAINED_CHARS = 8_000_000;
20
+ const MAX_DEPTH = 128;
21
+ /** 键长超过它就不再逐字符保留:清单里没有这么长的键。 */
22
+ const MAX_KEY_CHARS = 256;
23
+ const MAX_BARE_CHARS = 64;
24
+ const NUMBER_PATTERN = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/u;
25
+ /**
26
+ * 一条 JSON 记录的增量解析器:一次一个字符块,行尾 `finish` 取结果。
27
+ * 任何不合法的输入都只把这一条记录标成作废,不抛。
28
+ */
29
+ export class OversizedRecordParser {
30
+ #fields;
31
+ #budget;
32
+ #options;
33
+ #retained = 0;
34
+ #malformed = false;
35
+ #done = false;
36
+ #result = undefined;
37
+ #stack = [];
38
+ // 词法状态。
39
+ #inString = false;
40
+ #stringIsKey = false;
41
+ #escape = false;
42
+ #unicodeLeft = 0;
43
+ #unicodeValue = 0;
44
+ #bare = "";
45
+ // 当前字符串的保留规则与累积。
46
+ #strObserver;
47
+ #strRetain = false;
48
+ #strCap = undefined;
49
+ #strHead = "";
50
+ #strTail = "";
51
+ #strLength = 0;
52
+ #key = "";
53
+ #keyOverflow = false;
54
+ // 值之间的位置。
55
+ #expectKey = false;
56
+ #expectColon = false;
57
+ #expectComma = false;
58
+ #expectValue = true;
59
+ constructor(fields, maxRetainedChars = DEFAULT_MAX_RETAINED_CHARS, options) {
60
+ this.#fields = fields;
61
+ this.#budget = maxRetainedChars;
62
+ this.#options = options;
63
+ }
64
+ write(chunk) {
65
+ if (this.#malformed)
66
+ return;
67
+ try {
68
+ this.#consume(chunk);
69
+ }
70
+ catch (cause) {
71
+ if (cause instanceof MalformedRecord) {
72
+ this.#malformed = true;
73
+ return;
74
+ }
75
+ throw cause;
76
+ }
77
+ }
78
+ /** 完整且合法才返回记录;其余(含空行)返回 `undefined`。 */
79
+ finish() {
80
+ if (this.#malformed)
81
+ return undefined;
82
+ try {
83
+ if (this.#bare.length > 0)
84
+ this.#finishBare();
85
+ }
86
+ catch (cause) {
87
+ if (cause instanceof MalformedRecord)
88
+ return undefined;
89
+ throw cause;
90
+ }
91
+ if (!this.#done || this.#inString || this.#result === SKIPPED)
92
+ return undefined;
93
+ return this.#result;
94
+ }
95
+ #fail() {
96
+ throw new MalformedRecord();
97
+ }
98
+ #charge(chars) {
99
+ this.#retained += chars;
100
+ if (this.#retained > this.#budget)
101
+ this.#fail();
102
+ }
103
+ #consume(chunk) {
104
+ const n = chunk.length;
105
+ let i = 0;
106
+ while (i < n) {
107
+ if (this.#inString) {
108
+ i = this.#consumeString(chunk, i);
109
+ continue;
110
+ }
111
+ const c = chunk.charCodeAt(i);
112
+ if (this.#bare.length > 0) {
113
+ // 数字与 true/false/null 的字符;遇到分隔符就结算。
114
+ if (isBareChar(c)) {
115
+ if (this.#bare.length >= MAX_BARE_CHARS)
116
+ this.#fail();
117
+ this.#bare += chunk[i];
118
+ i += 1;
119
+ continue;
120
+ }
121
+ this.#finishBare();
122
+ }
123
+ i += 1;
124
+ // 空白。
125
+ if (c === 0x20 || c === 0x09 || c === 0x0a || c === 0x0d)
126
+ continue;
127
+ if (this.#done)
128
+ this.#fail();
129
+ switch (c) {
130
+ case 0x7b: // {
131
+ this.#requireValue();
132
+ this.#openContainer(false);
133
+ break;
134
+ case 0x5b: // [
135
+ this.#requireValue();
136
+ this.#openContainer(true);
137
+ break;
138
+ case 0x7d: // }
139
+ this.#closeContainer(false);
140
+ break;
141
+ case 0x5d: // ]
142
+ this.#closeContainer(true);
143
+ break;
144
+ case 0x2c: // ,
145
+ this.#comma();
146
+ break;
147
+ case 0x3a: // :
148
+ if (!this.#expectColon)
149
+ this.#fail();
150
+ this.#expectColon = false;
151
+ this.#expectValue = true;
152
+ break;
153
+ case 0x22: // "
154
+ this.#openString();
155
+ break;
156
+ default:
157
+ if (isBareChar(c)) {
158
+ this.#requireValue();
159
+ this.#bare = chunk[i - 1] ?? "";
160
+ }
161
+ else
162
+ this.#fail();
163
+ }
164
+ }
165
+ }
166
+ #top() {
167
+ return this.#stack[this.#stack.length - 1];
168
+ }
169
+ #requireValue() {
170
+ if (!this.#expectValue)
171
+ this.#fail();
172
+ this.#expectValue = false;
173
+ }
174
+ /** 当前要读的值(刚要开始)落在什么模式;`isContainer` 决定 scalarOnly 清单项是否适用。 */
175
+ #childMode(isContainer) {
176
+ const parent = this.#top();
177
+ if (parent === undefined)
178
+ return this.#classify([], isContainer);
179
+ if (parent.mode === "skip")
180
+ return { mode: "skip", cap: undefined, path: [] };
181
+ if (parent.mode === "keep")
182
+ return { mode: "keep", cap: parent.cap, path: [] };
183
+ const segment = parent.isArray ? String(parent.index) : (parent.key ?? "");
184
+ if (parent.skipValue)
185
+ return { mode: "skip", cap: undefined, path: [] };
186
+ return this.#classify([...parent.path, segment], isContainer);
187
+ }
188
+ #classify(path, isContainer) {
189
+ let descend = false;
190
+ for (const field of this.#fields) {
191
+ const fieldPath = field.path;
192
+ if (fieldPath.length <= path.length) {
193
+ if (field.scalarOnly === true && (isContainer || fieldPath.length !== path.length)) {
194
+ // scalarOnly 项只管它自己那个位置,且只管标量。
195
+ continue;
196
+ }
197
+ if (matchesPrefix(fieldPath, path, fieldPath.length)) {
198
+ return { mode: "keep", cap: field.maxChars, path };
199
+ }
200
+ }
201
+ else if (matchesPrefix(fieldPath, path, path.length)) {
202
+ descend = true;
203
+ }
204
+ }
205
+ return { mode: descend ? "descend" : "skip", cap: undefined, path };
206
+ }
207
+ #openContainer(isArray) {
208
+ if (this.#stack.length >= MAX_DEPTH)
209
+ this.#fail();
210
+ const { mode, cap, path } = this.#childMode(true);
211
+ const keeping = mode !== "skip";
212
+ let target = null;
213
+ if (keeping) {
214
+ this.#charge(16);
215
+ target = isArray ? [] : {};
216
+ }
217
+ this.#stack.push({ isArray, mode, path, cap, target, key: null, skipValue: false, index: 0 });
218
+ this.#expectValue = isArray;
219
+ this.#expectKey = !isArray;
220
+ this.#expectComma = false;
221
+ this.#expectColon = false;
222
+ }
223
+ #closeContainer(isArray) {
224
+ const top = this.#top();
225
+ if (top === undefined || top.isArray !== isArray)
226
+ this.#fail();
227
+ // 刚开就关的空容器合法;逗号之后紧接关括号、或键后没有值都不合法。
228
+ if (!this.#expectComma && top.index > 0)
229
+ this.#fail();
230
+ if (this.#expectColon || (!isArray && this.#expectValue))
231
+ this.#fail();
232
+ this.#stack.pop();
233
+ this.#expectValue = false;
234
+ this.#expectKey = false;
235
+ this.#expectColon = false;
236
+ this.#expectComma = true;
237
+ const empty = top.target !== null && Object.keys(top.target).length === 0;
238
+ this.#store(this.#options?.pruneEmpty && top.mode === "descend" && empty && this.#stack.length > 0
239
+ ? SKIPPED
240
+ : (top.target ?? SKIPPED));
241
+ }
242
+ #comma() {
243
+ const top = this.#top();
244
+ if (top === undefined || !this.#expectComma)
245
+ this.#fail();
246
+ this.#expectComma = false;
247
+ if (top.isArray) {
248
+ this.#expectValue = true;
249
+ }
250
+ else {
251
+ this.#expectKey = true;
252
+ }
253
+ }
254
+ /** 一个值读完:交给外层容器,或成为整条记录。 */
255
+ #store(value) {
256
+ const parent = this.#top();
257
+ if (parent === undefined) {
258
+ this.#result = value;
259
+ this.#done = true;
260
+ this.#expectComma = false;
261
+ return;
262
+ }
263
+ this.#expectComma = true;
264
+ if (parent.isArray) {
265
+ parent.index += 1;
266
+ if (parent.target !== null && value !== SKIPPED) {
267
+ parent.target.push(value);
268
+ this.#charge(8);
269
+ }
270
+ return;
271
+ }
272
+ parent.index += 1;
273
+ const key = parent.key;
274
+ parent.key = null;
275
+ parent.skipValue = false;
276
+ if (parent.target !== null && value !== SKIPPED && key !== null) {
277
+ // defineProperty 才能像 JSON.parse 一样把 "__proto__" 当普通自有键。
278
+ Object.defineProperty(parent.target, key, {
279
+ value,
280
+ enumerable: true,
281
+ writable: true,
282
+ configurable: true,
283
+ });
284
+ this.#charge(key.length + 8);
285
+ }
286
+ }
287
+ #openString() {
288
+ const parent = this.#top();
289
+ const asKey = parent !== undefined && !parent.isArray && this.#expectKey;
290
+ if (!asKey)
291
+ this.#requireValue();
292
+ this.#inString = true;
293
+ this.#stringIsKey = asKey;
294
+ this.#escape = false;
295
+ this.#unicodeLeft = 0;
296
+ this.#strLength = 0;
297
+ this.#strHead = "";
298
+ this.#strTail = "";
299
+ this.#key = "";
300
+ if (asKey) {
301
+ this.#keyOverflow = false;
302
+ this.#strRetain = true;
303
+ this.#strCap = undefined;
304
+ return;
305
+ }
306
+ const { mode, cap, path } = this.#childMode(false);
307
+ this.#strObserver = this.#fields.find((field) => field.path.length === path.length && matchesPrefix(field.path, path, path.length));
308
+ this.#strRetain = mode === "keep" && this.#strObserver?.onChunk === undefined;
309
+ this.#strCap = cap;
310
+ }
311
+ /** 字符串里的一个解码后片段;按保留规则累积。 */
312
+ #append(text) {
313
+ if (text.length === 0)
314
+ return;
315
+ if (this.#stringIsKey) {
316
+ if (this.#keyOverflow)
317
+ return;
318
+ this.#key += text;
319
+ if (this.#key.length > MAX_KEY_CHARS) {
320
+ this.#keyOverflow = true;
321
+ this.#key = "";
322
+ }
323
+ return;
324
+ }
325
+ this.#strLength += text.length;
326
+ this.#strObserver?.onChunk?.(text);
327
+ if (!this.#strRetain)
328
+ return;
329
+ const cap = this.#strCap;
330
+ if (cap === undefined) {
331
+ this.#strHead += text;
332
+ this.#charge(text.length);
333
+ return;
334
+ }
335
+ if (this.#strHead.length < cap) {
336
+ const room = cap - this.#strHead.length;
337
+ this.#strHead += text.slice(0, room);
338
+ text = text.slice(room);
339
+ if (text.length === 0)
340
+ return;
341
+ }
342
+ this.#strTail += text;
343
+ if (this.#strTail.length > cap * 2)
344
+ this.#strTail = this.#strTail.slice(-cap);
345
+ }
346
+ #consumeString(chunk, start) {
347
+ const n = chunk.length;
348
+ let i = start;
349
+ while (i < n) {
350
+ if (this.#unicodeLeft > 0) {
351
+ const digit = hexValue(chunk.charCodeAt(i));
352
+ if (digit < 0)
353
+ this.#fail();
354
+ this.#unicodeValue = this.#unicodeValue * 16 + digit;
355
+ this.#unicodeLeft -= 1;
356
+ i += 1;
357
+ if (this.#unicodeLeft === 0)
358
+ this.#append(String.fromCharCode(this.#unicodeValue));
359
+ continue;
360
+ }
361
+ if (this.#escape) {
362
+ this.#escape = false;
363
+ const c = chunk[i] ?? "";
364
+ i += 1;
365
+ switch (c) {
366
+ case '"':
367
+ case "\\":
368
+ case "/":
369
+ this.#append(c);
370
+ break;
371
+ case "b":
372
+ this.#append("\b");
373
+ break;
374
+ case "f":
375
+ this.#append("\f");
376
+ break;
377
+ case "n":
378
+ this.#append("\n");
379
+ break;
380
+ case "r":
381
+ this.#append("\r");
382
+ break;
383
+ case "t":
384
+ this.#append("\t");
385
+ break;
386
+ case "u":
387
+ this.#unicodeLeft = 4;
388
+ this.#unicodeValue = 0;
389
+ break;
390
+ default:
391
+ this.#fail();
392
+ }
393
+ continue;
394
+ }
395
+ // 普通字符:一路扫到下一个引号、反斜杠或控制字符。
396
+ const runStart = i;
397
+ while (i < n) {
398
+ const c = chunk.charCodeAt(i);
399
+ if (c === 0x22 || c === 0x5c || c < 0x20)
400
+ break;
401
+ i += 1;
402
+ }
403
+ if (i > runStart)
404
+ this.#append(chunk.slice(runStart, i));
405
+ if (i >= n)
406
+ break;
407
+ const c = chunk.charCodeAt(i);
408
+ i += 1;
409
+ if (c === 0x5c) {
410
+ this.#escape = true;
411
+ }
412
+ else if (c === 0x22) {
413
+ this.#inString = false;
414
+ this.#endString();
415
+ return i;
416
+ }
417
+ else {
418
+ this.#fail(); // 字符串里不许有裸控制字符。
419
+ }
420
+ }
421
+ return i;
422
+ }
423
+ #endString() {
424
+ if (this.#stringIsKey) {
425
+ const parent = this.#top();
426
+ if (parent === undefined)
427
+ this.#fail();
428
+ parent.key = this.#key;
429
+ parent.skipValue = this.#keyOverflow;
430
+ this.#key = "";
431
+ this.#expectKey = false;
432
+ this.#expectColon = true;
433
+ return;
434
+ }
435
+ this.#strObserver?.onEnd?.(this.#strLength);
436
+ this.#strObserver = undefined;
437
+ if (!this.#strRetain) {
438
+ this.#store(SKIPPED);
439
+ return;
440
+ }
441
+ const cap = this.#strCap;
442
+ let text = this.#strHead;
443
+ if (cap !== undefined) {
444
+ let tail = this.#strTail;
445
+ if (tail.length > cap)
446
+ tail = tail.slice(-cap);
447
+ if (this.#strLength > text.length + tail.length) {
448
+ // 确有省略:别把代理对从边界中间劈开。
449
+ if (isHighSurrogate(text.charCodeAt(text.length - 1)))
450
+ text = text.slice(0, -1);
451
+ if (isLowSurrogate(tail.charCodeAt(0)))
452
+ tail = tail.slice(1);
453
+ const omitted = this.#strLength - text.length - tail.length;
454
+ text = `${text}\n[… ${omitted} chars omitted …]\n${tail}`;
455
+ }
456
+ else {
457
+ text = `${text}${tail}`;
458
+ }
459
+ this.#charge(text.length);
460
+ }
461
+ this.#strHead = "";
462
+ this.#strTail = "";
463
+ this.#store(text);
464
+ }
465
+ #finishBare() {
466
+ const text = this.#bare;
467
+ this.#bare = "";
468
+ const { mode, path } = this.#childMode(false);
469
+ let value;
470
+ if (text === "true")
471
+ value = true;
472
+ else if (text === "false")
473
+ value = false;
474
+ else if (text === "null")
475
+ value = null;
476
+ else if (NUMBER_PATTERN.test(text))
477
+ value = Number(text);
478
+ else
479
+ this.#fail();
480
+ const observer = this.#fields.find((field) => field.path.length === path.length && matchesPrefix(field.path, path, path.length));
481
+ if (observer?.onScalar !== undefined) {
482
+ observer.onScalar(value);
483
+ this.#store(SKIPPED);
484
+ }
485
+ else
486
+ this.#store(mode === "keep" ? value : SKIPPED);
487
+ }
488
+ }
489
+ const SKIPPED = Symbol("skipped");
490
+ class MalformedRecord extends Error {
491
+ }
492
+ function isBareChar(c) {
493
+ return ((c >= 0x30 && c <= 0x39) || // 0-9
494
+ (c >= 0x61 && c <= 0x7a) || // a-z
495
+ (c >= 0x41 && c <= 0x5a) || // A-Z
496
+ c === 0x2b ||
497
+ c === 0x2d ||
498
+ c === 0x2e);
499
+ }
500
+ function hexValue(c) {
501
+ if (c >= 0x30 && c <= 0x39)
502
+ return c - 0x30;
503
+ if (c >= 0x61 && c <= 0x66)
504
+ return c - 0x61 + 10;
505
+ if (c >= 0x41 && c <= 0x46)
506
+ return c - 0x41 + 10;
507
+ return -1;
508
+ }
509
+ const isHighSurrogate = (c) => c >= 0xd800 && c <= 0xdbff;
510
+ const isLowSurrogate = (c) => c >= 0xdc00 && c <= 0xdfff;
511
+ function matchesPrefix(pattern, path, length) {
512
+ for (let index = 0; index < length; index += 1) {
513
+ const expected = pattern[index];
514
+ if (expected !== "*" && expected !== path[index])
515
+ return false;
516
+ }
517
+ return true;
518
+ }
519
+ /**
520
+ * 一行转成记录:没超长的先过 `accepts` 预筛再 `JSON.parse`,超长的已经是筛好的记录。
521
+ * 解析失败(写到一半的尾行是常态)返回 `undefined`。
522
+ */
523
+ export function jsonlEntryRecord(entry, accepts) {
524
+ if ("oversized" in entry)
525
+ return entry.oversized;
526
+ if (!accepts(entry.line))
527
+ return undefined;
528
+ try {
529
+ return JSON.parse(entry.line);
530
+ }
531
+ catch {
532
+ return undefined;
533
+ }
534
+ }
535
+ /**
536
+ * 逐行读一份 jsonl。行以 `\n` 结尾(尾部 `\r` 去掉),空行不吐。
537
+ *
538
+ * 读失败(文件不存在、权限)原样抛出,由调用方决定是空还是错。
539
+ */
540
+ export async function* readJsonlEntries(fs, path, options) {
541
+ const decoder = new TextDecoder("utf-8");
542
+ let pieces = [];
543
+ let pending = 0;
544
+ let parser = null;
545
+ const startOversized = () => {
546
+ const next = new OversizedRecordParser(options.fields, options.maxRetainedChars);
547
+ for (const piece of pieces)
548
+ next.write(piece);
549
+ pieces = [];
550
+ pending = 0;
551
+ return next;
552
+ };
553
+ // 把一段不含换行的文本接到当前行上。
554
+ const append = (text) => {
555
+ if (text.length === 0)
556
+ return;
557
+ if (parser !== null) {
558
+ parser.write(text);
559
+ return;
560
+ }
561
+ pieces.push(text);
562
+ pending += text.length;
563
+ if (pending > options.maxLineChars)
564
+ parser = startOversized();
565
+ };
566
+ const takeLine = () => {
567
+ if (parser !== null) {
568
+ const record = parser.finish();
569
+ parser = null;
570
+ return { oversized: record };
571
+ }
572
+ if (pending === 0)
573
+ return null;
574
+ let line = pieces.length === 1 ? (pieces[0] ?? "") : pieces.join("");
575
+ pieces = [];
576
+ pending = 0;
577
+ if (line.endsWith("\r"))
578
+ line = line.slice(0, -1);
579
+ return line.length === 0 ? null : { line };
580
+ };
581
+ const feed = function* (text) {
582
+ let from = 0;
583
+ while (from <= text.length) {
584
+ const newline = text.indexOf("\n", from);
585
+ if (newline < 0) {
586
+ append(text.slice(from));
587
+ return;
588
+ }
589
+ append(text.slice(from, newline));
590
+ const entry = takeLine();
591
+ if (entry !== null)
592
+ yield entry;
593
+ from = newline + 1;
594
+ }
595
+ };
596
+ for await (const chunk of fs.createReadStream(path)) {
597
+ yield* feed(decoder.decode(chunk, { stream: true }));
598
+ }
599
+ yield* feed(decoder.decode());
600
+ const last = takeLine();
601
+ if (last !== null)
602
+ yield last;
603
+ }
@@ -0,0 +1,69 @@
1
+ import { type AppendedJsonlPosition } from "./appendedJsonl.ts";
2
+ /** 落盘时把重复的字符串(模型名、会话 id)驻留成下标。 */
3
+ /**
4
+ * 落盘缓存的驻留表与文件表。版本不对、表不是字符串数组、或 files 不是对象时整份作废。
5
+ * 驻留表必须全是字符串:混进一个数字会越过后面的 undefined 守卫,落到记录的 model 里,
6
+ * 到宿主按模型名查价时才崩。
7
+ */
8
+ export type ScanCacheTables = {
9
+ readonly models: readonly string[];
10
+ readonly sessions: readonly string[];
11
+ readonly files: Record<string, unknown>;
12
+ };
13
+ export declare function decodeScanCacheTables(document: unknown, version: number): ScanCacheTables | undefined;
14
+ export declare function createStringTable(): {
15
+ readonly values: readonly string[];
16
+ readonly intern: (value: string) => number;
17
+ };
18
+ /**
19
+ * 一个文件的缓存条目落盘:`s` 大小、`m` 修改时间、`r` / `t` 行记录与末尾段记录(按 `serialize`
20
+ * 的行式形状)、`o` / `gl` / `gh` 续读位置。执行器自己的字段接在后面。
21
+ */
22
+ export declare function encodeCachedFile<R, Row>(entry: {
23
+ readonly size: number;
24
+ readonly mtimeMs: number;
25
+ readonly records: readonly R[];
26
+ readonly tailRecords: readonly R[];
27
+ readonly position: AppendedJsonlPosition;
28
+ }, serialize: (record: R) => Row): {
29
+ s: number;
30
+ m: number;
31
+ r: Row[];
32
+ t: Row[];
33
+ o: number;
34
+ gl: number;
35
+ gh: number;
36
+ };
37
+ /**
38
+ * 落盘的续读位置:`o` 偏移、`gl` guard 长度、`gh` guard 哈希。
39
+ *
40
+ * 位置字段会变成读取的字节偏移与窗口长度:越界的值必须整条拒掉(返回 undefined),否则一个
41
+ * 离谱的 guard 长度会让这个文件的每次解析都失败,用量悄悄丢掉,而不是付一次冷重扫。
42
+ */
43
+ export declare function decodeCachedPosition(entry: {
44
+ readonly o?: unknown;
45
+ readonly gl?: unknown;
46
+ readonly gh?: unknown;
47
+ }): AppendedJsonlPosition | undefined;
48
+ export type PruneOptions = {
49
+ /** 这次遍历刚看到的文件。只在遍历过的窗口里有意义。 */
50
+ readonly livePaths: ReadonlySet<string>;
51
+ /**
52
+ * 遍历实际走完的根。文件不在 `livePaths` 里,只有它的根确实走过才说明它没了:这次解析不出
53
+ * 目录的根,它的热条目不能被清掉。
54
+ */
55
+ readonly walkedRoots: readonly string[];
56
+ /** 遍历窗口的起点;比它更早的条目当时没去找。 */
57
+ readonly windowStartMs: number;
58
+ /** 比它更老的条目不论如何都丢。 */
59
+ readonly retentionCutoffMs: number;
60
+ };
61
+ /**
62
+ * 丢掉过期条目,以及文件已经消失的条目。返回丢掉的条数。
63
+ *
64
+ * 遍历只覆盖请求的窗口,所以「不在 livePaths 里」只对窗口内的条目说明文件被删了。
65
+ * 把遍历没碰到的全剪掉,会让每次看 7 天都把 30 天的条目逐出去。
66
+ */
67
+ export declare function pruneScanCache<T extends {
68
+ readonly mtimeMs: number;
69
+ }>(cache: Map<string, T>, options: PruneOptions): number;