@deftai/directive-core 0.107.0 → 0.108.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 (167) hide show
  1. package/dist/architecture/sor-preflight.js +2 -0
  2. package/dist/check/cached-orchestrator.js +28 -3
  3. package/dist/check/named-cause.js +18 -3
  4. package/dist/codebase/provider.js +7 -1
  5. package/dist/delivery-attempt/handoff.js +1 -1
  6. package/dist/design-critique/citation-grammar.d.ts +53 -0
  7. package/dist/design-critique/citation-grammar.js +303 -0
  8. package/dist/design-critique/completed-arc-record.d.ts +62 -0
  9. package/dist/design-critique/completed-arc-record.js +232 -0
  10. package/dist/freshness/bind.d.ts +32 -3
  11. package/dist/freshness/bind.js +58 -12
  12. package/dist/hooks/classify/host-session-identity.d.ts +76 -0
  13. package/dist/hooks/classify/host-session-identity.js +470 -0
  14. package/dist/hooks/classify/index.d.ts +3 -2
  15. package/dist/hooks/classify/index.js +3 -2
  16. package/dist/hooks/classify/paths.d.ts +7 -0
  17. package/dist/hooks/classify/paths.js +35 -0
  18. package/dist/hooks/classify/stdin.d.ts +5 -0
  19. package/dist/hooks/classify/stdin.js +27 -0
  20. package/dist/hooks/dispatcher.d.ts +52 -1
  21. package/dist/hooks/dispatcher.js +538 -45
  22. package/dist/hooks/fixtures/cases.d.ts +16 -2
  23. package/dist/hooks/fixtures/cases.js +175 -0
  24. package/dist/hooks/scope.js +3 -1
  25. package/dist/init-deposit/agent-hooks.d.ts +2 -1
  26. package/dist/init-deposit/agent-hooks.js +8 -1
  27. package/dist/init-deposit/hook-runtime-travel.d.ts +77 -0
  28. package/dist/init-deposit/hook-runtime-travel.js +269 -0
  29. package/dist/intake/clause-derivation.js +9 -3
  30. package/dist/intake/issue-ingest.js +62 -2
  31. package/dist/intake/platform-capabilities.d.ts +9 -2
  32. package/dist/intake/platform-capabilities.js +38 -9
  33. package/dist/orphan-active/evaluate.d.ts +25 -0
  34. package/dist/orphan-active/evaluate.js +193 -128
  35. package/dist/orphan-active/index.d.ts +1 -0
  36. package/dist/orphan-active/index.js +1 -0
  37. package/dist/orphan-active/issue-state.d.ts +121 -0
  38. package/dist/orphan-active/issue-state.js +278 -0
  39. package/dist/platform/cursor-managed-runtime.d.ts +95 -0
  40. package/dist/platform/cursor-managed-runtime.js +241 -0
  41. package/dist/platform/index.d.ts +1 -0
  42. package/dist/platform/index.js +1 -0
  43. package/dist/platform/platform-capabilities.d.ts +23 -1
  44. package/dist/platform/platform-capabilities.js +55 -15
  45. package/dist/policy/ceremony-dial.js +5 -9
  46. package/dist/policy/host-hooks.js +4 -9
  47. package/dist/policy/org-force-on-migration.js +5 -10
  48. package/dist/policy/plan-extensions.d.ts +14 -2
  49. package/dist/policy/plan-extensions.js +24 -3
  50. package/dist/policy/product-signal.js +5 -10
  51. package/dist/policy/require-human-merge.js +5 -9
  52. package/dist/policy/resolve.js +77 -16
  53. package/dist/policy/value-feedback.js +9 -18
  54. package/dist/pr-closeout-attestable/evaluate.d.ts +80 -0
  55. package/dist/pr-closeout-attestable/evaluate.js +306 -0
  56. package/dist/pr-closeout-attestable/index.d.ts +2 -0
  57. package/dist/pr-closeout-attestable/index.js +2 -0
  58. package/dist/pr-protected-issues/gh.d.ts +6 -2
  59. package/dist/pr-protected-issues/gh.js +7 -3
  60. package/dist/pr-wait-mergeable/cascade.d.ts +9 -1
  61. package/dist/pr-wait-mergeable/cascade.js +21 -1
  62. package/dist/pr-wait-mergeable/types.d.ts +2 -0
  63. package/dist/pr-wait-mergeable/wrappers.d.ts +6 -0
  64. package/dist/pr-wait-mergeable/wrappers.js +11 -0
  65. package/dist/preflight/evaluate.d.ts +15 -0
  66. package/dist/preflight/evaluate.js +29 -3
  67. package/dist/product-first-done-gate/acceptance-resolver.d.ts +14 -2
  68. package/dist/product-first-done-gate/acceptance-resolver.js +13 -2
  69. package/dist/product-first-done-gate/evaluate.js +17 -7
  70. package/dist/release/cli-drift-report.d.ts +68 -0
  71. package/dist/release/cli-drift-report.js +189 -0
  72. package/dist/release/index.d.ts +1 -0
  73. package/dist/release/index.js +1 -0
  74. package/dist/release/issue-state-fetch.d.ts +5 -3
  75. package/dist/release/issue-state-fetch.js +86 -21
  76. package/dist/release/native-steps.js +1 -0
  77. package/dist/release/pipeline.js +19 -0
  78. package/dist/release/types.d.ts +14 -0
  79. package/dist/render/constants.d.ts +4 -0
  80. package/dist/render/constants.js +11 -8
  81. package/dist/render/export-spec.js +31 -5
  82. package/dist/render/index.d.ts +1 -1
  83. package/dist/render/index.js +1 -1
  84. package/dist/render/prd-render.d.ts +4 -1
  85. package/dist/render/prd-render.js +82 -23
  86. package/dist/render/project-render.js +14 -7
  87. package/dist/render/scope-outlook.d.ts +2 -0
  88. package/dist/render/scope-outlook.js +3 -0
  89. package/dist/render/spec-render.js +2 -2
  90. package/dist/resolution/package-manager.d.ts +33 -4
  91. package/dist/resolution/package-manager.js +210 -9
  92. package/dist/review-monitor/constants.d.ts +7 -0
  93. package/dist/review-monitor/constants.js +7 -0
  94. package/dist/review-monitor/github-lease.d.ts +98 -1
  95. package/dist/review-monitor/github-lease.js +182 -1
  96. package/dist/review-monitor/lease-comment.d.ts +36 -0
  97. package/dist/review-monitor/lease-comment.js +103 -2
  98. package/dist/scm/call.js +12 -3
  99. package/dist/scm/design-critique-chip.d.ts +1 -0
  100. package/dist/scm/design-critique-chip.js +22 -6
  101. package/dist/scm/gh-rest.d.ts +9 -1
  102. package/dist/scm/gh-rest.js +103 -0
  103. package/dist/scm/readiness.d.ts +2 -0
  104. package/dist/scm/readiness.js +28 -4
  105. package/dist/scope/project-definition-sync.js +14 -7
  106. package/dist/session/git.d.ts +24 -0
  107. package/dist/session/git.js +138 -2
  108. package/dist/session/occupancy.d.ts +238 -12
  109. package/dist/session/occupancy.js +815 -29
  110. package/dist/session/ritual-sentinel.d.ts +5 -0
  111. package/dist/session/ritual-sentinel.js +5 -0
  112. package/dist/session/session-ready.d.ts +5 -1
  113. package/dist/session/session-ready.js +98 -8
  114. package/dist/session/session-start.d.ts +5 -1
  115. package/dist/session/session-start.js +32 -17
  116. package/dist/session/toolchain-preflight.d.ts +13 -3
  117. package/dist/session/toolchain-preflight.js +93 -18
  118. package/dist/session/verify-session-ritual.d.ts +11 -0
  119. package/dist/session/verify-session-ritual.js +60 -15
  120. package/dist/slice/constants.d.ts +1 -1
  121. package/dist/spec-authority/constants.d.ts +10 -2
  122. package/dist/spec-authority/constants.js +53 -8
  123. package/dist/spec-authority/resolver.d.ts +3 -0
  124. package/dist/spec-authority/resolver.js +55 -7
  125. package/dist/subprocess/max-buffer.d.ts +14 -0
  126. package/dist/subprocess/max-buffer.js +15 -0
  127. package/dist/swarm/complete-cohort.d.ts +2 -0
  128. package/dist/swarm/complete-cohort.js +21 -13
  129. package/dist/swarm/index.d.ts +1 -0
  130. package/dist/swarm/index.js +1 -0
  131. package/dist/swarm/launch-cli.js +53 -33
  132. package/dist/swarm/launch.d.ts +8 -0
  133. package/dist/swarm/launch.js +63 -43
  134. package/dist/swarm/pre-dispatch-cli.js +2 -1
  135. package/dist/swarm/pre-dispatch.js +10 -1
  136. package/dist/swarm/subagent-status-dir.d.ts +27 -0
  137. package/dist/swarm/subagent-status-dir.js +42 -0
  138. package/dist/swarm/worktrees.d.ts +17 -3
  139. package/dist/swarm/worktrees.js +48 -4
  140. package/dist/triage/scope/mutations-core.d.ts +2 -6
  141. package/dist/triage/scope/mutations-core.js +23 -55
  142. package/dist/triage/scope-drift/add-ignore.js +5 -39
  143. package/dist/triage/subscribe/index.d.ts +2 -6
  144. package/dist/triage/subscribe/index.js +15 -65
  145. package/dist/triage/welcome/writers.js +15 -74
  146. package/dist/vbrief-build/index.d.ts +2 -1
  147. package/dist/vbrief-build/index.js +2 -1
  148. package/dist/vbrief-build/parity-scenarios.js +5 -5
  149. package/dist/vbrief-build/project-definition-io.d.ts +63 -3
  150. package/dist/vbrief-build/project-definition-io.js +357 -31
  151. package/dist/vbrief-build/project-definition-mutation.d.ts +32 -0
  152. package/dist/vbrief-build/project-definition-mutation.js +43 -0
  153. package/dist/vbrief-validate/conformance.d.ts +1 -0
  154. package/dist/vbrief-validate/conformance.js +85 -8
  155. package/dist/vbrief-validate/main.js +2 -0
  156. package/dist/vbrief-validate/precutover.js +5 -10
  157. package/dist/verify-ac/clauses.d.ts +62 -2
  158. package/dist/verify-ac/clauses.js +183 -111
  159. package/dist/verify-ac/index.d.ts +1 -1
  160. package/dist/verify-ac/index.js +1 -1
  161. package/dist/verify-env/node-runtime.d.ts +8 -4
  162. package/dist/verify-env/node-runtime.js +9 -6
  163. package/dist/verify-env/toolchain-check.d.ts +35 -3
  164. package/dist/verify-env/toolchain-check.js +155 -36
  165. package/dist/verify-source/project-definition-mutation-boundary.d.ts +104 -0
  166. package/dist/verify-source/project-definition-mutation-boundary.js +326 -0
  167. package/package.json +7 -3
