@flyingrobots/graft 0.3.5 → 0.5.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 (111) hide show
  1. package/ARCHITECTURE.md +386 -0
  2. package/CHANGELOG.md +69 -0
  3. package/CODE_OF_CONDUCT.md +65 -0
  4. package/README.md +153 -17
  5. package/bin/graft.js +4 -11
  6. package/docs/ADVANCED_GUIDE.md +49 -0
  7. package/docs/CLI.md +43 -0
  8. package/docs/GUIDE.md +321 -32
  9. package/docs/MCP.md +44 -0
  10. package/package.json +17 -4
  11. package/src/adapters/node-fs.ts +4 -0
  12. package/src/adapters/node-git.ts +47 -0
  13. package/src/adapters/node-process-runner.ts +27 -0
  14. package/src/cli/index-cmd.ts +86 -0
  15. package/src/cli/init.ts +808 -57
  16. package/src/cli/main.ts +437 -0
  17. package/src/contracts/capabilities.ts +341 -0
  18. package/src/contracts/causal-ontology.ts +622 -0
  19. package/src/contracts/causal-surface-next-action.ts +18 -0
  20. package/src/contracts/output-schemas.ts +1169 -0
  21. package/src/git/diff.ts +25 -21
  22. package/src/git/target-git-hook-bootstrap.ts +56 -0
  23. package/src/hooks/posttooluse-read.ts +21 -74
  24. package/src/hooks/pretooluse-read.ts +20 -56
  25. package/src/hooks/read-governor.ts +95 -0
  26. package/src/hooks/read-messages.ts +53 -0
  27. package/src/mcp/burden.ts +123 -0
  28. package/src/mcp/cache.ts +51 -0
  29. package/src/mcp/cached-file.ts +10 -8
  30. package/src/mcp/context.ts +67 -2
  31. package/src/mcp/daemon-control-plane.ts +554 -0
  32. package/src/mcp/daemon-job-scheduler.ts +279 -0
  33. package/src/mcp/daemon-repos.ts +216 -0
  34. package/src/mcp/daemon-server.ts +396 -0
  35. package/src/mcp/daemon-worker-pool.ts +310 -0
  36. package/src/mcp/daemon-worker-process.ts +52 -0
  37. package/src/mcp/metrics.ts +108 -1
  38. package/src/mcp/monitor-tick-job.ts +99 -0
  39. package/src/mcp/persisted-local-history.ts +1246 -0
  40. package/src/mcp/persistent-monitor-runtime.ts +549 -0
  41. package/src/mcp/policy.ts +84 -0
  42. package/src/mcp/receipt.ts +82 -12
  43. package/src/mcp/repo-concurrency.ts +318 -0
  44. package/src/mcp/repo-state.ts +777 -0
  45. package/src/mcp/repo-tool-job.ts +302 -0
  46. package/src/mcp/run-capture-config.ts +33 -0
  47. package/src/mcp/runtime-causal-context.ts +72 -0
  48. package/src/mcp/runtime-observability.ts +219 -0
  49. package/src/mcp/runtime-staged-target.ts +161 -0
  50. package/src/mcp/runtime-workspace-overlay.ts +255 -0
  51. package/src/mcp/semantic-transition-guidance.ts +60 -0
  52. package/src/mcp/semantic-transition-summary.ts +130 -0
  53. package/src/mcp/server.ts +704 -45
  54. package/src/mcp/stdio-server.ts +12 -0
  55. package/src/mcp/stdio.ts +2 -5
  56. package/src/mcp/tools/activity-view.ts +325 -0
  57. package/src/mcp/tools/causal-attach.ts +67 -0
  58. package/src/mcp/tools/causal-status.ts +58 -0
  59. package/src/mcp/tools/changed-since.ts +13 -11
  60. package/src/mcp/tools/code-find.ts +164 -0
  61. package/src/mcp/tools/code-refs.ts +466 -0
  62. package/src/mcp/tools/code-show.ts +252 -0
  63. package/src/mcp/tools/daemon-monitors.ts +14 -0
  64. package/src/mcp/tools/daemon-repos.ts +22 -0
  65. package/src/mcp/tools/daemon-sessions.ts +14 -0
  66. package/src/mcp/tools/daemon-status.ts +12 -0
  67. package/src/mcp/tools/doctor.ts +45 -2
  68. package/src/mcp/tools/explain.ts +4 -0
  69. package/src/mcp/tools/file-outline.ts +7 -3
  70. package/src/mcp/tools/git-files.ts +73 -0
  71. package/src/mcp/tools/graft-diff.ts +12 -4
  72. package/src/mcp/tools/map.ts +136 -0
  73. package/src/mcp/tools/monitor-pause.ts +18 -0
  74. package/src/mcp/tools/monitor-resume.ts +18 -0
  75. package/src/mcp/tools/monitor-start.ts +20 -0
  76. package/src/mcp/tools/monitor-stop.ts +18 -0
  77. package/src/mcp/tools/precision-match.ts +51 -0
  78. package/src/mcp/tools/precision-query.ts +127 -0
  79. package/src/mcp/tools/precision.ts +312 -0
  80. package/src/mcp/tools/run-capture.ts +126 -44
  81. package/src/mcp/tools/safe-read.ts +14 -12
  82. package/src/mcp/tools/since.ts +49 -0
  83. package/src/mcp/tools/state.ts +11 -3
  84. package/src/mcp/tools/stats.ts +5 -1
  85. package/src/mcp/tools/workspace-authorizations.ts +14 -0
  86. package/src/mcp/tools/workspace-authorize.ts +20 -0
  87. package/src/mcp/tools/workspace-bind.ts +25 -0
  88. package/src/mcp/tools/workspace-rebind.ts +25 -0
  89. package/src/mcp/tools/workspace-revoke.ts +18 -0
  90. package/src/mcp/tools/workspace-status.ts +12 -0
  91. package/src/mcp/warp-pool.ts +36 -0
  92. package/src/mcp/workspace-router.ts +984 -0
  93. package/src/operations/file-outline.ts +12 -2
  94. package/src/operations/graft-diff.ts +56 -10
  95. package/src/operations/safe-read.ts +27 -4
  96. package/src/operations/state.ts +6 -9
  97. package/src/parser/lang.ts +19 -3
  98. package/src/parser/outline.ts +191 -2
  99. package/src/parser/types.ts +9 -1
  100. package/src/policy/types.ts +4 -3
  101. package/src/ports/filesystem.ts +1 -0
  102. package/src/ports/git.ts +16 -0
  103. package/src/ports/process-runner.ts +22 -0
  104. package/src/release/security-gate.ts +102 -0
  105. package/src/session/tracker.ts +31 -0
  106. package/src/version.ts +3 -0
  107. package/src/warp/indexer.ts +513 -0
  108. package/src/warp/observers.ts +105 -0
  109. package/src/warp/open.ts +31 -0
  110. package/src/warp/plumbing.d.ts +15 -0
  111. package/src/warp/writer-id.ts +30 -0
