@sema-agent/core 7.69.0 → 7.69.1

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 (309) hide show
  1. package/CHANGELOG.md +583 -0
  2. package/dist/agents/repair-loop.js +6 -14
  3. package/dist/agents/subagent.js +2 -1
  4. package/dist/agents/verify.d.ts +1 -1
  5. package/dist/agents/verify.js +7 -1
  6. package/dist/brain/reasoning-request.d.ts +7 -0
  7. package/dist/brain/reasoning-request.js +6 -0
  8. package/dist/core/agent-config-dir.d.ts +39 -3
  9. package/dist/core/agent-config-dir.js +67 -11
  10. package/dist/core/allow-entry.d.ts +5 -5
  11. package/dist/core/allow-entry.js +12 -6
  12. package/dist/core/ask-headline.d.ts +2 -2
  13. package/dist/core/ask-origin.d.ts +31 -31
  14. package/dist/core/ask-origin.js +20 -13
  15. package/dist/core/attachment-source.d.ts +1 -1
  16. package/dist/core/attachment-source.js +1 -0
  17. package/dist/core/auto-compaction.d.ts +44 -62
  18. package/dist/core/auto-compaction.js +44 -103
  19. package/dist/core/cache-break-detector.d.ts +23 -11
  20. package/dist/core/cache-break-detector.js +11 -5
  21. package/dist/core/checkpoint-features.d.ts +1 -1
  22. package/dist/core/checkpoint-features.js +1 -1
  23. package/dist/core/checkpoint-requirements.d.ts +19 -1
  24. package/dist/core/checkpoint-requirements.js +6 -0
  25. package/dist/core/checkpoint-row-verdict.js +3 -0
  26. package/dist/core/checkpoint-store.d.ts +13 -2
  27. package/dist/core/child-clamp-seats.d.ts +30 -3
  28. package/dist/core/child-clamp-seats.js +24 -0
  29. package/dist/core/compaction-retired-members.d.ts +12 -0
  30. package/dist/core/compaction-retired-members.js +22 -0
  31. package/dist/core/config-refusal-codes.d.ts +1 -1
  32. package/dist/core/config-refusal-codes.js +2 -0
  33. package/dist/core/context-edit.d.ts +32 -17
  34. package/dist/core/context-edit.js +28 -0
  35. package/dist/core/cron.d.ts +68 -0
  36. package/dist/core/cron.js +259 -0
  37. package/dist/core/engine-attestation.d.ts +16 -0
  38. package/dist/core/engine-attestation.js +2 -0
  39. package/dist/core/engine-capabilities.d.ts +14 -1
  40. package/dist/core/engine-capabilities.js +1 -1
  41. package/dist/core/engine-notice.d.ts +18 -7
  42. package/dist/core/engine-version.js +1 -1
  43. package/dist/core/gate-outcome.d.ts +29 -2
  44. package/dist/core/gate-outcome.js +6 -0
  45. package/dist/core/git-read-only.d.ts +10 -0
  46. package/dist/core/git-read-only.js +1 -0
  47. package/dist/core/git-worktree-env.js +9 -8
  48. package/dist/core/governance-codes.d.ts +9 -2
  49. package/dist/core/governance-codes.js +9 -0
  50. package/dist/core/locked-config.js +7 -3
  51. package/dist/core/lsp.d.ts +3 -0
  52. package/dist/core/lsp.js +3 -2
  53. package/dist/core/memory-engine/engine.d.ts +48 -12
  54. package/dist/core/memory-engine/engine.js +108 -46
  55. package/dist/core/memory-engine/file-backend.d.ts +2 -1
  56. package/dist/core/memory-engine/index-copies.d.ts +47 -0
  57. package/dist/core/memory-engine/index-copies.js +88 -0
  58. package/dist/core/memory-engine/layout.d.ts +20 -3
  59. package/dist/core/memory-engine/layout.js +39 -3
  60. package/dist/core/memory-engine/origin-clearance.d.ts +9 -7
  61. package/dist/core/memory-engine/origin-clearance.js +2 -4
  62. package/dist/core/memory-engine/types.d.ts +16 -3
  63. package/dist/core/mode-policy.d.ts +7 -1
  64. package/dist/core/mode-policy.js +9 -0
  65. package/dist/core/mods/session-compact.js +3 -3
  66. package/dist/core/mods/stations.d.ts +2 -0
  67. package/dist/core/mods/stations.js +2 -1
  68. package/dist/core/path-guard-rows.d.ts +8 -3
  69. package/dist/core/path-guard-rows.js +18 -5
  70. package/dist/core/peer-seat.d.ts +3 -0
  71. package/dist/core/peer-seat.js +3 -0
  72. package/dist/core/permission-rule-model.js +3 -2
  73. package/dist/core/process-identity.d.ts +11 -3
  74. package/dist/core/process-identity.js +26 -16
  75. package/dist/core/runner/assemble-result.d.ts +5 -0
  76. package/dist/core/runner/assemble-result.js +1 -1
  77. package/dist/core/runner/cleared-results-record.d.ts +33 -0
  78. package/dist/core/runner/cleared-results-record.js +56 -0
  79. package/dist/core/runner/compact-session.js +14 -9
  80. package/dist/core/runner/compaction-knobs.d.ts +1 -1
  81. package/dist/core/runner/continuation-gate.js +2 -1
  82. package/dist/core/runner/continues-from.d.ts +1 -1
  83. package/dist/core/runner/continues-from.js +1 -1
  84. package/dist/core/runner/contracts.d.ts +83 -31
  85. package/dist/core/runner/conversation-segment.d.ts +19 -12
  86. package/dist/core/runner/conversation-segment.js +27 -7
  87. package/dist/core/runner/derived-route-fallback.d.ts +2 -12
  88. package/dist/core/runner/derived-route-fallback.js +0 -6
  89. package/dist/core/runner/gate-exit.d.ts +2 -1
  90. package/dist/core/runner/gate-exit.js +4 -2
  91. package/dist/core/runner/git-status-frame.d.ts +2 -2
  92. package/dist/core/runner/git-status-frame.js +2 -1
  93. package/dist/core/runner/initial-run-state.js +1 -1
  94. package/dist/core/runner/leg-caller-places.d.ts +39 -0
  95. package/dist/core/runner/leg-caller-places.js +23 -0
  96. package/dist/core/runner/leg-frame.d.ts +8 -2
  97. package/dist/core/runner/leg-receipt.d.ts +29 -2
  98. package/dist/core/runner/leg-receipt.js +37 -6
  99. package/dist/core/runner/media-removed-seat.d.ts +10 -6
  100. package/dist/core/runner/media-removed-seat.js +6 -3
  101. package/dist/core/runner/messages-omitted-seat.d.ts +2 -1
  102. package/dist/core/runner/messages-omitted-seat.js +15 -5
  103. package/dist/core/runner/person-path-rows.d.ts +25 -7
  104. package/dist/core/runner/person-path-rows.js +16 -2
  105. package/dist/core/runner/prefix-ledger.d.ts +120 -0
  106. package/dist/core/runner/prefix-ledger.js +176 -0
  107. package/dist/core/runner/prepare-advisor.d.ts +2 -0
  108. package/dist/core/runner/prepare-advisor.js +2 -1
  109. package/dist/core/runner/prepare-artifact.d.ts +3 -0
  110. package/dist/core/runner/prepare-artifact.js +2 -2
  111. package/dist/core/runner/prepare-caps-and-workflow.d.ts +2 -0
  112. package/dist/core/runner/prepare-caps-and-workflow.js +4 -3
  113. package/dist/core/runner/prepare-config-doors.d.ts +9 -8
  114. package/dist/core/runner/prepare-config-doors.js +15 -12
  115. package/dist/core/runner/prepare-context-lane.d.ts +25 -6
  116. package/dist/core/runner/prepare-context-lane.js +82 -27
  117. package/dist/core/runner/prepare-gate-stations.d.ts +1 -2
  118. package/dist/core/runner/prepare-gate-stations.js +2 -4
  119. package/dist/core/runner/prepare-hands-readface.d.ts +18 -5
  120. package/dist/core/runner/prepare-hands-readface.js +41 -22
  121. package/dist/core/runner/prepare-inherited-gate.d.ts +5 -0
  122. package/dist/core/runner/prepare-inherited-gate.js +2 -0
  123. package/dist/core/runner/prepare-memory.d.ts +10 -2
  124. package/dist/core/runner/prepare-memory.js +51 -5
  125. package/dist/core/runner/prepare-policy-chain.d.ts +3 -0
  126. package/dist/core/runner/prepare-policy-chain.js +13 -2
  127. package/dist/core/runner/prepare-prompt-assembly.d.ts +12 -4
  128. package/dist/core/runner/prepare-prompt-assembly.js +10 -4
  129. package/dist/core/runner/prepare-prompt-inputs.d.ts +3 -7
  130. package/dist/core/runner/prepare-prompt-inputs.js +6 -7
  131. package/dist/core/runner/prepare-safety-scan.js +7 -0
  132. package/dist/core/runner/prepare-suspend-saga.d.ts +4 -0
  133. package/dist/core/runner/prepare-suspend-saga.js +3 -2
  134. package/dist/core/runner/prepare-task.js +59 -53
  135. package/dist/core/runner/prepare-tool-disclosure-mount.d.ts +5 -11
  136. package/dist/core/runner/prepare-tool-disclosure-mount.js +7 -8
  137. package/dist/core/runner/prepare-turn-wiring.d.ts +16 -25
  138. package/dist/core/runner/prepare-turn-wiring.js +8 -18
  139. package/dist/core/runner/prepare-wiring-manifest.d.ts +1 -0
  140. package/dist/core/runner/prepare-wiring-manifest.js +3 -2
  141. package/dist/core/runner/prompt-suggestions.d.ts +3 -1
  142. package/dist/core/runner/prompt-suggestions.js +1 -1
  143. package/dist/core/runner/read-block-judge.d.ts +47 -0
  144. package/dist/core/runner/read-block-judge.js +33 -0
  145. package/dist/core/runner/role-resolution-table.d.ts +15 -10
  146. package/dist/core/runner/role-resolution-table.js +4 -5
  147. package/dist/core/runner/rule-store-unreadable-notice.d.ts +7 -4
  148. package/dist/core/runner/rule-store-unreadable-notice.js +4 -3
  149. package/dist/core/runner/run-compaction-machinery.d.ts +0 -10
  150. package/dist/core/runner/run-compaction-machinery.js +3 -39
  151. package/dist/core/runner/run-harness-handlers.js +4 -17
  152. package/dist/core/runner/run-identity-wiring.js +4 -1
  153. package/dist/core/runner/run-leg.js +11 -1
  154. package/dist/core/runner/run-recovery-lanes.d.ts +0 -4
  155. package/dist/core/runner/run-recovery-lanes.js +34 -18
  156. package/dist/core/runner/run-settle-and-teardown.d.ts +1 -3
  157. package/dist/core/runner/run-settle-and-teardown.js +14 -22
  158. package/dist/core/runner/run-telemetry-and-budget-seats.js +0 -1
  159. package/dist/core/runner/run-terminal-adoption.d.ts +11 -30
  160. package/dist/core/runner/run-terminal-adoption.js +5 -88
  161. package/dist/core/runner/run-tool-mount-facts.js +1 -5
  162. package/dist/core/runner/run-turn-boundary.js +56 -26
  163. package/dist/core/runner/runtask.js +7 -7
  164. package/dist/core/runner/session-hash-salt.d.ts +18 -0
  165. package/dist/core/runner/session-hash-salt.js +36 -0
  166. package/dist/core/runner/shell-env-withheld.d.ts +8 -5
  167. package/dist/core/runner/shell-env-withheld.js +3 -3
  168. package/dist/core/runner/side-call-account.d.ts +9 -1
  169. package/dist/core/runner/side-call-account.js +15 -4
  170. package/dist/core/runner/skill-mention.d.ts +12 -0
  171. package/dist/core/runner/skill-mention.js +50 -0
  172. package/dist/core/runner/stream-settle-backstop.js +1 -0
  173. package/dist/core/runner/synthetic-tools.js +2 -2
  174. package/dist/core/runner/tool-table-view.d.ts +5 -9
  175. package/dist/core/runner/tool-table-view.js +23 -21
  176. package/dist/core/runner/write-protection-station.d.ts +5 -2
  177. package/dist/core/runner/write-protection-station.js +1 -1
  178. package/dist/core/runner-deps.d.ts +25 -27
  179. package/dist/core/scheduler.d.ts +13 -3
  180. package/dist/core/scheduler.js +0 -12
  181. package/dist/core/secret-env.d.ts +47 -11
  182. package/dist/core/secret-env.js +65 -4
  183. package/dist/core/sensitive-path-policy.js +4 -0
  184. package/dist/core/session-checkout.js +10 -9
  185. package/dist/core/shell-read-silence.d.ts +2 -1
  186. package/dist/core/shell-run-candidates.d.ts +1 -1
  187. package/dist/core/shell-run-candidates.js +11 -3
  188. package/dist/core/task-event.d.ts +32 -10
  189. package/dist/core/task-intake.d.ts +5 -3
  190. package/dist/core/task-intake.js +6 -2
  191. package/dist/core/task-result.d.ts +39 -0
  192. package/dist/core/task-spec-members.d.ts +3 -0
  193. package/dist/core/task-spec-members.js +2 -2
  194. package/dist/core/task-spec.d.ts +37 -20
  195. package/dist/core/tool-policy.d.ts +30 -18
  196. package/dist/core/tool-policy.js +44 -32
  197. package/dist/core/tool-roster.d.ts +21 -7
  198. package/dist/core/tool-roster.js +34 -6
  199. package/dist/core/tool-spec-members.js +1 -1
  200. package/dist/core/tool-spec.d.ts +27 -0
  201. package/dist/core/tools.js +1 -0
  202. package/dist/core/trace.d.ts +42 -19
  203. package/dist/core/unc-spelling.d.ts +31 -0
  204. package/dist/core/unc-spelling.js +51 -0
  205. package/dist/core/wiring-manifest.d.ts +30 -3
  206. package/dist/core/wiring-manifest.js +10 -0
  207. package/dist/core/workflow-run-store-contract.js +22 -0
  208. package/dist/core/workflow-run-store.d.ts +17 -13
  209. package/dist/core/workflow-run-store.js +1 -1
  210. package/dist/core/write-protect.d.ts +4 -0
  211. package/dist/core/write-protect.js +17 -15
  212. package/dist/engine/compaction/compaction.d.ts +48 -98
  213. package/dist/engine/compaction/compaction.js +136 -216
  214. package/dist/engine/compaction/utils.d.ts +2 -11
  215. package/dist/engine/compaction/utils.js +4 -13
  216. package/dist/engine/execution-env/node-execution-env.js +17 -6
  217. package/dist/engine/harness/agent-harness.d.ts +19 -5
  218. package/dist/engine/harness/agent-harness.js +18 -7
  219. package/dist/engine/harness/types.d.ts +5 -7
  220. package/dist/engine/loop/agent-loop.d.ts +14 -1
  221. package/dist/engine/loop/agent-loop.js +31 -11
  222. package/dist/engine/loop/types.d.ts +8 -2
  223. package/dist/index.d.ts +9 -6
  224. package/dist/index.js +8 -6
  225. package/dist/internal/harness-types.d.ts +1 -1
  226. package/dist/internal/harness.d.ts +1 -1
  227. package/dist/internal/harness.js +1 -1
  228. package/dist/orchestration/run-spec.js +7 -2
  229. package/dist/orchestration/run-workflow-tool.js +5 -3
  230. package/dist/orchestration/workflow-park-adoption.d.ts +66 -8
  231. package/dist/orchestration/workflow-park-adoption.js +195 -45
  232. package/dist/orchestration/workflow-types.d.ts +20 -0
  233. package/dist/orchestration/workflow-types.js +1 -0
  234. package/dist/orchestration/workflow.d.ts +3 -2
  235. package/dist/orchestration/workflow.js +10 -4
  236. package/dist/prompt-assembly/catalog/core-behavior.js +6 -0
  237. package/dist/prompt-assembly/catalog/events.d.ts +8 -0
  238. package/dist/prompt-assembly/catalog/locales/zh-Hans/core-behavior.js +6 -0
  239. package/dist/prompt-assembly/catalog/locales/zh-Hans/mechanism.js +1 -0
  240. package/dist/prompt-assembly/catalog/mechanism.d.ts +8 -0
  241. package/dist/prompt-assembly/catalog/mechanism.js +1 -0
  242. package/dist/prompt-assembly/catalog/schema.d.ts +1 -1
  243. package/dist/prompt-assembly/catalog/schema.js +1 -0
  244. package/dist/prompt-assembly/catalog/sections.js +8 -0
  245. package/dist/prompt-assembly/event-registry.js +1 -0
  246. package/dist/prompt-assembly/select.js +1 -0
  247. package/dist/prompt-assembly/turn-snapshot.d.ts +14 -27
  248. package/dist/prompt-assembly/turn-snapshot.js +6 -18
  249. package/dist/prompt-assembly/types.d.ts +3 -0
  250. package/dist/prompts/default.d.ts +9 -0
  251. package/dist/prompts/default.js +4 -0
  252. package/dist/shell/lex.d.ts +75 -11
  253. package/dist/shell/lex.js +340 -76
  254. package/dist/tools/fs/bash-program-position.js +2 -6
  255. package/dist/tools/fs/bash-readonly-classifier.d.ts +18 -3
  256. package/dist/tools/fs/bash-readonly-classifier.js +19 -9
  257. package/dist/tools/fs/containment-face.d.ts +12 -3
  258. package/dist/tools/fs/containment-face.js +16 -7
  259. package/dist/tools/fs/file-read-check.d.ts +26 -1
  260. package/dist/tools/fs/file-read-check.js +20 -2
  261. package/dist/tools/fs/fs-bash.js +4 -4
  262. package/dist/tools/fs/fs-read.d.ts +3 -0
  263. package/dist/tools/fs/fs-read.js +2 -2
  264. package/dist/tools/fs/fs-search-tools.d.ts +2 -2
  265. package/dist/tools/fs/fs-search-tools.js +4 -4
  266. package/dist/tools/fs/index.js +5 -3
  267. package/dist/tools/fs/pdf.js +12 -4
  268. package/dist/tools/fs/read-deny-home.d.ts +13 -2
  269. package/dist/tools/fs/read-deny-home.js +26 -22
  270. package/dist/tools/fs/read-deny.js +15 -6
  271. package/dist/tools/fs/removal-targets.d.ts +33 -17
  272. package/dist/tools/fs/removal-targets.js +160 -61
  273. package/dist/tools/fs/repo-map.d.ts +1 -1
  274. package/dist/tools/fs/repo-map.js +2 -2
  275. package/dist/tools/fs/safety.d.ts +28 -5
  276. package/dist/tools/fs/safety.js +72 -8
  277. package/dist/tools/fs/shell-read-bindings.d.ts +8 -2
  278. package/dist/tools/fs/shell-read-bindings.js +25 -6
  279. package/dist/tools/fs/shell-read-check.d.ts +3 -0
  280. package/dist/tools/fs/shell-read-check.js +22 -4
  281. package/dist/tools/fs/shell-read-establish.js +73 -43
  282. package/dist/tools/fs/shell-read-judge.d.ts +32 -14
  283. package/dist/tools/fs/shell-read-judge.js +13 -5
  284. package/dist/tools/fs/shell-read-object-path.d.ts +5 -1
  285. package/dist/tools/fs/shell-read-object-path.js +17 -4
  286. package/dist/tools/fs/shell-read-plan.d.ts +8 -2
  287. package/dist/tools/fs/shell-read-plan.js +36 -22
  288. package/dist/tools/fs/shell-read-sourced.js +12 -4
  289. package/dist/tools/fs/shell-read-start.d.ts +4 -2
  290. package/dist/tools/fs/shell-read-start.js +3 -3
  291. package/dist/tools/fs/shell-read-workdir.d.ts +14 -6
  292. package/dist/tools/fs/shell-read-workdir.js +35 -17
  293. package/dist/tools/scheduler-tools.d.ts +7 -17
  294. package/dist/tools/scheduler-tools.js +9 -85
  295. package/dist/tools/worktree.js +5 -4
  296. package/package.json +3 -2
  297. package/test/export-surface.snapshot.json +47 -1
  298. package/wire-changes/core-stores-sql@7.68.6.json +39 -0
  299. package/wire-changes/core-stores-sql@7.69.1.json +39 -0
  300. package/wire-changes/core@7.68.6.json +125 -0
  301. package/wire-changes/core@7.69.1.json +2561 -0
  302. package/wire-changes/mods-kernel@7.68.6.json +39 -0
  303. package/wire-changes/mods-kernel@7.69.1.json +39 -0
  304. package/wire-changes/primitives@7.68.6.json +39 -0
  305. package/wire-changes/primitives@7.69.1.json +39 -0
  306. package/wire-changes/wiki-gate@7.68.6.json +39 -0
  307. package/wire-changes/wiki-gate@7.69.1.json +39 -0
  308. package/dist/core/runner/prompt-hash-salt.d.ts +0 -1
  309. package/dist/core/runner/prompt-hash-salt.js +0 -2
