@agimon-ai/doompi-git 0.0.1-alpha.4 → 0.0.1-alpha.41

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 (180) hide show
  1. package/README.md +33 -1
  2. package/dist/apiContracts.cjs +387 -0
  3. package/dist/apiContracts.cjs.map +1 -0
  4. package/dist/apiContracts.d.mts +53 -0
  5. package/dist/apiContracts.d.mts.map +1 -0
  6. package/dist/apiContracts.mjs +382 -0
  7. package/dist/apiContracts.mjs.map +1 -0
  8. package/dist/extensions/pi.cjs +105 -1
  9. package/dist/extensions/pi.cjs.map +1 -0
  10. package/dist/extensions/pi.d.mts +6 -2
  11. package/dist/extensions/pi.d.mts.map +1 -0
  12. package/dist/extensions/pi.mjs +100 -1
  13. package/dist/extensions/pi.mjs.map +1 -0
  14. package/dist/extensions/server.cjs +1828 -0
  15. package/dist/extensions/server.cjs.map +1 -0
  16. package/dist/extensions/server.d.mts +5 -0
  17. package/dist/extensions/server.d.mts.map +1 -0
  18. package/dist/extensions/server.mjs +1820 -0
  19. package/dist/extensions/server.mjs.map +1 -0
  20. package/dist/extensions/web.mjs +2641 -0
  21. package/dist/extensions/web.mjs.map +1 -0
  22. package/dist/gitDependencies-CtVubYvU.mjs +22 -0
  23. package/dist/gitDependencies-CtVubYvU.mjs.map +1 -0
  24. package/dist/gitDependencies-Dqsz2HjK.cjs +33 -0
  25. package/dist/gitDependencies-Dqsz2HjK.cjs.map +1 -0
  26. package/dist/index-D79MSCee.d.mts +145 -0
  27. package/dist/index-D79MSCee.d.mts.map +1 -0
  28. package/dist/index.cjs +4 -1
  29. package/dist/index.d.mts +2 -5
  30. package/dist/index.mjs +2 -1
  31. package/dist/runWorktree-BE795LxL.cjs +374 -0
  32. package/dist/runWorktree-BE795LxL.cjs.map +1 -0
  33. package/dist/runWorktree-SPzJvIBt.mjs +303 -0
  34. package/dist/runWorktree-SPzJvIBt.mjs.map +1 -0
  35. package/dist/worktreeOperations-BYmVU0UV.cjs +1350 -0
  36. package/dist/worktreeOperations-BYmVU0UV.cjs.map +1 -0
  37. package/dist/worktreeOperations-BrWG2muV.mjs +1194 -0
  38. package/dist/worktreeOperations-BrWG2muV.mjs.map +1 -0
  39. package/llms.txt +4 -0
  40. package/package.json +56 -41
  41. package/src/prompts/doompi-use-git/SKILL.md +26 -3
  42. package/dist/_virtual/_rolldown/runtime.cjs +0 -1
  43. package/dist/adapters/channel/worktreeChannel.cjs +0 -2
  44. package/dist/adapters/channel/worktreeChannel.cjs.map +0 -1
  45. package/dist/adapters/channel/worktreeChannel.d.cts +0 -40
  46. package/dist/adapters/channel/worktreeChannel.d.cts.map +0 -1
  47. package/dist/adapters/channel/worktreeChannel.d.mts +0 -40
  48. package/dist/adapters/channel/worktreeChannel.d.mts.map +0 -1
  49. package/dist/adapters/channel/worktreeChannel.mjs +0 -2
  50. package/dist/adapters/channel/worktreeChannel.mjs.map +0 -1
  51. package/dist/adapters/filesystem/atomicJson.cjs +0 -2
  52. package/dist/adapters/filesystem/atomicJson.cjs.map +0 -1
  53. package/dist/adapters/filesystem/atomicJson.mjs +0 -2
  54. package/dist/adapters/filesystem/atomicJson.mjs.map +0 -1
  55. package/dist/adapters/filesystem/paths.cjs +0 -2
  56. package/dist/adapters/filesystem/paths.cjs.map +0 -1
  57. package/dist/adapters/filesystem/paths.mjs +0 -2
  58. package/dist/adapters/filesystem/paths.mjs.map +0 -1
  59. package/dist/adapters/hub/hubClient.cjs +0 -2
  60. package/dist/adapters/hub/hubClient.cjs.map +0 -1
  61. package/dist/adapters/hub/hubClient.d.cts +0 -1
  62. package/dist/adapters/hub/hubClient.d.mts +0 -1
  63. package/dist/adapters/hub/hubClient.mjs +0 -2
  64. package/dist/adapters/hub/hubClient.mjs.map +0 -1
  65. package/dist/adapters/pi/extension.cjs +0 -2
  66. package/dist/adapters/pi/extension.cjs.map +0 -1
  67. package/dist/adapters/pi/extension.d.cts +0 -9
  68. package/dist/adapters/pi/extension.d.cts.map +0 -1
  69. package/dist/adapters/pi/extension.d.mts +0 -9
  70. package/dist/adapters/pi/extension.d.mts.map +0 -1
  71. package/dist/adapters/pi/extension.mjs +0 -2
  72. package/dist/adapters/pi/extension.mjs.map +0 -1
  73. package/dist/adapters/pi/extensions/runWorktreeTool.cjs +0 -15
  74. package/dist/adapters/pi/extensions/runWorktreeTool.cjs.map +0 -1
  75. package/dist/adapters/pi/extensions/runWorktreeTool.mjs +0 -15
  76. package/dist/adapters/pi/extensions/runWorktreeTool.mjs.map +0 -1
  77. package/dist/adapters/web/worktreesChannel.cjs +0 -2
  78. package/dist/adapters/web/worktreesChannel.cjs.map +0 -1
  79. package/dist/adapters/web/worktreesChannel.d.cts +0 -21
  80. package/dist/adapters/web/worktreesChannel.d.cts.map +0 -1
  81. package/dist/adapters/web/worktreesChannel.d.mts +0 -21
  82. package/dist/adapters/web/worktreesChannel.d.mts.map +0 -1
  83. package/dist/adapters/web/worktreesChannel.mjs +0 -2
  84. package/dist/adapters/web/worktreesChannel.mjs.map +0 -1
  85. package/dist/adapters/worktree/dependencyInstall.cjs +0 -2
  86. package/dist/adapters/worktree/dependencyInstall.cjs.map +0 -1
  87. package/dist/adapters/worktree/dependencyInstall.mjs +0 -2
  88. package/dist/adapters/worktree/dependencyInstall.mjs.map +0 -1
  89. package/dist/adapters/worktree/gitCli.cjs +0 -4
  90. package/dist/adapters/worktree/gitCli.cjs.map +0 -1
  91. package/dist/adapters/worktree/gitCli.mjs +0 -4
  92. package/dist/adapters/worktree/gitCli.mjs.map +0 -1
  93. package/dist/adapters/worktree/repositoryIdentity.cjs +0 -2
  94. package/dist/adapters/worktree/repositoryIdentity.cjs.map +0 -1
  95. package/dist/adapters/worktree/repositoryIdentity.mjs +0 -2
  96. package/dist/adapters/worktree/repositoryIdentity.mjs.map +0 -1
  97. package/dist/adapters/worktree/worktreeOperations.cjs +0 -2
  98. package/dist/adapters/worktree/worktreeOperations.cjs.map +0 -1
  99. package/dist/adapters/worktree/worktreeOperations.d.cts +0 -32
  100. package/dist/adapters/worktree/worktreeOperations.d.cts.map +0 -1
  101. package/dist/adapters/worktree/worktreeOperations.d.mts +0 -32
  102. package/dist/adapters/worktree/worktreeOperations.d.mts.map +0 -1
  103. package/dist/adapters/worktree/worktreeOperations.mjs +0 -2
  104. package/dist/adapters/worktree/worktreeOperations.mjs.map +0 -1
  105. package/dist/adapters/worktree/worktreeRegistry.cjs +0 -2
  106. package/dist/adapters/worktree/worktreeRegistry.cjs.map +0 -1
  107. package/dist/adapters/worktree/worktreeRegistry.mjs +0 -2
  108. package/dist/adapters/worktree/worktreeRegistry.mjs.map +0 -1
  109. package/dist/commands/doomGitCommand.cjs +0 -2
  110. package/dist/commands/doomGitCommand.cjs.map +0 -1
  111. package/dist/commands/doomGitCommand.mjs +0 -2
  112. package/dist/commands/doomGitCommand.mjs.map +0 -1
  113. package/dist/container/index.cjs +0 -2
  114. package/dist/container/index.cjs.map +0 -1
  115. package/dist/container/index.d.cts +0 -6
  116. package/dist/container/index.d.cts.map +0 -1
  117. package/dist/container/index.d.mts +0 -6
  118. package/dist/container/index.d.mts.map +0 -1
  119. package/dist/container/index.mjs +0 -2
  120. package/dist/container/index.mjs.map +0 -1
  121. package/dist/extensions/pi.d.cts +0 -2
  122. package/dist/hubApi.cjs +0 -2
  123. package/dist/hubApi.cjs.map +0 -1
  124. package/dist/hubApi.d.cts +0 -6
  125. package/dist/hubApi.d.cts.map +0 -1
  126. package/dist/hubApi.d.mts +0 -6
  127. package/dist/hubApi.d.mts.map +0 -1
  128. package/dist/hubApi.mjs +0 -2
  129. package/dist/hubApi.mjs.map +0 -1
  130. package/dist/index.d.cts +0 -5
  131. package/dist/schemas/runWorktreeTool.cjs +0 -2
  132. package/dist/schemas/runWorktreeTool.cjs.map +0 -1
  133. package/dist/schemas/runWorktreeTool.mjs +0 -2
  134. package/dist/schemas/runWorktreeTool.mjs.map +0 -1
  135. package/dist/services/extensionService.cjs +0 -2
  136. package/dist/services/extensionService.cjs.map +0 -1
  137. package/dist/services/extensionService.d.cts +0 -8
  138. package/dist/services/extensionService.d.cts.map +0 -1
  139. package/dist/services/extensionService.d.mts +0 -8
  140. package/dist/services/extensionService.d.mts.map +0 -1
  141. package/dist/services/extensionService.mjs +0 -2
  142. package/dist/services/extensionService.mjs.map +0 -1
  143. package/dist/services/support/errors.cjs +0 -2
  144. package/dist/services/support/errors.cjs.map +0 -1
  145. package/dist/services/support/errors.mjs +0 -2
  146. package/dist/services/support/errors.mjs.map +0 -1
  147. package/dist/services/worktreeNaming.cjs +0 -2
  148. package/dist/services/worktreeNaming.cjs.map +0 -1
  149. package/dist/services/worktreeNaming.d.cts +0 -27
  150. package/dist/services/worktreeNaming.d.cts.map +0 -1
  151. package/dist/services/worktreeNaming.d.mts +0 -27
  152. package/dist/services/worktreeNaming.d.mts.map +0 -1
  153. package/dist/services/worktreeNaming.mjs +0 -2
  154. package/dist/services/worktreeNaming.mjs.map +0 -1
  155. package/dist/types/extension.d.cts +0 -17
  156. package/dist/types/extension.d.cts.map +0 -1
  157. package/dist/types/extension.d.mts +0 -17
  158. package/dist/types/extension.d.mts.map +0 -1
  159. package/dist/types/webWorktrees.cjs +0 -2
  160. package/dist/types/webWorktrees.cjs.map +0 -1
  161. package/dist/types/webWorktrees.mjs +0 -2
  162. package/dist/types/webWorktrees.mjs.map +0 -1
  163. package/dist/types/worktreeRegistry.d.cts +0 -41
  164. package/dist/types/worktreeRegistry.d.cts.map +0 -1
  165. package/dist/types/worktreeRegistry.d.mts +0 -41
  166. package/dist/types/worktreeRegistry.d.mts.map +0 -1
  167. package/dist/webHub.cjs +0 -1
  168. package/dist/webHub.d.cts +0 -2
  169. package/dist/webHub.d.mts +0 -2
  170. package/dist/webHub.mjs +0 -1
  171. package/src/exports/webClient.ts +0 -1
  172. package/src/types/webWorktrees.ts +0 -55
  173. package/src/types/worktreeRegistry.ts +0 -69
  174. package/src/web/components/RunWorktreeToolMessage.tsx +0 -37
  175. package/src/web/components/WorktreesActivitySection.tsx +0 -64
  176. package/src/web/components/WorktreesPanel.tsx +0 -125
  177. package/src/web/components/toolRenderers.ts +0 -7
  178. package/src/web/index.ts +0 -30
  179. package/src/web/stores/worktreesActivityStore.ts +0 -124
  180. package/src/web/tsconfig.json +0 -22