@@ -6,16 +6,67 @@ import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
6
6
  import type { MetricsSnapshot } from "./metrics.js";
7
7
  import type { Tripwire } from "../session/types.js";
8
8
  import type { JsonCodec } from "../ports/codec.js";
9
+ import { attachMcpSchemaMeta, type McpToolName } from "../contracts/output-schemas.js";
10
+ import {
11
+ burdenKindForTool,
12
+ isNonReadBurdenKind,
13
+ projectBurdenByKind,
14
+ totalNonReadBytesReturned,
15
+ type BurdenByKind,
16
+ type BurdenKind,
17
+ } from "./burden.js";
9
18
 
10
19
  export type McpToolResult = CallToolResult;
11
20
 
21
+ export interface ReceiptBudget {
22
+ readonly total: number;
23
+ readonly consumed: number;
24
+ readonly remaining: number;
25
+ readonly fraction: number;
26
+ }
27
+
28
+ export interface ReceiptCumulative {
29
+ readonly reads: number;
30
+ readonly outlines: number;
31
+ readonly refusals: number;
32
+ readonly cacheHits: number;
33
+ readonly bytesReturned: number;
34
+ readonly bytesAvoided: number;
35
+ readonly nonReadBytesReturned: number;
36
+ readonly burdenByKind: Readonly<BurdenByKind>;
37
+ }
38
+
39
+ export interface ReceiptBurden {
40
+ readonly kind: BurdenKind;
41
+ readonly nonRead: boolean;
42
+ }
43
+
44
+ export interface McpToolReceipt {
45
+ readonly sessionId: string;
46
+ readonly traceId: string;
47
+ readonly seq: number;
48
+ readonly ts: string;
49
+ readonly tool: McpToolName;
50
+ readonly projection: string;
51
+ readonly reason: string;
52
+ readonly latencyMs: number;
53
+ readonly fileBytes: number | null;
54
+ readonly returnedBytes: number;
55
+ readonly burden: ReceiptBurden;
56
+ readonly cumulative: ReceiptCumulative;
57
+ readonly budget?: ReceiptBudget;
58
+ readonly compressionRatio?: number | null;
59
+ }
60
+
12
61
  export interface ReceiptDeps {
13
62
  readonly sessionId: string;
63
+ readonly traceId: string;
14
64
  readonly seq: number;
65
+ readonly latencyMs: number;
15
66
  readonly metrics: MetricsSnapshot;
16
67
  readonly tripwires: Tripwire[];
17
68
  readonly codec: JsonCodec;
18
- readonly budget?: { total: number; consumed: number; remaining: number; fraction: number } | null;
69
+ readonly budget?: ReceiptBudget | null;
19
70
  }
