@sema-agent/core 7.61.0 → 7.63.0

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 (298) hide show
  1. package/CHANGELOG.md +840 -0
  2. package/dist/agents/cascade.d.ts +11 -3
  3. package/dist/agents/cascade.js +1 -1
  4. package/dist/agents/peer-held-queue.d.ts +27 -4
  5. package/dist/agents/peer-held-queue.js +81 -36
  6. package/dist/agents/peer-session-drain.js +13 -3
  7. package/dist/agents/subagent-transcript-placement.d.ts +23 -0
  8. package/dist/agents/subagent-transcript-placement.js +14 -0
  9. package/dist/agents/subagent.d.ts +6 -1
  10. package/dist/agents/subagent.js +143 -58
  11. package/dist/agents/suspend-guard.d.ts +38 -16
  12. package/dist/agents/suspend-guard.js +9 -4
  13. package/dist/agents/verify.js +4 -3
  14. package/dist/brain/route-adjudicator.js +9 -4
  15. package/dist/core/approval-object.js +1 -1
  16. package/dist/core/arg-summary.js +1 -1
  17. package/dist/core/ask-question.d.ts +2 -2
  18. package/dist/core/canonical-json.d.ts +23 -13
  19. package/dist/core/canonical-json.js +4 -4
  20. package/dist/core/checkpoint-features.d.ts +722 -0
  21. package/dist/core/checkpoint-features.js +98 -0
  22. package/dist/core/checkpoint-queries.d.ts +47 -4
  23. package/dist/core/checkpoint-queries.js +36 -16
  24. package/dist/core/checkpoint-requirements.d.ts +12 -5
  25. package/dist/core/checkpoint-requirements.js +11 -5
  26. package/dist/core/checkpoint-row-verdict.d.ts +110 -0
  27. package/dist/core/checkpoint-row-verdict.js +245 -0
  28. package/dist/core/checkpoint-store.d.ts +76 -753
  29. package/dist/core/checkpoint-store.js +83 -144
  30. package/dist/core/compliance.d.ts +6 -2
  31. package/dist/core/compliance.js +3 -3
  32. package/dist/core/engine-attestation.d.ts +1 -1
  33. package/dist/core/engine-notice.d.ts +38 -7
  34. package/dist/core/engine-notice.js +9 -2
  35. package/dist/core/gate-fold.js +2 -2
  36. package/dist/core/git-worktree-env.d.ts +62 -3
  37. package/dist/core/git-worktree-env.js +91 -30
  38. package/dist/core/governance-codes.d.ts +9 -3
  39. package/dist/core/governance-codes.js +16 -3
  40. package/dist/core/hooks.d.ts +13 -0
  41. package/dist/core/human-input-projection.d.ts +1 -0
  42. package/dist/core/human-input-projection.js +3 -0
  43. package/dist/core/image-admission.d.ts +1 -1
  44. package/dist/core/locked-config.d.ts +2 -2
  45. package/dist/core/mcp-host-seat.js +6 -1
  46. package/dist/core/memory-engine/consolidation-driver.d.ts +33 -18
  47. package/dist/core/memory-engine/consolidation-driver.js +99 -98
  48. package/dist/core/memory-engine/consolidation.d.ts +73 -10
  49. package/dist/core/memory-engine/consolidation.js +167 -11
  50. package/dist/core/memory-engine/data-plane.d.ts +8 -4
  51. package/dist/core/memory-engine/data-plane.js +11 -6
  52. package/dist/core/memory-engine/delegation-settlement.d.ts +37 -35
  53. package/dist/core/memory-engine/delegation-settlement.js +53 -59
  54. package/dist/core/memory-engine/dual-root.d.ts +12 -6
  55. package/dist/core/memory-engine/dual-root.js +9 -5
  56. package/dist/core/memory-engine/engine.d.ts +98 -35
  57. package/dist/core/memory-engine/engine.js +477 -380
  58. package/dist/core/memory-engine/entry-scan.d.ts +8 -0
  59. package/dist/core/memory-engine/entry-scan.js +3 -0
  60. package/dist/core/memory-engine/file-backend.d.ts +29 -3
  61. package/dist/core/memory-engine/file-backend.js +66 -35
  62. package/dist/core/memory-engine/index.d.ts +7 -7
  63. package/dist/core/memory-engine/index.js +6 -6
  64. package/dist/core/memory-engine/landing-admission.d.ts +5 -3
  65. package/dist/core/memory-engine/landing-admission.js +3 -3
  66. package/dist/core/memory-engine/layout.d.ts +121 -45
  67. package/dist/core/memory-engine/layout.js +252 -102
  68. package/dist/core/memory-engine/memory-backend-contract.d.ts +19 -1
  69. package/dist/core/memory-engine/memory-backend-contract.js +320 -0
  70. package/dist/core/memory-engine/memory-deployment.d.ts +31 -8
  71. package/dist/core/memory-engine/memory-deployment.js +26 -12
  72. package/dist/core/memory-engine/scope-contract.d.ts +52 -11
  73. package/dist/core/memory-engine/scope-contract.js +68 -19
  74. package/dist/core/memory-engine/session-write-home.d.ts +3 -1
  75. package/dist/core/memory-engine/session-write-home.js +1 -1
  76. package/dist/core/memory-engine/sync-client.d.ts +3 -1
  77. package/dist/core/memory-engine/sync-client.js +7 -0
  78. package/dist/core/memory-engine/sync-report.d.ts +3 -2
  79. package/dist/core/memory-engine/sync-report.js +6 -2
  80. package/dist/core/memory-engine/tools.d.ts +2 -2
  81. package/dist/core/memory-engine/tools.js +10 -10
  82. package/dist/core/memory-engine/types.d.ts +118 -19
  83. package/dist/core/memory-engine/types.js +1 -0
  84. package/dist/core/memory-vector.d.ts +1 -1
  85. package/dist/core/memory.d.ts +10 -10
  86. package/dist/core/memory.js +15 -6
  87. package/dist/core/mode-policy.js +1 -1
  88. package/dist/core/mods/classic-events.d.ts +9 -1
  89. package/dist/core/mods/dollar.d.ts +32 -1
  90. package/dist/core/mods/dollar.js +13 -1
  91. package/dist/core/mods/kernel/events.d.ts +11 -3
  92. package/dist/core/mods/kernel/events.js +13 -10
  93. package/dist/core/mods/lifecycle-stations.d.ts +36 -4
  94. package/dist/core/mods/lifecycle-stations.js +44 -9
  95. package/dist/core/mods/session-append.d.ts +4 -1
  96. package/dist/core/mods/session-append.js +7 -0
  97. package/dist/core/mods/session-receive.js +12 -15
  98. package/dist/core/mods/stations.d.ts +70 -6
  99. package/dist/core/mods/stations.js +144 -1
  100. package/dist/core/own-property-receipt.d.ts +7 -0
  101. package/dist/core/own-property-receipt.js +3 -0
  102. package/dist/core/park-selfcheck.js +2 -2
  103. package/dist/core/pause-registry.d.ts +2 -0
  104. package/dist/core/pause-registry.js +3 -0
  105. package/dist/core/permission-rule-consent.d.ts +10 -3
  106. package/dist/core/permission-rule-consent.js +37 -24
  107. package/dist/core/permission-rule-store.d.ts +35 -1
  108. package/dist/core/permission-rule-store.js +27 -8
  109. package/dist/core/read-only-shell.js +3 -2
  110. package/dist/core/read-root-candidate.js +1 -1
  111. package/dist/core/redaction-marks.d.ts +8 -0
  112. package/dist/core/redaction-marks.js +6 -0
  113. package/dist/core/remote-env.d.ts +12 -1
  114. package/dist/core/remote-env.js +3 -0
  115. package/dist/core/resume-startup-cause.d.ts +5 -4
  116. package/dist/core/runner/child-tree.d.ts +29 -3
  117. package/dist/core/runner/child-tree.js +39 -16
  118. package/dist/core/runner/compact-session.js +5 -2
  119. package/dist/core/runner/contracts.d.ts +36 -17
  120. package/dist/core/runner/delegation-owner.d.ts +43 -0
  121. package/dist/core/runner/delegation-owner.js +59 -0
  122. package/dist/core/runner/gate-exit.js +2 -2
  123. package/dist/core/runner/inherited-ask-grants.js +1 -1
  124. package/dist/core/runner/lineage-reroot-view.d.ts +56 -0
  125. package/dist/core/runner/lineage-reroot-view.js +108 -0
  126. package/dist/core/runner/memory-consolidation-driver.d.ts +6 -3
  127. package/dist/core/runner/memory-consolidation-driver.js +4 -4
  128. package/dist/core/runner/memory-consolidation-write-judge.d.ts +2 -1
  129. package/dist/core/runner/memory-consolidation-write-judge.js +3 -2
  130. package/dist/core/runner/permission-rule-lanes.d.ts +2 -1
  131. package/dist/core/runner/permission-rule-lanes.js +2 -2
  132. package/dist/core/runner/person-path-rows.d.ts +23 -3
  133. package/dist/core/runner/person-path-rows.js +31 -4
  134. package/dist/core/runner/placement-fence.d.ts +12 -0
  135. package/dist/core/runner/placement-fence.js +20 -6
  136. package/dist/core/runner/prepare-acquire-reconcile.d.ts +4 -1
  137. package/dist/core/runner/prepare-acquire-reconcile.js +7 -2
  138. package/dist/core/runner/prepare-ask-lane.js +2 -2
  139. package/dist/core/runner/prepare-caps-and-workflow.d.ts +5 -1
  140. package/dist/core/runner/prepare-caps-and-workflow.js +6 -3
  141. package/dist/core/runner/prepare-config-doors.d.ts +6 -6
  142. package/dist/core/runner/prepare-config-doors.js +3 -4
  143. package/dist/core/runner/prepare-execution-env.js +17 -6
  144. package/dist/core/runner/prepare-hands-readface.d.ts +3 -2
  145. package/dist/core/runner/prepare-hands-readface.js +32 -10
  146. package/dist/core/runner/prepare-inherited-gate.d.ts +12 -8
  147. package/dist/core/runner/prepare-inherited-gate.js +14 -6
  148. package/dist/core/runner/prepare-memory-engine-session.d.ts +2 -4
  149. package/dist/core/runner/prepare-memory-engine-session.js +4 -4
  150. package/dist/core/runner/prepare-memory.d.ts +3 -2
  151. package/dist/core/runner/prepare-memory.js +28 -20
  152. package/dist/core/runner/prepare-park-ask.js +4 -3
  153. package/dist/core/runner/prepare-task.js +22 -16
  154. package/dist/core/runner/prepare-workspace-restore.js +3 -6
  155. package/dist/core/runner/resume-admission.d.ts +3 -3
  156. package/dist/core/runner/resume-admission.js +6 -3
  157. package/dist/core/runner/resume-apply.d.ts +4 -0
  158. package/dist/core/runner/resume-apply.js +11 -4
  159. package/dist/core/runner/resume-checkpoint-screen.d.ts +9 -0
  160. package/dist/core/runner/resume-checkpoint-screen.js +4 -84
  161. package/dist/core/runner/run-git-lane.d.ts +2 -0
  162. package/dist/core/runner/run-git-lane.js +4 -1
  163. package/dist/core/runner/run-harness-handlers.d.ts +2 -0
  164. package/dist/core/runner/run-harness-handlers.js +3 -2
  165. package/dist/core/runner/run-leg.js +25 -8
  166. package/dist/core/runner/run-settle-and-teardown.js +6 -4
  167. package/dist/core/runner/run-stop-and-final-verify.js +2 -1
  168. package/dist/core/runner/run-turn-boundary.js +7 -4
  169. package/dist/core/runner/runtask.js +6 -6
  170. package/dist/core/runner/steering-preview.d.ts +9 -0
  171. package/dist/core/runner/steering-preview.js +5 -0
  172. package/dist/core/runner/stream-steer-verb.js +8 -0
  173. package/dist/core/runner/terminal-account.js +1 -1
  174. package/dist/core/runner/tool-face-overlay.js +2 -0
  175. package/dist/core/runner/turn-attachments.d.ts +5 -0
  176. package/dist/core/runner/turn-attachments.js +1 -1
  177. package/dist/core/runner-deps.d.ts +8 -4
  178. package/dist/core/secret-env.d.ts +9 -6
  179. package/dist/core/secret-env.js +41 -22
  180. package/dist/core/session-checkout.d.ts +108 -0
  181. package/dist/core/session-checkout.js +485 -0
  182. package/dist/core/session-store.d.ts +13 -5
  183. package/dist/core/session-store.js +28 -4
  184. package/dist/core/session.d.ts +29 -5
  185. package/dist/core/session.js +3 -0
  186. package/dist/core/store-contracts/checkpoint-store-contract.d.ts +6 -12
  187. package/dist/core/store-contracts/checkpoint-store-contract.js +73 -6
  188. package/dist/core/store-contracts/session-store-contract.js +61 -8
  189. package/dist/core/task-event.d.ts +28 -1
  190. package/dist/core/task-notification.d.ts +9 -1
  191. package/dist/core/task-notification.js +8 -1
  192. package/dist/core/task-registry-agent.d.ts +40 -8
  193. package/dist/core/task-registry-agent.js +57 -24
  194. package/dist/core/task-registry-shared.d.ts +33 -11
  195. package/dist/core/task-registry-workflow.js +4 -3
  196. package/dist/core/task-registry.d.ts +27 -10
  197. package/dist/core/task-registry.js +19 -13
  198. package/dist/core/task-result.d.ts +5 -5
  199. package/dist/core/task-spec-members.js +1 -1
  200. package/dist/core/task-spec.d.ts +21 -1
  201. package/dist/core/terminal-cause.d.ts +6 -6
  202. package/dist/core/tool-face.d.ts +6 -2
  203. package/dist/core/tool-face.js +1 -1
  204. package/dist/core/tool-policy.d.ts +16 -13
  205. package/dist/core/tool-policy.js +11 -10
  206. package/dist/core/tool-roster.d.ts +4 -4
  207. package/dist/core/tool-spec.d.ts +42 -19
  208. package/dist/core/types.d.ts +2 -2
  209. package/dist/core/types.js +1 -1
  210. package/dist/core/untrusted-egress.d.ts +15 -4
  211. package/dist/core/untrusted-egress.js +23 -22
  212. package/dist/core/workspace-snapshot.d.ts +3 -1
  213. package/dist/core/workspace-snapshot.js +1 -0
  214. package/dist/engine/execution-env/node-execution-env.d.ts +3 -0
  215. package/dist/engine/execution-env/node-execution-env.js +12 -5
  216. package/dist/engine/harness/types.d.ts +77 -1
  217. package/dist/engine/harness/types.js +108 -0
  218. package/dist/engine/session/import-validate.js +6 -1
  219. package/dist/engine/session/parent-chain-guard.d.ts +3 -3
  220. package/dist/engine/session/parent-chain-guard.js +1 -1
  221. package/dist/engine/session/path-to-root.d.ts +18 -3
  222. package/dist/engine/session/path-to-root.js +15 -1
  223. package/dist/engine/session/repo-utils.d.ts +12 -1
  224. package/dist/engine/session/repo-utils.js +41 -0
  225. package/dist/engine/session/session.d.ts +24 -1
  226. package/dist/engine/session/session.js +46 -2
  227. package/dist/engine/session/storage-base.d.ts +20 -0
  228. package/dist/engine/session/storage-base.js +28 -9
  229. package/dist/index.d.ts +16 -12
  230. package/dist/index.js +11 -8
  231. package/dist/internal/harness-types.d.ts +1 -1
  232. package/dist/internal/harness.d.ts +4 -3
  233. package/dist/internal/harness.js +3 -2
  234. package/dist/orchestration/run-workflow-tool.d.ts +7 -1
  235. package/dist/orchestration/run-workflow-tool.js +28 -11
  236. package/dist/orchestration/workflow-governance.d.ts +1 -1
  237. package/dist/orchestration/workflow.d.ts +6 -0
  238. package/dist/orchestration/workflow.js +5 -8
  239. package/dist/stores/file/checkpoint-store.d.ts +2 -0
  240. package/dist/stores/file/checkpoint-store.js +7 -6
  241. package/dist/stores/file/fs-atomic.d.ts +0 -22
  242. package/dist/stores/file/fs-atomic.js +0 -17
  243. package/dist/stores/file/index.d.ts +2 -9
  244. package/dist/stores/file/index.js +2 -4
  245. package/dist/stores/file/session-store.d.ts +13 -7
  246. package/dist/stores/file/session-store.js +42 -18
  247. package/dist/tools/fs/bash-program-position.d.ts +3 -2
  248. package/dist/tools/fs/bash-program-position.js +10 -10
  249. package/dist/tools/fs/program-read-model.d.ts +34 -10
  250. package/dist/tools/fs/program-read-model.js +37 -7
  251. package/dist/tools/fs/program-read-model.rows.js +13 -2
  252. package/dist/tools/fs/read-deny.d.ts +13 -1
  253. package/dist/tools/fs/read-deny.js +10 -2
  254. package/dist/tools/fs/removal-targets.js +5 -5
  255. package/dist/tools/fs/safety.d.ts +2 -0
  256. package/dist/tools/fs/safety.js +1 -1
  257. package/dist/tools/fs/shell-read-argv.d.ts +11 -2
  258. package/dist/tools/fs/shell-read-argv.js +45 -29
  259. package/dist/tools/fs/shell-read-bindings.d.ts +20 -2
  260. package/dist/tools/fs/shell-read-bindings.js +153 -69
  261. package/dist/tools/fs/shell-read-brace.d.ts +18 -0
  262. package/dist/tools/fs/shell-read-brace.js +79 -0
  263. package/dist/tools/fs/shell-read-check.js +29 -11
  264. package/dist/tools/fs/shell-read-establish.js +43 -22
  265. package/dist/tools/fs/shell-read-functions.js +2 -2
  266. package/dist/tools/fs/shell-read-judge.d.ts +11 -0
  267. package/dist/tools/fs/shell-read-judge.js +3 -3
  268. package/dist/tools/fs/shell-read-object-path.d.ts +59 -0
  269. package/dist/tools/fs/shell-read-object-path.js +119 -0
  270. package/dist/tools/fs/shell-read-plan.d.ts +18 -0
  271. package/dist/tools/fs/shell-read-plan.js +217 -104
  272. package/dist/tools/fs/shell-read-start.d.ts +76 -13
  273. package/dist/tools/fs/shell-read-start.js +97 -17
  274. package/dist/tools/fs/shell-read-word.d.ts +10 -0
  275. package/dist/tools/fs/shell-read-word.js +9 -5
  276. package/dist/tools/fs/shell-read-workdir.d.ts +54 -14
  277. package/dist/tools/fs/shell-read-workdir.js +103 -19
  278. package/dist/tools/fs/shell-read-write-targets.js +13 -8
  279. package/dist/tools/worktree.d.ts +3 -2
  280. package/dist/tools/worktree.js +7 -35
  281. package/package.json +3 -2
  282. package/test/export-surface.snapshot.json +93 -43
  283. package/wire-changes/core-stores-sql@7.62.0.json +30 -0
  284. package/wire-changes/core-stores-sql@7.63.0.json +30 -0
  285. package/wire-changes/core@7.62.0.json +2181 -0
  286. package/wire-changes/core@7.63.0.json +3523 -0
  287. package/wire-changes/mods-kernel@7.62.0.json +42 -0
  288. package/wire-changes/mods-kernel@7.63.0.json +30 -0
  289. package/wire-changes/primitives@7.62.0.json +30 -0
  290. package/wire-changes/primitives@7.63.0.json +445 -0
  291. package/dist/core/consolidate-scope.d.ts +0 -93
  292. package/dist/core/consolidate-scope.js +0 -98
  293. package/dist/core/credential-vocabulary.d.ts +0 -176
  294. package/dist/core/credential-vocabulary.js +0 -190
  295. package/dist/core/redaction-passes.d.ts +0 -82
  296. package/dist/core/redaction-passes.js +0 -377
  297. package/dist/core/runner/memory-consolidation.d.ts +0 -109
  298. package/dist/core/runner/memory-consolidation.js +0 -251
