semantic-atlas 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/.agents/skills/semantic-atlas/SKILL.md +59 -43
  2. package/.agents/skills/semantic-atlas/references/graph-patch.md +22 -5
  3. package/.agents/skills/semantic-atlas/references/result-routing.md +32 -19
  4. package/.agents/skills/semantic-atlas/references/snapshot-bootstrap.md +17 -32
  5. package/README.md +34 -20
  6. package/README.zh-CN.md +30 -20
  7. package/dist/cli/argument-parser.js +41 -23
  8. package/dist/cli/bin.js +12 -15
  9. package/dist/cli/cli-application.d.ts +2 -2
  10. package/dist/cli/cli-application.d.ts.map +1 -1
  11. package/dist/cli/cli-application.js +40 -36
  12. package/dist/cli/standalone-cli.d.ts +3 -0
  13. package/dist/cli/standalone-cli.d.ts.map +1 -0
  14. package/dist/cli/standalone-cli.js +65 -0
  15. package/dist/cli/types.d.ts +7 -6
  16. package/dist/cli/types.d.ts.map +1 -1
  17. package/dist/contracts/cli.d.ts +370 -1360
  18. package/dist/contracts/cli.d.ts.map +1 -1
  19. package/dist/contracts/cli.js +58 -65
  20. package/dist/contracts/graph.d.ts +4 -0
  21. package/dist/contracts/graph.d.ts.map +1 -1
  22. package/dist/contracts/graph.js +2 -2
  23. package/dist/evaluation/contracts.d.ts +11 -2
  24. package/dist/evaluation/contracts.d.ts.map +1 -1
  25. package/dist/evaluation/contracts.js +7 -2
  26. package/dist/graph/graph-store.d.ts +3 -1
  27. package/dist/graph/graph-store.d.ts.map +1 -1
  28. package/dist/graph/graph-store.js +48 -2
  29. package/dist/graph/types.d.ts +30 -9
  30. package/dist/graph/types.d.ts.map +1 -1
  31. package/dist/index.d.ts +0 -1
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/package-metadata.d.ts +2 -0
  34. package/dist/package-metadata.d.ts.map +1 -0
  35. package/dist/package-metadata.js +9 -0
  36. package/dist/runtime/sqlite-compatibility-process.d.ts +8 -0
  37. package/dist/runtime/sqlite-compatibility-process.d.ts.map +1 -0
  38. package/dist/runtime/sqlite-compatibility-process.js +20 -0
  39. package/dist/setup/skill-installer.d.ts +19 -0
  40. package/dist/setup/skill-installer.d.ts.map +1 -0
  41. package/dist/setup/skill-installer.js +156 -0
  42. package/dist/world/business-map-projector.d.ts +14 -0
  43. package/dist/world/business-map-projector.d.ts.map +1 -0
  44. package/dist/world/business-map-projector.js +178 -0
  45. package/dist/world/world-graph-query.d.ts +8 -13
  46. package/dist/world/world-graph-query.d.ts.map +1 -1
  47. package/dist/world/world-graph-query.js +26 -229
  48. package/docs/README.md +30 -0
  49. package/docs/architecture/codegraph-backend.md +2 -2
  50. package/docs/architecture/continuous-business-learning.md +240 -0
  51. package/docs/contracts/cli-v1.md +40 -17
  52. package/docs/contracts/graph-model.md +11 -7
  53. package/docs/contracts/graph-patch-v1.md +8 -1
  54. package/docs/evaluation.md +20 -15
  55. package/docs/index.md +41 -0
  56. package/docs/log.md +56 -0
  57. package/docs/product-contract.md +10 -5
  58. package/package.json +1 -1
  59. package/schemas/cli-envelope-v1.schema.json +365 -564
  60. package/schemas/evaluation-run-v1.schema.json +4 -1
@@ -52,6 +52,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
52
52
  });
53
53
  import { GraphStore } from "../graph/graph-store.js";
54
54
  import { CodeGraphStructuralBackend } from "../structural-backend/codegraph-backend.js";
55
+ import { BusinessMapProjector } from "./business-map-projector.js";
55
56
  import { WorldSnapshotStore } from "./world-snapshot-store.js";