@@ -0,0 +1,470 @@
1
+ /**
2
+ * Cooperative host-session identity and exact lifecycle-command rewriting (#3611).
3
+ *
4
+ * This is payload classification, not authentication. Host hook JSON is locally
5
+ * forgeable under the occupancy model's cooperating-process assumption.
6
+ */
7
+ import { isShellTool } from "../tools.js";
8
+ import { record, toolInputRecord } from "./payload.js";
9
+ import { hookToolName } from "./tool-name.js";
10
+ export const HOST_IDENTITY_PROVIDERS = ["codex", "claude", "cursor"];
11
+ export const MAX_HOOK_HOST_IDENTITY_UTF8_BYTES = 512;
12
+ function hasControlCharacter(value) {
13
+ for (let index = 0; index < value.length; index += 1) {
14
+ const unit = value.charCodeAt(index);
15
+ if (unit <= 0x1f || (unit >= 0x7f && unit <= 0x9f))
16
+ return true;
17
+ }
18
+ return false;
19
+ }
20
+ function hasUnpairedUtf16Surrogate(value) {
21
+ for (let index = 0; index < value.length; index += 1) {
22
+ const unit = value.charCodeAt(index);
23
+ if (unit >= 0xd800 && unit <= 0xdbff) {
24
+ const next = value.charCodeAt(index + 1);
25
+ if (!(next >= 0xdc00 && next <= 0xdfff))
26
+ return true;
27
+ index += 1;
28
+ continue;
29
+ }
30
+ if (unit >= 0xdc00 && unit <= 0xdfff)
31
+ return true;
32
+ }
33
+ return false;
34
+ }
35
+ function identityField(input, fieldName) {
36
+ if (!(fieldName in input))
37
+ return { status: "missing", value: null };
38
+ const raw = input[fieldName];
39
+ if (typeof raw !== "string" ||
40
+ raw.length === 0 ||
41
+ raw !== raw.trim() ||
42
+ hasControlCharacter(raw) ||
43
+ hasUnpairedUtf16Surrogate(raw) ||
44
+ Buffer.byteLength(raw, "utf8") > MAX_HOOK_HOST_IDENTITY_UTF8_BYTES) {
45
+ return { status: "invalid", value: null };
46
+ }
47
+ return { status: "ok", value: raw };
48
+ }
49
+ function unresolvedHostIdentity(status, provider, message) {
50
+ return { status, provider, rawSessionId: null, sessionId: null, message };
51
+ }
52
+ function canonicalHostSessionId(provider, rawSessionId) {
53
+ const encoded = Buffer.from(rawSessionId, "utf8").toString("base64url");
54
+ return `host:${provider}:v1:${encoded}`;
55
+ }
56
+ function resolvedHostIdentity(provider, rawSessionId) {
57
+ return {
58
+ status: "ok",
59
+ provider,
60
+ rawSessionId,
61
+ sessionId: canonicalHostSessionId(provider, rawSessionId),
62
+ message: null,
63
+ };
64
+ }
65
+ /**
66
+ * Resolve the stable conversation/session-family key documented by each host.
67
+ *
68
+ * - Codex: `session_id` (parent and subagents share it).
69
+ * - Claude Code: `session_id` (`agent_id` is deliberately not the owner key).
70
+ * - Cursor: `conversation_id`; simultaneous `session_id` must agree.
71
+ * - Grok/unknown: no guessed payload identity; callers keep explicit/env flow.
72
+ */
73
+ export function resolveHookHostIdentity(host, payload) {
74
+ if (!HOST_IDENTITY_PROVIDERS.includes(host)) {
75
+ return unresolvedHostIdentity("unsupported", host, `Host ${host || "<empty>"} has no verified payload identity contract.`);
76
+ }
77
+ const provider = host;
78
+ const input = record(payload);
79
+ if (input === null) {
80
+ return unresolvedHostIdentity("invalid", provider, `${provider} hook payload is not an object.`);
81
+ }
82
+ if (provider === "codex" || provider === "claude") {
83
+ const session = identityField(input, "session_id");
84
+ if (session.status !== "ok") {
85
+ return unresolvedHostIdentity(session.status, provider, `${provider} hook payload ${session.status === "missing" ? "omits" : "has invalid"} session_id.`);
86
+ }
87
+ return resolvedHostIdentity(provider, session.value);
88
+ }
89
+ const conversation = identityField(input, "conversation_id");
90
+ if (conversation.status !== "ok") {
91
+ return unresolvedHostIdentity(conversation.status, provider, `Cursor hook payload ${conversation.status === "missing" ? "omits" : "has invalid"} conversation_id.`);
92
+ }
93
+ const simultaneousSession = identityField(input, "session_id");
94
+ if (simultaneousSession.status === "invalid") {
95
+ return unresolvedHostIdentity("invalid", provider, "Cursor hook payload has invalid session_id alongside conversation_id.");
96
+ }
97
+ if (simultaneousSession.status === "ok" && simultaneousSession.value !== conversation.value) {
98
+ return unresolvedHostIdentity("conflict", provider, "Cursor hook identity conflict: conversation_id and session_id differ.");
99
+ }
100
+ return resolvedHostIdentity(provider, conversation.value);
101
+ }
102
+ export const EXACT_LIFECYCLE_VERBS = [
103
+ "session:start",
104
+ "session:ready",
105
+ "session:end",
106
+ "occupancy:steal",
107
+ "occupancy:release",
108
+ "occupancy:heartbeat",
109
+ "swarm:launch",
110
+ ];
111
+ const DIRECT_LIFECYCLE_VERBS = {
112
+ "session:start": "session:start",
113
+ "session:ready": "session:ready",
114
+ "session:end": "session:end",
115
+ "occupancy:steal": "occupancy:steal",
116
+ "occupancy:release": "occupancy:release",
117
+ "occupancy:heartbeat": "occupancy:heartbeat",
118
+ "swarm-launch": "swarm:launch",
119
+ };
120
+ const CANONICAL_OWNER_PATTERN = /^host:(?:codex|claude|cursor):v1:[A-Za-z0-9_-]+$/;
121
+ // Shell expansion markers are deliberately absent: `$`/backticks for POSIX,
122
+ // `@` splatting for PowerShell, and `%NAME%` expansion for command shells.
123
+ // Backslashes are inspectable so Windows path-bearing lifecycle commands fail
124
+ // closed, but they remain outside the auto-approved rewrite surface below.
125
+ const INSPECTABLE_TOKEN_PATTERN = /^[A-Za-z0-9_./\\:+=,-]+$/;
126
+ const LIFECYCLE_ARGUMENT_POLICIES = {
127
+ "session:start": {
128
+ booleanFlags: new Set([
129
+ "--json",
130
+ "--no-history",
131
+ "--read-only",
132
+ "--with-network",
133
+ "--rearm",
134
+ "--hard-budget",
135
+ "--compact",
136
+ "--steal",
137
+ "--confirm",
138
+ ]),
139
+ valueFlags: new Set([
140
+ "--tier",
141
+ "--defer",
142
+ "--task-size",
143
+ "--ceremony-task-size",
144
+ "--model-tier",
145
+ "--ceremony-model-tier",
146
+ "--project-shape",
147
+ "--ceremony-project-shape",
148
+ "--ceremony-depth",
149
+ "--max-turns",
150
+ "--max-budget",
151
+ "--session-id",
152
+ "--occupant",
153
+ "--project-root",
154
+ ]),
155
+ rewriteUnsafeFlags: new Set(["--project-root"]),
156
+ },
157
+ "session:ready": {
158
+ booleanFlags: new Set(["--json", "--with-network"]),
159
+ valueFlags: new Set(["--repo", "--session-id", "--project-root"]),
160
+ rewriteUnsafeFlags: new Set(["--project-root"]),
161
+ },
162
+ "session:end": {
163
+ booleanFlags: new Set(),
164
+ valueFlags: new Set(["--session-id", "--project-root"]),
165
+ rewriteUnsafeFlags: new Set(["--project-root"]),
166
+ },
167
+ "occupancy:steal": {
168
+ booleanFlags: new Set(["--confirm"]),
169
+ valueFlags: new Set(["--occupant", "--session-id", "--project-root"]),
170
+ rewriteUnsafeFlags: new Set(["--project-root"]),
171
+ },
172
+ "occupancy:release": {
173
+ booleanFlags: new Set(),
174
+ valueFlags: new Set(["--session-id", "--project-root"]),
175
+ rewriteUnsafeFlags: new Set(["--project-root"]),
176
+ },
177
+ "occupancy:heartbeat": {
178
+ booleanFlags: new Set(),
179
+ valueFlags: new Set(["--session-id", "--project-root"]),
180
+ rewriteUnsafeFlags: new Set(["--project-root"]),
181
+ },
182
+ "swarm:launch": {
183
+ booleanFlags: new Set([
184
+ "--autonomous",
185
+ "--no-create-worktrees",
186
+ "--enforce-gates",
187
+ "--no-audit",
188
+ ]),
189
+ valueFlags: new Set([
190
+ "--stories",
191
+ "--paths",
192
+ "--group",
193
+ "--worktree-map",
194
+ "--base-branch",
195
+ "--allocation-plan-id",
196
+ "--batching-rationale",
197
+ "--operator-approval",
198
+ "--output",
199
+ "--gate-clearances",
200
+ "--project-root",
201
+ "--session-id",
202
+ ]),
203
+ rewriteUnsafeFlags: new Set([
204
+ "--paths",
205
+ "--worktree-map",
206
+ "--output",
207
+ "--gate-clearances",
208
+ "--project-root",
209
+ "--no-audit",
210
+ ]),
211
+ separateOnlyValueFlags: new Set([
212
+ "--stories",
213
+ "--paths",
214
+ "--group",
215
+ "--worktree-map",
216
+ "--base-branch",
217
+ "--allocation-plan-id",
218
+ "--batching-rationale",
219
+ "--operator-approval",
220
+ "--output",
221
+ "--gate-clearances",
222
+ "--project-root",
223
+ ]),
224
+ },
225
+ };
226
+ function lifecycleArgumentValueIsRewriteSafe(verb, flag, value) {
227
+ if (verb !== "swarm:launch" || flag !== "--stories")
228
+ return true;
229
+ // `swarm:launch --stories` accepts both logical IDs and filesystem paths.
230
+ // Only the ID form belongs in the auto-approved rewrite bridge; path-bearing
231
+ // invocations must keep the host's ordinary permission decision.
232
+ return value
233
+ .split(",")
234
+ .every((story) => story.length > 0 &&
235
+ !story.endsWith(".json") &&
236
+ !story.includes("/") &&
237
+ !story.includes("\\"));
238
+ }
239
+ /**
240
+ * Validate the complete CLI argument surface before returning an allow+rewrite.
241
+ * Path rebinding/destination flags and unknown/future flags are recognized but
242
+ * kept outside the auto-rewrite bridge. Supported hosts must carry the matching
243
+ * owner explicitly before those commands retain normal permission handling.
244
+ */
245
+ function analyzeLifecycleArguments(verb, args) {
246
+ const policy = LIFECYCLE_ARGUMENT_POLICIES[verb];
247
+ let rewriteSafe = true;
248
+ let readOnly = false;
249
+ for (let index = 0; index < args.length; index += 1) {
250
+ const token = args[index] ?? "";
251
+ if (policy.booleanFlags.has(token)) {
252
+ if (verb === "session:start" && token === "--read-only")
253
+ readOnly = true;
254
+ if (policy.rewriteUnsafeFlags.has(token))
255
+ rewriteSafe = false;
256
+ continue;
257
+ }
258
+ const equals = token.indexOf("=");
259
+ if (equals > 0) {
260
+ const flag = token.slice(0, equals);
261
+ const value = token.slice(equals + 1);
262
+ if (!policy.valueFlags.has(flag)) {
263
+ rewriteSafe = false;
264
+ continue;
265
+ }
266
+ if (policy.rewriteUnsafeFlags.has(flag))
267
+ rewriteSafe = false;
268
+ if (policy.separateOnlyValueFlags?.has(flag) === true)
269
+ rewriteSafe = false;
270
+ // Keep malformed session identity visible to inspection so the
271
+ // dispatcher can deny ambiguity instead of silently skipping rewrite.
272
+ if (value.length === 0 && flag !== "--session-id")
273
+ rewriteSafe = false;
274
+ if (!lifecycleArgumentValueIsRewriteSafe(verb, flag, value))
275
+ rewriteSafe = false;
276
+ continue;
277
+ }
278
+ if (!policy.valueFlags.has(token)) {
279
+ rewriteSafe = false;
280
+ continue;
281
+ }
282
+ if (policy.rewriteUnsafeFlags.has(token))
283
+ rewriteSafe = false;
284
+ const value = args[index + 1];
285
+ if (value === undefined) {
286
+ if (token !== "--session-id")
287
+ rewriteSafe = false;
288
+ continue;
289
+ }
290
+ if (value.startsWith("--"))
291
+ rewriteSafe = false;
292
+ if (!lifecycleArgumentValueIsRewriteSafe(verb, token, value))
293
+ rewriteSafe = false;
294
+ // Match the real lifecycle parsers: known value flags consume the next
295
+ // argv token even when it begins with `--`. This prevents a swallowed
296
+ // `--session-id=...` from being mistaken for an effective owner.
297
+ index += 1;
298
+ }
299
+ return { rewriteSafe, readOnly };
300
+ }
301
+ /**
302
+ * Inspect single-space-separated lifecycle commands while excluding shell
303
+ * quoting, substitution, redirection, chaining, aliases, and wrappers.
304
+ * Windows path separators remain inspectable but make rewriting unsafe.
305
+ */
306
+ function exactLifecycleInvocation(command) {
307
+ if (command.length === 0 || command !== command.trim())
308
+ return null;
309
+ const tokens = command.split(" ");
310
+ if (tokens.some((token) => token.length === 0 || !INSPECTABLE_TOKEN_PATTERN.test(token))) {
311
+ return null;
312
+ }
313
+ if (tokens.length < 2)
314
+ return null;
315
+ const executable = tokens[0];
316
+ const verb = tokens[1];
317
+ if (executable === "deft" || executable === "directive") {
318
+ const typedVerb = verb === undefined ? undefined : DIRECT_LIFECYCLE_VERBS[verb];
319
+ if (typedVerb === undefined)
320
+ return null;
321
+ const forwardedArgs = tokens.slice(2);
322
+ const { rewriteSafe, readOnly } = analyzeLifecycleArguments(typedVerb, forwardedArgs);
323
+ const requiresOwner = typedVerb !== "session:start" || !readOnly;
324
+ return {
325
+ verb: typedVerb,
326
+ task: false,
327
+ forwardedArgs,
328
+ rewriteSafe: rewriteSafe && forwardedArgs.every((token) => !token.includes("\\")),
329
+ requiresOwner,
330
+ };
331
+ }
332
+ if (executable !== "task")
333
+ return null;
334
+ if (!EXACT_LIFECYCLE_VERBS.includes(verb ?? ""))
335
+ return null;
336
+ const typedVerb = verb;
337
+ if (tokens.length === 2) {
338
+ return {
339
+ verb: typedVerb,
340
+ task: true,
341
+ forwardedArgs: [],
342
+ rewriteSafe: true,
343
+ requiresOwner: true,
344
+ };
345
+ }
346
+ // Go Task's canonical CLI_ARGS boundary. Flags without `--` are ambiguous
347
+ // Task CLI flags and must not receive an auto-approving rewrite.
348
+ if (tokens[2] !== "--")
349
+ return null;
350
+ const forwardedArgs = tokens.slice(3);
351
+ const { rewriteSafe, readOnly } = analyzeLifecycleArguments(typedVerb, forwardedArgs);
352
+ const requiresOwner = typedVerb !== "session:start" || !readOnly;
353
+ return {
354
+ verb: typedVerb,
355
+ task: true,
356
+ forwardedArgs,
357
+ rewriteSafe: rewriteSafe && forwardedArgs.every((token) => !token.includes("\\")),
358
+ requiresOwner,
359
+ };
360
+ }
361
+ function exactLifecyclePayload(payload) {
362
+ const toolName = hookToolName(payload);
363
+ if (toolName === null || !isShellTool(toolName)) {
364
+ return null;
365
+ }
366
+ const input = record(payload);
367
+ if (input === null)
368
+ return null;
369
+ const toolInput = toolInputRecord(input);
370
+ if (toolInput === null || typeof toolInput.command !== "string")
371
+ return null;
372
+ const originalCommand = toolInput.command;
373
+ const invocation = exactLifecycleInvocation(originalCommand);
374
+ return invocation === null ? null : { invocation, originalCommand, toolInput };
375
+ }
376
+ /** Logical lifecycle verb for one exact, simple shell command; otherwise null. */
377
+ export function exactLifecycleCommandVerb(payload) {
378
+ return exactLifecyclePayload(payload)?.invocation.verb ?? null;
379
+ }
380
+ function sessionIdArgs(verb, args) {
381
+ const policy = LIFECYCLE_ARGUMENT_POLICIES[verb];
382
+ const values = [];
383
+ for (let i = 0; i < args.length; i += 1) {
384
+ const token = args[i] ?? "";
385
+ if (token.startsWith("--session-id=")) {
386
+ const value = token.slice("--session-id=".length);
387
+ if (value.length === 0)
388
+ return { status: "invalid", values: [] };
389
+ values.push(value);
390
+ continue;
391
+ }
392
+ if (token === "--session-id") {
393
+ const value = args[i + 1];
394
+ if (value === undefined ||
395
+ value.length === 0 ||
396
+ (verb === "swarm:launch" && value.startsWith("--"))) {
397
+ return { status: "invalid", values: [] };
398
+ }
399
+ values.push(value);
400
+ i += 1;
401
+ continue;
402
+ }
403
+ if (token.indexOf("=") < 0 && policy.valueFlags.has(token) && args[i + 1] !== undefined)
404
+ i += 1;
405
+ }
406
+ if (values.length === 0)
407
+ return { status: "absent", values };
408
+ if (values.length !== 1)
409
+ return { status: "invalid", values };
410
+ return { status: "present", values };
411
+ }
412
+ /** Inspect one exact lifecycle command without requiring a host identity. */
413
+ export function inspectExactLifecycleCommand(payload) {
414
+ const exact = exactLifecyclePayload(payload);
415
+ if (exact === null)
416
+ return null;
417
+ const session = sessionIdArgs(exact.invocation.verb, exact.invocation.forwardedArgs);
418
+ return {
419
+ verb: exact.invocation.verb,
420
+ task: exact.invocation.task,
421
+ rewriteSafe: exact.invocation.rewriteSafe,
422
+ requiresOwner: exact.invocation.requiresOwner,
423
+ sessionIdStatus: session.status,
424
+ sessionId: session.status === "present" ? (session.values[0] ?? null) : null,
425
+ };
426
+ }
427
+ /**
428
+ * Append a canonical host owner to one exact Directive lifecycle command.
429
+ *
430
+ * The returned `updatedInput` is a complete shallow clone of the original
431
+ * host tool input. `null` means no safe rewrite. An explicit foreign identity
432
+ * is surfaced as a conflict so dispatcher wiring can deny instead of replacing
433
+ * operator input.
434
+ */
435
+ export function rewriteExactLifecycleCommand(payload, sessionId) {
436
+ if (!CANONICAL_OWNER_PATTERN.test(sessionId))
437
+ return null;
438
+ const exact = exactLifecyclePayload(payload);
439
+ if (exact === null)
440
+ return null;
441
+ const { invocation, originalCommand, toolInput } = exact;
442
+ if (!invocation.rewriteSafe || !invocation.requiresOwner)
443
+ return null;
444
+ const existing = sessionIdArgs(invocation.verb, invocation.forwardedArgs);
445
+ if (existing.status === "invalid")
446
+ return null;
447
+ if (existing.status === "present") {
448
+ const existingSessionId = existing.values[0] ?? "";
449
+ if (existingSessionId === sessionId)
450
+ return null;
451
+ return {
452
+ kind: "conflict",
453
+ verb: invocation.verb,
454
+ existingSessionId,
455
+ requestedSessionId: sessionId,
456
+ message: `Lifecycle command ${invocation.verb} already names a different ` +
457
+ "--session-id; refusing rewrite.",
458
+ };
459
+ }
460
+ const taskForwarding = invocation.task && originalCommand.split(" ").length === 2 ? " --" : "";
461
+ const rewrittenCommand = `${originalCommand}${taskForwarding} --session-id=${sessionId}`;
462
+ return {
463
+ kind: "rewrite",
464
+ verb: invocation.verb,
465
+ originalCommand,
466
+ rewrittenCommand,
467
+ updatedInput: { ...toolInput, command: rewrittenCommand },
468
+ };
469
+ }
470
+ //# sourceMappingURL=host-session-identity.js.map
@@ -5,9 +5,10 @@
5
5
  * Policy / ritual / scope / permission emission stay in dispatcher + CLI adapter.