@@ -1,3 +1,8 @@
1
+ import { literalTildeSpelling } from "../../shell/lex.js";
2
+ import { programBasename } from "./bash-program-position.js";
3
+ import { launcherOptionPathRole, launcherOptionStep, programRow, readOptionRole } from "./program-read-model.js";
4
+ import { chdirFrom } from "./shell-read-object-path.js";
5
+ import { anchorSpelling, pathFamilyOf } from "./safety.js";
1
6
  const NO_NAMES = new Set();
2
7
  export function heldAt(table, bound = NO_NAMES) {
3
8
  return { start: (key) => table.stateOf(key), bound };
@@ -62,27 +67,101 @@ export function stdinFeeds(segments, start, piped) {
62
67
  };
63
68
  return fedAt;
64
69
  }
65
- export function rebasePlan(plan, from, to, floor) {
66
- if (to !== "unmodelled" && to.length === 1 && to[0] === from)
67
- return plan;
68
- const fromSep = from.endsWith("/") || from.endsWith("\\") ? from : `${from}/`;
69
- const moveInto = (into, b) => {
70
- if (b === from)
71
- return [...into];
72
- if (b.startsWith(fromSep))
73
- return into.map((t) => `${t.endsWith("/") || t.endsWith("\\") ? t : `${t}/`}${b.slice(fromSep.length)}`);
74
- return [b];
70
+ export function nestedStartOf(cwd) {
71
+ return anchorSpelling(pathFamilyOf({ cwd }), cwd, "\u0000");
72
+ }
73
+ export function walkFromStart(base) {
74
+ const at = base.search(/[\\/]\u0000(?:[\\/]|$)/);
75
+ return at < 0 ? undefined : base.slice(at + 3);
76
+ }
77
+ const settledWord = (r) => r.expands === false && (r.values?.length ?? 1) <= 1 && r.word.pieces.every((q) => q.literal !== undefined || q.expansion !== undefined);
78
+ export function launchedInOf(pos, toks) {
79
+ if (pos.baseMovedAt.length === 0)
80
+ return undefined;
81
+ const dirs = [];
82
+ let owner;
83
+ for (const at of pos.baseMovedAt) {
84
+ const l = pos.launchers.filter((x) => x.index < at).at(-1);
85
+ const row = programRow(programBasename(l.name));
86
+ const w = toks[at].folded;
87
+ const step = launcherOptionStep(row, w);
88
+ const own = step === 2 ? undefined : readOptionRole(row, w);
89
+ const role = step === 2 ? launcherOptionPathRole(row, w) : own?.form === "attached" ? own.role : undefined;
90
+ if (role?.kind !== "path" || role.base !== "working-directory")
91
+ return "unmodelled";
92
+ const word = toks[step === 2 ? at + 1 : at];
93
+ const twoWays = own?.form === "attached" && own.spec?.arity === "attached" && own.value.startsWith("=");
94
+ const value = step === 2 ? word.folded : own?.form === "attached" && !twoWays ? literalTildeSpelling(own.value) : undefined;
95
+ const dir = !settledWord(word) ? null : value !== undefined && value !== "" ? value : undefined;
96
+ if (l.index === owner)
97
+ dirs[dirs.length - 1] = dir;
98
+ else
99
+ dirs.push(dir);
100
+ owner = l.index;
101
+ }
102
+ return dirs.includes(undefined) ? "unmodelled" : dirs;
103
+ }
104
+ export function launchedAt(launchedIn, bases, floor, homeDir) {
105
+ if (launchedIn === undefined)
106
+ return undefined;
107
+ const here = bases !== "unmodelled" ? bases : floor;
108
+ let to = launchedIn === "unmodelled" ? "unmodelled" : bases;
109
+ let toFloor = here;
110
+ let unsettled = false;
111
+ for (const dir of launchedIn === "unmodelled" ? [] : launchedIn) {
112
+ if (dir === null) {
113
+ toFloor = [...new Set([...(to !== "unmodelled" ? to : (toFloor ?? [])), ...(here ?? [])])];
114
+ to = "unmodelled";
115
+ unsettled = true;
116
+ }
117
+ else if (to !== "unmodelled") {
118
+ const next = chdirFrom(to, dir, homeDir);
119
+ if (next === undefined)
120
+ to = "unmodelled";
121
+ else
122
+ toFloor = to = next;
123
+ }
124
+ else if (toFloor !== undefined)
125
+ toFloor = chdirFrom(toFloor, dir, homeDir) ?? toFloor;
126
+ }
127
+ const words = [...new Set([...(here ?? []), ...(toFloor ?? [])])];
128
+ return { to, ...(toFloor !== undefined && toFloor.length > 0 ? { floor: toFloor } : {}), ...(words.length > 0 ? { words } : {}), ...(unsettled ? { unsettled: true } : {}) };
129
+ }
130
+ export function rebasePlan(plan, to, floor, unsettled = false) {
131
+ const key = JSON.stringify([to, floor, unsettled]);
132
+ const known = REBASED.get(plan)?.get(key);
133
+ if (known !== undefined)
134
+ return known;
135
+ const out = rebasedOnto(plan, to, floor, unsettled);
136
+ REBASED.set(plan, (REBASED.get(plan) ?? new Map()).set(key, out));
137
+ return out;
138
+ }
139
+ const REBASED = new WeakMap();
140
+ function rebasedOnto(plan, to, floor, unsettled) {
141
+ const placed = (into, b) => {
142
+ const rest = walkFromStart(b);
143
+ if (rest === undefined)
144
+ return [b];
145
+ return (into ?? []).map((t) => (rest === "" ? t : anchorSpelling(pathFamilyOf({ cwd: t }), t, rest)));
75
146
  };
76
- const moveBase = (b) => (to === "unmodelled" ? [] : moveInto(to, b));
147
+ const moveBase = (b) => (to === "unmodelled" ? [] : placed(to, b));
77
148
  const floorAt = to === "unmodelled" ? floor : to;
78
- const moveFloor = (bases, into) => [...new Set(bases.flatMap((b) => moveInto(into, b)))];
149
+ const floorsOf = (bases, into) => {
150
+ const floors = [...new Set(bases.flatMap((b) => placed(into, b)))];
151
+ const fromStart = unsettled && to === "unmodelled" && bases.some((b) => walkFromStart(b) !== undefined);
152
+ return { ...(floors.length > 0 ? { floorBases: floors } : {}), ...(fromStart ? { unsettledStart: true } : {}) };
153
+ };
79
154
  const moveCandidate = (c) => {
80
- if (c.bases === "unmodelled")
81
- return c.floorBases === undefined || floorAt === undefined ? c : { ...c, floorBases: moveFloor(c.floorBases, floorAt) };
155
+ if (c.bases === "unmodelled") {
156
+ if (c.floorBases === undefined)
157
+ return c;
158
+ const { floorBases: _, ...rest } = c;
159
+ return { ...rest, ...floorsOf(c.floorBases, floorAt) };
160
+ }
82
161
  if (c.bases.length === 0)
83
162
  return c;
84
163
  if (to === "unmodelled")
85
- return floor === undefined ? { ...c, bases: "unmodelled" } : { ...c, bases: "unmodelled", floorBases: moveFloor(c.bases, floor) };
164
+ return { ...c, bases: "unmodelled", ...floorsOf(c.bases, floor) };
86
165
  return { ...c, bases: [...new Set(c.bases.flatMap(moveBase))] };
87
166
  };
88
167
  return {
@@ -93,8 +172,9 @@ export function rebasePlan(plan, from, to, floor) {
93
172
  redirects: s.redirects.map((r) => (r.target === undefined ? r : { ...r, target: moveCandidate(r.target) })),
94
173
  ...(s.baseMove !== undefined ? { baseMove: { target: typeof s.baseMove.target === "string" ? s.baseMove.target : moveCandidate(s.baseMove.target) } } : {}),
95
174
  bases: s.bases === "unmodelled" || to === "unmodelled" ? "unmodelled" : [...new Set(s.bases.flatMap(moveBase))],
96
- nested: s.nested.map((n) => rebasePlan(n, from, to, floor)),
97
- ...(s.printed !== undefined ? { printed: s.printed.map((n) => rebasePlan(n, from, to, floor)) } : {}),
175
+ ...(s.programStart !== undefined ? { programStart: moveCandidate(s.programStart) } : {}),
176
+ nested: s.nested.map((n) => rebasePlan(n, to, floor, unsettled)),
177
+ ...(s.printed !== undefined ? { printed: s.printed.map((n) => rebasePlan(n, to, floor, unsettled)) } : {}),
98
178
  })),
99
179
  };
100
180
  }
