@sema-agent/core 7.62.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 (193) hide show
  1. package/CHANGELOG.md +528 -0
  2. package/dist/agents/subagent-transcript-placement.d.ts +23 -0
  3. package/dist/agents/subagent-transcript-placement.js +14 -0
  4. package/dist/agents/subagent.js +82 -51
  5. package/dist/brain/route-adjudicator.js +9 -4
  6. package/dist/core/approval-object.js +1 -1
  7. package/dist/core/arg-summary.js +1 -1
  8. package/dist/core/ask-question.d.ts +2 -2
  9. package/dist/core/canonical-json.d.ts +23 -13
  10. package/dist/core/canonical-json.js +4 -4
  11. package/dist/core/checkpoint-queries.d.ts +47 -4
  12. package/dist/core/checkpoint-queries.js +36 -16
  13. package/dist/core/checkpoint-requirements.d.ts +3 -3
  14. package/dist/core/checkpoint-requirements.js +2 -1
  15. package/dist/core/checkpoint-row-verdict.d.ts +15 -4
  16. package/dist/core/checkpoint-row-verdict.js +22 -2
  17. package/dist/core/checkpoint-store.d.ts +23 -10
  18. package/dist/core/checkpoint-store.js +45 -4
  19. package/dist/core/compliance.d.ts +6 -2
  20. package/dist/core/compliance.js +3 -3
  21. package/dist/core/engine-attestation.d.ts +1 -1
  22. package/dist/core/engine-notice.d.ts +19 -4
  23. package/dist/core/git-worktree-env.d.ts +62 -3
  24. package/dist/core/git-worktree-env.js +91 -30
  25. package/dist/core/governance-codes.d.ts +7 -3
  26. package/dist/core/governance-codes.js +13 -3
  27. package/dist/core/memory-engine/consolidation-driver.d.ts +32 -17
  28. package/dist/core/memory-engine/consolidation-driver.js +99 -98
  29. package/dist/core/memory-engine/consolidation.d.ts +73 -10
  30. package/dist/core/memory-engine/consolidation.js +167 -11
  31. package/dist/core/memory-engine/data-plane.d.ts +8 -4
  32. package/dist/core/memory-engine/data-plane.js +11 -6
  33. package/dist/core/memory-engine/delegation-settlement.d.ts +37 -35
  34. package/dist/core/memory-engine/delegation-settlement.js +53 -59
  35. package/dist/core/memory-engine/dual-root.d.ts +12 -6
  36. package/dist/core/memory-engine/dual-root.js +9 -5
  37. package/dist/core/memory-engine/engine.d.ts +98 -35
  38. package/dist/core/memory-engine/engine.js +477 -380
  39. package/dist/core/memory-engine/entry-scan.d.ts +8 -0
  40. package/dist/core/memory-engine/entry-scan.js +3 -0
  41. package/dist/core/memory-engine/file-backend.d.ts +29 -3
  42. package/dist/core/memory-engine/file-backend.js +66 -35
  43. package/dist/core/memory-engine/index.d.ts +7 -7
  44. package/dist/core/memory-engine/index.js +6 -6
  45. package/dist/core/memory-engine/landing-admission.d.ts +5 -3
  46. package/dist/core/memory-engine/landing-admission.js +3 -3
  47. package/dist/core/memory-engine/layout.d.ts +121 -45
  48. package/dist/core/memory-engine/layout.js +252 -102
  49. package/dist/core/memory-engine/memory-backend-contract.d.ts +19 -1
  50. package/dist/core/memory-engine/memory-backend-contract.js +320 -0
  51. package/dist/core/memory-engine/memory-deployment.d.ts +31 -8
  52. package/dist/core/memory-engine/memory-deployment.js +26 -12
  53. package/dist/core/memory-engine/scope-contract.d.ts +52 -11
  54. package/dist/core/memory-engine/scope-contract.js +68 -19
  55. package/dist/core/memory-engine/session-write-home.d.ts +3 -1
  56. package/dist/core/memory-engine/session-write-home.js +1 -1
  57. package/dist/core/memory-engine/sync-client.d.ts +3 -1
  58. package/dist/core/memory-engine/sync-client.js +7 -0
  59. package/dist/core/memory-engine/sync-report.d.ts +3 -2
  60. package/dist/core/memory-engine/sync-report.js +6 -2
  61. package/dist/core/memory-engine/tools.d.ts +2 -2
  62. package/dist/core/memory-engine/tools.js +10 -10
  63. package/dist/core/memory-engine/types.d.ts +118 -19
  64. package/dist/core/memory-engine/types.js +1 -0
  65. package/dist/core/memory.js +15 -6
  66. package/dist/core/mods/stations.d.ts +20 -1
  67. package/dist/core/mods/stations.js +21 -2
  68. package/dist/core/park-selfcheck.js +2 -2
  69. package/dist/core/pause-registry.d.ts +2 -0
  70. package/dist/core/pause-registry.js +3 -0
  71. package/dist/core/permission-rule-consent.d.ts +10 -3
  72. package/dist/core/permission-rule-consent.js +37 -24
  73. package/dist/core/permission-rule-store.d.ts +35 -1
  74. package/dist/core/permission-rule-store.js +27 -8
  75. package/dist/core/read-only-shell.js +3 -2
  76. package/dist/core/redaction-marks.d.ts +8 -0
  77. package/dist/core/redaction-marks.js +6 -0
  78. package/dist/core/remote-env.d.ts +12 -1
  79. package/dist/core/remote-env.js +3 -0
  80. package/dist/core/runner/child-tree.d.ts +29 -3
  81. package/dist/core/runner/child-tree.js +39 -16
  82. package/dist/core/runner/contracts.d.ts +18 -9
  83. package/dist/core/runner/gate-exit.js +2 -2
  84. package/dist/core/runner/inherited-ask-grants.js +1 -1
  85. package/dist/core/runner/memory-consolidation-driver.d.ts +6 -3
  86. package/dist/core/runner/memory-consolidation-driver.js +4 -4
  87. package/dist/core/runner/memory-consolidation-write-judge.d.ts +2 -1
  88. package/dist/core/runner/memory-consolidation-write-judge.js +3 -2
  89. package/dist/core/runner/person-path-rows.d.ts +8 -3
  90. package/dist/core/runner/person-path-rows.js +5 -1
  91. package/dist/core/runner/placement-fence.d.ts +12 -0
  92. package/dist/core/runner/placement-fence.js +20 -6
  93. package/dist/core/runner/prepare-ask-lane.js +2 -2
  94. package/dist/core/runner/prepare-caps-and-workflow.d.ts +4 -1
  95. package/dist/core/runner/prepare-caps-and-workflow.js +5 -2
  96. package/dist/core/runner/prepare-execution-env.js +17 -6
  97. package/dist/core/runner/prepare-hands-readface.d.ts +2 -1
  98. package/dist/core/runner/prepare-hands-readface.js +4 -3
  99. package/dist/core/runner/prepare-inherited-gate.d.ts +10 -8
  100. package/dist/core/runner/prepare-inherited-gate.js +8 -4
  101. package/dist/core/runner/prepare-memory-engine-session.d.ts +2 -4
  102. package/dist/core/runner/prepare-memory-engine-session.js +4 -4
  103. package/dist/core/runner/prepare-memory.d.ts +3 -2
  104. package/dist/core/runner/prepare-memory.js +28 -20
  105. package/dist/core/runner/prepare-park-ask.js +4 -3
  106. package/dist/core/runner/prepare-task.js +15 -12
  107. package/dist/core/runner/prepare-workspace-restore.js +3 -6
  108. package/dist/core/runner/run-leg.js +1 -1
  109. package/dist/core/runner/run-settle-and-teardown.js +3 -2
  110. package/dist/core/runner/terminal-account.js +1 -1
  111. package/dist/core/runner-deps.d.ts +6 -2
  112. package/dist/core/secret-env.d.ts +1 -1
  113. package/dist/core/secret-env.js +4 -4
  114. package/dist/core/session-checkout.d.ts +108 -0
  115. package/dist/core/session-checkout.js +485 -0
  116. package/dist/core/session-store.d.ts +12 -4
  117. package/dist/core/session-store.js +23 -3
  118. package/dist/core/session.d.ts +16 -2
  119. package/dist/core/session.js +3 -0
  120. package/dist/core/store-contracts/checkpoint-store-contract.d.ts +0 -12
  121. package/dist/core/store-contracts/checkpoint-store-contract.js +6 -7
  122. package/dist/core/store-contracts/session-store-contract.js +39 -0
  123. package/dist/core/task-event.d.ts +21 -0
  124. package/dist/core/task-notification.d.ts +9 -1
  125. package/dist/core/task-notification.js +6 -0
  126. package/dist/core/task-registry-agent.d.ts +14 -2
  127. package/dist/core/task-registry-agent.js +45 -19
  128. package/dist/core/task-registry-shared.d.ts +26 -11
  129. package/dist/core/task-registry.d.ts +15 -9
  130. package/dist/core/task-registry.js +9 -10
  131. package/dist/core/task-spec-members.js +1 -1
  132. package/dist/core/task-spec.d.ts +20 -0
  133. package/dist/core/tool-policy.d.ts +14 -12
  134. package/dist/core/tool-policy.js +11 -10
  135. package/dist/core/tool-spec.d.ts +38 -19
  136. package/dist/core/types.d.ts +1 -1
  137. package/dist/core/untrusted-egress.d.ts +15 -4
  138. package/dist/core/untrusted-egress.js +23 -22
  139. package/dist/core/workspace-snapshot.d.ts +3 -1
  140. package/dist/core/workspace-snapshot.js +1 -0
  141. package/dist/engine/harness/types.d.ts +66 -1
  142. package/dist/engine/harness/types.js +108 -0
  143. package/dist/engine/session/import-validate.js +6 -1
  144. package/dist/engine/session/session.d.ts +24 -1
  145. package/dist/engine/session/session.js +45 -1
  146. package/dist/index.d.ts +7 -4
  147. package/dist/index.js +7 -4
  148. package/dist/internal/harness-types.d.ts +1 -1
  149. package/dist/orchestration/run-workflow-tool.d.ts +4 -1
  150. package/dist/orchestration/run-workflow-tool.js +1 -0
  151. package/dist/orchestration/workflow.d.ts +3 -0
  152. package/dist/orchestration/workflow.js +3 -1
  153. package/dist/stores/file/session-store.js +2 -1
  154. package/dist/tools/fs/bash-program-position.js +3 -3
  155. package/dist/tools/fs/program-read-model.d.ts +34 -10
  156. package/dist/tools/fs/program-read-model.js +37 -7
  157. package/dist/tools/fs/program-read-model.rows.js +13 -2
  158. package/dist/tools/fs/read-deny.d.ts +13 -1
  159. package/dist/tools/fs/read-deny.js +10 -2
  160. package/dist/tools/fs/removal-targets.js +3 -3
  161. package/dist/tools/fs/safety.d.ts +2 -0
  162. package/dist/tools/fs/safety.js +1 -1
  163. package/dist/tools/fs/shell-read-argv.d.ts +8 -1
  164. package/dist/tools/fs/shell-read-argv.js +34 -29
  165. package/dist/tools/fs/shell-read-bindings.d.ts +2 -0
  166. package/dist/tools/fs/shell-read-bindings.js +5 -1
  167. package/dist/tools/fs/shell-read-brace.d.ts +18 -0
  168. package/dist/tools/fs/shell-read-brace.js +79 -0
  169. package/dist/tools/fs/shell-read-check.js +10 -8
  170. package/dist/tools/fs/shell-read-establish.js +23 -16
  171. package/dist/tools/fs/shell-read-judge.d.ts +11 -0
  172. package/dist/tools/fs/shell-read-judge.js +3 -3
  173. package/dist/tools/fs/shell-read-plan.d.ts +10 -0
  174. package/dist/tools/fs/shell-read-plan.js +189 -93
  175. package/dist/tools/fs/shell-read-start.d.ts +19 -9
  176. package/dist/tools/fs/shell-read-start.js +20 -12
  177. package/dist/tools/fs/shell-read-word.d.ts +10 -0
  178. package/dist/tools/fs/shell-read-word.js +9 -5
  179. package/dist/tools/fs/shell-read-workdir.d.ts +26 -7
  180. package/dist/tools/fs/shell-read-workdir.js +73 -12
  181. package/dist/tools/fs/shell-read-write-targets.js +13 -8
  182. package/dist/tools/worktree.d.ts +3 -2
  183. package/dist/tools/worktree.js +7 -35
  184. package/package.json +3 -2
  185. package/test/export-surface.snapshot.json +73 -11
  186. package/wire-changes/core-stores-sql@7.63.0.json +30 -0
  187. package/wire-changes/core@7.63.0.json +3523 -0
  188. package/wire-changes/mods-kernel@7.63.0.json +30 -0
  189. package/wire-changes/primitives@7.63.0.json +445 -0
  190. package/dist/core/credential-vocabulary.d.ts +0 -176
  191. package/dist/core/credential-vocabulary.js +0 -190
  192. package/dist/core/redaction-passes.d.ts +0 -82
  193. package/dist/core/redaction-passes.js +0 -377