6
6
  */
7
7
  export { classifyHookPayload } from "./classify.js";
8
- export { hookMcpArgsText, hookPathSet, hookShellCommand, hookWriteTargetPath, } from "./paths.js";
8
+ export { EXACT_LIFECYCLE_VERBS, type ExactLifecycleCommandConflict, type ExactLifecycleCommandInspection, type ExactLifecycleCommandResult, type ExactLifecycleCommandRewrite, type ExactLifecycleVerb, exactLifecycleCommandVerb, HOST_IDENTITY_PROVIDERS, type HookHostIdentityProvider, type HookHostIdentityResolution, type HookHostIdentityStatus, inspectExactLifecycleCommand, MAX_HOOK_HOST_IDENTITY_UTF8_BYTES, resolveHookHostIdentity, rewriteExactLifecycleCommand, } from "./host-session-identity.js";
9
+ export { hookApplyPatchBodyPaths, hookMcpArgsText, hookMutationTargetPaths, hookPathSet, hookShellCommand, hookWriteTargetPath, } from "./paths.js";
9
10
  export { fieldPresent, fieldString, firstString, hookPayloadTopLevelKeys, record, toolInputRecord, } from "./payload.js";
10
- export { parseHookStdin, stripUtf8Bom } from "./stdin.js";
11
+ export { applyPatchMutationPaths, parseHookStdin, stripUtf8Bom } from "./stdin.js";
11
12
  export { hookToolName, inferCursorDirectWriteToolName, type MissingToolNameInput, missingToolNameMessage, } from "./tool-name.js";