20
71
 
21
72
  /**
@@ -24,19 +75,26 @@ export interface ReceiptDeps {
24
75
  * so the caller can feed it back into cumulative metrics.
25
76
  */
26
77
  export function buildReceiptResult(
27
- tool: string,
78
+ tool: McpToolName,
28
79
  data: Record<string, unknown>,
29
80
  deps: ReceiptDeps,
30
- ): { result: McpToolResult; textBytes: number } {
31
- const receipt: Record<string, unknown> = {
81
+ ): { result: McpToolResult; textBytes: number; receipt: McpToolReceipt } {
82
+ const burdenKind = burdenKindForTool(tool);
83
+ const receipt: McpToolReceipt = {
32
84
  sessionId: deps.sessionId,
85
+ traceId: deps.traceId,
33
86
  seq: deps.seq,
34
87
  ts: new Date().toISOString(),
35
88
  tool,
36
89
  projection: (data["projection"] as string | undefined) ?? "none",
37
90
  reason: (data["reason"] as string | undefined) ?? "none",
91
+ latencyMs: deps.latencyMs,
38
92
  fileBytes: (data["actual"] as { bytes: number } | undefined)?.bytes ?? null,
39
93
  returnedBytes: 0,
94
+ burden: {
95
+ kind: burdenKind,
96
+ nonRead: isNonReadBurdenKind(burdenKind),
97
+ },
40
98
  cumulative: {
41
99
  reads: deps.metrics.reads,
42
100
  outlines: deps.metrics.outlines,
@@ -44,16 +102,21 @@ export function buildReceiptResult(
44
102
  cacheHits: deps.metrics.cacheHits,
45
103
  bytesReturned: 0,
46
104
  bytesAvoided: deps.metrics.bytesAvoided,
105
+ nonReadBytesReturned: totalNonReadBytesReturned(deps.metrics.burdenByKind),
106
+ burdenByKind: deps.metrics.burdenByKind,
47
107
  },
48
108
  };
49
109
 
50
110
  if (deps.budget != null) {
51
- receipt["budget"] = deps.budget;
111
+ (receipt as McpToolReceipt & { budget?: ReceiptBudget }).budget = deps.budget;
52
112
  }
53
113
 
54
- const fullData: Record<string, unknown> = { ...data, _receipt: receipt };
114
+ const fullData: Record<string, unknown> & { tripwire?: Tripwire[] } = attachMcpSchemaMeta(tool, {
115
+ ...data,
116
+ _receipt: receipt,
117
+ });
55
118
  if (deps.tripwires.length > 0) {
56
- fullData["tripwire"] = deps.tripwires;
119
+ fullData.tripwire = deps.tripwires;
57
120
  }
58
121
 
59
122
  // Stabilize self-referential size fields (use UTF-8 byte length, not char count)
@@ -64,20 +127,27 @@ export function buildReceiptResult(
64
127
  const byteLen = Buffer.byteLength(text, "utf8");
65
128
  if (byteLen === prev) break;
66
129
  prev = byteLen;
67
- receipt["returnedBytes"] = byteLen;
68
- const fb = receipt["fileBytes"] as number | null;
69
- receipt["compressionRatio"] = fb !== null && fb > 0
130
+ (receipt as McpToolReceipt & { returnedBytes: number }).returnedBytes = byteLen;
131
+ const fb = receipt.fileBytes;
132
+ const burdenByKind = projectBurdenByKind(deps.metrics.burdenByKind, tool, byteLen);
133
+ (receipt as McpToolReceipt & { compressionRatio?: number | null }).compressionRatio = fb !== null && fb > 0
70
134
  ? Math.round((byteLen / fb) * 1000) / 1000
71
135
  : null;
72
- (receipt["cumulative"] as Record<string, number>)["bytesReturned"] =
136
+ (receipt.cumulative as ReceiptCumulative & { bytesReturned: number }).bytesReturned =
73
137
  deps.metrics.bytesReturned + byteLen;
138
+ (receipt.cumulative as ReceiptCumulative & { nonReadBytesReturned: number }).nonReadBytesReturned =
139
+ totalNonReadBytesReturned(burdenByKind);
140
+ (receipt.cumulative as ReceiptCumulative & { burdenByKind: Readonly<BurdenByKind> }).burdenByKind =
141
+ burdenByKind;
74
142
  }
75
143
 
76
- Object.freeze(receipt["cumulative"]);
144
+ Object.freeze(receipt.burden);
145
+ Object.freeze(receipt.cumulative);
77
146
  Object.freeze(receipt);
78
147
 
79
148
  return {
80
149
  result: { content: [{ type: "text", text }] },
81
150
  textBytes: Buffer.byteLength(text, "utf8"),
151
+ receipt,
82
152
  };
83
153
  }
@@ -0,0 +1,318 @@
1
+ import type {
2
+ AttributionSummary,
3
+ CausalEvent,
4
+ RepoConcurrencyAuthority,
5
+ RepoConcurrencyPosture,
6
+ } from "../contracts/causal-ontology.js";
7
+
8
+ export interface RepoConcurrencyTouch {
9
+ readonly contributorKey: string;
10
+ readonly path: string;
11
+ }
12
+
13
+ export interface RepoConcurrencyWorktreeHistory {
14
+ readonly worktreeId: string;
15
+ readonly active: boolean;
16
+ readonly checkoutEpochId: string | null;
17
+ readonly causalSessionIds: readonly string[];
18
+ readonly actorIds: readonly string[];
19
+ readonly contributorKeys: readonly string[];
20
+ readonly explicitHandoff: boolean;
21
+ readonly touches: readonly RepoConcurrencyTouch[];
22
+ }
23
+
24
+ export interface RepoConcurrencySummary {
25
+ readonly posture: RepoConcurrencyPosture;
26
+ readonly authority: RepoConcurrencyAuthority;
27
+ readonly observedWorktreeCount: number;
28
+ readonly observedCausalSessionCount: number;
29
+ readonly observedActorCount: number;
30
+ readonly overlappingPathCount: number;
31
+ readonly summary: string;
32
+ }
33
+
34
+ interface RepoConcurrencyInput {
35
+ readonly currentWorktreeId: string;
36
+ readonly histories: readonly RepoConcurrencyWorktreeHistory[];
37
+ }
38
+
39
+ export interface RepoConcurrencyLiveSession {
40
+ readonly bindState: "bound" | "unbound";
41
+ readonly repoId: string | null;
42
+ readonly worktreeId: string | null;
43
+ readonly causalSessionId: string | null;
44
+ readonly checkoutEpochId: string | null;
45
+ }
46
+
47
+ function countOverlapPaths(touches: readonly RepoConcurrencyTouch[]): number {
48
+ const contributorsByPath = new Map<string, Set<string>>();
49
+ for (const touch of touches) {
50
+ const existing = contributorsByPath.get(touch.path) ?? new Set<string>();
51
+ existing.add(touch.contributorKey);
52
+ contributorsByPath.set(touch.path, existing);
53
+ }
54
+
55
+ let count = 0;
56
+ for (const contributors of contributorsByPath.values()) {
57
+ if (contributors.size > 1) {
58
+ count += 1;
59
+ }
60
+ }
61
+ return count;
62
+ }
63
+
64
+ function buildSummary(
65
+ posture: RepoConcurrencyPosture,
66
+ authority: RepoConcurrencyAuthority,
67
+ observedWorktreeCount: number,
68
+ observedCausalSessionCount: number,
69
+ observedActorCount: number,
70
+ overlappingPathCount: number,
71
+ summary: string,
72
+ ): RepoConcurrencySummary {
73
+ return {
74
+ posture,
75
+ authority,
76
+ observedWorktreeCount,
77
+ observedCausalSessionCount,
78
+ observedActorCount,
79
+ overlappingPathCount,
80
+ summary,
81
+ };
82
+ }
83
+
84
+ function withObservedCounts(
85
+ summary: RepoConcurrencySummary,
86
+ input: {
87
+ readonly observedWorktreeCount: number;
88
+ readonly observedCausalSessionCount: number;
89
+ readonly observedActorCount: number;
90
+ },
91
+ ): RepoConcurrencySummary {
92
+ return {
93
+ ...summary,
94
+ observedWorktreeCount: Math.max(summary.observedWorktreeCount, input.observedWorktreeCount),
95
+ observedCausalSessionCount: Math.max(summary.observedCausalSessionCount, input.observedCausalSessionCount),
96
+ observedActorCount: Math.max(summary.observedActorCount, input.observedActorCount),
97
+ };
98
+ }
99
+
100
+ export function deriveRepoConcurrencySummary(input: RepoConcurrencyInput): RepoConcurrencySummary {
101
+ const activeHistories = input.histories.filter((history) => history.active);
102
+ const current = activeHistories.find((history) => history.worktreeId === input.currentWorktreeId)
103
+ ?? input.histories.find((history) => history.worktreeId === input.currentWorktreeId)
104
+ ?? null;
105
+
106
+ if (current === null) {
107
+ return buildSummary(
108
+ "unknown",
109
+ "unknown",
110
+ 0,
111
+ 0,
112
+ 0,
113
+ 0,
114
+ "No lawful same-repo concurrency evidence is available for the current worktree.",
115
+ );
116
+ }
117
+
118
+ const observedWorktreeCount = Math.max(1, new Set(activeHistories.map((history) => history.worktreeId)).size);
119
+ const observedCausalSessionCount = new Set(
120
+ activeHistories.flatMap((history) => history.causalSessionIds),
121
+ ).size;
122
+ const observedActorCount = new Set(
123
+ activeHistories.flatMap((history) => history.actorIds),
124
+ ).size;
125
+ const overlappingPathCount = countOverlapPaths(current.touches);
126
+ const otherActiveWorktrees = activeHistories.filter((history) => history.worktreeId !== current.worktreeId);
127
+
128
+ if (otherActiveWorktrees.some((history) =>
129
+ history.checkoutEpochId !== null &&
130
+ current.checkoutEpochId !== null &&
131
+ history.checkoutEpochId !== current.checkoutEpochId
132
+ )) {
133
+ return buildSummary(
134
+ "divergent_checkout",
135
+ "active_history_scan",
136
+ observedWorktreeCount,
137
+ observedCausalSessionCount,
138
+ observedActorCount,
139
+ overlappingPathCount,
140
+ "Active same-repo work appears to be split across different checkout epochs or branches.",
141
+ );
142
+ }
143
+
144
+ if (current.contributorKeys.length > 1 && overlappingPathCount > 0 && !current.explicitHandoff) {
145
+ return buildSummary(
146
+ "overlapping_actors",
147
+ "footprint_overlap",
148
+ observedWorktreeCount,
149
+ observedCausalSessionCount,
150
+ observedActorCount,
151
+ overlappingPathCount,
152
+ "Multiple contributors appear to touch overlapping paths in the same live worktree without explicit handoff evidence.",
153
+ );
154
+ }
155
+
156
+ if (current.explicitHandoff && current.contributorKeys.length > 1 && otherActiveWorktrees.length === 0) {
157
+ return buildSummary(
158
+ "exclusive",
159
+ "explicit_handoff",
160
+ observedWorktreeCount,
161
+ observedCausalSessionCount,
162
+ observedActorCount,
163
+ overlappingPathCount,
164
+ "Explicit attach or handoff evidence keeps the current live worktree on one lawful line of work.",
165
+ );
166
+ }
167
+
168
+ if (current.contributorKeys.length > 1) {
169
+ return buildSummary(
170
+ "shared_worktree",
171
+ "active_history_scan",
172
+ observedWorktreeCount,
173
+ observedCausalSessionCount,
174
+ observedActorCount,
175
+ overlappingPathCount,
176
+ "Multiple contributors have been observed on the current live worktree, so ownership stays shared rather than exclusive.",
177
+ );
178
+ }
179
+
180
+ if (otherActiveWorktrees.length > 0) {
181
+ return buildSummary(
182
+ "shared_repo_only",
183
+ "repo_identity_only",
184
+ observedWorktreeCount,
185
+ observedCausalSessionCount,
186
+ observedActorCount,
187
+ overlappingPathCount,
188
+ "Other active worktrees share the same canonical repo identity, but not this live worktree footing.",
189
+ );
190
+ }
191
+
192
+ return buildSummary(
193
+ "exclusive",
194
+ "active_history_scan",
195
+ observedWorktreeCount,
196
+ observedCausalSessionCount,
197
+ observedActorCount,
198
+ overlappingPathCount,
199
+ "No other active same-repo worktree or overlapping actor evidence is currently visible.",
200
+ );
201
+ }
202
+
203
+ export function mergeRepoConcurrencySummaryWithLiveSessions(input: {
204
+ readonly currentSummary: RepoConcurrencySummary;
205
+ readonly currentRepoId: string;
206
+ readonly currentWorktreeId: string;
207
+ readonly currentCheckoutEpochId: string | null;
208
+ readonly sessions: readonly RepoConcurrencyLiveSession[];
209
+ }): RepoConcurrencySummary {
210
+ const liveRepoSessions = input.sessions.filter((session) =>
211
+ session.bindState === "bound" &&
212
+ session.repoId === input.currentRepoId &&
213
+ session.worktreeId !== null
214
+ );
215
+
216
+ if (liveRepoSessions.length === 0) {
217
+ return input.currentSummary;
218
+ }
219
+
220
+ const observedWorktreeCount = new Set(liveRepoSessions.map((session) => session.worktreeId)).size;
221
+ const observedCausalSessionCount = new Set(
222
+ liveRepoSessions
223
+ .map((session) => session.causalSessionId)
224
+ .filter((sessionId): sessionId is string => sessionId !== null),
225
+ ).size;
226
+ const summaryWithCounts = withObservedCounts(input.currentSummary, {
227
+ observedWorktreeCount: Math.max(1, observedWorktreeCount),
228
+ observedCausalSessionCount,
229
+ observedActorCount: input.currentSummary.observedActorCount,
230
+ });
231
+
232
+ const currentWorktreeSessions = liveRepoSessions.filter((session) => session.worktreeId === input.currentWorktreeId);
233
+ const otherWorktreeSessions = liveRepoSessions.filter((session) => session.worktreeId !== input.currentWorktreeId);
234
+
235
+ if (
236
+ otherWorktreeSessions.some((session) =>
237
+ session.checkoutEpochId !== null &&
238
+ input.currentCheckoutEpochId !== null &&
239
+ session.checkoutEpochId !== input.currentCheckoutEpochId
240
+ )
241
+ ) {
242
+ return {
243
+ ...summaryWithCounts,
244
+ posture: "divergent_checkout",
245
+ authority: "daemon_live_sessions",
246
+ summary: "Active same-repo daemon sessions are split across different checkout epochs or branches.",
247
+ };
248
+ }
249
+
250
+ if (summaryWithCounts.posture === "overlapping_actors") {
251
+ return summaryWithCounts;
252
+ }
253
+
254
+ if (currentWorktreeSessions.length > 1) {
255
+ if (
256
+ summaryWithCounts.posture === "exclusive" &&
257
+ summaryWithCounts.authority === "explicit_handoff" &&
258
+ otherWorktreeSessions.length === 0
259
+ ) {
260
+ return summaryWithCounts;
261
+ }
262
+ return {
263
+ ...summaryWithCounts,
264
+ posture: "shared_worktree",
265
+ authority: "daemon_live_sessions",
266
+ summary:
267
+ "Multiple active daemon sessions share the current live worktree, so ownership stays shared rather than exclusive.",
268
+ };
269
+ }
270
+
271
+ if (otherWorktreeSessions.length > 0) {
272
+ return {
273
+ ...summaryWithCounts,
274
+ posture: "shared_repo_only",
275
+ authority: "daemon_live_sessions",
276
+ summary: "Other active daemon sessions share the same canonical repo identity, but not this live worktree footing.",
277
+ };
278
+ }
279
+
280
+ return summaryWithCounts;
281
+ }
282
+
283
+ export function buildRepoConcurrencyContributorKey(input: {
284
+ readonly actorId: string | null;
285
+ readonly attribution: AttributionSummary;
286
+ readonly causalSessionId: string | null;
287
+ readonly transportSessionId: string | null;
288
+ }): string | null {
289
+ if (input.actorId !== null && input.attribution.actor.actorKind !== "unknown") {
290
+ return `actor:${input.actorId}`;
291
+ }
292
+ if (input.causalSessionId !== null) {
293
+ return `causal:${input.causalSessionId}`;
294
+ }
295
+ if (input.transportSessionId !== null) {
296
+ return `transport:${input.transportSessionId}`;
297
+ }
298
+ return null;
299
+ }
300
+
301
+ export function buildRepoConcurrencyTouches(events: readonly CausalEvent[]): RepoConcurrencyTouch[] {
302
+ const touches: RepoConcurrencyTouch[] = [];
303
+ for (const event of events) {
304
+ const contributorKey = buildRepoConcurrencyContributorKey({
305
+ actorId: event.actorId,
306
+ attribution: event.attribution,
307
+ causalSessionId: event.causalSessionId,
308
+ transportSessionId: event.transportSessionId,
309
+ });
310
+ if (contributorKey === null) {
311
+ continue;
312
+ }
313
+ for (const path of event.footprint.paths) {
314
+ touches.push({ contributorKey, path });
315
+ }
316
+ }
317
+ return touches;
318
+ }