@@ -0,0 +1,1820 @@
1
+ import { _ as NETWORK_TIMEOUT_MS, a as GIT_WORKTREE_LIFECYCLE_EVENT, b as createWorktreeGit, c as isWorktreeLifecycleEvent, d as registryFile, f as readJson, g as invalidRequest, h as HubUnavailableError, i as GIT_SESSION_CHANGED_EVENT, l as credentialsFile, m as DoomGitExpectedError, n as createWorktreeRegistry, o as createWorktreeMessageInbox, p as writeJsonAtomic, r as validRefName, s as isSessionChangedEvent, t as createWorktreeOperations, u as doomGitRoot, v as READ_TIMEOUT_MS, x as runGit, y as REBASE_TIMEOUT_MS } from "../worktreeOperations-BrWG2muV.mjs";
2
+ import { a as RunWorktreeToolSchema, i as validateParams, n as RUN_WORKTREE_TOOL_NAME, r as executeRunWorktreeTool, t as RUN_WORKTREE_DESCRIPTION } from "../runWorktree-SPzJvIBt.mjs";
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+ import os from "node:os";
6
+ import { composeRootHooks, defineChannel, defineHook, defineResource, defineRoot, defineRoute, defineServerTool } from "@agimon-ai/doompi-core/extensionFile";
7
+ import { defineServerPlugin, readPackageResource } from "@agimon-ai/doompi-core/serverFacet";
8
+ import { readDoomSessionDelivery } from "@agimon-ai/doompi-session";
9
+ //#region src/extensions/workspaces/sessions/(backend)/_lib/root.server.ts
10
+ function progressResult(action, label) {
11
+ return {
12
+ content: [{
13
+ type: "text",
14
+ text: label
15
+ }],
16
+ details: { action }
17
+ };
18
+ }
19
+ function createGitSession({ context, host: serverHost }) {
20
+ if (serverHost.context.directEvents === void 0) throw new Error("Git headless facet requires the session direct event bus.");
21
+ if (serverHost.context.sessionId === void 0) throw new Error("Git headless facet requires a session identity.");
22
+ const sessionId = serverHost.context.sessionId;
23
+ const directEvents = serverHost.context.directEvents;
24
+ const messageInbox = createWorktreeMessageInbox(directEvents, sessionId);
25
+ const operations = createWorktreeOperations({
26
+ git: createWorktreeGit(),
27
+ sessionService: serverHost.context.sessionService,
28
+ messageInbox,
29
+ sessionDelivery: () => readDoomSessionDelivery(context)
30
+ });
31
+ const resources = [{
32
+ when: {
33
+ state: { "minor-mode": "help" },
34
+ attribution: {
35
+ kind: "minor",
36
+ mode: "help"
37
+ }
38
+ },
39
+ name: "doompi-git",
40
+ kind: "context",
41
+ read: () => readPackageResource(import.meta.url, "llms.txt")
42
+ }, {
43
+ name: "doompi-use-git",
44
+ kind: "skill",
45
+ read: () => readPackageResource(import.meta.url, "src/prompts/doompi-use-git/SKILL.md")
46
+ }];
47
+ const tool = {
48
+ name: RUN_WORKTREE_TOOL_NAME,
49
+ label: "Worktree",
50
+ description: RUN_WORKTREE_DESCRIPTION,
51
+ parameters: RunWorktreeToolSchema,
52
+ promptSnippet: "Create and manage Git worktree sessions",
53
+ executionMode: "serial",
54
+ async execute(_toolCallId, parameters, signal, onUpdate, execution) {
55
+ const params = validateParams(parameters);
56
+ return executeRunWorktreeTool(params, operations, execution, {
57
+ ...signal === void 0 ? {} : { signal },
58
+ onProgress: (label) => onUpdate?.(progressResult(params.action, label))
59
+ });
60
+ }
61
+ };
62
+ const changed = (_event, execution) => {
63
+ directEvents.publish(GIT_SESSION_CHANGED_EVENT, execution.sessionId, { version: 1 });
64
+ };
65
+ return {
66
+ value: {
67
+ resources,
68
+ tool,
69
+ hooks: [{
70
+ event: "tool_execution_end",
71
+ handle: changed
72
+ }, {
73
+ event: "agent_settled",
74
+ handle: changed
75
+ }]
76
+ },
77
+ onDispose: () => messageInbox.close()
78
+ };
79
+ }
80
+ const root = defineRoot(createGitSession);
81
+ //#endregion
82
+ //#region src/extensions/workspaces/sessions/(backend)/root.server.ts
83
+ var root_server_default = defineRoot(root);
84
+ //#endregion
85
+ //#region src/services/branchDiff/index.ts
86
+ /**
87
+ * What a session's checkout changed against its base, the way a pull request
88
+ * shows it: every commit since the branch left its base, plus staged, unstaged
89
+ * and untracked work.
90
+ *
91
+ * DESIGN PATTERNS:
92
+ * - git computes every diff. The counts come from `--numstat` and the hunks
93
+ * from the same unified diff, so the two always agree.
94
+ * - The base is the worktree's own recorded base when there is one, then the
95
+ * remote's default branch, then the branch's upstream, and with none of those
96
+ * only uncommitted work counts. It is always named in the UI.
97
+ * - Untracked files are read only when they are small regular files; a symlink
98
+ * shows its link text and is never followed.
99
+ *
100
+ * AVOID:
101
+ * - Passing a browser path to git without checking it against the change set.
102
+ */
103
+ /** git's well-known empty tree, the base of a repository with no commits. */
104
+ const EMPTY_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
105
+ const MAX_LISTED_FILES = 500;
106
+ const MAX_UNTRACKED_COUNTED = 200;
107
+ const MAX_FILE_BYTES = 1048576;
108
+ const MAX_ROWS = 5e3;
109
+ const BINARY_PROBE_BYTES = 8192;
110
+ const SHORT_SHA = 7;
111
+ const DEFAULT_BRANCHES = ["main", "master"];
112
+ async function read(cwd, args) {
113
+ const result = await runGit(cwd, args, { timeout: READ_TIMEOUT_MS });
114
+ return result.code === 0 ? result.stdout : void 0;
115
+ }
116
+ async function readLine$1(cwd, args) {
117
+ const out = (await read(cwd, args))?.trim();
118
+ return out === void 0 || out === "" ? void 0 : out;
119
+ }
120
+ async function commitExists(cwd, ref) {
121
+ return await readLine$1(cwd, [
122
+ "rev-parse",
123
+ "--verify",
124
+ "--quiet",
125
+ `${ref}^{commit}`
126
+ ]) !== void 0;
127
+ }
128
+ /** A local branch's upstream, so the review measures against what fetch keeps current. */
129
+ async function upstreamOf(cwd, ref) {
130
+ return readLine$1(cwd, [
131
+ "rev-parse",
132
+ "--abbrev-ref",
133
+ "--symbolic-full-name",
134
+ `${ref}@{upstream}`
135
+ ]);
136
+ }
137
+ /** The ref the review is measured against. Exported for sync, whose rebase uses the same base. */
138
+ async function resolveBaseRef(cwd, branch, recordedBaseRef) {
139
+ if (!(recordedBaseRef === void 0 || branch !== void 0 && (recordedBaseRef === branch || recordedBaseRef.endsWith(`/${branch}`))) && recordedBaseRef !== void 0 && await commitExists(cwd, recordedBaseRef)) return (await readLine$1(cwd, [
140
+ "rev-parse",
141
+ "--verify",
142
+ "--quiet",
143
+ `refs/heads/${recordedBaseRef}`
144
+ ]) !== void 0 ? await upstreamOf(cwd, recordedBaseRef) : void 0) ?? recordedBaseRef;
145
+ const originHead = await readLine$1(cwd, [
146
+ "symbolic-ref",
147
+ "--quiet",
148
+ "--short",
149
+ "refs/remotes/origin/HEAD"
150
+ ]);
151
+ if (originHead !== void 0) return originHead;
152
+ for (const name of DEFAULT_BRANCHES) if (await commitExists(cwd, `origin/${name}`)) return `origin/${name}`;
153
+ return branch === void 0 ? void 0 : upstreamOf(cwd, branch);
154
+ }
155
+ async function resolveBase(cwd, branch, recordedBaseRef) {
156
+ const ref = await resolveBaseRef(cwd, branch, recordedBaseRef);
157
+ const head = await commitExists(cwd, "HEAD");
158
+ if (ref !== void 0 && head) {
159
+ const mergeBase = await readLine$1(cwd, [
160
+ "merge-base",
161
+ ref,
162
+ "HEAD"
163
+ ]);
164
+ if (mergeBase !== void 0) return {
165
+ ref,
166
+ from: mergeBase,
167
+ mergeBase: mergeBase.slice(0, SHORT_SHA)
168
+ };
169
+ }
170
+ return { from: head ? "HEAD" : EMPTY_TREE };
171
+ }
172
+ /** NUL-separated fields from a `-z` listing, without the trailing empty one. */
173
+ function zFields(out) {
174
+ if (out === void 0 || out === "") return [];
175
+ const fields = out.split("\0");
176
+ if (fields.at(-1) === "") fields.pop();
177
+ return fields;
178
+ }
179
+ const NAME_STATUS = {
180
+ A: "added",
181
+ D: "deleted",
182
+ M: "modified",
183
+ T: "modified"
184
+ };
185
+ /** Line count of a small regular file, or undefined when it is binary, large, or not a regular file. */
186
+ function untrackedLines(root, relPath) {
187
+ const absolute = path.join(root, relPath);
188
+ let stat;
189
+ try {
190
+ stat = fs.lstatSync(absolute);
191
+ } catch {
192
+ return;
193
+ }
194
+ if (stat.isSymbolicLink()) return {
195
+ lines: 1,
196
+ binary: false
197
+ };
198
+ if (!stat.isFile() || stat.size > MAX_FILE_BYTES) return void 0;
199
+ const content = fs.readFileSync(absolute);
200
+ if (content.subarray(0, BINARY_PROBE_BYTES).includes(0)) return {
201
+ lines: 0,
202
+ binary: true
203
+ };
204
+ if (content.length === 0) return {
205
+ lines: 0,
206
+ binary: false
207
+ };
208
+ const text = content.toString("utf8");
209
+ const newlines = text.split("\n").length - 1;
210
+ return {
211
+ lines: text.endsWith("\n") ? newlines : newlines + 1,
212
+ binary: false
213
+ };
214
+ }
215
+ /** The hunks of a unified diff, numbering each row by the file it lives in. */
216
+ function parseUnifiedDiff(diff) {
217
+ const hunks = [];
218
+ let current;
219
+ let oldLine = 0;
220
+ let newLine = 0;
221
+ let rows = 0;
222
+ let binary = false;
223
+ for (const line of diff.split("\n")) {
224
+ const header = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/u.exec(line);
225
+ if (header !== null) {
226
+ oldLine = Number(header[1]);
227
+ newLine = Number(header[2]);
228
+ current = {
229
+ start: newLine,
230
+ rows: []
231
+ };
232
+ hunks.push(current);
233
+ continue;
234
+ }
235
+ if (current === void 0) {
236
+ if (line.startsWith("Binary files ") || line === "GIT binary patch") binary = true;
237
+ continue;
238
+ }
239
+ const marker = line[0];
240
+ let row;
241
+ if (marker === " ") row = {
242
+ marker: " ",
243
+ line: newLine,
244
+ content: line.slice(1)
245
+ };
246
+ else if (marker === "-") row = {
247
+ marker: "-",
248
+ line: oldLine,
249
+ content: line.slice(1)
250
+ };
251
+ else if (marker === "+") row = {
252
+ marker: "+",
253
+ line: newLine,
254
+ content: line.slice(1)
255
+ };
256
+ if (row === void 0) continue;
257
+ if (marker !== "+") oldLine += 1;
258
+ if (marker !== "-") newLine += 1;
259
+ current.rows.push(row);
260
+ rows += 1;
261
+ }
262
+ return {
263
+ hunks,
264
+ binary,
265
+ rows
266
+ };
267
+ }
268
+ function createBranchDiff() {
269
+ return {
270
+ async review(cwd, options = {}) {
271
+ const root = await readLine$1(cwd, ["rev-parse", "--show-toplevel"]);
272
+ if (root === void 0) return void 0;
273
+ const branch = await readLine$1(root, [
274
+ "symbolic-ref",
275
+ "--quiet",
276
+ "--short",
277
+ "HEAD"
278
+ ]);
279
+ const base = await resolveBase(root, branch, options.recordedBaseRef);
280
+ const numstat = zFields(await read(root, [
281
+ "diff",
282
+ "--numstat",
283
+ "-z",
284
+ "--no-renames",
285
+ base.from
286
+ ]));
287
+ const nameStatus = zFields(await read(root, [
288
+ "diff",
289
+ "--name-status",
290
+ "-z",
291
+ "--no-renames",
292
+ base.from
293
+ ]));
294
+ const unmerged = new Set(zFields(await read(root, [
295
+ "diff",
296
+ "--name-only",
297
+ "--diff-filter=U",
298
+ "-z"
299
+ ])));
300
+ const statusOf = /* @__PURE__ */ new Map();
301
+ for (let index = 0; index + 1 < nameStatus.length; index += 2) statusOf.set(nameStatus[index + 1] ?? "", NAME_STATUS[(nameStatus[index] ?? "").charAt(0)] ?? "modified");
302
+ const entries = [];
303
+ for (const field of numstat) {
304
+ const [added = "", removed = "", ...rest] = field.split(" ");
305
+ const filePath = rest.join(" ");
306
+ if (filePath === "") continue;
307
+ const binary = added === "-" && removed === "-";
308
+ entries.push({
309
+ path: filePath,
310
+ status: unmerged.has(filePath) ? "conflicted" : statusOf.get(filePath) ?? "modified",
311
+ added: binary ? 0 : Number(added),
312
+ removed: binary ? 0 : Number(removed),
313
+ ...binary ? { binary: true } : {}
314
+ });
315
+ }
316
+ const untracked = zFields(await read(root, [
317
+ "ls-files",
318
+ "--others",
319
+ "--exclude-standard",
320
+ "-z"
321
+ ]));
322
+ untracked.forEach((relPath, index) => {
323
+ const counted = index < MAX_UNTRACKED_COUNTED ? untrackedLines(root, relPath) : void 0;
324
+ entries.push({
325
+ path: relPath,
326
+ status: "untracked",
327
+ added: counted?.lines ?? 0,
328
+ removed: 0,
329
+ ...counted?.binary ? { binary: true } : {}
330
+ });
331
+ });
332
+ entries.sort((left, right) => left.path.localeCompare(right.path));
333
+ const conflicts = [...unmerged];
334
+ const rebasing = [await readLine$1(root, [
335
+ "rev-parse",
336
+ "--git-path",
337
+ "rebase-merge"
338
+ ]), await readLine$1(root, [
339
+ "rev-parse",
340
+ "--git-path",
341
+ "rebase-apply"
342
+ ])].some((gitPath) => gitPath !== void 0 && fs.existsSync(path.resolve(root, gitPath)));
343
+ const upstreamRef = branch === void 0 ? void 0 : await upstreamOf(root, branch);
344
+ let upstream;
345
+ if (upstreamRef !== void 0) {
346
+ const [behind, ahead] = (await readLine$1(root, [
347
+ "rev-list",
348
+ "--left-right",
349
+ "--count",
350
+ `${upstreamRef}...HEAD`
351
+ ]) ?? "").split(/\s+/u).map(Number);
352
+ if (Number.isInteger(ahead) && Number.isInteger(behind)) upstream = {
353
+ ref: upstreamRef,
354
+ ahead,
355
+ behind
356
+ };
357
+ }
358
+ const truncated = entries.length > MAX_LISTED_FILES || untracked.length > MAX_UNTRACKED_COUNTED;
359
+ return {
360
+ root,
361
+ base,
362
+ changes: {
363
+ added: entries.reduce((sum, entry) => sum + entry.added, 0),
364
+ removed: entries.reduce((sum, entry) => sum + entry.removed, 0),
365
+ files: entries.length,
366
+ ...branch === void 0 ? {} : { branch },
367
+ ...base.ref === void 0 ? {} : { base: base.ref },
368
+ ...truncated ? { truncated: true } : {},
369
+ ...upstream === void 0 ? {} : { upstream },
370
+ ...rebasing ? { rebase: { conflicts } } : {}
371
+ },
372
+ summary: {
373
+ repository: true,
374
+ files: entries.slice(0, MAX_LISTED_FILES),
375
+ ...branch === void 0 ? {} : { branch },
376
+ ...base.ref === void 0 ? {} : { base: base.ref },
377
+ ...base.mergeBase === void 0 ? {} : { mergeBase: base.mergeBase },
378
+ ...truncated ? { truncated: true } : {}
379
+ }
380
+ };
381
+ },
382
+ async fileDiff(review, filePath) {
383
+ const entry = review.summary.files.find((file) => file.path === filePath);
384
+ if (entry === void 0) throw new Error("That path is not in this review.");
385
+ if (entry.status === "untracked") {
386
+ const absolute = path.join(review.root, entry.path);
387
+ const stat = fs.lstatSync(absolute);
388
+ if (stat.isSymbolicLink()) return {
389
+ path: entry.path,
390
+ hunks: [{
391
+ start: 1,
392
+ rows: [{
393
+ marker: "+",
394
+ line: 1,
395
+ content: fs.readlinkSync(absolute)
396
+ }]
397
+ }]
398
+ };
399
+ if (!stat.isFile() || stat.size > MAX_FILE_BYTES) return {
400
+ path: entry.path,
401
+ hunks: [],
402
+ tooLarge: true
403
+ };
404
+ const content = fs.readFileSync(absolute);
405
+ if (content.subarray(0, BINARY_PROBE_BYTES).includes(0)) return {
406
+ path: entry.path,
407
+ hunks: [],
408
+ binary: true
409
+ };
410
+ const lines = content.toString("utf8").split("\n");
411
+ if (lines.at(-1) === "") lines.pop();
412
+ if (lines.length > MAX_ROWS) return {
413
+ path: entry.path,
414
+ hunks: [],
415
+ tooLarge: true
416
+ };
417
+ return {
418
+ path: entry.path,
419
+ hunks: lines.length === 0 ? [] : [{
420
+ start: 1,
421
+ rows: lines.map((content, index) => ({
422
+ marker: "+",
423
+ line: index + 1,
424
+ content
425
+ }))
426
+ }]
427
+ };
428
+ }
429
+ const result = await runGit(review.root, [
430
+ "diff",
431
+ "-U3",
432
+ "--no-color",
433
+ "--no-ext-diff",
434
+ "--no-textconv",
435
+ "--no-renames",
436
+ review.base.from,
437
+ "--",
438
+ `:(top,literal)${entry.path}`
439
+ ], {
440
+ timeout: READ_TIMEOUT_MS,
441
+ maxBuffer: MAX_FILE_BYTES
442
+ });
443
+ if (result.overflow) return {
444
+ path: entry.path,
445
+ hunks: [],
446
+ tooLarge: true
447
+ };
448
+ if (result.code !== 0) throw new Error(`git diff failed (exit ${String(result.code)}).`);
449
+ const parsed = parseUnifiedDiff(result.stdout);
450
+ if (parsed.binary || entry.binary === true) return {
451
+ path: entry.path,
452
+ hunks: [],
453
+ binary: true
454
+ };
455
+ if (parsed.rows > MAX_ROWS) return {
456
+ path: entry.path,
457
+ hunks: [],
458
+ tooLarge: true
459
+ };
460
+ return {
461
+ path: entry.path,
462
+ hunks: parsed.hunks
463
+ };
464
+ }
465
+ };
466
+ }
467
+ //#endregion
468
+ //#region src/services/gitAuth/index.ts
469
+ /**
470
+ * Per-workspace remote auth for doompi-git's own fetch, pull, push and rebase.
471
+ *
472
+ * DESIGN PATTERNS:
473
+ * - One file outside every repository, folder 0700 and file 0600, repaired on
474
+ * read if something loosened it. The token never leaves this module except
475
+ * into the environment of one git child process.
476
+ * - Config reaches git through GIT_CONFIG_COUNT environment entries, never
477
+ * argv, so nothing secret shows in `ps`. The first entry empties the
478
+ * credential helper list, which is what keeps macOS's osxkeychain helper from
479
+ * storing the token after a successful push.
480
+ * - The token is offered only to the host it was entered for. A repository
481
+ * whose remote points anywhere else gets this machine's own credentials.
482
+ *
483
+ * AVOID:
484
+ * - Touching process.env. The hub runs every session; anything set there
485
+ * would reach the agent's shell, which the user ruled out.
486
+ */
487
+ const OWNER_ONLY_DIRECTORY = 448;
488
+ const OWNER_ONLY_FILE = 384;
489
+ const LOOSE_BITS = 63;
490
+ const HOST_PATTERN = /^[a-z0-9.-]{1,253}(:\d{1,5})?$/u;
491
+ const MAX_USERNAME = 256;
492
+ const MAX_TOKEN = 4096;
493
+ const SETTINGS_HINT = "Open settings, then repository, then git remote.";
494
+ /** Environment that would make git log or skip what this module relies on. */
495
+ const STRIPPED_ALWAYS = ["GIT_CURL_VERBOSE", "GIT_TRACE_REDACT"];
496
+ const STRIPPED_PREFIXES = ["GIT_TRACE"];
497
+ /** Environment that could redirect or override credentials when the workspace's own are in play. */
498
+ const STRIPPED_CREDENTIALED = [
499
+ "GIT_CONFIG_PARAMETERS",
500
+ "GIT_CONFIG_COUNT",
501
+ "GIT_SSL_NO_VERIFY",
502
+ "SSH_ASKPASS",
503
+ "GIT_SSH",
504
+ "GIT_SSH_COMMAND"
505
+ ];
506
+ const CONFIG_ENTRY = /^GIT_CONFIG_(KEY|VALUE)_\d+$/u;
507
+ /** The inline helper: answers `get` from this child's environment, and ignores `store` and `erase`. */
508
+ const ENV_CREDENTIAL_HELPER = "!f(){ test \"$1\" = get && printf \"username=%s\\npassword=%s\\n\" \"$DOOMPI_GIT_USERNAME\" \"$DOOMPI_GIT_TOKEN\"; }; f";
509
+ /** Whether a value holds an ASCII control character; a newline in a credential field could add a line to git's credential protocol. */
510
+ function hasControl(value) {
511
+ for (let index = 0; index < value.length; index += 1) {
512
+ const code = value.charCodeAt(index);
513
+ if (code < 32 || code === 127) return true;
514
+ }
515
+ return false;
516
+ }
517
+ function isRecord(value) {
518
+ return typeof value === "object" && value !== null && !Array.isArray(value);
519
+ }
520
+ /** The workspace key: the canonical root, so a symlinked path and its target share one entry. */
521
+ function workspaceKey(workspaceRoot) {
522
+ try {
523
+ return fs.realpathSync(workspaceRoot);
524
+ } catch {
525
+ return path.resolve(workspaceRoot);
526
+ }
527
+ }
528
+ function expandHome(keyPath, homeDir) {
529
+ return keyPath.startsWith("~/") ? path.join(homeDir, keyPath.slice(2)) : keyPath;
530
+ }
531
+ function parseConfig(value) {
532
+ if (!isRecord(value)) return void 0;
533
+ if (value.method === "none") return { method: "none" };
534
+ if (value.method === "ssh") {
535
+ if (value.keyPath === void 0) return { method: "ssh" };
536
+ return typeof value.keyPath === "string" && value.keyPath !== "" ? {
537
+ method: "ssh",
538
+ keyPath: value.keyPath
539
+ } : void 0;
540
+ }
541
+ if (value.method === "https" && typeof value.host === "string" && typeof value.username === "string" && typeof value.token === "string" && value.token !== "") return {
542
+ method: "https",
543
+ host: value.host,
544
+ username: value.username,
545
+ token: value.token
546
+ };
547
+ }
548
+ /** What the browser may see. The token becomes a yes or no. */
549
+ function publicView(config) {
550
+ if (config.method === "ssh") return config.keyPath === void 0 ? {
551
+ method: "ssh",
552
+ ssh: {}
553
+ } : {
554
+ method: "ssh",
555
+ ssh: { keyPath: config.keyPath }
556
+ };
557
+ if (config.method === "https") return {
558
+ method: "https",
559
+ https: {
560
+ host: config.host,
561
+ username: config.username,
562
+ hasToken: config.token !== ""
563
+ }
564
+ };
565
+ return { method: "none" };
566
+ }
567
+ /**
568
+ * A browser save request, checked field by field. An absent HTTPS token keeps
569
+ * the saved one, and only for the same host: a token never follows a host it
570
+ * was not entered for.
571
+ */
572
+ function parseGitAuthSave(request, current, homeDir) {
573
+ if (!isRecord(request)) throw invalidRequest("The git auth request is not an object.", "Send method none, ssh or https.");
574
+ if (request.method === "none") return { method: "none" };
575
+ if (request.method === "ssh") {
576
+ if (request.keyPath === void 0 || request.keyPath === "") return { method: "ssh" };
577
+ if (typeof request.keyPath !== "string" || hasControl(request.keyPath)) throw invalidRequest("The key path is not a plain path.", "Give the key as an absolute path or one under ~/.");
578
+ const keyPath = request.keyPath.trim();
579
+ if (!keyPath.startsWith("/") && !keyPath.startsWith("~/")) throw invalidRequest("The key path is not absolute.", "Give the key as an absolute path or one under ~/.");
580
+ let isFile = false;
581
+ try {
582
+ isFile = fs.statSync(expandHome(keyPath, homeDir)).isFile();
583
+ } catch {
584
+ isFile = false;
585
+ }
586
+ if (!isFile) throw invalidRequest(`No key file at ${keyPath}.`, "Check the path, or leave it blank to use ssh-agent.");
587
+ return {
588
+ method: "ssh",
589
+ keyPath
590
+ };
591
+ }
592
+ if (request.method === "https") {
593
+ const host = typeof request.host === "string" ? request.host.trim().toLowerCase() : "";
594
+ if (!HOST_PATTERN.test(host)) throw invalidRequest("The host is not a host name.", "Use a host like github.com, with an optional :port.");
595
+ const username = typeof request.username === "string" ? request.username.trim() : "";
596
+ if (username === "" || username.length > MAX_USERNAME || hasControl(username)) throw invalidRequest("The username is empty or not plain text.", "Enter the username the host knows you by.");
597
+ if (request.token !== void 0 && request.token !== "") {
598
+ if (typeof request.token !== "string" || request.token.length > MAX_TOKEN || hasControl(request.token)) throw invalidRequest("The token is not plain text.", "Paste the token again without line breaks.");
599
+ return {
600
+ method: "https",
601
+ host,
602
+ username,
603
+ token: request.token
604
+ };
605
+ }
606
+ if (current.method === "https" && current.host === host && current.token !== "") return {
607
+ method: "https",
608
+ host,
609
+ username,
610
+ token: current.token
611
+ };
612
+ throw invalidRequest(current.method === "https" ? "The host changed, so the saved token cannot follow it." : "A token is required.", "Enter the personal access token for this host.");
613
+ }
614
+ throw invalidRequest("The git auth method is unknown.", "Send method none, ssh or https.");
615
+ }
616
+ /** Tightens a file or folder something left readable by group or others. */
617
+ function tighten(target, mode) {
618
+ try {
619
+ if ((fs.statSync(target).mode & LOOSE_BITS) !== 0) fs.chmodSync(target, mode);
620
+ } catch {}
621
+ }
622
+ function createGitAuthStore(homeDir = os.homedir()) {
623
+ const file = credentialsFile(homeDir);
624
+ const readFile = () => {
625
+ tighten(file, OWNER_ONLY_FILE);
626
+ const raw = readJson(file);
627
+ const workspaces = {};
628
+ if (isRecord(raw) && raw.version === 1 && isRecord(raw.workspaces)) for (const [key, value] of Object.entries(raw.workspaces)) {
629
+ const config = parseConfig(value);
630
+ if (config !== void 0) workspaces[key] = config;
631
+ }
632
+ return {
633
+ version: 1,
634
+ workspaces
635
+ };
636
+ };
637
+ const read = (workspaceRoot) => readFile().workspaces[workspaceKey(workspaceRoot)] ?? { method: "none" };
638
+ return {
639
+ read,
640
+ view: (workspaceRoot) => publicView(read(workspaceRoot)),
641
+ save(workspaceRoot, request) {
642
+ const key = workspaceKey(workspaceRoot);
643
+ const current = readFile();
644
+ const next = parseGitAuthSave(request, current.workspaces[key] ?? { method: "none" }, homeDir);
645
+ const root = doomGitRoot(homeDir);
646
+ fs.mkdirSync(root, {
647
+ recursive: true,
648
+ mode: OWNER_ONLY_DIRECTORY
649
+ });
650
+ fs.chmodSync(root, OWNER_ONLY_DIRECTORY);
651
+ const workspaces = { ...current.workspaces };
652
+ if (next.method === "none") delete workspaces[key];
653
+ else workspaces[key] = next;
654
+ writeJsonAtomic(file, {
655
+ version: 1,
656
+ workspaces
657
+ });
658
+ fs.chmodSync(file, OWNER_ONLY_FILE);
659
+ return publicView(next);
660
+ }
661
+ };
662
+ }
663
+ /** The transport and host a remote URL names, or undefined for a local path or anything else. */
664
+ function remoteTransport(url) {
665
+ const trimmed = url.trim();
666
+ if (/^[a-z][a-z0-9+.-]*:\/\//iu.test(trimmed)) try {
667
+ const parsed = new URL(trimmed);
668
+ const kind = parsed.protocol === "https:" ? "https" : parsed.protocol === "http:" ? "http" : parsed.protocol === "ssh:" ? "ssh" : void 0;
669
+ return kind === void 0 ? void 0 : {
670
+ kind,
671
+ host: parsed.host.toLowerCase()
672
+ };
673
+ } catch {
674
+ return;
675
+ }
676
+ const scp = /^(?:[^@/]+@)?([^:/]+):(?!\/\/)/u.exec(trimmed);
677
+ if (scp !== null && scp[1] !== void 0 && scp[1].length > 1) return {
678
+ kind: "ssh",
679
+ host: scp[1].toLowerCase()
680
+ };
681
+ }
682
+ /** Quotes one word for the POSIX shell git runs GIT_SSH_COMMAND through. */
683
+ function shellQuote(value) {
684
+ return `'${value.replaceAll("'", "'\\''")}'`;
685
+ }
686
+ /**
687
+ * The environment for one fetch or push against `remoteUrl`.
688
+ *
689
+ * Every call drops tracing (which can log credentials) and turns prompts off,
690
+ * because no terminal is attached. Only when the workspace's method matches the
691
+ * remote does anything secret go in, and then only into this child's env.
692
+ */
693
+ function remoteEnv(config, remoteUrl, baseEnv, homeDir) {
694
+ const env = { ...baseEnv };
695
+ for (const key of Object.keys(env)) if (STRIPPED_ALWAYS.includes(key) || STRIPPED_PREFIXES.some((prefix) => key.startsWith(prefix))) delete env[key];
696
+ env.GIT_TERMINAL_PROMPT = "0";
697
+ env.GCM_INTERACTIVE = "never";
698
+ const transport = remoteTransport(remoteUrl);
699
+ const credentialed = () => {
700
+ for (const key of Object.keys(env)) if (STRIPPED_CREDENTIALED.includes(key) || CONFIG_ENTRY.test(key)) delete env[key];
701
+ env.GIT_ASKPASS = "true";
702
+ };
703
+ if (config.method === "https") {
704
+ if (transport?.kind !== "https" || transport.host !== config.host) return {
705
+ env,
706
+ credentialed: false,
707
+ mismatch: `This workspace's token is for https://${config.host}, and this remote is not.`
708
+ };
709
+ credentialed();
710
+ env.GIT_CONFIG_COUNT = "3";
711
+ env.GIT_CONFIG_KEY_0 = "credential.helper";
712
+ env.GIT_CONFIG_VALUE_0 = "";
713
+ env.GIT_CONFIG_KEY_1 = `credential.https://${config.host}.helper`;
714
+ env.GIT_CONFIG_VALUE_1 = ENV_CREDENTIAL_HELPER;
715
+ env.GIT_CONFIG_KEY_2 = "http.sslVerify";
716
+ env.GIT_CONFIG_VALUE_2 = "true";
717
+ env.GIT_ALLOW_PROTOCOL = "https";
718
+ env.DOOMPI_GIT_USERNAME = config.username;
719
+ env.DOOMPI_GIT_TOKEN = config.token;
720
+ return {
721
+ env,
722
+ credentialed: true
723
+ };
724
+ }
725
+ if (config.method === "ssh") {
726
+ if (transport?.kind !== "ssh") return {
727
+ env,
728
+ credentialed: false,
729
+ mismatch: "This workspace uses SSH, and this remote is not an SSH remote."
730
+ };
731
+ credentialed();
732
+ env.GIT_SSH_COMMAND = [
733
+ "ssh",
734
+ ...config.keyPath === void 0 ? [] : [
735
+ "-i",
736
+ shellQuote(expandHome(config.keyPath, homeDir)),
737
+ "-o",
738
+ "IdentitiesOnly=yes"
739
+ ],
740
+ "-o",
741
+ "BatchMode=yes",
742
+ "-o",
743
+ "ConnectTimeout=15"
744
+ ].join(" ");
745
+ env.GIT_ALLOW_PROTOCOL = "ssh";
746
+ return {
747
+ env,
748
+ credentialed: true
749
+ };
750
+ }
751
+ return {
752
+ env,
753
+ credentialed: false
754
+ };
755
+ }
756
+ //#endregion
757
+ //#region src/services/newSessionRequest/index.ts
758
+ const MAX_NAME_LENGTH = 120;
759
+ /** Trimmed text, undefined when absent or blank, false when not text at all. */
760
+ function optionalText(value) {
761
+ if (value === void 0) return void 0;
762
+ if (typeof value !== "string") return false;
763
+ const trimmed = value.trim();
764
+ return trimmed === "" ? void 0 : trimmed;
765
+ }
766
+ /**
767
+ * Validates a new-session dialog request for a worktree session. Every ref is
768
+ * checked here, before it can reach git as a positional argument.
769
+ */
770
+ function newSessionRequest(body) {
771
+ if (typeof body !== "object" || body === null || Array.isArray(body)) return { error: "A request body is required." };
772
+ const record = body;
773
+ const branch = optionalText(record.branch);
774
+ if (branch === void 0 || branch === false || !validRefName(branch)) return { error: "A valid branch name is required." };
775
+ const name = optionalText(record.name);
776
+ if (name === false || name !== void 0 && name.length > MAX_NAME_LENGTH) return { error: `The session name must be text of up to ${String(MAX_NAME_LENGTH)} characters.` };
777
+ const named = name === void 0 ? {} : { name };
778
+ if (record.mode === "existing-branch") {
779
+ const remote = optionalText(record.remote);
780
+ if (remote === false || remote !== void 0 && !validRefName(remote)) return { error: "That remote name is not valid." };
781
+ return {
782
+ mode: "existing-branch",
783
+ branch,
784
+ ...remote === void 0 ? {} : { remote },
785
+ ...named
786
+ };
787
+ }
788
+ if (record.mode === "new-branch") {
789
+ const baseRef = optionalText(record.baseRef);
790
+ if (baseRef === false || baseRef !== void 0 && !validRefName(baseRef)) return { error: "That base branch is not valid." };
791
+ return {
792
+ mode: "new-branch",
793
+ branch,
794
+ ...baseRef === void 0 ? {} : { baseRef },
795
+ ...named
796
+ };
797
+ }
798
+ return { error: "The mode must be existing-branch or new-branch." };
799
+ }
800
+ //#endregion
801
+ //#region src/extensions/(backend)/api/git/_lib/route.server.ts
802
+ /**
803
+ * The worktree surface the cockpit panel calls.
804
+ *
805
+ * Hub-scoped because worktrees are a property of a repository, not of one
806
+ * session: the panel lists and creates them whether or not the session that
807
+ * made them is focused, and a session-scoped API would disappear with it.
808
+ *
809
+ * The panel names a repository by the hub's opaque id and never by path.
810
+ * `resolveRepository` turns that into an admitted root, so a browser cannot
811
+ * point this at a directory the cockpit was never given.
812
+ */
813
+ function badRequest(message) {
814
+ return Response.json({ error: message }, { status: 400 });
815
+ }
816
+ function failed(error) {
817
+ if (error instanceof DoomGitExpectedError) return Response.json({
818
+ error: error.message,
819
+ code: error.code
820
+ }, { status: error.retryable ? 503 : 409 });
821
+ return Response.json({ error: "The worktree operation failed." }, { status: 500 });
822
+ }
823
+ //#endregion
824
+ //#region src/extensions/(backend)/api/git/route.server.ts
825
+ var route_server_default = defineRoute({
826
+ basePath: "git",
827
+ start(context) {
828
+ if (context.directEvents === void 0) throw new Error("Git hub API requires the direct event bus.");
829
+ const directEvents = context.directEvents;
830
+ const publishLifecycle = (sessionId, repositoryRoot) => {
831
+ directEvents.publish(GIT_WORKTREE_LIFECYCLE_EVENT, sessionId, {
832
+ version: 1,
833
+ repositoryRoot
834
+ });
835
+ };
836
+ const git = createWorktreeGit();
837
+ const operations = createWorktreeOperations({
838
+ git,
839
+ sessionService: context.sessionService,
840
+ ...context.homeDirectory === void 0 ? {} : { homeDir: context.homeDirectory }
841
+ });
842
+ /**
843
+ * The new-session dialog's routes: the workspace's branches, and a top-level
844
+ * worktree session on one. Workspace-mounted only, and always rooted at the
845
+ * mount's own workspace, so a request can never name another directory.
846
+ */
847
+ const newSession = async (request, url) => {
848
+ const isBranches = request.method === "GET" && url.pathname === "/branches";
849
+ const isCreate = request.method === "POST" && url.pathname === "/sessions";
850
+ if (!isBranches && !isCreate) return void 0;
851
+ const workspaceRoot = context.scope === "workspace" ? context.workspaceRoot : void 0;
852
+ if (workspaceRoot === void 0) return Response.json({ error: "This route belongs to a workspace." }, { status: 404 });
853
+ if (isBranches) {
854
+ const repositoryRoot = await git.repositoryRoot(workspaceRoot);
855
+ const branches = repositoryRoot === void 0 ? {
856
+ repository: false,
857
+ local: [],
858
+ remote: []
859
+ } : {
860
+ repository: true,
861
+ ...await git.listBranches(repositoryRoot)
862
+ };
863
+ return Response.json(branches);
864
+ }
865
+ const parsed = newSessionRequest(await request.json().catch(() => void 0));
866
+ if ("error" in parsed) return badRequest(parsed.error);
867
+ const record = await operations.spawn({ cwd: workspaceRoot }, {
868
+ branch: parsed.branch,
869
+ checkout: parsed.mode === "existing-branch" ? "existing" : "new",
870
+ ...parsed.mode === "existing-branch" && parsed.remote !== void 0 ? { remote: parsed.remote } : {},
871
+ ...parsed.mode === "new-branch" && parsed.baseRef !== void 0 ? { baseRef: parsed.baseRef } : {},
872
+ ...parsed.name === void 0 ? {} : { name: parsed.name }
873
+ });
874
+ publishLifecycle(record.sessionId, record.repositoryRoot);
875
+ const created = {
876
+ sessionId: record.sessionId,
877
+ worktreeId: record.id
878
+ };
879
+ return Response.json(created, { status: 201 });
880
+ };
881
+ /** The admitted root for a request, or a response explaining why not. */
882
+ const rootOf = (url) => {
883
+ const repositoryId = url.searchParams.get("repositoryId");
884
+ if (repositoryId === null || repositoryId === "") return badRequest("A repositoryId is required.");
885
+ const root = context.resolveRepository?.(repositoryId);
886
+ if (root === void 0) return badRequest("That repository is not admitted to this cockpit.");
887
+ return root;
888
+ };
889
+ const homeDir = context.homeDirectory ?? os.homedir();
890
+ const gitAuth = createGitAuthStore(homeDir);
891
+ const branchDiff = createBranchDiff();
892
+ /**
893
+ * A workspace's remote auth. Workspace mounts only, so the step-up gate on
894
+ * `PUT /api/workspaces/<id>/plugins/git/auth` is the one way to write it.
895
+ * The view never carries the token.
896
+ */
897
+ const auth = async (request, url) => {
898
+ if (url.pathname !== "/auth") return void 0;
899
+ if (context.scope !== "workspace") return Response.json({ error: "This route belongs to a workspace." }, { status: 404 });
900
+ if (request.method !== "GET" && request.method !== "PUT") return Response.json({ error: "Use GET or PUT." }, { status: 405 });
901
+ const root = rootOf(url);
902
+ if (root instanceof Response) return root;
903
+ if (request.method === "GET") return Response.json(gitAuth.view(root));
904
+ try {
905
+ return Response.json(gitAuth.save(root, await request.json().catch(() => void 0)));
906
+ } catch (error) {
907
+ if (error instanceof DoomGitExpectedError && error.code === "invalid_request") {
908
+ const text = error.message.replace(/^\[[a-z_]+\] /u, "").replace("\nRecovery: ", " ");
909
+ return Response.json({
910
+ error: text,
911
+ code: error.code
912
+ }, { status: 400 });
913
+ }
914
+ throw error;
915
+ }
916
+ };
917
+ /**
918
+ * The session's change against its base. Session mounts only, rooted at the
919
+ * session's own checkout; a file is served only when it is in the change set
920
+ * computed for the same request.
921
+ */
922
+ const review = async (request, url) => {
923
+ if (url.pathname !== "/review" && url.pathname !== "/review/file") return void 0;
924
+ const cwd = context.scope === "session" ? context.cwd : void 0;
925
+ if (cwd === void 0) return Response.json({ error: "This route belongs to a session." }, { status: 404 });
926
+ if (request.method !== "GET") return Response.json({ error: "Use GET." }, { status: 405 });
927
+ let recordedBaseRef;
928
+ try {
929
+ recordedBaseRef = createWorktreeRegistry(registryFile(cwd, homeDir)).list().find((record) => record.sessionId === context.sessionId)?.baseRef;
930
+ } catch {
931
+ recordedBaseRef = void 0;
932
+ }
933
+ const result = await branchDiff.review(cwd, recordedBaseRef === void 0 ? {} : { recordedBaseRef });
934
+ if (url.pathname === "/review") return Response.json(result?.summary ?? {
935
+ repository: false,
936
+ files: []
937
+ });
938
+ const filePath = url.searchParams.get("path") ?? "";
939
+ if (result === void 0 || !result.summary.files.some((file) => file.path === filePath)) return Response.json({ error: "That file is not part of this review." }, { status: 404 });
940
+ return Response.json(await branchDiff.fileDiff(result, filePath));
941
+ };
942
+ return {
943
+ async fetch(request) {
944
+ const url = new URL(request.url);
945
+ try {
946
+ const answered = await newSession(request, url) ?? await auth(request, url) ?? await review(request, url);
947
+ if (answered !== void 0) return answered;
948
+ } catch (error) {
949
+ context.onNotice(`git request failed: ${error.name}`);
950
+ return failed(error);
951
+ }
952
+ const root = rootOf(url);
953
+ if (root instanceof Response) return root;
954
+ const callerContext = {
955
+ cwd: root,
956
+ sessionId: url.searchParams.get("sessionId") ?? ""
957
+ };
958
+ if ((request.method === "POST" && url.pathname === "/worktrees" || request.method === "DELETE" && /^\/worktrees\/[^/]+$/u.test(url.pathname)) && callerContext.sessionId === "") return badRequest("A sessionId is required for worktree mutations.");
959
+ try {
960
+ if (request.method === "GET" && url.pathname === "/worktrees") return Response.json({ worktrees: await operations.list(callerContext) });
961
+ if (request.method === "POST" && url.pathname === "/worktrees") {
962
+ const body = await request.json();
963
+ if (typeof body.branch !== "string" || body.branch.trim() === "") return badRequest("A branch is required.");
964
+ const record = await operations.spawn(callerContext, {
965
+ branch: body.branch,
966
+ ...body.baseRef === void 0 ? {} : { baseRef: body.baseRef },
967
+ ...body.name === void 0 ? {} : { name: body.name }
968
+ });
969
+ publishLifecycle(callerContext.sessionId, record.repositoryRoot);
970
+ return Response.json({ worktree: record }, { status: 201 });
971
+ }
972
+ const closing = /^\/worktrees\/([^/]+)$/u.exec(url.pathname);
973
+ if (request.method === "DELETE" && closing) {
974
+ const record = await operations.close(callerContext, decodeURIComponent(String(closing[1])), url.searchParams.get("force") === "true");
975
+ publishLifecycle(callerContext.sessionId, record.repositoryRoot);
976
+ return Response.json({ worktree: record });
977
+ }
978
+ return Response.json({ error: "No such worktree route." }, { status: 404 });
979
+ } catch (error) {
980
+ context.onNotice(`worktree request failed: ${error.name}`);
981
+ return failed(error);
982
+ }
983
+ },
984
+ close() {}
985
+ };
986
+ }
987
+ });
988
+ //#endregion
989
+ //#region src/services/gitFailure/index.ts
990
+ const RULES = [
991
+ {
992
+ code: "host_key_changed",
993
+ phrases: ["remote host identification has changed"],
994
+ message: "The remote's SSH host key changed since this machine last saw it.",
995
+ recovery: "Check with the host that the change is expected, then update ~/.ssh/known_hosts from a terminal.",
996
+ retryable: false
997
+ },
998
+ {
999
+ code: "host_key_unknown",
1000
+ phrases: [
1001
+ "host key verification failed",
1002
+ "no ed25519 host key is known",
1003
+ "no rsa host key is known"
1004
+ ],
1005
+ message: "This machine does not trust the remote's SSH host key yet.",
1006
+ recovery: "Run `ssh -T git@<host>` once in a terminal and accept the key, then try again.",
1007
+ retryable: false
1008
+ },
1009
+ {
1010
+ code: "auth_failed",
1011
+ phrases: [
1012
+ "authentication failed",
1013
+ "could not read username",
1014
+ "could not read password",
1015
+ "permission denied (publickey",
1016
+ "returned error: 401",
1017
+ "returned error: 403",
1018
+ "terminal prompts disabled",
1019
+ "load key",
1020
+ "unprotected private key",
1021
+ "invalid format"
1022
+ ],
1023
+ message: "The remote refused the credentials.",
1024
+ recovery: `Check this workspace's git remote auth. ${SETTINGS_HINT}`,
1025
+ retryable: false
1026
+ },
1027
+ {
1028
+ code: "repository_not_found",
1029
+ phrases: ["repository not found", "does not appear to be a git repository"],
1030
+ message: "The remote repository was not found, or these credentials cannot see it.",
1031
+ recovery: `Check the remote URL and that the credentials have access. ${SETTINGS_HINT}`,
1032
+ retryable: false
1033
+ },
1034
+ {
1035
+ code: "remote_unreachable",
1036
+ phrases: [
1037
+ "could not resolve host",
1038
+ "connection refused",
1039
+ "connection timed out",
1040
+ "operation timed out",
1041
+ "unable to access",
1042
+ "network is unreachable"
1043
+ ],
1044
+ message: "The remote could not be reached.",
1045
+ recovery: "Check the network and the remote URL, then try again.",
1046
+ retryable: true
1047
+ },
1048
+ {
1049
+ code: "push_lease_rejected",
1050
+ phrases: ["(stale info)"],
1051
+ message: "The remote branch moved since your last fetch, so the force push was refused.",
1052
+ recovery: "Pull to see the new commits, then decide whether to push again.",
1053
+ retryable: false
1054
+ },
1055
+ {
1056
+ code: "push_needs_force",
1057
+ phrases: ["(non-fast-forward)", "(fetch first)"],
1058
+ message: "The remote branch has commits yours replaced, usually after a rebase.",
1059
+ recovery: "Confirm a force push with lease, or pull first to keep the remote commits.",
1060
+ retryable: false
1061
+ },
1062
+ {
1063
+ code: "worktree_dirty",
1064
+ phrases: [
1065
+ "unstaged changes",
1066
+ "uncommitted changes",
1067
+ "would be overwritten",
1068
+ "your index contains uncommitted changes"
1069
+ ],
1070
+ message: "There are uncommitted changes in the way.",
1071
+ recovery: "Commit or stash your changes first.",
1072
+ retryable: false
1073
+ }
1074
+ ];
1075
+ function classifyGitFailure(operation, result, mismatch) {
1076
+ if (result.timedOut) return new DoomGitExpectedError("git_timeout", `git ${operation} took too long and was stopped.`, true, "Check the network, then try again.");
1077
+ const text = `${result.stderr}\n${result.stdout}`.toLowerCase();
1078
+ const rule = RULES.find((candidate) => candidate.phrases.some((phrase) => text.includes(phrase)));
1079
+ if (rule === void 0) return new DoomGitExpectedError("git_failed", `git ${operation} failed (exit ${String(result.code)}).`, false, "Run the same command in a terminal in this checkout to see what git says.");
1080
+ const recovery = rule.code === "auth_failed" && mismatch !== void 0 ? `${mismatch} ${rule.recovery}` : rule.recovery;
1081
+ return new DoomGitExpectedError(rule.code, rule.message, rule.retryable, recovery);
1082
+ }
1083
+ //#endregion
1084
+ //#region src/services/gitSync/index.ts
1085
+ /**
1086
+ * Manual pull, push and rebase for a session's checkout.
1087
+ *
1088
+ * DESIGN PATTERNS:
1089
+ * - The workspace's auth goes into the environment of the one fetch or push
1090
+ * that needs it, built fresh per call. Rebase itself runs locally and never
1091
+ * sees a credential.
1092
+ * - Fetch and rebase are separate commands, so a hung network can never leave a
1093
+ * rebase half applied.
1094
+ * - A conflicted rebase is not an error. It is left paused for the reader to
1095
+ * hand to the agent or abort, and git's own state reports it from then on.
1096
+ * - Pull and rebase refuse uncommitted tracked changes rather than stash them.
1097
+ * Untracked files only block when git says they would be overwritten.
1098
+ * - Push never forces on its own. A rejection comes back as `push_needs_force`,
1099
+ * and only a confirmed retry sends `--force-with-lease --force-if-includes`,
1100
+ * which git refuses if the remote holds commits this branch never integrated.
1101
+ */
1102
+ const DONE = { outcome: "done" };
1103
+ function expected(code, message, recovery) {
1104
+ return new DoomGitExpectedError(code, message, false, recovery);
1105
+ }
1106
+ async function readLine(cwd, args) {
1107
+ const result = await runGit(cwd, args, { timeout: READ_TIMEOUT_MS });
1108
+ const out = result.stdout.trim();
1109
+ return result.code === 0 && out !== "" ? out : void 0;
1110
+ }
1111
+ async function toplevel(cwd) {
1112
+ const root = await readLine(cwd, ["rev-parse", "--show-toplevel"]);
1113
+ if (root === void 0) throw expected("not_a_repository", "This session is not in a git checkout.", "Open the session in a git repository.");
1114
+ return root;
1115
+ }
1116
+ async function rebasing(root) {
1117
+ for (const name of ["rebase-merge", "rebase-apply"]) {
1118
+ const gitPath = await readLine(root, [
1119
+ "rev-parse",
1120
+ "--git-path",
1121
+ name
1122
+ ]);
1123
+ if (gitPath !== void 0 && fs.existsSync(path.resolve(root, gitPath))) return true;
1124
+ }
1125
+ return false;
1126
+ }
1127
+ async function conflicts(root) {
1128
+ return (await runGit(root, [
1129
+ "diff",
1130
+ "--name-only",
1131
+ "--diff-filter=U",
1132
+ "-z"
1133
+ ], { timeout: READ_TIMEOUT_MS })).stdout.split("\0").filter((file) => file !== "");
1134
+ }
1135
+ async function currentBranch(root) {
1136
+ const branch = await readLine(root, [
1137
+ "symbolic-ref",
1138
+ "--quiet",
1139
+ "--short",
1140
+ "HEAD"
1141
+ ]);
1142
+ if (branch === void 0) throw expected("detached_head", "HEAD is detached, so there is no branch to sync.", "Check out a branch first.");
1143
+ return branch;
1144
+ }
1145
+ async function guard(root, needsCleanTree) {
1146
+ if (await rebasing(root)) throw expected("rebase_in_progress", "A rebase is paused in this checkout.", "Ask the agent to resolve it, or abort the rebase.");
1147
+ const branch = await currentBranch(root);
1148
+ if (needsCleanTree) {
1149
+ if (await readLine(root, [
1150
+ "status",
1151
+ "--porcelain",
1152
+ "--untracked-files=no"
1153
+ ]) !== void 0) throw expected("worktree_dirty", "There are uncommitted changes.", "Commit or stash your changes first.");
1154
+ }
1155
+ return branch;
1156
+ }
1157
+ function createGitSync(options) {
1158
+ const baseEnv = options.baseEnv ?? process.env;
1159
+ /** One credentialed network command against `remote`, classified on failure. */
1160
+ const network = async (root, context, remote, operation, args, urlArgs = []) => {
1161
+ const url = await readLine(root, [
1162
+ "remote",
1163
+ "get-url",
1164
+ ...urlArgs,
1165
+ remote
1166
+ ]);
1167
+ if (url === void 0) throw expected("no_remote", `There is no remote named ${remote}.`, "Add the remote in a terminal first.");
1168
+ const prepared = remoteEnv(context.auth, url, baseEnv, options.homeDir);
1169
+ const result = await runGit(root, args, {
1170
+ timeout: NETWORK_TIMEOUT_MS,
1171
+ env: prepared.env
1172
+ });
1173
+ if (result.code !== 0) throw classifyGitFailure(operation, result, prepared.mismatch);
1174
+ };
1175
+ /** Rebases onto `onto`; a stop on conflicts leaves the rebase paused and says so. */
1176
+ const rebaseOnto = async (root, onto) => {
1177
+ const result = await runGit(root, ["rebase", onto], {
1178
+ timeout: REBASE_TIMEOUT_MS,
1179
+ env: {
1180
+ ...baseEnv,
1181
+ GIT_EDITOR: "true",
1182
+ GIT_TERMINAL_PROMPT: "0"
1183
+ }
1184
+ });
1185
+ if (result.code === 0) return DONE;
1186
+ if (await rebasing(root)) return {
1187
+ outcome: "paused",
1188
+ conflicts: await conflicts(root)
1189
+ };
1190
+ throw classifyGitFailure("rebase", result);
1191
+ };
1192
+ /** The remote a ref like `origin/main` or `refs/remotes/origin/main` belongs to. */
1193
+ const remoteOfRef = async (root, ref) => {
1194
+ const full = await readLine(root, [
1195
+ "rev-parse",
1196
+ "--symbolic-full-name",
1197
+ ref
1198
+ ]);
1199
+ if (full === void 0 || !full.startsWith("refs/remotes/")) return void 0;
1200
+ const rest = full.slice(13);
1201
+ const cut = rest.indexOf("/");
1202
+ return cut > 0 ? rest.slice(0, cut) : void 0;
1203
+ };
1204
+ return {
1205
+ async pull(context) {
1206
+ const root = await toplevel(context.cwd);
1207
+ const branch = await guard(root, true);
1208
+ const remote = await readLine(root, ["config", `branch.${branch}.remote`]);
1209
+ const upstream = await readLine(root, [
1210
+ "rev-parse",
1211
+ "--abbrev-ref",
1212
+ "--symbolic-full-name",
1213
+ `${branch}@{upstream}`
1214
+ ]);
1215
+ if (remote === void 0 || upstream === void 0) throw expected("no_upstream", `${branch} has no upstream to pull from.`, "Push once to set an upstream.");
1216
+ if (remote !== ".") await network(root, context, remote, "fetch", [
1217
+ "fetch",
1218
+ "--prune",
1219
+ remote
1220
+ ]);
1221
+ return rebaseOnto(root, upstream);
1222
+ },
1223
+ async push(context, force) {
1224
+ const root = await toplevel(context.cwd);
1225
+ const branch = await guard(root, false);
1226
+ const configuredRemote = await readLine(root, ["config", `branch.${branch}.remote`]);
1227
+ const merge = await readLine(root, ["config", `branch.${branch}.merge`]);
1228
+ let remote = configuredRemote;
1229
+ let target = merge;
1230
+ const args = ["push", "--porcelain"];
1231
+ if (remote === void 0 || target === void 0 || remote === ".") {
1232
+ const remotes = (await readLine(root, ["remote"]) ?? "").split("\n").filter((name) => name !== "");
1233
+ remote = remotes.includes("origin") ? "origin" : remotes.length === 1 ? remotes[0] : void 0;
1234
+ if (remote === void 0) throw expected("no_remote", "There is no remote to push to.", remotes.length === 0 ? "Add a remote in a terminal first." : "Push once from a terminal to choose the remote.");
1235
+ target = `refs/heads/${branch}`;
1236
+ args.push("--set-upstream");
1237
+ }
1238
+ if (force) args.push(`--force-with-lease=${target}`, "--force-if-includes");
1239
+ args.push(remote, `HEAD:${target}`);
1240
+ await network(root, context, remote, "push", args, ["--push"]);
1241
+ return DONE;
1242
+ },
1243
+ async rebase(context) {
1244
+ const root = await toplevel(context.cwd);
1245
+ const base = await resolveBaseRef(root, await guard(root, true), context.recordedBaseRef);
1246
+ if (base === void 0) throw expected("no_base", "No base branch to rebase onto.", "Fetch the remote, or create the worktree from a base.");
1247
+ const remote = await remoteOfRef(root, base);
1248
+ if (remote !== void 0) await network(root, context, remote, "fetch", [
1249
+ "fetch",
1250
+ "--prune",
1251
+ remote
1252
+ ]);
1253
+ return rebaseOnto(root, base);
1254
+ },
1255
+ async abortRebase(context) {
1256
+ const root = await toplevel(context.cwd);
1257
+ if (!await rebasing(root)) return DONE;
1258
+ const result = await runGit(root, ["rebase", "--abort"], { timeout: REBASE_TIMEOUT_MS });
1259
+ if (result.code !== 0) throw classifyGitFailure("rebase --abort", result);
1260
+ return DONE;
1261
+ }
1262
+ };
1263
+ }
1264
+ //#endregion
1265
+ //#region src/types/gitReview.ts
1266
+ /**
1267
+ * The cockpit's view of what this session changed against its base: the counts
1268
+ * the '# diff' group shows, what the review tab lists and draws, and the manual
1269
+ * sync that sits beside them.
1270
+ *
1271
+ * DESIGN PATTERNS:
1272
+ * - Declarations only, shared by the hub and the page.
1273
+ * - Its own channel, apart from worktrees. Worktrees are repository
1274
+ * management; the diff is this session's code change.
1275
+ * - A diff row carries one line number whose file follows from its marker: the
1276
+ * new file's for context and additions, the old file's for removals. It is
1277
+ * the same shape the shared DiffView draws.
1278
+ *
1279
+ * AVOID:
1280
+ * - Behaviour or node builtins. Browser code imports this file.
1281
+ */
1282
+ /** The session channel carrying this checkout's changes and the sync around them. */
1283
+ const GIT_CHANGES_TYPE = "git_changes";
1284
+ //#endregion
1285
+ //#region src/extensions/(backend)/channel/_lib/git-changes.server.ts
1286
+ /**
1287
+ * The '# diff' data channel: this session's change against its base, and the
1288
+ * manual sync commands the review tab sends back through it.
1289
+ *
1290
+ * DESIGN PATTERNS:
1291
+ * - Apart from the worktrees channel. Worktrees are repository management;
1292
+ * this is the session's own code change.
1293
+ * - Push, not poll. The session facet publishes a "changed" event after every
1294
+ * tool call and settled turn; recounts are coalesced to one in flight plus
1295
+ * one rerun, so a burst of tool calls costs at most two.
1296
+ * - One sync command per session. A rebase and a push must never overlap.
1297
+ * - Auth is read fresh for each command from the session's workspace, and goes
1298
+ * only into that command's git child.
1299
+ *
1300
+ * AVOID:
1301
+ * - Echoing git output. Failures go through classifyGitFailure, which matches
1302
+ * stderr and drops it, because a remote URL in it can carry a token.
1303
+ */
1304
+ function payloadOf$1(state) {
1305
+ return {
1306
+ ...state.changes === void 0 ? {} : { changes: state.changes },
1307
+ ...state.pending === void 0 ? {} : { pending: state.pending },
1308
+ ...state.error === void 0 ? {} : { error: state.error },
1309
+ ...state.errorTarget === void 0 ? {} : { errorTarget: state.errorTarget }
1310
+ };
1311
+ }
1312
+ /** A command the page sent, or undefined when the frame was not one. */
1313
+ function parseChangesCommand(payload) {
1314
+ if (typeof payload !== "object" || payload === null) return void 0;
1315
+ const value = payload;
1316
+ if (value.action === "pull" || value.action === "rebase" || value.action === "abort-rebase") return { action: value.action };
1317
+ if (value.action === "push") return value.force === true ? {
1318
+ action: "push",
1319
+ force: true
1320
+ } : { action: "push" };
1321
+ }
1322
+ const PENDING = {
1323
+ pull: "pulling…",
1324
+ push: "pushing…",
1325
+ rebase: "rebasing…",
1326
+ "abort-rebase": "aborting the rebase…"
1327
+ };
1328
+ /** The error target a failure maps to: a rejected plain push asks for a confirmed lease force. */
1329
+ function errorTargetFor$1(command, error) {
1330
+ if (command.action !== "push") return { action: command.action };
1331
+ return error instanceof DoomGitExpectedError && error.code === "push_needs_force" && command.force !== true ? {
1332
+ action: "push",
1333
+ forceRequired: true
1334
+ } : { action: "push" };
1335
+ }
1336
+ function failureText$1(error) {
1337
+ if (error instanceof DoomGitExpectedError) return error.message.replace(/^\[[a-z_]+\] /u, "").replace("\nRecovery: ", " ");
1338
+ return "The git command failed.";
1339
+ }
1340
+ function createGitChangesChannel(options = {}) {
1341
+ let receiveCommand;
1342
+ return {
1343
+ frameType: GIT_CHANGES_TYPE,
1344
+ start(host) {
1345
+ const homeDir = options.homeDir ?? os.homedir();
1346
+ const diff = options.diff ?? createBranchDiff();
1347
+ const sync = options.sync ?? createGitSync({ homeDir });
1348
+ const auth = options.auth ?? createGitAuthStore(homeDir);
1349
+ const latest = /* @__PURE__ */ new Map();
1350
+ const subscriptions = /* @__PURE__ */ new Map();
1351
+ const scopes = /* @__PURE__ */ new Map();
1352
+ const inFlight = /* @__PURE__ */ new Set();
1353
+ const rerun = /* @__PURE__ */ new Set();
1354
+ const busy = /* @__PURE__ */ new Set();
1355
+ /** The base the session's own worktree was created from, when it has one. */
1356
+ const recordedBaseRef = (scope) => {
1357
+ try {
1358
+ return createWorktreeRegistry(registryFile(scope.cwd, homeDir)).list().find((record) => record.sessionId === scope.sessionId)?.baseRef;
1359
+ } catch {
1360
+ return;
1361
+ }
1362
+ };
1363
+ /** Republishes a session, skipping a frame that would say nothing new. */
1364
+ const publish = (sessionId, next) => {
1365
+ const previous = latest.get(sessionId);
1366
+ latest.set(sessionId, next);
1367
+ if (previous !== void 0 && JSON.stringify(payloadOf$1(previous)) === JSON.stringify(payloadOf$1(next))) return;
1368
+ host.publish(sessionId, payloadOf$1(next));
1369
+ };
1370
+ const update = (sessionId, patch) => {
1371
+ const previous = latest.get(sessionId) ?? {
1372
+ changes: void 0,
1373
+ pending: void 0,
1374
+ error: void 0,
1375
+ errorTarget: void 0
1376
+ };
1377
+ publish(sessionId, {
1378
+ ...previous,
1379
+ ...patch
1380
+ });
1381
+ };
1382
+ /** Recounts one session; a request while one runs schedules exactly one more. */
1383
+ const refresh = (sessionId) => {
1384
+ const scope = scopes.get(sessionId);
1385
+ if (scope === void 0) return;
1386
+ if (inFlight.has(sessionId)) {
1387
+ rerun.add(sessionId);
1388
+ return;
1389
+ }
1390
+ inFlight.add(sessionId);
1391
+ const base = recordedBaseRef(scope);
1392
+ diff.review(scope.cwd, base === void 0 ? {} : { recordedBaseRef: base }).then((review) => {
1393
+ if (scopes.has(sessionId)) update(sessionId, { changes: review?.changes });
1394
+ }).catch((error) => {
1395
+ host.onNotice(`git diff refresh failed for ${sessionId} (${error instanceof Error ? error.name : "unknown"})`);
1396
+ }).finally(() => {
1397
+ inFlight.delete(sessionId);
1398
+ if (rerun.delete(sessionId)) refresh(sessionId);
1399
+ });
1400
+ };
1401
+ const run = (scope, command) => {
1402
+ const workspaceRoot = scope.sessionContext?.workspaceRoot ?? scope.cwd;
1403
+ const base = recordedBaseRef(scope);
1404
+ const context = {
1405
+ cwd: scope.cwd,
1406
+ auth: auth.read(workspaceRoot),
1407
+ ...base === void 0 ? {} : { recordedBaseRef: base }
1408
+ };
1409
+ if (command.action === "pull") return sync.pull(context);
1410
+ if (command.action === "push") return sync.push(context, command.force === true);
1411
+ if (command.action === "rebase") return sync.rebase(context);
1412
+ return sync.abortRebase(context);
1413
+ };
1414
+ const source = {
1415
+ payloadFor(scope) {
1416
+ const state = latest.get(scope.sessionId);
1417
+ return state === void 0 ? void 0 : payloadOf$1(state);
1418
+ },
1419
+ sessionAdded(scope) {
1420
+ scopes.set(scope.sessionId, scope);
1421
+ refresh(scope.sessionId);
1422
+ const unsubscribe = host.directEvents.subscribe(GIT_SESSION_CHANGED_EVENT, scope.sessionId, (payload) => {
1423
+ if (isSessionChangedEvent(payload)) refresh(scope.sessionId);
1424
+ });
1425
+ subscriptions.set(scope.sessionId, unsubscribe);
1426
+ },
1427
+ sessionRemoved(sessionId) {
1428
+ subscriptions.get(sessionId)?.();
1429
+ subscriptions.delete(sessionId);
1430
+ scopes.delete(sessionId);
1431
+ latest.delete(sessionId);
1432
+ rerun.delete(sessionId);
1433
+ busy.delete(sessionId);
1434
+ },
1435
+ close() {
1436
+ for (const unsubscribe of subscriptions.values()) unsubscribe();
1437
+ subscriptions.clear();
1438
+ scopes.clear();
1439
+ latest.clear();
1440
+ rerun.clear();
1441
+ busy.clear();
1442
+ }
1443
+ };
1444
+ receiveCommand = (scope, payload) => {
1445
+ const command = parseChangesCommand(payload);
1446
+ if (command === void 0 || !scopes.has(scope.sessionId)) return;
1447
+ if (busy.has(scope.sessionId)) return;
1448
+ busy.add(scope.sessionId);
1449
+ update(scope.sessionId, {
1450
+ pending: PENDING[command.action],
1451
+ error: void 0,
1452
+ errorTarget: void 0
1453
+ });
1454
+ run(scope, command).then(() => update(scope.sessionId, { pending: void 0 })).catch((error) => {
1455
+ host.onNotice(`git ${command.action} failed for ${scope.sessionId} (${error instanceof DoomGitExpectedError ? error.code : "unexpected"})`);
1456
+ update(scope.sessionId, {
1457
+ pending: void 0,
1458
+ error: failureText$1(error),
1459
+ errorTarget: errorTargetFor$1(command, error)
1460
+ });
1461
+ }).finally(() => {
1462
+ busy.delete(scope.sessionId);
1463
+ refresh(scope.sessionId);
1464
+ });
1465
+ };
1466
+ return source;
1467
+ },
1468
+ receive(scope, payload) {
1469
+ receiveCommand?.(scope, payload);
1470
+ }
1471
+ };
1472
+ }
1473
+ //#endregion
1474
+ //#region src/extensions/(backend)/channel/git-changes.server.ts
1475
+ var git_changes_server_default = defineChannel(createGitChangesChannel);
1476
+ //#endregion
1477
+ //#region src/types/webWorktrees.ts
1478
+ /**
1479
+ * The cockpit's view of this session's worktrees.
1480
+ *
1481
+ * DESIGN PATTERNS:
1482
+ * - Declarations only. The channel frame type is a constant so the hub and the
1483
+ * page cannot drift apart on a string.
1484
+ * - A view is the subset of a record the dock needs, not the record. The
1485
+ * registry may hold paths and provenance the page has no business rendering.
1486
+ *
1487
+ * AVOID:
1488
+ * - Adding behaviour here. This file is imported by browser code, so it must
1489
+ * stay free of node builtins.
1490
+ */
1491
+ /** The data channel carrying one session's worktrees to the page. */
1492
+ const GIT_WORKTREES_TYPE = "git_worktrees";
1493
+ //#endregion
1494
+ //#region src/extensions/(backend)/channel/_lib/git-worktrees.server.ts
1495
+ function payloadOf(state) {
1496
+ return {
1497
+ worktrees: state.worktrees,
1498
+ ...state.pending === void 0 ? {} : { pending: state.pending },
1499
+ ...state.error === void 0 ? {} : { error: state.error },
1500
+ ...state.errorTarget === void 0 ? {} : { errorTarget: state.errorTarget }
1501
+ };
1502
+ }
1503
+ /** Worktrees visible to this session, using the hub's live session table for ownership. */
1504
+ function viewsFor(scope, deps) {
1505
+ try {
1506
+ return createWorktreeRegistry(registryFile(scope.cwd, deps.homeDir)).list().flatMap((record) => {
1507
+ if (record.parentSessionId === void 0 && record.sessionId !== scope.sessionId) return [];
1508
+ const owned = record.parentSessionId === void 0 || record.parentSessionId === scope.sessionId;
1509
+ const unowned = !owned && !deps.sessionService.isLive(record.parentSessionId);
1510
+ if (!owned && !unowned) return [];
1511
+ return [{
1512
+ id: record.id,
1513
+ branch: record.branch,
1514
+ path: record.path,
1515
+ sessionId: record.status === "orphaned" ? null : record.sessionId,
1516
+ orphaned: record.status === "orphaned",
1517
+ unowned
1518
+ }];
1519
+ });
1520
+ } catch {
1521
+ return [];
1522
+ }
1523
+ }
1524
+ function sameList(left, right) {
1525
+ if (left.length !== right.length) return false;
1526
+ return left.every((entry, index) => {
1527
+ const other = right[index];
1528
+ return other !== void 0 && entry.id === other.id && entry.branch === other.branch && entry.sessionId === other.sessionId && entry.orphaned === other.orphaned && entry.unowned === other.unowned;
1529
+ });
1530
+ }
1531
+ function errorTargetFor(command) {
1532
+ return command.action === "create" ? { action: "create" } : {
1533
+ action: "close",
1534
+ id: command.id
1535
+ };
1536
+ }
1537
+ /** A command the page sent, or undefined when the frame was not one. */
1538
+ function parseCommand(payload) {
1539
+ if (typeof payload !== "object" || payload === null) return void 0;
1540
+ const value = payload;
1541
+ if (value.action === "create") {
1542
+ if (typeof value.branch !== "string" || value.branch.trim() === "") return void 0;
1543
+ if (value.reservationId !== void 0 && (typeof value.reservationId !== "string" || !/^[A-Za-z0-9_-]+$/u.test(value.reservationId))) return void 0;
1544
+ return {
1545
+ action: "create",
1546
+ branch: value.branch.trim(),
1547
+ ...typeof value.baseRef === "string" && value.baseRef.trim() !== "" ? { baseRef: value.baseRef.trim() } : {},
1548
+ ...typeof value.reservationId === "string" ? { reservationId: value.reservationId } : {}
1549
+ };
1550
+ }
1551
+ if (value.action === "close") {
1552
+ if (typeof value.id !== "string" || value.id === "") return void 0;
1553
+ return {
1554
+ action: "close",
1555
+ id: value.id,
1556
+ ...value.force === true ? { force: true } : {}
1557
+ };
1558
+ }
1559
+ }
1560
+ /**
1561
+ * What the reader is told when a command fails.
1562
+ *
1563
+ * An expected failure explains itself. Anything else is summarised, never
1564
+ * echoed: a git error can carry a remote URL with a token in it.
1565
+ */
1566
+ function failureText(error) {
1567
+ return error instanceof DoomGitExpectedError ? error.message : "The worktree operation failed.";
1568
+ }
1569
+ /** Starts one direct-event subscription per managed session. */
1570
+ function createWorktreesChannel(options = {}) {
1571
+ let receiveCommand;
1572
+ return {
1573
+ frameType: GIT_WORKTREES_TYPE,
1574
+ start(host) {
1575
+ const sessionService = host.sessionService;
1576
+ if (sessionService === void 0) throw new HubUnavailableError("The cockpit session service is unavailable. Start the cockpit and try again.");
1577
+ const worktrees = options.operations ?? createWorktreeOperations({
1578
+ git: createWorktreeGit(),
1579
+ sessionService
1580
+ });
1581
+ /** Announces a successful mutation to this session's lifecycle subscribers. */
1582
+ const publishLifecycle = (scope, repositoryRoot) => {
1583
+ host.directEvents.publish(GIT_WORKTREE_LIFECYCLE_EVENT, scope.sessionId, {
1584
+ version: 1,
1585
+ repositoryRoot
1586
+ });
1587
+ };
1588
+ const unregisterWorktreeProvisioner = sessionService.registerReservedWorktreeProvisioner?.(async ({ reservationId, parentSessionId, signal }) => {
1589
+ const parent = host.sessions().find((scope) => scope.sessionId === parentSessionId);
1590
+ if (parent === void 0) throw new Error("The conversation parent session is unavailable.");
1591
+ const branch = `doompi/conversation-${reservationId.slice(0, 12)}`;
1592
+ const record = await worktrees.spawn({
1593
+ cwd: parent.cwd,
1594
+ sessionId: parent.sessionId
1595
+ }, {
1596
+ branch,
1597
+ name: `conversation ${reservationId.slice(0, 8)}`,
1598
+ reservationId
1599
+ }, { signal });
1600
+ const reservations = sessionService.reservations;
1601
+ if (reservations === void 0) throw new Error("Conversation reservations are unavailable.");
1602
+ const session = await reservations.complete(reservationId, parentSessionId);
1603
+ publishLifecycle(parent, record.repositoryRoot);
1604
+ return session;
1605
+ });
1606
+ const views = {
1607
+ homeDir: options.homeDir,
1608
+ sessionService
1609
+ };
1610
+ const latest = /* @__PURE__ */ new Map();
1611
+ const subscriptions = /* @__PURE__ */ new Map();
1612
+ const busy = /* @__PURE__ */ new Set();
1613
+ const generations = /* @__PURE__ */ new Map();
1614
+ const newGeneration = (sessionId) => {
1615
+ const generation = Symbol(sessionId);
1616
+ generations.set(sessionId, generation);
1617
+ return generation;
1618
+ };
1619
+ /** Republishes a session, skipping a frame that would say nothing new. */
1620
+ const publish = (scope, force) => {
1621
+ const previous = latest.get(scope.sessionId);
1622
+ const next = {
1623
+ worktrees: viewsFor(scope, views),
1624
+ pending: previous?.pending,
1625
+ error: previous?.error,
1626
+ errorTarget: previous?.errorTarget
1627
+ };
1628
+ if (!force && previous !== void 0 && sameList(previous.worktrees, next.worktrees) && previous.pending === next.pending && previous.error === next.error && previous.errorTarget === next.errorTarget) return;
1629
+ latest.set(scope.sessionId, next);
1630
+ host.publish(scope.sessionId, payloadOf(next));
1631
+ };
1632
+ const mark = (scope, pending, error, errorTarget) => {
1633
+ const previous = latest.get(scope.sessionId);
1634
+ latest.set(scope.sessionId, {
1635
+ worktrees: previous?.worktrees ?? viewsFor(scope, views),
1636
+ pending,
1637
+ error,
1638
+ errorTarget
1639
+ });
1640
+ publish(scope, true);
1641
+ };
1642
+ const run = async (scope, command, generation) => {
1643
+ const context = {
1644
+ cwd: scope.cwd,
1645
+ sessionId: scope.sessionId
1646
+ };
1647
+ const markCurrent = (pending, error, errorTarget) => {
1648
+ if (generations.get(scope.sessionId) !== generation) return;
1649
+ mark(scope, pending, error, errorTarget);
1650
+ };
1651
+ if (command.action === "create") {
1652
+ markCurrent(`creating ${command.branch}…`, void 0, void 0);
1653
+ const record = await worktrees.spawn(context, {
1654
+ branch: command.branch,
1655
+ ...command.baseRef === void 0 ? {} : { baseRef: command.baseRef },
1656
+ ...command.reservationId === void 0 ? {} : { reservationId: command.reservationId }
1657
+ }, { onProgress: (label) => markCurrent(label, void 0, void 0) });
1658
+ if (generations.get(scope.sessionId) !== generation) return;
1659
+ publishLifecycle(scope, record.repositoryRoot);
1660
+ return;
1661
+ }
1662
+ markCurrent(`closing ${command.id}…`, void 0, void 0);
1663
+ const record = await worktrees.close(context, command.id, command.force ?? false);
1664
+ if (generations.get(scope.sessionId) !== generation) return;
1665
+ publishLifecycle(scope, record.repositoryRoot);
1666
+ };
1667
+ const source = {
1668
+ payloadFor(scope) {
1669
+ const state = latest.get(scope.sessionId);
1670
+ return state === void 0 ? void 0 : payloadOf(state);
1671
+ },
1672
+ sessionAdded(scope) {
1673
+ newGeneration(scope.sessionId);
1674
+ publish(scope, false);
1675
+ const unsubscribe = host.directEvents.subscribe(GIT_WORKTREE_LIFECYCLE_EVENT, scope.sessionId, (payload) => {
1676
+ if (!isWorktreeLifecycleEvent(payload)) return;
1677
+ let sameRepository = false;
1678
+ try {
1679
+ sameRepository = registryFile(scope.cwd, options.homeDir) === registryFile(payload.repositoryRoot, options.homeDir);
1680
+ } catch {
1681
+ sameRepository = false;
1682
+ }
1683
+ if (sameRepository) publish(scope, false);
1684
+ });
1685
+ subscriptions.set(scope.sessionId, unsubscribe);
1686
+ },
1687
+ sessionRemoved(sessionId) {
1688
+ newGeneration(sessionId);
1689
+ subscriptions.get(sessionId)?.();
1690
+ subscriptions.delete(sessionId);
1691
+ latest.delete(sessionId);
1692
+ busy.delete(sessionId);
1693
+ },
1694
+ close() {
1695
+ unregisterWorktreeProvisioner?.();
1696
+ for (const unsubscribe of subscriptions.values()) unsubscribe();
1697
+ subscriptions.clear();
1698
+ latest.clear();
1699
+ busy.clear();
1700
+ generations.clear();
1701
+ }
1702
+ };
1703
+ receiveCommand = (scope, payload) => {
1704
+ const command = parseCommand(payload);
1705
+ if (command === void 0) return;
1706
+ if (busy.has(scope.sessionId)) return;
1707
+ const generation = generations.get(scope.sessionId);
1708
+ if (generation === void 0) return;
1709
+ busy.add(scope.sessionId);
1710
+ run(scope, command, generation).then(() => {
1711
+ if (generations.get(scope.sessionId) !== generation) return;
1712
+ mark(scope, void 0, void 0, void 0);
1713
+ }).catch((error) => {
1714
+ if (generations.get(scope.sessionId) !== generation) return;
1715
+ host.onNotice(`worktree command failed for ${scope.sessionId} (${failureText(error)})`);
1716
+ mark(scope, void 0, failureText(error), errorTargetFor(command));
1717
+ }).finally(() => {
1718
+ if (generations.get(scope.sessionId) === generation) busy.delete(scope.sessionId);
1719
+ });
1720
+ };
1721
+ return source;
1722
+ },
1723
+ receive(scope, payload) {
1724
+ receiveCommand?.(scope, payload);
1725
+ }
1726
+ };
1727
+ }
1728
+ //#endregion
1729
+ //#region src/extensions/(backend)/channel/git-worktrees.server.ts
1730
+ var git_worktrees_server_default = defineChannel(createWorktreesChannel);
1731
+ //#endregion
1732
+ //#region src/extensions/workspaces/sessions/(backend)/hook/agent-settled.server.ts
1733
+ var agent_settled_server_default = defineHook((context) => context.root.hooks[1]);
1734
+ //#endregion
1735
+ //#region src/extensions/workspaces/sessions/(backend)/hook/tool-execution-end.server.ts
1736
+ var tool_execution_end_server_default = defineHook((context) => context.root.hooks[0]);
1737
+ //#endregion
1738
+ //#region src/extensions/workspaces/sessions/(backend)/resource/_lib/doompiGit.server.ts
1739
+ var doompiGit_server_default$1 = (context) => context.root.resources[0];
1740
+ //#endregion
1741
+ //#region src/extensions/workspaces/sessions/(backend)/resource/doompiGit.server.ts
1742
+ var doompiGit_server_default = defineResource(doompiGit_server_default$1);
1743
+ //#endregion
1744
+ //#region src/extensions/workspaces/sessions/(backend)/resource/_lib/doompiGitReadme.server.ts
1745
+ var doompiGitReadme_server_default$1 = (context) => context.root.resources[2];
1746
+ //#endregion
1747
+ //#region src/extensions/workspaces/sessions/(backend)/resource/doompiGitReadme.server.ts
1748
+ var doompiGitReadme_server_default = defineResource(doompiGitReadme_server_default$1);
1749
+ //#endregion
1750
+ //#region src/extensions/workspaces/sessions/(backend)/resource/_lib/doompiUseGit.server.ts
1751
+ var doompiUseGit_server_default$1 = (context) => context.root.resources[1];
1752
+ //#endregion
1753
+ //#region src/extensions/workspaces/sessions/(backend)/resource/doompiUseGit.server.ts
1754
+ var doompiUseGit_server_default = defineResource(doompiUseGit_server_default$1);
1755
+ //#endregion
1756
+ //#region src/extensions/workspaces/sessions/(backend)/tool/runWorktree.server.ts
1757
+ var runWorktree_server_default = defineServerTool((context) => context.root.tool);
1758
+ //#endregion
1759
+ //#region generated/server.ts
1760
+ const at = (value, context) => typeof value === "function" ? value(context) : value;
1761
+ const via = (identity, value) => value === void 0 ? void 0 : {
1762
+ ...identity,
1763
+ ...value
1764
+ };
1765
+ const facet = defineServerPlugin({
1766
+ name: "@agimon-ai/doompi-git",
1767
+ global: (context) => ({
1768
+ get api() {
1769
+ return [at(route_server_default, context)];
1770
+ },
1771
+ get channels() {
1772
+ return [() => via({ frameType: "git_changes" }, git_changes_server_default()), () => via({ frameType: "git_worktrees" }, git_worktrees_server_default())];
1773
+ }
1774
+ }),
1775
+ workspace: (context) => ({
1776
+ get api() {
1777
+ return [at(route_server_default, context)];
1778
+ },
1779
+ get channels() {
1780
+ return [() => via({ frameType: "git_changes" }, git_changes_server_default()), () => via({ frameType: "git_worktrees" }, git_worktrees_server_default())];
1781
+ }
1782
+ }),
1783
+ session: async (context) => {
1784
+ const scopeSession = await root_server_default(context);
1785
+ const contextSession = {
1786
+ ...context,
1787
+ root: scopeSession.value
1788
+ };
1789
+ return {
1790
+ ...composeRootHooks(scopeSession),
1791
+ get api() {
1792
+ return [at(route_server_default, context)];
1793
+ },
1794
+ get channels() {
1795
+ return [() => via({ frameType: "git_changes" }, git_changes_server_default()), () => via({ frameType: "git_worktrees" }, git_worktrees_server_default())];
1796
+ },
1797
+ get hooks() {
1798
+ return [via({ event: "agent_settled" }, at(agent_settled_server_default, contextSession)), via({ event: "tool_execution_end" }, at(tool_execution_end_server_default, contextSession))];
1799
+ },
1800
+ get resources() {
1801
+ return [
1802
+ at(doompiGit_server_default, contextSession),
1803
+ at(doompiGitReadme_server_default, contextSession),
1804
+ at(doompiUseGit_server_default, contextSession)
1805
+ ];
1806
+ },
1807
+ get tools() {
1808
+ return [via({ name: "run_worktree" }, at(runWorktree_server_default, contextSession))];
1809
+ },
1810
+ services: [...scopeSession.services ?? []],
1811
+ get activities() {
1812
+ return [...scopeSession.activities ?? []];
1813
+ }
1814
+ };
1815
+ }
1816
+ });
1817
+ //#endregion
1818
+ export { facet as default, facet };
1819
+
1820
+ //# sourceMappingURL=server.mjs.map