12
13
  export type { ClassifyHookHost, ClassifyHookPayloadInput, HookClassification, HookPayloadContext, HookWriteIntent, ParsedHookPayload, } from "./types.js";
13
14
  export { CLASSIFY_HOOK_HOSTS } from "./types.js";
@@ -5,9 +5,10 @@
5
5
  * Policy / ritual / scope / permission emission stay in dispatcher + CLI adapter.
6
6
  */
7
7
  export { classifyHookPayload } from "./classify.js";
8
- export { hookMcpArgsText, hookPathSet, hookShellCommand, hookWriteTargetPath, } from "./paths.js";
8
+ export { EXACT_LIFECYCLE_VERBS, exactLifecycleCommandVerb, HOST_IDENTITY_PROVIDERS, inspectExactLifecycleCommand, MAX_HOOK_HOST_IDENTITY_UTF8_BYTES, resolveHookHostIdentity, rewriteExactLifecycleCommand, } from "./host-session-identity.js";
9
+ export { hookApplyPatchBodyPaths, hookMcpArgsText, hookMutationTargetPaths, hookPathSet, hookShellCommand, hookWriteTargetPath, } from "./paths.js";
9
10
  export { fieldPresent, fieldString, firstString, hookPayloadTopLevelKeys, record, toolInputRecord, } from "./payload.js";