@@ -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";
@@ -14,7 +14,7 @@
14
14
  import type { Bare } from "./shell-read-argv.js";
15
15
  import type { BindingTable, WorkingDirectoryState } from "./shell-read-bindings.js";
16
16
  import type { Read } from "./shell-read-word.js";
17
- import { type LexedWord } from "../../shell/lex.js";
17
+ import { type LexedSegment, type LexedWord } from "../../shell/lex.js";
18
18
  import { type FunctionCall } from "./shell-read-functions.js";
19
19
  /** A working directory the shell could be standing in, with the latest move this candidate assumes did NOT run
20
20
  * (`skippedCd`), and what the shell holds there: `pwd` — what `PWD` spells (absent: not the base's spelling — a physical
@@ -45,14 +45,16 @@ export type BaseShift = {
45
45
  };
46
46
  /** What the walk asks of the plan around it: the declared home (a `~` target), whether the text binds a name anywhere
47
47
  * (`OLDPWD` — then `cd -` reads the table's value; `PWD` — then a move's `OLDPWD` is not the base the candidate stood
48
- * 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`). */
49
50
  export interface WorkdirContext {
50
51
  readonly homeDir?: string;
51
52
  readonly textBinds: (name: string) => boolean;
52
- readonly evalEnd: (text: string, from: BaseCandidate, table: BindingTable) => {
53
+ readonly evalEnd: (text: string, from: readonly BaseCandidate[] | "unmodelled", table: BindingTable) => {
53
54
  readonly end: readonly BaseCandidate[] | "unmodelled";
54
55
  readonly physical: boolean;
55
56
  readonly reached: ReadonlySet<string>;
57
+ readonly unsettled?: true;
56
58
  };
57
59
  }
58
60
  /** One segment's move as the walk applies it: the shift, whether an `&&` run proves it ran, the first segment of the
@@ -83,6 +85,13 @@ export declare class WorkdirThread {
83
85
  /** The physical mode may already be on where the text starts (an `eval` text run in a shell that set it). */
84
86
  startsPhysical?: boolean);
