@aragon-agent/cli 0.6.77 → 0.6.79
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/README.md +89 -21
- package/dist/agent/controller.d.ts +36 -0
- package/dist/agent/controller.js +108 -0
- package/dist/agent/system-prompt.d.ts +10 -0
- package/dist/agent/system-prompt.js +6 -0
- package/dist/app/App.js +108 -27
- package/dist/app/index-console.d.ts +59 -0
- package/dist/app/index-console.js +99 -0
- package/dist/cli.js +80 -4
- package/dist/commands/builtins.js +4 -2
- package/dist/commands/catalog.js +7 -1
- package/dist/commands/index-build.js +10 -1
- package/dist/commands/index-command.d.ts +43 -0
- package/dist/commands/index-command.js +244 -0
- package/dist/commands/index-copy.d.ts +50 -0
- package/dist/commands/index-copy.js +82 -0
- package/dist/commands/kit-command.d.ts +10 -0
- package/dist/commands/kit-command.js +8 -2
- package/dist/commands/kits-command.d.ts +35 -0
- package/dist/commands/kits-command.js +137 -0
- package/dist/commands/registry.d.ts +10 -0
- package/dist/compaction/memory-agent.js +61 -0
- package/dist/config/env.d.ts +30 -0
- package/dist/config/env.js +47 -0
- package/dist/config/launch-notice.d.ts +61 -0
- package/dist/config/launch-notice.js +138 -0
- package/dist/config/load.d.ts +20 -0
- package/dist/config/load.js +72 -2
- package/dist/diagnostics/info.d.ts +28 -0
- package/dist/diagnostics/info.js +41 -0
- package/dist/exec/cli-commands.d.ts +24 -0
- package/dist/exec/cli-commands.js +48 -1
- package/dist/exec/index.js +9 -0
- package/dist/exec/options.d.ts +12 -0
- package/dist/host/index-build.d.ts +38 -7
- package/dist/host/index-build.js +74 -20
- package/dist/indexer/commit.d.ts +77 -0
- package/dist/indexer/commit.js +145 -0
- package/dist/indexer/discover.d.ts +50 -0
- package/dist/indexer/discover.js +147 -0
- package/dist/indexer/engine.d.ts +51 -0
- package/dist/indexer/engine.js +477 -0
- package/dist/indexer/errors.d.ts +62 -0
- package/dist/indexer/errors.js +86 -0
- package/dist/indexer/finalize.d.ts +30 -0
- package/dist/indexer/finalize.js +19 -0
- package/dist/indexer/fingerprint.d.ts +41 -0
- package/dist/indexer/fingerprint.js +49 -0
- package/dist/indexer/ignore.d.ts +59 -0
- package/dist/indexer/ignore.js +140 -0
- package/dist/indexer/index.d.ts +21 -0
- package/dist/indexer/index.js +21 -0
- package/dist/indexer/l0.d.ts +55 -0
- package/dist/indexer/l0.js +168 -0
- package/dist/indexer/limits.d.ts +75 -0
- package/dist/indexer/limits.js +106 -0
- package/dist/indexer/link.d.ts +68 -0
- package/dist/indexer/link.js +220 -0
- package/dist/indexer/lock.d.ts +115 -0
- package/dist/indexer/lock.js +270 -0
- package/dist/indexer/parse.d.ts +68 -0
- package/dist/indexer/parse.js +163 -0
- package/dist/indexer/paths.d.ts +99 -0
- package/dist/indexer/paths.js +198 -0
- package/dist/indexer/plan.d.ts +48 -0
- package/dist/indexer/plan.js +80 -0
- package/dist/indexer/rank.d.ts +42 -0
- package/dist/indexer/rank.js +125 -0
- package/dist/indexer/read/cache.d.ts +28 -0
- package/dist/indexer/read/cache.js +54 -0
- package/dist/indexer/read/query.d.ts +48 -0
- package/dist/indexer/read/query.js +361 -0
- package/dist/indexer/scanner/extract.d.ts +36 -0
- package/dist/indexer/scanner/extract.js +134 -0
- package/dist/indexer/scanner/mask.d.ts +44 -0
- package/dist/indexer/scanner/mask.js +198 -0
- package/dist/indexer/scanner/py-extract.d.ts +29 -0
- package/dist/indexer/scanner/py-extract.js +164 -0
- package/dist/indexer/scanner/refs.d.ts +31 -0
- package/dist/indexer/scanner/refs.js +116 -0
- package/dist/indexer/scanner/ts-extract.d.ts +39 -0
- package/dist/indexer/scanner/ts-extract.js +277 -0
- package/dist/indexer/store.d.ts +88 -0
- package/dist/indexer/store.js +309 -0
- package/dist/indexer/tools.d.ts +120 -0
- package/dist/indexer/tools.js +132 -0
- package/dist/indexer/types.d.ts +225 -0
- package/dist/indexer/types.js +44 -0
- package/dist/indexer/wiring.d.ts +45 -0
- package/dist/indexer/wiring.js +42 -0
- package/dist/tools/index.d.ts +16 -2
- package/dist/tools/index.js +23 -0
- package/package.json +3 -3
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the index lives and how paths are normalized (spec 6.2 `paths.ts`).
|
|
3
|
+
*
|
|
4
|
+
* Two rules matter and both break silently if edited by hand:
|
|
5
|
+
*
|
|
6
|
+
* 1. EVERY stored path is repo-relative with forward slashes. Absolute paths in
|
|
7
|
+
* the artifact would make it machine-specific, break `git diff` review of a
|
|
8
|
+
* committed index, and leak the user's directory layout into the L0 block.
|
|
9
|
+
* 2. The root is discovered once per build (`git rev-parse --show-toplevel`,
|
|
10
|
+
* falling back to the working directory) and then threaded through every
|
|
11
|
+
* stage. A stage that re-discovers the root can disagree with the stage that
|
|
12
|
+
* wrote the rows, which shows up as unresolvable references and nothing else.
|
|
13
|
+
*
|
|
14
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
15
|
+
*/
|
|
16
|
+
/** Directory name of the artifact root, a sibling of `index.md` / `config.md`. */
|
|
17
|
+
export declare const INDEX_DIR_NAME = ".claude-index";
|
|
18
|
+
/** Subdirectory that holds only generated artifacts (the committed prose files stay outside it). */
|
|
19
|
+
export declare const INDEX_AST_DIR_NAME = "ast";
|
|
20
|
+
/** All artifact paths for one workspace root. Pure: derived from `root` alone. */
|
|
21
|
+
export interface IndexPaths {
|
|
22
|
+
/** Workspace root, absolute, forward slashes. */
|
|
23
|
+
readonly root: string;
|
|
24
|
+
/** `<root>/.claude-index` */
|
|
25
|
+
readonly indexDir: string;
|
|
26
|
+
/** `<root>/.claude-index/ast` */
|
|
27
|
+
readonly astDir: string;
|
|
28
|
+
/** `<root>/.claude-index/ast/build.lock` */
|
|
29
|
+
readonly lockFile: string;
|
|
30
|
+
/** `<root>/.claude-index/ast/current` */
|
|
31
|
+
readonly currentFile: string;
|
|
32
|
+
/** `<root>/.claude-index/ast/generations` */
|
|
33
|
+
readonly generationsDir: string;
|
|
34
|
+
/** The committed prose index, if the skill has run. */
|
|
35
|
+
readonly proseIndexFile: string;
|
|
36
|
+
/** The skill's config file, if it has run. */
|
|
37
|
+
readonly proseConfigFile: string;
|
|
38
|
+
}
|
|
39
|
+
/** Normalize any path to forward slashes without touching case (Windows is case-insensitive, the artifact is not). */
|
|
40
|
+
export declare function toPosix(path: string): string;
|
|
41
|
+
/** Derive every artifact path from one root. */
|
|
42
|
+
export declare function indexPathsFor(root: string): IndexPaths;
|
|
43
|
+
/**
|
|
44
|
+
* Repo-relative path for storage. Returns '' for a path outside the root: callers
|
|
45
|
+
* skip such files instead of storing an `..` path, because a stored path that
|
|
46
|
+
* escapes the root makes every query's path filter wrong.
|
|
47
|
+
*/
|
|
48
|
+
export declare function relativeToRoot(root: string, absolute: string): string;
|
|
49
|
+
/** Absolute path for a stored repo-relative path. */
|
|
50
|
+
export declare function absoluteOf(root: string, relative: string): string;
|
|
51
|
+
/**
|
|
52
|
+
* The top path segment of a repo-relative path, used as the area label in the
|
|
53
|
+
* L0 block (`packages/cli/src` -> area). Two segments is the useful granularity
|
|
54
|
+
* for monorepo layouts and one is enough for flat repositories.
|
|
55
|
+
*/
|
|
56
|
+
export declare function areaOf(relative: string): string;
|
|
57
|
+
/** Directory part of a repo-relative path (`''` for a root-level file). */
|
|
58
|
+
export declare function dirOf(relative: string): string;
|
|
59
|
+
/** File extension in lower case, including the dot (`'.tsx'`), or '' when absent. */
|
|
60
|
+
export declare function extensionOf(relative: string): string;
|
|
61
|
+
/** File name without its directory. */
|
|
62
|
+
export declare function baseNameOf(relative: string): string;
|
|
63
|
+
/** True when the path has a `__tests__` segment or a `.test.` / `.spec.` file name. */
|
|
64
|
+
export declare function isTestPath(relative: string): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Discover the workspace root. `git rev-parse --show-toplevel` wins because the
|
|
67
|
+
* index is per-repository by contract (spec 12.5 keeps monorepo sub-roots out of
|
|
68
|
+
* scope); the working directory is the documented fallback when git is absent.
|
|
69
|
+
*
|
|
70
|
+
* Returns `gitAvailable: false` in the fallback case so DISCOVER can label the
|
|
71
|
+
* panel `DISCOVER (fs walk)` instead of pretending git was used.
|
|
72
|
+
*/
|
|
73
|
+
export declare function discoverRoot(cwd: string): {
|
|
74
|
+
root: string;
|
|
75
|
+
gitAvailable: boolean;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* The frozen git head for this generation (`meta.json.gitHead`). Recorded once
|
|
79
|
+
* per build and never re-read: the L0 block must stay byte-identical for a given
|
|
80
|
+
* generation, so it may only use this frozen value (spec I-4).
|
|
81
|
+
*/
|
|
82
|
+
export declare function readGitHead(root: string): {
|
|
83
|
+
head: string | null;
|
|
84
|
+
dirty: boolean;
|
|
85
|
+
};
|
|
86
|
+
/** Commits between a recorded head and the current one, or undefined when git cannot say. */
|
|
87
|
+
export declare function countCommitsBehind(root: string, builtHead: string | null): number | undefined;
|
|
88
|
+
/** Generation directory `<ast>/generations/<n>`. */
|
|
89
|
+
export declare function generationDir(generationsDir: string, generation: number): string;
|
|
90
|
+
/**
|
|
91
|
+
* Create the artifact directories, translating a permission failure into
|
|
92
|
+
* `E_INDEX_UNWRITABLE` with the directory name in the message (spec 5.9: the
|
|
93
|
+
* user must learn WHICH directory is not writable).
|
|
94
|
+
*/
|
|
95
|
+
export declare function ensureArtifactDirs(paths: IndexPaths): void;
|
|
96
|
+
/** True when a pointer file exists, i.e. some generation was committed at least once. */
|
|
97
|
+
export declare function hasCurrentPointer(paths: IndexPaths): boolean;
|
|
98
|
+
/** Windows-safe display form of a repo-relative path (`a/b.ts` -> `a\b.ts`) for confirm dialogs. */
|
|
99
|
+
export declare function displayPath(relative: string): string;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the index lives and how paths are normalized (spec 6.2 `paths.ts`).
|
|
3
|
+
*
|
|
4
|
+
* Two rules matter and both break silently if edited by hand:
|
|
5
|
+
*
|
|
6
|
+
* 1. EVERY stored path is repo-relative with forward slashes. Absolute paths in
|
|
7
|
+
* the artifact would make it machine-specific, break `git diff` review of a
|
|
8
|
+
* committed index, and leak the user's directory layout into the L0 block.
|
|
9
|
+
* 2. The root is discovered once per build (`git rev-parse --show-toplevel`,
|
|
10
|
+
* falling back to the working directory) and then threaded through every
|
|
11
|
+
* stage. A stage that re-discovers the root can disagree with the stage that
|
|
12
|
+
* wrote the rows, which shows up as unresolvable references and nothing else.
|
|
13
|
+
*
|
|
14
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
15
|
+
*/
|
|
16
|
+
import { execFileSync } from 'node:child_process';
|
|
17
|
+
import { existsSync, mkdirSync, realpathSync } from 'node:fs';
|
|
18
|
+
import { join, resolve, sep } from 'node:path';
|
|
19
|
+
import { unwritableError } from './errors.js';
|
|
20
|
+
/** Directory name of the artifact root, a sibling of `index.md` / `config.md`. */
|
|
21
|
+
export const INDEX_DIR_NAME = '.claude-index';
|
|
22
|
+
/** Subdirectory that holds only generated artifacts (the committed prose files stay outside it). */
|
|
23
|
+
export const INDEX_AST_DIR_NAME = 'ast';
|
|
24
|
+
/** Normalize any path to forward slashes without touching case (Windows is case-insensitive, the artifact is not). */
|
|
25
|
+
export function toPosix(path) {
|
|
26
|
+
return path.replace(/\\/g, '/');
|
|
27
|
+
}
|
|
28
|
+
/** Derive every artifact path from one root. */
|
|
29
|
+
export function indexPathsFor(root) {
|
|
30
|
+
const normalizedRoot = toPosix(resolve(root));
|
|
31
|
+
const indexDir = join(normalizedRoot, INDEX_DIR_NAME);
|
|
32
|
+
const astDir = join(indexDir, INDEX_AST_DIR_NAME);
|
|
33
|
+
return {
|
|
34
|
+
root: normalizedRoot,
|
|
35
|
+
indexDir,
|
|
36
|
+
astDir,
|
|
37
|
+
lockFile: join(astDir, 'build.lock'),
|
|
38
|
+
currentFile: join(astDir, 'current'),
|
|
39
|
+
generationsDir: join(astDir, 'generations'),
|
|
40
|
+
proseIndexFile: join(indexDir, 'index.md'),
|
|
41
|
+
proseConfigFile: join(indexDir, 'config.md'),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Repo-relative path for storage. Returns '' for a path outside the root: callers
|
|
46
|
+
* skip such files instead of storing an `..` path, because a stored path that
|
|
47
|
+
* escapes the root makes every query's path filter wrong.
|
|
48
|
+
*/
|
|
49
|
+
export function relativeToRoot(root, absolute) {
|
|
50
|
+
const normalizedRoot = toPosix(resolve(root));
|
|
51
|
+
const normalizedPath = toPosix(resolve(absolute));
|
|
52
|
+
if (normalizedPath === normalizedRoot)
|
|
53
|
+
return '';
|
|
54
|
+
const prefix = normalizedRoot.endsWith('/') ? normalizedRoot : `${normalizedRoot}/`;
|
|
55
|
+
if (!normalizedPath.startsWith(prefix))
|
|
56
|
+
return '';
|
|
57
|
+
return normalizedPath.slice(prefix.length);
|
|
58
|
+
}
|
|
59
|
+
/** Absolute path for a stored repo-relative path. */
|
|
60
|
+
export function absoluteOf(root, relative) {
|
|
61
|
+
return join(toPosix(resolve(root)), relative);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The top path segment of a repo-relative path, used as the area label in the
|
|
65
|
+
* L0 block (`packages/cli/src` -> area). Two segments is the useful granularity
|
|
66
|
+
* for monorepo layouts and one is enough for flat repositories.
|
|
67
|
+
*/
|
|
68
|
+
export function areaOf(relative) {
|
|
69
|
+
const parts = relative.split('/');
|
|
70
|
+
if (parts.length <= 1)
|
|
71
|
+
return '.';
|
|
72
|
+
return parts.length >= 3 ? `${parts[0]}/${parts[1]}` : parts[0];
|
|
73
|
+
}
|
|
74
|
+
/** Directory part of a repo-relative path (`''` for a root-level file). */
|
|
75
|
+
export function dirOf(relative) {
|
|
76
|
+
const cut = relative.lastIndexOf('/');
|
|
77
|
+
return cut === -1 ? '' : relative.slice(0, cut);
|
|
78
|
+
}
|
|
79
|
+
/** File extension in lower case, including the dot (`'.tsx'`), or '' when absent. */
|
|
80
|
+
export function extensionOf(relative) {
|
|
81
|
+
const base = relative.slice(relative.lastIndexOf('/') + 1);
|
|
82
|
+
const dot = base.lastIndexOf('.');
|
|
83
|
+
if (dot <= 0)
|
|
84
|
+
return '';
|
|
85
|
+
return base.slice(dot).toLowerCase();
|
|
86
|
+
}
|
|
87
|
+
/** File name without its directory. */
|
|
88
|
+
export function baseNameOf(relative) {
|
|
89
|
+
return relative.slice(relative.lastIndexOf('/') + 1);
|
|
90
|
+
}
|
|
91
|
+
/** True when the path has a `__tests__` segment or a `.test.` / `.spec.` file name. */
|
|
92
|
+
export function isTestPath(relative) {
|
|
93
|
+
if (/(^|\/)__tests__(\/|$)/.test(relative))
|
|
94
|
+
return true;
|
|
95
|
+
return /\.(test|spec)\.[cm]?[jt]sx?$/.test(relative);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Discover the workspace root. `git rev-parse --show-toplevel` wins because the
|
|
99
|
+
* index is per-repository by contract (spec 12.5 keeps monorepo sub-roots out of
|
|
100
|
+
* scope); the working directory is the documented fallback when git is absent.
|
|
101
|
+
*
|
|
102
|
+
* Returns `gitAvailable: false` in the fallback case so DISCOVER can label the
|
|
103
|
+
* panel `DISCOVER (fs walk)` instead of pretending git was used.
|
|
104
|
+
*/
|
|
105
|
+
export function discoverRoot(cwd) {
|
|
106
|
+
try {
|
|
107
|
+
const out = execFileSync('git', ['rev-parse', '--show-toplevel'], {
|
|
108
|
+
cwd,
|
|
109
|
+
encoding: 'utf8',
|
|
110
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
111
|
+
timeout: 5_000,
|
|
112
|
+
}).trim();
|
|
113
|
+
if (out)
|
|
114
|
+
return { root: toPosix(realpathOrSelf(out)), gitAvailable: true };
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
// No git, not a repository, or git refused: the fallback below is the answer.
|
|
118
|
+
}
|
|
119
|
+
return { root: toPosix(realpathOrSelf(resolve(cwd))), gitAvailable: false };
|
|
120
|
+
}
|
|
121
|
+
/** `realpathSync` that keeps the original string when the path cannot be resolved. */
|
|
122
|
+
function realpathOrSelf(path) {
|
|
123
|
+
try {
|
|
124
|
+
return realpathSync(path);
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return path;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The frozen git head for this generation (`meta.json.gitHead`). Recorded once
|
|
132
|
+
* per build and never re-read: the L0 block must stay byte-identical for a given
|
|
133
|
+
* generation, so it may only use this frozen value (spec I-4).
|
|
134
|
+
*/
|
|
135
|
+
export function readGitHead(root) {
|
|
136
|
+
try {
|
|
137
|
+
const head = execFileSync('git', ['rev-parse', '--short=7', 'HEAD'], {
|
|
138
|
+
cwd: root,
|
|
139
|
+
encoding: 'utf8',
|
|
140
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
141
|
+
timeout: 5_000,
|
|
142
|
+
}).trim();
|
|
143
|
+
const status = execFileSync('git', ['status', '--porcelain', '--untracked-files=no'], {
|
|
144
|
+
cwd: root,
|
|
145
|
+
encoding: 'utf8',
|
|
146
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
147
|
+
timeout: 5_000,
|
|
148
|
+
}).trim();
|
|
149
|
+
return { head: head || null, dirty: status.length > 0 };
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return { head: null, dirty: false };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/** Commits between a recorded head and the current one, or undefined when git cannot say. */
|
|
156
|
+
export function countCommitsBehind(root, builtHead) {
|
|
157
|
+
if (!builtHead)
|
|
158
|
+
return undefined;
|
|
159
|
+
try {
|
|
160
|
+
const out = execFileSync('git', ['rev-list', '--count', `${builtHead}..HEAD`], {
|
|
161
|
+
cwd: root,
|
|
162
|
+
encoding: 'utf8',
|
|
163
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
164
|
+
timeout: 5_000,
|
|
165
|
+
}).trim();
|
|
166
|
+
const parsed = Number.parseInt(out, 10);
|
|
167
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** Generation directory `<ast>/generations/<n>`. */
|
|
174
|
+
export function generationDir(generationsDir, generation) {
|
|
175
|
+
return join(generationsDir, String(generation));
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Create the artifact directories, translating a permission failure into
|
|
179
|
+
* `E_INDEX_UNWRITABLE` with the directory name in the message (spec 5.9: the
|
|
180
|
+
* user must learn WHICH directory is not writable).
|
|
181
|
+
*/
|
|
182
|
+
export function ensureArtifactDirs(paths) {
|
|
183
|
+
try {
|
|
184
|
+
mkdirSync(paths.generationsDir, { recursive: true });
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
const cause = error instanceof Error ? error.message : String(error);
|
|
188
|
+
throw unwritableError(paths.astDir, cause);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/** True when a pointer file exists, i.e. some generation was committed at least once. */
|
|
192
|
+
export function hasCurrentPointer(paths) {
|
|
193
|
+
return existsSync(paths.currentFile);
|
|
194
|
+
}
|
|
195
|
+
/** Windows-safe display form of a repo-relative path (`a/b.ts` -> `a\b.ts`) for confirm dialogs. */
|
|
196
|
+
export function displayPath(relative) {
|
|
197
|
+
return sep === '\\' ? relative.replace(/\//g, '\\') : relative;
|
|
198
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stage 2 PLAN (spec 5.3): decide what to parse and what to reuse.
|
|
3
|
+
*
|
|
4
|
+
* The whole point of this stage is the one number the panel shows next to PLAN:
|
|
5
|
+
* `812 changed - 11,619 reused`. It compares the CONTENT HASH of each candidate against
|
|
6
|
+
* the previous generation's `files.jsonl`, which is what makes an incremental rebuild
|
|
7
|
+
* cost a fraction of a cold one.
|
|
8
|
+
*
|
|
9
|
+
* WHY A HASH AND NOT THE SIZE: a same-length edit is invisible to a size comparison, and
|
|
10
|
+
* a plan that trusts size turns the incremental path into a permanent no-op - the failure
|
|
11
|
+
* the design nails shut in §13.4 (the pointer never advances, so the stale hint never
|
|
12
|
+
* clears). `fingerprint.ts` reads and hashes the candidates once; PARSE then re-reads only
|
|
13
|
+
* the ones this stage marks changed.
|
|
14
|
+
*
|
|
15
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
16
|
+
*/
|
|
17
|
+
import type { DiscoveredFile } from './discover.js';
|
|
18
|
+
import type { FileFingerprint } from './fingerprint.js';
|
|
19
|
+
import type { FileRow } from './types.js';
|
|
20
|
+
/** The three-way split PLAN produces. */
|
|
21
|
+
export interface PlanResult {
|
|
22
|
+
/** Files that must be parsed: new, or whose content hash changed. */
|
|
23
|
+
readonly toParse: readonly DiscoveredFile[];
|
|
24
|
+
/** Paths whose previous row can be carried over untouched. */
|
|
25
|
+
readonly toReuse: readonly string[];
|
|
26
|
+
/** Paths present in the previous generation and gone now. */
|
|
27
|
+
readonly removed: readonly string[];
|
|
28
|
+
/** True when the file SET is identical to the previous generation (paths only, no hashes). */
|
|
29
|
+
readonly sameFileSet: boolean;
|
|
30
|
+
/** Current fingerprint per parsed-or-reused path, so COMMIT need not re-hash. */
|
|
31
|
+
readonly fingerprints: ReadonlyMap<string, FileFingerprint>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Build the plan.
|
|
35
|
+
*
|
|
36
|
+
* `previous` is the previous generation's `files.jsonl` (empty on a first build) and
|
|
37
|
+
* `fingerprints` is this run's content hash per candidate. A file is reused only when
|
|
38
|
+
* both its hash AND its size match, because a truncated read (hash of a shorter buffer)
|
|
39
|
+
* must never be mistaken for an unchanged file.
|
|
40
|
+
*/
|
|
41
|
+
export declare function plan(options: {
|
|
42
|
+
readonly files: readonly DiscoveredFile[];
|
|
43
|
+
readonly fingerprints: readonly FileFingerprint[];
|
|
44
|
+
readonly previous: readonly FileRow[];
|
|
45
|
+
readonly maxFiles: number;
|
|
46
|
+
}): PlanResult;
|
|
47
|
+
/** Total bytes PLAN expects PARSE to read, for the "over budget" degradation check. */
|
|
48
|
+
export declare function plannedBytes(files: readonly DiscoveredFile[]): number;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stage 2 PLAN (spec 5.3): decide what to parse and what to reuse.
|
|
3
|
+
*
|
|
4
|
+
* The whole point of this stage is the one number the panel shows next to PLAN:
|
|
5
|
+
* `812 changed - 11,619 reused`. It compares the CONTENT HASH of each candidate against
|
|
6
|
+
* the previous generation's `files.jsonl`, which is what makes an incremental rebuild
|
|
7
|
+
* cost a fraction of a cold one.
|
|
8
|
+
*
|
|
9
|
+
* WHY A HASH AND NOT THE SIZE: a same-length edit is invisible to a size comparison, and
|
|
10
|
+
* a plan that trusts size turns the incremental path into a permanent no-op - the failure
|
|
11
|
+
* the design nails shut in §13.4 (the pointer never advances, so the stale hint never
|
|
12
|
+
* clears). `fingerprint.ts` reads and hashes the candidates once; PARSE then re-reads only
|
|
13
|
+
* the ones this stage marks changed.
|
|
14
|
+
*
|
|
15
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
16
|
+
*/
|
|
17
|
+
import { tooLargeError } from './errors.js';
|
|
18
|
+
/**
|
|
19
|
+
* Build the plan.
|
|
20
|
+
*
|
|
21
|
+
* `previous` is the previous generation's `files.jsonl` (empty on a first build) and
|
|
22
|
+
* `fingerprints` is this run's content hash per candidate. A file is reused only when
|
|
23
|
+
* both its hash AND its size match, because a truncated read (hash of a shorter buffer)
|
|
24
|
+
* must never be mistaken for an unchanged file.
|
|
25
|
+
*/
|
|
26
|
+
export function plan(options) {
|
|
27
|
+
const { files, fingerprints, previous, maxFiles } = options;
|
|
28
|
+
if (files.length > maxFiles)
|
|
29
|
+
throw tooLargeError(files.length, maxFiles);
|
|
30
|
+
const previousByPath = new Map();
|
|
31
|
+
for (const row of previous)
|
|
32
|
+
previousByPath.set(row.p, row);
|
|
33
|
+
const fingerprintByPath = new Map();
|
|
34
|
+
for (const entry of fingerprints)
|
|
35
|
+
fingerprintByPath.set(entry.path, entry);
|
|
36
|
+
const toParse = [];
|
|
37
|
+
const toReuse = [];
|
|
38
|
+
const presentPaths = new Set();
|
|
39
|
+
for (const file of files) {
|
|
40
|
+
presentPaths.add(file.path);
|
|
41
|
+
const prior = previousByPath.get(file.path);
|
|
42
|
+
const fingerprint = fingerprintByPath.get(file.path);
|
|
43
|
+
if (prior && fingerprint && isReusable(prior, fingerprint)) {
|
|
44
|
+
toReuse.push(file.path);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
toParse.push(file);
|
|
48
|
+
}
|
|
49
|
+
const removed = [];
|
|
50
|
+
for (const row of previous) {
|
|
51
|
+
if (!presentPaths.has(row.p))
|
|
52
|
+
removed.push(row.p);
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
toParse,
|
|
56
|
+
toReuse,
|
|
57
|
+
removed,
|
|
58
|
+
sameFileSet: removed.length === 0 && toParse.length === 0,
|
|
59
|
+
fingerprints: fingerprintByPath,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A previous row may be reused when the content hash and the size both match, and the
|
|
64
|
+
* prior row is not a failure. A file that previously failed to parse is always retried:
|
|
65
|
+
* the failure may have been transient (a lock, a permission blip), and carrying it
|
|
66
|
+
* forward would make one bad afternoon permanent.
|
|
67
|
+
*/
|
|
68
|
+
function isReusable(prior, fingerprint) {
|
|
69
|
+
if (fingerprint.unreadable)
|
|
70
|
+
return false;
|
|
71
|
+
if (fingerprint.sha1 === '')
|
|
72
|
+
return false;
|
|
73
|
+
if (prior.s === 'error' || prior.s === 'timeout' || prior.s === 'large')
|
|
74
|
+
return false;
|
|
75
|
+
return prior.h === fingerprint.sha1 && prior.b === fingerprint.bytes;
|
|
76
|
+
}
|
|
77
|
+
/** Total bytes PLAN expects PARSE to read, for the "over budget" degradation check. */
|
|
78
|
+
export function plannedBytes(files) {
|
|
79
|
+
return files.reduce((sum, file) => sum + file.bytes, 0);
|
|
80
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PageRank over the file import graph (spec 6.2 `rank.ts`, damping 0.85, 20 rounds
|
|
3
|
+
* or 1e-6 convergence).
|
|
4
|
+
*
|
|
5
|
+
* WHY IT EARNS ITS PLACE: the L0 block can only show a handful of areas, and the
|
|
6
|
+
* `map` query can only show a handful of files. Ranking by import fan-in alone
|
|
7
|
+
* would put a tiny shared `types.ts` on top of every list, because everything
|
|
8
|
+
* imports it. PageRank weights an import by HOW IMPORTANT the importer is, which
|
|
9
|
+
* surfaces the files a reader should actually open first.
|
|
10
|
+
*
|
|
11
|
+
* The computation is deterministic: nodes are sorted by id before iterating, so
|
|
12
|
+
* two runs over the same edges produce byte-identical scores and the L0 block
|
|
13
|
+
* stays stable for a given generation (spec I-4).
|
|
14
|
+
*
|
|
15
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
16
|
+
*/
|
|
17
|
+
import type { FileGraph, FileRow, GraphEdge, ImportRow } from './types.js';
|
|
18
|
+
/** Build the edge list from resolved imports, collapsing duplicate edges into a weight. */
|
|
19
|
+
export declare function buildEdges(imports: readonly ImportRow[]): GraphEdge[];
|
|
20
|
+
/**
|
|
21
|
+
* PageRank with a uniform restart distribution over the node set.
|
|
22
|
+
*
|
|
23
|
+
* Dangling nodes (files that import nothing) redistribute their mass evenly, so
|
|
24
|
+
* the score vector always sums to 1 and no node can reach 0 by construction.
|
|
25
|
+
*/
|
|
26
|
+
export declare function pageRank(ids: readonly string[], edges: readonly GraphEdge[], options?: {
|
|
27
|
+
readonly damping?: number;
|
|
28
|
+
readonly iterations?: number;
|
|
29
|
+
readonly epsilon?: number;
|
|
30
|
+
}): Map<string, number>;
|
|
31
|
+
/**
|
|
32
|
+
* Build `graph.json`: nodes (with rank and area) sorted by rank descending, and
|
|
33
|
+
* the edge list. Ties break on id so the file is stable across runs.
|
|
34
|
+
*/
|
|
35
|
+
export declare function buildGraph(files: readonly FileRow[], imports: readonly ImportRow[]): FileGraph;
|
|
36
|
+
/** Attach the file rank to each symbol, so a symbol query can sort by importance. */
|
|
37
|
+
export declare function rankSymbols<T extends {
|
|
38
|
+
readonly f: string;
|
|
39
|
+
readonly r: number;
|
|
40
|
+
}>(symbols: readonly T[], ranks: ReadonlyMap<string, number>): T[];
|
|
41
|
+
/** Ranks by file from a built graph, for the symbol pass above. */
|
|
42
|
+
export declare function rankMap(graph: FileGraph): Map<string, number>;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PageRank over the file import graph (spec 6.2 `rank.ts`, damping 0.85, 20 rounds
|
|
3
|
+
* or 1e-6 convergence).
|
|
4
|
+
*
|
|
5
|
+
* WHY IT EARNS ITS PLACE: the L0 block can only show a handful of areas, and the
|
|
6
|
+
* `map` query can only show a handful of files. Ranking by import fan-in alone
|
|
7
|
+
* would put a tiny shared `types.ts` on top of every list, because everything
|
|
8
|
+
* imports it. PageRank weights an import by HOW IMPORTANT the importer is, which
|
|
9
|
+
* surfaces the files a reader should actually open first.
|
|
10
|
+
*
|
|
11
|
+
* The computation is deterministic: nodes are sorted by id before iterating, so
|
|
12
|
+
* two runs over the same edges produce byte-identical scores and the L0 block
|
|
13
|
+
* stays stable for a given generation (spec I-4).
|
|
14
|
+
*
|
|
15
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
16
|
+
*/
|
|
17
|
+
import { RANK_DAMPING, RANK_EPSILON, RANK_ITERATIONS } from './limits.js';
|
|
18
|
+
import { areaOf } from './paths.js';
|
|
19
|
+
/** Build the edge list from resolved imports, collapsing duplicate edges into a weight. */
|
|
20
|
+
export function buildEdges(imports) {
|
|
21
|
+
const weights = new Map();
|
|
22
|
+
for (const row of imports) {
|
|
23
|
+
if (!row.tp || row.tp === row.f)
|
|
24
|
+
continue;
|
|
25
|
+
const key = `${row.f}\u0000${row.tp}`;
|
|
26
|
+
weights.set(key, (weights.get(key) ?? 0) + 1);
|
|
27
|
+
}
|
|
28
|
+
const edges = [];
|
|
29
|
+
for (const [key, weight] of weights) {
|
|
30
|
+
const [from, to] = key.split('\u0000');
|
|
31
|
+
edges.push({ from, to, w: weight });
|
|
32
|
+
}
|
|
33
|
+
edges.sort((a, b) => (a.from === b.from ? (a.to < b.to ? -1 : a.to > b.to ? 1 : 0) : a.from < b.from ? -1 : 1));
|
|
34
|
+
return edges;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* PageRank with a uniform restart distribution over the node set.
|
|
38
|
+
*
|
|
39
|
+
* Dangling nodes (files that import nothing) redistribute their mass evenly, so
|
|
40
|
+
* the score vector always sums to 1 and no node can reach 0 by construction.
|
|
41
|
+
*/
|
|
42
|
+
export function pageRank(ids, edges, options = {}) {
|
|
43
|
+
const damping = options.damping ?? RANK_DAMPING;
|
|
44
|
+
const iterations = options.iterations ?? RANK_ITERATIONS;
|
|
45
|
+
const epsilon = options.epsilon ?? RANK_EPSILON;
|
|
46
|
+
const sorted = [...ids].sort();
|
|
47
|
+
const n = sorted.length;
|
|
48
|
+
const scores = new Map();
|
|
49
|
+
if (n === 0)
|
|
50
|
+
return scores;
|
|
51
|
+
const initial = 1 / n;
|
|
52
|
+
for (const id of sorted)
|
|
53
|
+
scores.set(id, initial);
|
|
54
|
+
const outgoing = new Map();
|
|
55
|
+
for (const edge of edges) {
|
|
56
|
+
const list = outgoing.get(edge.from) ?? [];
|
|
57
|
+
list.push(edge);
|
|
58
|
+
outgoing.set(edge.from, list);
|
|
59
|
+
}
|
|
60
|
+
for (let round = 0; round < iterations; round += 1) {
|
|
61
|
+
const next = new Map();
|
|
62
|
+
let danglingMass = 0;
|
|
63
|
+
for (const id of sorted) {
|
|
64
|
+
const list = outgoing.get(id);
|
|
65
|
+
if (!list || list.length === 0)
|
|
66
|
+
danglingMass += scores.get(id) ?? 0;
|
|
67
|
+
}
|
|
68
|
+
const base = (1 - damping) / n + (damping * danglingMass) / n;
|
|
69
|
+
for (const id of sorted)
|
|
70
|
+
next.set(id, base);
|
|
71
|
+
for (const id of sorted) {
|
|
72
|
+
const list = outgoing.get(id) ?? [];
|
|
73
|
+
if (list.length === 0)
|
|
74
|
+
continue;
|
|
75
|
+
const total = list.reduce((sum, edge) => sum + edge.w, 0);
|
|
76
|
+
const score = scores.get(id) ?? 0;
|
|
77
|
+
for (const edge of list) {
|
|
78
|
+
const share = total === 0 ? 0 : (edge.w / total) * score * damping;
|
|
79
|
+
next.set(edge.to, (next.get(edge.to) ?? 0) + share);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
let delta = 0;
|
|
83
|
+
for (const id of sorted)
|
|
84
|
+
delta += Math.abs((next.get(id) ?? 0) - (scores.get(id) ?? 0));
|
|
85
|
+
for (const [id, value] of next)
|
|
86
|
+
scores.set(id, value);
|
|
87
|
+
if (delta < epsilon)
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
return scores;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Build `graph.json`: nodes (with rank and area) sorted by rank descending, and
|
|
94
|
+
* the edge list. Ties break on id so the file is stable across runs.
|
|
95
|
+
*/
|
|
96
|
+
export function buildGraph(files, imports) {
|
|
97
|
+
const ids = files.map((file) => file.p);
|
|
98
|
+
const edges = buildEdges(imports);
|
|
99
|
+
const scores = pageRank(ids, edges);
|
|
100
|
+
const nodes = ids.map((id) => ({
|
|
101
|
+
id,
|
|
102
|
+
rank: round6(scores.get(id) ?? 0),
|
|
103
|
+
area: areaOf(id),
|
|
104
|
+
}));
|
|
105
|
+
nodes.sort((a, b) => (b.rank === a.rank ? (a.id < b.id ? -1 : 1) : b.rank - a.rank));
|
|
106
|
+
return { nodes, edges };
|
|
107
|
+
}
|
|
108
|
+
/** Six decimals is the precision the L0 block and the queries ever display. */
|
|
109
|
+
function round6(value) {
|
|
110
|
+
return Math.round(value * 1e6) / 1e6;
|
|
111
|
+
}
|
|
112
|
+
/** Attach the file rank to each symbol, so a symbol query can sort by importance. */
|
|
113
|
+
export function rankSymbols(symbols, ranks) {
|
|
114
|
+
return symbols.map((symbol) => {
|
|
115
|
+
const rank = ranks.get(symbol.f) ?? 0;
|
|
116
|
+
return rank === symbol.r ? symbol : { ...symbol, r: rank };
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/** Ranks by file from a built graph, for the symbol pass above. */
|
|
120
|
+
export function rankMap(graph) {
|
|
121
|
+
const map = new Map();
|
|
122
|
+
for (const node of graph.nodes)
|
|
123
|
+
map.set(node.id, node.rank);
|
|
124
|
+
return map;
|
|
125
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-process memoization for generation reads (spec 6.2 `read/cache.ts`).
|
|
3
|
+
*
|
|
4
|
+
* WHY: `code_query` is called several times in one agent turn, and every call
|
|
5
|
+
* would otherwise re-read tens of megabytes of JSONL. The cache is keyed by the
|
|
6
|
+
* `current` pointer's generation number, so a rebuild invalidates it by
|
|
7
|
+
* construction - there is no expiry timer to get wrong, and a stale generation is
|
|
8
|
+
* impossible because the key names the exact generation.
|
|
9
|
+
*
|
|
10
|
+
* The cache is per-process and holds at most ONE generation: an agent session
|
|
11
|
+
* only ever queries the current index, so an LRU of size one is the whole need.
|
|
12
|
+
*
|
|
13
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
14
|
+
*/
|
|
15
|
+
import type { IndexPaths } from '../paths.js';
|
|
16
|
+
import { type LoadedGeneration } from '../store.js';
|
|
17
|
+
/**
|
|
18
|
+
* Load the current generation, reusing the previous load when the pointer did not
|
|
19
|
+
* move. Returns null when there is no index (callers turn that into a sentence,
|
|
20
|
+
* never into an exception: `code_query` on an unbuilt index is a normal answer).
|
|
21
|
+
*/
|
|
22
|
+
export declare function loadCached(paths: IndexPaths): LoadedGeneration | null;
|
|
23
|
+
/** Drop the slot. Called after a build so the next query sees the new generation. */
|
|
24
|
+
export declare function invalidateCache(): void;
|
|
25
|
+
/** Number of real disk loads since import; the cache test asserts this stays at 1. */
|
|
26
|
+
export declare function cacheLoadCount(): number;
|
|
27
|
+
/** Reset the counter (tests only). */
|
|
28
|
+
export declare function resetCacheStats(): void;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-process memoization for generation reads (spec 6.2 `read/cache.ts`).
|
|
3
|
+
*
|
|
4
|
+
* WHY: `code_query` is called several times in one agent turn, and every call
|
|
5
|
+
* would otherwise re-read tens of megabytes of JSONL. The cache is keyed by the
|
|
6
|
+
* `current` pointer's generation number, so a rebuild invalidates it by
|
|
7
|
+
* construction - there is no expiry timer to get wrong, and a stale generation is
|
|
8
|
+
* impossible because the key names the exact generation.
|
|
9
|
+
*
|
|
10
|
+
* The cache is per-process and holds at most ONE generation: an agent session
|
|
11
|
+
* only ever queries the current index, so an LRU of size one is the whole need.
|
|
12
|
+
*
|
|
13
|
+
* ASCII ONLY: inside the glyph scanner's scope.
|
|
14
|
+
*/
|
|
15
|
+
import { loadCurrent, readCurrentPointer } from '../store.js';
|
|
16
|
+
/** The single slot, module-level because a process has one current index. */
|
|
17
|
+
let slot;
|
|
18
|
+
/** How many loads happened, exposed for the tests that assert the cache works. */
|
|
19
|
+
let loadCount = 0;
|
|
20
|
+
/**
|
|
21
|
+
* Load the current generation, reusing the previous load when the pointer did not
|
|
22
|
+
* move. Returns null when there is no index (callers turn that into a sentence,
|
|
23
|
+
* never into an exception: `code_query` on an unbuilt index is a normal answer).
|
|
24
|
+
*/
|
|
25
|
+
export function loadCached(paths) {
|
|
26
|
+
const pointer = readCurrentPointer(paths);
|
|
27
|
+
if (!pointer) {
|
|
28
|
+
slot = undefined;
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
if (slot && slot.generation === pointer.generation)
|
|
32
|
+
return slot.value;
|
|
33
|
+
const loaded = loadCurrent(paths);
|
|
34
|
+
if (!loaded) {
|
|
35
|
+
slot = undefined;
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
loadCount += 1;
|
|
39
|
+
slot = { generation: pointer.generation, value: loaded };
|
|
40
|
+
return loaded;
|
|
41
|
+
}
|
|
42
|
+
/** Drop the slot. Called after a build so the next query sees the new generation. */
|
|
43
|
+
export function invalidateCache() {
|
|
44
|
+
slot = undefined;
|
|
45
|
+
}
|
|
46
|
+
/** Number of real disk loads since import; the cache test asserts this stays at 1. */
|
|
47
|
+
export function cacheLoadCount() {
|
|
48
|
+
return loadCount;
|
|
49
|
+
}
|
|
50
|
+
/** Reset the counter (tests only). */
|
|
51
|
+
export function resetCacheStats() {
|
|
52
|
+
loadCount = 0;
|
|
53
|
+
slot = undefined;
|
|
54
|
+
}
|