10
- export { parseHookStdin, stripUtf8Bom } from "./stdin.js";
11
+ export { applyPatchMutationPaths, parseHookStdin, stripUtf8Bom } from "./stdin.js";
11
12
  export { hookToolName, inferCursorDirectWriteToolName, missingToolNameMessage, } from "./tool-name.js";
12
13
  export { CLASSIFY_HOOK_HOSTS } from "./types.js";
13
14
  //# sourceMappingURL=index.js.map
@@ -7,6 +7,13 @@
7
7
  * Hosts disagree on nesting (`tool_input.file_path` vs top-level `path`).
8
8
  */
9
9
  export declare function hookWriteTargetPath(payload: unknown): string | null;
10
+ /** ApplyPatch body paths from patch / unified_diff / diff fields. */
11
+ export declare function hookApplyPatchBodyPaths(payload: unknown): string[];
12
+ /**
13
+ * Declared write target plus ApplyPatch body members. Used to admit one
14
+ * effectiveRoot; a span across two Git toplevels is refused (#3794).
15
+ */
16
+ export declare function hookMutationTargetPaths(payload: unknown): string[];
10
17
  /**
11
18
  * Best-effort shell command string from host PreToolUse payloads (#2711).
12
19
  * Hosts disagree on nesting (`tool_input.command` vs top-level `command`).
@@ -3,6 +3,7 @@
3
3
  * No process I/O — does not resolve against projectRoot or realpath.
4
4
  */
