@massa-ai/core 1.17.0 → 1.19.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/data/memory/observation-contract.d.ts +14 -2
- package/dist/data/memory/observation-contract.d.ts.map +1 -1
- package/dist/data/memory/observation-contract.js.map +1 -1
- package/dist/data/memory/observation-repository-pg.d.ts +2 -2
- package/dist/data/memory/observation-repository-pg.d.ts.map +1 -1
- package/dist/data/memory/observation-repository-pg.js.map +1 -1
- package/dist/kernel/sanitize/credential-scrub.d.ts +63 -0
- package/dist/kernel/sanitize/credential-scrub.d.ts.map +1 -0
- package/dist/kernel/sanitize/credential-scrub.js +119 -0
- package/dist/kernel/sanitize/credential-scrub.js.map +1 -0
- package/dist/services/cache/lru-evict.d.ts +63 -0
- package/dist/services/cache/lru-evict.d.ts.map +1 -0
- package/dist/services/cache/lru-evict.js +76 -0
- package/dist/services/cache/lru-evict.js.map +1 -0
- package/dist/services/file-read/file-content-cache.d.ts +41 -0
- package/dist/services/file-read/file-content-cache.d.ts.map +1 -0
- package/dist/services/file-read/file-content-cache.js +109 -0
- package/dist/services/file-read/file-content-cache.js.map +1 -0
- package/dist/services/file-read/file-metadata.d.ts +23 -0
- package/dist/services/file-read/file-metadata.d.ts.map +1 -0
- package/dist/services/file-read/file-metadata.js +127 -0
- package/dist/services/file-read/file-metadata.js.map +1 -0
- package/dist/services/file-read/line-range.d.ts +91 -0
- package/dist/services/file-read/line-range.d.ts.map +1 -0
- package/dist/services/file-read/line-range.js +146 -0
- package/dist/services/file-read/line-range.js.map +1 -0
- package/dist/services/file-read/path-containment.d.ts +49 -0
- package/dist/services/file-read/path-containment.d.ts.map +1 -0
- package/dist/services/file-read/path-containment.js +128 -0
- package/dist/services/file-read/path-containment.js.map +1 -0
- package/dist/services/file-read/project-root-cache.d.ts +21 -0
- package/dist/services/file-read/project-root-cache.d.ts.map +1 -0
- package/dist/services/file-read/project-root-cache.js +78 -0
- package/dist/services/file-read/project-root-cache.js.map +1 -0
- package/dist/services/file-read/read-file.service.d.ts +68 -0
- package/dist/services/file-read/read-file.service.d.ts.map +1 -0
- package/dist/services/file-read/read-file.service.js +222 -0
- package/dist/services/file-read/read-file.service.js.map +1 -0
- package/dist/services/hooks/co-retrieval-hook.js +1 -1
- package/dist/services/hooks/co-retrieval-hook.js.map +1 -1
- package/dist/services/hooks/hook-service.d.ts.map +1 -1
- package/dist/services/hooks/hook-service.js +14 -1
- package/dist/services/hooks/hook-service.js.map +1 -1
- package/dist/services/index.d.ts +5 -5
- package/dist/services/index.d.ts.map +1 -1
- package/dist/services/index.js +4 -4
- package/dist/services/index.js.map +1 -1
- package/dist/services/indexing/acquire-indexing-lease.d.ts +34 -0
- package/dist/services/indexing/acquire-indexing-lease.d.ts.map +1 -0
- package/dist/services/indexing/acquire-indexing-lease.js +101 -0
- package/dist/services/indexing/acquire-indexing-lease.js.map +1 -0
- package/dist/services/indexing/execute-indexing.d.ts +33 -0
- package/dist/services/indexing/execute-indexing.d.ts.map +1 -0
- package/dist/services/indexing/execute-indexing.js +136 -0
- package/dist/services/indexing/execute-indexing.js.map +1 -0
- package/dist/services/jobs/memory-consolidation-job.js +1 -1
- package/dist/services/jobs/memory-consolidation-job.js.map +1 -1
- package/dist/services/memory/memory-controller.js +1 -1
- package/dist/services/memory/memory-controller.js.map +1 -1
- package/dist/services/memory-graph/graph-queries.d.ts.map +1 -0
- package/dist/services/memory-graph/graph-queries.js.map +1 -0
- package/dist/services/memory-graph/graph-store-factory.d.ts.map +1 -0
- package/dist/services/memory-graph/graph-store-factory.js.map +1 -0
- package/dist/services/memory-graph/graph-store-pg.d.ts.map +1 -0
- package/dist/services/memory-graph/graph-store-pg.js.map +1 -0
- package/dist/services/memory-graph/index.d.ts.map +1 -0
- package/dist/services/memory-graph/index.js.map +1 -0
- package/dist/services/memory-graph/memory-graph.service.d.ts.map +1 -0
- package/dist/services/memory-graph/memory-graph.service.js.map +1 -0
- package/dist/services/memory-graph/relation-extractor.d.ts.map +1 -0
- package/dist/services/memory-graph/relation-extractor.js.map +1 -0
- package/dist/services/memory-graph/types.d.ts.map +1 -0
- package/dist/services/memory-graph/types.js.map +1 -0
- package/dist/services/project-identity/invalidator-registry.d.ts +9 -3
- package/dist/services/project-identity/invalidator-registry.d.ts.map +1 -1
- package/dist/services/project-identity/invalidator-registry.js +9 -3
- package/dist/services/project-identity/invalidator-registry.js.map +1 -1
- package/dist/services/project-identity/production-wiring.d.ts +11 -2
- package/dist/services/project-identity/production-wiring.d.ts.map +1 -1
- package/dist/services/project-identity/production-wiring.js +11 -2
- package/dist/services/project-identity/production-wiring.js.map +1 -1
- package/dist/services/project-identity/project-root-identity.d.ts +11 -0
- package/dist/services/project-identity/project-root-identity.d.ts.map +1 -0
- package/dist/services/project-identity/project-root-identity.js +51 -0
- package/dist/services/project-identity/project-root-identity.js.map +1 -0
- package/dist/services/search/file-filter-cache.d.ts +30 -1
- package/dist/services/search/file-filter-cache.d.ts.map +1 -1
- package/dist/services/search/file-filter-cache.js +32 -13
- package/dist/services/search/file-filter-cache.js.map +1 -1
- package/dist/services/search/graph-stream.js +1 -1
- package/dist/services/search/graph-stream.js.map +1 -1
- package/dist/services/symbol/symbol-graph.service.d.ts +12 -1
- package/dist/services/symbol/symbol-graph.service.d.ts.map +1 -1
- package/dist/services/symbol/symbol-graph.service.js +14 -7
- package/dist/services/symbol/symbol-graph.service.js.map +1 -1
- package/dist/services/web/web-controller.d.ts.map +1 -1
- package/dist/services/web/web-controller.js +6 -7
- package/dist/services/web/web-controller.js.map +1 -1
- package/dist/tools/batch_execute.d.ts +0 -3
- package/dist/tools/batch_execute.d.ts.map +1 -1
- package/dist/tools/batch_execute.js.map +1 -1
- package/dist/tools/compact_snapshot.d.ts.map +1 -1
- package/dist/tools/compact_snapshot.js +18 -7
- package/dist/tools/compact_snapshot.js.map +1 -1
- package/dist/tools/index.d.ts +1 -1
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index_project.d.ts +0 -19
- package/dist/tools/index_project.d.ts.map +1 -1
- package/dist/tools/index_project.js +38 -142
- package/dist/tools/index_project.js.map +1 -1
- package/dist/tools/read_file.d.ts +9 -72
- package/dist/tools/read_file.d.ts.map +1 -1
- package/dist/tools/read_file.js +22 -487
- package/dist/tools/read_file.js.map +1 -1
- package/package.json +2 -2
- package/dist/data/vector/hybrid-search.d.ts +0 -71
- package/dist/data/vector/hybrid-search.d.ts.map +0 -1
- package/dist/data/vector/hybrid-search.js +0 -243
- package/dist/data/vector/hybrid-search.js.map +0 -1
- package/dist/data/vector/index.d.ts +0 -7
- package/dist/data/vector/index.d.ts.map +0 -1
- package/dist/data/vector/index.js +0 -6
- package/dist/data/vector/index.js.map +0 -1
- package/dist/services/graph/graph-queries.d.ts.map +0 -1
- package/dist/services/graph/graph-queries.js.map +0 -1
- package/dist/services/graph/graph-store-factory.d.ts.map +0 -1
- package/dist/services/graph/graph-store-factory.js.map +0 -1
- package/dist/services/graph/graph-store-pg.d.ts.map +0 -1
- package/dist/services/graph/graph-store-pg.js.map +0 -1
- package/dist/services/graph/index.d.ts.map +0 -1
- package/dist/services/graph/index.js.map +0 -1
- package/dist/services/graph/memory-graph.service.d.ts.map +0 -1
- package/dist/services/graph/memory-graph.service.js.map +0 -1
- package/dist/services/graph/relation-extractor.d.ts.map +0 -1
- package/dist/services/graph/relation-extractor.js.map +0 -1
- package/dist/services/graph/types.d.ts.map +0 -1
- package/dist/services/graph/types.js.map +0 -1
- /package/dist/services/{graph → memory-graph}/graph-queries.d.ts +0 -0
- /package/dist/services/{graph → memory-graph}/graph-queries.js +0 -0
- /package/dist/services/{graph → memory-graph}/graph-store-factory.d.ts +0 -0
- /package/dist/services/{graph → memory-graph}/graph-store-factory.js +0 -0
- /package/dist/services/{graph → memory-graph}/graph-store-pg.d.ts +0 -0
- /package/dist/services/{graph → memory-graph}/graph-store-pg.js +0 -0
- /package/dist/services/{graph → memory-graph}/index.d.ts +0 -0
- /package/dist/services/{graph → memory-graph}/index.js +0 -0
- /package/dist/services/{graph → memory-graph}/memory-graph.service.d.ts +0 -0
- /package/dist/services/{graph → memory-graph}/memory-graph.service.js +0 -0
- /package/dist/services/{graph → memory-graph}/relation-extractor.d.ts +0 -0
- /package/dist/services/{graph → memory-graph}/relation-extractor.js +0 -0
- /package/dist/services/{graph → memory-graph}/types.d.ts +0 -0
- /package/dist/services/{graph → memory-graph}/types.js +0 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { SymbolGraphService } from "../symbol/symbol-graph.service.js";
|
|
2
|
+
export interface FileMetadata {
|
|
3
|
+
totalLines: number;
|
|
4
|
+
language?: string;
|
|
5
|
+
symbols?: {
|
|
6
|
+
definitions: number;
|
|
7
|
+
references: number;
|
|
8
|
+
};
|
|
9
|
+
imports?: string[];
|
|
10
|
+
}
|
|
11
|
+
export declare class FileMetadataExtractor {
|
|
12
|
+
private readonly symbolGraph?;
|
|
13
|
+
constructor(symbolGraph?: SymbolGraphService | undefined);
|
|
14
|
+
extractMetadata(content: string, filePath: string, options: {
|
|
15
|
+
includeSymbols: boolean;
|
|
16
|
+
includeImports: boolean;
|
|
17
|
+
projectId?: string;
|
|
18
|
+
relativePath?: string;
|
|
19
|
+
}): Promise<FileMetadata>;
|
|
20
|
+
private detectLanguage;
|
|
21
|
+
private extractImports;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=file-metadata.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-metadata.d.ts","sourceRoot":"","sources":["../../../src/services/file-read/file-metadata.ts"],"names":[],"mappings":"AA+BA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AAE5E,MAAM,WAAW,YAAY;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE;QACR,WAAW,EAAE,MAAM,CAAC;QACpB,UAAU,EAAE,MAAM,CAAC;KACpB,CAAC;IACF,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,qBAAa,qBAAqB;IACpB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC;gBAAZ,WAAW,CAAC,EAAE,kBAAkB,YAAA;IAEvD,eAAe,CACnB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE;QACP,cAAc,EAAE,OAAO,CAAC;QACxB,cAAc,EAAE,OAAO,CAAC;QACxB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB,GACA,OAAO,CAAC,YAAY,CAAC;IAuCxB,OAAO,CAAC,cAAc;IAsCtB,OAAO,CAAC,cAAc;CAwBvB"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @massa-ai/core - File metadata extraction
|
|
3
|
+
*
|
|
4
|
+
* Module 5 of the `tools/read_file.ts` extraction (PR-D, T10). Owns the three
|
|
5
|
+
* private methods `ReadFileTool` used to carry for describing a file's content:
|
|
6
|
+
* `extractMetadata` (the composer), `detectLanguage` (extension → language) and
|
|
7
|
+
* `extractImports` (per-language import scan), together with the `FileMetadata`
|
|
8
|
+
* shape they produce.
|
|
9
|
+
*
|
|
10
|
+
* ALL THREE BODIES ARE MOVED VERBATIM, and so is `interface FileMetadata`.
|
|
11
|
+
*
|
|
12
|
+
* IT NAMES SymbolGraphService AND MODULE 4 DOES NOT, WHICH IS THE WHOLE POINT
|
|
13
|
+
* OF THE 4 → 5 EDGE. `design.md` §5.1 records that `readFileWithCache` calls
|
|
14
|
+
* `extractMetadata` on two of three paths and that `extractMetadata` reaches
|
|
15
|
+
* `symbolGraph.listDefinitions`, so a direct edge would make the file CONTENT
|
|
16
|
+
* cache thread a database-backed collaborator through purely to satisfy this
|
|
17
|
+
* module. `tasks.md` §4.1 decided the mechanism — module 4 takes a
|
|
18
|
+
* `(content, filePath, options) => Promise<FileMetadata>` callback — and
|
|
19
|
+
* rejected inverting the call order on measurement rather than on taste: module
|
|
20
|
+
* 7 pre-computing metadata is eager on all three paths, so every cache HIT
|
|
21
|
+
* would newly run a `listDefinitions` query. That is a behavior change inside a
|
|
22
|
+
* behavior-preserving PR, and the callback keeps the laziness and the
|
|
23
|
+
* legacy-entry write-back exactly where they were.
|
|
24
|
+
*
|
|
25
|
+
* INSTANTIATED PER ReadFileTool, on module 3's precedent. `symbolGraph` is the
|
|
26
|
+
* tool's own optional constructor parameter, forwarded unchanged; a module
|
|
27
|
+
* singleton would have to pick one graph for every tool ever constructed, and
|
|
28
|
+
* the parameter is public surface (`design.md` §3.2, 41 construction sites).
|
|
29
|
+
*/
|
|
30
|
+
import { logger } from "@massa-ai/shared";
|
|
31
|
+
import path from "path";
|
|
32
|
+
export class FileMetadataExtractor {
|
|
33
|
+
symbolGraph;
|
|
34
|
+
constructor(symbolGraph) {
|
|
35
|
+
this.symbolGraph = symbolGraph;
|
|
36
|
+
}
|
|
37
|
+
async extractMetadata(content, filePath, options) {
|
|
38
|
+
const lines = content.split("\n");
|
|
39
|
+
const language = this.detectLanguage(filePath);
|
|
40
|
+
const metadata = {
|
|
41
|
+
totalLines: lines.length,
|
|
42
|
+
language,
|
|
43
|
+
};
|
|
44
|
+
// Extract imports if requested
|
|
45
|
+
if (options.includeImports && language) {
|
|
46
|
+
metadata.imports = this.extractImports(lines, language);
|
|
47
|
+
}
|
|
48
|
+
// Get symbol metadata if symbol graph available
|
|
49
|
+
if (options.includeSymbols && this.symbolGraph && options.projectId) {
|
|
50
|
+
try {
|
|
51
|
+
// Symbol DB stores relative paths — use original relative path for queries
|
|
52
|
+
const queryPath = options.relativePath || filePath;
|
|
53
|
+
const { definitions } = await this.symbolGraph.listDefinitions(options.projectId, {
|
|
54
|
+
file: queryPath,
|
|
55
|
+
limit: 100,
|
|
56
|
+
});
|
|
57
|
+
metadata.symbols = {
|
|
58
|
+
definitions: definitions.length,
|
|
59
|
+
references: 0, // Would need separate query
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
logger.debug("Failed to get symbol metadata", { filePath, error });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return metadata;
|
|
67
|
+
}
|
|
68
|
+
detectLanguage(filePath) {
|
|
69
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
70
|
+
const languageMap = {
|
|
71
|
+
".ts": "TypeScript",
|
|
72
|
+
".tsx": "TypeScript",
|
|
73
|
+
".js": "JavaScript",
|
|
74
|
+
".jsx": "JavaScript",
|
|
75
|
+
".vue": "Vue",
|
|
76
|
+
".py": "Python",
|
|
77
|
+
".go": "Go",
|
|
78
|
+
".rs": "Rust",
|
|
79
|
+
".java": "Java",
|
|
80
|
+
".cpp": "C++",
|
|
81
|
+
".c": "C",
|
|
82
|
+
".h": "C",
|
|
83
|
+
".hpp": "C++",
|
|
84
|
+
".cs": "C#",
|
|
85
|
+
".rb": "Ruby",
|
|
86
|
+
".php": "PHP",
|
|
87
|
+
".swift": "Swift",
|
|
88
|
+
".kt": "Kotlin",
|
|
89
|
+
".kts": "Kotlin",
|
|
90
|
+
".scala": "Scala",
|
|
91
|
+
".md": "Markdown",
|
|
92
|
+
".json": "JSON",
|
|
93
|
+
".yaml": "YAML",
|
|
94
|
+
".yml": "YAML",
|
|
95
|
+
".xml": "XML",
|
|
96
|
+
".html": "HTML",
|
|
97
|
+
".css": "CSS",
|
|
98
|
+
".scss": "SCSS",
|
|
99
|
+
".sql": "SQL",
|
|
100
|
+
".sh": "Shell",
|
|
101
|
+
".bash": "Shell",
|
|
102
|
+
};
|
|
103
|
+
return languageMap[ext];
|
|
104
|
+
}
|
|
105
|
+
extractImports(lines, language) {
|
|
106
|
+
const imports = [];
|
|
107
|
+
const importPatterns = {
|
|
108
|
+
TypeScript: /^(import\s+.*?from\s+['"]|import\s+['"])/,
|
|
109
|
+
JavaScript: /^(import\s+.*?from\s+['"]|import\s+['"]|require\s*\(\s*['"])/,
|
|
110
|
+
Python: /^(import\s+|from\s+\S+\s+import)/,
|
|
111
|
+
Go: /^import\s+/,
|
|
112
|
+
Java: /^import\s+/,
|
|
113
|
+
Rust: /^use\s+/,
|
|
114
|
+
};
|
|
115
|
+
const pattern = importPatterns[language];
|
|
116
|
+
if (!pattern)
|
|
117
|
+
return imports;
|
|
118
|
+
for (const line of lines) {
|
|
119
|
+
const trimmed = line.trim();
|
|
120
|
+
if (pattern.test(trimmed)) {
|
|
121
|
+
imports.push(trimmed);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return imports;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=file-metadata.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-metadata.js","sourceRoot":"","sources":["../../../src/services/file-read/file-metadata.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,IAAI,MAAM,MAAM,CAAC;AAaxB,MAAM,OAAO,qBAAqB;IACH;IAA7B,YAA6B,WAAgC;QAAhC,gBAAW,GAAX,WAAW,CAAqB;IAAG,CAAC;IAEjE,KAAK,CAAC,eAAe,CACnB,OAAe,EACf,QAAgB,EAChB,OAKC;QAED,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAClC,MAAM,QAAQ,GAAG,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QAE/C,MAAM,QAAQ,GAAiB;YAC7B,UAAU,EAAE,KAAK,CAAC,MAAM;YACxB,QAAQ;SACT,CAAC;QAEF,+BAA+B;QAC/B,IAAI,OAAO,CAAC,cAAc,IAAI,QAAQ,EAAE,CAAC;YACvC,QAAQ,CAAC,OAAO,GAAG,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QAC1D,CAAC;QAED,gDAAgD;QAChD,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M,EAAE,QAAQ;YAChB,QAAQ,EAAE,OAAO;YACjB,KAAK,EAAE,UAAU;YACjB,OAAO,EAAE,MAAM;YACf,OAAO,EAAE,MAAM;YACf,MAAM,EAAE,MAAM;YACd,MAAM,EAAE,KAAK;YACb,OAAO,EAAE,MAAM;YACf,MAAM,EAAE,KAAK;YACb,OAAO,EAAE,MAAM;YACf,MAAM,EAAE,KAAK;YACb,KAAK,EAAE,OAAO;YACd,OAAO,EAAE,OAAO;SACjB,CAAC;QACF,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAEO,cAAc,CAAC,KAAe,EAAE,QAAgB;QACtD,MAAM,OAAO,GAAa,EAAE,CAAC;QAE7B,MAAM,cAAc,GAA2B;YAC7C,UAAU,EAAE,0CAA0C;YACtD,UAAU,EAAE,8DAA8D;YAC1E,MAAM,EAAE,kCAAkC;YAC1C,EAAE,EAAE,YAAY;YAChB,IAAI,EAAE,YAAY;YAClB,IAAI,EAAE,SAAS;SAChB,CAAC;QAEF,MAAM,OAAO,GAAG,cAAc,CAAC,QAAQ,CAAC,CAAC;QACzC,IAAI,CAAC,OAAO;YAAE,OAAO,OAAO,CAAC;QAE7B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;YAC5B,IAAI,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC1B,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACxB,CAAC;QACH,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @massa-ai/core - Line range selection
|
|
3
|
+
*
|
|
4
|
+
* Module 6 of the `tools/read_file.ts` extraction (PR-D, T11). Owns everything
|
|
5
|
+
* that turns a request into the slice of a file the caller actually receives:
|
|
6
|
+
* `calculateRange` (request → range), `adjustRange` (range → range clamped to
|
|
7
|
+
* the file), `extractLines` (range → line-numbered text), the `ReadRange` shape
|
|
8
|
+
* they pass around, and the N9 output cap — the `MASSA_AI_READ_FILE_MAX_LINES`
|
|
9
|
+
* ceiling together with the clipping block that used to sit inside `handle()`.
|
|
10
|
+
*
|
|
11
|
+
* FREE FUNCTIONS, NOT A CLASS, and that is a decision rather than a default.
|
|
12
|
+
* Modules 2-5 are classes because each owns per-tool state — a Map, a
|
|
13
|
+
* subscription, an injected collaborator. This module owns none: every function
|
|
14
|
+
* here is pure, and the one piece of state is a process-wide constant read from
|
|
15
|
+
* the environment. `services/cache/lru-evict.ts` (module 1) is a bare function
|
|
16
|
+
* for exactly this reason (`design.md` §5.2). A class would also have to be
|
|
17
|
+
* constructed in `ReadFileTool`'s constructor, which is the one surface C64 is
|
|
18
|
+
* about, and would falsely imply per-instance state.
|
|
19
|
+
*
|
|
20
|
+
* IT IMPORTS NOTHING AT ALL, which is `lru-evict.ts`'s property and is asserted
|
|
21
|
+
* by this module's own unit test rather than by any tier rule.
|
|
22
|
+
*
|
|
23
|
+
* `LineRangeRequest` EXISTS BECAUSE `ReadFileParams` IS MODULE 7's. `design.md`
|
|
24
|
+
* §5.1 assigns `interface ReadFileParams` to `read-file.service.ts`, which T12
|
|
25
|
+
* built and which COMPOSES this module — so importing it here would be a 6 → 7
|
|
26
|
+
* edge against the composition direction. It exists now and the rule is
|
|
27
|
+
* unchanged. `calculateRange` reads exactly four fields off its argument, so
|
|
28
|
+
* this module declares those four and `ReadFileParams` satisfies it
|
|
29
|
+
* structurally. The alternative Design's module table hides is that module 6
|
|
30
|
+
* silently depends on module 7's type; naming the subset is what keeps 6
|
|
31
|
+
* standalone. Same shape as the 4 → 5 edge §4.1 decided, which Design handed to
|
|
32
|
+
* Tasks explicitly — this one it handed to nobody.
|
|
33
|
+
*
|
|
34
|
+
* THE CLIPPED PATH RETURNS UNNUMBERED TEXT, AND THAT IS PRESERVED DELIBERATELY.
|
|
35
|
+
* `extractLines` prefixes every line with a padded line number; the cap does NOT
|
|
36
|
+
* slice that string — it re-slices the RAW `lines` array and joins it, so a
|
|
37
|
+
* clipped response loses the prefixes an unclipped one carries. Found by the
|
|
38
|
+
* Plan Challenge gate's red-team lens before this file was written, and
|
|
39
|
+
* confirmed by the evidence audit: no test anywhere asserts content on the
|
|
40
|
+
* clipped path, so composing this as "extract, then slice the extracted text"
|
|
41
|
+
* would have been a silent behavior change inside a behavior-preserving PR.
|
|
42
|
+
* `__tests__/line-range.test.ts` pins both branches' exact format. Whether the
|
|
43
|
+
* asymmetry is intentional is not PR-D's question — it is logged, not fixed
|
|
44
|
+
* (`spec.md` §6, R-07's precedent).
|
|
45
|
+
*/
|
|
46
|
+
/** An inclusive, 1-indexed line range. `end` may be `Infinity` before adjustment. */
|
|
47
|
+
export interface ReadRange {
|
|
48
|
+
start: number;
|
|
49
|
+
end: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The four fields `calculateRange` reads. `ReadFileParams` (module 7, T12)
|
|
53
|
+
* satisfies this structurally; declaring the subset is what stops module 6
|
|
54
|
+
* depending on the module that composes it.
|
|
55
|
+
*/
|
|
56
|
+
export interface LineRangeRequest {
|
|
57
|
+
offset?: number;
|
|
58
|
+
limit?: number;
|
|
59
|
+
lineStart?: number;
|
|
60
|
+
lineEnd?: number;
|
|
61
|
+
}
|
|
62
|
+
/** The result of selecting lines, after the N9 cap has been applied. */
|
|
63
|
+
export interface LineSelection {
|
|
64
|
+
/** The text served to the caller. Line-numbered unless `clipped`. */
|
|
65
|
+
content: string;
|
|
66
|
+
/** `content.split("\n").length` — recomputed after any clipping. */
|
|
67
|
+
lineCount: number;
|
|
68
|
+
/** True when the N9 cap trimmed the selection. Surfaces as `source_clipped`. */
|
|
69
|
+
clipped: boolean;
|
|
70
|
+
}
|
|
71
|
+
export declare function calculateRange(params: LineRangeRequest): ReadRange;
|
|
72
|
+
export declare function adjustRange(range: ReadRange, totalLines: number): ReadRange;
|
|
73
|
+
export declare function extractLines(lines: string[], range: ReadRange): string;
|
|
74
|
+
/**
|
|
75
|
+
* Extract the requested lines and apply the N9 cap, returning the clipped flag
|
|
76
|
+
* rather than mutating the caller's locals.
|
|
77
|
+
*
|
|
78
|
+
* The two steps ship as one function because they were never separable at the
|
|
79
|
+
* call site: before this task the cap read and reassigned the very
|
|
80
|
+
* `selectedContent` and `selectedLineCount` that the `extractLines` call one
|
|
81
|
+
* line above it had produced, inside `handle()`. Deliberately unnumbered — a
|
|
82
|
+
* line citation into `read_file.ts` written here would name a revision that no
|
|
83
|
+
* longer exists the moment this commit lands, and T12 moves that whole region
|
|
84
|
+
* again (T8b's de-numbering precedent). Splitting them would mean passing
|
|
85
|
+
* the already-extracted string and its line count back in as parameters, which
|
|
86
|
+
* duplicates the `.split("\n")` at the call site and adds an argument derivable
|
|
87
|
+
* from another. `extractLines` stays exported so its own contract is pinnable
|
|
88
|
+
* independently.
|
|
89
|
+
*/
|
|
90
|
+
export declare function selectLines(lines: string[], range: ReadRange): LineSelection;
|
|
91
|
+
//# sourceMappingURL=line-range.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"line-range.d.ts","sourceRoot":"","sources":["../../../src/services/file-read/line-range.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AA2BH,qFAAqF;AACrF,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb;AAED;;;;GAIG;AACH,MAAM,WAAW,gBAAgB;IAC/B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,wEAAwE;AACxE,MAAM,WAAW,aAAa;IAC5B,qEAAqE;IACrE,OAAO,EAAE,MAAM,CAAC;IAChB,oEAAoE;IACpE,SAAS,EAAE,MAAM,CAAC;IAClB,gFAAgF;IAChF,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,gBAAgB,GAAG,SAAS,CAsBlE;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,GAAG,SAAS,CAK3E;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,SAAS,GAAG,MAAM,CAatE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,SAAS,GAAG,aAAa,CA0B5E"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @massa-ai/core - Line range selection
|
|
3
|
+
*
|
|
4
|
+
* Module 6 of the `tools/read_file.ts` extraction (PR-D, T11). Owns everything
|
|
5
|
+
* that turns a request into the slice of a file the caller actually receives:
|
|
6
|
+
* `calculateRange` (request → range), `adjustRange` (range → range clamped to
|
|
7
|
+
* the file), `extractLines` (range → line-numbered text), the `ReadRange` shape
|
|
8
|
+
* they pass around, and the N9 output cap — the `MASSA_AI_READ_FILE_MAX_LINES`
|
|
9
|
+
* ceiling together with the clipping block that used to sit inside `handle()`.
|
|
10
|
+
*
|
|
11
|
+
* FREE FUNCTIONS, NOT A CLASS, and that is a decision rather than a default.
|
|
12
|
+
* Modules 2-5 are classes because each owns per-tool state — a Map, a
|
|
13
|
+
* subscription, an injected collaborator. This module owns none: every function
|
|
14
|
+
* here is pure, and the one piece of state is a process-wide constant read from
|
|
15
|
+
* the environment. `services/cache/lru-evict.ts` (module 1) is a bare function
|
|
16
|
+
* for exactly this reason (`design.md` §5.2). A class would also have to be
|
|
17
|
+
* constructed in `ReadFileTool`'s constructor, which is the one surface C64 is
|
|
18
|
+
* about, and would falsely imply per-instance state.
|
|
19
|
+
*
|
|
20
|
+
* IT IMPORTS NOTHING AT ALL, which is `lru-evict.ts`'s property and is asserted
|
|
21
|
+
* by this module's own unit test rather than by any tier rule.
|
|
22
|
+
*
|
|
23
|
+
* `LineRangeRequest` EXISTS BECAUSE `ReadFileParams` IS MODULE 7's. `design.md`
|
|
24
|
+
* §5.1 assigns `interface ReadFileParams` to `read-file.service.ts`, which T12
|
|
25
|
+
* built and which COMPOSES this module — so importing it here would be a 6 → 7
|
|
26
|
+
* edge against the composition direction. It exists now and the rule is
|
|
27
|
+
* unchanged. `calculateRange` reads exactly four fields off its argument, so
|
|
28
|
+
* this module declares those four and `ReadFileParams` satisfies it
|
|
29
|
+
* structurally. The alternative Design's module table hides is that module 6
|
|
30
|
+
* silently depends on module 7's type; naming the subset is what keeps 6
|
|
31
|
+
* standalone. Same shape as the 4 → 5 edge §4.1 decided, which Design handed to
|
|
32
|
+
* Tasks explicitly — this one it handed to nobody.
|
|
33
|
+
*
|
|
34
|
+
* THE CLIPPED PATH RETURNS UNNUMBERED TEXT, AND THAT IS PRESERVED DELIBERATELY.
|
|
35
|
+
* `extractLines` prefixes every line with a padded line number; the cap does NOT
|
|
36
|
+
* slice that string — it re-slices the RAW `lines` array and joins it, so a
|
|
37
|
+
* clipped response loses the prefixes an unclipped one carries. Found by the
|
|
38
|
+
* Plan Challenge gate's red-team lens before this file was written, and
|
|
39
|
+
* confirmed by the evidence audit: no test anywhere asserts content on the
|
|
40
|
+
* clipped path, so composing this as "extract, then slice the extracted text"
|
|
41
|
+
* would have been a silent behavior change inside a behavior-preserving PR.
|
|
42
|
+
* `__tests__/line-range.test.ts` pins both branches' exact format. Whether the
|
|
43
|
+
* asymmetry is intentional is not PR-D's question — it is logged, not fixed
|
|
44
|
+
* (`spec.md` §6, R-07's precedent).
|
|
45
|
+
*/
|
|
46
|
+
/**
|
|
47
|
+
* N9: per-read line ceiling on user-facing read_file output. Default 500;
|
|
48
|
+
* override via `MASSA_AI_READ_FILE_MAX_LINES`. Invalid/negative/zero
|
|
49
|
+
* values fall back to 500 (treat invalid as unset). When the requested
|
|
50
|
+
* range exceeds the cap, `selectedContent` is sliced and `source_clipped:
|
|
51
|
+
* true` is emitted in the same response. The true total line count is
|
|
52
|
+
* always surfaced as `lineRange.actual.total` so `omitted = total - shown`
|
|
53
|
+
* is derivable. Internal enrichment paths (SymbolGraphService.readSnippet
|
|
54
|
+
* /readContext used by go_to_definition) are NOT capped — see
|
|
55
|
+
* symbol-graph.service.ts for the exclusion comment.
|
|
56
|
+
*
|
|
57
|
+
* STILL EVALUATED ONCE AT MODULE LOAD, which is what keeps the move
|
|
58
|
+
* behavior-preserving. This module is a dependency of `tools/read_file.ts`, so
|
|
59
|
+
* it now evaluates strictly EARLIER in the graph — but both happen on the first
|
|
60
|
+
* import of `read_file.ts`, and a module-level const was never re-readable at
|
|
61
|
+
* call time in either arrangement. Measured: no test can reach this branch.
|
|
62
|
+
* `apps/tools-api/src/routes/workspace.test.ts:422` does set this variable, but
|
|
63
|
+
* it drives `workspace.ts`'s own PER-REQUEST copy of the same IIFE for
|
|
64
|
+
* `symbol_snippet` (a third, separate read of the same name), never this one.
|
|
65
|
+
*/
|
|
66
|
+
const MASSA_AI_READ_FILE_MAX_LINES = (() => {
|
|
67
|
+
const v = Number(process.env.MASSA_AI_READ_FILE_MAX_LINES);
|
|
68
|
+
return Number.isFinite(v) && v > 0 ? Math.floor(v) : 500;
|
|
69
|
+
})();
|
|
70
|
+
export function calculateRange(params) {
|
|
71
|
+
// Priority: lineStart/lineEnd > offset/limit > entire file
|
|
72
|
+
if (params.lineStart !== undefined && params.lineEnd !== undefined) {
|
|
73
|
+
return {
|
|
74
|
+
start: Math.max(1, params.lineStart),
|
|
75
|
+
end: params.lineEnd,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (params.offset !== undefined) {
|
|
79
|
+
const offset = Math.max(1, params.offset);
|
|
80
|
+
const limit = params.limit || 1000;
|
|
81
|
+
return {
|
|
82
|
+
start: offset,
|
|
83
|
+
end: offset + limit - 1,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
start: 1,
|
|
88
|
+
end: Infinity,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export function adjustRange(range, totalLines) {
|
|
92
|
+
const start = Math.max(1, Math.min(range.start, totalLines));
|
|
93
|
+
const end = range.end === Infinity ? totalLines : Math.min(range.end, totalLines);
|
|
94
|
+
return { start, end };
|
|
95
|
+
}
|
|
96
|
+
export function extractLines(lines, range) {
|
|
97
|
+
const start = range.start - 1; // Convert to 0-indexed
|
|
98
|
+
const end = range.end;
|
|
99
|
+
const selectedLines = lines.slice(start, end);
|
|
100
|
+
// Add line numbers for context
|
|
101
|
+
return selectedLines
|
|
102
|
+
.map((line, index) => {
|
|
103
|
+
const lineNum = start + index + 1;
|
|
104
|
+
return `${lineNum.toString().padStart(6, " ")}: ${line}`;
|
|
105
|
+
})
|
|
106
|
+
.join("\n");
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Extract the requested lines and apply the N9 cap, returning the clipped flag
|
|
110
|
+
* rather than mutating the caller's locals.
|
|
111
|
+
*
|
|
112
|
+
* The two steps ship as one function because they were never separable at the
|
|
113
|
+
* call site: before this task the cap read and reassigned the very
|
|
114
|
+
* `selectedContent` and `selectedLineCount` that the `extractLines` call one
|
|
115
|
+
* line above it had produced, inside `handle()`. Deliberately unnumbered — a
|
|
116
|
+
* line citation into `read_file.ts` written here would name a revision that no
|
|
117
|
+
* longer exists the moment this commit lands, and T12 moves that whole region
|
|
118
|
+
* again (T8b's de-numbering precedent). Splitting them would mean passing
|
|
119
|
+
* the already-extracted string and its line count back in as parameters, which
|
|
120
|
+
* duplicates the `.split("\n")` at the call site and adds an argument derivable
|
|
121
|
+
* from another. `extractLines` stays exported so its own contract is pinnable
|
|
122
|
+
* independently.
|
|
123
|
+
*/
|
|
124
|
+
export function selectLines(lines, range) {
|
|
125
|
+
let content = extractLines(lines, range);
|
|
126
|
+
let lineCount = content.split("\n").length;
|
|
127
|
+
// N9: cap user-facing read_file output at MASSA_AI_READ_FILE_MAX_LINES
|
|
128
|
+
// (default 500). When the adjusted range exceeds the cap, slice the
|
|
129
|
+
// content and emit `source_clipped: true` in the same response so the
|
|
130
|
+
// caller knows lines were omitted. `lineRange.actual.total` keeps the
|
|
131
|
+
// true total line count so `omitted = total - shown` is derivable.
|
|
132
|
+
//
|
|
133
|
+
// NOTE the slice is over `lines`, the RAW array — not over `content`, which
|
|
134
|
+
// `extractLines` has already numbered. That is the pre-existing shape, moved
|
|
135
|
+
// verbatim: a clipped response is unnumbered where an unclipped one is
|
|
136
|
+
// numbered. See this file's header.
|
|
137
|
+
let clipped = false;
|
|
138
|
+
if (lineCount > MASSA_AI_READ_FILE_MAX_LINES) {
|
|
139
|
+
const cappedLines = lines.slice(range.start - 1, range.start - 1 + MASSA_AI_READ_FILE_MAX_LINES);
|
|
140
|
+
content = cappedLines.join("\n");
|
|
141
|
+
lineCount = content.split("\n").length;
|
|
142
|
+
clipped = true;
|
|
143
|
+
}
|
|
144
|
+
return { content, lineCount, clipped };
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=line-range.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"line-range.js","sourceRoot":"","sources":["../../../src/services/file-read/line-range.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAEH;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,4BAA4B,GAAG,CAAC,GAAG,EAAE;IACzC,MAAM,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;IAC3D,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;AAC3D,CA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,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ProjectRootCache } from "./project-root-cache.js";
|
|
2
|
+
export declare class PathContainment {
|
|
3
|
+
private readonly projectRoots;
|
|
4
|
+
constructor(projectRoots: ProjectRootCache);
|
|
5
|
+
/**
|
|
6
|
+
* Resolve a filePath to an absolute path.
|
|
7
|
+
*
|
|
8
|
+
* Resolution rules:
|
|
9
|
+
* - Absolute path → returned verbatim (still normalized via path.resolve, but base-independent).
|
|
10
|
+
* - `projectId` present → resolved against the workspace `project_path`. If the
|
|
11
|
+
* workspace lookup fails (no root), this returns null (ambiguous).
|
|
12
|
+
* - No `projectId` AND relative `filePath` → returns null. We deliberately do NOT
|
|
13
|
+
* fall back to `path.resolve(filePath)` against process.cwd(), because a
|
|
14
|
+
* relative path arriving without a projectId is ambiguous and historically
|
|
15
|
+
* read from the server's cwd (COVERAGE finding #3). Callers must provide an
|
|
16
|
+
* absolute path or a projectId.
|
|
17
|
+
*
|
|
18
|
+
* Returns null to signal an ambiguous/unsatisfiable path so `handle()` can map it
|
|
19
|
+
* to a distinct, clear error instead of the generic "Failed to read file" catch.
|
|
20
|
+
*/
|
|
21
|
+
resolveFilePath(filePath: string, projectId?: string): Promise<string | null>;
|
|
22
|
+
/**
|
|
23
|
+
* Wave 5 FR-12 / AD-W5-006: filesystem-side path containment.
|
|
24
|
+
*
|
|
25
|
+
* An absolute path is allowed iff it resolves under one of:
|
|
26
|
+
* 1. the project root (workspace lookup for projectId, when provided)
|
|
27
|
+
* 2. process.cwd()
|
|
28
|
+
* 3. an entry in MASSA_AI_READ_FILE_ROOTS (colon-separated env)
|
|
29
|
+
*
|
|
30
|
+
* Project root + cwd are ALWAYS allowed. Outside → teaching error listing
|
|
31
|
+
* valid roots only (no host path enumeration). The check uses path.relative
|
|
32
|
+
* to detect traversal out of a root (a result starting with ".." or an
|
|
33
|
+
* absolute path on another drive means outside).
|
|
34
|
+
*
|
|
35
|
+
* The env allowlist is read at CALL TIME (not config-load time) so test
|
|
36
|
+
* suites and runtime operators can set it without restarting the process.
|
|
37
|
+
* config.readFile.extraRoots mirrors the same env for introspection.
|
|
38
|
+
*
|
|
39
|
+
* Returns { allowed: true } on success, or { allowed: false, error } with a
|
|
40
|
+
* Wave-4-N6-style teaching error listing the valid roots.
|
|
41
|
+
*/
|
|
42
|
+
checkPathContainment(absoluteFilePath: string, projectId?: string): Promise<{
|
|
43
|
+
allowed: true;
|
|
44
|
+
} | {
|
|
45
|
+
allowed: false;
|
|
46
|
+
error: string;
|
|
47
|
+
}>;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=path-containment.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-containment.d.ts","sourceRoot":"","sources":["../../../src/services/file-read/path-containment.ts"],"names":[],"mappings":"AA4BA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAEhE,qBAAa,eAAe;IACd,OAAO,CAAC,QAAQ,CAAC,YAAY;gBAAZ,YAAY,EAAE,gBAAgB;IAE3D;;;;;;;;;;;;;;;OAeG;IACG,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAmBnF;;;;;;;;;;;;;;;;;;;OAmBG;IACG,oBAAoB,CACxB,gBAAgB,EAAE,MAAM,EACxB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC;QAAE,OAAO,EAAE,IAAI,CAAA;KAAE,GAAG;QAAE,OAAO,EAAE,KAAK,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;CAuClE"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @massa-ai/core - Path resolution and filesystem containment
|
|
3
|
+
*
|
|
4
|
+
* Module 2 of the `tools/read_file.ts` extraction (PR-D, T9). Holds the two
|
|
5
|
+
* security-sensitive path functions `ReadFileTool` used to carry as private
|
|
6
|
+
* methods: `resolveFilePath` (absolute / projectId / ambiguous) and
|
|
7
|
+
* `checkPathContainment` (Wave 5 FR-12 / AD-W5-006).
|
|
8
|
+
*
|
|
9
|
+
* BOTH BODIES ARE MOVED VERBATIM. RFS-01's gate is structural and cannot tell a
|
|
10
|
+
* correct delegate from a widened one (RFS-01 AC-6), and `check-core-layering`
|
|
11
|
+
* cannot either, so this module's only real sensor is its tests —
|
|
12
|
+
* `__tests__/read-file-containment-shapes.test.ts` (RFS-06, the three mutation
|
|
13
|
+
* shapes all seven pre-existing containment tests fail to kill) plus
|
|
14
|
+
* `__tests__/path-containment.test.ts`. Neither the containment predicate nor
|
|
15
|
+
* the teaching-error text may drift here; RFS-06 AC-2 pins the latter.
|
|
16
|
+
*
|
|
17
|
+
* It takes the project-root cache as a constructor dependency. `design.md` §5.1
|
|
18
|
+
* names the 2 → 3 edge as real and one-directional — "module 2 cannot be
|
|
19
|
+
* constructed without module 3" — but chooses no mechanism, unlike the 4 → 5
|
|
20
|
+
* edge it hands to Tasks explicitly (`tasks.md` §4.1). Constructor injection is
|
|
21
|
+
* chosen over a `getProjectRoot` callback because the two are behaviorally
|
|
22
|
+
* indistinguishable here (module 3 caches, so neither adds a lookup) and the
|
|
23
|
+
* direct edge is the one §5.1 describes; the callback shape exists in §4.1/§4.2
|
|
24
|
+
* to stop a module NAMING a heavy collaborator, and module 3 is neither heavy
|
|
25
|
+
* nor a type this module would otherwise avoid.
|
|
26
|
+
*/
|
|
27
|
+
import { sanitizeFilePath } from "@massa-ai/shared";
|
|
28
|
+
import path from "path";
|
|
29
|
+
export class PathContainment {
|
|
30
|
+
projectRoots;
|
|
31
|
+
constructor(projectRoots) {
|
|
32
|
+
this.projectRoots = projectRoots;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Resolve a filePath to an absolute path.
|
|
36
|
+
*
|
|
37
|
+
* Resolution rules:
|
|
38
|
+
* - Absolute path → returned verbatim (still normalized via path.resolve, but base-independent).
|
|
39
|
+
* - `projectId` present → resolved against the workspace `project_path`. If the
|
|
40
|
+
* workspace lookup fails (no root), this returns null (ambiguous).
|
|
41
|
+
* - No `projectId` AND relative `filePath` → returns null. We deliberately do NOT
|
|
42
|
+
* fall back to `path.resolve(filePath)` against process.cwd(), because a
|
|
43
|
+
* relative path arriving without a projectId is ambiguous and historically
|
|
44
|
+
* read from the server's cwd (COVERAGE finding #3). Callers must provide an
|
|
45
|
+
* absolute path or a projectId.
|
|
46
|
+
*
|
|
47
|
+
* Returns null to signal an ambiguous/unsatisfiable path so `handle()` can map it
|
|
48
|
+
* to a distinct, clear error instead of the generic "Failed to read file" catch.
|
|
49
|
+
*/
|
|
50
|
+
async resolveFilePath(filePath, projectId) {
|
|
51
|
+
if (path.isAbsolute(filePath)) {
|
|
52
|
+
return path.resolve(filePath);
|
|
53
|
+
}
|
|
54
|
+
if (projectId) {
|
|
55
|
+
const root = await this.projectRoots.getProjectRoot(projectId);
|
|
56
|
+
if (root) {
|
|
57
|
+
// Wave 5 FR-12: strip ../ traversal tokens from relative paths before
|
|
58
|
+
// resolving under the project root. sanitizeFilePath removes ../ and
|
|
59
|
+
// ..\ segments so a crafted relative path can't escape the root.
|
|
60
|
+
const cleaned = sanitizeFilePath(filePath);
|
|
61
|
+
return path.resolve(root, cleaned);
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
// Relative path with no projectId — do not guess against cwd.
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Wave 5 FR-12 / AD-W5-006: filesystem-side path containment.
|
|
70
|
+
*
|
|
71
|
+
* An absolute path is allowed iff it resolves under one of:
|
|
72
|
+
* 1. the project root (workspace lookup for projectId, when provided)
|
|
73
|
+
* 2. process.cwd()
|
|
74
|
+
* 3. an entry in MASSA_AI_READ_FILE_ROOTS (colon-separated env)
|
|
75
|
+
*
|
|
76
|
+
* Project root + cwd are ALWAYS allowed. Outside → teaching error listing
|
|
77
|
+
* valid roots only (no host path enumeration). The check uses path.relative
|
|
78
|
+
* to detect traversal out of a root (a result starting with ".." or an
|
|
79
|
+
* absolute path on another drive means outside).
|
|
80
|
+
*
|
|
81
|
+
* The env allowlist is read at CALL TIME (not config-load time) so test
|
|
82
|
+
* suites and runtime operators can set it without restarting the process.
|
|
83
|
+
* config.readFile.extraRoots mirrors the same env for introspection.
|
|
84
|
+
*
|
|
85
|
+
* Returns { allowed: true } on success, or { allowed: false, error } with a
|
|
86
|
+
* Wave-4-N6-style teaching error listing the valid roots.
|
|
87
|
+
*/
|
|
88
|
+
async checkPathContainment(absoluteFilePath, projectId) {
|
|
89
|
+
// Collect valid roots. Project root is included only when actually
|
|
90
|
+
// resolvable (workspace lookup succeeds); cwd is always available.
|
|
91
|
+
const roots = [];
|
|
92
|
+
if (projectId) {
|
|
93
|
+
const root = await this.projectRoots.getProjectRoot(projectId);
|
|
94
|
+
if (root)
|
|
95
|
+
roots.push(path.resolve(root));
|
|
96
|
+
}
|
|
97
|
+
roots.push(path.resolve(process.cwd()));
|
|
98
|
+
// Read the env allowlist at call time so tests/operators can set it
|
|
99
|
+
// without restarting the process. Colon-separated (POSIX-style).
|
|
100
|
+
const envRoots = (process.env.MASSA_AI_READ_FILE_ROOTS ?? "")
|
|
101
|
+
.split(":")
|
|
102
|
+
.map((s) => s.trim())
|
|
103
|
+
.filter((s) => s.length > 0);
|
|
104
|
+
for (const extra of envRoots) {
|
|
105
|
+
roots.push(path.resolve(extra));
|
|
106
|
+
}
|
|
107
|
+
const target = path.resolve(absoluteFilePath);
|
|
108
|
+
for (const root of roots) {
|
|
109
|
+
const rel = path.relative(root, target);
|
|
110
|
+
// Inside iff rel does not start with ".." and is not absolute (cross-drive).
|
|
111
|
+
if (rel !== "" && !rel.startsWith("..") && !path.isAbsolute(rel)) {
|
|
112
|
+
return { allowed: true };
|
|
113
|
+
}
|
|
114
|
+
// Exact root match (rel === "") is also inside.
|
|
115
|
+
if (rel === "")
|
|
116
|
+
return { allowed: true };
|
|
117
|
+
}
|
|
118
|
+
// Outside all roots → teaching error listing valid roots only.
|
|
119
|
+
const validRootsList = roots.map((r) => ` - ${r}`).join("\n");
|
|
120
|
+
return {
|
|
121
|
+
allowed: false,
|
|
122
|
+
error: `read_file path containment: "${target}" is outside the allowed roots.\n` +
|
|
123
|
+
`Valid roots (project root + cwd + MASSA_AI_READ_FILE_ROOTS):\n${validRootsList}\n` +
|
|
124
|
+
`Provide a filePath that resolves under one of these roots.`,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=path-containment.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-containment.js","sourceRoot":"","sources":["../../../src/services/file-read/path-containment.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,IAAI,MAAM,MAAM,CAAC;AAGxB,MAAM,OAAO,eAAe;IACG;IAA7B,YAA6B,YAA8B;QAA9B,iBAAY,GAAZ,YAAY,CAAkB;IAAG,CAAC;IAE/D;;;;;;;;;;;;;;;OAeG;IACH,KAAK,CAAC,eAAe,CAAC,QAAgB,EAAE,SAAkB;QACxD,IAAI,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC9B,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAChC,CAAC;QACD,IAAI,SAAS,EAAE,CAAC;YACd,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;YAC/D,IAAI,IAAI,EAAE,CAAC;gBACT,sEAAsE;gBACtE,qEAAqE;gBACrE,iEAAiE;gBACjE,MAAM,OAAO,GAAG,gBAAgB,CAAC,QAAQ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AG,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;YACxC,6EAA6E;YAC7E,IAAI,GAAG,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;gBACjE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;YAC3B,CAAC;YACD,gDAAgD;YAChD,IAAI,GAAG,KAAK,EAAE;gBAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;QAC3C,CAAC;QACD,+DAA+D;QAC/D,MAAM,cAAc,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/D,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EACH,gCAAgC,MAAM,mCAAmC;gBACzE,iEAAiE,cAAc,IAAI;gBACnF,4DAA4D;SAC/D,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare class ProjectRootCache {
|
|
2
|
+
private projectRootCache;
|
|
3
|
+
private readonly ROOT_CACHE_TTL;
|
|
4
|
+
/**
|
|
5
|
+
* Maximum entries retained in the cache. Without a cap, an adversarial caller
|
|
6
|
+
* cycling distinct projectIds grows the map for the process lifetime. Map
|
|
7
|
+
* preserves INSERTION order in JS; we promote a key to most-recently-used on
|
|
8
|
+
* GET via delete+set, and evict the oldest key on SET while over the cap.
|
|
9
|
+
*
|
|
10
|
+
* Before the extraction this bound was read from `ReadFileTool`'s own
|
|
11
|
+
* FILE_CACHE_MAX_ENTRIES, which belongs to the file CONTENT cache and which
|
|
12
|
+
* T10 moved to `file-content-cache.ts`. Both were 512 and neither ever read
|
|
13
|
+
* the other's value, so naming the bound here retains the same number at the
|
|
14
|
+
* same call positions.
|
|
15
|
+
* Mirrors SymbolGraphService.PROJECT_ROOT_CACHE_MAX_ENTRIES.
|
|
16
|
+
*/
|
|
17
|
+
private readonly PROJECT_ROOT_CACHE_MAX_ENTRIES;
|
|
18
|
+
constructor();
|
|
19
|
+
getProjectRoot(projectId: string): Promise<string | null>;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=project-root-cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"project-root-cache.d.ts","sourceRoot":"","sources":["../../../src/services/file-read/project-root-cache.ts"],"names":[],"mappings":"AA2BA,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,gBAAgB,CAAkC;IAC1D,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAU;IACzC;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,QAAQ,CAAC,8BAA8B,CAAO;;IAehD,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CAqBhE"}
|