trellis 1.0.8 → 2.0.6

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.
Files changed (107) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +564 -83
  3. package/bin/trellis.mjs +2 -0
  4. package/dist/cli/index.js +4718 -0
  5. package/dist/core/index.js +12 -0
  6. package/dist/decisions/index.js +19 -0
  7. package/dist/embeddings/index.js +43 -0
  8. package/dist/index-1j1anhmr.js +4038 -0
  9. package/dist/index-3s0eak0p.js +1556 -0
  10. package/dist/index-8pce39mh.js +272 -0
  11. package/dist/index-a76rekgs.js +67 -0
  12. package/dist/index-cy9k1g6v.js +684 -0
  13. package/dist/index-fd4e26s4.js +69 -0
  14. package/dist/{store/eav-store.js → index-gkvhzm9f.js} +4 -6
  15. package/dist/index-gnw8d7d6.js +51 -0
  16. package/dist/index-vkpkfwhq.js +817 -0
  17. package/dist/index.js +118 -2876
  18. package/dist/links/index.js +55 -0
  19. package/dist/transformers-m9je15kg.js +32491 -0
  20. package/dist/vcs/index.js +110 -0
  21. package/logo.png +0 -0
  22. package/logo.svg +9 -0
  23. package/package.json +79 -76
  24. package/src/cli/index.ts +2340 -0
  25. package/src/core/index.ts +35 -0
  26. package/src/core/kernel/middleware.ts +44 -0
  27. package/src/core/persist/backend.ts +64 -0
  28. package/src/core/store/eav-store.ts +467 -0
  29. package/src/decisions/auto-capture.ts +136 -0
  30. package/src/decisions/hooks.ts +163 -0
  31. package/src/decisions/index.ts +261 -0
  32. package/src/decisions/types.ts +103 -0
  33. package/src/embeddings/chunker.ts +327 -0
  34. package/src/embeddings/index.ts +41 -0
  35. package/src/embeddings/model.ts +95 -0
  36. package/src/embeddings/search.ts +305 -0
  37. package/src/embeddings/store.ts +313 -0
  38. package/src/embeddings/types.ts +85 -0
  39. package/src/engine.ts +1083 -0
  40. package/src/garden/cluster.ts +330 -0
  41. package/src/garden/garden.ts +306 -0
  42. package/src/garden/index.ts +29 -0
  43. package/src/git/git-exporter.ts +286 -0
  44. package/src/git/git-importer.ts +329 -0
  45. package/src/git/git-reader.ts +189 -0
  46. package/src/git/index.ts +22 -0
  47. package/src/identity/governance.ts +211 -0
  48. package/src/identity/identity.ts +224 -0
  49. package/src/identity/index.ts +30 -0
  50. package/src/identity/signing-middleware.ts +97 -0
  51. package/src/index.ts +20 -0
  52. package/src/links/index.ts +49 -0
  53. package/src/links/lifecycle.ts +400 -0
  54. package/src/links/parser.ts +484 -0
  55. package/src/links/ref-index.ts +186 -0
  56. package/src/links/resolver.ts +314 -0
  57. package/src/links/types.ts +108 -0
  58. package/src/mcp/index.ts +22 -0
  59. package/src/mcp/server.ts +1278 -0
  60. package/src/semantic/csharp-parser.ts +493 -0
  61. package/src/semantic/go-parser.ts +585 -0
  62. package/src/semantic/index.ts +34 -0
  63. package/src/semantic/java-parser.ts +456 -0
  64. package/src/semantic/python-parser.ts +659 -0
  65. package/src/semantic/ruby-parser.ts +446 -0
  66. package/src/semantic/rust-parser.ts +784 -0
  67. package/src/semantic/semantic-merge.ts +210 -0
  68. package/src/semantic/ts-parser.ts +681 -0
  69. package/src/semantic/types.ts +175 -0
  70. package/src/sync/index.ts +32 -0
  71. package/src/sync/memory-transport.ts +66 -0
  72. package/src/sync/reconciler.ts +237 -0
  73. package/src/sync/sync-engine.ts +258 -0
  74. package/src/sync/types.ts +104 -0
  75. package/src/vcs/blob-store.ts +124 -0
  76. package/src/vcs/branch.ts +150 -0
  77. package/src/vcs/checkpoint.ts +64 -0
  78. package/src/vcs/decompose.ts +469 -0
  79. package/src/vcs/diff.ts +409 -0
  80. package/src/vcs/engine-context.ts +26 -0
  81. package/src/vcs/index.ts +23 -0
  82. package/src/vcs/issue.ts +800 -0
  83. package/src/vcs/merge.ts +425 -0
  84. package/src/vcs/milestone.ts +124 -0
  85. package/src/vcs/ops.ts +59 -0
  86. package/src/vcs/types.ts +213 -0
  87. package/src/vcs/vcs-middleware.ts +81 -0
  88. package/src/watcher/fs-watcher.ts +217 -0
  89. package/src/watcher/index.ts +9 -0
  90. package/src/watcher/ingestion.ts +116 -0
  91. package/dist/ai/index.js +0 -688
  92. package/dist/cli/server.js +0 -3321
  93. package/dist/cli/tql.js +0 -5282
  94. package/dist/client/tql-client.js +0 -108
  95. package/dist/graph/index.js +0 -2248
  96. package/dist/kernel/logic-middleware.js +0 -179
  97. package/dist/kernel/middleware.js +0 -0
  98. package/dist/kernel/operations.js +0 -32
  99. package/dist/kernel/schema-middleware.js +0 -34
  100. package/dist/kernel/security-middleware.js +0 -53
  101. package/dist/kernel/trellis-kernel.js +0 -2239
  102. package/dist/kernel/workspace.js +0 -91
  103. package/dist/persist/backend.js +0 -0
  104. package/dist/persist/sqlite-backend.js +0 -123
  105. package/dist/query/index.js +0 -1643
  106. package/dist/server/index.js +0 -3309
  107. package/dist/workflows/index.js +0 -3160