56
57
  export class WorldGraphQuery {
57
58
  #repository;
@@ -65,78 +66,47 @@ export class WorldGraphQuery {
65
66
  this.#structural = dependencies.structural;
66
67
  this.#ownsGraph = dependencies.ownsGraph;
67
68
  }
68
- async roots() {
69
+ async view(focusKey) {
69
70
  return this.withCurrentWorld(async (snapshot) => {
70
- const businessRoots = this.#graph.listCapabilityRoots(snapshot.snapshotId);
71
- if (businessRoots.length > 0) {
72
- return businessRoots;
73
- }
74
- return Promise.all((await this.#structural.listRoots())
75
- .map((node) => this.structuralNode(node, snapshot)));
71
+ const nodes = this.#graph.listBusinessNodes(snapshot.snapshotId);
72
+ const relations = this.#graph.listBusinessRelations(snapshot.snapshotId);
73
+ return new BusinessMapProjector(nodes, relations).project(focusKey);
76
74
  });
77
75
  }
78
- async children(reference) {
76
+ async searchBusiness(query, options = {}) {
77
+ const terms = lexicalTerms(query);
78
+ if (terms.length === 0) {
79
+ return [];
80
+ }
79
81
  return this.withCurrentWorld(async (snapshot) => {
80
- if (await this.getNode(reference, snapshot) === undefined) {
81
- return undefined;
82
- }
83
- const direction = reference.domain === "business" ? "incoming" : "outgoing";
84
- const relationTypes = reference.domain === "business"
85
- ? ["part_of"]
86
- : ["contains"];
87
- return (await this.traverseSnapshot(reference, {
88
- maxDepth: 1,
89
- direction,
90
- relationTypes,
91
- }, snapshot)).neighbors.map(({ node }) => node);
82
+ const limit = requireSearchLimit(options.limit);
83
+ return this.#graph.search(query, { snapshotId: snapshot.snapshotId, limit })
84
+ .flatMap((result) => (result.node.domain === "business" ? [{ score: result.score, node: result.node }] : []));
92
85
  });
93
86
  }
94
- async search(query, options = {}) {
87
+ async searchCode(query, options = {}) {
95
88
  const terms = lexicalTerms(query);
96
89
  if (terms.length === 0) {
97
90
  return [];
98
91
  }
99
92
  return this.withCurrentWorld(async (snapshot) => {
100
93
  const limit = requireSearchLimit(options.limit);
101
- const [businessResults, structuralResults] = await Promise.all([
102
- Promise.resolve(this.#graph.search(query, { snapshotId: snapshot.snapshotId, limit })),
103
- this.#structural.search({ query, limit }),
104
- ]);
105
- const combined = [
106
- ...businessResults.map((result, index) => ({
107
- score: reciprocalRank(index),
108
- node: result.node,
109
- })),
110
- ...await Promise.all(structuralResults.map(async (result, index) => ({
111
- score: reciprocalRank(index),
112
- node: await this.structuralNode(result.node, snapshot),
113
- }))),
114
- ];
115
- return uniqueSearchResults(combined)
116
- .sort((left, right) => right.score - left.score || nodeIdentity(left.node).localeCompare(nodeIdentity(right.node)))
117
- .slice(0, limit);
94
+ const results = await this.#structural.search({ query, limit });
95
+ return Promise.all(results.map(async (result, index) => ({
96
+ score: reciprocalRank(index),
97
+ node: await this.structuralNode(result.node, snapshot),
98
+ })));
118
99
  });
119
100
  }
120
- async show(reference, options = {}) {
101
+ async showBusiness(key) {
121
102
  return this.withCurrentWorld(async (snapshot) => {
122
- const node = await this.getNode(reference, snapshot);
123
- if (node === undefined) {
103
+ const node = this.#graph.getNode({ domain: "business", key }, snapshot.snapshotId);
104
+ if (node?.domain !== "business") {
124
105
  return undefined;
125
106
  }
126
- const depth = requireTraversalDepth(options.maxDepth);
127
- const traversal = await this.traverseSnapshot(reference, { maxDepth: depth }, snapshot);
128
107
  return {
129
108
  node,
130
- depth,
131
- neighbors: traversal.neighbors,
132
- invariants: uniqueNodes(traversal.neighbors
133
- .map(({ node: neighbor }) => neighbor)
134
- .filter((neighbor) => (neighbor.domain === "business" && neighbor.kind === "Invariant"))),
135
- tests: uniqueNodes(traversal.neighbors
136
- .filter(({ relation }) => relation.type === "verified_by")
137
- .map(({ node: neighbor }) => neighbor)
138
- .filter((neighbor) => neighbor.domain === "structural")),
139
- unknowns: traversal.unknowns,
109
+ relations: (await this.businessAdjacency({ domain: "business", key }, snapshot)).sort(compareNeighbors),
140
110
  };
141
111
  });
142
112
  }
@@ -154,57 +124,6 @@ export class WorldGraphQuery {
154
124
  __disposeResources(env_1);
155
125
  }
156
126
  }
157
- async traverse(start, options = {}) {
158
- return this.withCurrentWorld((snapshot) => this.traverseSnapshot(start, options, snapshot));
159
- }
160
- async traverseSnapshot(start, options, snapshot) {
161
- const maxDepth = requireTraversalDepth(options.maxDepth);
162
- const direction = options.direction ?? "both";
163
- const relationTypes = options.relationTypes === undefined
164
- ? undefined
165
- : new Set(options.relationTypes);
166
- const queue = [
167
- { reference: start, depth: 0 },
168
- ];
169
- const visitedNodes = new Set([referenceIdentity(start)]);
170
- const emittedRelations = new Set();
171
- const emittedUnknowns = new Set();
172
- const neighbors = [];
173
- const unknowns = [];
174
- for (let index = 0; index < queue.length; index += 1) {
175
- const current = queue[index];
176
- if (current.depth >= maxDepth) {
177
- continue;
178
- }
179
- const adjacent = await this.adjacent(current.reference, snapshot, direction);
180
- for (const boundary of adjacent.unknowns) {
181
- if (!emittedUnknowns.has(boundary.id)) {
182
- emittedUnknowns.add(boundary.id);
183
- unknowns.push(boundary);
184
- }
185
- }
186
- for (const candidate of adjacent.neighbors) {
187
- const relationKey = relationIdentity(candidate.relation);
188
- if (emittedRelations.has(relationKey)
189
- || (relationTypes !== undefined && !relationTypes.has(candidate.relation.type))) {
190
- continue;
191
- }
192
- emittedRelations.add(relationKey);
193
- const depth = current.depth + 1;
194
- neighbors.push({ ...candidate, depth });
195
- const nextReference = referenceForNode(candidate.node);
196
- const nextIdentity = referenceIdentity(nextReference);
197
- if (!visitedNodes.has(nextIdentity)) {
198
- visitedNodes.add(nextIdentity);
199
- queue.push({ reference: nextReference, depth });
200
- }
201
- }
202
- }
203
- return {
204
- neighbors: neighbors.sort(compareNeighbors),
205
- unknowns: unknowns.sort((left, right) => left.id.localeCompare(right.id)),
206
- };
207
- }
208
127
  close() {
209
128
  if (this.#ownsGraph) {
210
129
  this.#graph.close();
@@ -245,23 +164,11 @@ export class WorldGraphQuery {
245
164
  const node = await this.#structural.getNode({ id: reference.id });
246
165
  return node === undefined ? undefined : this.structuralNode(node, snapshot);
247
166
  }
248
- async adjacent(reference, snapshot, direction) {
249
- const [business, structural] = await Promise.all([
250
- this.businessAdjacency(reference, snapshot, direction),
251
- reference.domain === "structural"
252
- ? this.structuralAdjacency(reference.id, snapshot, direction)
253
- : Promise.resolve({ neighbors: [], unknowns: [] }),
254
- ]);
255
- return {
256
- neighbors: [...business, ...structural.neighbors],
257
- unknowns: structural.unknowns,
258
- };
259
- }
260
- async businessAdjacency(reference, snapshot, direction) {
167
+ async businessAdjacency(reference, snapshot) {
261
168
  const result = [];
262
169
  for (const relation of this.#graph.listBusinessRelations(snapshot.snapshotId)) {
263
170
  const relationDirection = directionForRelation(reference, relation);
264
- if (relationDirection === undefined || (direction !== "both" && direction !== relationDirection)) {
171
+ if (relationDirection === undefined) {
265
172
  continue;
266
173
  }
267
174
  const otherReference = relationDirection === "outgoing" ? relation.to : relation.from;
@@ -272,48 +179,6 @@ export class WorldGraphQuery {
272
179
  }
273
180
  return result;
274
181
  }
275
- async structuralAdjacency(reference, snapshot, direction) {
276
- const traversal = await this.#structural.traverse({
277
- reference: { id: reference },
278
- maxDepth: 1,
279
- direction,
280
- });
281
- const nodes = new Map(traversal.nodes.map((node) => [node.reference.id, node]));
282
- const neighbors = await Promise.all(traversal.relations.flatMap((relation) => {
283
- const relationDirection = structuralRelationDirection(reference, relation);
284
- if (relationDirection === undefined) {
285
- return [];
286
- }
287
- const target = relationDirection === "outgoing" ? relation.to.id : relation.from.id;
288
- const node = nodes.get(target);
289
- if (node === undefined) {
290
- return [];
291
- }
292
- return [this.structuralNeighbor(relation, relationDirection, node, snapshot)];
293
- }));
294
- return {
295
- neighbors,
296
- unknowns: await Promise.all(traversal.boundaries.map((boundary) => (this.unknownBoundary(boundary, traversal, snapshot)))),
297
- };
298
- }
299
- async structuralNeighbor(relation, direction, node, snapshot) {
300
- return {
301
- depth: 1,
302
- direction,
303
- relation: {
304
- domain: "structural",
305
- from: { domain: "structural", id: relation.from.id },
306
- type: relation.type,
307
- to: { domain: "structural", id: relation.to.id },
308
- snapshotId: snapshot.snapshotId,
309
- certainty: null,
310
- validity: "valid",
311
- evidence: [],
312
- support: relation.support,
313
- },
314
- node: await this.structuralNode(node, snapshot),
315
- };
316
- }
317
182
  async structuralNode(node, snapshot) {
318
183
  return {
319
184
  domain: "structural",
@@ -326,33 +191,6 @@ export class WorldGraphQuery {
326
191
  support: node.support,
327
192
  };
328
193
  }
329
- async unknownBoundary(boundary, traversal, snapshot) {
330
- const owner = traversal.nodes.find((node) => node.reference.id === boundary.owner.id)
331
- ?? await this.#structural.getNode(boundary.owner);
332
- const path = boundary.path ?? owner?.path;
333
- const position = boundary.position ?? owner?.range.start;
334
- if (path === undefined || position === undefined) {
335
- throw new Error(`Unknown boundary ${boundary.reference.id} has no source owner`);
336
- }
337
- return {
338
- domain: "structural",
339
- id: boundary.reference.id,
340
- kind: "UnknownBoundary",
341
- label: `${boundary.operation} unresolved`,
342
- snapshotId: snapshot.snapshotId,
343
- validity: "unknown",
344
- owner: { domain: "structural", id: boundary.owner.id },
345
- operation: boundary.operation,
346
- reason: boundary.reason,
347
- location: {
348
- file: path,
349
- range: { start: position, end: position },
350
- contentHash: contentHashFor(path, snapshot),
351
- },
352
- candidates: boundary.candidates,
353
- support: boundary.support,
354
- };
355
- }
356
194
  }
357
195
  function createDependencies(repository, graph, structural) {
358
196
  return {
@@ -381,56 +219,22 @@ function directionForRelation(reference, relation) {
381
219
  }
382
220
  return sameReference(reference, relation.to) ? "incoming" : undefined;
383
221
  }
384
- function structuralRelationDirection(reference, relation) {
385
- if (relation.from.id === reference) {
386
- return "outgoing";
387
- }
388
- return relation.to.id === reference ? "incoming" : undefined;
389
- }
390
222
  function sameReference(left, right) {
391
223
  return referenceIdentity(left) === referenceIdentity(right);
392
224
  }
393
- function referenceForNode(node) {
394
- return node.domain === "business"
395
- ? { domain: "business", key: node.key }
396
- : { domain: "structural", id: node.id };
397
- }
398
225
  function referenceIdentity(reference) {
399
226
  return reference.domain === "business"
400
227
  ? `business:${reference.key}`
401
228
  : `structural:${reference.id}`;
402
229
  }
403
230
  function nodeIdentity(node) {
404
- return referenceIdentity(referenceForNode(node));
405
- }
406
- function relationIdentity(relation) {
407
- return [
408
- relation.domain,
409
- referenceIdentity(relation.from),
410
- relation.type,
411
- referenceIdentity(relation.to),
412
- ].join("\0");
231
+ return node.domain === "business" ? `business:${node.key}` : `structural:${node.id}`;
413
232
  }
414
233
  function compareNeighbors(left, right) {
415
234
  return left.depth - right.depth
416
235
  || left.relation.type.localeCompare(right.relation.type)
417
236
  || nodeIdentity(left.node).localeCompare(nodeIdentity(right.node));
418
237
  }
419
- function uniqueSearchResults(results) {
420
- const byNode = new Map();
421
- for (const result of results) {
422
- const identity = nodeIdentity(result.node);
423
- const existing = byNode.get(identity);
424
- if (existing === undefined || result.score > existing.score) {
425
- byNode.set(identity, result);
426
- }
427
- }
428
- return [...byNode.values()];
429
- }
430
- function uniqueNodes(nodes) {
431
- return [...new Map(nodes.map((node) => [nodeIdentity(node), node])).values()]
432
- .sort((left, right) => nodeIdentity(left).localeCompare(nodeIdentity(right)));
433
- }
434
238
  function reciprocalRank(index) {
435
239
  return 1 / (index + 1);
436
240
  }
@@ -444,10 +248,3 @@ function requireSearchLimit(limit) {
444
248
  }
445
249
  return resolved;
446
250
  }
447
- function requireTraversalDepth(depth) {
448
- const resolved = depth ?? 1;
449
- if (!Number.isInteger(resolved) || resolved < 1 || resolved > 3) {
450
- throw new Error("World graph traversal depth must be an integer from 1 through 3");
451
- }
452
- return resolved;
453
- }
package/docs/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Semantic Atlas Knowledge Base
2
+
3
+ This directory is the maintained knowledge layer for Semantic Atlas product
4
+ contracts, architecture, graph semantics, evaluation, and agent workflows. It
5
+ answers how the product behaves and why its boundaries exist; source code,
6
+ tests, generated schemas, and released artifacts remain the authority for the
7
+ current implementation.
8
+
9
+ Use [the documentation index](index.md) to find the owning page for a topic.
10
+ When a design changes, revise that owning page, update affected contracts and
11
+ incoming links, and record the knowledge change in [the maintenance log](log.md).
12
+
13
+ ## Source boundary
14
+
15
+ - Product decisions come from accepted design discussions and are expressed in
16
+ the product and graph contracts.
17
+ - Runtime claims are verified against source, tests, packaged CLI behavior, and
18
+ generated schemas.
19
+ - CodeGraph documentation describes an internal structural dependency; Semantic
20
+ Atlas public behavior remains owned by this repository.
21
+ - Unknown behavior and planned work stay explicitly marked rather than being
22
+ presented as implemented capability.
23
+
24
+ ## Maintenance rules
25
+
26
+ - Keep one authoritative page for each stable topic and link to it elsewhere.
27
+ - Start pages with the question they answer, their scope, and current status.
28
+ - Revise current conclusions in place; use `log.md` for chronology.
29
+ - Keep public documentation free of private repositories, internal operational
30
+ measurements, and maintainer-only delivery details.
@@ -74,9 +74,9 @@ private-worker execution does not degenerate into a process-per-node traversal.
74
74
 
75
75
  `WorldModelService` owns orchestration. `BusinessKnowledgeStore` owns Atlas tables and transactions. `WorldGraphQuery` resolves cross-domain references and returns the existing versioned CLI graph contract.
76
76
 
77
- `WorldGraphQuery` is the only read coordinator used by map and change workflows. It requires one published world snapshot, combines business hierarchy and evidence relations with backend traversal, and verifies that the published snapshot did not change while an asynchronous structural query was running. Structural results retain normalized support and provenance; business results retain certainty, validity, and evidence.
77
+ `WorldGraphQuery` is the read coordinator used by map, code-search, and change workflows. It requires one published world snapshot, projects the business hierarchy into semantic zoom views, resolves direct evidence links, delegates bounded structural search to the adapter, and verifies that the published snapshot did not change while an asynchronous structural query was running. Structural results retain normalized support and provenance; business results retain certainty, validity, and evidence.
78
78
 
79
- Lexical search uses deterministic reciprocal-rank fusion across Atlas business vocabulary and CodeGraph structural search. Raw backend scores are not compared across domains because their scales are implementation-specific. The query layer performs no embeddings, model calls, or natural-language inference.
79
+ Lexical search keeps its ownership domains explicit. `map search` ranks only Atlas business vocabulary. `code search` ranks only CodeGraph structural results and normalizes their order into bounded public scores. The query layer performs no cross-domain score fusion, embeddings, model calls, or natural-language inference.
80
80
 
81
81
  When CodeGraph has no explicit module or namespace nodes, the adapter exposes deterministic top-directory module roots with Atlas `module:` references. These roots are computed from the current backend file manifest and traverse to backend file nodes; they are not stored in Atlas tables or presented as business facts.
82
82
 
@@ -0,0 +1,240 @@
1
+ # 持续业务学习与演进式业务地图
2
+
3
+ 本页回答 Semantic Atlas 何时学习业务知识、业务根节点如何随认知演进、
4
+ 以及层级调整如何保持身份与证据稳定。适用于业务图查询、GraphPatch、
5
+ Agent Skill 和后续影响分析设计。状态:已接受,按垂直切片逐步实施。
6
+
7
+ ## 产品判断
8
+
9
+ Semantic Atlas 在真实工程任务中持续学习业务,不在首次索引时生成一套
10
+ 推测性的完整业务模型。真实任务同时提供产品意图、相关源码、测试、修改
11
+ 结果和复用价值,是形成可靠业务知识的最佳证据边界。
12
+
13
+ `index` 只发布当前工作树的结构投影、仓库快照和已有证据有效性。调用
14
+ Agent 负责理解自然语言和业务意义;CodeGraph 负责提供符号、引用、调用、
15
+ 模块和源码位置等结构事实。两者通过证据关系协作,但结构目录不替代业务
16
+ 地图。
17
+
18
+ 业务地图是一个持续成长并允许重组的心智模型:
19
+
20
+ ```text
21
+ 真实工程任务
22
+ -> 查询已有业务知识
23
+ -> 从 CodeGraph 与源码补足未知部分
24
+ -> 完成修改与验证
25
+ -> 提炼可复用业务知识
26
+ -> 新增、扩展、修正或重挂载业务子树
27
+ ```
28
+
29
+ ## 根节点语义
30
+
31
+ 根节点表示当前有效业务图中没有已知主要父节点的业务节点。它是查询时的
32
+ 结构状态,不是永久地位,也不是 `Capability` 等节点类型。
33
+
34
+ 第一次任务可能只验证“退款”,它暂时作为根节点;后续任务发现“退款”
35
+ 属于“订单”,再发现“订单”属于“Commerce”,地图可以持续演进:
36
+
37
+ ```text
38
+ Refunds
39
+
40
+ Orders
41
+ └── Refunds
42
+
43
+ Commerce
44
+ └── Orders
45
+ └── Refunds
46
+ ```
47
+
48
+ 世界层 `map view` 只把当前父节点为空的业务知识作为 root regions:
49
+
50
+ - 空业务图返回空数组和稳定的 `BUSINESS_KNOWLEDGE_EMPTY` 告警;
51
+ - 结构 `Module` 根节点通过 `code search` 参与源码回退,不进入业务 regions;
52
+ - 任意业务节点类型都可以在尚未发现父节点时暂时成为根;
53
+ - 新的上层知识可以在一个原子事务中把既有根节点变成子节点;
54
+ - 根节点变化不改变节点身份、证据或其他业务关系。
55
+
56
+ ## 稳定身份与可变位置
57
+
58
+ 业务身份的生命周期长于当前分类位置。v1 继续使用 `key` 作为仓库内稳定
59
+ 身份;key 中的斜杠只表达可读命名空间,不构成父子关系,也不随重挂载
60
+ 改写。权威层级只来自 `part_of` 关系。
61
+
62
+ 例如 `refunds` 或既有的 `commerce/refunds` 在挂到 `orders` 下时保持原 key。
63
+ 调用方通过稳定 key 继续引用该节点,展示路径则根据当前 `part_of` 链计算。
64
+ 后续版本只有在真实碰撞或跨仓库身份需求出现时,才引入独立 opaque ID;
65
+ 本阶段不为假设性需求迁移现有知识。
66
+
67
+ ## 业务层级不变量
68
+
69
+ `part_of` 是导航层级关系,并满足以下不变量:
70
+
71
+ 1. 一个业务节点最多拥有一个 outgoing `part_of` 主要父节点。
72
+ 2. 父节点和子节点都属于业务域并已存在或在同一 GraphPatch 中创建。
73
+ 3. `part_of` 图保持无环;节点不能成为自身或自身后代的父节点。
74
+ 4. 横向关系不受主要父节点限制,继续使用 `invokes`、`reads`、`writes`、
75
+ `publishes`、`consumes`、`constrained_by`、`realized_by` 和 `verified_by`。
76
+ 5. 层级变化保留节点 key、业务断言、证据和非层级关系。
77
+
78
+ 树形主要归属提供稳定的逐层导航;横向关系表达真实业务协作。需要从多个
79
+ 角度发现同一节点时,使用 aliases、搜索和横向关系,而不是创建多个主要
80
+ 父节点。
81
+
82
+ ## 原子重挂载
83
+
84
+ GraphPatch v1 已支持在一个事务中删除旧关系并新增关系。重挂载使用同一
85
+ patch 完成以下动作:
86
+
87
+ 1. 必要时 upsert 新的上层业务节点;
88
+ 2. remove 既有 outgoing `part_of`;
89
+ 3. upsert 指向新父节点的 `part_of`;
90
+ 4. 保持被移动节点及其子树 key 不变。
91
+
92
+ Atlas 在写入前根据删除和新增操作计算最终层级,并整体校验单父节点和无环
93
+ 不变量。任何一步失败都会拒绝完整 patch,旧地图保持不变。
94
+
95
+ 未来若多个 Agent 同时修改共享业务地图,需要在 GraphPatch 中加入独立于
96
+ 源码 snapshot 的 knowledge revision。当前 v1 继续依赖串行 Skill 工作流、
97
+ SQLite 写事务和完整 patch 校验;知识 revision 属于后续并发切片。
98
+
99
+ ## 任务驱动的知识协调
100
+
101
+ 每个受支持任务先查询已有业务图。地图不足时,Agent 显式使用结构搜索和
102
+ 有界源码阅读完成工程工作。在最终源码和测试得到验证后,Agent 对新理解
103
+ 执行一次知识协调:
104
+
105
+ | 决策 | 含义 |
106
+ | --- | --- |
107
+ | `reuse` | 已有节点和关系完整表达本次验证的业务意义 |
108
+ | `extend` | 给已有知识增加节点、断言或横向关系 |
109
+ | `introduce` | 当前没有可信上层概念,新增暂时的根节点 |
110
+ | `reparent` | 发现更高层归属,原子移动已有节点或子树 |
111
+ | `refine` | 修正 kind、label、summary、aliases 或 certainty |
112
+ | `transient` | 本次信息是实现细节、症状或未验证假设,不持久化 |
113
+
114
+ 创建节点前必须搜索近似业务词并查看世界 regions,减少重复根节点。发现更上层业务
115
+ 时优先重挂载现有知识,而不是复制一棵新子树。所有持久事实仍需要当前源码
116
+ 证据;结构不确定性保留为显式边界。
117
+
118
+ ## CodeGraph 的位置
119
+
120
+ CodeGraph 保持为 Semantic Atlas 内部结构证据层:
121
+
122
+ - 索引文件、符号和可静态证明的关系;
123
+ - 为业务断言提供 source range、content hash 和可重绑定身份;
124
+ - 在业务知识不足时提供有界源码种子;
125
+ - 从已选业务节点向调用方、消费者、数据和测试扩展未来 impact 查询。
126
+
127
+ CodeGraph 不命名业务、不决定业务父子关系,也不把目录层级呈现为业务地图。
128
+ 结构 exact 不自动成为业务 exact;无法证明的运行时行为继续保持
129
+ `UnknownBoundary`。
130
+
131
+ ## 一张地图与语义缩放
132
+
133
+ Semantic Atlas 保存一张权威业务图,不分别保存总览图和模块子图。`part_of`
134
+ 形成可逐层进入的区域层级,其他业务关系形成跨区域连接。查询根据当前焦点
135
+ 选择一个可见业务边界,同一底图因此可以像游戏地图一样连续缩放。
136
+
137
+ 世界视图显示当前 roots。聚焦某个节点后,视图显示它的直接子区域,并把与
138
+ 当前区域发生业务关系的外部节点折叠到最接近当前视野的业务分支。例如源码
139
+ 确认的关系是 `place-order reads user-profile`,在 Commerce 视图中可以投影为
140
+ `orders reads users`,在 Orders 视图中投影为
141
+ `place-order reads users`。
142
+
143
+ 投影关系满足以下规则:
144
+
145
+ 1. GraphPatch 只保存 Agent 明确验证的原始业务关系。
146
+ 2. 缩放视图把下层关系端点提升到当前可见区域,形成只读聚合连接。
147
+ 3. 同一对可见区域按 relation type 合并贡献关系,分别报告 direct 与
148
+ aggregated 数量、certainty 分布和 validity 分布。
149
+ 4. 投影后落在同一可见区域内的关系属于该区域内部细节,本层隐藏,放大后
150
+ 再显示。
151
+ 5. 聚合连接不是新的业务事实,不写回 GraphPatch,也不获得虚构的 certainty。
152
+ 6. breadcrumb 始终从当前 root 指向焦点;每个区域返回 direct child count 和
153
+ expandable 状态,让 Agent 明确下一次缩放入口。
154
+
155
+ 节点 kind 表达业务角色,不表达固定缩放级别。`Capability` 可以包含更细的
156
+ `Capability`,尚未发现父节点的 `Operation` 也可以暂时成为世界区域;视图
157
+ 完全根据当前 `part_of` 层级计算。
158
+
159
+ ## AI-first 查询契约
160
+
161
+ 公共 API 以调用 Agent 完成真实工程任务的最短可靠路径设计:
162
+
163
+ ```text
164
+ status -> index when needed
165
+ map view [business-key]
166
+ map search <business vocabulary>
167
+ map show <business-key>
168
+ code search <symbol or structural vocabulary> when business knowledge is insufficient
169
+ source confirmation -> engineering work -> index -> changes -> learn
170
+ ```
171
+
172
+ - `map view` 是主要导航入口。省略 key 时返回世界视图;提供 key 时向该业务
173
+ 区域放大一层,同时返回 breadcrumb、child/context regions 和聚合连接。
174
+ - `map search` 只搜索业务词汇并返回业务节点,避免代码符号淹没业务意图。
175
+ - `map show` 只接受业务 key,返回该业务断言、直接业务关系和直接结构证据,
176
+ 不递归展开 CodeGraph。
177
+ - `code search` 是业务知识为空或不足时的显式结构回退,返回有界源码种子。
178
+ - 源码读取、编辑和测试继续属于 Agent 的正常工程工具,不包装进 Atlas CLI。
179
+
180
+ `map roots`、`map children`、混合业务/结构的 `map search`、接受结构 ID 与任意
181
+ depth 的 `map show` 不再属于公共契约。当前没有外部使用者,本阶段直接替换
182
+ 旧 API,不增加 alias、兼容 schema 或双轨行为。
183
+
184
+ API 的首要 dogfood 场景是让一个没有预置 key 的 Agent 使用 Semantic Atlas
185
+ 理解并修改 Semantic Atlas 自身:先从世界视图寻找业务区域;知识不足时通过
186
+ `code search` 定位 `WorldGraphQuery` 等结构入口;完成修改后学习查询能力、
187
+ 层级和关系,使下一个 Agent 能从 `map view` 逐层到达实现证据。
188
+
189
+ ## 分阶段实现
190
+
191
+ ### Slice 1:业务 roots 与安全层级
192
+
193
+ - 世界视图只选择所有类型的 parentless business nodes;
194
+ - 空业务图返回 `BUSINESS_KNOWLEDGE_EMPTY`;
195
+ - GraphPatch 最终状态强制单父节点和无环;
196
+ - Skill 在真实任务后执行 `reuse/extend/introduce/reparent/refine/transient`;
197
+ - 文档明确 key 与位置分离。
198
+
199
+ ### Slice 2:语义缩放与 AI-first 查询视角
200
+
201
+ - 以 `map view` 替换 roots/children,生成 breadcrumb、可展开区域和聚合连接;
202
+ - 业务搜索不与结构结果混排,增加 `code search` 作为显式回退;
203
+ - `map show` 限制在业务关系和直接结构证据,避免结构图淹没业务语境;
204
+ - 使用 Semantic Atlas 对自身完成一次世界视图到源码修改再到知识写回的验证。
205
+
206
+ ### Slice 3:业务影响分析
207
+
208
+ - 从业务节点遍历 scenario、operation、data、interface 和 invariant;
209
+ - 通过 `realized_by`、`verified_by` 和 CodeGraph 扩展实现、调用者、消费者与
210
+ 测试;
211
+ - 分别报告 exact、inferred 和 unknown 影响,支持 Agent 制定修改范围。
212
+
213
+ ### Slice 4:并发知识 revision 与长期治理
214
+
215
+ - 为共享业务知识增加 optimistic knowledge revision;
216
+ - 显式记录 reparent、merge 和分类修正;
217
+ - 在真实重复知识或并发冲突出现后设计 merge 操作,不提前引入复杂治理。
218
+
219
+ ## 验收场景
220
+
221
+ 1. 新仓库完成 `index` 后,世界 `map view` 返回空业务图而不是 `src` 或 `libs`。
222
+ 2. 第一次任务学到一个 Operation 时,它可以作为暂时根节点被后续任务复用。
223
+ 3. 后续任务创建更高层 Capability,并在一个 GraphPatch 中把该 Operation
224
+ 重挂载到 Capability 下;Operation key 与证据保持不变。
225
+ 4. 同一 patch 尝试给节点添加两个主要父节点时整体失败。
226
+ 5. 同一 patch 尝试形成 `A -> B -> A` 层级环时整体失败。
227
+ 6. 世界视图只显示根区域;聚焦父节点时显示直接子区域、breadcrumb 和可展开
228
+ 状态。
229
+ 7. 两个深层节点的横向关系在上层视图聚合到各自可见祖先,放大后恢复更具体
230
+ 的端点;聚合连接保留贡献关系的 certainty 与 validity 分布。
231
+ 8. 一个 Fresh Agent 通过 `map view/search/show` 发现当前业务路径,并在业务
232
+ 知识不足时通过 `code search` 明确转入结构与源码回退。
233
+
234
+ ## 相关页面
235
+
236
+ - [产品契约](../product-contract.md)
237
+ - [图模型](../contracts/graph-model.md)
238
+ - [GraphPatch v1](../contracts/graph-patch-v1.md)
239
+ - [CLI v1](../contracts/cli-v1.md)
240
+ - [CodeGraph 后端边界](codegraph-backend.md)