@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.
- package/dist/__tests__/test-repositories.d.ts +3 -0
- package/dist/__tests__/test-repositories.d.ts.map +1 -0
- package/dist/__tests__/test-repositories.js +18 -0
- package/dist/__tests__/test-repositories.js.map +1 -0
- package/dist/core/create-core-server.js +1 -1
- package/dist/core/create-core-server.js.map +1 -1
- package/dist/core/create-core-services.d.ts +3 -0
- package/dist/core/create-core-services.d.ts.map +1 -1
- package/dist/core/create-core-services.js +32 -4
- package/dist/core/create-core-services.js.map +1 -1
- package/dist/core/lifecycle.d.ts +8 -0
- package/dist/core/lifecycle.d.ts.map +1 -1
- package/dist/core/lifecycle.js +4 -0
- package/dist/core/lifecycle.js.map +1 -1
- package/dist/modules/access/access-control.interface.d.ts +20 -0
- package/dist/modules/access/access-control.interface.d.ts.map +1 -1
- package/dist/modules/access/access-control.service.d.ts +32 -1
- package/dist/modules/access/access-control.service.d.ts.map +1 -1
- package/dist/modules/access/access-control.service.js +138 -16
- package/dist/modules/access/access-control.service.js.map +1 -1
- package/dist/modules/access/access.routes.d.ts.map +1 -1
- package/dist/modules/access/access.routes.js +20 -6
- package/dist/modules/access/access.routes.js.map +1 -1
- package/dist/modules/agent-instructions/shared-file-rules.d.ts +1 -1
- package/dist/modules/agent-instructions/shared-file-rules.js +1 -1
- package/dist/modules/diff/diff.service.d.ts.map +1 -1
- package/dist/modules/diff/diff.service.js +7 -1
- package/dist/modules/diff/diff.service.js.map +1 -1
- package/dist/modules/kb-fs/git-dir.d.ts +21 -0
- package/dist/modules/kb-fs/git-dir.d.ts.map +1 -0
- package/dist/modules/kb-fs/git-dir.js +58 -0
- package/dist/modules/kb-fs/git-dir.js.map +1 -0
- package/dist/modules/kb-fs/index.d.ts +2 -0
- package/dist/modules/kb-fs/index.d.ts.map +1 -1
- package/dist/modules/kb-fs/index.js +2 -1
- package/dist/modules/kb-fs/index.js.map +1 -1
- package/dist/modules/kb-fs/ref-files.d.ts +63 -0
- package/dist/modules/kb-fs/ref-files.d.ts.map +1 -0
- package/dist/modules/kb-fs/ref-files.js +249 -0
- package/dist/modules/kb-fs/ref-files.js.map +1 -0
- package/dist/modules/plugins/teams.routes.d.ts +4 -0
- package/dist/modules/plugins/teams.routes.d.ts.map +1 -1
- package/dist/modules/plugins/teams.routes.js +9 -1
- package/dist/modules/plugins/teams.routes.js.map +1 -1
- package/dist/modules/tool-auth/internal-token.service.d.ts +1 -1
- package/dist/modules/tool-auth/tool-auth.middleware.d.ts +1 -2
- package/dist/modules/tool-auth/tool-auth.middleware.d.ts.map +1 -1
- package/dist/modules/tool-auth/tool-auth.middleware.js +1 -2
- package/dist/modules/tool-auth/tool-auth.middleware.js.map +1 -1
- package/dist/modules/tool-helpers/tool-handler.d.ts +2 -2
- package/dist/modules/tool-helpers/tool.contract.d.ts +2 -2
- package/dist/modules/workflow/git/git.service.d.ts +29 -0
- package/dist/modules/workflow/git/git.service.d.ts.map +1 -1
- package/dist/modules/workflow/git/git.service.js +177 -24
- package/dist/modules/workflow/git/git.service.js.map +1 -1
- package/dist/modules/workflow/workflow-hooks.d.ts +1 -1
- package/dist/modules/workspace/agent-access.gate.d.ts +2 -2
- package/dist/modules/workspace/agent-access.gate.js +2 -2
- package/dist/modules/workspace/bounded-matcher.d.ts +79 -0
- package/dist/modules/workspace/bounded-matcher.d.ts.map +1 -0
- package/dist/modules/workspace/bounded-matcher.js +261 -0
- package/dist/modules/workspace/bounded-matcher.js.map +1 -0
- package/dist/modules/workspace/fetch-instant.d.ts +9 -0
- package/dist/modules/workspace/fetch-instant.d.ts.map +1 -0
- package/dist/modules/workspace/fetch-instant.js +13 -0
- package/dist/modules/workspace/fetch-instant.js.map +1 -0
- package/dist/modules/workspace/index.d.ts +7 -0
- package/dist/modules/workspace/index.d.ts.map +1 -1
- package/dist/modules/workspace/index.js +6 -0
- package/dist/modules/workspace/index.js.map +1 -1
- package/dist/modules/workspace/kb-repository.contract.d.ts +124 -0
- package/dist/modules/workspace/kb-repository.contract.d.ts.map +1 -0
- package/dist/modules/workspace/kb-repository.contract.js +19 -0
- package/dist/modules/workspace/kb-repository.contract.js.map +1 -0
- package/dist/modules/workspace/kb-repository.d.ts +236 -0
- package/dist/modules/workspace/kb-repository.d.ts.map +1 -0
- package/dist/modules/workspace/kb-repository.js +811 -0
- package/dist/modules/workspace/kb-repository.js.map +1 -0
- package/dist/modules/workspace/pattern-matcher.contract.d.ts +50 -0
- package/dist/modules/workspace/pattern-matcher.contract.d.ts.map +1 -0
- package/dist/modules/workspace/pattern-matcher.contract.js +27 -0
- package/dist/modules/workspace/pattern-matcher.contract.js.map +1 -0
- package/dist/modules/workspace/routine-write-policy.d.ts +0 -7
- package/dist/modules/workspace/routine-write-policy.d.ts.map +1 -1
- package/dist/modules/workspace/routine-write-policy.js +0 -5
- package/dist/modules/workspace/routine-write-policy.js.map +1 -1
- package/dist/modules/workspace/set-aside-clone.d.ts +40 -0
- package/dist/modules/workspace/set-aside-clone.d.ts.map +1 -1
- package/dist/modules/workspace/set-aside-clone.js +139 -0
- package/dist/modules/workspace/set-aside-clone.js.map +1 -1
- package/dist/modules/workspace/startup/kb-startup-runner.d.ts +99 -0
- package/dist/modules/workspace/startup/kb-startup-runner.d.ts.map +1 -1
- package/dist/modules/workspace/startup/kb-startup-runner.js +407 -29
- package/dist/modules/workspace/startup/kb-startup-runner.js.map +1 -1
- package/dist/modules/workspace/workspace.service.d.ts +68 -32
- package/dist/modules/workspace/workspace.service.d.ts.map +1 -1
- package/dist/modules/workspace/workspace.service.js +240 -161
- package/dist/modules/workspace/workspace.service.js.map +1 -1
- package/dist/modules/workspace/workspace.tools.d.ts +13 -4
- package/dist/modules/workspace/workspace.tools.d.ts.map +1 -1
- package/dist/modules/workspace/workspace.tools.js +162 -336
- package/dist/modules/workspace/workspace.tools.js.map +1 -1
- package/dist/test-setup.js +4 -0
- package/dist/test-setup.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/test-repositories.ts +22 -0
- package/src/__tests__/workspace-service.ts +69 -0
- package/src/core/__tests__/lifecycle.test.ts +16 -0
- package/src/core/create-core-server.ts +1 -1
- package/src/core/create-core-services.ts +36 -3
- package/src/core/lifecycle.ts +10 -0
- package/src/modules/access/__tests__/access-control.atref-cache.test.ts +144 -4
- package/src/modules/access/__tests__/access-control.group-read.test.ts +73 -0
- package/src/modules/access/__tests__/access.routes.group-grant.test.ts +66 -0
- package/src/modules/access/__tests__/access.routes.unknown-email.test.ts +2 -0
- package/src/modules/access/access-control.interface.ts +22 -0
- package/src/modules/access/access-control.service.ts +142 -14
- package/src/modules/access/access.routes.ts +22 -6
- package/src/modules/agent-instructions/shared-file-rules.ts +1 -1
- package/src/modules/diff/__tests__/diff.service.seed-atomicity.test.ts +2 -1
- package/src/modules/diff/__tests__/diff.service.test.ts +2 -1
- package/src/modules/diff/__tests__/diff.service.worktree.test.ts +76 -0
- package/src/modules/diff/diff.service.ts +6 -4
- package/src/modules/kb-fs/__tests__/git-dir.test.ts +77 -0
- package/src/modules/kb-fs/__tests__/ref-files.test.ts +338 -0
- package/src/modules/kb-fs/git-dir.ts +53 -0
- package/src/modules/kb-fs/index.ts +2 -0
- package/src/modules/kb-fs/ref-files.ts +279 -0
- package/src/modules/plugins/__tests__/teams.routes.test.ts +55 -0
- package/src/modules/plugins/teams.routes.ts +14 -1
- package/src/modules/settings/__tests__/setup.routes.layout-phase.test.ts +2 -2
- package/src/modules/settings/__tests__/setup.routes.managed-phase.test.ts +2 -2
- package/src/modules/tool-auth/internal-token.service.ts +1 -1
- package/src/modules/tool-auth/tool-auth.middleware.ts +1 -2
- package/src/modules/tool-helpers/__tests__/branch-declaration.test.ts +2 -2
- package/src/modules/tool-helpers/tool-handler.ts +2 -2
- package/src/modules/tool-helpers/tool.contract.ts +2 -2
- package/src/modules/tool-registry/__tests__/tool-description-length.test.ts +6 -1
- package/src/modules/workflow/git/__tests__/git.service.changedPathsMemo.test.ts +175 -0
- package/src/modules/workflow/git/__tests__/git.service.worktrees.test.ts +284 -0
- package/src/modules/workflow/git/git.service.ts +184 -22
- package/src/modules/workflow/workflow-hooks.ts +1 -1
- package/src/modules/workspace/__tests__/agent-downloads.test.ts +2 -2
- package/src/modules/workspace/__tests__/bounded-matcher.test.ts +213 -0
- package/src/modules/workspace/__tests__/git-internals.security.test.ts +22 -7
- package/src/modules/workspace/__tests__/kb-repository.test.ts +481 -0
- package/src/modules/workspace/__tests__/rest-tool-arguments.test.ts +14 -0
- package/src/modules/workspace/__tests__/routine-write-policy.test.ts +0 -11
- package/src/modules/workspace/__tests__/workspace-paths-inside-the-repo.test.ts +2 -2
- package/src/modules/workspace/__tests__/workspace.routes.folder-zip-access.test.ts +2 -2
- package/src/modules/workspace/__tests__/workspace.routes.folders.test.ts +4 -3
- package/src/modules/workspace/__tests__/workspace.service.any-workspace-credentials.test.ts +2 -2
- package/src/modules/workspace/__tests__/workspace.service.ensure-remotes-fetched.test.ts +2 -2
- package/src/modules/workspace/__tests__/workspace.service.forget-clone-races.test.ts +12 -5
- package/src/modules/workspace/__tests__/workspace.service.list-cloned.test.ts +6 -6
- package/src/modules/workspace/__tests__/workspace.service.move-never-overwrites.test.ts +2 -1
- package/src/modules/workspace/__tests__/workspace.service.read-filter.test.ts +2 -1
- package/src/modules/workspace/__tests__/workspace.service.replaced-repository.test.ts +2 -1
- package/src/modules/workspace/__tests__/workspace.service.test.ts +24 -23
- package/src/modules/workspace/__tests__/workspace.service.unknown-branch.test.ts +2 -1
- package/src/modules/workspace/__tests__/workspace.service.worktrees.test.ts +620 -0
- package/src/modules/workspace/__tests__/workspace.tools.agents-file.test.ts +15 -1
- package/src/modules/workspace/__tests__/workspace.tools.branch-errors.test.ts +71 -5
- package/src/modules/workspace/__tests__/workspace.tools.test.ts +153 -298
- package/src/modules/workspace/agent-access.gate.ts +2 -2
- package/src/modules/workspace/bounded-matcher.ts +328 -0
- package/src/modules/workspace/fetch-instant.ts +13 -0
- package/src/modules/workspace/index.ts +12 -0
- package/src/modules/workspace/kb-repository.contract.ts +124 -0
- package/src/modules/workspace/kb-repository.ts +844 -0
- package/src/modules/workspace/pattern-matcher.contract.ts +69 -0
- package/src/modules/workspace/routine-write-policy.ts +0 -15
- package/src/modules/workspace/set-aside-clone.ts +140 -0
- package/src/modules/workspace/startup/__tests__/kb-startup-runner.degraded.test.ts +3 -2
- package/src/modules/workspace/startup/__tests__/kb-startup-runner.test.ts +12 -10
- package/src/modules/workspace/startup/__tests__/kb-startup-runner.worktrees.test.ts +613 -0
- package/src/modules/workspace/startup/kb-startup-runner.ts +422 -29
- package/src/modules/workspace/startup/steps/__tests__/steps.test.ts +4 -3
- package/src/modules/workspace/workspace.service.ts +247 -169
- package/src/modules/workspace/workspace.tools.ts +179 -365
- package/src/test-setup.ts +5 -0
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { spawn } from 'node:child_process';
|
|
2
1
|
import nodeFs from 'node:fs/promises';
|
|
3
2
|
import { join } from 'node:path';
|
|
4
3
|
import AdmZip from 'adm-zip';
|
|
@@ -11,7 +10,6 @@ import { workspaceIdForBranch } from '../../shared/workspace-id.js';
|
|
|
11
10
|
import { GitInternalsError, WorkflowValidationError } from '../../shared/domain-errors.js';
|
|
12
11
|
// Leaf-level shared primitive (same exception `workspace.service.ts` already
|
|
13
12
|
// relies on) — not a workflow service, so this stays inside the module boundary.
|
|
14
|
-
import { assertValidBranchName } from '../kb-fs/branch-name.js';
|
|
15
13
|
import { assertInsideRepo, assertRepoRootNameFree, assertRepoRootNameFreeArgs, isInsideRepo, normalizePathArgs, normalizeWorkspacePath, } from '../kb-fs/repo-path.js';
|
|
16
14
|
import { GitGuardedFilesystem } from '../kb-fs/git-guarded-filesystem.js';
|
|
17
15
|
import { assertNoGitInternalsSegment, assertNotGitInternals, hasGitInternalsSegment } from '../../shared/git-internals.js';
|
|
@@ -29,6 +27,7 @@ import { folderPlaceholderPath, isFolderPlaceholder, isPlatformFile, isPlatformF
|
|
|
29
27
|
import { AccessDeniedError } from '../access-model/access-errors.js';
|
|
30
28
|
import { AGENT_GUIDE_FILE, isAgentGuidePath, isManagedGuide, withPlatformGuideAppended, } from '../agent-guide/agent-guide.js';
|
|
31
29
|
import { removeEmptyDirs } from './empty-dirs.js';
|
|
30
|
+
import { endsTheSearch } from './pattern-matcher.contract.js';
|
|
32
31
|
import { FIRST_RUN_SECTION_ID, firstRunNote, knowledgeFolderIsNew } from './first-run.js';
|
|
33
32
|
import { planMoveLinks } from './move-links.js';
|
|
34
33
|
import { MoveLockedError, MoveRacedError } from '../kb-fs/locking-filesystem.js';
|
|
@@ -488,7 +487,7 @@ function modeOf(a) {
|
|
|
488
487
|
* Throws whatever reading the file throws — the caller decides whether that is
|
|
489
488
|
* a file to skip (the walk) or an error to surface (a single-file grep).
|
|
490
489
|
*/
|
|
491
|
-
async function grepOneFile(fs, path,
|
|
490
|
+
async function grepOneFile(fs, path, matcher, out, max, docs) {
|
|
492
491
|
const reader = docs.readers.readerFor(path);
|
|
493
492
|
const bytes = asBytes(await fs.readFile(path));
|
|
494
493
|
let content = reader.greppableText ? await reader.greppableText(bytes, path) : null;
|
|
@@ -508,10 +507,10 @@ async function grepOneFile(fs, path, re, out, max, docs) {
|
|
|
508
507
|
if (content === null)
|
|
509
508
|
return 'no-text';
|
|
510
509
|
const lines = content.split('\n');
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
510
|
+
// matchingLines posts these to the worker in bounded batches, so no one
|
|
511
|
+
// copy of a large file runs on the request thread.
|
|
512
|
+
for (const i of await matcher.matchingLines(lines, max - out.length)) {
|
|
513
|
+
out.push({ path, line: i + 1, text: lines[i].slice(0, 300) });
|
|
515
514
|
}
|
|
516
515
|
return 'searched';
|
|
517
516
|
}
|
|
@@ -548,7 +547,7 @@ async function searchRootKind(fs, path) {
|
|
|
548
547
|
}
|
|
549
548
|
}
|
|
550
549
|
/** JS grep over the workspace tree (read methods only) — bounded by match + depth caps. */
|
|
551
|
-
async function grepWalk(fs, dir,
|
|
550
|
+
async function grepWalk(fs, dir, matcher, out, max, depth, gate, notifyRead, docs,
|
|
552
551
|
/** A file the walk leaves to its caller: never opened, never counted against `max`. */
|
|
553
552
|
skip = () => false) {
|
|
554
553
|
if (out.length >= max || depth > 12)
|
|
@@ -572,7 +571,7 @@ skip = () => false) {
|
|
|
572
571
|
continue;
|
|
573
572
|
const p = dir ? `${dir}/${e.name}` : e.name;
|
|
574
573
|
if (e.type === 'directory') {
|
|
575
|
-
await grepWalk(fs, p,
|
|
574
|
+
await grepWalk(fs, p, matcher, out, max, depth + 1, gate, notifyRead, docs, skip);
|
|
576
575
|
}
|
|
577
576
|
else if (skip(p)) {
|
|
578
577
|
continue;
|
|
@@ -584,10 +583,14 @@ skip = () => false) {
|
|
|
584
583
|
await notifyRead(p);
|
|
585
584
|
// A file the walk cannot read is silently skipped: one unreadable entry
|
|
586
585
|
// must not fail a search over the whole tree.
|
|
586
|
+
// A pattern too expensive to finish, or no worker free to run it, is not
|
|
587
|
+
// an unreadable file: either ends the whole search.
|
|
587
588
|
try {
|
|
588
|
-
await grepOneFile(fs, p,
|
|
589
|
+
await grepOneFile(fs, p, matcher, out, max, docs);
|
|
589
590
|
}
|
|
590
|
-
catch {
|
|
591
|
+
catch (err) {
|
|
592
|
+
if (endsTheSearch(err))
|
|
593
|
+
throw err;
|
|
591
594
|
continue;
|
|
592
595
|
}
|
|
593
596
|
}
|
|
@@ -627,9 +630,10 @@ export function sessionIdInputOf(def) {
|
|
|
627
630
|
* Workspace tools) + unzip. Most just re-expose the SAME `LocalFilesystem`
|
|
628
631
|
* methods Mastra's tools call (via `ctx.getFilesystem(a.branch as string)`), so behaviour is
|
|
629
632
|
* identical and write-side ops still flow through the lock/commit pipeline.
|
|
630
|
-
* `edit_file
|
|
631
|
-
*
|
|
632
|
-
*
|
|
633
|
+
* `edit_file` and `grep` are tool-level (no filesystem method), so they're
|
|
634
|
+
* implemented here. There is no shell tool: an agent never composes a command
|
|
635
|
+
* line, and every git operation it needs is a platform tool with fixed
|
|
636
|
+
* arguments.
|
|
633
637
|
*/
|
|
634
638
|
export function registerWorkspaceTools(registry, router, toolAuth, toolHandler, spillStore, docExtract, accessControl, kb, agentAccessGate, writePolicy, sessionSink,
|
|
635
639
|
/**
|
|
@@ -681,7 +685,14 @@ starterPacks,
|
|
|
681
685
|
* note's look at the knowledge folder. Optional for the harnesses about the
|
|
682
686
|
* file primitives; without it `start_session` answers the id alone.
|
|
683
687
|
*/
|
|
684
|
-
disk
|
|
688
|
+
disk,
|
|
689
|
+
/**
|
|
690
|
+
* What runs a caller's `grep` pattern off the request thread, under a
|
|
691
|
+
* budget (see `pattern-matcher.contract.ts`). Owned by the composition
|
|
692
|
+
* root, which closes it with the graph. Optional for the harnesses about the
|
|
693
|
+
* other file primitives; without it `grep` is not mounted.
|
|
694
|
+
*/
|
|
695
|
+
patternMatcher) {
|
|
685
696
|
const { kbDirName } = kb;
|
|
686
697
|
/**
|
|
687
698
|
* The one extension→reader registry every read-shaped decision routes
|
|
@@ -1314,9 +1325,9 @@ disk) {
|
|
|
1314
1325
|
redescribe();
|
|
1315
1326
|
kb.onLayoutApplied(redescribe);
|
|
1316
1327
|
agentAccessGate.notes.onChange(redescribe);
|
|
1317
|
-
// Internal-only tools
|
|
1318
|
-
//
|
|
1319
|
-
//
|
|
1328
|
+
// Internal-only tools keep their route mounted — our agent calls them over
|
|
1329
|
+
// the same loopback — but gate them to internal-source callers so an
|
|
1330
|
+
// external connection key can't invoke one by name.
|
|
1320
1331
|
router.post(path.slice('/api'.length), toolAuth, ...(spec.internalOnly ? [requireInternalSource] : []),
|
|
1321
1332
|
// EVERY path input becomes a repository path here, once, before any
|
|
1322
1333
|
// handler runs: the root-anchored `/<kbDirName>/…` form Copy path gives
|
|
@@ -1357,9 +1368,7 @@ disk) {
|
|
|
1357
1368
|
catch (err) {
|
|
1358
1369
|
return rethrowAsWriteDenial(err, { tool: spec.name, branch: args.branch, userEmail: ctx.user.email, userId: ctx.user.id }, accessControl, kb, changeGate);
|
|
1359
1370
|
}
|
|
1360
|
-
},
|
|
1361
|
-
// `execute_command` resolves its own branch (see `resolvesBranchItself`).
|
|
1362
|
-
{ write: spec.write, writeScope: spec.writeScope, ...(spec.resolvesBranchItself ? { branch: 'own' } : {}) }));
|
|
1371
|
+
}, { write: spec.write, writeScope: spec.writeScope }));
|
|
1363
1372
|
};
|
|
1364
1373
|
// ── session bootstrap (external agents) ─────────────────────────────────
|
|
1365
1374
|
// Every read/write tool below takes a `sessionId`: the conversation the
|
|
@@ -2005,147 +2014,146 @@ disk) {
|
|
|
2005
2014
|
return { ...out, ...fileTypeOf(reader, p, bytes) };
|
|
2006
2015
|
},
|
|
2007
2016
|
});
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2017
|
+
// Mounted only when the composition supplied a matcher — see the
|
|
2018
|
+
// `patternMatcher` parameter. Core always does.
|
|
2019
|
+
if (patternMatcher) {
|
|
2020
|
+
const patterns = patternMatcher;
|
|
2021
|
+
mount({
|
|
2022
|
+
name: 'grep',
|
|
2023
|
+
gated: true,
|
|
2024
|
+
description: 'Regex search across the workspace. Returns `{ matches: [{ path, line, text }] }` (capped). Use to find where something is defined/referenced. `path` may name a DIRECTORY (searches the subtree) or a single FILE (searches just that file); a path with nothing at it is an error, never an empty result. Searches INSIDE Office and OpenDocument files (.docx/.pptx/.xlsx, .odt/.odp/.ods), PDFs and email files (.eml/.msg) via their extracted text — matches there carry the extraction\'s line numbers, and the `[slide N]`/`[sheet: Name]`/`[page N]`/`[from]`/`[subject]` marker lines locate them; a bounded number of not-yet-extracted documents is extracted per call, and the result notes how many were skipped (re-run to cover them).',
|
|
2025
|
+
inputs: {
|
|
2026
|
+
type: 'object',
|
|
2027
|
+
properties: {
|
|
2028
|
+
branch: BRANCH_INPUT,
|
|
2029
|
+
pattern: str('JavaScript regular expression.'),
|
|
2030
|
+
path: str(`Subtree to search, or a single file to search on its own, with or without a leading slash — a path without the \`${kbDirName}/\` prefix is placed under \`${kbDirName}/\` (default: the whole repository, \`${kbDirName}/\`).`),
|
|
2031
|
+
ignore_case: { type: 'boolean', description: 'Case-insensitive match.' },
|
|
2032
|
+
max_results: { type: 'integer', minimum: 1, maximum: 1000, description: 'Cap on matches (default 200).' },
|
|
2033
|
+
sessionId: SESSION_ID_INPUT,
|
|
2034
|
+
},
|
|
2035
|
+
required: ['branch', 'pattern'],
|
|
2036
|
+
additionalProperties: false,
|
|
2021
2037
|
},
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
properties: { path: str('Workspace-relative file path.'), line: int('1-based line number.'), text: str('The matching line (truncated to 300 chars).') },
|
|
2034
|
-
required: ['path', 'line', 'text'],
|
|
2038
|
+
outputs: {
|
|
2039
|
+
type: 'object',
|
|
2040
|
+
properties: {
|
|
2041
|
+
matches: {
|
|
2042
|
+
type: 'array',
|
|
2043
|
+
description: 'Matching lines (capped by `max_results`).',
|
|
2044
|
+
items: {
|
|
2045
|
+
type: 'object',
|
|
2046
|
+
properties: { path: str('Workspace-relative file path.'), line: int('1-based line number.'), text: str('The matching line (truncated to 300 chars).') },
|
|
2047
|
+
required: ['path', 'line', 'text'],
|
|
2048
|
+
},
|
|
2035
2049
|
},
|
|
2050
|
+
truncated: { type: 'boolean', description: 'True if the match cap was hit and results may be incomplete.' },
|
|
2051
|
+
note: str('Present when the empty/partial result needs explaining: a `path` naming a file with no searchable text (image, binary, corrupt document), or documents (office/PDF/email files) left unsearched because their text was not yet extracted and the per-call extraction budget ran out — re-run grep to cover those.'),
|
|
2036
2052
|
},
|
|
2037
|
-
|
|
2038
|
-
note: str('Present when the empty/partial result needs explaining: a `path` naming a file with no searchable text (image, binary, corrupt document), or documents (office/PDF/email files) left unsearched because their text was not yet extracted and the per-call extraction budget ran out — re-run grep to cover those.'),
|
|
2053
|
+
required: ['matches', 'truncated'],
|
|
2039
2054
|
},
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
// hook of the organisation's file there exactly as `read_file` does
|
|
2095
|
-
// (after the gate), and the platform's guide alone is nobody's file to
|
|
2096
|
-
// note. Each file the walk opens goes to the hook per file below, so a
|
|
2097
|
-
// hook sees every path a grep reached rather than only its root.
|
|
2098
|
-
if (!(coversGuide && kind !== 'directory')) {
|
|
2099
|
-
await notifyAgentRead(agentAccessGate, ctx, a.branch, searchRoot);
|
|
2100
|
-
}
|
|
2101
|
-
if (coversGuide) {
|
|
2102
|
-
if (kind === 'directory') {
|
|
2103
|
-
await grepWalk(fs, searchRoot, re, out, max, 0, gate, (p) => notifyAgentRead(agentAccessGate, ctx, a.branch, p), docs, (p) => p === guidePath);
|
|
2055
|
+
write: false,
|
|
2056
|
+
handler: async (a, ctx) => {
|
|
2057
|
+
// The caller's pattern never runs on the request thread: a backtracking
|
|
2058
|
+
// one would hold the event loop for everyone (see IPatternMatcher).
|
|
2059
|
+
const matcher = patterns.search(a.pattern, a.ignore_case ? 'i' : '');
|
|
2060
|
+
// No path means the whole REPOSITORY, not the workspace directory above
|
|
2061
|
+
// it. The workspace root also holds whatever an older build left beside
|
|
2062
|
+
// the checkout, and the read gate has no rules for a path outside the
|
|
2063
|
+
// repository — it answers "readable" — so a walk from there would hand
|
|
2064
|
+
// any caller the contents of every stray, including documents that were
|
|
2065
|
+
// uploaded to a restricted folder and landed beside it instead. The
|
|
2066
|
+
// boot note names those for an operator; no tool reads them.
|
|
2067
|
+
// An EMPTY string is the same absence: the normaliser leaves it alone
|
|
2068
|
+
// (an empty path is the handler's to explain), and here it would
|
|
2069
|
+
// otherwise name the workspace directory by another spelling.
|
|
2070
|
+
const searchRoot = typeof a.path === 'string' && a.path.length > 0 ? a.path : kbDirName;
|
|
2071
|
+
const fs = await ctx.getFilesystem(a.branch);
|
|
2072
|
+
const gate = readGateFor(a.branch, ctx);
|
|
2073
|
+
const out = [];
|
|
2074
|
+
const max = typeof a.max_results === 'number' ? Math.min(a.max_results, 1000) : 200;
|
|
2075
|
+
const docs = { readers, uncachedBudget: UNCACHED_DOCS_PER_GREP, skippedUncached: 0 };
|
|
2076
|
+
// The empty root is the workspace itself — always a directory, and never
|
|
2077
|
+
// worth a stat.
|
|
2078
|
+
// A placeholder named on its own is searched as what it is to every
|
|
2079
|
+
// other tool: nothing.
|
|
2080
|
+
const kind = searchRoot === ''
|
|
2081
|
+
? 'directory'
|
|
2082
|
+
: isFolderPlaceholder(searchRoot)
|
|
2083
|
+
? 'missing'
|
|
2084
|
+
: await searchRootKind(fs, searchRoot);
|
|
2085
|
+
/** Why a single-file search found nothing, when "no matches" would be a lie. */
|
|
2086
|
+
let fileNote;
|
|
2087
|
+
// The guide is searched where it is read: a search of the repository
|
|
2088
|
+
// root covers it, and a search of its own path is a search of what
|
|
2089
|
+
// `read_file` answers there. The composed text is what is searched —
|
|
2090
|
+
// the knowledge base's own readable file first, then the platform's
|
|
2091
|
+
// guide — under the guide's path and with the line numbers a read of
|
|
2092
|
+
// it gives. The walk leaves that one file to this: a match the walk
|
|
2093
|
+
// made there would be the same line again, and one it COUNTED against
|
|
2094
|
+
// `max_results` would be a file later in the tree never searched while
|
|
2095
|
+
// the answer says nothing was cut. A file the caller may not read is
|
|
2096
|
+
// absent from it, as it is from the read.
|
|
2097
|
+
const guidePath = `${kbDirName}/${AGENT_GUIDE_FILE}`;
|
|
2098
|
+
const rel = toKbRelative(searchRoot, kbDirName);
|
|
2099
|
+
const coversGuide = agentGuide !== undefined && (kind === 'directory' ? rel === null : isAgentGuidePath(rel ?? ''));
|
|
2100
|
+
// The search root goes to the read hook — once, and only when it is
|
|
2101
|
+
// repository content: a folder, or a file of the knowledge base's own.
|
|
2102
|
+
// The guide's own path is not told here, because `guideAt` tells the
|
|
2103
|
+
// hook of the organisation's file there exactly as `read_file` does
|
|
2104
|
+
// (after the gate), and the platform's guide alone is nobody's file to
|
|
2105
|
+
// note. Each file the walk opens goes to the hook per file below, so a
|
|
2106
|
+
// hook sees every path a grep reached rather than only its root.
|
|
2107
|
+
if (!(coversGuide && kind !== 'directory')) {
|
|
2108
|
+
await notifyAgentRead(agentAccessGate, ctx, a.branch, searchRoot);
|
|
2104
2109
|
}
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2110
|
+
if (coversGuide) {
|
|
2111
|
+
if (kind === 'directory') {
|
|
2112
|
+
await grepWalk(fs, searchRoot, matcher, out, max, 0, gate, (p) => notifyAgentRead(agentAccessGate, ctx, a.branch, p), docs, (p) => p === guidePath);
|
|
2113
|
+
}
|
|
2114
|
+
const lines = (await guideAt(a.branch, ctx, guidePath)).split('\n');
|
|
2115
|
+
for (const i of await matcher.matchingLines(lines, max - out.length)) {
|
|
2109
2116
|
out.push({ path: guidePath, line: i + 1, text: lines[i].slice(0, 300) });
|
|
2117
|
+
}
|
|
2110
2118
|
}
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
await grepWalk(fs, searchRoot, re, out, max, 0, gate, (p) => notifyAgentRead(agentAccessGate, ctx, a.branch, p), docs);
|
|
2114
|
-
}
|
|
2115
|
-
else {
|
|
2116
|
-
// Not a directory: the permission verdict comes BEFORE every other
|
|
2117
|
-
// one, and it is `read_file`'s own gate on the same path — so grep
|
|
2118
|
-
// answers a path the caller may not read exactly as read_file does,
|
|
2119
|
-
// and can never confirm the existence of one read_file would hide.
|
|
2120
|
-
// That ordering holds even when the stat itself failed: a denied path
|
|
2121
|
-
// gets the 403, never the filesystem's complaint about it.
|
|
2122
|
-
await assertCanRead(gate, searchRoot);
|
|
2123
|
-
if (kind === 'missing')
|
|
2124
|
-
throw notFound(searchRoot, 'Nothing to search');
|
|
2125
|
-
// A named FILE is searched directly: routing it through the walk would
|
|
2126
|
-
// fail its readdir and answer an empty match list, which the caller
|
|
2127
|
-
// cannot tell from "the pattern is not in this file".
|
|
2128
|
-
const outcome = await orNotFound(searchRoot, () => grepOneFile(fs, searchRoot, re, out, max, docs), 'Nothing to search');
|
|
2129
|
-
if (outcome === 'no-text') {
|
|
2130
|
-
fileNote =
|
|
2131
|
-
`"${displayPath(searchRoot)}" has no searchable text — it is an image, binary content, or a document ` +
|
|
2132
|
-
'whose text could not be extracted. There were no matches because there was nothing to search, not ' +
|
|
2133
|
-
'because the pattern is absent.';
|
|
2119
|
+
else if (kind === 'directory') {
|
|
2120
|
+
await grepWalk(fs, searchRoot, matcher, out, max, 0, gate, (p) => notifyAgentRead(agentAccessGate, ctx, a.branch, p), docs);
|
|
2134
2121
|
}
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2122
|
+
else {
|
|
2123
|
+
// Not a directory: the permission verdict comes BEFORE every other
|
|
2124
|
+
// one, and it is `read_file`'s own gate on the same path — so grep
|
|
2125
|
+
// answers a path the caller may not read exactly as read_file does,
|
|
2126
|
+
// and can never confirm the existence of one read_file would hide.
|
|
2127
|
+
// That ordering holds even when the stat itself failed: a denied path
|
|
2128
|
+
// gets the 403, never the filesystem's complaint about it.
|
|
2129
|
+
await assertCanRead(gate, searchRoot);
|
|
2130
|
+
if (kind === 'missing')
|
|
2131
|
+
throw notFound(searchRoot, 'Nothing to search');
|
|
2132
|
+
// A named FILE is searched directly: routing it through the walk would
|
|
2133
|
+
// fail its readdir and answer an empty match list, which the caller
|
|
2134
|
+
// cannot tell from "the pattern is not in this file".
|
|
2135
|
+
const outcome = await orNotFound(searchRoot, () => grepOneFile(fs, searchRoot, matcher, out, max, docs), 'Nothing to search');
|
|
2136
|
+
if (outcome === 'no-text') {
|
|
2137
|
+
fileNote =
|
|
2138
|
+
`"${displayPath(searchRoot)}" has no searchable text — it is an image, binary content, or a document ` +
|
|
2139
|
+
'whose text could not be extracted. There were no matches because there was nothing to search, not ' +
|
|
2140
|
+
'because the pattern is absent.';
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
const note = fileNote ??
|
|
2144
|
+
(docs.skippedUncached > 0
|
|
2145
|
+
? `${docs.skippedUncached} document(s) (office/PDF/email files) were not searched: their text was not yet ` +
|
|
2146
|
+
`extracted and this call's extraction budget (${UNCACHED_DOCS_PER_GREP}) ran out. Re-run the ` +
|
|
2147
|
+
'same grep to extract and search the next batch.'
|
|
2148
|
+
: undefined);
|
|
2149
|
+
return {
|
|
2150
|
+
matches: out,
|
|
2151
|
+
truncated: out.length >= max,
|
|
2152
|
+
...(note !== undefined ? { note } : {}),
|
|
2153
|
+
};
|
|
2154
|
+
},
|
|
2155
|
+
});
|
|
2156
|
+
}
|
|
2149
2157
|
// ── writes (through the lock/commit pipeline) ───────────────────────────
|
|
2150
2158
|
mount({
|
|
2151
2159
|
name: 'write_file',
|
|
@@ -3589,188 +3597,6 @@ disk) {
|
|
|
3589
3597
|
};
|
|
3590
3598
|
});
|
|
3591
3599
|
}
|
|
3592
|
-
// ── shell (internal-only) ───────────────────────────────────────────────
|
|
3593
|
-
mount({
|
|
3594
|
-
name: 'execute_command',
|
|
3595
|
-
gated: true,
|
|
3596
|
-
description: 'Run a shell command in the workspace directory. Returns `{ stdout, stderr, exitCode }` (output capped). Use for git status/log, grep/rg, build/test commands.',
|
|
3597
|
-
internalOnly: true,
|
|
3598
|
-
fileTool: false,
|
|
3599
|
-
// The one tool the mount's branch check skips: the handler below resolves an
|
|
3600
|
-
// omitted `branch` to the internal caller's focused branch before refusing.
|
|
3601
|
-
resolvesBranchItself: true,
|
|
3602
|
-
inputs: {
|
|
3603
|
-
type: 'object',
|
|
3604
|
-
properties: {
|
|
3605
|
-
branch: BRANCH_INPUT,
|
|
3606
|
-
command: str('The shell command line to run.'),
|
|
3607
|
-
timeout_ms: { type: 'integer', minimum: 1000, maximum: 120000, description: 'Timeout in ms (default 30000).' },
|
|
3608
|
-
sessionId: SESSION_ID_INPUT,
|
|
3609
|
-
},
|
|
3610
|
-
// `branch` stays REQUIRED here on purpose, and must not be relaxed to make
|
|
3611
|
-
// the handler's focused-branch fallback "reachable". This list is the
|
|
3612
|
-
// contract the model is TAUGHT — declaring it required is what makes the
|
|
3613
|
-
// caller always name its branch, which is the fix for the workspace this
|
|
3614
|
-
// tool used to resolve as "undefined". It is not a runtime gate: nothing in
|
|
3615
|
-
// the tool route validates inputs against this schema, so a branch-less call
|
|
3616
|
-
// still reaches the handler and still hits the `ctx.focusedBranch` fallback
|
|
3617
|
-
// (covered by "falls back to the session focused branch when branch is
|
|
3618
|
-
// omitted"). The fallback is a safety net for a model that disobeys the
|
|
3619
|
-
// contract — not a sanctioned calling convention to advertise.
|
|
3620
|
-
required: ['branch', 'command'],
|
|
3621
|
-
additionalProperties: false,
|
|
3622
|
-
},
|
|
3623
|
-
outputs: {
|
|
3624
|
-
type: 'object',
|
|
3625
|
-
properties: {
|
|
3626
|
-
stdout: str('Captured standard output (truncated to 50,000 chars).'),
|
|
3627
|
-
stderr: str('Captured standard error (truncated to 50,000 chars).'),
|
|
3628
|
-
exitCode: int('Process exit code; -1 if it was killed (timeout/abort) or failed to spawn.'),
|
|
3629
|
-
},
|
|
3630
|
-
required: ['stdout', 'stderr', 'exitCode'],
|
|
3631
|
-
},
|
|
3632
|
-
write: true,
|
|
3633
|
-
handler: async (a, ctx) => {
|
|
3634
|
-
// Resolve the branch FIRST — before the policy gates below — so a malformed
|
|
3635
|
-
// call always gets the 400 that names what is missing, rather than a 403
|
|
3636
|
-
// from a restricted session masking it. Unlike the file tools (which route
|
|
3637
|
-
// through `getOrCreateForUser` and fall back to the default branch),
|
|
3638
|
-
// execute_command turns `branch` straight into a workspace — so a bad value
|
|
3639
|
-
// would `encodeURIComponent(undefined)` to the string "undefined", then try
|
|
3640
|
-
// to CLONE a branch literally named "undefined", 500ing and leaving an
|
|
3641
|
-
// `<workspaces>/undefined/` shell behind.
|
|
3642
|
-
//
|
|
3643
|
-
// Two distinct cases:
|
|
3644
|
-
// - branch OMITTED (`undefined`): the in-app chat agent may leave it off a
|
|
3645
|
-
// call. For an internal session we fall back to the caller's own focused
|
|
3646
|
-
// branch (`ctx.focusedBranch`, from its signed token), so the command runs
|
|
3647
|
-
// against the workspace the session is on (e.g. `main`) end to end. An
|
|
3648
|
-
// external caller carries no focused branch, so it stays absent → 400.
|
|
3649
|
-
// - branch PRESENT but invalid (the literal "undefined"/"null", empty, or
|
|
3650
|
-
// malformed): a broken value, never a real branch — fail closed, never
|
|
3651
|
-
// silently reinterpret it as the focused branch.
|
|
3652
|
-
const raw = a.branch;
|
|
3653
|
-
const branch = raw === undefined ? ctx.focusedBranch : raw;
|
|
3654
|
-
if (typeof branch !== 'string' || branch.length === 0) {
|
|
3655
|
-
throw new ToolError('execute_command requires a `branch`: pass the branch (draft) whose workspace to run the command in — the one you are currently working on.', 400,
|
|
3656
|
-
// The same discriminator every other KB tool answers a branch-less
|
|
3657
|
-
// call with, so a client switches on one kind across the surface.
|
|
3658
|
-
// The MESSAGE stays this tool's own: it can name the focused-branch
|
|
3659
|
-
// fallback that only applies here.
|
|
3660
|
-
{ kind: 'branch-required' });
|
|
3661
|
-
}
|
|
3662
|
-
// A stringified absent value. Both are syntactically valid git branch names,
|
|
3663
|
-
// so `assertValidBranchName` below happily accepts them — and accepting one
|
|
3664
|
-
// is the whole bug this tool is guarded for: `getOrCreateForBranch("undefined")`
|
|
3665
|
-
// clones a branch literally named "undefined" into `<workspaces>/undefined/`
|
|
3666
|
-
// and 500s. Reject them by name, ahead of the shape check.
|
|
3667
|
-
if (branch === 'undefined' || branch === 'null') {
|
|
3668
|
-
throw new ToolError(`execute_command got the literal string "${branch}" as \`branch\` — that is a stringified absent value, not a branch. ` +
|
|
3669
|
-
'Pass the real branch (draft) whose workspace to run the command in.', 400, { kind: 'branch-required' });
|
|
3670
|
-
}
|
|
3671
|
-
// Then the SHAPE, via the one canonical validator every other branch path
|
|
3672
|
-
// uses — no hand-maintained list of suspicious literals, which would both
|
|
3673
|
-
// miss malformed refs (`..`, `-x`, `foo/.lock`) and reserve names git
|
|
3674
|
-
// considers perfectly valid. Runs on the RAW string, so a whitespace-padded
|
|
3675
|
-
// `" main "` is refused rather than silently trimmed into a different
|
|
3676
|
-
// branch than the caller passed.
|
|
3677
|
-
try {
|
|
3678
|
-
assertValidBranchName(branch);
|
|
3679
|
-
}
|
|
3680
|
-
catch (err) {
|
|
3681
|
-
throw new ToolError(`execute_command got an invalid \`branch\`: ${err.message} — ` +
|
|
3682
|
-
'pass the exact branch (draft) whose workspace to run the command in.', 400);
|
|
3683
|
-
}
|
|
3684
|
-
// Shell is a write path with no single target path to check, so the
|
|
3685
|
-
// write hook is asked once for the call itself, with no path — and the
|
|
3686
|
-
// run must not be restricted to a file type either, since shell could
|
|
3687
|
-
// write anything.
|
|
3688
|
-
writePolicy.assertUnrestricted(ctx.sessionId);
|
|
3689
|
-
await assertAgentWriteAllowed(agentAccessGate, ctx, branch);
|
|
3690
|
-
// Canonical per-branch bootstrap entry point — it owns the workspace-id
|
|
3691
|
-
// encoding and the single-flight clone, so the shell never derives a
|
|
3692
|
-
// workspace path by hand.
|
|
3693
|
-
const cwd = (await ctx.workspaceService.getOrCreateForBranch(branch)).absolutePath;
|
|
3694
|
-
const timeoutMs = typeof a.timeout_ms === 'number' ? a.timeout_ms : 30_000;
|
|
3695
|
-
// cwd is the workspace ROOT so shell paths match the workspace-relative
|
|
3696
|
-
// paths every other file tool uses — but the git clone lives one level
|
|
3697
|
-
// deeper, at <workspace>/<kbDirName>/.git. For a bare `git status` /
|
|
3698
|
-
// `git diff` (what the agent prompt teaches) point git there explicitly so
|
|
3699
|
-
// it resolves the KB clone instead of failing with "not a git repository".
|
|
3700
|
-
//
|
|
3701
|
-
// Because the command runs under `shell: true`, GIT_DIR/GIT_WORK_TREE
|
|
3702
|
-
// apply to the WHOLE shell, so restrict the override to a *standalone* bare
|
|
3703
|
-
// `git …` invocation. Otherwise the KB repo context would (a) leak into a
|
|
3704
|
-
// chained step that spawns its own git — `git commit && npm ci` — and (b)
|
|
3705
|
-
// override a caller's explicit `git -C …` / `--git-dir` / `--work-tree`.
|
|
3706
|
-
const command = a.command.trim();
|
|
3707
|
-
const isStandaloneGit = /^git(\s|$)/.test(command) &&
|
|
3708
|
-
!/[;&|`\n]|\$\(/.test(command) &&
|
|
3709
|
-
!/(?:^|\s)(?:-C|--git-dir|--work-tree)(?:[=\s]|$)/.test(command);
|
|
3710
|
-
const env = isStandaloneGit
|
|
3711
|
-
? {
|
|
3712
|
-
...process.env,
|
|
3713
|
-
GIT_DIR: join(cwd, kbDirName, '.git'),
|
|
3714
|
-
GIT_WORK_TREE: join(cwd, kbDirName),
|
|
3715
|
-
}
|
|
3716
|
-
: process.env;
|
|
3717
|
-
return new Promise((resolve) => {
|
|
3718
|
-
// Own process group on POSIX, so a timeout or abort kills the WHOLE
|
|
3719
|
-
// tree. `shell: true` puts an `sh` between us and the command; killing
|
|
3720
|
-
// only that shell left whatever it had started running on — orphaned,
|
|
3721
|
-
// unreaped, still holding the workspace and its memory. A self-hosted
|
|
3722
|
-
// deployment leaked ~4,500 tasks that way before nothing could fork.
|
|
3723
|
-
const detached = process.platform !== 'win32';
|
|
3724
|
-
const child = spawn(a.command, { cwd, shell: true, env, detached });
|
|
3725
|
-
let stdout = '';
|
|
3726
|
-
let stderr = '';
|
|
3727
|
-
const killTree = () => {
|
|
3728
|
-
try {
|
|
3729
|
-
if (detached && child.pid)
|
|
3730
|
-
process.kill(-child.pid, 'SIGKILL');
|
|
3731
|
-
else
|
|
3732
|
-
child.kill('SIGKILL');
|
|
3733
|
-
}
|
|
3734
|
-
catch {
|
|
3735
|
-
try {
|
|
3736
|
-
child.kill('SIGKILL');
|
|
3737
|
-
}
|
|
3738
|
-
catch {
|
|
3739
|
-
// Already gone.
|
|
3740
|
-
}
|
|
3741
|
-
}
|
|
3742
|
-
};
|
|
3743
|
-
const timer = setTimeout(killTree, timeoutMs);
|
|
3744
|
-
// If the caller disconnects (request aborted), kill the tree instead of
|
|
3745
|
-
// letting it run to the timeout; the resulting `close`/`error` settles
|
|
3746
|
-
// the promise through `finish`.
|
|
3747
|
-
const onAbort = killTree;
|
|
3748
|
-
ctx.abortSignal.addEventListener('abort', onAbort, { once: true });
|
|
3749
|
-
const finish = (value) => {
|
|
3750
|
-
clearTimeout(timer);
|
|
3751
|
-
ctx.abortSignal.removeEventListener('abort', onAbort);
|
|
3752
|
-
resolve(value);
|
|
3753
|
-
};
|
|
3754
|
-
// Cap accumulation while streaming so a verbose command can't exhaust
|
|
3755
|
-
// memory before `close`; the final slice keeps the output bound.
|
|
3756
|
-
const OUTPUT_CAP = 50_000;
|
|
3757
|
-
child.stdout.on('data', (d) => {
|
|
3758
|
-
if (stdout.length < OUTPUT_CAP)
|
|
3759
|
-
stdout += d.toString();
|
|
3760
|
-
});
|
|
3761
|
-
child.stderr.on('data', (d) => {
|
|
3762
|
-
if (stderr.length < OUTPUT_CAP)
|
|
3763
|
-
stderr += d.toString();
|
|
3764
|
-
});
|
|
3765
|
-
child.on('close', (code) => {
|
|
3766
|
-
finish({ stdout: stdout.slice(0, 50_000), stderr: stderr.slice(0, 50_000), exitCode: code ?? -1 });
|
|
3767
|
-
});
|
|
3768
|
-
child.on('error', (err) => {
|
|
3769
|
-
finish({ stdout, stderr: `${stderr}\n${String(err)}`.slice(0, 50_000), exitCode: -1 });
|
|
3770
|
-
});
|
|
3771
|
-
});
|
|
3772
|
-
},
|
|
3773
|
-
});
|
|
3774
3600
|
// The one read `open_page` borrows — see `readForTool` above.
|
|
3775
3601
|
return { readForTool };
|
|
3776
3602
|
}
|