5
5
  import { firstString, record, toolInputRecord } from "./payload.js";
6
+ import { applyPatchMutationPaths } from "./stdin.js";
6
7
  /**
7
8
  * Best-effort write-target path from host PreToolUse payloads (#2625).
8
9
  * Hosts disagree on nesting (`tool_input.file_path` vs top-level `path`).
@@ -21,6 +22,40 @@ export function hookWriteTargetPath(payload) {
21
22
  input.path,
22
23
  ]);
23
24
  }
25
+ /** ApplyPatch body paths from patch / unified_diff / diff fields. */
26
+ export function hookApplyPatchBodyPaths(payload) {
27
+ const input = record(payload);
28
+ if (input === null)
29
+ return [];
30
+ const toolInput = toolInputRecord(input);
31
+ const patch = firstString([
32
+ toolInput?.patch,
33
+ toolInput?.unified_diff,
34
+ toolInput?.diff,
35
+ input.patch,
36
+ input.unified_diff,
37
+ input.diff,
38
+ ]);
39
+ return patch === null ? [] : applyPatchMutationPaths(patch);
40
+ }
41
+ /**
42
+ * Declared write target plus ApplyPatch body members. Used to admit one
43
+ * effectiveRoot; a span across two Git toplevels is refused (#3794).
44
+ */
45
+ export function hookMutationTargetPaths(payload) {
46
+ const seen = new Set();
47
+ const out = [];
48
+ const push = (value) => {
49
+ if (value === null || value.length === 0 || seen.has(value))
50
+ return;
51
+ seen.add(value);
52
+ out.push(value);
53
+ };
54
+ push(hookWriteTargetPath(payload));
55
+ for (const path of hookApplyPatchBodyPaths(payload))
56
+ push(path);
57
+ return out;
58
+ }
24
59
  /**
25
60
  * Best-effort shell command string from host PreToolUse payloads (#2711).
26
61
  * Hosts disagree on nesting (`tool_input.command` vs top-level `command`).
@@ -4,6 +4,11 @@
4
4
  */
