@bevel-software/platform-core-backend 0.29.0 → 0.30.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 (181) hide show
  1. package/dist/__tests__/test-repositories.d.ts +3 -0
  2. package/dist/__tests__/test-repositories.d.ts.map +1 -0
  3. package/dist/__tests__/test-repositories.js +18 -0
  4. package/dist/__tests__/test-repositories.js.map +1 -0
  5. package/dist/core/create-core-server.js +1 -1
  6. package/dist/core/create-core-server.js.map +1 -1
  7. package/dist/core/create-core-services.d.ts +3 -0
  8. package/dist/core/create-core-services.d.ts.map +1 -1
  9. package/dist/core/create-core-services.js +32 -4
  10. package/dist/core/create-core-services.js.map +1 -1
  11. package/dist/core/lifecycle.d.ts +8 -0
  12. package/dist/core/lifecycle.d.ts.map +1 -1
  13. package/dist/core/lifecycle.js +4 -0
  14. package/dist/core/lifecycle.js.map +1 -1
  15. package/dist/modules/access/access-control.interface.d.ts +20 -0
  16. package/dist/modules/access/access-control.interface.d.ts.map +1 -1
  17. package/dist/modules/access/access-control.service.d.ts +32 -1
  18. package/dist/modules/access/access-control.service.d.ts.map +1 -1
  19. package/dist/modules/access/access-control.service.js +138 -16
  20. package/dist/modules/access/access-control.service.js.map +1 -1
  21. package/dist/modules/access/access.routes.d.ts.map +1 -1
  22. package/dist/modules/access/access.routes.js +20 -6
  23. package/dist/modules/access/access.routes.js.map +1 -1
  24. package/dist/modules/agent-instructions/shared-file-rules.d.ts +1 -1
  25. package/dist/modules/agent-instructions/shared-file-rules.js +1 -1
  26. package/dist/modules/diff/diff.service.d.ts.map +1 -1
  27. package/dist/modules/diff/diff.service.js +7 -1
  28. package/dist/modules/diff/diff.service.js.map +1 -1
  29. package/dist/modules/kb-fs/git-dir.d.ts +21 -0
  30. package/dist/modules/kb-fs/git-dir.d.ts.map +1 -0
  31. package/dist/modules/kb-fs/git-dir.js +58 -0
  32. package/dist/modules/kb-fs/git-dir.js.map +1 -0
  33. package/dist/modules/kb-fs/index.d.ts +2 -0
  34. package/dist/modules/kb-fs/index.d.ts.map +1 -1
  35. package/dist/modules/kb-fs/index.js +2 -1
  36. package/dist/modules/kb-fs/index.js.map +1 -1
  37. package/dist/modules/kb-fs/ref-files.d.ts +63 -0
  38. package/dist/modules/kb-fs/ref-files.d.ts.map +1 -0
  39. package/dist/modules/kb-fs/ref-files.js +249 -0
  40. package/dist/modules/kb-fs/ref-files.js.map +1 -0
  41. package/dist/modules/plugins/teams.routes.d.ts +4 -0
  42. package/dist/modules/plugins/teams.routes.d.ts.map +1 -1
  43. package/dist/modules/plugins/teams.routes.js +9 -1
  44. package/dist/modules/plugins/teams.routes.js.map +1 -1
  45. package/dist/modules/tool-auth/internal-token.service.d.ts +1 -1
  46. package/dist/modules/tool-auth/tool-auth.middleware.d.ts +1 -2
  47. package/dist/modules/tool-auth/tool-auth.middleware.d.ts.map +1 -1
  48. package/dist/modules/tool-auth/tool-auth.middleware.js +1 -2
  49. package/dist/modules/tool-auth/tool-auth.middleware.js.map +1 -1
  50. package/dist/modules/tool-helpers/tool-handler.d.ts +2 -2
  51. package/dist/modules/tool-helpers/tool.contract.d.ts +2 -2
  52. package/dist/modules/workflow/git/git.service.d.ts +29 -0
  53. package/dist/modules/workflow/git/git.service.d.ts.map +1 -1
  54. package/dist/modules/workflow/git/git.service.js +177 -24
  55. package/dist/modules/workflow/git/git.service.js.map +1 -1
  56. package/dist/modules/workflow/workflow-hooks.d.ts +1 -1
  57. package/dist/modules/workspace/agent-access.gate.d.ts +2 -2
  58. package/dist/modules/workspace/agent-access.gate.js +2 -2
  59. package/dist/modules/workspace/bounded-matcher.d.ts +79 -0
  60. package/dist/modules/workspace/bounded-matcher.d.ts.map +1 -0
  61. package/dist/modules/workspace/bounded-matcher.js +261 -0
  62. package/dist/modules/workspace/bounded-matcher.js.map +1 -0
  63. package/dist/modules/workspace/fetch-instant.d.ts +9 -0
  64. package/dist/modules/workspace/fetch-instant.d.ts.map +1 -0
  65. package/dist/modules/workspace/fetch-instant.js +13 -0
  66. package/dist/modules/workspace/fetch-instant.js.map +1 -0
  67. package/dist/modules/workspace/index.d.ts +7 -0
  68. package/dist/modules/workspace/index.d.ts.map +1 -1
  69. package/dist/modules/workspace/index.js +6 -0
  70. package/dist/modules/workspace/index.js.map +1 -1
  71. package/dist/modules/workspace/kb-repository.contract.d.ts +124 -0
  72. package/dist/modules/workspace/kb-repository.contract.d.ts.map +1 -0
  73. package/dist/modules/workspace/kb-repository.contract.js +19 -0
  74. package/dist/modules/workspace/kb-repository.contract.js.map +1 -0
  75. package/dist/modules/workspace/kb-repository.d.ts +236 -0
  76. package/dist/modules/workspace/kb-repository.d.ts.map +1 -0
  77. package/dist/modules/workspace/kb-repository.js +811 -0
  78. package/dist/modules/workspace/kb-repository.js.map +1 -0
  79. package/dist/modules/workspace/pattern-matcher.contract.d.ts +50 -0
  80. package/dist/modules/workspace/pattern-matcher.contract.d.ts.map +1 -0
  81. package/dist/modules/workspace/pattern-matcher.contract.js +27 -0
  82. package/dist/modules/workspace/pattern-matcher.contract.js.map +1 -0
  83. package/dist/modules/workspace/routine-write-policy.d.ts +0 -7
  84. package/dist/modules/workspace/routine-write-policy.d.ts.map +1 -1
  85. package/dist/modules/workspace/routine-write-policy.js +0 -5
  86. package/dist/modules/workspace/routine-write-policy.js.map +1 -1
  87. package/dist/modules/workspace/set-aside-clone.d.ts +40 -0
  88. package/dist/modules/workspace/set-aside-clone.d.ts.map +1 -1
  89. package/dist/modules/workspace/set-aside-clone.js +139 -0
  90. package/dist/modules/workspace/set-aside-clone.js.map +1 -1
  91. package/dist/modules/workspace/startup/kb-startup-runner.d.ts +99 -0
  92. package/dist/modules/workspace/startup/kb-startup-runner.d.ts.map +1 -1
  93. package/dist/modules/workspace/startup/kb-startup-runner.js +407 -29
  94. package/dist/modules/workspace/startup/kb-startup-runner.js.map +1 -1
  95. package/dist/modules/workspace/workspace.service.d.ts +68 -32
  96. package/dist/modules/workspace/workspace.service.d.ts.map +1 -1
  97. package/dist/modules/workspace/workspace.service.js +240 -161
  98. package/dist/modules/workspace/workspace.service.js.map +1 -1
  99. package/dist/modules/workspace/workspace.tools.d.ts +13 -4
  100. package/dist/modules/workspace/workspace.tools.d.ts.map +1 -1
  101. package/dist/modules/workspace/workspace.tools.js +162 -336
  102. package/dist/modules/workspace/workspace.tools.js.map +1 -1
  103. package/dist/test-setup.js +4 -0
  104. package/dist/test-setup.js.map +1 -1
  105. package/package.json +3 -3
  106. package/src/__tests__/test-repositories.ts +22 -0
  107. package/src/__tests__/workspace-service.ts +69 -0
  108. package/src/core/__tests__/lifecycle.test.ts +16 -0
  109. package/src/core/create-core-server.ts +1 -1
  110. package/src/core/create-core-services.ts +36 -3
  111. package/src/core/lifecycle.ts +10 -0
  112. package/src/modules/access/__tests__/access-control.atref-cache.test.ts +144 -4
  113. package/src/modules/access/__tests__/access-control.group-read.test.ts +73 -0
  114. package/src/modules/access/__tests__/access.routes.group-grant.test.ts +66 -0
  115. package/src/modules/access/__tests__/access.routes.unknown-email.test.ts +2 -0
  116. package/src/modules/access/access-control.interface.ts +22 -0
  117. package/src/modules/access/access-control.service.ts +142 -14
  118. package/src/modules/access/access.routes.ts +22 -6
  119. package/src/modules/agent-instructions/shared-file-rules.ts +1 -1
  120. package/src/modules/diff/__tests__/diff.service.seed-atomicity.test.ts +2 -1
  121. package/src/modules/diff/__tests__/diff.service.test.ts +2 -1
  122. package/src/modules/diff/__tests__/diff.service.worktree.test.ts +76 -0
  123. package/src/modules/diff/diff.service.ts +6 -4
  124. package/src/modules/kb-fs/__tests__/git-dir.test.ts +77 -0
  125. package/src/modules/kb-fs/__tests__/ref-files.test.ts +338 -0
  126. package/src/modules/kb-fs/git-dir.ts +53 -0
  127. package/src/modules/kb-fs/index.ts +2 -0
  128. package/src/modules/kb-fs/ref-files.ts +279 -0
  129. package/src/modules/plugins/__tests__/teams.routes.test.ts +55 -0
  130. package/src/modules/plugins/teams.routes.ts +14 -1
  131. package/src/modules/settings/__tests__/setup.routes.layout-phase.test.ts +2 -2
  132. package/src/modules/settings/__tests__/setup.routes.managed-phase.test.ts +2 -2
  133. package/src/modules/tool-auth/internal-token.service.ts +1 -1
  134. package/src/modules/tool-auth/tool-auth.middleware.ts +1 -2
  135. package/src/modules/tool-helpers/__tests__/branch-declaration.test.ts +2 -2
  136. package/src/modules/tool-helpers/tool-handler.ts +2 -2
  137. package/src/modules/tool-helpers/tool.contract.ts +2 -2
  138. package/src/modules/tool-registry/__tests__/tool-description-length.test.ts +6 -1
  139. package/src/modules/workflow/git/__tests__/git.service.changedPathsMemo.test.ts +175 -0
  140. package/src/modules/workflow/git/__tests__/git.service.worktrees.test.ts +284 -0
  141. package/src/modules/workflow/git/git.service.ts +184 -22
  142. package/src/modules/workflow/workflow-hooks.ts +1 -1
  143. package/src/modules/workspace/__tests__/agent-downloads.test.ts +2 -2
  144. package/src/modules/workspace/__tests__/bounded-matcher.test.ts +213 -0
  145. package/src/modules/workspace/__tests__/git-internals.security.test.ts +22 -7
  146. package/src/modules/workspace/__tests__/kb-repository.test.ts +481 -0
  147. package/src/modules/workspace/__tests__/rest-tool-arguments.test.ts +14 -0
  148. package/src/modules/workspace/__tests__/routine-write-policy.test.ts +0 -11
  149. package/src/modules/workspace/__tests__/workspace-paths-inside-the-repo.test.ts +2 -2
  150. package/src/modules/workspace/__tests__/workspace.routes.folder-zip-access.test.ts +2 -2
  151. package/src/modules/workspace/__tests__/workspace.routes.folders.test.ts +4 -3
  152. package/src/modules/workspace/__tests__/workspace.service.any-workspace-credentials.test.ts +2 -2
  153. package/src/modules/workspace/__tests__/workspace.service.ensure-remotes-fetched.test.ts +2 -2
  154. package/src/modules/workspace/__tests__/workspace.service.forget-clone-races.test.ts +12 -5
  155. package/src/modules/workspace/__tests__/workspace.service.list-cloned.test.ts +6 -6
  156. package/src/modules/workspace/__tests__/workspace.service.move-never-overwrites.test.ts +2 -1
  157. package/src/modules/workspace/__tests__/workspace.service.read-filter.test.ts +2 -1
  158. package/src/modules/workspace/__tests__/workspace.service.replaced-repository.test.ts +2 -1
  159. package/src/modules/workspace/__tests__/workspace.service.test.ts +24 -23
  160. package/src/modules/workspace/__tests__/workspace.service.unknown-branch.test.ts +2 -1
  161. package/src/modules/workspace/__tests__/workspace.service.worktrees.test.ts +620 -0
  162. package/src/modules/workspace/__tests__/workspace.tools.agents-file.test.ts +15 -1
  163. package/src/modules/workspace/__tests__/workspace.tools.branch-errors.test.ts +71 -5
  164. package/src/modules/workspace/__tests__/workspace.tools.test.ts +153 -298
  165. package/src/modules/workspace/agent-access.gate.ts +2 -2
  166. package/src/modules/workspace/bounded-matcher.ts +328 -0
  167. package/src/modules/workspace/fetch-instant.ts +13 -0
  168. package/src/modules/workspace/index.ts +12 -0
  169. package/src/modules/workspace/kb-repository.contract.ts +124 -0
  170. package/src/modules/workspace/kb-repository.ts +844 -0
  171. package/src/modules/workspace/pattern-matcher.contract.ts +69 -0
  172. package/src/modules/workspace/routine-write-policy.ts +0 -15
  173. package/src/modules/workspace/set-aside-clone.ts +140 -0
  174. package/src/modules/workspace/startup/__tests__/kb-startup-runner.degraded.test.ts +3 -2
  175. package/src/modules/workspace/startup/__tests__/kb-startup-runner.test.ts +12 -10
  176. package/src/modules/workspace/startup/__tests__/kb-startup-runner.worktrees.test.ts +613 -0
  177. package/src/modules/workspace/startup/kb-startup-runner.ts +422 -29
  178. package/src/modules/workspace/startup/steps/__tests__/steps.test.ts +4 -3
  179. package/src/modules/workspace/workspace.service.ts +247 -169
  180. package/src/modules/workspace/workspace.tools.ts +179 -365
  181. package/src/test-setup.ts +5 -0