@@ -62,6 +62,14 @@ export interface SegmentReading {
62
62
  * one reading.
63
63
  */
64
64
  export declare function segmentReadingsOf(argv: readonly LexedWord[], targets: readonly LexedWord[], table: BindingTable, homeDir: string | undefined): SegmentReading[];
65
+ /** The word the shell hands the program for one read in a reading: its one value as the argument word it is
66
+ * ({@link argumentWordOf}), named by the read's `raw` — the word's own spelling as written, or a split field's own text —
67
+ * where the read settled into one value that is no pattern and is not the word's own spelling; the word as written
68
+ * otherwise. An assignment the shell PERFORMS (at command position) stays the assignment it is; an assignment-shaped
69
+ * ARGUMENT (`dd of="$T"`) is the value it expands to, as any argument is — and a word whose value is assignment-shaped
70
+ * is that argument (`X=of=k; dd "$X"` hands dd the keyed `of=k`): the readers that ask an argument's `NAME=value` shape
71
+ * ask it of this word. */
72
+ export declare function receivedWordOf(r: Read): LexedWord;
65
73
  /** The spelling a reader names a value by, read directly as a path — for a pattern, the name the program opens where it
66
74
  * matches nothing: its text, a leading `~` spelled as the literal tilde it is ({@link literalTildeSpelling}, the lexer's
67
75
  * spelling of a written one) — the shell never tilde-expands what a quoted piece or an expansion produced, so a value that
@@ -113,6 +121,8 @@ export declare function spelledWithHome(spelled: string, raw: string, home: stri
113
121
  * arithmetic expression (`$((2*(3+4)))`), which bash evaluates and never globs. A compound assignment's value is no
114
122
  * such word — the assignment reads nothing — unless the unread value may run something ({@link compoundValueHidesRun}). */
115
123
  export declare function unreadableSentenceOf(lexed: LexedCommand, text: string, globs?: boolean): string | undefined;
124
+ /** The mark a settled value carries: `"glob"` when an unquoted pathname-expansion character survives in it. */
125
+ export declare function globMark(glob: boolean): WordExpands;
116
126
  /** What the shell does to a word (or an assignment's value) before the program sees it, read off its pieces: the
117
127
  * lexer's expansion facts alone. A quoted text that an evaluator or an interpreter later RUNS (an arithmetic
118
128
  * subscript, an inline program) is no rewrite of the word: the slot it stands in says what it is (the program read
@@ -1,6 +1,7 @@
1
1
  import { argvOf, compoundValueHidesRun, lexShell, literalTildeSpelling, UNRESOLVED_SENTENCE } from "../../shell/lex.js";
2
2
  import { expansionInWord, extglobOpenerIn, globInWord, homeExpansionOf } from "./bash-readonly-classifier.js";
3
3
  import { floorOf, judgedSpellingOf, namesIn, wordFloorValues, wordValues } from "./shell-read-bindings.js";
4
+ import { READINGS_MAX } from "./shell-read-brace.js";
4
5
  export function readsOf(words, table, homeDir, split = false) {
5
6
  return words.map((word) => {
6
7
  const refersToName = word.pieces.some((p) => p.expansion?.kind === "parameter") || namesIn(word).length > 0;
@@ -24,14 +25,13 @@ export function readsOf(words, table, homeDir, split = false) {
24
25
  raw: word.raw,
25
26
  folded: spellingOf(values[0]),
26
27
  ...(pattern !== undefined ? { pattern } : {}),
27
- expands: glob ? "glob" : false,
28
+ expands: globMark(glob),
28
29
  settled: true,
29
30
  values,
30
31
  ...(floor !== undefined && !values.every((v) => spellingOf(v) === floor) ? { homeFloor: floor } : {}),
31
32
  };
32
33
  });
33
34
  }
34
- const READINGS_MAX = 64;
35
35
  export function segmentReadingsOf(argv, targets, table, homeDir) {
36
36
  const several = new Map();
37
37
  for (const word of [...argv, ...targets]) {
@@ -73,7 +73,7 @@ export function segmentReadingsOf(argv, targets, table, homeDir) {
73
73
  floors.set(name, floor);
74
74
  }
75
75
  }
76
- return { table: reading, reads: fielded, argv: fielded.map((r) => valuedWord(r.word, r)), floors };
76
+ return { table: reading, reads: fielded, argv: fielded.map(receivedWordOf), floors };
77
77
  });
78
78
  }
79
79
  function namesReadIn(word, table) {
@@ -96,9 +96,10 @@ function overlaidTable(table, overlay) {
96
96
  inherited: table.inherited,
97
97
  };
98
98
  }
99
- function valuedWord(word, r) {
99
+ export function receivedWordOf(r) {
100
+ const word = r.word;
100
101
  const v = r.values?.length === 1 ? r.values[0] : undefined;
101
- if (word.assignment !== undefined || v === undefined || v.glob === true || v.spelled === word.folded)
102
+ if ((word.assignment !== undefined && word.atCommandPosition) || v === undefined || v.glob === true || v.spelled === word.folded)
102
103
  return word;
103
104
  return argumentWordOf(v.spelled, r.raw) ?? word;
104
105
  }
@@ -158,6 +159,9 @@ export function unreadableSentenceOf(lexed, text, globs = true) {
158
159
  }
159
160
  return globs && extglobOpenerIn(lexed, text) ? "an extended pattern (`?(…)` `*(…)` `+(…)` `@(…)` `!(…)`) is one this reader does not expand" : undefined;
160
161
  }
162
+ export function globMark(glob) {
163
+ return glob ? "glob" : false;
164
+ }
161
165
  export function expandsOf(w) {
162
166
  if (expansionInWord(w))
163
167
  return "unresolvable";
@@ -6,14 +6,15 @@
6
6
  * `popd` with the stack the text pushed, and a literal `eval` text read as the text it is, in this shell. Any other text
7
7
  * that may move the base where the plan cannot follow it (`source`, a non-literal `eval`, a computed program word, a
8
8
  * call into a function body or a code body that may move, an option the stack reading does not follow) LOSES it: what
9
- * stands after it cannot be placed. A move inside a subshell, a pipeline member or a function body nobody called is read
10
- * as a move too — a candidate SET, more places, never fewer (a move may also fail: the unmoved place stays a candidate
11
- * unless an `&&` run proves the move ran).
9
+ * stands after it cannot be placed. The moves are a candidate SET — a move may fail: the unmoved place stays a candidate
10
+ * unless an `&&` run proves the move ran. A move reaches a later segment where a binding of the same segment would: the
11
+ * binding thread's one visibility rule (`BindingThread.reaches`) — a move inside a `( … )` group, a pipeline member that
12
+ * is not the last, a background job or a function body is no move for a segment outside it ({@link WorkdirScope}).
12
13
  */
13
14
  import type { Bare } from "./shell-read-argv.js";
14
15
  import type { BindingTable, WorkingDirectoryState } from "./shell-read-bindings.js";
15
16
  import type { Read } from "./shell-read-word.js";
16
- import { type LexedWord } from "../../shell/lex.js";
17
+ import { type LexedSegment, type LexedWord } from "../../shell/lex.js";
17
18
  import { type FunctionCall } from "./shell-read-functions.js";
18
19
  /** A working directory the shell could be standing in, with the latest move this candidate assumes did NOT run
19
20
  * (`skippedCd`), and what the shell holds there: `pwd` — what `PWD` spells (absent: not the base's spelling — a physical
@@ -44,13 +45,16 @@ export type BaseShift = {
44
45
  };
45
46
  /** What the walk asks of the plan around it: the declared home (a `~` target), whether the text binds a name anywhere
46
47
  * (`OLDPWD` — then `cd -` reads the table's value; `PWD` — then a move's `OLDPWD` is not the base the candidate stood
47
- * in), and where a literal `eval` text started at a candidate ends ({@link WorkdirThread.end}). */
48
+ * in), and where a literal `eval` text started at candidates — a candidate, the floor of a walk that lost track, or a place
49
+ * the walk does not know — ends ({@link WorkdirThread.end}), and where its walk went that its end does not say (`reached`). */
48
50
  export interface WorkdirContext {
49
51
  readonly homeDir?: string;
50
52
  readonly textBinds: (name: string) => boolean;
51
- readonly evalEnd: (text: string, from: BaseCandidate, table: BindingTable) => {
53
+ readonly evalEnd: (text: string, from: readonly BaseCandidate[] | "unmodelled", table: BindingTable) => {
52
54
  readonly end: readonly BaseCandidate[] | "unmodelled";
53
55
  readonly physical: boolean;
56
+ readonly reached: ReadonlySet<string>;
57
+ readonly unsettled?: true;
54
58
  };
55
59
  }
56
60
  /** One segment's move as the walk applies it: the shift, whether an `&&` run proves it ran, the first segment of the
@@ -67,8 +71,6 @@ export interface CommandTable extends BindingTable {
67
71
  readonly commandSets: ReadonlySet<string>;
68
72
  }
69
73
  export declare class WorkdirThread {
70
- /** The start is where the shell stands (not a placeholder a nested plan is re-based from): `PWD` reads it. */
71
- private readonly readable;
72
74
  private candidates;
73
75
  private lostAt;
74
76
  /** The reachable bases at the moment the walk lost track — the deny floor of a word it cannot place. */
@@ -78,13 +80,21 @@ export declare class WorkdirThread {
78
80
  private physical;
79
81
  /** The last segment whose move was applied or lost track ({@link WorkingDirectoryState.movedAt}). */
80
82
  private movedAt;
83
+ /** The start is a place the shell stands in — no placeholder start, no base walked from one (an `eval` text a nested text runs): `PWD` reads it. */ private readonly readable;
81
84
  constructor(start: readonly BaseCandidate[] | readonly string[] | "unmodelled",
82
- /** The start is where the shell stands (not a placeholder a nested plan is re-based from): `PWD` reads it. */
83
- readable: boolean,
84
85
  /** The physical mode may already be on where the text starts (an `eval` text run in a shell that set it). */
85
86
  startsPhysical?: boolean);
86
87
  get lost(): boolean;
88
+ /** The walk lost track at a move to a directory the text does not settle ({@link movesToUnsettled}, a literal `eval` text's
89
+ * own such move): what the text does after it, it does from a directory it does not know
90
+ * ({@link import("./shell-read-plan.js").ReadCandidate.unsettledStart}); so does a loop it does not count that moved it
91
+ * (each beat moves again from where the last one went). Once set it stays: a closed scope's places stay
92
+ * in the deny floor ({@link restore}), and a function body that moved may run at any later call. */
93
+ get unsettled(): boolean;
94
+ private unsettledAt;
95
+ /** The deny floor of the words the walk could not place: a closed scope that lost track keeps its floor for its words. */
87
96
  get floorBases(): readonly string[] | undefined;
97
+ private closedFloors;
88
98
  /** The candidates a reader whose `&&` run starts at `andRunStart` may stand in. */
89
99
  reachable(andRunStart: number): readonly BaseCandidate[];
90
100
  /** Where the text ends: every candidate, or `unmodelled`. */
@@ -95,14 +105,43 @@ export declare class WorkdirThread {
95
105
  setPhysical(): void;
96
106
  /** Where the text ends the physical mode may be on (an `eval` text hands it back to the shell that ran it). */
97
107
  get mayBePhysical(): boolean;
98
- /** Stop placing anything from segment `at` on: the reachable bases (and a target's floor spellings) become the deny floor. */
99
- lose(at: number, reachable: readonly BaseCandidate[], floorTargets?: readonly string[]): void;
108
+ /** What the walk holds before a segment that runs apart from the shell after it ({@link WorkdirScope}). */
109
+ snapshot(): WorkdirScope;
110
+ /** A scope closed: what its moves did never reached the shell outside it — the walk holds again what it held before them.
111
+ * The places they went stay a deny floor of a walk that later loses track ({@link scopeReached}). */
112
+ restore(s: WorkdirScope): void;
113
+ /** Places a closed scope's moves went (a function body run at a call goes there, `eval`'s text closed one): as a binding's
114
+ * deny floor stays wherever the binding may have run, they join the deny floor where the walk loses track. */
115
+ private scopeReached;
116
+ get reached(): ReadonlySet<string>;
117
+ reach(bases: Iterable<string>): void;
118
+ /** The walk once more, from `at` — candidates, or `lost` where this walk's floor says —, holding what this walk holds where
119
+ * it ends: its mode, the places its scopes went, whether it went to a directory it does not know. Where a text written
120
+ * earlier runs later (a function body at a call after it) it is walked from there. `floor`: places a walk started where it
121
+ * does not know may stand, beside this walk's floor. */
122
+ fork(at: readonly BaseCandidate[] | "lost", floor?: readonly string[]): WorkdirThread;
123
+ /** Where a text this walk runs later went (`places`) is where a word it could not place may stand: they join the deny floor,
124
+ * where it has one. */
125
+ widen(places: Iterable<string>): void;
126
+ /** Stop placing anything from segment `at` on: the reachable bases (and a target's floor spellings, the places a closed
127
+ * scope went) become the deny floor. `unsettled`: the walk no longer knows where it stands either (a move it ran a number
128
+ * of times it does not count). */
129
+ lose(at: number, reachable: readonly BaseCandidate[], floorTargets?: readonly string[], unsettled?: boolean): void;
100
130
  /** Apply segment `si`'s move. */
101
131
  shift(si: number, at: ShiftAt, ctx: WorkdirContext): void;
102
132
  /** Where one candidate goes: the places (the CDPATH entries a bare target is searched through, then the target itself),
103
133
  * each with the `PWD`, `OLDPWD` and stack the move leaves — or `lost`. */
104
134
  private movedFrom;
105
135
  }
136
+ /** The walk's state before a segment's moves, where the segment runs apart from the shell after it (a `( … )` group, a
137
+ * pipeline member that is not the last, a background job, a function body): handed back where the scope closes. */
138
+ export interface WorkdirScope {
139
+ readonly candidates: readonly BaseCandidate[];
140
+ readonly lost: boolean;
141
+ readonly floors: readonly string[] | undefined;
142
+ readonly physical: boolean;
143
+ readonly movedAt: number;
144
+ }
106
145
  /** Does bash search `CDPATH` for this `cd` target: not an absolute one, not `.` / `..`, not one starting `./` / `../`? */
107
146
  export declare function searchesCdPath(dir: string): boolean;
108
147
  /** The base shift of a `cd` / `pushd` / `popd` / `dirs` segment, read off its arguments as the program receives them (and
@@ -137,7 +176,8 @@ export interface TextMoves {
137
176
  readonly mayMove: readonly boolean[];
138
177
  /** Segment `si` may call a function (read before the walk, to bound where bodies run). */
139
178
  readonly mayCallAt: readonly boolean[];
140
- /** Segment `si` may run a function body: it may call one, hands a code body to run later, or runs text the plan does not read. */
179
+ /** Segment `si` may run a function body where it stands ({@link segmentRunsBody}): it may call one, hands a code body to run
180
+ * later, runs text the plan does not read, or hands a shell a text that may (a substitution). */
141
181
  readonly runsBodyAt: readonly boolean[];
142
182
  readonly bodiesAt: readonly (readonly string[])[];
143
183
  readonly physicalAt: readonly boolean[];
@@ -150,7 +190,7 @@ export interface TextMoves {
150
190
  /** The segment runs a body that may move the base where the walk cannot follow it: a call when a body may move or whose body is not read, a code body that may move or call one. */
151
191
  losesBaseAt(si: number, table: BindingTable): boolean;
152
192
  }
153
- export declare function textMovesOf(defined: ReadonlySet<string>, argvs: readonly (readonly LexedWord[])[], inFunctionBody: (si: number) => boolean, inherited?: {
193
+ export declare function textMovesOf(defined: ReadonlySet<string>, segments: readonly LexedSegment[], argvs: readonly (readonly LexedWord[])[], inFunctionBody: (si: number) => boolean, inherited?: {
154
194
  readonly functions: ReadonlySet<string>;
155
195
  readonly unread: ReadonlySet<string>;
156
196
  }): TextMoves;
@@ -1,33 +1,39 @@
1
- import { argvOf, lexShell } from "../../shell/lex.js";
1
+ import { argvOf, expansionsInHeredocBody, lexShell } from "../../shell/lex.js";
2
2
  import { shellWordsOf } from "../../shell/shell-words.js";
3
- import { resolveOperandLexically } from "./bash-readonly-classifier.js";
3
+ import { resolveOperandLexically, resolveOperandSpellings } from "./bash-readonly-classifier.js";
4
4
  import { readProgramPosition } from "./bash-program-position.js";
5
5
  import { valueOf } from "./shell-read-bindings.js";
6
6
  import { evalDefinedFunctions, functionCallOf } from "./shell-read-functions.js";
7
7
  import { codeBodies } from "./shell-reentry-text.js";
8
8
  import { isAbsoluteForFamily } from "./safety.js";
9
+ import { walkFromStart } from "./shell-read-start.js";
9
10
  const TEXT_RUNNING_PROGRAMS = new Set(["eval", "source", "."]);
10
11
  const BASE_CANDIDATE_MAX = 256;
11
12
  const tableSays = (name, at, ctx) => ctx.textBinds(name) || at.table.commandSets.has(name);
12
13
  export class WorkdirThread {
13
- readable;
14
14
  candidates;
15
15
  lostAt = false;
16
16
  floors;
17
17
  physical;
18
18
  movedAt = -1;
19
- constructor(start, readable, startsPhysical = false) {
20
- this.readable = readable;
19
+ readable;
20
+ constructor(start, startsPhysical = false) {
21
21
  this.physical = startsPhysical;
22
22
  this.lostAt = start === "unmodelled";
23
- this.candidates = start === "unmodelled" ? [] : start.map((c) => (typeof c === "string" ? { base: c, skippedCd: -1, ...(readable && isAbsoluteForFamily("own", c) ? { pwd: c } : {}), dirStack: [] } : { ...c, skippedCd: -1 }));
23
+ this.readable = start === "unmodelled" || start.every((c) => walkFromStart(typeof c === "string" ? c : c.base) === undefined);
24
+ this.candidates = start === "unmodelled" ? [] : start.map((c) => (typeof c === "string" ? { base: c, skippedCd: -1, ...(this.readable && isAbsoluteForFamily("own", c) ? { pwd: c } : {}), dirStack: [] } : { ...c, skippedCd: -1 }));
24
25
  }
25
26
  get lost() {
26
27
  return this.lostAt;
27
28
  }
29
+ get unsettled() {
30
+ return this.unsettledAt;
31
+ }
32
+ unsettledAt = false;
28
33
  get floorBases() {
29
- return this.floors;
34
+ return this.closedFloors.length === 0 ? this.floors : [...new Set([...(this.floors ?? []), ...this.closedFloors])];
30
35
  }
36
+ closedFloors = [];
31
37
  reachable(andRunStart) {
32
38
  return this.candidates.filter((c) => c.skippedCd < andRunStart);
33
39
  }
@@ -56,15 +62,63 @@ export class WorkdirThread {
56
62
  get mayBePhysical() {
57
63
  return this.physical;
58
64
  }
59
- lose(at, reachable, floorTargets = []) {
65
+ snapshot() {
66
+ return { candidates: this.candidates, lost: this.lostAt, floors: this.floors, physical: this.physical, movedAt: this.movedAt };
67
+ }
68
+ restore(s) {
69
+ this.reach(this.lostAt ? (this.floors ?? []) : this.candidates.map((c) => c.base));
70
+ if (this.lostAt && this.floors !== undefined)
71
+ this.closedFloors = [...this.closedFloors, ...this.floors];
72
+ this.candidates = s.candidates;
73
+ this.lostAt = s.lost;
74
+ this.floors = s.floors;
75
+ this.physical = s.physical;
76
+ this.movedAt = s.movedAt;
77
+ }
78
+ scopeReached = new Set();
79
+ get reached() {
80
+ return this.scopeReached;
81
+ }
82
+ reach(bases) {
83
+ this.scopeReached = new Set([...this.scopeReached, ...bases]);
84
+ }
85
+ fork(at, floor = []) {
86
+ const w = new WorkdirThread(at === "lost" ? "unmodelled" : at, this.physical);
87
+ if (at === "lost") {
88
+ w.candidates = this.candidates;
89
+ const floors = [...new Set([...(this.floorBases ?? []), ...floor])];
90
+ if (floors.length > 0)
91
+ w.floors = floors;
92
+ }
93
+ w.scopeReached = this.scopeReached;
94
+ w.unsettledAt = this.unsettledAt;
95
+ w.movedAt = this.movedAt;
96
+ return w;
97
+ }
98
+ widen(places) {
99
+ if ((this.floorBases ?? []).length > 0)
100
+ this.closedFloors = [...new Set([...this.closedFloors, ...places])];
101
+ }
102
+ lose(at, reachable, floorTargets = [], unsettled = false) {
60
103
  this.movedAt = Math.max(this.movedAt, at);
61
104
  this.lostAt = true;
62
- this.floors ??= [...new Set([...reachable.map((c) => c.base), ...floorTargets])];
105
+ if (unsettled)
106
+ this.unsettledAt = true;
107
+ this.floors ??= [...new Set([...reachable.map((c) => c.base), ...floorTargets, ...this.scopeReached])];
63
108
  }
64
109
  shift(si, at, ctx) {
65
110
  if (this.lostAt) {
66
- if (this.floors !== undefined && this.floors.length > 0)
67
- this.floors = [...new Set([...this.floors, ...floorOf(at.shift, this.reachable(at.andRunStart), ctx)])];
111
+ const floors = this.floors ?? [];
112
+ const before = this.reachable(at.andRunStart).map((c) => c.base);
113
+ const named = [...new Set([...floors, ...before])];
114
+ if (named.length > BASE_CANDIDATE_MAX)
115
+ this.unsettledAt = true;
116
+ const from = floors.length === 0 ? [] : named.length > BASE_CANDIDATE_MAX ? [...new Set(before)] : named;
117
+ const ran = at.shift.kind === "eval" ? ctx.evalEnd(at.shift.text, from.length === 0 ? "unmodelled" : from.map((base) => ({ base, skippedCd: -1 })), at.table) : undefined;
118
+ if (from.length > 0)
119
+ this.floors = [...new Set([...floors, ...(ran === undefined ? floorOf(at.shift, from, ctx) : [...ran.reached, ...(ran.end === "unmodelled" ? [] : ran.end.map((c) => c.base))])])];
120
+ if (movesToUnsettled(at.shift) || ran?.unsettled === true)
121
+ this.unsettledAt = true;
68
122
  return;
69
123
  }
70
124
  this.movedAt = si;
@@ -80,8 +134,11 @@ export class WorkdirThread {
80
134
  for (const c of this.candidates) {
81
135
  if (c.skippedCd < at.andRunStart) {
82
136
  const moved = this.movedFrom(c, at, ctx);
83
- if (moved === "lost")
84
- return this.lose(si, reachable, floorOf(s, reachable, ctx));
137
+ if (moved === "lost") {
138
+ if (movesToUnsettled(s))
139
+ this.unsettledAt = true;
140
+ return this.lose(si, reachable, floorOf(s, reachable.map((r) => r.base), ctx));
141
+ }
85
142
  for (const m of moved)
86
143
  branched.push({ ...m, skippedCd: c.skippedCd });
87
144
  }
@@ -103,9 +160,12 @@ export class WorkdirThread {
103
160
  movedFrom(c, at, ctx) {
104
161
  const s = at.shift;
105
162
  if (s.kind === "eval") {
106
- const ran = ctx.evalEnd(s.text, c, at.table);
163
+ const ran = ctx.evalEnd(s.text, [c], at.table);
107
164
  if (ran.physical)
108
165
  this.physical = true;
166
+ if (ran.unsettled === true)
167
+ this.unsettledAt = true;
168
+ this.reach(ran.reached);
109
169
  return ran.end === "unmodelled" ? "lost" : ran.end.map((e) => (this.physical ? withoutPwd(e) : e));
110
170
  }
111
171
  const old = tableSays("PWD", at, ctx) ? {} : c.pwd !== undefined ? { old: c.pwd } : {};
@@ -135,7 +195,7 @@ export class WorkdirThread {
135
195
  }
136
196
  }
137
197
  const withoutPwd = ({ pwd: _, ...c }) => c;
138
- function floorOf(s, reachable, ctx) {
198
+ function floorOf(s, from, ctx) {
139
199
  if (s.kind !== "cd")
140
200
  return [];
141
201
  if (s.target === "home")
@@ -143,7 +203,10 @@ function floorOf(s, reachable, ctx) {
143
203
  if (typeof s.target === "string")
144
204
  return [];
145
205
  const target = s.target;
146
- return [...(target.floorSpelled !== undefined ? [target.floorSpelled] : []), ...(target.expands === false ? reachable.flatMap((c) => resolveOperandLexically(c.base, target.spelled, ctx.homeDir) ?? []) : [])];
206
+ return [...(target.floorSpelled !== undefined ? [target.floorSpelled] : []), ...(target.expands === false ? from.flatMap((base) => { const at = resolveOperandSpellings(base, target.spelled, ctx.homeDir); return at === undefined ? [] : [at.spelled ?? at.folded]; }) : [])];
207
+ }
208
+ function movesToUnsettled(s) {
209
+ return s.kind === "cd" && typeof s.target !== "string" && s.target.expands !== false;
147
210
  }
148
211
  function targetsOf(target, c, at, ctx) {
149
212
  if (target === "home")
@@ -261,7 +324,29 @@ export function segmentMayMove(argv, functions) {
261
324
  const word = argv[pos.program.index];
262
325
  return shellWordsOf("base_mover").has(pos.program.basename) || TEXT_RUNNING_PROGRAMS.has(pos.program.basename) || functionCallOf(argv, functions) !== undefined || word.pieces.some((p) => p.expansion !== undefined || p.literal === undefined);
263
326
  }
264
- export function textMovesOf(defined, argvs, inFunctionBody, inherited) {
327
+ function segmentRunsBody(segment, argv, functions) {
328
+ const pos = readProgramPosition(argv);
329
+ const word = pos.program === undefined ? undefined : argv[pos.program.index];
330
+ const p = programOf(argv);
331
+ if (functionCallOf(argv, functions) !== undefined || (p !== undefined && codeBodies(p.name, p.args).length > 0))
332
+ return true;
333
+ if (word !== undefined && (TEXT_RUNNING_PROGRAMS.has(pos.program.basename) || word.pieces.some((q) => q.expansion !== undefined || q.literal === undefined)))
334
+ return true;
335
+ if (functions.size === 0)
336
+ return false;
337
+ const words = segment.tokens.flatMap((t) => (t.kind === "word" ? [t] : t.kind === "redirect" && t.target !== undefined ? [t.target] : []));
338
+ if (words.some((w) => w.unresolved === "parameter_text"))
339
+ return true;
340
+ const heredocs = segment.tokens.flatMap((t) => (t.kind === "heredoc" && !t.quoted ? [expansionsInHeredocBody(t.body)] : []));
341
+ if (heredocs.some((b) => b === undefined || b.some((x) => x.unresolved !== undefined)))
342
+ return true;
343
+ const texts = [...segment.substitutions, ...heredocs.flatMap((b) => (b ?? []).flatMap((x) => (x.expansion.kind === "command" ? [x.expansion.body ?? ""] : [])))];
344
+ return texts.some((text) => {
345
+ const lexed = lexShell(text);
346
+ return "parseFailure" in lexed || lexed.segments.some((s) => segmentRunsBody(s, argvOf(s), functions));
347
+ });
348
+ }
349
+ export function textMovesOf(defined, segments, argvs, inFunctionBody, inherited) {
265
350
  const unread = new Set([...evalDefinedFunctions(argvs), ...(inherited?.unread ?? [])]);
266
351
  const functions = new Set([...defined, ...unread, ...(inherited?.functions ?? [])]);
267
352
  const programs = argvs.map(programOf);
@@ -269,11 +354,10 @@ export function textMovesOf(defined, argvs, inFunctionBody, inherited) {
269
354
  const bodyMoves = mayMove.some((m, si) => m && inFunctionBody(si));
270
355
  const bodiesAt = programs.map((p) => (p === undefined ? [] : codeBodies(p.name, p.args)));
271
356
  const mayCallAt = argvs.map((argv) => functionCallOf(argv, functions) !== undefined);
272
- const runsTextAt = argvs.map((argv) => { const pos = readProgramPosition(argv); const word = pos.program === undefined ? undefined : argv[pos.program.index]; return word !== undefined && (["eval", "source", "."].includes(pos.program.basename) || word.pieces.some((p) => p.expansion !== undefined || p.literal === undefined)); });
273
357
  const callAt = (si, table) => functionCallOf(argvs[si], functions, (name) => table.functionState(name));
274
358
  const callsUnread = (si, table) => functionCallOf(argvs[si], unread, (name) => table.functionState(name)) !== undefined;
275
359
  return {
276
- functions, unread, mayMove, mayCallAt, runsBodyAt: mayCallAt.map((c, si) => c || runsTextAt[si] || bodiesAt[si].length > 0), bodiesAt, bodyMoves, callAt, callsUnread,
360
+ functions, unread, mayMove, mayCallAt, runsBodyAt: argvs.map((argv, si) => segmentRunsBody(segments[si], argv, functions)), bodiesAt, bodyMoves, callAt, callsUnread,
277
361
  physicalAt: programs.map(setsPhysical),
278
362
  cdableFrom: programs.findIndex((p) => p?.name === "shopt" && p.args.includes("-s") && p.args.includes("cdable_vars")),
279
363
  losesBaseAt: (si, table) => (callAt(si, table) !== undefined && bodyMoves) || callsUnread(si, table) || bodiesAt[si].some((b) => bodyMoves || textMayMove(b, functions)),
@@ -105,7 +105,7 @@ function writeTargetsOf(segment) {
105
105
  writeOnly.add(c);
106
106
  }
107
107
  for (const o of owners) {
108
- for (const c of copyDestinationChildren(segment, o.row, o.words, o.readings))
108
+ for (const c of copyDestinationChildren(segment, o.row, o.words, o.all))
109
109
  write(c, ["landing", "entry"], undefined, true);
110
110
  out.push(...backupTargetsOf(edited.get(o) ?? [], o.readings, o.override));
111
111
  }
@@ -134,23 +134,28 @@ function backupTargetsOf(edited, readings, override) {
134
134
  }
135
135
  function copyDestinationChildren(segment, row, words, readings) {
136
136
  const candidateOf = (raw) => segment.reads.find((c) => c.raw === raw && WORD_PROVENANCES.has(c.provenance));
137
+ const start = segment.programStart;
137
138
  const children = new Map();
138
139
  for (const r of readings) {
139
140
  if (r.copy === undefined)
140
141
  continue;
141
- const destWord = words[r.copy.destination.at];
142
- const dest = { raw: destWord.raw, spelled: r.copy.destination.value ?? destWord.spelled };
143
- const destCandidate = candidateOf(dest.raw);
142
+ const { destination, sources } = r.copy;
143
+ const destWord = destination !== undefined ? words[destination.at] : { raw: ".", spelled: "." };
144
+ const dest = { raw: destWord.raw, spelled: destination?.value ?? destWord.spelled };
145
+ const destCandidate = destination !== undefined ? candidateOf(dest.raw) : start;
144
146
  if (destCandidate !== undefined && destCandidate.expands !== false)
145
147
  continue;
146
- const destRole = r.written.find((w) => w.at === r.copy.destination.at)?.role;
148
+ const destRole = r.written.find((w) => w.at === destination?.at)?.role;
147
149
  if (destCandidate !== undefined && destRole !== undefined && passesUnasked({ ...destCandidate, face: "write", ...(writesThrough([destRole], row, words) ? { through: true } : {}) }))
148
150
  continue;
149
- for (const at of r.copy.sources) {
151
+ for (const at of sources) {
150
152
  const src = words[at];
151
153
  const source = candidateOf(src.raw);
152
- if (source !== undefined && source.expands !== false)
153
- continue;
154
+ if (source !== undefined && source.expands !== false) {
155
+ if (destination !== undefined)
156
+ continue;
157
+ children.set(`\0${source.raw}`, source);
158
+ }
154
159
  const child = `${dest.spelled.replace(/\/+$/, "")}/${src.spelled.replace(/\/+$/, "").split("/").pop() ?? ""}`;
155
160
  if (child.endsWith("/") || children.has(child))
156
161
  continue;
@@ -90,8 +90,9 @@ export declare function currentCheckoutOf(session: WorktreeSessionRef | undefine
90
90
  }): string;
91
91
  /** A leg's write-location seat (the EnterWorktree session; empty until the worktree mount seeds it) and its readers, minted
92
92
  * before any write-root reader: `writeLocation` ({@link currentCheckoutOf} over the seat) and `checkout` — the write location
93
- * when it is a managed tree (a switch, or a task root that IS one): its canonical identity and the spelling the leg's paths
94
- * carry it under, for the write-protection judge's current-tree reading (`WriteProtectionCompileOptions.checkoutOf`). */
93
+ * when it is in a managed tree (a switch, a task root that IS one, or a task root below one — a session's tree with the task
94
+ * root in `pkg/`): the tree's canonical identity and the spelling the leg's paths carry it under, for the write-protection
95
+ * judge's current-tree reading (`WriteProtectionCompileOptions.checkoutOf`). */
95
96
  export declare function writeLocationOf(taskRoot: {
96
97
  readonly spelled: string;
97
98
  readonly canonical: string;