85
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;
86
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;
88
97
  private closedFloors;
@@ -106,9 +115,18 @@ export declare class WorkdirThread {
106
115
  private scopeReached;
107
116
  get reached(): ReadonlySet<string>;
108
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;
109
126
  /** Stop placing anything from segment `at` on: the reachable bases (and a target's floor spellings, the places a closed
110
- * scope went) become the deny floor. */
111
- lose(at: number, reachable: readonly BaseCandidate[], floorTargets?: readonly string[]): void;
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;
112
130
  /** Apply segment `si`'s move. */
113
131
  shift(si: number, at: ShiftAt, ctx: WorkdirContext): void;
114
132
  /** Where one candidate goes: the places (the CDPATH entries a bare target is searched through, then the target itself),
@@ -158,7 +176,8 @@ export interface TextMoves {
158
176
  readonly mayMove: readonly boolean[];
159
177
  /** Segment `si` may call a function (read before the walk, to bound where bodies run). */
160
178
  readonly mayCallAt: readonly boolean[];
161
- /** 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). */
162
181
  readonly runsBodyAt: readonly boolean[];
163
182
  readonly bodiesAt: readonly (readonly string[])[];
164
183
  readonly physicalAt: readonly boolean[];
@@ -171,7 +190,7 @@ export interface TextMoves {
171
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. */
172
191
  losesBaseAt(si: number, table: BindingTable): boolean;
173
192
  }
174
- 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?: {
175
194
  readonly functions: ReadonlySet<string>;
176
195
  readonly unread: ReadonlySet<string>;
177
196
  }): TextMoves;