5
5
  import type { ParsedHookPayload } from "./types.js";
6
6
  export declare function stripUtf8Bom(raw: string): string;
7
+ /**
8
+ * Paths named by ApplyPatch mutation headers, plus `*** Move to:` rename
9
+ * destinations. Order preserved, duplicates dropped.
10
+ */
11
+ export declare function applyPatchMutationPaths(text: string): string[];
7
12
  /**
8
13
  * Parse host hook stdin text into a payload + parse context.
9
14
  * Empty → stdinEmpty; invalid JSON without free-form ApplyPatch → parseFailed.
@@ -6,9 +6,36 @@ const UTF8_BOM = "\uFEFF";
6
6
  const APPLY_PATCH_BEGIN_MARKER = "*** Begin Patch";
7
7
  /** Single-file Add/Update only — other *** … File: ops must fail closed (#2738 Greptile). */
8
8
  const APPLY_PATCH_MUTATION_LINE_RE = /^\*\*\* (Add File|Update File|Delete File|Move File|Rename File): (.+)$/gm;
9
+ /**
10
+ * Canonical apply_patch spells a rename as `*** Update File:` followed by
11
+ * `*** Move to:`. The destination is a mutation target in its own right, so it
12
+ * must reach root admission; kept separate from the header regex above so the
13
+ * single-mutation #2738 synthesis contract is unchanged (#3794).
14
+ */
15
+ const APPLY_PATCH_MOVE_DESTINATION_RE = /^\*\*\* Move to: (.+)$/gm;
9
16
  export function stripUtf8Bom(raw) {
10
17
  return raw.startsWith(UTF8_BOM) ? raw.slice(UTF8_BOM.length) : raw;
11
18
  }
19
+ /**
20
+ * Paths named by ApplyPatch mutation headers, plus `*** Move to:` rename
21
+ * destinations. Order preserved, duplicates dropped.
22
+ */
23
+ export function applyPatchMutationPaths(text) {
24
+ const paths = [];
25
+ const seen = new Set();
26
+ const push = (raw) => {
27
+ const path = raw?.trim();
28
+ if (!path || seen.has(path))
29
+ return;
30
+ seen.add(path);
31
+ paths.push(path);
32
+ };
33
+ for (const match of text.matchAll(APPLY_PATCH_MUTATION_LINE_RE))
34
+ push(match[2]);
35
+ for (const match of text.matchAll(APPLY_PATCH_MOVE_DESTINATION_RE))
36
+ push(match[1]);
37
+ return paths;
38
+ }
12
39
  function trySynthesizeFreeFormApplyPatch(normalized) {
13
40
  if (!normalized.includes(APPLY_PATCH_BEGIN_MARKER))
14
41
  return null;