@arnilo/prism-tools 0.0.0-stage → 0.13.1
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/CHANGELOG.md +418 -0
- package/LICENSE +21 -0
- package/README.md +44 -2
- package/browser/README.md +98 -0
- package/browser/skills/browser-automation/SKILL.md +14 -0
- package/browser/src/actions/cdp.ts +131 -0
- package/browser/src/actions/dialog.ts +25 -0
- package/browser/src/actions/download_release.ts +22 -0
- package/browser/src/actions/index.ts +37 -0
- package/browser/src/actions/locator.ts +70 -0
- package/browser/src/actions/navigate.ts +23 -0
- package/browser/src/actions/screenshot.ts +27 -0
- package/browser/src/actions/scroll.ts +24 -0
- package/browser/src/actions/select_page.ts +21 -0
- package/browser/src/actions/types.ts +114 -0
- package/browser/src/actions/upload.ts +44 -0
- package/browser/src/actions/wait.ts +45 -0
- package/browser/src/browser.ts +197 -0
- package/browser/src/cdp.ts +178 -0
- package/browser/src/checkpoint.ts +189 -0
- package/browser/src/config.ts +72 -0
- package/browser/src/downloads.ts +253 -0
- package/browser/src/errors.ts +31 -0
- package/browser/src/evaluate.ts +101 -0
- package/browser/src/index.ts +129 -0
- package/browser/src/limits.ts +271 -0
- package/browser/src/manager.ts +730 -0
- package/browser/src/network.ts +263 -0
- package/browser/src/observe.ts +178 -0
- package/browser/src/policy.ts +133 -0
- package/browser/src/screenshot.ts +107 -0
- package/browser/src/shared-sandbox.ts +53 -0
- package/browser/src/snapshot.ts +99 -0
- package/browser/src/targets.ts +165 -0
- package/browser/src/tools.ts +480 -0
- package/browser/src/types.ts +316 -0
- package/browser/src/uploads.ts +174 -0
- package/coding/README.md +337 -0
- package/coding/src/agent/acp-operations.ts +146 -0
- package/coding/src/agent/artifacts.ts +38 -0
- package/coding/src/agent/ask-user-decision.ts +623 -0
- package/coding/src/agent/atomic-write.ts +23 -0
- package/coding/src/agent/bounded-file.ts +21 -0
- package/coding/src/agent/checks.ts +301 -0
- package/coding/src/agent/coding-checkpoint.ts +828 -0
- package/coding/src/agent/delete.ts +237 -0
- package/coding/src/agent/diagnostics-tool.ts +163 -0
- package/coding/src/agent/diagnostics.ts +262 -0
- package/coding/src/agent/edit-diff.ts +382 -0
- package/coding/src/agent/edit.ts +322 -0
- package/coding/src/agent/effects.ts +102 -0
- package/coding/src/agent/env.ts +28 -0
- package/coding/src/agent/execution-policy.ts +89 -0
- package/coding/src/agent/file-mutation-queue.ts +72 -0
- package/coding/src/agent/forge/github.ts +603 -0
- package/coding/src/agent/forge/index.ts +16 -0
- package/coding/src/agent/forge/types.ts +185 -0
- package/coding/src/agent/git-aware-repository.ts +305 -0
- package/coding/src/agent/git-exec.ts +326 -0
- package/coding/src/agent/git-status.ts +177 -0
- package/coding/src/agent/git-tools.ts +360 -0
- package/coding/src/agent/git.ts +1074 -0
- package/coding/src/agent/glob-match.ts +146 -0
- package/coding/src/agent/glob.ts +182 -0
- package/coding/src/agent/goal-verify.ts +385 -0
- package/coding/src/agent/grep-worker.ts +74 -0
- package/coding/src/agent/grep.ts +459 -0
- package/coding/src/agent/identity.ts +48 -0
- package/coding/src/agent/index.ts +676 -0
- package/coding/src/agent/language/client.ts +429 -0
- package/coding/src/agent/language/framing.ts +190 -0
- package/coding/src/agent/language/index.ts +23 -0
- package/coding/src/agent/language/intelligence.ts +682 -0
- package/coding/src/agent/language/types.ts +187 -0
- package/coding/src/agent/lifecycle-events.ts +121 -0
- package/coding/src/agent/lifecycle.ts +166 -0
- package/coding/src/agent/limits.ts +214 -0
- package/coding/src/agent/move.ts +178 -0
- package/coding/src/agent/mutation-path.ts +52 -0
- package/coding/src/agent/output-accumulator.ts +304 -0
- package/coding/src/agent/path-utils.ts +120 -0
- package/coding/src/agent/process/index.ts +58 -0
- package/coding/src/agent/process/recovery.ts +508 -0
- package/coding/src/agent/process/sessions-host.ts +222 -0
- package/coding/src/agent/process/sessions-monitor.ts +141 -0
- package/coding/src/agent/process/sessions-recovery.ts +241 -0
- package/coding/src/agent/process/sessions-spawn.ts +613 -0
- package/coding/src/agent/process/sessions-teardown.ts +281 -0
- package/coding/src/agent/process/sessions.ts +30 -0
- package/coding/src/agent/process/types.ts +358 -0
- package/coding/src/agent/read-path-set.ts +126 -0
- package/coding/src/agent/read.ts +713 -0
- package/coding/src/agent/repository/glob.ts +147 -0
- package/coding/src/agent/repository/indexed-search.ts +480 -0
- package/coding/src/agent/repository/list.ts +131 -0
- package/coding/src/agent/repository/operations.ts +22 -0
- package/coding/src/agent/repository/path.ts +98 -0
- package/coding/src/agent/repository/search.ts +277 -0
- package/coding/src/agent/repository/types.ts +243 -0
- package/coding/src/agent/repository/walk.ts +126 -0
- package/coding/src/agent/repository.ts +12 -0
- package/coding/src/agent/shell-background.ts +264 -0
- package/coding/src/agent/shell-output.ts +75 -0
- package/coding/src/agent/shell.ts +526 -0
- package/coding/src/agent/todo.ts +399 -0
- package/coding/src/agent/truncate.ts +251 -0
- package/coding/src/agent/write.ts +170 -0
- package/coding/src/ask-user-host.ts +48 -0
- package/coding/src/config.ts +278 -0
- package/coding/src/hard-deny.ts +97 -0
- package/coding/src/index.ts +318 -0
- package/coding/src/principal-guard.ts +84 -0
- package/coding/src/sandbox.ts +92 -0
- package/coding/src/security/approval.ts +144 -0
- package/coding/src/security/command-rules.ts +57 -0
- package/coding/src/security/docker-cli.ts +222 -0
- package/coding/src/security/docker-sandbox.ts +1205 -0
- package/coding/src/security/e2b-sandbox.ts +853 -0
- package/coding/src/security/egress/dns-pin.ts +88 -0
- package/coding/src/security/egress/index.ts +32 -0
- package/coding/src/security/egress/limits.ts +57 -0
- package/coding/src/security/egress/policy.ts +137 -0
- package/coding/src/security/egress/proxy.ts +599 -0
- package/coding/src/security/egress/types.ts +30 -0
- package/coding/src/security/index.ts +199 -0
- package/coding/src/security/native-sandbox.ts +639 -0
- package/coding/src/security/path-containment.ts +55 -0
- package/coding/src/security/sandbox-coding-operations.ts +392 -0
- package/coding/src/security/sandbox-fs-operations.ts +644 -0
- package/coding/src/security/sandbox-limits.ts +147 -0
- package/coding/src/security/sandbox-tar.ts +308 -0
- package/coding/src/security/sandbox.ts +179 -0
- package/coding/src/security/semaphore.ts +55 -0
- package/coding/src/targets.ts +110 -0
- package/coding/src/workspace/diff.ts +78 -0
- package/coding/src/workspace/errors.ts +29 -0
- package/coding/src/workspace/index.ts +5 -0
- package/coding/src/workspace/merge.ts +151 -0
- package/coding/src/workspace/routing.ts +76 -0
- package/coding/src/workspace/service.ts +584 -0
- package/computer-use/README.md +105 -0
- package/computer-use/skills/computer-use-linux/SKILL.md +17 -0
- package/computer-use/src/classify.ts +53 -0
- package/computer-use/src/command.ts +29 -0
- package/computer-use/src/config.ts +69 -0
- package/computer-use/src/create.ts +170 -0
- package/computer-use/src/devices.ts +247 -0
- package/computer-use/src/env.ts +28 -0
- package/computer-use/src/index.ts +157 -0
- package/computer-use/src/skill.ts +21 -0
- package/openapi/README.md +97 -0
- package/openapi/src/compile.ts +386 -0
- package/openapi/src/config.ts +73 -0
- package/openapi/src/errors.ts +16 -0
- package/openapi/src/execute.ts +241 -0
- package/openapi/src/index.ts +124 -0
- package/openapi/src/limits.ts +63 -0
- package/openapi/src/specs.ts +86 -0
- package/openapi/src/tools.ts +60 -0
- package/openapi/src/types.ts +61 -0
- package/package.json +171 -4
- package/research/README.md +72 -0
- package/research/skills/research-report/SKILL.md +12 -0
- package/research/skills/research-scoping/SKILL.md +10 -0
- package/research/skills/research-synthesis/SKILL.md +10 -0
- package/research/skills/source-triage/SKILL.md +10 -0
- package/research/src/check.ts +71 -0
- package/research/src/config.ts +41 -0
- package/research/src/index.ts +147 -0
- package/research/src/ledger.ts +157 -0
- package/research/src/urls.ts +21 -0
- package/web/README.md +287 -0
- package/web/skills/web-research/SKILL.md +17 -0
- package/web/src/brave.ts +66 -0
- package/web/src/cache.ts +151 -0
- package/web/src/config.ts +210 -0
- package/web/src/controls.ts +128 -0
- package/web/src/domains.ts +114 -0
- package/web/src/exa.ts +60 -0
- package/web/src/firecrawl.ts +160 -0
- package/web/src/index.ts +387 -0
- package/web/src/kagi.ts +73 -0
- package/web/src/limits.ts +55 -0
- package/web/src/native/convert.ts +84 -0
- package/web/src/native/fetch.ts +226 -0
- package/web/src/normalize.ts +131 -0
- package/web/src/obscura/cdp.ts +201 -0
- package/web/src/obscura/classify.ts +35 -0
- package/web/src/obscura/cli.ts +254 -0
- package/web/src/obscura/errors.ts +8 -0
- package/web/src/obscura/index.ts +59 -0
- package/web/src/obscura/limits.ts +92 -0
- package/web/src/obscura/mcp.ts +104 -0
- package/web/src/obscura/playwright.ts +133 -0
- package/web/src/obscura/process.ts +235 -0
- package/web/src/obscura/search-profile.ts +73 -0
- package/web/src/obscura/types.ts +48 -0
- package/web/src/obscura/web.ts +419 -0
- package/web/src/prompt-fetch.ts +88 -0
- package/web/src/render.ts +75 -0
- package/web/src/searxng.ts +78 -0
- package/web/src/slice.ts +139 -0
- package/web/src/tavily.ts +77 -0
- package/web/src/tools.ts +182 -0
- package/web/src/transport.ts +310 -0
- package/web/src/types.ts +199 -0
- package/work/README.md +77 -0
- package/work/THIRD_PARTY_NOTICES +24 -0
- package/work/docling/ocr.py +150 -0
- package/work/sandbox/Dockerfile +36 -0
- package/work/sandbox/soffice.sh +12 -0
- package/work/src/command.ts +29 -0
- package/work/src/config.ts +138 -0
- package/work/src/connectors/cli.ts +343 -0
- package/work/src/connectors/drafts.ts +647 -0
- package/work/src/connectors/errors.ts +8 -0
- package/work/src/connectors/file-bytes.ts +149 -0
- package/work/src/connectors/google-workspace-http.ts +399 -0
- package/work/src/connectors/google-workspace.ts +465 -0
- package/work/src/connectors/gws-tools.ts +378 -0
- package/work/src/connectors/http.ts +123 -0
- package/work/src/connectors/idempotency.ts +235 -0
- package/work/src/connectors/identity.ts +34 -0
- package/work/src/connectors/index.ts +93 -0
- package/work/src/connectors/limits.ts +50 -0
- package/work/src/connectors/m365-tools.ts +308 -0
- package/work/src/connectors/microsoft365-http.ts +428 -0
- package/work/src/connectors/microsoft365.ts +402 -0
- package/work/src/connectors/normalize.ts +120 -0
- package/work/src/connectors/tool-helpers.ts +398 -0
- package/work/src/connectors/tools.ts +42 -0
- package/work/src/connectors/types.ts +399 -0
- package/work/src/diagrams/canonicalize.ts +166 -0
- package/work/src/diagrams/caps.ts +52 -0
- package/work/src/diagrams/embed.ts +340 -0
- package/work/src/diagrams/errors.ts +76 -0
- package/work/src/diagrams/index.ts +81 -0
- package/work/src/diagrams/messages.ts +150 -0
- package/work/src/diagrams/origin.ts +52 -0
- package/work/src/diagrams/telemetry.ts +34 -0
- package/work/src/diagrams/xml.ts +267 -0
- package/work/src/document-extraction/index.ts +563 -0
- package/work/src/document-reader/ambient.d.ts +2 -0
- package/work/src/document-reader/errors.ts +7 -0
- package/work/src/document-reader/index.ts +181 -0
- package/work/src/document-reader/mistral-ocr.ts +318 -0
- package/work/src/document-reader/parsers.ts +171 -0
- package/work/src/documents/caps.ts +135 -0
- package/work/src/documents/diff.ts +269 -0
- package/work/src/documents/errors.ts +44 -0
- package/work/src/documents/fidelity.ts +98 -0
- package/work/src/documents/generate.ts +96 -0
- package/work/src/documents/hash.ts +8 -0
- package/work/src/documents/index.ts +14 -0
- package/work/src/documents/model-schema.ts +639 -0
- package/work/src/documents/parse.ts +252 -0
- package/work/src/documents/patch-history.ts +81 -0
- package/work/src/documents/patch.ts +476 -0
- package/work/src/documents/preview-html.ts +245 -0
- package/work/src/documents/preview.ts +227 -0
- package/work/src/documents/telemetry.ts +36 -0
- package/work/src/documents/translate/docx.ts +292 -0
- package/work/src/documents/translate/pptx.ts +304 -0
- package/work/src/documents/translate/xlsx.ts +137 -0
- package/work/src/documents/types.ts +188 -0
- package/work/src/index.ts +311 -0
- package/work/src/runtime/worker-pool-entry.ts +102 -0
- package/work/src/runtime/worker-pool.ts +267 -0
- package/work/src/sandbox/composition.ts +212 -0
- package/work/src/sandbox/image.ts +6 -0
- package/work/src/sandbox/index.ts +14 -0
- package/work/src/sheets/caps.ts +68 -0
- package/work/src/sheets/csv.ts +351 -0
- package/work/src/sheets/decimal.ts +388 -0
- package/work/src/sheets/errors.ts +52 -0
- package/work/src/sheets/index.ts +62 -0
- package/work/src/sheets/inference.ts +378 -0
- package/work/src/sheets/telemetry.ts +34 -0
- package/work/src/sheets/types.ts +162 -0
- package/work/src/sheets/xlsx.ts +388 -0
- package/work/src/skills/index.ts +8 -0
- package/work/src/skills/load.ts +43 -0
- package/work/src/tools/filesystem.ts +21 -0
- package/work/src/tools/index.ts +1 -0
- package/work/src/tools/local-filesystem.ts +33 -0
- package/work/src/tools/office.ts +311 -0
- package/work/vendor/hermes-agent/LICENSE +21 -0
- package/work/vendor/hermes-agent/skills/productivity/docx/LICENSE +21 -0
- package/work/vendor/hermes-agent/skills/productivity/docx/SKILL.md +196 -0
- package/work/vendor/hermes-agent/skills/productivity/docx/scripts/docx_comments.py +289 -0
- package/work/vendor/hermes-agent/skills/productivity/docx/scripts/docx_common.py +94 -0
- package/work/vendor/hermes-agent/skills/productivity/docx/scripts/docx_create.py +177 -0
- package/work/vendor/hermes-agent/skills/productivity/docx/scripts/docx_edit.py +250 -0
- package/work/vendor/hermes-agent/skills/productivity/docx/scripts/docx_read.py +149 -0
- package/work/vendor/hermes-agent/skills/productivity/docx/scripts/docx_revisions.py +147 -0
- package/work/vendor/hermes-agent/skills/productivity/docx/scripts/docx_template.py +70 -0
- package/work/vendor/hermes-agent/skills/productivity/docx/scripts/docx_validate.py +156 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/LICENSE +21 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/SKILL.md +125 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/_raster.py +76 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/extract_marker.py +88 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/extract_pymupdf.py +101 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/pdf_create.py +130 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/pdf_fill_form.py +97 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/pdf_form_layout.py +168 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/pdf_make_form.py +145 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/pdf_merge.py +50 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/pdf_meta.py +115 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/pdf_page_image.py +99 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/pdf_read.py +153 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/pdf_secure.py +71 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/pdf_split.py +84 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/pdf_stamp.py +143 -0
- package/work/vendor/hermes-agent/skills/productivity/pdf/scripts/pdf_watermark.py +51 -0
- package/work/vendor/hermes-agent/skills/productivity/powerpoint/LICENSE +21 -0
- package/work/vendor/hermes-agent/skills/productivity/powerpoint/SKILL.md +220 -0
- package/work/vendor/hermes-agent/skills/productivity/powerpoint/scripts/pptx_create.py +214 -0
- package/work/vendor/hermes-agent/skills/productivity/powerpoint/scripts/pptx_edit.py +436 -0
- package/work/vendor/hermes-agent/skills/productivity/powerpoint/scripts/pptx_from_template.py +88 -0
- package/work/vendor/hermes-agent/skills/productivity/powerpoint/scripts/pptx_read.py +131 -0
- package/work/vendor/hermes-agent/skills/productivity/powerpoint/scripts/pptx_render.py +93 -0
- package/work/vendor/hermes-agent/skills/productivity/xlsx/LICENSE +21 -0
- package/work/vendor/hermes-agent/skills/productivity/xlsx/SKILL.md +196 -0
- package/work/vendor/hermes-agent/skills/productivity/xlsx/scripts/csv_to_xlsx.py +104 -0
- package/work/vendor/hermes-agent/skills/productivity/xlsx/scripts/xlsx_create.py +259 -0
- package/work/vendor/hermes-agent/skills/productivity/xlsx/scripts/xlsx_edit.py +263 -0
- package/work/vendor/hermes-agent/skills/productivity/xlsx/scripts/xlsx_read.py +160 -0
- package/work/vendor/hermes-agent/skills/productivity/xlsx/scripts/xlsx_recalc.py +111 -0
- package/work/vendor/hermes-agent/skills/productivity/xlsx/scripts/xlsx_restructure.py +337 -0
- package/work/vendor/hermes-agent/skills/productivity/xlsx/scripts/xlsx_to_csv.py +69 -0
- package/work/vendor-lock.json +11 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/** Repository glob family (0.2.5 plan 025 Task 1 split).
|
|
2
|
+
* Moved verbatim from repository.ts; public surface unchanged behind the barrel. */
|
|
3
|
+
|
|
4
|
+
import { lstat } from "node:fs/promises";
|
|
5
|
+
import { compileGlob, validateGlobPattern } from "../glob-match.js";
|
|
6
|
+
import {
|
|
7
|
+
HARD_MAX_REPO_DEPTH,
|
|
8
|
+
HARD_MAX_REPO_ENTRIES,
|
|
9
|
+
HARD_MAX_REPO_RESULTS,
|
|
10
|
+
validateCodingLimit,
|
|
11
|
+
validateCodingLimitAllowZero,
|
|
12
|
+
} from "../limits.js";
|
|
13
|
+
import { resolveRepoPath } from "./path.js";
|
|
14
|
+
import type { RepositoryGlobRequest, RepositoryGlobResult, ResolvedRepositoryLimits } from "./types.js";
|
|
15
|
+
import { RepositoryError } from "./types.js";
|
|
16
|
+
import type { RepositoryWalk } from "./walk.js";
|
|
17
|
+
|
|
18
|
+
export async function globLocal(
|
|
19
|
+
request: RepositoryGlobRequest,
|
|
20
|
+
defaults: ResolvedRepositoryLimits,
|
|
21
|
+
walk: RepositoryWalk,
|
|
22
|
+
): Promise<RepositoryGlobResult> {
|
|
23
|
+
try {
|
|
24
|
+
validateGlobPattern(request.pattern, defaults.maxPatternBytes, { braceExpansion: request.braceExpansion });
|
|
25
|
+
} catch (error) {
|
|
26
|
+
throw new RepositoryError(error instanceof Error ? error.message : String(error));
|
|
27
|
+
}
|
|
28
|
+
// Bounded brace expansion (default on): textual alternatives only, never the
|
|
29
|
+
// filesystem; bounds enforced by expandGlobBraces (max alternatives / bytes).
|
|
30
|
+
const matchesAnyPattern = compileGlob(request.pattern, request.braceExpansion !== false);
|
|
31
|
+
const resolved = await resolveRepoPath(request.root, request.path);
|
|
32
|
+
const maxResults = validateCodingLimit("maxResults", request.maxResults ?? defaults.maxResults, HARD_MAX_REPO_RESULTS);
|
|
33
|
+
const offset = validateCodingLimitAllowZero("offset", request.offset ?? 0, HARD_MAX_REPO_ENTRIES);
|
|
34
|
+
const maxDepth = validateCodingLimit("maxDepth", request.maxDepth ?? defaults.maxDepth, HARD_MAX_REPO_DEPTH);
|
|
35
|
+
const exclude = new Set(request.exclude ?? defaults.exclude);
|
|
36
|
+
const deadlineAt = request.deadlineMs !== undefined ? Date.now() + request.deadlineMs : Date.now() + defaults.maxTimeMs;
|
|
37
|
+
|
|
38
|
+
const collected: string[] = [];
|
|
39
|
+
let scannedEntries = 0;
|
|
40
|
+
let scannedFiles = 0;
|
|
41
|
+
let seen = 0;
|
|
42
|
+
let truncated = false;
|
|
43
|
+
let truncatedBy: RepositoryGlobResult["truncatedBy"] = null;
|
|
44
|
+
|
|
45
|
+
const maybeCollect = (relativePath: string): boolean => {
|
|
46
|
+
if (!matchesAnyPattern(relativePath)) return false;
|
|
47
|
+
if (seen < offset) {
|
|
48
|
+
seen++;
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
if (collected.length >= maxResults) {
|
|
52
|
+
truncated = true;
|
|
53
|
+
truncatedBy = "results";
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
collected.push(relativePath);
|
|
57
|
+
seen++;
|
|
58
|
+
return truncated;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
const startStat = await lstat(resolved.absolute);
|
|
63
|
+
if (!startStat.isDirectory()) {
|
|
64
|
+
scannedEntries = 1;
|
|
65
|
+
if (startStat.isFile()) {
|
|
66
|
+
scannedFiles = 1;
|
|
67
|
+
if (matchesAnyPattern(resolved.relative)) {
|
|
68
|
+
if (offset === 0 && maxResults > 0) collected.push(resolved.relative);
|
|
69
|
+
else if (offset === 0 && maxResults === 0) {
|
|
70
|
+
truncated = true;
|
|
71
|
+
truncatedBy = "results";
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
paths: collected,
|
|
77
|
+
truncated,
|
|
78
|
+
truncatedBy,
|
|
79
|
+
scannedEntries,
|
|
80
|
+
scannedFiles,
|
|
81
|
+
offset,
|
|
82
|
+
nextOffset: undefined,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
} catch (error) {
|
|
86
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
87
|
+
throw new RepositoryError(`cannot open path: ${message}`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
for await (const event of walk(resolved.rootReal, resolved.absolute, {
|
|
92
|
+
maxDepth,
|
|
93
|
+
maxEntries: defaults.maxEntries,
|
|
94
|
+
maxFiles: defaults.maxFiles,
|
|
95
|
+
exclude,
|
|
96
|
+
includeHidden: request.includeHidden === true,
|
|
97
|
+
signal: request.signal,
|
|
98
|
+
deadlineAt,
|
|
99
|
+
})) {
|
|
100
|
+
if (event.type === "limit") {
|
|
101
|
+
truncated = true;
|
|
102
|
+
truncatedBy = event.truncatedBy;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
scannedEntries++;
|
|
106
|
+
if (event.entry.kind === "file") scannedFiles++;
|
|
107
|
+
if (event.entry.kind !== "file") continue;
|
|
108
|
+
if (maybeCollect(event.entry.path)) break;
|
|
109
|
+
}
|
|
110
|
+
} catch (error) {
|
|
111
|
+
if (error instanceof RepositoryError && error.message === "Operation aborted") {
|
|
112
|
+
return {
|
|
113
|
+
paths: collected,
|
|
114
|
+
truncated: true,
|
|
115
|
+
truncatedBy: "abort",
|
|
116
|
+
scannedEntries,
|
|
117
|
+
scannedFiles,
|
|
118
|
+
offset,
|
|
119
|
+
nextOffset: collected.length > 0 || offset > 0 ? offset + collected.length : undefined,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (error instanceof RepositoryError && error.message === "Repository operation exceeded time limit") {
|
|
123
|
+
return {
|
|
124
|
+
paths: collected,
|
|
125
|
+
truncated: true,
|
|
126
|
+
truncatedBy: "time",
|
|
127
|
+
scannedEntries,
|
|
128
|
+
scannedFiles,
|
|
129
|
+
offset,
|
|
130
|
+
nextOffset: offset + collected.length,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
paths: collected,
|
|
138
|
+
truncated,
|
|
139
|
+
truncatedBy,
|
|
140
|
+
scannedEntries,
|
|
141
|
+
scannedFiles,
|
|
142
|
+
offset,
|
|
143
|
+
nextOffset: truncated ? offset + collected.length : undefined,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Local filesystem repository operations (default backend). */
|
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 26 Task 2: host-indexed repository search seam.
|
|
3
|
+
*
|
|
4
|
+
* `createIndexedRepositoryOperations` composes a host-owned incremental index
|
|
5
|
+
* backend with an existing literal `RepositoryOperations` fallback. `grep`
|
|
6
|
+
* stays bounded literal by default; `indexed_literal`/`semantic` modes are
|
|
7
|
+
* explicit and never fall back silently when the index is missing, stale, or
|
|
8
|
+
* failed. Index output is untrusted: every hit is containment-checked,
|
|
9
|
+
* bounded, and labeled `untrusted_index` on the result.
|
|
10
|
+
*/
|
|
11
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
12
|
+
import {
|
|
13
|
+
DEFAULT_MAX_INDEX_QUERY_TIMEOUT_MS,
|
|
14
|
+
DEFAULT_MAX_INDEX_SNIPPET_BYTES,
|
|
15
|
+
DEFAULT_MAX_INDEX_STALE_MAX_AGE_MS,
|
|
16
|
+
DEFAULT_MAX_INDEX_UPDATE_BYTES,
|
|
17
|
+
DEFAULT_MAX_INDEX_UPDATE_FILES,
|
|
18
|
+
DEFAULT_MAX_REPO_RESULTS,
|
|
19
|
+
HARD_MAX_INDEX_QUERY_TIMEOUT_MS,
|
|
20
|
+
HARD_MAX_INDEX_SNIPPET_BYTES,
|
|
21
|
+
HARD_MAX_INDEX_STALE_MAX_AGE_MS,
|
|
22
|
+
HARD_MAX_INDEX_UPDATE_BYTES,
|
|
23
|
+
HARD_MAX_INDEX_UPDATE_FILES,
|
|
24
|
+
HARD_MAX_REPO_RESULTS,
|
|
25
|
+
validateCodingLimit,
|
|
26
|
+
} from "../limits.js";
|
|
27
|
+
import { isPathInsideRoot } from "./path.js";
|
|
28
|
+
import type { RepositoryOperations, RepositorySearchMatch, RepositorySearchRequest, RepositorySearchResult } from "./types.js";
|
|
29
|
+
|
|
30
|
+
/** Frozen index state machine: empty | building | ready | stale | failed. */
|
|
31
|
+
export type IndexState = "empty" | "building" | "ready" | "stale" | "failed";
|
|
32
|
+
|
|
33
|
+
export const INDEX_STATES: readonly IndexState[] = ["empty", "building", "ready", "stale", "failed"];
|
|
34
|
+
|
|
35
|
+
export interface IndexResourceDiagnostics {
|
|
36
|
+
readonly entries: number;
|
|
37
|
+
readonly bytes: number;
|
|
38
|
+
readonly lastUpdatedAt?: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface RepositoryIndexStatus {
|
|
42
|
+
readonly state: IndexState;
|
|
43
|
+
/** Source revision the index was built from; required when `requireSourceRevision`. */
|
|
44
|
+
readonly sourceRevision?: string;
|
|
45
|
+
/** Indexed-at timestamp (epoch ms); used for the staleness age check. */
|
|
46
|
+
readonly updatedAt?: number;
|
|
47
|
+
readonly diagnostics?: IndexResourceDiagnostics;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type IndexFileChangeKind = "add" | "edit" | "delete" | "rename";
|
|
51
|
+
|
|
52
|
+
export interface IndexFileChange {
|
|
53
|
+
/** Repository-relative path (forward slashes, no leading slash, no `..`). */
|
|
54
|
+
readonly path: string;
|
|
55
|
+
readonly kind: IndexFileChangeKind;
|
|
56
|
+
/** Rename source path (repository-relative). */
|
|
57
|
+
readonly oldPath?: string;
|
|
58
|
+
readonly bytes?: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface RepositoryIndexUpdateRequest {
|
|
62
|
+
/** Optional host identity scoping (bounded; never credential-bearing). */
|
|
63
|
+
readonly repositoryId?: string;
|
|
64
|
+
readonly worktreeId?: string;
|
|
65
|
+
/** Revision the changes apply on top of. */
|
|
66
|
+
readonly sourceRevision: string;
|
|
67
|
+
readonly changes: readonly IndexFileChange[];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface RepositoryIndexRemoveRequest {
|
|
71
|
+
/** Repository-relative paths to drop from the index. */
|
|
72
|
+
readonly paths: readonly string[];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface IndexSearchHit {
|
|
76
|
+
/** Repository-relative path (validated for containment). */
|
|
77
|
+
readonly path: string;
|
|
78
|
+
/** Relevance in [0,1]; non-finite or out-of-range scores fail closed. */
|
|
79
|
+
readonly score: number;
|
|
80
|
+
/** Snippet text (bounded; treated as untrusted prompt-injection surface). */
|
|
81
|
+
readonly snippet: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface RepositoryIndexQueryRequest {
|
|
85
|
+
readonly query: string;
|
|
86
|
+
readonly mode: "indexed_literal" | "semantic";
|
|
87
|
+
/** Optional repository-relative scope filter. */
|
|
88
|
+
readonly path?: string;
|
|
89
|
+
readonly maxResults: number;
|
|
90
|
+
readonly signal?: AbortSignal;
|
|
91
|
+
readonly deadlineMs?: number;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface RepositoryIndexQueryResult {
|
|
95
|
+
readonly hits: readonly IndexSearchHit[];
|
|
96
|
+
readonly truncated: boolean;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface RepositoryIndexBackend {
|
|
100
|
+
/** Explicit capability declaration; semantic mode is never duck-typed. */
|
|
101
|
+
readonly capabilities: { readonly semantic: boolean };
|
|
102
|
+
update(request: RepositoryIndexUpdateRequest): Promise<void>;
|
|
103
|
+
remove(request: RepositoryIndexRemoveRequest): Promise<void>;
|
|
104
|
+
search(request: RepositoryIndexQueryRequest): Promise<RepositoryIndexQueryResult>;
|
|
105
|
+
status(): Promise<RepositoryIndexStatus>;
|
|
106
|
+
dispose(): Promise<void>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface ResolvedIndexLimits {
|
|
110
|
+
readonly maxUpdateFiles: number;
|
|
111
|
+
readonly maxUpdateBytes: number;
|
|
112
|
+
readonly maxResults: number;
|
|
113
|
+
readonly maxSnippetBytes: number;
|
|
114
|
+
readonly staleMaxAgeMs: number;
|
|
115
|
+
readonly queryTimeoutMs: number;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface IndexLimitOptions {
|
|
119
|
+
readonly maxUpdateFiles?: number;
|
|
120
|
+
readonly maxUpdateBytes?: number;
|
|
121
|
+
readonly maxResults?: number;
|
|
122
|
+
readonly maxSnippetBytes?: number;
|
|
123
|
+
readonly staleMaxAgeMs?: number;
|
|
124
|
+
readonly queryTimeoutMs?: number;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function resolveIndexLimits(options?: IndexLimitOptions): ResolvedIndexLimits {
|
|
128
|
+
return {
|
|
129
|
+
maxUpdateFiles: validateCodingLimit(
|
|
130
|
+
"maxUpdateFiles",
|
|
131
|
+
options?.maxUpdateFiles ?? DEFAULT_MAX_INDEX_UPDATE_FILES,
|
|
132
|
+
HARD_MAX_INDEX_UPDATE_FILES,
|
|
133
|
+
),
|
|
134
|
+
maxUpdateBytes: validateCodingLimit(
|
|
135
|
+
"maxUpdateBytes",
|
|
136
|
+
options?.maxUpdateBytes ?? DEFAULT_MAX_INDEX_UPDATE_BYTES,
|
|
137
|
+
HARD_MAX_INDEX_UPDATE_BYTES,
|
|
138
|
+
),
|
|
139
|
+
maxResults: validateCodingLimit("maxResults", options?.maxResults ?? DEFAULT_MAX_REPO_RESULTS, HARD_MAX_REPO_RESULTS),
|
|
140
|
+
maxSnippetBytes: validateCodingLimit(
|
|
141
|
+
"maxSnippetBytes",
|
|
142
|
+
options?.maxSnippetBytes ?? DEFAULT_MAX_INDEX_SNIPPET_BYTES,
|
|
143
|
+
HARD_MAX_INDEX_SNIPPET_BYTES,
|
|
144
|
+
),
|
|
145
|
+
staleMaxAgeMs: validateCodingLimit(
|
|
146
|
+
"staleMaxAgeMs",
|
|
147
|
+
options?.staleMaxAgeMs ?? DEFAULT_MAX_INDEX_STALE_MAX_AGE_MS,
|
|
148
|
+
HARD_MAX_INDEX_STALE_MAX_AGE_MS,
|
|
149
|
+
),
|
|
150
|
+
queryTimeoutMs: validateCodingLimit(
|
|
151
|
+
"queryTimeoutMs",
|
|
152
|
+
options?.queryTimeoutMs ?? DEFAULT_MAX_INDEX_QUERY_TIMEOUT_MS,
|
|
153
|
+
HARD_MAX_INDEX_QUERY_TIMEOUT_MS,
|
|
154
|
+
),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export class IndexError extends Error {
|
|
159
|
+
constructor(
|
|
160
|
+
readonly code:
|
|
161
|
+
| "ERR_PRISM_INDEX_UNSUPPORTED"
|
|
162
|
+
| "ERR_PRISM_INDEX_STALE"
|
|
163
|
+
| "ERR_PRISM_INDEX_FAILED"
|
|
164
|
+
| "ERR_PRISM_INDEX_LIMIT"
|
|
165
|
+
| "ERR_PRISM_INDEX_TIMEOUT"
|
|
166
|
+
| "ERR_PRISM_INDEX_UNTRUSTED",
|
|
167
|
+
message: string,
|
|
168
|
+
) {
|
|
169
|
+
super(message);
|
|
170
|
+
this.name = "IndexError";
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export interface IndexedRepositoryOptions {
|
|
175
|
+
/** Host-owned incremental index backend (never started or built implicitly). */
|
|
176
|
+
readonly index: RepositoryIndexBackend;
|
|
177
|
+
/** Literal fallback (e.g. createGitAwareRepositoryOperations) for mode "literal". */
|
|
178
|
+
readonly fallback: RepositoryOperations;
|
|
179
|
+
/** Modes this composite accepts; default literal only. `indexed_literal`/`semantic` must be listed explicitly. */
|
|
180
|
+
readonly allowedModes?: readonly ("literal" | "indexed_literal" | "semantic")[];
|
|
181
|
+
readonly stale?: {
|
|
182
|
+
readonly maxAgeMs?: number;
|
|
183
|
+
/** Require the index to attest a sourceRevision before serving queries. */
|
|
184
|
+
readonly requireSourceRevision?: boolean;
|
|
185
|
+
};
|
|
186
|
+
readonly limits?: IndexLimitOptions;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface IndexFacade {
|
|
190
|
+
update(request: RepositoryIndexUpdateRequest): Promise<void>;
|
|
191
|
+
remove(request: RepositoryIndexRemoveRequest): Promise<void>;
|
|
192
|
+
status(): Promise<RepositoryIndexStatus>;
|
|
193
|
+
dispose(): Promise<void>;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const MAX_IDENTITY_BYTES = 512;
|
|
197
|
+
const MAX_REVISION_BYTES = 4096;
|
|
198
|
+
|
|
199
|
+
function assertRelativeRepoPath(path: string): string {
|
|
200
|
+
if (typeof path !== "string" || path.length === 0) {
|
|
201
|
+
throw new IndexError("ERR_PRISM_INDEX_UNTRUSTED", "index reported an invalid path");
|
|
202
|
+
}
|
|
203
|
+
if (path.startsWith("/") || path.includes("\\") || isAbsolute(path) || path === ".." || path.startsWith("../") || path.includes("/../")) {
|
|
204
|
+
throw new IndexError("ERR_PRISM_INDEX_UNTRUSTED", "index reported a path outside the repository");
|
|
205
|
+
}
|
|
206
|
+
return path;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function assertBoundedIdentity(value: string | undefined, label: string): string | undefined {
|
|
210
|
+
if (value === undefined) return undefined;
|
|
211
|
+
if (typeof value !== "string" || Buffer.byteLength(value, "utf8") > MAX_IDENTITY_BYTES) {
|
|
212
|
+
throw new IndexError("ERR_PRISM_INDEX_LIMIT", `${label} exceeds the identity bound`);
|
|
213
|
+
}
|
|
214
|
+
return value;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function assertBoundedRevision(value: string): string {
|
|
218
|
+
if (typeof value !== "string" || value.length === 0 || Buffer.byteLength(value, "utf8") > MAX_REVISION_BYTES) {
|
|
219
|
+
throw new IndexError("ERR_PRISM_INDEX_LIMIT", "sourceRevision is required and bounded");
|
|
220
|
+
}
|
|
221
|
+
return value;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function truncateUtf8(value: string, maxBytes: number): string {
|
|
225
|
+
if (Buffer.byteLength(value, "utf8") <= maxBytes) return value;
|
|
226
|
+
let end = 0;
|
|
227
|
+
let bytes = 0;
|
|
228
|
+
for (const char of value) {
|
|
229
|
+
const size = Buffer.byteLength(char, "utf8");
|
|
230
|
+
if (bytes + size > maxBytes) break;
|
|
231
|
+
bytes += size;
|
|
232
|
+
end += char.length;
|
|
233
|
+
}
|
|
234
|
+
return value.slice(0, end);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function isFresh(status: RepositoryIndexStatus, staleMaxAgeMs: number, requireSourceRevision: boolean): IndexError | null {
|
|
238
|
+
switch (status.state) {
|
|
239
|
+
case "failed":
|
|
240
|
+
return new IndexError("ERR_PRISM_INDEX_FAILED", "index is failed; no search is served");
|
|
241
|
+
case "empty":
|
|
242
|
+
return new IndexError("ERR_PRISM_INDEX_STALE", "index has no data; update it before querying");
|
|
243
|
+
case "building":
|
|
244
|
+
return new IndexError("ERR_PRISM_INDEX_STALE", "index is still building");
|
|
245
|
+
case "ready":
|
|
246
|
+
case "stale":
|
|
247
|
+
break;
|
|
248
|
+
default:
|
|
249
|
+
return new IndexError("ERR_PRISM_INDEX_FAILED", "index reported an unknown state");
|
|
250
|
+
}
|
|
251
|
+
if (status.updatedAt === undefined) {
|
|
252
|
+
return new IndexError("ERR_PRISM_INDEX_STALE", "index does not attest a freshness timestamp");
|
|
253
|
+
}
|
|
254
|
+
if (Date.now() - status.updatedAt > staleMaxAgeMs) {
|
|
255
|
+
return new IndexError("ERR_PRISM_INDEX_STALE", "index is stale; refresh it before querying");
|
|
256
|
+
}
|
|
257
|
+
if (requireSourceRevision && status.sourceRevision === undefined) {
|
|
258
|
+
return new IndexError("ERR_PRISM_INDEX_STALE", "index does not attest a source revision");
|
|
259
|
+
}
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function isIndexMode(mode: string | undefined): mode is "indexed_literal" | "semantic" {
|
|
264
|
+
return mode === "indexed_literal" || mode === "semantic";
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Compose a host index with a literal fallback. Mode "literal" is routed to
|
|
269
|
+
* `fallback` unchanged; indexed modes are served only by the host backend with
|
|
270
|
+
* freshness checks, result validation, and no silent fallback.
|
|
271
|
+
*/
|
|
272
|
+
export function createIndexedRepositoryOperations(
|
|
273
|
+
cwd: string,
|
|
274
|
+
options: IndexedRepositoryOptions,
|
|
275
|
+
): RepositoryOperations & { readonly index: IndexFacade } {
|
|
276
|
+
const allowed = new Set(options.allowedModes ?? ["literal"]);
|
|
277
|
+
const resolved = resolveIndexLimits(options.limits);
|
|
278
|
+
const staleMaxAgeMs = options.stale?.maxAgeMs ?? resolved.staleMaxAgeMs;
|
|
279
|
+
const requireSourceRevision = options.stale?.requireSourceRevision === true;
|
|
280
|
+
const root = resolve(cwd);
|
|
281
|
+
|
|
282
|
+
const backend = options.index;
|
|
283
|
+
|
|
284
|
+
async function checkFresh(): Promise<RepositoryIndexStatus> {
|
|
285
|
+
let status: RepositoryIndexStatus;
|
|
286
|
+
try {
|
|
287
|
+
status = await backend.status();
|
|
288
|
+
} catch {
|
|
289
|
+
throw new IndexError("ERR_PRISM_INDEX_FAILED", "index status failed");
|
|
290
|
+
}
|
|
291
|
+
const stale = isFresh(status, staleMaxAgeMs, requireSourceRevision);
|
|
292
|
+
if (stale) throw stale;
|
|
293
|
+
return status;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function validateHit(hit: IndexSearchHit, scope: string | undefined): RepositorySearchMatch {
|
|
297
|
+
const path = assertRelativeRepoPath(hit.path);
|
|
298
|
+
const absolute = join(root, ...path.split("/"));
|
|
299
|
+
if (!isPathInsideRoot(root, absolute)) {
|
|
300
|
+
throw new IndexError("ERR_PRISM_INDEX_UNTRUSTED", "index reported a path outside the repository root");
|
|
301
|
+
}
|
|
302
|
+
if (scope !== undefined && scope !== "." && path !== scope && !path.startsWith(`${scope}/`)) {
|
|
303
|
+
throw new IndexError("ERR_PRISM_INDEX_UNTRUSTED", "index reported a result outside the requested path scope");
|
|
304
|
+
}
|
|
305
|
+
if (typeof hit.score !== "number" || !Number.isFinite(hit.score) || hit.score < 0 || hit.score > 1) {
|
|
306
|
+
throw new IndexError("ERR_PRISM_INDEX_UNTRUSTED", "index reported an invalid score");
|
|
307
|
+
}
|
|
308
|
+
const snippet = typeof hit.snippet === "string" ? truncateUtf8(hit.snippet, resolved.maxSnippetBytes) : "";
|
|
309
|
+
return {
|
|
310
|
+
path,
|
|
311
|
+
line: 0,
|
|
312
|
+
column: 0,
|
|
313
|
+
text: snippet,
|
|
314
|
+
before: [],
|
|
315
|
+
after: [],
|
|
316
|
+
score: hit.score,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
async search(request: RepositorySearchRequest): Promise<RepositorySearchResult> {
|
|
322
|
+
const mode = request.mode ?? "literal";
|
|
323
|
+
if (!isIndexMode(mode)) return options.fallback.search(request);
|
|
324
|
+
|
|
325
|
+
if (!allowed.has(mode)) {
|
|
326
|
+
throw new IndexError("ERR_PRISM_INDEX_UNSUPPORTED", `search mode ${mode} is not enabled on this composite`);
|
|
327
|
+
}
|
|
328
|
+
if (mode === "semantic" && backend.capabilities.semantic !== true) {
|
|
329
|
+
throw new IndexError("ERR_PRISM_INDEX_UNSUPPORTED", "semantic search is not supported by this index backend");
|
|
330
|
+
}
|
|
331
|
+
const status = await checkFresh();
|
|
332
|
+
|
|
333
|
+
let scope: string | undefined;
|
|
334
|
+
if (request.path !== undefined && request.path !== "" && request.path !== ".") {
|
|
335
|
+
scope = assertRelativeRepoPath(request.path);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const timeoutMs = Math.min(request.deadlineMs ?? resolved.queryTimeoutMs, resolved.queryTimeoutMs);
|
|
339
|
+
const deadlineAt = Date.now() + timeoutMs;
|
|
340
|
+
const maxResults = Math.min(
|
|
341
|
+
resolved.maxResults,
|
|
342
|
+
validateCodingLimit("maxMatches", request.maxMatches ?? resolved.maxResults, HARD_MAX_REPO_RESULTS),
|
|
343
|
+
);
|
|
344
|
+
let queryResult: RepositoryIndexQueryResult;
|
|
345
|
+
try {
|
|
346
|
+
queryResult = await Promise.race([
|
|
347
|
+
backend.search({
|
|
348
|
+
query: request.query,
|
|
349
|
+
mode,
|
|
350
|
+
path: scope,
|
|
351
|
+
maxResults,
|
|
352
|
+
signal: request.signal,
|
|
353
|
+
deadlineMs: timeoutMs,
|
|
354
|
+
}),
|
|
355
|
+
new Promise<never>((_, reject) => {
|
|
356
|
+
const timer = setTimeout(() => reject(new IndexError("ERR_PRISM_INDEX_TIMEOUT", "index query timed out")), timeoutMs + 5);
|
|
357
|
+
timer.unref();
|
|
358
|
+
}),
|
|
359
|
+
]);
|
|
360
|
+
} catch (error) {
|
|
361
|
+
if (error instanceof IndexError) throw error;
|
|
362
|
+
if (request.signal?.aborted) throw new IndexError("ERR_PRISM_INDEX_TIMEOUT", "index query aborted");
|
|
363
|
+
throw new IndexError("ERR_PRISM_INDEX_FAILED", "index query failed");
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (Date.now() > deadlineAt) {
|
|
367
|
+
throw new IndexError("ERR_PRISM_INDEX_TIMEOUT", "index query exceeded the deadline");
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const seen = new Set<string>();
|
|
371
|
+
const matches: RepositorySearchMatch[] = [];
|
|
372
|
+
for (const hit of queryResult.hits) {
|
|
373
|
+
if (matches.length >= maxResults) break;
|
|
374
|
+
const match = validateHit(hit, scope);
|
|
375
|
+
if (seen.has(match.path)) continue; // duplicate paths: keep first
|
|
376
|
+
seen.add(match.path);
|
|
377
|
+
matches.push(match);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
return {
|
|
381
|
+
matches,
|
|
382
|
+
truncated: queryResult.truncated || queryResult.hits.length > matches.length,
|
|
383
|
+
truncatedBy: queryResult.truncated || queryResult.hits.length > matches.length ? "index" : null,
|
|
384
|
+
scannedBytes: 0,
|
|
385
|
+
scannedFiles: 0,
|
|
386
|
+
scannedEntries: 0,
|
|
387
|
+
filesSkippedBinary: 0,
|
|
388
|
+
filesSkippedOversize: 0,
|
|
389
|
+
indexed: {
|
|
390
|
+
mode,
|
|
391
|
+
state: status.state,
|
|
392
|
+
sourceRevision: status.sourceRevision,
|
|
393
|
+
updatedAt: status.updatedAt,
|
|
394
|
+
},
|
|
395
|
+
untrusted_index: true,
|
|
396
|
+
};
|
|
397
|
+
},
|
|
398
|
+
|
|
399
|
+
list: (request) => options.fallback.list(request),
|
|
400
|
+
glob: (request) => options.fallback.glob(request),
|
|
401
|
+
|
|
402
|
+
index: {
|
|
403
|
+
async update(request: RepositoryIndexUpdateRequest): Promise<void> {
|
|
404
|
+
assertBoundedRevision(request.sourceRevision);
|
|
405
|
+
assertBoundedIdentity(request.repositoryId, "repositoryId");
|
|
406
|
+
assertBoundedIdentity(request.worktreeId, "worktreeId");
|
|
407
|
+
if (!Array.isArray(request.changes) || request.changes.length > resolved.maxUpdateFiles) {
|
|
408
|
+
throw new IndexError("ERR_PRISM_INDEX_LIMIT", `update exceeds the ${resolved.maxUpdateFiles} change cap`);
|
|
409
|
+
}
|
|
410
|
+
let totalBytes = Buffer.byteLength(request.sourceRevision, "utf8");
|
|
411
|
+
const updates: IndexFileChange[] = [];
|
|
412
|
+
const removals: string[] = [];
|
|
413
|
+
for (const change of request.changes) {
|
|
414
|
+
assertRelativeRepoPath(change.path);
|
|
415
|
+
if (change.oldPath !== undefined) assertRelativeRepoPath(change.oldPath);
|
|
416
|
+
if (change.bytes !== undefined && (typeof change.bytes !== "number" || !Number.isFinite(change.bytes) || change.bytes < 0)) {
|
|
417
|
+
throw new IndexError("ERR_PRISM_INDEX_LIMIT", "change bytes must be a non-negative finite number");
|
|
418
|
+
}
|
|
419
|
+
totalBytes += Buffer.byteLength(change.path, "utf8") + (change.oldPath ? Buffer.byteLength(change.oldPath, "utf8") : 0);
|
|
420
|
+
if (totalBytes > resolved.maxUpdateBytes) {
|
|
421
|
+
throw new IndexError("ERR_PRISM_INDEX_LIMIT", `update exceeds the ${resolved.maxUpdateBytes} byte cap`);
|
|
422
|
+
}
|
|
423
|
+
if (change.kind === "delete") {
|
|
424
|
+
removals.push(change.path);
|
|
425
|
+
} else if (change.kind === "rename") {
|
|
426
|
+
removals.push(change.oldPath!);
|
|
427
|
+
updates.push({ path: change.path, kind: "add", bytes: change.bytes });
|
|
428
|
+
} else {
|
|
429
|
+
updates.push(change);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
try {
|
|
433
|
+
if (updates.length > 0)
|
|
434
|
+
await backend.update({
|
|
435
|
+
repositoryId: request.repositoryId,
|
|
436
|
+
worktreeId: request.worktreeId,
|
|
437
|
+
sourceRevision: request.sourceRevision,
|
|
438
|
+
changes: updates,
|
|
439
|
+
});
|
|
440
|
+
if (removals.length > 0) await backend.remove({ paths: removals });
|
|
441
|
+
} catch {
|
|
442
|
+
throw new IndexError("ERR_PRISM_INDEX_FAILED", "index update failed");
|
|
443
|
+
}
|
|
444
|
+
},
|
|
445
|
+
|
|
446
|
+
async remove(request: RepositoryIndexRemoveRequest): Promise<void> {
|
|
447
|
+
if (!Array.isArray(request.paths) || request.paths.length > resolved.maxUpdateFiles) {
|
|
448
|
+
throw new IndexError("ERR_PRISM_INDEX_LIMIT", `remove exceeds the ${resolved.maxUpdateFiles} path cap`);
|
|
449
|
+
}
|
|
450
|
+
const paths = request.paths.map((p) => assertRelativeRepoPath(p));
|
|
451
|
+
try {
|
|
452
|
+
await backend.remove({ paths });
|
|
453
|
+
} catch {
|
|
454
|
+
throw new IndexError("ERR_PRISM_INDEX_FAILED", "index remove failed");
|
|
455
|
+
}
|
|
456
|
+
},
|
|
457
|
+
|
|
458
|
+
async status(): Promise<RepositoryIndexStatus> {
|
|
459
|
+
let status: RepositoryIndexStatus;
|
|
460
|
+
try {
|
|
461
|
+
status = await backend.status();
|
|
462
|
+
} catch {
|
|
463
|
+
throw new IndexError("ERR_PRISM_INDEX_FAILED", "index status failed");
|
|
464
|
+
}
|
|
465
|
+
if (!INDEX_STATES.includes(status.state)) {
|
|
466
|
+
throw new IndexError("ERR_PRISM_INDEX_FAILED", "index reported an unknown state");
|
|
467
|
+
}
|
|
468
|
+
return status;
|
|
469
|
+
},
|
|
470
|
+
|
|
471
|
+
async dispose(): Promise<void> {
|
|
472
|
+
try {
|
|
473
|
+
await backend.dispose();
|
|
474
|
+
} catch {
|
|
475
|
+
throw new IndexError("ERR_PRISM_INDEX_FAILED", "index dispose failed");
|
|
476
|
+
}
|
|
477
|
+
},
|
|
478
|
+
},
|
|
479
|
+
};
|
|
480
|
+
}
|