@@ -1,4 +1,4 @@
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
3
  import { resolveOperandLexically, resolveOperandSpellings } from "./bash-readonly-classifier.js";
4
4
  import { readProgramPosition } from "./bash-program-position.js";
@@ -26,6 +26,10 @@ export class WorkdirThread {
26
26
  get lost() {
27
27
  return this.lostAt;
28
28
  }
29
+ get unsettled() {
30
+ return this.unsettledAt;
31
+ }
32
+ unsettledAt = false;
29
33
  get floorBases() {
30
34
  return this.closedFloors.length === 0 ? this.floors : [...new Set([...(this.floors ?? []), ...this.closedFloors])];
31
35
  }
@@ -78,15 +82,43 @@ export class WorkdirThread {
78
82
  reach(bases) {
79
83
  this.scopeReached = new Set([...this.scopeReached, ...bases]);
80
84
  }
81
- lose(at, reachable, floorTargets = []) {
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) {
82
103
  this.movedAt = Math.max(this.movedAt, at);
83
104
  this.lostAt = true;
105
+ if (unsettled)
106
+ this.unsettledAt = true;
84
107
  this.floors ??= [...new Set([...reachable.map((c) => c.base), ...floorTargets, ...this.scopeReached])];
85
108
  }
86
109
  shift(si, at, ctx) {
87
110
  if (this.lostAt) {
88
- if (this.floors !== undefined && this.floors.length > 0)
89
- 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;
90
122
  return;
91
123
  }
92
124
  this.movedAt = si;
@@ -102,8 +134,11 @@ export class WorkdirThread {
102
134
  for (const c of this.candidates) {
103
135
  if (c.skippedCd < at.andRunStart) {
104
136
  const moved = this.movedFrom(c, at, ctx);
105
- if (moved === "lost")
106
- 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
+ }
107
142
  for (const m of moved)
108
143
  branched.push({ ...m, skippedCd: c.skippedCd });
109
144
  }
@@ -125,9 +160,11 @@ export class WorkdirThread {
125
160
  movedFrom(c, at, ctx) {
126
161
  const s = at.shift;
127
162
  if (s.kind === "eval") {
128
- const ran = ctx.evalEnd(s.text, c, at.table);
163
+ const ran = ctx.evalEnd(s.text, [c], at.table);
129
164
  if (ran.physical)
130
165
  this.physical = true;
166
+ if (ran.unsettled === true)
167
+ this.unsettledAt = true;
131
168
  this.reach(ran.reached);
132
169
  return ran.end === "unmodelled" ? "lost" : ran.end.map((e) => (this.physical ? withoutPwd(e) : e));
133
170
  }
@@ -158,7 +195,7 @@ export class WorkdirThread {
158
195
  }
159
196
  }
160
197
  const withoutPwd = ({ pwd: _, ...c }) => c;
161
- function floorOf(s, reachable, ctx) {
198
+ function floorOf(s, from, ctx) {
162
199
  if (s.kind !== "cd")
163
200
  return [];
164
201
  if (s.target === "home")
@@ -166,7 +203,10 @@ function floorOf(s, reachable, ctx) {
166
203
  if (typeof s.target === "string")
167
204
  return [];
168
205
  const target = s.target;
169
- return [...(target.floorSpelled !== undefined ? [target.floorSpelled] : []), ...(target.expands === false ? reachable.flatMap((c) => { const at = resolveOperandSpellings(c.base, target.spelled, ctx.homeDir); return at === undefined ? [] : [at.spelled ?? at.folded]; }) : [])];
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;
170
210
  }
171
211
  function targetsOf(target, c, at, ctx) {
172
212
  if (target === "home")
@@ -284,7 +324,29 @@ export function segmentMayMove(argv, functions) {
284
324
  const word = argv[pos.program.index];
285
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);
286
326
  }
287
- 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) {
288
350
  const unread = new Set([...evalDefinedFunctions(argvs), ...(inherited?.unread ?? [])]);
289
351
  const functions = new Set([...defined, ...unread, ...(inherited?.functions ?? [])]);
290
352
  const programs = argvs.map(programOf);
@@ -292,11 +354,10 @@ export function textMovesOf(defined, argvs, inFunctionBody, inherited) {
292
354
  const bodyMoves = mayMove.some((m, si) => m && inFunctionBody(si));
293
355
  const bodiesAt = programs.map((p) => (p === undefined ? [] : codeBodies(p.name, p.args)));
294
356
  const mayCallAt = argvs.map((argv) => functionCallOf(argv, functions) !== undefined);
295
- 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)); });
296
357
  const callAt = (si, table) => functionCallOf(argvs[si], functions, (name) => table.functionState(name));
297
358
  const callsUnread = (si, table) => functionCallOf(argvs[si], unread, (name) => table.functionState(name)) !== undefined;
298
359
  return {
299
- 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,
300
361
  physicalAt: programs.map(setsPhysical),
301
362
  cdableFrom: programs.findIndex((p) => p?.name === "shopt" && p.args.includes("-s") && p.args.includes("cdable_vars")),
302
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;
@@ -1,7 +1,7 @@
1
1
  import { resolve } from "node:path";
2
2
  import { Type } from "typebox";
3
3
  import { defineTool, errorResult } from "../core/tools.js";
4
- import { addWorktree, managedWorktreeBaseOf, pruneWorktrees, removeManagedWorktree, worktreeNameProblem, WORKTREE_PARENT } from "../core/git-worktree-env.js";
4
+ import { addWorktree, managedTreeAt, managedWorktreeBaseOf, pruneWorktrees, removeManagedWorktree, resolveBaseCommit, worktreeNameProblem, WORKTREE_PARENT } from "../core/git-worktree-env.js";
5
5
  import { mountRuleFace, toolFace } from "../core/tool-catalog-entries.js";
6
6
  import { thrownText } from "../core/thrown-value.js";
7
7
  export function currentCheckoutOf(session, taskRoot) {
@@ -18,10 +18,10 @@ export function writeLocationOf(taskRoot) {
18
18
  const writeLocation = () => currentCheckoutOf(seat, taskRoot);
19
19
  const checkout = () => {
20
20
  const views = [];
21
- for (const [root, kind] of [[writeLocation(), "canonical"], [seat.current?.worktreeDir ?? taskRoot.spelled, "spelled"]]) {
22
- const base = managedWorktreeBaseOf(root);
23
- if (base !== undefined)
24
- views.push({ root, base, kind });
21
+ for (const [at, kind] of [[writeLocation(), "canonical"], [seat.current?.worktreeDir ?? taskRoot.spelled, "spelled"]]) {
22
+ const tree = managedTreeAt(at);
23
+ if (tree !== undefined)
24
+ views.push({ root: tree.root, base: tree.base, kind });
25
25
  }
26
26
  return views.length > 0 ? views : undefined;
27
27
  };
@@ -105,34 +105,6 @@ function reasonOf(e) {
105
105
  }
106
106
  return thrownText(e);
107
107
  }
108
- async function resolveBaseCommit(env, repoRoot, baseRef, readFloor) {
109
- const rev = async (ref) => {
110
- const r = await env.exec(`git rev-parse --verify --quiet ${shq(`${ref}^{commit}`)}`, { cwd: repoRoot, ...(readFloor !== undefined ? { readFloor } : {}) });
111
- return r.ok && r.value.exitCode === 0 ? r.value.stdout.trim() : undefined;
112
- };
113
- if (baseRef === "head") {
114
- const sha = await rev("HEAD");
115
- return sha ? { sha, label: "HEAD" } : { error: "cannot resolve HEAD (empty repository with no commits?)" };
116
- }
117
- const sym = await env.exec("git symbolic-ref --quiet refs/remotes/origin/HEAD", { cwd: repoRoot, ...(readFloor !== undefined ? { readFloor } : {}) });
118
- if (sym.ok && sym.value.exitCode === 0) {
119
- const branch = sym.value.stdout.trim().replace(/^refs\/remotes\//, "");
120
- if (branch) {
121
- const sha = await rev(branch);
122
- if (sha)
123
- return { sha, label: branch };
124
- }
125
- }
126
- for (const candidate of ["main", "master"]) {
127
- const sha = await rev(candidate);
128
- if (sha)
129
- return { sha, label: candidate };
130
- }
131
- const sha = await rev("HEAD");
132
- return sha
133
- ? { sha, label: "HEAD", note: " (no remote default branch or local main/master found — fell back to HEAD)" }
134
- : { error: "cannot resolve a fresh base (no default branch and no HEAD)" };
135
- }
136
108
  export function createWorktreeTools(env, opts) {
137
109
  const { repoRoot, cwdRef } = opts;
138
110
  const session = opts.session ?? {};
@@ -207,7 +179,7 @@ export function createWorktreeTools(env, opts) {
207
179
  }
208
180
  const rootCanon = await env.canonicalPath(repoRoot);
209
181
  const fence = rootCanon.ok ? rootCanon.value : repoRoot;
210
- if (target !== fence && !target.startsWith(fence.endsWith("/") ? fence : `${fence}/`)) {
182
+ if (target !== fence && !target.startsWith(fence.endsWith("/") ? fence : `${fence}/`) && managedWorktreeBaseOf(target) === undefined) {
211
183
  return errorResult(`Error (EnterWorktree): the worktree ${target} lives outside the task root ${repoRoot}; ` +
212
184
  "read containment stays anchored at the task root, so it cannot be entered from this session.");
213
185
  }
@@ -412,7 +384,7 @@ export function createWorktreeTools(env, opts) {
412
384
  details: { type: "worktree", action: "exit", worktreeDir: active.worktreeDir, kept: true },
413
385
  };
414
386
  }
415
- const removal = await removeManagedWorktree(env, repoRoot, active.worktreeDir, active.branch !== undefined ? { name: active.branch, baseSha: active.baseSha } : undefined, () => readFloor);
387
+ const removal = await removeManagedWorktree(env, repoRoot, active.worktreeDir, active.branch !== undefined ? { name: active.branch, baseSha: active.baseSha } : undefined, () => readFloor, "discard");
416
388
  const removeFailure = removal.ok ? undefined : removal.detail.trim();
417
389
  const branchNote = !removal.ok ? "" : removal.branchKept !== undefined ? ` Its branch was kept: ${removal.branchKept}.` : removal.branchDeleted !== undefined ? ` Its branch ${removal.branchDeleted} was deleted.` : "";
418
390
  const pruned = await pruneWorktrees(env, repoRoot, readFloor);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sema-agent/core",
3
- "version": "7.62.0",
3
+ "version": "7.63.0",
4
4
  "description": "Stateless, task-oriented AI agent core",
5
5
  "type": "module",
6
6
  "license": "BUSL-1.1",
@@ -124,7 +124,7 @@
124
124
  "dependencies": {
125
125
  "@modelcontextprotocol/client": "2.4.0",
126
126
  "@modelcontextprotocol/core": "2.4.0",
127
- "@sema-agent/primitives": "7.62.0",
127
+ "@sema-agent/primitives": "7.63.0",
128
128
  "typebox": "1.1.39",
129
129
  "undici": "7.30.0"
130
130
  },
@@ -135,6 +135,7 @@
135
135
  "@modelcontextprotocol/server": "2.4.0",
136
136
  "@types/node": "22.10.2",
137
137
  "@types/pg": "^8.20.0",
138
+ "mysql2": "^3.22.4",
138
139
  "pg": "^8.22.0",
139
140
  "pg-mem": "^3.0.14",
140
141
  "tsx": "4.19.2",