package/dist/shell/lex.js CHANGED
@@ -46,6 +46,18 @@ export const PARSE_FAILURE_SENTENCE = {
46
46
  heredoc_without_delimiter: "a here-document operator names no delimiter",
47
47
  heredoc_delimiter_expands: "a here-document delimiter is not a plain word",
48
48
  };
49
+ const STATEMENT_RUNS_PAST = new Set(["undelimited_substitution", "undelimited_substitution_heredoc", "heredoc_delimiter_expands"]);
50
+ export function segmentsAroundStop(source, failure) {
51
+ const stopped = failure.readBefore.at(-1);
52
+ const tokens = stopped.tokens.filter((t) => t.kind !== "heredoc");
53
+ const start = source.length - stopped.text.length;
54
+ const runs = (e) => [...(e.kind === "command" || e.kind === "process" || e.kind === "arithmetic" ? [e.body ?? ""] : []), ...(e.inner ?? []).flatMap(runs)];
55
+ const substitutions = tokens.flatMap((t) => (t.kind === "word" ? assignedWords(t) : t.kind === "redirect" && t.target !== undefined ? [t.target] : [])).flatMap((w) => w.pieces.flatMap((p) => (p.expansion !== undefined ? runs(p.expansion) : [])));
56
+ const currentShell = stopped.currentShell?.filter((b) => substitutions.includes(b));
57
+ const { currentShell: _all, ...rest } = stopped;
58
+ const head = STATEMENT_RUNS_PAST.has(failure.parseFailure.reason) && tokens.length > 0 ? [{ ...rest, text: source.slice(start, Math.max(start, failure.parseFailure.at)), tokens, substitutions, ...(currentShell !== undefined && currentShell.length > 0 ? { currentShell } : {}), spliced: true, stopHead: true }] : [];
59
+ return [...failure.readBefore.slice(0, -1), ...head, ...segmentsPastStop(source, failure)];
60
+ }
49
61
  export const BASH_ONLY_CONSTRUCTS = Object.freeze([
50
62
  "ansi_c_quoting",
51
63
  "locale_string",
@@ -116,17 +128,30 @@ function decodeAnsiC(body) {
116
128
  continue;
117
129
  }
118
130
  const next = body[i + 1];
119
- const folded = ANSI_C_ESCAPES[next];
131
+ const folded = ANSI_C_ESCAPES[next] ?? numericEscapeAt(body, i + 1);
120
132
  if (folded === undefined) {
121
133
  unresolved = true;
122
134
  text += c + next;
135
+ i++;
123
136
  }
124
- else
137
+ else if (typeof folded === "string") {
125
138
  text += folded;
126
- i++;
139
+ i++;
140
+ }
141
+ else {
142
+ text += folded.char;
143
+ i += folded.length;
144
+ }
127
145
  }
128
146
  return { text, unresolved };
129
147
  }
