@myrialabs/clopen 0.4.37 → 0.4.39
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 +2 -2
- package/backend/auth/permissions.ts +39 -0
- package/backend/bootstrap.ts +16 -0
- package/backend/chat/stream-manager.ts +1 -1
- package/backend/database/crypto/envelope.test.ts +93 -0
- package/backend/database/crypto/envelope.ts +127 -0
- package/backend/database/crypto/index.ts +22 -0
- package/backend/database/crypto/master-key.ts +129 -0
- package/backend/database/crypto/secret-columns.ts +151 -0
- package/backend/database/migrations/071_create_notes_tables.ts +90 -0
- package/backend/database/migrations/072_encrypt_stored_secrets.ts +107 -0
- package/backend/database/migrations/073_create_integration_tables.ts +119 -0
- package/backend/database/migrations/074_create_work_tables.ts +94 -0
- package/backend/database/migrations/075_create_deploy_bindings.ts +54 -0
- package/backend/database/migrations/076_remove_memory_code_graph.ts +229 -0
- package/backend/database/migrations/077_create_integration_db_links.ts +150 -0
- package/backend/database/migrations/index.ts +49 -0
- package/backend/database/queries/db-client-connection-queries.ts +42 -12
- package/backend/database/queries/deploy-queries.ts +124 -0
- package/backend/database/queries/engine-queries.ts +23 -10
- package/backend/database/queries/graph-queries.ts +291 -247
- package/backend/database/queries/index.ts +10 -1
- package/backend/database/queries/integration-db-link-queries.ts +168 -0
- package/backend/database/queries/integration-queries.ts +325 -0
- package/backend/database/queries/mcp-server-queries.ts +38 -11
- package/backend/database/queries/note-queries.test.ts +190 -0
- package/backend/database/queries/note-queries.ts +269 -0
- package/backend/database/queries/profile-queries.ts +1 -1
- package/backend/database/queries/ssh-connection-queries.ts +19 -8
- package/backend/database/queries/work-queries.ts +210 -0
- package/backend/db-client/integrations/index.ts +37 -0
- package/backend/db-client/integrations/links.ts +533 -0
- package/backend/db-client/integrations/projector.test.ts +280 -0
- package/backend/db-client/integrations/projector.ts +256 -0
- package/backend/db-client/integrations/supabase-service.ts +266 -0
- package/backend/db-client/providers/registry.ts +27 -0
- package/backend/db-client/providers/supabase/adapter.ts +500 -0
- package/backend/db-client/providers/supabase/api-types.ts +92 -0
- package/backend/db-client/providers/supabase/apply-migration.ts +104 -0
- package/backend/db-client/providers/supabase/client.ts +259 -0
- package/backend/db-client/providers/supabase/features.ts +502 -0
- package/backend/db-client/providers/supabase/local.test.ts +78 -0
- package/backend/db-client/providers/supabase/local.ts +137 -0
- package/backend/db-client/providers/types.ts +137 -0
- package/backend/deployments/bindings.test.ts +43 -0
- package/backend/deployments/bindings.ts +216 -0
- package/backend/deployments/index.ts +53 -0
- package/backend/deployments/log-streams.ts +214 -0
- package/backend/deployments/prompts.ts +70 -0
- package/backend/deployments/providers/vercel/adapter.ts +757 -0
- package/backend/deployments/providers/vercel/build-logs.test.ts +80 -0
- package/backend/deployments/providers/vercel/build-logs.ts +211 -0
- package/backend/deployments/providers/vercel/client.ts +203 -0
- package/backend/deployments/providers/vercel/list.test.ts +624 -0
- package/backend/deployments/providers/vercel/normalise.test.ts +115 -0
- package/backend/deployments/providers/vercel/normalise.ts +113 -0
- package/backend/deployments/providers/vercel/types.ts +104 -0
- package/backend/deployments/registry.ts +27 -0
- package/backend/deployments/service.ts +458 -0
- package/backend/deployments/types.ts +278 -0
- package/backend/engine/docs/artifacts.md +22 -9
- package/backend/files/os-clipboard.test.ts +209 -0
- package/backend/files/os-clipboard.ts +567 -0
- package/backend/http/integration-hooks.ts +42 -0
- package/backend/http/notes-images.test.ts +228 -0
- package/backend/http/notes-images.ts +196 -0
- package/backend/index.ts +13 -0
- package/backend/integrations/accounts.ts +229 -0
- package/backend/integrations/health.ts +146 -0
- package/backend/integrations/index.ts +36 -0
- package/backend/integrations/projections/index.ts +0 -0
- package/backend/integrations/projections/mcp-projector.test.ts +204 -0
- package/backend/integrations/projections/mcp-projector.ts +163 -0
- package/backend/integrations/projections/types.ts +63 -0
- package/backend/integrations/registry/define.ts +38 -0
- package/backend/integrations/registry/index.ts +60 -0
- package/backend/integrations/registry/providers/context7.ts +44 -0
- package/backend/integrations/registry/providers/firecrawl.ts +41 -0
- package/backend/integrations/registry/providers/github.ts +78 -0
- package/backend/integrations/registry/providers/supabase.ts +61 -0
- package/backend/integrations/registry/providers/vercel.ts +46 -0
- package/backend/integrations/registry/types.ts +96 -0
- package/backend/integrations/webhooks/gateway.ts +160 -0
- package/backend/integrations/webhooks/verify.ts +65 -0
- package/backend/mcp/README.md +25 -3
- package/backend/mcp/external/config.ts +35 -1
- package/backend/mcp/index.ts +1 -1
- package/backend/mcp/internal/config.ts +1 -1
- package/backend/mcp/internal/servers/memory-graph/description.ts +3 -3
- package/backend/mcp/internal/servers/memory-graph/runner.ts +4 -9
- package/backend/mcp/internal/servers/memory-graph/schema.ts +0 -4
- package/backend/memory/README.md +57 -34
- package/backend/memory/compose.test.ts +3 -3
- package/backend/memory/compose.ts +6 -9
- package/backend/memory/config.ts +0 -5
- package/backend/memory/consolidate.ts +1 -2
- package/backend/memory/context.test.ts +8 -54
- package/backend/memory/context.ts +19 -25
- package/backend/memory/extract/episodic.ts +10 -12
- package/backend/memory/extract/index.ts +23 -52
- package/backend/memory/extract/scheduler.ts +3 -6
- package/backend/memory/index.ts +6 -5
- package/backend/memory/invalidate.ts +36 -48
- package/backend/memory/layout.ts +319 -130
- package/backend/memory/retention.ts +12 -42
- package/backend/memory/retrieval.eval.test.ts +2 -2
- package/backend/memory/retrieval.test.ts +228 -183
- package/backend/memory/retrieval.ts +22 -21
- package/backend/memory/revise.test.ts +62 -116
- package/backend/memory/revise.ts +0 -1
- package/backend/memory/view.ts +8 -5
- package/backend/notes/access.test.ts +82 -0
- package/backend/notes/access.ts +27 -0
- package/backend/notes/storage.test.ts +77 -0
- package/backend/notes/storage.ts +114 -0
- package/backend/permissions/service.ts +2 -2
- package/backend/profiles/service.ts +1 -1
- package/backend/projects/project-cleanup.ts +6 -0
- package/backend/snapshot/helpers.ts +35 -41
- package/backend/snapshot/snapshot-service.ts +90 -0
- package/backend/snapshot/turn-changes.test.ts +163 -0
- package/backend/snapshot/turn-changes.ts +131 -0
- package/backend/work/bindings.ts +124 -0
- package/backend/work/index.ts +42 -0
- package/backend/work/prompts.ts +150 -0
- package/backend/work/providers/github/actions-logs.ts +180 -0
- package/backend/work/providers/github/adapter.test.ts +88 -0
- package/backend/work/providers/github/adapter.ts +1255 -0
- package/backend/work/providers/github/client.ts +280 -0
- package/backend/work/providers/github/list-range.test.ts +158 -0
- package/backend/work/providers/github/token-kind.test.ts +42 -0
- package/backend/work/pull-requests.ts +242 -0
- package/backend/work/registry.ts +27 -0
- package/backend/work/service.ts +436 -0
- package/backend/work/start-work.test.ts +71 -0
- package/backend/work/start-work.ts +232 -0
- package/backend/work/types.ts +180 -0
- package/backend/ws/db-client/accounts.ts +225 -0
- package/backend/ws/db-client/connections.ts +34 -3
- package/backend/ws/db-client/context.ts +37 -0
- package/backend/ws/db-client/index.ts +20 -1
- package/backend/ws/db-client/supabase.ts +188 -0
- package/backend/ws/deployments/actions.ts +360 -0
- package/backend/ws/deployments/context.ts +43 -0
- package/backend/ws/deployments/crud.ts +152 -0
- package/backend/ws/deployments/index.ts +15 -0
- package/backend/ws/deployments/schemas.ts +183 -0
- package/backend/ws/files/clipboard.ts +78 -0
- package/backend/ws/files/index.ts +5 -0
- package/backend/ws/git/diff-budget.ts +70 -11
- package/backend/ws/index.ts +16 -2
- package/backend/ws/integrations/crud.ts +219 -0
- package/backend/ws/integrations/index.ts +22 -0
- package/backend/ws/mcp/crud.ts +25 -1
- package/backend/ws/memory/crud.ts +4 -16
- package/backend/ws/notes/index.ts +278 -0
- package/backend/ws/profiles/crud.ts +1 -1
- package/backend/ws/snapshot/changes.ts +279 -0
- package/backend/ws/snapshot/index.ts +4 -3
- package/backend/ws/work/actions.ts +206 -0
- package/backend/ws/work/context.ts +51 -0
- package/backend/ws/work/crud.ts +347 -0
- package/backend/ws/work/index.ts +17 -0
- package/backend/ws/work/pull-requests.ts +177 -0
- package/backend/ws/work/schemas.ts +226 -0
- package/dist/assets/{index-sJmS_CCm.js → index-BrF6nqld.js} +1 -1
- package/dist/assets/{index-Bik9TCBi.js → index-C8Gi-b6U.js} +1 -1
- package/dist/assets/main-Bd3gb79F.css +1 -0
- package/dist/assets/main-Bo1hD7mt.js +1191 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/constants/engine-tools.ts +1 -1
- package/shared/constants/tool-icons.ts +42 -0
- package/shared/types/database/schema.ts +41 -0
- package/shared/types/db-client/connection.ts +6 -0
- package/shared/types/db-client/index.ts +2 -0
- package/shared/types/db-client/integrations.ts +174 -0
- package/shared/types/db-client/supabase.ts +131 -0
- package/shared/types/deployments.ts +382 -0
- package/shared/types/integrations.ts +140 -0
- package/shared/types/memory.ts +34 -51
- package/shared/types/stores/settings.ts +8 -0
- package/shared/types/ui/components.ts +1 -1
- package/shared/types/work.ts +426 -0
- package/shared/utils/diff-calculator.test.ts +89 -0
- package/shared/utils/diff-calculator.ts +30 -65
- package/shared/utils/logger.ts +4 -0
- package/backend/memory/extract/languages.ts +0 -176
- package/backend/memory/extract/structural.test.ts +0 -215
- package/backend/memory/extract/structural.ts +0 -364
- package/backend/ws/snapshot/content.ts +0 -85
- package/dist/assets/main-DLqolBI2.css +0 -1
- package/dist/assets/main-Df1I3hnw.js +0 -1096
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Structural extraction — the codebase as graph nodes.
|
|
3
|
-
*
|
|
4
|
-
* The input is the set of files that CHANGED ON DISK during a turn, taken from
|
|
5
|
-
* the snapshot service. That source matters more than it looks: snapshots are a
|
|
6
|
-
* gitignore-aware disk diff against the session baseline
|
|
7
|
-
* (`snapshot-service.ts:166` — "the source of truth is the DISK"), so a file is
|
|
8
|
-
* seen no matter how it was edited.
|
|
9
|
-
*
|
|
10
|
-
* Deriving this from tool calls instead would have been wrong. Agents rewrite
|
|
11
|
-
* files through `Bash` constantly — `sed -i`, a codemod script, `git checkout`,
|
|
12
|
-
* a formatter, a build step — and none of that appears as an Edit or Write tool
|
|
13
|
-
* call. Watching the disk sees all of it and needs no per-engine knowledge of
|
|
14
|
-
* tool names, which also means every engine gets identical behaviour for free.
|
|
15
|
-
*
|
|
16
|
-
* Only changed files are read. There is no repository-wide indexing pass, so the
|
|
17
|
-
* cost is bounded by how much the turn actually touched, and the graph grows to
|
|
18
|
-
* describe the parts of the codebase that are genuinely being worked on.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
import { readFile, stat } from 'node:fs/promises';
|
|
22
|
-
import { basename, join, posix, relative, resolve } from 'node:path';
|
|
23
|
-
import { graphQueries } from '$backend/database/queries/graph-queries';
|
|
24
|
-
import { debug } from '$shared/utils/logger';
|
|
25
|
-
import type { GraphNode } from '$shared/types/memory';
|
|
26
|
-
import { detectLanguage, extractDefinitions, extractImports, isParseable, RESOLVE_EXTENSIONS } from './languages';
|
|
27
|
-
|
|
28
|
-
/** Files parsed per turn. A large refactor is summarised, not exhaustively mapped. */
|
|
29
|
-
const MAX_FILES_PER_TURN = 60;
|
|
30
|
-
|
|
31
|
-
/** Source files above this are skipped — bundles and generated blobs teach nothing. */
|
|
32
|
-
const MAX_FILE_BYTES = 400_000;
|
|
33
|
-
|
|
34
|
-
/** Symbol nodes created per file, so a barrel export cannot flood the graph. */
|
|
35
|
-
const MAX_SYMBOLS_PER_FILE = 25;
|
|
36
|
-
|
|
37
|
-
/** Paths that describe machinery rather than the project's own design. */
|
|
38
|
-
const IGNORED_SEGMENTS = new Set([
|
|
39
|
-
'node_modules',
|
|
40
|
-
'dist',
|
|
41
|
-
'build',
|
|
42
|
-
'.git',
|
|
43
|
-
'.svelte-kit',
|
|
44
|
-
'coverage',
|
|
45
|
-
'vendor',
|
|
46
|
-
'__pycache__',
|
|
47
|
-
'target',
|
|
48
|
-
'.next',
|
|
49
|
-
'.cache'
|
|
50
|
-
]);
|
|
51
|
-
|
|
52
|
-
export interface StructuralIngestInput {
|
|
53
|
-
projectId: string;
|
|
54
|
-
projectPath: string;
|
|
55
|
-
sessionId: string | null;
|
|
56
|
-
/** Repo-relative paths that changed on disk this turn. */
|
|
57
|
-
changedPaths: string[];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Everything one file contributes, resolved off disk before any row is written.
|
|
62
|
-
*
|
|
63
|
-
* The two phases are separated on purpose. Parsing is async (`readFile`, `stat`)
|
|
64
|
-
* and SQLite transactions are synchronous, so interleaving them would mean either
|
|
65
|
-
* no transaction at all — which is what this originally did, at a cost of several
|
|
66
|
-
* thousand individually-durable statements on a busy turn — or a transaction held
|
|
67
|
-
* open across every file read on the machine.
|
|
68
|
-
*/
|
|
69
|
-
interface ParsedFile {
|
|
70
|
-
path: string;
|
|
71
|
-
language: string | null;
|
|
72
|
-
/** Repo-relative paths this file imports, already resolved to real files. */
|
|
73
|
-
importsLocal: string[];
|
|
74
|
-
/** npm package names this file imports. */
|
|
75
|
-
importsExternal: string[];
|
|
76
|
-
definitions: string[];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export interface StructuralIngestResult {
|
|
80
|
-
files: number;
|
|
81
|
-
symbols: number;
|
|
82
|
-
edges: number;
|
|
83
|
-
/** Path → node id, so episodic extraction can attach `about` edges. */
|
|
84
|
-
fileNodes: Map<string, string>;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function isIgnored(path: string): boolean {
|
|
88
|
-
return path.split(/[/\\]/).some(segment => IGNORED_SEGMENTS.has(segment));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Normalize to forward slashes so Windows and POSIX produce the same digest. */
|
|
92
|
-
function normalize(path: string): string {
|
|
93
|
-
return path.replace(/\\/g, '/');
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Resolve an import specifier to a repo-relative path, or null when it points
|
|
98
|
-
* outside the project (a package, a stdlib module, an unresolvable alias).
|
|
99
|
-
*
|
|
100
|
-
* Only relative specifiers are resolved. TypeScript path aliases (`$backend/…`)
|
|
101
|
-
* would need tsconfig parsing per project and are deliberately left alone: a
|
|
102
|
-
* wrong edge is worse than a missing one, because it invents a dependency the
|
|
103
|
-
* codebase does not have.
|
|
104
|
-
*/
|
|
105
|
-
async function resolveImport(
|
|
106
|
-
fromPath: string,
|
|
107
|
-
specifier: string,
|
|
108
|
-
projectPath: string
|
|
109
|
-
): Promise<string | null> {
|
|
110
|
-
if (!specifier.startsWith('.')) return null;
|
|
111
|
-
|
|
112
|
-
const baseDir = posix.dirname(normalize(fromPath));
|
|
113
|
-
const joined = posix.normalize(posix.join(baseDir, specifier));
|
|
114
|
-
|
|
115
|
-
// Escaping the project root means it is not ours to describe.
|
|
116
|
-
if (joined.startsWith('..')) return null;
|
|
117
|
-
|
|
118
|
-
const candidates = [
|
|
119
|
-
joined,
|
|
120
|
-
...RESOLVE_EXTENSIONS.map(ext => `${joined}${ext}`),
|
|
121
|
-
...RESOLVE_EXTENSIONS.map(ext => posix.join(joined, `index${ext}`))
|
|
122
|
-
];
|
|
123
|
-
|
|
124
|
-
for (const candidate of candidates) {
|
|
125
|
-
// Strip any query/hash a bundler-style specifier might carry.
|
|
126
|
-
const clean = candidate.split(/[?#]/)[0];
|
|
127
|
-
const absolute = resolve(projectPath, clean);
|
|
128
|
-
// Containment check: a crafted specifier must not let this stat outside
|
|
129
|
-
// the project.
|
|
130
|
-
if (relative(projectPath, absolute).startsWith('..')) continue;
|
|
131
|
-
try {
|
|
132
|
-
const info = await stat(absolute);
|
|
133
|
-
if (info.isFile()) return clean;
|
|
134
|
-
} catch {
|
|
135
|
-
// keep trying
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/** Upsert the file node for a repo-relative path. */
|
|
142
|
-
function upsertFileNode(projectId: string, path: string, sessionId: string | null): GraphNode {
|
|
143
|
-
const normalized = normalize(path);
|
|
144
|
-
return graphQueries.upsert({
|
|
145
|
-
kind: 'structural',
|
|
146
|
-
subkind: 'file',
|
|
147
|
-
scope: 'project',
|
|
148
|
-
projectId,
|
|
149
|
-
sessionId,
|
|
150
|
-
label: basename(normalized),
|
|
151
|
-
body: normalized,
|
|
152
|
-
path: normalized,
|
|
153
|
-
language: detectLanguage(normalized),
|
|
154
|
-
// Structural facts read off disk are near-certain, but "near": the file
|
|
155
|
-
// existed when it was read and may not now.
|
|
156
|
-
confidence: 0.9,
|
|
157
|
-
source: 'agent'
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Turn a turn's disk changes into structural nodes and edges.
|
|
163
|
-
*
|
|
164
|
-
* Never throws — a file that vanished mid-turn, or an unreadable path, is
|
|
165
|
-
* skipped. This runs after the user's turn has already completed, so failing
|
|
166
|
-
* loudly would only break something the user considers finished.
|
|
167
|
-
*/
|
|
168
|
-
export async function ingestStructuralChanges(
|
|
169
|
-
input: StructuralIngestInput
|
|
170
|
-
): Promise<StructuralIngestResult> {
|
|
171
|
-
const { projectId, projectPath, sessionId } = input;
|
|
172
|
-
const result: StructuralIngestResult = { files: 0, symbols: 0, edges: 0, fileNodes: new Map() };
|
|
173
|
-
|
|
174
|
-
const paths = input.changedPaths
|
|
175
|
-
.map(normalize)
|
|
176
|
-
.filter(path => path && !isIgnored(path))
|
|
177
|
-
.slice(0, MAX_FILES_PER_TURN);
|
|
178
|
-
|
|
179
|
-
if (paths.length === 0) return result;
|
|
180
|
-
|
|
181
|
-
// ── phase 1: read and parse, off the database entirely ──────────────────
|
|
182
|
-
const parsed: ParsedFile[] = [];
|
|
183
|
-
for (const path of paths) {
|
|
184
|
-
try {
|
|
185
|
-
parsed.push(await parseFile(path, projectPath));
|
|
186
|
-
} catch (error) {
|
|
187
|
-
debug.warn('memory', `Structural extraction skipped ${path}`, error);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// ── phase 2: one transaction for every write ────────────────────────────
|
|
192
|
-
// A busy turn touching sixty files that define twenty-five symbols each is
|
|
193
|
-
// several thousand statements, and each upsert is itself a SELECT, an
|
|
194
|
-
// INSERT/UPDATE and an FTS rewrite. Outside a transaction every one of those is
|
|
195
|
-
// its own durability barrier, which made this by far the most expensive thing
|
|
196
|
-
// on the write path — for no benefit, since a partially-ingested turn is not a
|
|
197
|
-
// state anyone wants to keep anyway.
|
|
198
|
-
try {
|
|
199
|
-
graphQueries.transaction(() => {
|
|
200
|
-
for (const file of parsed) {
|
|
201
|
-
const fileNode = upsertFileNode(projectId, file.path, sessionId);
|
|
202
|
-
result.files++;
|
|
203
|
-
result.fileNodes.set(file.path, fileNode.id);
|
|
204
|
-
|
|
205
|
-
// Directory containment gives the graph its skeleton, which is what makes
|
|
206
|
-
// community detection produce meaningful "lobes" in the visualization
|
|
207
|
-
// rather than one undifferentiated cloud.
|
|
208
|
-
const parent = posix.dirname(file.path);
|
|
209
|
-
if (parent && parent !== '.' && parent !== '/') {
|
|
210
|
-
const moduleNode = graphQueries.upsert({
|
|
211
|
-
kind: 'structural',
|
|
212
|
-
subkind: 'module',
|
|
213
|
-
scope: 'project',
|
|
214
|
-
projectId,
|
|
215
|
-
label: basename(parent),
|
|
216
|
-
body: parent,
|
|
217
|
-
path: parent,
|
|
218
|
-
confidence: 0.9,
|
|
219
|
-
source: 'agent'
|
|
220
|
-
});
|
|
221
|
-
if (graphQueries.link({ srcId: moduleNode.id, dstId: fileNode.id, rel: 'contains' })) {
|
|
222
|
-
result.edges++;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
for (const target of file.importsLocal) {
|
|
227
|
-
const targetNode = upsertFileNode(projectId, target, sessionId);
|
|
228
|
-
if (graphQueries.link({ srcId: fileNode.id, dstId: targetNode.id, rel: 'imports' })) {
|
|
229
|
-
result.edges++;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// Non-relative specifiers become dependency nodes, which is what makes
|
|
234
|
-
// "what in this codebase touches puppeteer" answerable — and, across
|
|
235
|
-
// projects, "which repositories share this dependency".
|
|
236
|
-
for (const pkg of file.importsExternal) {
|
|
237
|
-
const depNode = graphQueries.upsert({
|
|
238
|
-
kind: 'structural',
|
|
239
|
-
subkind: 'dependency',
|
|
240
|
-
scope: 'global',
|
|
241
|
-
projectId: null,
|
|
242
|
-
label: pkg,
|
|
243
|
-
body: pkg,
|
|
244
|
-
confidence: 0.8,
|
|
245
|
-
source: 'agent'
|
|
246
|
-
});
|
|
247
|
-
if (graphQueries.link({ srcId: fileNode.id, dstId: depNode.id, rel: 'imports' })) {
|
|
248
|
-
result.edges++;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
for (const name of file.definitions) {
|
|
253
|
-
const symbolNode = graphQueries.upsert({
|
|
254
|
-
kind: 'structural',
|
|
255
|
-
subkind: 'symbol',
|
|
256
|
-
scope: 'project',
|
|
257
|
-
projectId,
|
|
258
|
-
sessionId,
|
|
259
|
-
label: name,
|
|
260
|
-
body: `${name} — ${file.path}`,
|
|
261
|
-
path: file.path,
|
|
262
|
-
symbol: name,
|
|
263
|
-
language: file.language,
|
|
264
|
-
// Regex-derived, so lower than a file's existence.
|
|
265
|
-
confidence: 0.6,
|
|
266
|
-
source: 'agent'
|
|
267
|
-
});
|
|
268
|
-
result.symbols++;
|
|
269
|
-
if (graphQueries.link({ srcId: fileNode.id, dstId: symbolNode.id, rel: 'defines' })) {
|
|
270
|
-
result.edges++;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
} catch (error) {
|
|
276
|
-
// The transaction rolled back, so the graph is exactly as it was. This runs
|
|
277
|
-
// after the user's turn has already completed, so failing loudly would only
|
|
278
|
-
// break something they consider finished.
|
|
279
|
-
debug.warn('memory', 'Structural ingest rolled back', error);
|
|
280
|
-
return { files: 0, symbols: 0, edges: 0, fileNodes: new Map() };
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
debug.log(
|
|
284
|
-
'memory',
|
|
285
|
-
`Structural ingest: ${result.files} file(s), ${result.symbols} symbol(s), ${result.edges} edge(s)`
|
|
286
|
-
);
|
|
287
|
-
return result;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Read one file and resolve everything the graph needs from it. Touches no
|
|
292
|
-
* database — see the two-phase note on `ParsedFile`.
|
|
293
|
-
*/
|
|
294
|
-
async function parseFile(path: string, projectPath: string): Promise<ParsedFile> {
|
|
295
|
-
const absolute = join(projectPath, path);
|
|
296
|
-
const language = detectLanguage(path);
|
|
297
|
-
|
|
298
|
-
// A file that vanished mid-turn still gets its node: the memories pointing at
|
|
299
|
-
// it remain true history, and `invalidate.ts` is what retires it once the
|
|
300
|
-
// snapshot confirms the deletion. Only the read is skipped.
|
|
301
|
-
let source: string | null = null;
|
|
302
|
-
if (isParseable(language)) {
|
|
303
|
-
try {
|
|
304
|
-
const info = await stat(absolute);
|
|
305
|
-
if (info.isFile() && info.size <= MAX_FILE_BYTES) {
|
|
306
|
-
source = await readFile(absolute, 'utf8');
|
|
307
|
-
}
|
|
308
|
-
} catch {
|
|
309
|
-
source = null;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
const parsed: ParsedFile = { path, language, importsLocal: [], importsExternal: [], definitions: [] };
|
|
314
|
-
if (!source || !language) return parsed;
|
|
315
|
-
|
|
316
|
-
for (const specifier of extractImports(source, language)) {
|
|
317
|
-
const target = await resolveImport(path, specifier, projectPath);
|
|
318
|
-
if (target) {
|
|
319
|
-
parsed.importsLocal.push(target);
|
|
320
|
-
continue;
|
|
321
|
-
}
|
|
322
|
-
const pkg = externalPackageName(specifier);
|
|
323
|
-
if (pkg) parsed.importsExternal.push(pkg);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
parsed.importsLocal = [...new Set(parsed.importsLocal)];
|
|
327
|
-
parsed.importsExternal = [...new Set(parsed.importsExternal)];
|
|
328
|
-
parsed.definitions = extractDefinitions(source, language).slice(0, MAX_SYMBOLS_PER_FILE);
|
|
329
|
-
return parsed;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/** npm package names: lowercase, no leading dot/underscore, limited punctuation. */
|
|
333
|
-
const NPM_NAME = /^[a-z0-9][a-z0-9._-]*$/;
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* Package name from a bare import specifier: `@scope/pkg/sub` → `@scope/pkg`,
|
|
337
|
-
* `lodash/merge` → `lodash`. Returns null for anything that is not actually a
|
|
338
|
-
* package.
|
|
339
|
-
*
|
|
340
|
-
* Rejecting non-packages matters more than it sounds. A build-tool path alias
|
|
341
|
-
* (`$shared/types`, `~/lib`, `@/utils`) is not a dependency, and turning one into
|
|
342
|
-
* a node invents a shared dependency between every file that uses the alias —
|
|
343
|
-
* which then distorts community detection and, because the resulting node's label
|
|
344
|
-
* is a two-token fragment, pollutes vector search as well. Aliases cannot be
|
|
345
|
-
* resolved to real files without parsing each project's build config, so they are
|
|
346
|
-
* dropped rather than guessed at.
|
|
347
|
-
*/
|
|
348
|
-
function externalPackageName(specifier: string): string | null {
|
|
349
|
-
const cleaned = specifier.replace(/^node:/, '').split(/[?#]/)[0];
|
|
350
|
-
if (!cleaned || cleaned.startsWith('/') || cleaned.startsWith('.')) return null;
|
|
351
|
-
|
|
352
|
-
const segments = cleaned.split('/');
|
|
353
|
-
if (cleaned.startsWith('@')) {
|
|
354
|
-
// A scoped package is `@scope/name`; `@/utils` has an empty scope and is an
|
|
355
|
-
// alias, not a package.
|
|
356
|
-
if (segments.length < 2) return null;
|
|
357
|
-
const scope = segments[0].slice(1);
|
|
358
|
-
if (!NPM_NAME.test(scope) || !NPM_NAME.test(segments[1])) return null;
|
|
359
|
-
return `${segments[0]}/${segments[1]}`;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
// Filters `$shared`, `~lib`, `#internal` and Windows-style absolute includes.
|
|
363
|
-
return NPM_NAME.test(segments[0]) ? segments[0] : null;
|
|
364
|
-
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Snapshot Content Handler
|
|
3
|
-
*
|
|
4
|
-
* Reads a file's content as it was *before* a given checkpoint's turn ran.
|
|
5
|
-
*
|
|
6
|
-
* The editor's AI gutter records `oldContent` per tool call, but the Write tool
|
|
7
|
-
* carries no "before" text — its input is the whole new file. Snapshots do carry
|
|
8
|
-
* it: each checkpoint stores `session_changes[path] = { oldHash, newHash }`, and
|
|
9
|
-
* `oldHash` is the blob of the file as it stood when the turn started. That is
|
|
10
|
-
* what lets the AI gutter diff a Write against something meaningful (and offer
|
|
11
|
-
* Discard on it) instead of painting the entire file as newly added.
|
|
12
|
-
*
|
|
13
|
-
* Granularity is per-turn, not per-tool: if the same file was touched more than
|
|
14
|
-
* once in the turn, the caller replays its own recorded edits on top of this
|
|
15
|
-
* content to reach the state immediately before a specific Write.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { t } from 'elysia';
|
|
19
|
-
import { createRouter } from '$shared/utils/ws-server';
|
|
20
|
-
import { isAbsolute, relative } from 'path';
|
|
21
|
-
import { sessionQueries, projectQueries, snapshotQueries } from '../../database/queries';
|
|
22
|
-
import { blobStore } from '../../snapshot/blob-store';
|
|
23
|
-
import { debug } from '$shared/utils/logger';
|
|
24
|
-
import type { SessionScopedChanges } from '$shared/types/database/schema';
|
|
25
|
-
import { requireMessageAccess } from '../access';
|
|
26
|
-
|
|
27
|
-
/** Reject blobs that aren't text — the caller only ever diffs source files. */
|
|
28
|
-
function isBinaryBuffer(buf: Buffer): boolean {
|
|
29
|
-
const limit = Math.min(buf.length, 8000);
|
|
30
|
-
for (let i = 0; i < limit; i++) {
|
|
31
|
-
if (buf[i] === 0) return true;
|
|
32
|
-
}
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export const contentHandler = createRouter()
|
|
37
|
-
.http('snapshot:read-file-before-checkpoint', {
|
|
38
|
-
data: t.Object({
|
|
39
|
-
/** Checkpoint message id — the user message that opened the turn. */
|
|
40
|
-
messageId: t.String(),
|
|
41
|
-
filePath: t.String()
|
|
42
|
-
}),
|
|
43
|
-
response: t.Object({
|
|
44
|
-
/** File text before the turn, '' when the turn created it, null when unknown. */
|
|
45
|
-
content: t.Union([t.String(), t.Null()]),
|
|
46
|
-
/** True when the file existed before the turn. */
|
|
47
|
-
existed: t.Boolean()
|
|
48
|
-
})
|
|
49
|
-
}, async ({ data, conn }) => {
|
|
50
|
-
const message = requireMessageAccess(conn, data.messageId);
|
|
51
|
-
|
|
52
|
-
const session = sessionQueries.getById(message.session_id);
|
|
53
|
-
const project = session ? projectQueries.getById(session.project_id) : null;
|
|
54
|
-
if (!project) return { content: null, existed: false };
|
|
55
|
-
|
|
56
|
-
const relativePath = (
|
|
57
|
-
isAbsolute(data.filePath) ? relative(project.path, data.filePath) : data.filePath
|
|
58
|
-
).replace(/\\/g, '/');
|
|
59
|
-
|
|
60
|
-
const snapshot = snapshotQueries.getByMessageId(data.messageId);
|
|
61
|
-
if (!snapshot?.session_changes) return { content: null, existed: false };
|
|
62
|
-
|
|
63
|
-
let changes: SessionScopedChanges;
|
|
64
|
-
try {
|
|
65
|
-
changes = JSON.parse(snapshot.session_changes as string) as SessionScopedChanges;
|
|
66
|
-
} catch {
|
|
67
|
-
return { content: null, existed: false };
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const change = changes[relativePath];
|
|
71
|
-
// No entry means the turn left the file untouched by the time the snapshot
|
|
72
|
-
// was captured — we have nothing better to offer than "unknown".
|
|
73
|
-
if (!change) return { content: null, existed: false };
|
|
74
|
-
// Empty oldHash means the file did not exist before the turn.
|
|
75
|
-
if (!change.oldHash) return { content: '', existed: false };
|
|
76
|
-
|
|
77
|
-
try {
|
|
78
|
-
const buf = await blobStore.readBlob(change.oldHash);
|
|
79
|
-
if (isBinaryBuffer(buf)) return { content: null, existed: true };
|
|
80
|
-
return { content: buf.toString('utf8'), existed: true };
|
|
81
|
-
} catch (err) {
|
|
82
|
-
debug.warn('snapshot', `Pre-checkpoint blob unavailable for ${relativePath}:`, err);
|
|
83
|
-
return { content: null, existed: true };
|
|
84
|
-
}
|
|
85
|
-
});
|