@@ -0,0 +1,279 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { isAbsence } from '../../shared/fs.contract.js';
4
+ import { gitDirOf } from './git-dir.js';
5
+
6
+ /**
7
+ * Where a ref points, read from the repository's own files instead of a
8
+ * `git rev-parse` process.
9
+ *
10
+ * A branch tip is a small file git maintains: `refs/remotes/origin/<branch>`
11
+ * as a loose file, or one line of `packed-refs`. Reading it costs a file read
12
+ * and no fork, and it is exactly as current as the last fetch — the same
13
+ * freshness a `rev-parse` has. A change-request listing resolves two tips per
14
+ * open request and the access gates resolve every ref they decide at; with a
15
+ * hundred open requests that was hundreds of forks per listing, each costing
16
+ * tens of milliseconds of the main thread on a server holding a large heap.
17
+ *
18
+ * What is read, in git's own order: a loose file under the common directory
19
+ * (a worktree shares `refs/` with the repository it belongs to), then
20
+ * `packed-refs`. `HEAD` and the per-worktree namespaces (`refs/worktree/`,
21
+ * `refs/bisect/`, `refs/rewritten/`) are a worktree's own: read loose from
22
+ * its git directory, never from the common directory, and never packed. A
23
+ * symbolic ref (`ref: refs/heads/main`) is followed. A tag is never decided
24
+ * here: its file may hold a tag object, and what that peels to is git's to
25
+ * say.
26
+ *
27
+ * The answer is one of three things, and the caller treats them differently:
28
+ * `{ sha }` is what git would answer too; `'missing'` means every file git
29
+ * would consult says there is no such ref, which is as authoritative as
30
+ * `rev-parse` answering nothing; `'unreadable'` means this reader could not
31
+ * decide — a repository it does not understand (one whose refs live in a
32
+ * reftable, say), a tag, a corrupt file, a name it refuses — and the caller
33
+ * asks git, which is authoritative. Never a guess: a read error is
34
+ * `'unreadable'`, not `'missing'`, so a flaky disk sends the caller to git
35
+ * rather than making a branch disappear.
36
+ *
37
+ * What the files guarantee about the object: git itself refuses to write a
38
+ * non-commit under `refs/heads/`, and `refs/remotes/origin/` mirrors the
39
+ * remote's `refs/heads/`, so a branch tip read here is a commit as surely
40
+ * as `rev-parse <branch>^{commit}` would make it one. A caller that holds
41
+ * an object id rather than a name, or a `<rev>^{...}` form, asks git.
42
+ */
43
+ export type RefFileAnswer = { sha: string } | 'missing' | 'unreadable';
44
+
45
+ const SHA = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
46
+ const MAX_SYMREF_DEPTH = 5;
47
+ const PER_WORKTREE = /^refs\/(?:worktree|bisect|rewritten)\//;
48
+
49
+ /** A ref name this reader is willing to turn into a path under `.git`. */
50
+ function isSafeRefName(ref: string): boolean {
51
+ if (ref === 'HEAD') return true;
52
+ if (!ref.startsWith('refs/')) return false;
53
+ if (!/^[A-Za-z0-9._\-/@+]+$/.test(ref)) return false;
54
+ return !ref.split('/').some((segment) => segment === '' || segment === '.' || segment === '..');
55
+ }
56
+
57
+ /**
58
+ * One resolution's view of the repository: the two directories, and the
59
+ * `packed-refs` table read at most once however many candidates a name
60
+ * tries — the table can grow with the history, and a name that is missing
61
+ * as itself consults it for every prefix.
62
+ */
63
+ interface GitDirs {
64
+ /** The worktree's own git directory: `HEAD` lives here. */
65
+ gitDir: string;
66
+ /** Where `refs/` and `packed-refs` live: the repository, shared by every worktree. */
67
+ commonDir: string;
68
+ /** The `packed-refs` table, read on first need; null when it could not be read. */
69
+ packed?: Promise<Map<string, string> | null>;
70
+ }
71
+
72
+ async function gitDirsOf(repoDir: string): Promise<GitDirs | null> {
73
+ let gitDir: string | null;
74
+ try {
75
+ gitDir = await gitDirOf(repoDir);
76
+ } catch {
77
+ return null;
78
+ }
79
+ if (gitDir === null) return null;
80
+ let commonDir = gitDir;
81
+ try {
82
+ const pointer = (await fs.readFile(path.join(gitDir, 'commondir'), 'utf-8')).trim();
83
+ if (pointer) commonDir = path.resolve(gitDir, pointer);
84
+ } catch (err) {
85
+ if (!isAbsence(err)) return null;
86
+ }
87
+ // A repository whose refs live in a reftable (`extensions.refStorage`)
88
+ // keeps nothing this reader understands under `refs/` or in `packed-refs`:
89
+ // every name would read as missing. Not a repository for this reader.
90
+ try {
91
+ await fs.stat(path.join(commonDir, 'reftable'));
92
+ return null;
93
+ } catch (err) {
94
+ if (!isAbsence(err)) return null;
95
+ }
96
+ try {
97
+ if (/^\s*refstorage\s*=\s*reftable\s*$/im.test(await fs.readFile(path.join(commonDir, 'config'), 'utf-8'))) {
98
+ return null;
99
+ }
100
+ } catch (err) {
101
+ if (!isAbsence(err)) return null;
102
+ }
103
+ return { gitDir, commonDir };
104
+ }
105
+
106
+ /**
107
+ * `packed-refs`: `<sha> <name>` lines, `^<sha>` peel lines, `#` comments.
108
+ * Absent file → an empty table. A line that is none of those is a file this
109
+ * reader does not understand, and the whole table is null (unreadable): a
110
+ * record it skipped could be the one that was asked for.
111
+ */
112
+ async function readPackedRefs(commonDir: string): Promise<Map<string, string> | null> {
113
+ let text: string;
114
+ try {
115
+ text = await fs.readFile(path.join(commonDir, 'packed-refs'), 'utf-8');
116
+ } catch (err) {
117
+ return isAbsence(err) ? new Map() : null;
118
+ }
119
+ const table = new Map<string, string>();
120
+ for (const raw of text.split('\n')) {
121
+ const line = raw.trim();
122
+ if (!line || line.startsWith('#')) continue;
123
+ if (line.startsWith('^')) {
124
+ if (!SHA.test(line.slice(1))) return null;
125
+ continue;
126
+ }
127
+ const space = line.indexOf(' ');
128
+ const sha = space < 0 ? '' : line.slice(0, space);
129
+ const name = space < 0 ? '' : line.slice(space + 1).trim();
130
+ if (!SHA.test(sha) || !name) return null;
131
+ table.set(name, sha);
132
+ }
133
+ return table;
134
+ }
135
+
136
+ /**
137
+ * The file at `<dir>/<relative>`, read as git reads a loose ref: a symbolic
138
+ * ref is followed, an object id is the answer, anything else is not decided.
139
+ */
140
+ async function readLooseRef(dirs: GitDirs, dir: string, relative: string, depth: number): Promise<RefFileAnswer> {
141
+ let text: string;
142
+ try {
143
+ text = (await fs.readFile(path.join(dir, ...relative.split('/')), 'utf-8')).trim();
144
+ } catch (err) {
145
+ return isAbsence(err) ? 'missing' : 'unreadable';
146
+ }
147
+ if (text.startsWith('ref:')) {
148
+ const answer = await resolveIn(dirs, text.slice('ref:'.length).trim(), depth + 1);
149
+ // A symbolic ref whose target is gone is dangling, and git reports it
150
+ // as such; a packed entry under the symbolic ref's own name does not
151
+ // stand in for it. Git's to report, then.
152
+ return answer === 'missing' ? 'unreadable' : answer;
153
+ }
154
+ return SHA.test(text) ? { sha: text } : 'unreadable';
155
+ }
156
+
157
+ async function resolveIn(dirs: GitDirs, ref: string, depth: number): Promise<RefFileAnswer> {
158
+ if (depth > MAX_SYMREF_DEPTH) return 'unreadable';
159
+ if (!isSafeRefName(ref)) return 'unreadable';
160
+ // `HEAD` and the per-worktree namespaces (`refs/worktree/`, `refs/bisect/`,
161
+ // `refs/rewritten/`) are a worktree's own, kept loose in its git directory
162
+ // and never packed; everything else is the repository's.
163
+ const own = ref === 'HEAD' || PER_WORKTREE.test(ref);
164
+ const loose = await readLooseRef(dirs, own ? dirs.gitDir : dirs.commonDir, ref, depth);
165
+ if (loose !== 'missing') return decidedHere(ref, loose);
166
+ if (own) return 'missing';
167
+ const packed = await (dirs.packed ??= readPackedRefs(dirs.commonDir));
168
+ if (!packed) return 'unreadable';
169
+ const sha = packed.get(ref);
170
+ return sha ? decidedHere(ref, { sha }) : 'missing';
171
+ }
172
+
173
+ /**
174
+ * The files decide only where git itself guarantees the object is a commit:
175
+ * `HEAD` and `refs/heads/*`, which git refuses to point at anything else,
176
+ * and `refs/remotes/*`, fetched from a remote's `refs/heads/*` under the
177
+ * same refusal. Anything else that exists — a tag, a ref directly under
178
+ * `refs/` — may name a tree or a blob, which only git's `^{commit}` refuses,
179
+ * so it is git's to resolve. Anything that does not exist is as missing as
180
+ * any other ref, so a short name can move on to the branch git would find
181
+ * next.
182
+ */
183
+ function decidedHere(ref: string, answer: RefFileAnswer): RefFileAnswer {
184
+ if (answer !== 'missing' && answer !== 'unreadable' && !commitGuaranteed(ref)) return 'unreadable';
185
+ return answer;
186
+ }
187
+
188
+ function commitGuaranteed(ref: string): boolean {
189
+ return ref === 'HEAD' || ref.startsWith('refs/heads/') || ref.startsWith('refs/remotes/');
190
+ }
191
+
192
+ /**
193
+ * The commit a full ref name (`refs/...`) or `HEAD` points at in the
194
+ * repository behind `repoDir` (a clone, or a worktree of one). See the module
195
+ * doc for the three answers and what each means to the caller.
196
+ *
197
+ * "Missing" is what `rev-parse` answers nothing for, and `rev-parse` tries
198
+ * the same prefixes for a full name as for a short one (`refs/heads/x` is
199
+ * also looked for as `refs/heads/refs/heads/x`), so a name that is missing
200
+ * as itself but present under a prefix is git's to resolve.
201
+ */
202
+ export async function readRefSha(repoDir: string, ref: string): Promise<RefFileAnswer> {
203
+ const dirs = await gitDirsOf(repoDir);
204
+ if (!dirs) return 'unreadable';
205
+ const exact = await resolveIn(dirs, ref, 0);
206
+ if (exact !== 'missing') return exact;
207
+ for (const name of prefixedCandidates(ref)) {
208
+ if ((await resolveIn(dirs, name, 0)) !== 'missing') return 'unreadable';
209
+ }
210
+ return 'missing';
211
+ }
212
+
213
+ /**
214
+ * The names `git rev-parse` tries for `<name>` after `$GIT_DIR/<name>`
215
+ * itself, in git's order (gitrevisions(7)): `refs/<name>`,
216
+ * `refs/tags/<name>`, `refs/heads/<name>`, `refs/remotes/<name>`,
217
+ * `refs/remotes/<name>/HEAD`. The same for every spelling, a full `refs/...`
218
+ * name included.
219
+ */
220
+ function prefixedCandidates(name: string): string[] {
221
+ return [`refs/${name}`, `refs/tags/${name}`, `refs/heads/${name}`, `refs/remotes/${name}`, `refs/remotes/${name}/HEAD`];
222
+ }
223
+
224
+ /**
225
+ * Every name `git rev-parse` tries for a spelling, in git's order: the
226
+ * spelling itself at `$GIT_DIR/<name>` (which for `HEAD` and a full
227
+ * `refs/...` name is the ref file), then the prefixed forms. Null for a
228
+ * spelling this reader does not resolve — an object id or its abbreviation,
229
+ * a `<rev>^{...}` or `@{...}` form, an empty or option-looking name — which
230
+ * the caller sends to git.
231
+ */
232
+ function refNameCandidates(ref: string): string[] | null {
233
+ if (!ref || ref.startsWith('-')) return null;
234
+ if (/[\^~:@{}\\\s]/.test(ref)) return null;
235
+ if (/^[0-9a-f]{4,64}$/i.test(ref)) return null;
236
+ // `main-worktree/<ref>` and `worktrees/<name>/<ref>` reach another
237
+ // worktree's own refs (gitrevisions(7)): git's to resolve.
238
+ if (/^(?:main-worktree|worktrees)\//.test(ref)) return null;
239
+ return [ref, ...prefixedCandidates(ref)];
240
+ }
241
+
242
+ /**
243
+ * {@link readRefSha} for a name as a caller holds it (`main`, `origin/main`,
244
+ * `HEAD`, `refs/heads/main`): the first of git's candidates that resolves,
245
+ * starting where git starts, at `$GIT_DIR/<name>` itself. Whatever is found
246
+ * there (`MERGE_HEAD`, `FETCH_HEAD`, `AUTO_MERGE`, which may name a tree) is
247
+ * git's to resolve; only its absence lets the `refs/` candidates be read.
248
+ * `'missing'` only when every candidate is missing; `'unreadable'` as soon
249
+ * as one cannot be decided, because the undecided one might be the one git
250
+ * would pick.
251
+ */
252
+ export async function readRevSha(repoDir: string, rev: string): Promise<RefFileAnswer> {
253
+ const candidates = refNameCandidates(rev);
254
+ if (!candidates) return 'unreadable';
255
+ const dirs = await gitDirsOf(repoDir);
256
+ if (!dirs) return 'unreadable';
257
+ for (const name of candidates) {
258
+ const answer = name === rev ? await resolveAsItself(dirs, rev) : await resolveIn(dirs, name, 0);
259
+ if (answer !== 'missing') return answer;
260
+ }
261
+ return 'missing';
262
+ }
263
+
264
+ /**
265
+ * Git's first step for any spelling: `$GIT_DIR/<name>`. For `HEAD` and a
266
+ * full `refs/...` name that is the ref itself, read as such. For anything
267
+ * else it is a pseudoref (`MERGE_HEAD`, `AUTO_MERGE`, which may name a
268
+ * tree), and whatever exists there is git's to resolve.
269
+ */
270
+ async function resolveAsItself(dirs: GitDirs, rev: string): Promise<RefFileAnswer> {
271
+ if (rev === 'HEAD' || rev.startsWith('refs/')) return resolveIn(dirs, rev, 0);
272
+ if (!isSafeRefName(`refs/${rev}`)) return 'unreadable';
273
+ try {
274
+ await fs.stat(path.join(dirs.gitDir, ...rev.split('/')));
275
+ return 'unreadable';
276
+ } catch (err) {
277
+ return isAbsence(err) ? 'missing' : 'unreadable';
278
+ }
279
+ }
@@ -4,6 +4,7 @@ import express from 'express';
4
4
  import { describe, it, expect, afterEach, vi } from 'vitest';
5
5
 
6
6
  import type { IAccessControl } from '../../access/access-control.interface.js';
7
+ import { AccessControlService } from '../../access/access-control.service.js';
7
8
  import type { ISkillService, SkillSummary } from '../../skills/skills.contract.js';
8
9
  import type { IToolManualService, ToolManualSummary } from '../../tool-manuals/tool-manuals.contract.js';
9
10
  import type { IPluginIndexService, PluginCatalogEntry } from '../plugins.contract.js';
@@ -62,6 +63,10 @@ interface HarnessOpts {
62
63
  /** What the org-wide `everyone` principal reads. */
63
64
  everyone?: string[];
64
65
  email?: string | null;
66
+ /** Whether the caller is an admin, who is shown every group. Defaults to true. */
67
+ admin?: boolean;
68
+ /** The groups the caller is a member of, which is all a non-admin is shown. */
69
+ memberOf?: string[];
65
70
  }
66
71
 
67
72
  /** The org-wide entry every answer opens with — empty unless `everyone` says otherwise. */
@@ -82,6 +87,12 @@ async function harness(opts: HarnessOpts = {}) {
82
87
  opts.team && group in opts.team ? tableVerdict(opts.team[group], paths) : null,
83
88
  ),
84
89
  canReadAsEveryoneBatch: vi.fn(async (_w: string, paths: string[]) => tableVerdict(opts.everyone, paths)),
90
+ holdsAdminRootWrite: vi.fn(async () => opts.admin ?? true),
91
+ groupsOfUser: vi.fn(async () => opts.memberOf ?? []),
92
+ // The real policy over the two stubs above: who sees which group is the
93
+ // resolver's one answer, not something a route (or this double) decides.
94
+ visibleGroups: (wsId: string, email: string, groups: readonly string[]) =>
95
+ AccessControlService.prototype.visibleGroups.call(accessControl, wsId, email, groups),
85
96
  } as unknown as IAccessControl;