148
+ function numericEscapeAt(body, at) {
149
+ const m = /^(?:x([0-9A-Fa-f]{1,2})|([0-7]{1,3})|c([!-~]))/.exec(body.slice(at, at + 4));
150
+ if (m === null || m[3] === "?" || m[3] === "\\" || m[3] === "'")
151
+ return undefined;
152
+ const byte = m[1] !== undefined ? parseInt(m[1], 16) : m[2] !== undefined ? parseInt(m[2], 8) & 0xff : m[3].charCodeAt(0) & 0x1f;
153
+ return byte > 0 && byte < 0x80 ? { char: String.fromCharCode(byte), length: m[0].length } : undefined;
154
+ }
130
155
  export function argvOf(segment) {
131
156
  return segment.tokens.filter((t) => t.kind === "word");
132
157
  }
@@ -164,14 +189,17 @@ function documentLine(text, p, joined) {
164
189
  }
165
190
  return { line, nl };
166
191
  }
167
- function heredocBodies(text, from, headers, closer) {
192
+ function heredocBodies(text, from, headers, closer, toEnd = false) {
168
193
  const bodies = [];
169
194
  let p = from;
170
195
  for (const h of headers) {
171
196
  const lines = [];
172
197
  for (;;) {
173
- if (p > text.length)
198
+ if (p > text.length) {
199
+ if (toEnd)
200
+ break;
174
201
  return "unterminated";
202
+ }
175
203
  const { line, nl } = documentLine(text, p, !h.quoted);
176
204
  if (closer !== undefined && text.slice(p, nl < 0 ? text.length : nl + 1).includes("\\\n"))
177
205
  return "ambiguous";
@@ -182,8 +210,14 @@ function heredocBodies(text, from, headers, closer) {
182
210
  }
183
211
  if (closer !== undefined && compared.startsWith(h.delimiter) && (closer === ")" ? compared.indexOf(")", h.delimiter.length) >= 0 : /^[ \t]*\}/.test(compared.slice(h.delimiter.length))))
184
212
  return "ambiguous";
185
- if (nl < 0)
186
- return "unterminated";
213
+ if (nl < 0) {
214
+ if (!toEnd)
215
+ return "unterminated";
216
+ if (p < text.length)
217
+ lines.push(line);
218
+ p = text.length + 1;
219
+ break;
220
+ }
187
221
  lines.push(line);
188
222
  p = nl + 1;
189
223
  }
@@ -303,7 +337,7 @@ function walkBody(text, open, reading, inDouble, level) {
303
337
  j = run.end - 1;
304
338
  continue;
305
339
  }
306
- const close = braceExpansionEnd(text, k);
340
+ const close = braceExpansionEnd(text, k, true, level);
307
341
  if (close === undefined)
308
342
  return "unterminated";
309
343
  j = close - 1;
@@ -395,7 +429,7 @@ function walkBody(text, open, reading, inDouble, level) {
395
429
  word = "";
396
430
  continue;
397
431
  }
398
- const close = braceExpansionEnd(text, k);
432
+ const close = braceExpansionEnd(text, k, false, level);
399
433
  if (close === undefined)
400
434
  return "unterminated";
401
435
  if (UNREADABLE_EXPANSION_TEXT.test(text.slice(k + 1, close - 1)))
@@ -490,7 +524,16 @@ function arithmeticWalk(text, open, inDouble, level) {
490
524
  return undefined;
491
525
  return past(text, read.innerClose + 1) === read.end - 1 ? { end: read.end, heredoc: false } : undefined;
492
526
  }
493
- function braceExpansionEnd(text, open) {
527
+ function braceExpansionEnd(text, open, inDouble, level, parsed = true) {
528
+ const before = braceQuoteClose(text, open);
529
+ if (before !== undefined && !/[$`<>]/.test(text.slice(open + 1, before)))
530
+ return before;
531
+ const walked = braceWalk(text, open, inDouble, level, parsed);
532
+ if (walked === undefined || walked === before || !parsed)
533
+ return walked ?? before;
534
+ return passesFor(text, open).every((pass) => { const e = bracePassEnd(text, open, pass); return e === walked || e === "unterminated"; }) ? walked : before;
535
+ }
536
+ function braceQuoteClose(text, open) {
494
537
  let quoted;
495
538
  for (let j = open + 1; j < text.length; j++) {
496
539
  const c = text[j];
@@ -514,6 +557,98 @@ function braceExpansionEnd(text, open) {
514
557
  }
515
558
  return undefined;
516
559
  }
560
+ function braceWalk(text, open, inDouble, level, parsed) {
561
+ if (level > BODY_NESTING_MAX)
562
+ return undefined;
563
+ for (let j = open + 1; j < text.length; j++) {
564
+ const c = text[j];
565
+ if (c === "\\") {
566
+ j++;
567
+ continue;
568
+ }
569
+ if (c === "}")
570
+ return j + 1;
571
+ if (c === "'") {
572
+ const close = text.indexOf("'", j + 1);
573
+ if (close < 0)
574
+ return undefined;
575
+ j = close;
576
+ continue;
577
+ }
578
+ const k = past(text, j + 1);
579
+ if (c === "$" && text[k] === "'") {
580
+ const close = ansiCQuoteClose(text, k);
581
+ if (close === undefined)
582
+ return undefined;
583
+ j = close;
584
+ continue;
585
+ }
586
+ if (c === '"' || (c === "$" && text[k] === '"')) {
587
+ const close = doubleQuotedSpanClose(text, c === '"' ? j : k, level, parsed);
588
+ if (close === undefined)
589
+ return undefined;
590
+ j = close;
591
+ continue;
592
+ }
593
+ if ((c === "<" || c === ">") && text[k] === "(") {
594
+ const close = substitutionBodyEnd(text, k, inDouble, level + 1, parsed ? "process" : "document");
595
+ if (typeof close !== "object" || !("end" in close))
596
+ return undefined;
597
+ j = close.end - 1;
598
+ continue;
599
+ }
600
+ const last = expansionLast(text, j, inDouble, level, parsed);
601
+ if (last === undefined)
602
+ return undefined;
603
+ if (last !== null)
604
+ j = last;
605
+ }
606
+ return undefined;
607
+ }
608
+ function doubleQuotedSpanClose(text, open, level, parsed) {
609
+ for (let j = open + 1; j < text.length; j++) {
610
+ const c = text[j];
611
+ if (c === "\\") {
612
+ j++;
613
+ continue;
614
+ }
615
+ if (c === '"')
616
+ return j;
617
+ const last = expansionLast(text, j, true, level, parsed);
618
+ if (last === undefined)
619
+ return undefined;
620
+ if (last !== null)
621
+ j = last;
622
+ }
623
+ return undefined;
624
+ }
625
+ function expansionLast(text, j, inDouble, level, parsed) {
626
+ const c = text[j];
627
+ if (c === "`")
628
+ return backtickBody(text, j, inDouble)?.close;
629
+ if (c !== "$")
630
+ return null;
631
+ const k = past(text, j + 1);
632
+ const n = text[k];
633
+ if (n === "$")
634
+ return k;
635
+ if (n === "(") {
636
+ const close = substitutionBodyEnd(text, k, inDouble, level + 1, parsed ? "command" : "document");
637
+ return typeof close === "object" && "end" in close ? close.end - 1 : undefined;
638
+ }
639
+ if (n === "[") {
640
+ const end = bracketExpansionEnd(text, k);
641
+ return end === undefined ? undefined : end - 1;
642
+ }
643
+ if (n !== "{")
644
+ return null;
645
+ if (opensCurrentShellSubstitution(text, k)) {
646
+ const run = currentShellSubstitutionEnd(text, k, level + 1);
647
+ return typeof run === "object" && "end" in run ? run.end - 1 : undefined;
648
+ }
649
+ const end = braceWalk(text, k, inDouble, level + 1, parsed);
650
+ return end === undefined ? undefined : end - 1;
651
+ }
517
652
  function opensCurrentShellSubstitution(text, open) {
518
653
  const after = text[open + 1];
519
654
  return after === "|" || after === " " || after === "\t" || after === "\n";
@@ -596,7 +731,7 @@ function matchedParen(text, open, level) {
596
731
  j = run.end - 1;
597
732
  continue;
598
733
  }
599
- const close = braceExpansionEnd(text, j + 1);
734
+ const close = braceExpansionEnd(text, j + 1, false, level);
600
735
  if (close === undefined)
601
736
  return undefined;
602
737
  j = close - 1;
@@ -614,6 +749,15 @@ const PARAMETER = String.raw `(?:[A-Za-z_][A-Za-z0-9_]*|\d+|[*@#?$!-])`;
614
749
  const PARAMETER_HEAD = new RegExp(String.raw `^[#!]?${PARAMETER}`);
615
750
  const EXPANSION_NAME = new RegExp(String.raw `^[#!]?(${PARAMETER})`);
616
751
  const BARE_PARAMETER = /^(?:[A-Za-z_][A-Za-z0-9_]*|\d|[@*#?$!0-])/;
752
+ const LOCALE_NAME_HEAD = /^[^\x00-\x7f](?:[^\x00-\x7f]|\w)*/;
753
+ function bareParameterAt(text, from) {
754
+ const window = text.slice(from, from + 64);
755
+ const head = BARE_PARAMETER.exec(window)?.[0];
756
+ if (head !== undefined)
757
+ return { head, name: head };
758
+ const local = LOCALE_NAME_HEAD.exec(window)?.[0];
759
+ return local === undefined ? undefined : { head: local };
760
+ }
617
761
  const IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;
618
762
  const PER_ELEMENT_BODY = /^!?@|\[@\]|^![^}]*@$/;
619
763
  const NAME_HEAD = /^[A-Za-z_][A-Za-z0-9_]*/;
@@ -642,58 +786,56 @@ export function expansionsInHeredocBody(body) {
642
786
  }
643
787
  function expansionsInBody(body) {
644
788
  const out = [];
645
- const push = (x, start, last) => (out.push({ ...x, span: [start, last + 1] }), last);
646
789
  for (let i = 0; i < body.length; i++) {
647
- const ch = body[i];
648
- if (ch === "\\") {
790
+ if (body[i] === "\\") {
649
791
  i++;
650
792
  continue;
651
793
  }
652
- if (ch === "`") {
653
- const tick = backtickBody(body, i);
654
- if (tick === undefined)
655
- return undefined;
656
- i = push({ expansion: { kind: "command", yields: "one", body: tick.body } }, i, tick.close);
657
- continue;
658
- }
659
- if (ch !== "$")
794
+ const at = bodyExpansionAt(body, i);
795
+ if (at === undefined)
796
+ return undefined;
797
+ if (at === null)
660
798
  continue;
661
- const open = body[i + 1];
662
- if (open === "(") {
663
- const close = substitutionBodyEnd(body, i + 1, true, 0, "document");
664
- if (typeof close !== "object" || !("end" in close))
665
- return undefined;
666
- i = push({ expansion: { kind: close.arithmetic ? "arithmetic" : "command", yields: "one", body: body.slice(i + 2, close.end - 1) } }, i, close.end - 1);
667
- }
668
- else if (open === "[") {
669
- const end = bracketExpansionEnd(body, i + 1);
670
- if (end === undefined)
671
- return undefined;
672
- i = push({ expansion: { kind: "arithmetic", yields: "one", body: body.slice(i + 2, end - 1) } }, i, end - 1);
673
- }
674
- else if (open === "{") {
675
- if (opensCurrentShellSubstitution(body, i + 1)) {
676
- const run = currentShellSubstitutionEnd(body, i + 1, 1);
677
- if (typeof run !== "object" || !("end" in run))
678
- return undefined;
679
- i = push({ expansion: { kind: "command", yields: "one", body: body.slice(run.from, run.end - 1) }, currentShell: true }, i, run.end - 1);
680
- continue;
681
- }
682
- const end = braceExpansionEnd(body, i + 1);
683
- if (end === undefined)
799
+ out.push({ ...at.found, span: [i, at.last + 1] });
800
+ i = at.last;
801
+ }
802
+ return out;
803
+ }
804
+ function bodyExpansionAt(body, i) {
805
+ const ch = body[i];
806
+ if (ch === "`") {
807
+ const tick = backtickBody(body, i);
808
+ return tick === undefined ? undefined : { found: { expansion: { kind: "command", yields: "one", body: tick.body } }, last: tick.close };
809
+ }
810
+ if (ch !== "$")
811
+ return null;
812
+ const open = body[i + 1];
813
+ if (open === "(") {
814
+ const close = substitutionBodyEnd(body, i + 1, true, 0, "document");
815
+ if (typeof close !== "object" || !("end" in close))
816
+ return undefined;
817
+ return { found: { expansion: { kind: close.arithmetic ? "arithmetic" : "command", yields: "one", body: body.slice(i + 2, close.end - 1) } }, last: close.end - 1 };
818
+ }
819
+ if (open === "[") {
820
+ const end = bracketExpansionEnd(body, i + 1);
821
+ return end === undefined ? undefined : { found: { expansion: { kind: "arithmetic", yields: "one", body: body.slice(i + 2, end - 1) } }, last: end - 1 };
822
+ }
823
+ if (open === "{") {
824
+ if (opensCurrentShellSubstitution(body, i + 1)) {
825
+ const run = currentShellSubstitutionEnd(body, i + 1, 1);
826
+ if (typeof run !== "object" || !("end" in run))
684
827
  return undefined;
685
- const text = body.slice(i + 2, end - 1);
686
- const name = EXPANSION_NAME.exec(text)?.[1];
687
- i = push({ expansion: { kind: "parameter", yields: "one", body: text, ...(name !== undefined ? { name } : {}) }, ...(UNREADABLE_EXPANSION_TEXT.test(text) ? { unresolved: "parameter_text" } : {}) }, i, end - 1);
688
- }
689
- else {
690
- const head = BARE_PARAMETER.exec(body.slice(i + 1, i + 65))?.[0];
691
- if (head === undefined)
692
- continue;
693
- i = push({ expansion: { kind: "parameter", yields: "one", name: head } }, i, i + head.length);
828
+ return { found: { expansion: { kind: "command", yields: "one", body: body.slice(run.from, run.end - 1) }, currentShell: true }, last: run.end - 1 };
694
829
  }
830
+ const end = braceExpansionEnd(body, i + 1, true, 0, false);
831
+ if (end === undefined)
832
+ return undefined;
833
+ const text = body.slice(i + 2, end - 1);
834
+ const name = EXPANSION_NAME.exec(text)?.[1];
835
+ return { found: { expansion: { kind: "parameter", yields: "one", body: text, ...(name !== undefined ? { name } : {}) }, ...(UNREADABLE_EXPANSION_TEXT.test(text) ? { unresolved: "parameter_text" } : {}) }, last: end - 1 };
695
836
  }
696
- return out;
837
+ const bare = bareParameterAt(body, i + 1);
838
+ return bare === undefined ? null : { found: { expansion: { kind: "parameter", yields: "one", ...(bare.name !== undefined ? { name: bare.name } : {}) } }, last: i + bare.head.length };
697
839
  }
698
840
  export function substitutionsInHeredocBody(body) {
699
841
  const all = expansionsInHeredocBody(body);
@@ -710,12 +852,16 @@ export function heredocRunsOf(body) {
710
852
  export function heredocBodyHidesRun(body) {
711
853
  return runsAny(heredocRunsOf(body));
712
854
  }
713
- const SUBSTITUTION_OPENER = /\$\(|`|<\(|>\(|\$\[/;
855
+ const SUBSTITUTION_OPENER = /\$\(|`|<\(|>\(|\$\[|\$\{[ \t\n|]/;
714
856
  export function parameterArithmeticTexts(body) {
857
+ const { subscript, offset } = parameterBodyParts(body);
858
+ return { ...(subscript !== undefined ? { subscript } : {}), ...(offset !== undefined ? { offset } : {}) };
859
+ }
860
+ function parameterBodyParts(body) {
715
861
  const text = body.replace(/\\\n/g, "");
716
862
  const head = PARAMETER_HEAD.exec(text);
717
863
  if (head === null)
718
- return {};
864
+ return { operand: text };
719
865
  let rest = text.slice(head[0].length);
720
866
  let subscript;
721
867
  if (rest.startsWith("[")) {
@@ -724,7 +870,57 @@ export function parameterArithmeticTexts(body) {
724
870
  rest = close < 0 ? "" : rest.slice(close + 1);
725
871
  }
726
872
  const offset = /^:(?![-=+?])(.*)$/s.exec(rest)?.[1];
727
- return { ...(subscript !== undefined ? { subscript } : {}), ...(offset !== undefined ? { offset } : {}) };
873
+ return { ...(subscript !== undefined ? { subscript } : {}), ...(offset !== undefined ? { offset } : rest !== "" ? { operand: rest } : {}) };
874
+ }
875
+ export function parameterOperandLines(body, quoted) {
876
+ const operand = parameterBodyParts(body).operand;
877
+ return operand === undefined || !/[$`<>]/.test(operand) ? [] : whileReading(operand, () => operandLines(operand, quoted, PATTERN_OPERAND.test(operand)));
878
+ }
879
+ const PATTERN_OPERAND = /^[#%/^,]/;
880
+ function operandLines(text, quoted, pattern) {
881
+ const lines = [];
882
+ let inDouble = false;
883
+ for (let k = 0; k < text.length; k++) {
884
+ const ch = text[k];
885
+ if (ch === "\\") {
886
+ k++;
887
+ continue;
888
+ }
889
+ const bare = !quoted && !inDouble;
890
+ if (bare && (ch === "'" || (ch === "$" && text[k + 1] === "'"))) {
891
+ const close = ch === "'" ? text.indexOf("'", k + 1) : ansiCQuoteClose(text, k + 1);
892
+ if (close === undefined || close < 0)
893
+ break;
894
+ k = close;
895
+ continue;
896
+ }
897
+ if (!quoted && ch === '"') {
898
+ inDouble = !inDouble;
899
+ continue;
900
+ }
901
+ if ((bare || (quoted && pattern)) && (ch === "<" || ch === ">") && text[k + 1] === "(") {
902
+ const close = substitutionBodyEnd(text, k + 1, false, 0, "process");
903
+ if (typeof close !== "object" || !("end" in close))
904
+ break;
905
+ lines.push(text.slice(k + 2, close.end - 1));
906
+ k = close.end - 1;
907
+ continue;
908
+ }
909
+ const at = bodyExpansionAt(text, k);
910
+ if (at === undefined)
911
+ break;
912
+ if (at === null)
913
+ continue;
914
+ const e = at.found.expansion;
915
+ if (e.kind === "command")
916
+ lines.push(e.body ?? "");
917
+ else if (e.kind === "arithmetic")
918
+ lines.push(...operandLines(e.body ?? "", true, false));
919
+ else if (e.kind === "parameter" && e.body !== undefined)
920
+ lines.push(...operandLines(e.body, quoted || inDouble, PATTERN_OPERAND.test(parameterBodyParts(e.body).operand ?? "")));
921
+ k = at.last;
922
+ }
923
+ return lines;
728
924
  }
729
925
  function bodyEvaluatesText(body) {
730
926
  const { subscript, offset } = parameterArithmeticTexts(body);
@@ -746,8 +942,8 @@ function joinRuns(parts) {
746
942
  return { lines: parts.flatMap((p) => p.lines), hidden: parts.some((p) => p.hidden) };
747
943
  }
748
944
  const runsAny = (r) => r.hidden || r.lines.length > 0;
749
- export function expansionRunsOf(e) {
750
- const inner = joinRuns((e.inner ?? []).map(expansionRunsOf));
945
+ export function expansionRunsOf(e, quoted = true) {
946
+ const inner = joinRuns((e.inner ?? []).map((x) => expansionRunsOf(x, quoted)));
751
947
  if (e.kind === "command" || e.kind === "process")
752
948
  return joinRuns([e.body === undefined ? HIDDEN_RUNS : { lines: [e.body], hidden: false }, inner]);
753
949
  const body = e.body?.replace(/\\\n/g, "");
@@ -756,7 +952,8 @@ export function expansionRunsOf(e) {
756
952
  if (e.kind === "arithmetic")
757
953
  return joinRuns([arithmeticTextRunsOf(body), inner]);
758
954
  const hidden = SUBSTITUTION_OPENER.test(body) || (e.kind === "parameter" && (bodyEvaluatesText(body) || parameterRereadsValue(body))) || nestedBodyEvaluatesText(body);
759
- return hidden ? joinRuns([HIDDEN_RUNS, inner]) : inner;
955
+ const operand = e.kind === "parameter" && hidden ? parameterOperandLines(body, quoted) : undefined;
956
+ return hidden ? joinRuns([HIDDEN_RUNS, { lines: operand ?? [], hidden: false }, inner]) : inner;
760
957
  }
761
958
  export function arithmeticTextRunsOf(text) {
762
959
  if (arithmeticTextIsPlain(text))
@@ -804,7 +1001,7 @@ export function subscriptHidesRun(w) {
804
1001
  return w.assignment?.subscript !== undefined && !arithmeticTextIsPlain(w.assignment.subscript.raw);
805
1002
  }
806
1003
  export function wordRunsOf(w) {
807
- return joinRuns(assignedWords(w).flatMap((e) => [e.assignment?.form === "compound" || subscriptHidesRun(e) ? HIDDEN_RUNS : NO_RUNS, ...e.expansions.flatMap((p) => (p.expansion !== undefined ? [expansionRunsOf(p.expansion)] : []))]));
1004
+ return joinRuns(assignedWords(w).flatMap((e) => [e.assignment?.form === "compound" || subscriptHidesRun(e) ? HIDDEN_RUNS : NO_RUNS, ...e.expansions.flatMap((p) => (p.expansion !== undefined ? [expansionRunsOf(p.expansion, p.quotedBy !== undefined)] : []))]));
808
1005
  }
809
1006
  export function wordHidesRun(w) {
810
1007
  return runsAny(wordRunsOf(w));
@@ -1275,10 +1472,10 @@ class WordBuilder {
1275
1472
  };
1276
1473
  }
1277
1474
  }
1278
- function scan(source, connectorsAreText, segments, level, list) {
1279
- return whileReading(source, () => scanText(source, connectorsAreText, segments, level, list));
1475
+ function scan(source, connectorsAreText, segments, level, list, resumed) {
1476
+ return whileReading(source, () => scanText(source, connectorsAreText, segments, level, list, resumed));
1280
1477
  }
1281
- function scanText(source, connectorsAreText, segments, level, list) {
1478
+ function scanText(source, connectorsAreText, segments, level, list, resumed) {
1282
1479
  const split = !connectorsAreText;
1283
1480
  const current = () => segments[segments.length - 1];
1284
1481
  const pendingHeredocs = [];
@@ -1308,6 +1505,21 @@ function scanText(source, connectorsAreText, segments, level, list) {
1308
1505
  current().substitutions.push(body);
1309
1506
  };
1310
1507
  const resetPosition = () => { acceptable = true; commandWord = undefined; afterLeadingRedirect = false; assignmentSeen = false; subscriptsClosed = false; timeGrammar = undefined; definitionName = false; coprocWord = undefined; };
1508
+ const finished = () => {
1509
+ const out = segments.map((s) => segmentView(source, s));
1510
+ return { command: { segments: out, substitutions: out.flatMap((s) => s.substitutions) } };
1511
+ };
1512
+ const metEarlier = () => {
1513
+ if (resumed === undefined || pendingHeredocs.length > 0 || arithParens > 0)
1514
+ return false;
1515
+ const at = current().start;
1516
+ resumed.fresh.add(at);
1517
+ if (!resumed.met.has(at))
1518
+ return false;
1519
+ resumed.metAt = at;
1520
+ segments.pop();
1521
+ return true;
1522
+ };
1311
1523
  const endWord = () => {
1312
1524
  if (!w.started)
1313
1525
  return undefined;
@@ -1411,12 +1623,23 @@ function scanText(source, connectorsAreText, segments, level, list) {
1411
1623
  if (read === "ambiguous")
1412
1624
  return fail("undelimited_substitution_heredoc", from);
1413
1625
  if (typeof read !== "object")
1414
- return fail("unterminated_heredoc", from);
1626
+ return unterminatedHeredocs(from, true);
1415
1627
  pendingHeredocs.forEach((h, k) => { h.token.body = read.bodies[k]; });
1416
1628
  pendingHeredocs.length = 0;
1417
1629
  return read.resume;
1418
1630
  };
1419
- for (let i = list?.from ?? 0; i < source.length; i++) {
1631
+ const unterminatedHeredocs = (from, lineBroken) => {
1632
+ const read = list === undefined ? heredocBodies(source, from, pendingHeredocs, undefined, true) : undefined;
1633
+ if (typeof read === "object") {
1634
+ pendingHeredocs.forEach((h, k) => { h.token.body = read.bodies[k]; });
1635
+ if (lineBroken)
1636
+ current().start = source.length;
1637
+ else
1638
+ segments.push({ start: source.length, end: source.length, connector: "\n", tokens: [], substitutions: [] });
1639
+ }
1640
+ return fail("unterminated_heredoc", from);
1641
+ };
1642
+ for (let i = list?.from ?? resumed?.from ?? 0; i < source.length; i++) {
1420
1643
  if (!inSingle) {
1421
1644
  const j = past(source, i);
1422
1645
  if (j >= source.length)
@@ -1577,7 +1800,7 @@ function scanText(source, connectorsAreText, segments, level, list) {
1577
1800
  i = run.end - 1;
1578
1801
  continue;
1579
1802
  }
1580
- const end = braceExpansionEnd(source, n1);
1803
+ const end = braceExpansionEnd(source, n1, inDouble, level);
1581
1804
  if (end === undefined)
1582
1805
  return fail("unterminated_parameter_expansion", i);
1583
1806
  const body = source.slice(n1 + 1, end - 1);
@@ -1590,11 +1813,11 @@ function scanText(source, connectorsAreText, segments, level, list) {
1590
1813
  continue;
1591
1814
  }
1592
1815
  if (ch === "$") {
1593
- const head = BARE_PARAMETER.exec(source.slice(n1, n1 + 64))?.[0];
1594
- if (head !== undefined) {
1595
- const text = source.slice(i, n1 + head.length);
1596
- w.opaque(text, text, quotedBy(), i, n1 + head.length, { kind: "parameter", yields: inDouble && head !== "@" ? "one" : "many", name: head });
1597
- i = n1 + head.length - 1;
1816
+ const bare = bareParameterAt(source, n1);
1817
+ if (bare !== undefined) {
1818
+ const text = source.slice(i, n1 + bare.head.length);
1819
+ w.opaque(text, text, quotedBy(), i, n1 + bare.head.length, { kind: "parameter", yields: inDouble && bare.head !== "@" ? "one" : "many", ...(bare.name !== undefined ? { name: bare.name } : {}) });
1820
+ i = n1 + bare.head.length - 1;
1598
1821
  continue;
1599
1822
  }
1600
1823
  }
@@ -1658,6 +1881,8 @@ function scanText(source, connectorsAreText, segments, level, list) {
1658
1881
  current().start = resume;
1659
1882
  i = resume - 1;
1660
1883
  }
1884
+ if (metEarlier())
1885
+ return finished();
1661
1886
  continue;
1662
1887
  }
1663
1888
  if (ch === "(" || ch === ")") {
@@ -1752,6 +1977,9 @@ function scanText(source, connectorsAreText, segments, level, list) {
1752
1977
  }
1753
1978
  else if (BLANK.test(ch)) {
1754
1979
  refusal = endWord();
1980
+ if (refusal !== undefined)
1981
+ return refusal;
1982
+ continue;
1755
1983
  }
1756
1984
  else {
1757
1985
  take(ch, i);
@@ -1759,6 +1987,8 @@ function scanText(source, connectorsAreText, segments, level, list) {
1759
1987
  }
1760
1988
  if (refusal !== undefined)
1761
1989
  return refusal;
1990
+ if (metEarlier())
1991
+ return finished();
1762
1992
  continue;
1763
1993
  }
1764
1994
  if (BLANK.test(ch)) {
@@ -1777,9 +2007,8 @@ function scanText(source, connectorsAreText, segments, level, list) {
1777
2007
  if (tail !== undefined)
1778
2008
  return tail;
1779
2009
  if (pendingHeredocs.length > 0)
1780
- return fail("unterminated_heredoc", source.length);
1781
- const out = segments.map((s) => segmentView(source, s));
1782
- return { command: { segments: out, substitutions: out.flatMap((s) => s.substitutions) } };
2010
+ return unterminatedHeredocs(source.length, false);
2011
+ return finished();
1783
2012
  }
1784
2013
  const firstSegment = (source) => ({ start: 0, end: source.length, connector: "start", tokens: [], substitutions: [] });
1785
2014
  const segmentView = (source, s) => ({
@@ -1795,6 +2024,41 @@ export function lexShell(source) {
1795
2024
  const pass = scan(source, false, segments, 0);
1796
2025
  return "parseFailure" in pass ? { ...pass, readBefore: segments.map((s) => segmentView(source, s)) } : pass.command;
1797
2026
  }
2027
+ const STATEMENT_BOUNDARY = new Set(["\n", ";", "&", "|"]);
2028
+ export const READ_PAST_STOP_BUDGET = 4 * 1024 * 1024;
2029
+ export function segmentsPastStop(source, failure) {
2030
+ let boundary = failure.parseFailure.at;
2031
+ while (boundary < source.length && !STATEMENT_BOUNDARY.has(source[boundary]))
2032
+ boundary++;
2033
+ if (boundary === source.length)
2034
+ return [];
2035
+ return whileReading(source, () => {
2036
+ const read = new Map();
2037
+ const starts = new Set();
2038
+ const walked = [firstSegment(source)];
2039
+ scan(source, false, walked, 0);
2040
+ let budget = READ_PAST_STOP_BUDGET;
2041
+ for (let j = Math.max(failure.parseFailure.at, walked.at(-1).start); j < source.length && budget > 0; j++) {
2042
+ if (!STATEMENT_BOUNDARY.has(source[j]) || starts.has(j + 1))
2043
+ continue;
2044
+ const from = j + 1;
2045
+ const segments = [{ ...firstSegment(source), start: from }];
2046
+ const reading = { from, met: starts, fresh: new Set([from]) };
2047
+ const pass = scan(source, false, segments, 0, undefined, reading);
2048
+ budget -= (reading.metAt ?? source.length) - from;
2049
+ const complete = "parseFailure" in pass ? segments.slice(0, -1) : segments;
2050
+ complete.forEach((s, k) => {
2051
+ if (reading.fresh.has(s.start))
2052
+ starts.add(s.start);
2053
+ const view = segmentView(source, s);
2054
+ const key = `${s.start}:${view.text}`;
2055
+ if (!read.has(key))
2056
+ read.set(key, { at: s.start, segment: { ...view, ...(k === 0 ? { connector: "\n" } : {}), spliced: true } });
2057
+ });
2058
+ }
2059
+ return [...read.values()].sort((a, b) => a.at - b.at || a.segment.text.length - b.segment.text.length).map((r) => r.segment);
2060
+ });
2061
+ }
1798
2062
  export function lexShellText(source) {
1799
2063
  const lexed = lexShell(source);
1800
2064
  if ("parseFailure" in lexed)
@@ -81,12 +81,8 @@ export function readProgramPosition(words) {
81
81
  return undefined;
82
82
  if (k === "glob")
83
83
  glob = true;
84
- if (p.literal !== undefined) {
85
- if (p.quotedBy === undefined && /[{}$`]/.test(p.literal))
86
- return undefined;
87
- if (p.quotedBy === '"' && /[$`]/.test(p.literal))
88
- return undefined;
89
- }
84
+ if (p.literal !== undefined && p.quotedBy === undefined && /[{}]/.test(p.literal))
85
+ return undefined;
90
86
  }
91
87
  return glob ? { kind: "wildcard_program_word", word: folded[idx], at: idx } : undefined;
92
88
  };