@@ -0,0 +1,817 @@
1
+ // @bun
2
+ import {
3
+ __esm,
4
+ __export,
5
+ __require
6
+ } from "./index-a76rekgs.js";
7
+
8
+ // src/links/parser.ts
9
+ function parseFileRefs(content, filePath) {
10
+ const ext = filePath.split(".").pop()?.toLowerCase() ?? "";
11
+ if (ext === "md") {
12
+ return parseMarkdownRefs(content, filePath);
13
+ }
14
+ return parseDocCommentRefs(content, filePath, ext);
15
+ }
16
+ function parseMarkdownRefs(content, filePath) {
17
+ return extractRefs(content, filePath, "markdown");
18
+ }
19
+ function parseDocCommentRefs(content, filePath, ext) {
20
+ const fileExt = ext ?? (filePath.split(".").pop()?.toLowerCase() ?? "");
21
+ const commentBlocks = extractDocComments(content, fileExt);
22
+ const refs = [];
23
+ for (const block of commentBlocks) {
24
+ const blockRefs = extractRefs(block.text, filePath, block.context, block.startLine);
25
+ refs.push(...blockRefs);
26
+ }
27
+ return refs;
28
+ }
29
+ function extractRefs(text, filePath, context, lineOffset = 0) {
30
+ const refs = [];
31
+ const lines = text.split(`
32
+ `);
33
+ for (let i = 0;i < lines.length; i++) {
34
+ const line = lines[i];
35
+ let match;
36
+ const re = new RegExp(WIKI_LINK_RE.source, WIKI_LINK_RE.flags);
37
+ while ((match = re.exec(line)) !== null) {
38
+ const raw = match[1];
39
+ const col = match.index;
40
+ const lineNum = i + 1 + lineOffset;
41
+ const parsed = parseRefContent(raw);
42
+ if (!parsed)
43
+ continue;
44
+ refs.push({
45
+ ...parsed,
46
+ raw,
47
+ source: {
48
+ filePath,
49
+ line: lineNum,
50
+ col,
51
+ context
52
+ }
53
+ });
54
+ }
55
+ }
56
+ return refs;
57
+ }
58
+ function parseRefContent(raw) {
59
+ if (!raw || !raw.trim())
60
+ return null;
61
+ let content;
62
+ let alias;
63
+ const pipeIdx = raw.indexOf("|");
64
+ if (pipeIdx !== -1) {
65
+ content = raw.substring(0, pipeIdx).trim();
66
+ alias = raw.substring(pipeIdx + 1).trim();
67
+ if (!alias)
68
+ alias = undefined;
69
+ } else {
70
+ content = raw.trim();
71
+ }
72
+ if (!content)
73
+ return null;
74
+ const colonIdx = content.indexOf(":");
75
+ if (colonIdx !== -1) {
76
+ const possibleNs = content.substring(0, colonIdx);
77
+ if (isValidNamespace(possibleNs)) {
78
+ const rest = content.substring(colonIdx + 1);
79
+ const { target: target2, anchor: anchor2 } = splitAnchor(rest);
80
+ const ns = possibleNs;
81
+ return { namespace: ns, target: target2, anchor: anchor2, alias };
82
+ }
83
+ }
84
+ const { target, anchor } = splitAnchor(content);
85
+ const inferred = inferNamespace(target, anchor);
86
+ if (!inferred)
87
+ return null;
88
+ return { namespace: inferred, target, anchor, alias };
89
+ }
90
+ function splitAnchor(content) {
91
+ const hashIdx = content.indexOf("#");
92
+ if (hashIdx === -1)
93
+ return { target: content };
94
+ return {
95
+ target: content.substring(0, hashIdx),
96
+ anchor: content.substring(hashIdx + 1) || undefined
97
+ };
98
+ }
99
+ function isValidNamespace(s) {
100
+ return VALID_NAMESPACES.has(s);
101
+ }
102
+ function inferNamespace(target, anchor) {
103
+ if (/^TRL-\d+$/i.test(target))
104
+ return "issue";
105
+ if (/^DEC-\d+$/i.test(target))
106
+ return "decision";
107
+ if (anchor)
108
+ return "symbol";
109
+ if (target.includes("/"))
110
+ return "file";
111
+ const ext = target.split(".").pop()?.toLowerCase();
112
+ if (ext && CODE_EXTENSIONS.has(ext))
113
+ return "file";
114
+ return null;
115
+ }
116
+ function extractDocComments(content, ext) {
117
+ switch (ext) {
118
+ case "ts":
119
+ case "tsx":
120
+ case "js":
121
+ case "jsx":
122
+ case "mjs":
123
+ case "cjs":
124
+ case "java":
125
+ case "cs":
126
+ return extractJSDocComments(content);
127
+ case "py":
128
+ case "pyi":
129
+ return extractPythonDocstrings(content);
130
+ case "rs":
131
+ return extractRustDocComments(content);
132
+ case "go":
133
+ return extractGoDocComments(content);
134
+ case "rb":
135
+ return extractRubyDocComments(content);
136
+ default:
137
+ return [];
138
+ }
139
+ }
140
+ function extractJSDocComments(content) {
141
+ const blocks = [];
142
+ const lines = content.split(`
143
+ `);
144
+ let inBlock = false;
145
+ let blockLines = [];
146
+ let blockStart = 0;
147
+ for (let i = 0;i < lines.length; i++) {
148
+ const trimmed = lines[i].trim();
149
+ if (!inBlock && (trimmed.startsWith("/**") || trimmed.startsWith("/*"))) {
150
+ inBlock = true;
151
+ blockStart = i;
152
+ blockLines = [trimmed];
153
+ if (trimmed.endsWith("*/") && trimmed.length > 4) {
154
+ blocks.push({
155
+ text: stripBlockCommentMarkers(blockLines.join(`
156
+ `)),
157
+ startLine: blockStart,
158
+ context: "jsdoc"
159
+ });
160
+ inBlock = false;
161
+ blockLines = [];
162
+ }
163
+ continue;
164
+ }
165
+ if (inBlock) {
166
+ blockLines.push(trimmed);
167
+ if (trimmed.includes("*/")) {
168
+ blocks.push({
169
+ text: stripBlockCommentMarkers(blockLines.join(`
170
+ `)),
171
+ startLine: blockStart,
172
+ context: "jsdoc"
173
+ });
174
+ inBlock = false;
175
+ blockLines = [];
176
+ }
177
+ continue;
178
+ }
179
+ if (trimmed.startsWith("//")) {
180
+ blocks.push({
181
+ text: trimmed.replace(/^\/\/\s?/, ""),
182
+ startLine: i,
183
+ context: "comment"
184
+ });
185
+ }
186
+ }
187
+ return blocks;
188
+ }
189
+ function stripBlockCommentMarkers(text) {
190
+ return text.replace(/^\/\*\*?\s?/, "").replace(/\*\/\s*$/, "").split(`
191
+ `).map((line) => line.replace(/^\s*\*\s?/, "")).join(`
192
+ `).trim();
193
+ }
194
+ function extractPythonDocstrings(content) {
195
+ const blocks = [];
196
+ const lines = content.split(`
197
+ `);
198
+ let inDocstring = false;
199
+ let delimiter = "";
200
+ let blockLines = [];
201
+ let blockStart = 0;
202
+ for (let i = 0;i < lines.length; i++) {
203
+ const trimmed = lines[i].trim();
204
+ if (!inDocstring) {
205
+ for (const delim of ['"""', "'''"]) {
206
+ if (trimmed.startsWith(delim)) {
207
+ if (trimmed.endsWith(delim) && trimmed.length > delim.length * 2) {
208
+ blocks.push({
209
+ text: trimmed.slice(delim.length, -delim.length).trim(),
210
+ startLine: i,
211
+ context: "pydoc"
212
+ });
213
+ break;
214
+ }
215
+ inDocstring = true;
216
+ delimiter = delim;
217
+ blockStart = i;
218
+ blockLines = [trimmed.slice(delim.length)];
219
+ break;
220
+ }
221
+ }
222
+ if (!inDocstring && trimmed.startsWith("#")) {
223
+ blocks.push({
224
+ text: trimmed.replace(/^#\s?/, ""),
225
+ startLine: i,
226
+ context: "comment"
227
+ });
228
+ }
229
+ } else {
230
+ if (trimmed.endsWith(delimiter)) {
231
+ blockLines.push(trimmed.slice(0, -delimiter.length));
232
+ blocks.push({
233
+ text: blockLines.join(`
234
+ `).trim(),
235
+ startLine: blockStart,
236
+ context: "pydoc"
237
+ });
238
+ inDocstring = false;
239
+ blockLines = [];
240
+ } else {
241
+ blockLines.push(trimmed);
242
+ }
243
+ }
244
+ }
245
+ return blocks;
246
+ }
247
+ function extractRustDocComments(content) {
248
+ const blocks = [];
249
+ const lines = content.split(`
250
+ `);
251
+ for (let i = 0;i < lines.length; i++) {
252
+ const trimmed = lines[i].trim();
253
+ if (trimmed.startsWith("///") || trimmed.startsWith("//!")) {
254
+ blocks.push({
255
+ text: trimmed.replace(/^\/\/[\/!]\s?/, ""),
256
+ startLine: i,
257
+ context: "rustdoc"
258
+ });
259
+ }
260
+ }
261
+ return blocks;
262
+ }
263
+ function extractGoDocComments(content) {
264
+ const blocks = [];
265
+ const lines = content.split(`
266
+ `);
267
+ for (let i = 0;i < lines.length; i++) {
268
+ const trimmed = lines[i].trim();
269
+ if (trimmed.startsWith("//")) {
270
+ blocks.push({
271
+ text: trimmed.replace(/^\/\/\s?/, ""),
272
+ startLine: i,
273
+ context: "godoc"
274
+ });
275
+ }
276
+ }
277
+ return blocks;
278
+ }
279
+ function extractRubyDocComments(content) {
280
+ const blocks = [];
281
+ const lines = content.split(`
282
+ `);
283
+ for (let i = 0;i < lines.length; i++) {
284
+ const trimmed = lines[i].trim();
285
+ if (trimmed.startsWith("#")) {
286
+ blocks.push({
287
+ text: trimmed.replace(/^#\s?/, ""),
288
+ startLine: i,
289
+ context: "comment"
290
+ });
291
+ }
292
+ }
293
+ return blocks;
294
+ }
295
+ var WIKI_LINK_RE, CODE_EXTENSIONS, VALID_NAMESPACES;
296
+ var init_parser = __esm(() => {
297
+ WIKI_LINK_RE = /\[\[([^\]]+)\]\]/g;
298
+ CODE_EXTENSIONS = new Set([
299
+ "ts",
300
+ "tsx",
301
+ "js",
302
+ "jsx",
303
+ "mjs",
304
+ "cjs",
305
+ "py",
306
+ "pyi",
307
+ "go",
308
+ "rs",
309
+ "rb",
310
+ "java",
311
+ "cs",
312
+ "md",
313
+ "json",
314
+ "yaml",
315
+ "yml",
316
+ "toml",
317
+ "css",
318
+ "scss",
319
+ "less",
320
+ "html",
321
+ "vue",
322
+ "svelte"
323
+ ]);
324
+ VALID_NAMESPACES = new Set([
325
+ "issue",
326
+ "file",
327
+ "symbol",
328
+ "identity",
329
+ "milestone",
330
+ "decision"
331
+ ]);
332
+ });
333
+
334
+ // src/links/resolver.ts
335
+ function resolveRef(ref, ctx) {
336
+ switch (ref.namespace) {
337
+ case "issue":
338
+ return resolveIssue(ref, ctx);
339
+ case "file":
340
+ return resolveFile(ref, ctx);
341
+ case "symbol":
342
+ return resolveSymbol(ref, ctx);
343
+ case "identity":
344
+ return resolveIdentity(ref, ctx);
345
+ case "milestone":
346
+ return resolveMilestone(ref, ctx);
347
+ case "decision":
348
+ return resolveDecision(ref, ctx);
349
+ default:
350
+ return { ...ref, state: "broken" };
351
+ }
352
+ }
353
+ function resolveRefs(refs, ctx) {
354
+ return refs.map((ref) => resolveRef(ref, ctx));
355
+ }
356
+ function resolveIssue(ref, ctx) {
357
+ const title = ctx.getIssueTitle(ref.target);
358
+ if (title !== undefined) {
359
+ return {
360
+ ...ref,
361
+ state: "resolved",
362
+ entityId: `issue:${ref.target}`,
363
+ title
364
+ };
365
+ }
366
+ return { ...ref, state: "broken" };
367
+ }
368
+ function resolveFile(ref, ctx) {
369
+ if (ctx.hasTrackedFile(ref.target)) {
370
+ return {
371
+ ...ref,
372
+ state: "resolved",
373
+ entityId: `file:${ref.target}`,
374
+ title: ref.target
375
+ };
376
+ }
377
+ return { ...ref, state: "broken" };
378
+ }
379
+ function resolveSymbol(ref, ctx) {
380
+ if (!ctx.hasTrackedFile(ref.target)) {
381
+ return { ...ref, state: "broken" };
382
+ }
383
+ if (ref.anchor && ctx.hasSymbol(ref.target, ref.anchor)) {
384
+ return {
385
+ ...ref,
386
+ state: "resolved",
387
+ entityId: `symbol:${ref.target}#${ref.anchor}`,
388
+ title: `${ref.anchor} in ${ref.target}`
389
+ };
390
+ }
391
+ if (ref.anchor) {
392
+ return { ...ref, state: "broken" };
393
+ }
394
+ return {
395
+ ...ref,
396
+ state: "resolved",
397
+ entityId: `file:${ref.target}`,
398
+ title: ref.target
399
+ };
400
+ }
401
+ function resolveIdentity(ref, ctx) {
402
+ if (ctx.hasIdentity(ref.target)) {
403
+ return {
404
+ ...ref,
405
+ state: "resolved",
406
+ entityId: `identity:${ref.target}`,
407
+ title: ref.target
408
+ };
409
+ }
410
+ return { ...ref, state: "broken" };
411
+ }
412
+ function resolveMilestone(ref, ctx) {
413
+ const title = ctx.getMilestoneTitle(ref.target);
414
+ if (title !== undefined) {
415
+ return {
416
+ ...ref,
417
+ state: "resolved",
418
+ entityId: `milestone:${ref.target}`,
419
+ title
420
+ };
421
+ }
422
+ return { ...ref, state: "broken" };
423
+ }
424
+ function resolveDecision(ref, ctx) {
425
+ if (ctx.hasDecision(ref.target)) {
426
+ return {
427
+ ...ref,
428
+ state: "resolved",
429
+ entityId: `decision:${ref.target}`,
430
+ title: ctx.getDecisionTitle(ref.target) ?? ref.target
431
+ };
432
+ }
433
+ return { ...ref, state: "broken" };
434
+ }
435
+ function createResolverContext(engine) {
436
+ const trackedSet = new Set(engine.trackedFiles().map((f) => f.path));
437
+ const agentIds = new Set(engine.getOps().map((op) => op.agentId));
438
+ const issues = engine.listIssues();
439
+ const milestones = engine.listMilestones();
440
+ return {
441
+ hasTrackedFile(path) {
442
+ return trackedSet.has(path);
443
+ },
444
+ getIssueTitle(id) {
445
+ const issue = engine.getIssue(id);
446
+ return issue?.title;
447
+ },
448
+ getMilestoneTitle(idOrMessage) {
449
+ const byId = milestones.find((m) => m.id === idOrMessage);
450
+ if (byId)
451
+ return byId.message;
452
+ const byMsg = milestones.find((m) => m.message && m.message.toLowerCase().includes(idOrMessage.toLowerCase()));
453
+ return byMsg?.message;
454
+ },
455
+ hasSymbol(filePath, symbolName) {
456
+ try {
457
+ const { readFileSync } = __require("fs");
458
+ const { join } = __require("path");
459
+ const absPath = join(engine.getRootPath(), filePath);
460
+ const content = readFileSync(absPath, "utf-8");
461
+ const result = engine.parseFile(content, filePath);
462
+ if (!result)
463
+ return false;
464
+ return result.declarations.some((d) => d.name === symbolName);
465
+ } catch {
466
+ return false;
467
+ }
468
+ },
469
+ hasIdentity(id) {
470
+ return agentIds.has(id) || agentIds.has(`agent:${id}`);
471
+ },
472
+ getKnownAgentIds() {
473
+ return [...agentIds];
474
+ },
475
+ getTrackedFilePaths() {
476
+ return [...trackedSet];
477
+ },
478
+ getIssueIds() {
479
+ return issues.map((i) => i.id);
480
+ },
481
+ getMilestoneIds() {
482
+ return milestones.map((m) => m.id);
483
+ },
484
+ hasDecision(id) {
485
+ if (!engine.getDecision)
486
+ return false;
487
+ return engine.getDecision(id) !== null;
488
+ },
489
+ getDecisionTitle(id) {
490
+ if (!engine.getDecision)
491
+ return;
492
+ const d = engine.getDecision(id);
493
+ return d?.toolName;
494
+ },
495
+ getSymbolNames(filePath) {
496
+ try {
497
+ const { readFileSync } = __require("fs");
498
+ const { join } = __require("path");
499
+ const absPath = join(engine.getRootPath(), filePath);
500
+ const content = readFileSync(absPath, "utf-8");
501
+ const result = engine.parseFile(content, filePath);
502
+ if (!result)
503
+ return [];
504
+ return result.declarations.map((d) => d.name);
505
+ } catch {
506
+ return [];
507
+ }
508
+ }
509
+ };
510
+ }
511
+ var init_resolver = () => {};
512
+
513
+ // src/links/ref-index.ts
514
+ function buildRefIndex(files, ctx) {
515
+ const index = {
516
+ outgoing: new Map,
517
+ incoming: new Map
518
+ };
519
+ for (const file of files) {
520
+ const refs = parseFileRefs(file.content, file.path);
521
+ addFileToIndex(index, file.path, refs, ctx);
522
+ }
523
+ return index;
524
+ }
525
+ function updateFileInIndex(index, filePath, content, ctx) {
526
+ removeFileFromIndex(index, filePath);
527
+ const refs = parseFileRefs(content, filePath);
528
+ addFileToIndex(index, filePath, refs, ctx);
529
+ }
530
+ function removeFileFromIndex(index, filePath) {
531
+ const oldRefs = index.outgoing.get(filePath);
532
+ if (!oldRefs)
533
+ return;
534
+ for (const ref of oldRefs) {
535
+ const resolved = resolveRefToEntityId(ref);
536
+ if (resolved) {
537
+ const sources = index.incoming.get(resolved);
538
+ if (sources) {
539
+ const filtered = sources.filter((s) => s.filePath !== filePath);
540
+ if (filtered.length > 0) {
541
+ index.incoming.set(resolved, filtered);
542
+ } else {
543
+ index.incoming.delete(resolved);
544
+ }
545
+ }
546
+ }
547
+ }
548
+ index.outgoing.delete(filePath);
549
+ }
550
+ function getOutgoingRefs(index, filePath) {
551
+ return index.outgoing.get(filePath) ?? [];
552
+ }
553
+ function getBacklinks(index, entityId) {
554
+ return index.incoming.get(entityId) ?? [];
555
+ }
556
+ function getReferencedEntities(index) {
557
+ return [...index.incoming.keys()];
558
+ }
559
+ function getFilesWithRefs(index) {
560
+ return [...index.outgoing.keys()];
561
+ }
562
+ function getIndexStats(index) {
563
+ let totalRefs = 0;
564
+ for (const refs of index.outgoing.values()) {
565
+ totalRefs += refs.length;
566
+ }
567
+ return {
568
+ totalFiles: index.outgoing.size,
569
+ totalRefs,
570
+ totalEntities: index.incoming.size
571
+ };
572
+ }
573
+ function addFileToIndex(index, filePath, refs, ctx) {
574
+ if (refs.length === 0)
575
+ return;
576
+ index.outgoing.set(filePath, refs);
577
+ for (const ref of refs) {
578
+ const resolved = resolveRef(ref, ctx);
579
+ const entityId = resolved.entityId ?? buildFallbackEntityId(ref);
580
+ const sources = index.incoming.get(entityId) ?? [];
581
+ sources.push(ref.source);
582
+ index.incoming.set(entityId, sources);
583
+ }
584
+ }
585
+ function buildFallbackEntityId(ref) {
586
+ if (ref.anchor) {
587
+ return `${ref.namespace}:${ref.target}#${ref.anchor}`;
588
+ }
589
+ return `${ref.namespace}:${ref.target}`;
590
+ }
591
+ function resolveRefToEntityId(ref) {
592
+ if (ref.anchor) {
593
+ return `${ref.namespace}:${ref.target}#${ref.anchor}`;
594
+ }
595
+ return `${ref.namespace}:${ref.target}`;
596
+ }
597
+ var init_ref_index = __esm(() => {
598
+ init_parser();
599
+ init_resolver();
600
+ });
601
+
602
+ // src/links/lifecycle.ts
603
+ import { readFileSync } from "fs";
604
+ import { join } from "path";
605
+
606
+ class StaleRefRegistry {
607
+ staleRefs = new Map;
608
+ markStale(entityId, reason, sources, opts) {
609
+ const entry = {
610
+ entityId,
611
+ reason,
612
+ causeOpHash: opts?.causeOpHash,
613
+ newTarget: opts?.newTarget,
614
+ timestamp: new Date().toISOString(),
615
+ sources
616
+ };
617
+ this.staleRefs.set(entityId, entry);
618
+ return entry;
619
+ }
620
+ clearStale(entityId) {
621
+ this.staleRefs.delete(entityId);
622
+ }
623
+ isStale(entityId) {
624
+ return this.staleRefs.has(entityId);
625
+ }
626
+ getStale(entityId) {
627
+ return this.staleRefs.get(entityId);
628
+ }
629
+ getAllStale() {
630
+ return [...this.staleRefs.values()];
631
+ }
632
+ getByReason(reason) {
633
+ return this.getAllStale().filter((s) => s.reason === reason);
634
+ }
635
+ }
636
+ function buildRenameProposal(index, filePath, oldName, newName) {
637
+ const oldEntityId = `symbol:${filePath}#${oldName}`;
638
+ const newEntityId = `symbol:${filePath}#${newName}`;
639
+ const sources = getBacklinks(index, oldEntityId);
640
+ const altEntityId = `symbol:${filePath}#${oldName}`;
641
+ const altSources = getBacklinks(index, altEntityId);
642
+ const allSources = deduplicateSources([...sources, ...altSources]);
643
+ const rewrites = [];
644
+ const affectedFiles = new Set;
645
+ for (const source of allSources) {
646
+ affectedFiles.add(source.filePath);
647
+ const oldText = buildRefText(filePath, oldName);
648
+ const newText = buildRefText(filePath, newName);
649
+ rewrites.push({
650
+ filePath: source.filePath,
651
+ line: source.line,
652
+ col: source.col,
653
+ oldText,
654
+ newText
655
+ });
656
+ }
657
+ return {
658
+ oldTarget: oldEntityId,
659
+ newTarget: newEntityId,
660
+ affectedFiles: [...affectedFiles],
661
+ rewrites
662
+ };
663
+ }
664
+ function applyRenameProposal(proposal, rootPath) {
665
+ const modifiedFiles = [];
666
+ const byFile = new Map;
667
+ for (const rw of proposal.rewrites) {
668
+ const existing = byFile.get(rw.filePath) ?? [];
669
+ existing.push(rw);
670
+ byFile.set(rw.filePath, existing);
671
+ }
672
+ for (const [filePath, rewrites] of byFile) {
673
+ try {
674
+ const absPath = join(rootPath, filePath);
675
+ let content = readFileSync(absPath, "utf-8");
676
+ let modified = false;
677
+ for (const rw of rewrites) {
678
+ if (content.includes(rw.oldText)) {
679
+ content = content.replaceAll(rw.oldText, rw.newText);
680
+ modified = true;
681
+ }
682
+ }
683
+ if (modified) {
684
+ const { writeFileSync } = __require("fs");
685
+ writeFileSync(absPath, content);
686
+ modifiedFiles.push(filePath);
687
+ }
688
+ } catch {}
689
+ }
690
+ return modifiedFiles;
691
+ }
692
+ function handleSymbolDeletion(index, registry, filePath, symbolName, causeOpHash) {
693
+ const entityId = `symbol:${filePath}#${symbolName}`;
694
+ const sources = getBacklinks(index, entityId);
695
+ if (sources.length === 0)
696
+ return null;
697
+ return registry.markStale(entityId, "deleted", sources, { causeOpHash });
698
+ }
699
+ function handleFileDeletion(index, registry, filePath, causeOpHash) {
700
+ const entityId = `file:${filePath}`;
701
+ const sources = getBacklinks(index, entityId);
702
+ if (sources.length === 0)
703
+ return null;
704
+ return registry.markStale(entityId, "deleted", sources, { causeOpHash });
705
+ }
706
+ function getDiagnostics(index, registry, resolvedEntityIds) {
707
+ const diagnostics = [];
708
+ for (const [filePath, refs] of index.outgoing) {
709
+ for (const ref of refs) {
710
+ const entityId = buildEntityIdFromRef(ref);
711
+ const staleInfo = registry.getStale(entityId);
712
+ if (staleInfo) {
713
+ const reason = staleInfo.reason === "renamed" ? `renamed to ${staleInfo.newTarget}` : "removed";
714
+ diagnostics.push({
715
+ entityId,
716
+ state: "stale",
717
+ source: ref.source,
718
+ message: `Reference to '${ref.target}${ref.anchor ? "#" + ref.anchor : ""}' is stale: target was ${reason}`
719
+ });
720
+ continue;
721
+ }
722
+ if (!resolvedEntityIds.has(entityId)) {
723
+ diagnostics.push({
724
+ entityId,
725
+ state: "broken",
726
+ source: ref.source,
727
+ message: `Cannot resolve reference: '${ref.target}${ref.anchor ? "#" + ref.anchor : ""}' does not exist`
728
+ });
729
+ }
730
+ }
731
+ }
732
+ return diagnostics;
733
+ }
734
+ function processSemanticPatches(patches, filePath, index, registry, causeOpHash) {
735
+ const events = [];
736
+ for (const patch of patches) {
737
+ if (patch.kind === "symbolRename") {
738
+ const proposal = buildRenameProposal(index, filePath, patch.oldName, patch.newName);
739
+ if (proposal.rewrites.length > 0) {
740
+ events.push({
741
+ type: "rename-proposal",
742
+ filePath,
743
+ proposal
744
+ });
745
+ }
746
+ }
747
+ if (patch.kind === "symbolRemove") {
748
+ const staleRef = handleSymbolDeletion(index, registry, filePath, patch.entityName, causeOpHash);
749
+ if (staleRef) {
750
+ events.push({
751
+ type: "stale-detected",
752
+ filePath,
753
+ staleRef
754
+ });
755
+ }
756
+ }
757
+ }
758
+ return events;
759
+ }
760
+ function buildRefText(filePath, symbolName) {
761
+ return `[[${filePath}#${symbolName}]]`;
762
+ }
763
+ function buildEntityIdFromRef(ref) {
764
+ if (ref.anchor) {
765
+ return `${ref.namespace}:${ref.target}#${ref.anchor}`;
766
+ }
767
+ return `${ref.namespace}:${ref.target}`;
768
+ }
769
+ function deduplicateSources(sources) {
770
+ const seen = new Set;
771
+ return sources.filter((s) => {
772
+ const key = `${s.filePath}:${s.line}:${s.col}`;
773
+ if (seen.has(key))
774
+ return false;
775
+ seen.add(key);
776
+ return true;
777
+ });
778
+ }
779
+ var init_lifecycle = __esm(() => {
780
+ init_ref_index();
781
+ });
782
+
783
+ // src/links/index.ts
784
+ var exports_links = {};
785
+ __export(exports_links, {
786
+ updateFileInIndex: () => updateFileInIndex,
787
+ resolveRefs: () => resolveRefs,
788
+ resolveRef: () => resolveRef,
789
+ removeFileFromIndex: () => removeFileFromIndex,
790
+ processSemanticPatches: () => processSemanticPatches,
791
+ parseRefContent: () => parseRefContent,
792
+ parseMarkdownRefs: () => parseMarkdownRefs,
793
+ parseFileRefs: () => parseFileRefs,
794
+ parseDocCommentRefs: () => parseDocCommentRefs,
795
+ inferNamespace: () => inferNamespace,
796
+ handleSymbolDeletion: () => handleSymbolDeletion,
797
+ handleFileDeletion: () => handleFileDeletion,
798
+ getReferencedEntities: () => getReferencedEntities,
799
+ getOutgoingRefs: () => getOutgoingRefs,
800
+ getIndexStats: () => getIndexStats,
801
+ getFilesWithRefs: () => getFilesWithRefs,
802
+ getDiagnostics: () => getDiagnostics,
803
+ getBacklinks: () => getBacklinks,
804
+ createResolverContext: () => createResolverContext,
805
+ buildRenameProposal: () => buildRenameProposal,
806
+ buildRefIndex: () => buildRefIndex,
807
+ applyRenameProposal: () => applyRenameProposal,
808
+ StaleRefRegistry: () => StaleRefRegistry
809
+ });
810
+ var init_links = __esm(() => {
811
+ init_parser();
812
+ init_resolver();
813
+ init_ref_index();
814
+ init_lifecycle();
815
+ });
816
+
817
+ export { parseFileRefs, parseMarkdownRefs, parseDocCommentRefs, parseRefContent, inferNamespace, resolveRef, resolveRefs, createResolverContext, buildRefIndex, updateFileInIndex, removeFileFromIndex, getOutgoingRefs, getBacklinks, getReferencedEntities, getFilesWithRefs, getIndexStats, StaleRefRegistry, buildRenameProposal, applyRenameProposal, handleSymbolDeletion, handleFileDeletion, getDiagnostics, processSemanticPatches, exports_links, init_links };