86
97
  const index = { catalog: async () => CATALOG, invalidate: () => undefined } as IPluginIndexService;
87
98
  const skills = { listSkills: async () => SKILLS } as unknown as ISkillService;
@@ -242,6 +253,50 @@ describe('GET /api/teams', () => {
242
253
  );
243
254
  });
244
255
 
256
+ it("shows someone who is not an admin only the groups they are in, never another group's name", async () => {
257
+ const h = await harness({
258
+ groups: ['Sales Team', 'Finance Team'],
259
+ caller: EVERYTHING,
260
+ team: {
261
+ 'Sales Team': ['Plugins/GTM', 'Skills/Sales/outreach/SKILL.md', 'Plugins/GTM/mcp.json'],
262
+ 'Finance Team': ['Plugins/Finance', 'Skills/Finance/ledger/SKILL.md', 'Plugins/Finance/books.tool'],
263
+ },
264
+ admin: false,
265
+ memberOf: ['Sales Team'],
266
+ });
267
+ const res = await h.get();
268
+ expect(res.status).toBe(200);
269
+ expect(await res.json()).toEqual({
270
+ teams: [
271
+ NOTHING_ORG_WIDE,
272
+ { name: 'Sales Team', plugins: ['gtm'], skills: ['outreach'], tools: ['hubspot'] },
273
+ ],
274
+ });
275
+ // A group that is not listed is not asked about either.
276
+ expect(h.accessControl.canReadAsGroupBatch).not.toHaveBeenCalledWith(
277
+ expect.anything(),
278
+ 'Finance Team',
279
+ expect.anything(),
280
+ );
281
+ });
282
+
283
+ it('shows someone in no group, who is not an admin, Everyone alone', async () => {
284
+ const h = await harness({ groups: ['Sales Team'], caller: EVERYTHING, team: { 'Sales Team': [] }, admin: false });
285
+ expect(await (await h.get()).json()).toEqual({ teams: [NOTHING_ORG_WIDE] });
286
+ });
287
+
288
+ it('shows an admin every group, including ones they are not in', async () => {
289
+ const h = await harness({
290
+ groups: ['Sales Team', 'Finance Team'],
291
+ caller: EVERYTHING,
292
+ team: { 'Sales Team': [], 'Finance Team': [] },
293
+ admin: true,
294
+ memberOf: [],
295
+ });
296
+ const body = (await (await h.get()).json()) as { teams: { name: string }[] };
297
+ expect(body.teams.map((t) => t.name)).toEqual(['Everyone', 'Sales Team', 'Finance Team']);
298
+ });
299
+
245
300
  it('is 401 without a caller', async () => {
246
301
  const h = await harness({ email: null });
247
302
  expect((await h.get()).status).toBe(401);
@@ -37,6 +37,10 @@ import type { IPluginIndexService, PluginCatalogEntry } from './plugins.contract
37
37
  * what `read: everyone` (or a public plugin) reaches. Same slice, same
38
38
  * caller gate, asked of the resolver as `canReadAsEveryoneBatch`. The name
39
39
  * cannot collide with a group's: `everyone` is reserved by the grammar.
40
+ *
41
+ * The groups listed after it are every group for an admin, and only the
42
+ * caller's own groups for anyone else (`groupsOfUser`), so the lens never
43
+ * names a group the caller has no part in.
40
44
  */
41
45
  export interface TeamAccess {
42
46
  name: string;
@@ -110,10 +114,19 @@ export function createTeamsRoutes(
110
114
  };
111
115
  };
112
116
 
117
+ // Which groups to list: the resolver's one answer to who sees which
118
+ // group (every group for an admin, only their own for anyone else), the
119
+ // same the share dialog offers and a grant may name. A group's NAME is
120
+ // information too: a deployment that gives each client, author or
121
+ // customer a group of their own would otherwise show every member the
122
+ // whole roster, even though the slices of groups they are not in come
123
+ // back empty.
124
+ const listed = await accessControl.visibleGroups(wsId, email, groups);
125
+
113
126
  const teams: TeamAccess[] = [
114
127
  slice(EVERYONE_TEAM, await accessControl.canReadAsEveryoneBatch(wsId, probes)),
115
128
  ];
116
- for (const name of groups) {
129
+ for (const name of listed) {
117
130
  const team = await accessControl.canReadAsGroupBatch(wsId, name, probes);
118
131
  if (team === null) continue; // gone between the two reads — nothing to say
119
132
  teams.push(slice(name, team));
@@ -10,13 +10,13 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
10
10
  import { testKbContext } from '../../../__tests__/kb-context.js';
11
11
  import { createSetupRoutes } from '../setup.routes.js';
12
12
  import { DeploymentSettingsService } from '../deployment-settings.service.js';
13
- import { KbStartupRunner } from '../../workspace/startup/kb-startup-runner.js';
14
13
  import { NodeGitRunner } from '../../workflow/git/node-git-runner.js';
15
14
  import { TemplateFilesStep } from '../../workspace/startup/steps/template-files.step.js';
16
15
  import { NodeFs } from '../../kb-fs/node-fs.js';
17
16
  import { defaultKbTemplateDir } from '../../../assets.js';
18
17
  import type { Database } from '../../database/connection.js';
19
18
  import type { IAdminAccessService } from '../../admin/admin.interface.js';
19
+ import { newKbStartupRunner } from '../../../__tests__/workspace-service.js';
20
20
 
21
21
  /**
22
22
  * The completing save and the REAL initialization phase, wired the way the
@@ -105,7 +105,7 @@ function boot() {
105
105
  // it: the completing save applies the admin's names to it, and the step
106
106
  // reads them from it when the phase runs.
107
107
  const kb = testKbContext();
108
- const runner = new KbStartupRunner({
108
+ const runner = newKbStartupRunner({
109
109
  gitRunner: new NodeGitRunner(),
110
110
  kbRepoUrl: () => upstream,
111
111
  workspacesRoot: path.join(root, 'workspaces'),
@@ -12,7 +12,6 @@ import { createSetupRoutes } from '../setup.routes.js';
12
12
  import { DeploymentSettingsService } from '../deployment-settings.service.js';
13
13
  import { ManagedRepository } from '../managed-repository.js';
14
14
  import { RepositorySource } from '../repository-source.js';
15
- import { KbStartupRunner } from '../../workspace/startup/kb-startup-runner.js';
16
15
  import { NodeGitRunner } from '../../workflow/git/node-git-runner.js';
17
16
  import { TemplateFilesStep } from '../../workspace/startup/steps/template-files.step.js';
18
17
  import { NodeFs } from '../../kb-fs/node-fs.js';
@@ -20,6 +19,7 @@ import { defaultKbTemplateDir } from '../../../assets.js';
20
19
  import type { Database } from '../../database/connection.js';
21
20
  import type { IAdminAccessService } from '../../admin/admin.interface.js';
22
21
  import type { KbContext } from '../../../shared/kb-context.js';
22
+ import { newKbStartupRunner } from '../../../__tests__/workspace-service.js';
23
23
 
24
24
  /**
25
25
  * A repository the deployment keeps, and the REAL startup phase, wired the
@@ -89,7 +89,7 @@ function boot(kb: KbContext) {
89
89
  const gitRunner = new NodeGitRunner(60_000, source.credentials);
90
90
  const workspacesRoot = path.join(root, 'workspaces');
91
91
  const setAsideRoot = path.join(root, 'backups', 'replaced-working-copies');
92
- const runner = new KbStartupRunner({
92
+ const runner = newKbStartupRunner({
93
93
  gitRunner,
94
94
  kbRepoUrl: () => source.url(),
95
95
  workspacesRoot,
@@ -33,7 +33,7 @@ export interface InternalTokenClaim {
33
33
  * connection key to pass through). The verifier resolves such tokens to
34
34
  * `source: 'external'`: the caller IS an external agent, and must be treated
35
35
  * like one everywhere — admitted to external-only surfaces (`start_session`,
36
- * `ask`), refused from internal-only ones (`execute_command`, …), and
36
+ * `ask`), refused from internal-only ones (`switch_branch`, …), and
37
37
  * expected to carry `sessionId` on the tool body, not in the token.
38
38
  */
39
39
  externalProxy?: boolean;
@@ -66,8 +66,7 @@ const WWW_AUTH = 'Bearer realm="bevel-tools"';
66
66
  * Gate that admits ONLY in-process callers (those bearing an internal HMAC
67
67
  * token). Mount it AFTER `toolAuth` on every internal-only surface — the
68
68
  * internal tool catalog and the routes of tools registered with
69
- * `registerInternalTool` but NOT `registerExternalTool` (e.g. `execute_command`,
70
- * `switch_branch`, the `sharepoint_*` reads). `toolAuth` alone is not enough:
69
+ * `registerInternalTool` but NOT `registerExternalTool` (e.g. `switch_branch`, the `sharepoint_*` reads). `toolAuth` alone is not enough:
71
70
  * an external connection key authenticates successfully, so without this gate it
72
71
  * could invoke an internal-only route directly by guessing its name. The route
73
72
  * MUST stay mounted (our own agent reaches it over the same loopback HTTP), so
@@ -8,7 +8,6 @@ import express from 'express';
8
8
  import { afterEach, describe, expect, it, vi } from 'vitest';
9
9
  import { testKbContext, TEST_BRANCH_MODEL } from '../../../__tests__/kb-context.js';
10
10
  import { NodeFs } from '../../kb-fs/node-fs.js';
11
- import { WorkspaceService } from '../../workspace/workspace.service.js';
12
11
  import type { AuthService } from '../../auth/auth.service.js';
13
12
  import type { ICreatorAccess } from '../../access-model/creator.js';
14
13
  import type { WorkflowEventBus } from '../../workflow/event-bus.js';
@@ -24,6 +23,7 @@ import {
24
23
  type BranchDeclaration,
25
24
  type ToolDefSpec,
26
25
  } from '../index.js';
26
+ import { newWorkspaceService } from '../../../__tests__/workspace-service.js';
27
27
 
28
28
  const execFileAsync = promisify(execFile);
29
29
  const KB_DIR = 'knowledge-base';
@@ -116,7 +116,7 @@ describe('a tool declares how it treats its branch', () => {
116
116
  await runGit(seed, ['push', 'origin', 'HEAD:refs/heads/refs/heads/ghost']);
117
117
 
118
118
  kb = testKbContext({ kbDirName: KB_DIR });
119
- const workspaces = new WorkspaceService(workspacesRoot, upstream, kb, new NodeFs());
119
+ const workspaces = newWorkspaceService(workspacesRoot, upstream, kb, new NodeFs());
120
120
  const resolve = createToolContextResolver({
121
121
  authService: {
122
122
  getUserById: async () => ({ id: 'u', email: 'alice@example.com', name: 'Alice' }),
@@ -36,8 +36,8 @@ export interface ToolHandlerOptions {
36
36
  writeScope?: boolean;
37
37
  /**
38
38
  * How this route treats `branch`, when it must differ from what its
39
- * `toolDef` recorded. Only `execute_command` sets it (`own`): it falls back
40
- * to an internal caller's focused branch rather than being refused.
39
+ * `toolDef` recorded (`own`: the handler resolves it itself rather than
40
+ * being refused).
41
41
  */
42
42
  branch?: BranchHandling;
43
43
  }
@@ -41,8 +41,8 @@ export interface ToolContext {
41
41
  sessionId?: string;
42
42
  /**
43
43
  * The branch the in-process agent's workspace is focused on, from its internal
44
- * token. An internal-only workspace tool (e.g. `execute_command`) falls back to
45
- * it when the model omits `branch`, so the in-app chat agent acts on its own
44
+ * token. A tool may fall back to it when the model omits `branch` (`list_tool_setup`
45
+ * reads the focused branch's setup), so the in-app chat agent acts on its own
46
46
  * workspace even on a branch-less call. Only set for `source: 'internal'`;
47
47
  * external callers must name the branch explicitly (a present-but-invalid value
48
48
  * still fails closed regardless of this).
@@ -15,6 +15,7 @@ import type { UtcpTool } from '../tool.contract.js';
15
15
  import { createToolHandlerFactory } from '../../tool-helpers/tool-handler.js';
16
16
  import type { ToolAuth } from '../../tool-auth/tool-auth.middleware.js';
17
17
  import { registerWorkspaceTools } from '../../workspace/workspace.tools.js';
18
+ import { WorkerPatternMatcher } from '../../workspace/bounded-matcher.js';
18
19
  import { registerWorkflowTools } from '../../workflow/agent-tools/workflow.tools.js';
19
20
  import { registerPluginsTools } from '../../plugins/plugins.tools.js';
20
21
  import { registerSkillsTools } from '../../skills/skills.tools.js';
@@ -110,6 +111,11 @@ async function hexisTools(skills: Parameters<typeof registerSkillsTools>[4] = em
110
111
  // mounted on the same terms as the upload one.
111
112
  undefined,
112
113
  unused(),
114
+ // Starter packs and the tree walk, not needed; then the grep matcher,
115
+ // mounted on the same terms (it spawns no worker until a search runs).
116
+ undefined,
117
+ undefined,
118
+ new WorkerPatternMatcher(),
113
119
  );
114
120
  registerWorkflowTools(registry, router, toolAuth, toolHandler, kb, async () => true);
115
121
  registerPluginsTools(registry, kb);
@@ -257,7 +263,6 @@ describe('every file tool opens with the guide-first sentence and carries no sha
257
263
  'move_file',
258
264
  'copy_file',
259
265
  'unzip',
260
- 'execute_command',
261
266
  'apply_file_upload',
262
267
  ];
263
268