@principal-ai/subsystems-core 0.51.0 → 0.51.2

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 CHANGED
@@ -6,7 +6,7 @@ Subsystem model types/schema and agent-session tooling for Principal AI.
6
6
 
7
7
  | Import | Use |
8
8
  |---|---|
9
- | `@principal-ai/subsystems-core` | Browser-safe subsystem model types + `isSubsystemModelDocument` |
9
+ | `@principal-ai/subsystems-core` | Browser-safe subsystem model/topic types, reference links + `isSubsystemModelDocument` |
10
10
  | `@principal-ai/subsystems-core/node` | OpenCode store, agent-session list/fetch/normalize, topic store, fixtures |
11
11
  | `@principal-ai/subsystems-core/pipeline` | Bun-safe session normalize/accumulate (no `better-sqlite3`) |
12
12
 
@@ -14,6 +14,42 @@ Subsystem model types/schema and agent-session tooling for Principal AI.
14
14
 
15
15
  Portable document schema: `schemas/subsystem-model.schema.json`
16
16
 
17
+ ## Topic references
18
+
19
+ `DraftTopic.references` attaches local topics, C4 sketches, and drawings by ID:
20
+
21
+ ```ts
22
+ import {
23
+ formatTopicReferenceLink,
24
+ parseTopicReferenceLink,
25
+ type TopicReference,
26
+ } from '@principal-ai/subsystems-core';
27
+
28
+ const reference: TopicReference = { kind: 'c4-sketch', sketchId: 'c4s-123-abc' };
29
+ formatTopicReferenceLink(reference); // principal://c4-sketch/c4s-123-abc
30
+ parseTopicReferenceLink('principal://topic/topic-123-abc');
31
+ // { kind: 'topic', topicId: 'topic-123-abc' }
32
+ parseTopicReferenceLink('principal://drawing/0b6f7c1e');
33
+ // { kind: 'drawing', drawingId: '0b6f7c1e' }
34
+ ```
35
+
36
+ A drawing ID is the file name, without extension, of a `.excalidraw` file in the
37
+ host's drawings folder (the desktop app uses `~/.alexandria/drawings/`). Titles
38
+ come from the drawing file's `appState.name` and are resolved by the host.
39
+
40
+ Create/update accepts `references`; updates replace the whole array (`[]` clears
41
+ it). Existing topics may omit the field. References are directional and local-only.
42
+ Targets stay separate artifacts; deleting a target leaves a missing reference,
43
+ not a cascading deletion. Existing `subsystemModels` associations are unchanged.
44
+
45
+ Markdown links such as `[Architecture](principal://c4-sketch/c4s-123-abc)`,
46
+ `[Whiteboard](principal://drawing/0b6f7c1e)`, and
47
+ `[Related](principal://topic/topic-123-abc)` use the same target shapes but are
48
+ independent of attachments. Helpers only format/parse hrefs; hosts must allow the
49
+ `principal` protocol in Markdown sanitizers and handle resolution, navigation,
50
+ target validation, deduplication, and self-reference rejection. Sketch references
51
+ do not turn authored hypotheses into evidence.
52
+
17
53
  ## Installation
18
54
 
19
55
  ```bash
package/dist/index.d.ts CHANGED
@@ -11,6 +11,8 @@
11
11
  * import { ... } from '@principal-ai/subsystems-core/pipeline'
12
12
  */
13
13
  export * from './types';
14
+ export type { DraftTopic, TopicAsset, TopicCreator, TopicSubsystemModelRef, TopicReference, } from './storage/topic-types';
15
+ export { formatTopicReferenceLink, parseTopicReferenceLink } from './storage/topic-reference-links';
14
16
  export { validateSubsystemModelCrossField } from './validation';
15
17
  export type { SubsystemValidationProblem } from './validation';
16
18
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,cAAc,SAAS,CAAC;AAKxB,OAAO,EAAE,gCAAgC,EAAE,MAAM,cAAc,CAAC;AAChE,YAAY,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,cAAc,SAAS,CAAC;AAExB,YAAY,EACV,UAAU,EACV,UAAU,EACV,YAAY,EACZ,sBAAsB,EACtB,cAAc,GACf,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAKpG,OAAO,EAAE,gCAAgC,EAAE,MAAM,cAAc,CAAC;AAChE,YAAY,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC"}
package/dist/index.js CHANGED
@@ -26,8 +26,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
26
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.validateSubsystemModelCrossField = void 0;
29
+ exports.validateSubsystemModelCrossField = exports.parseTopicReferenceLink = exports.formatTopicReferenceLink = void 0;
30
30
  __exportStar(require("./types"), exports);
31
+ var topic_reference_links_1 = require("./storage/topic-reference-links");
32
+ Object.defineProperty(exports, "formatTopicReferenceLink", { enumerable: true, get: function () { return topic_reference_links_1.formatTopicReferenceLink; } });
33
+ Object.defineProperty(exports, "parseTopicReferenceLink", { enumerable: true, get: function () { return topic_reference_links_1.parseTopicReferenceLink; } });
31
34
  // Cross-field validation (the rules the JSON Schema can't express). Structural
32
35
  // (schema) validation is enforced per surface against
33
36
  // `schemas/subsystem-model.schema.json`.
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;;;;;;;;;;;;;;;AAEH,0CAAwB;AAExB,+EAA+E;AAC/E,sDAAsD;AACtD,yCAAyC;AACzC,2CAAgE;AAAvD,8HAAA,gCAAgC,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;;;;;;;;;;;;;;;AAEH,0CAAwB;AASxB,yEAAoG;AAA3F,iIAAA,wBAAwB,OAAA;AAAE,gIAAA,uBAAuB,OAAA;AAE1D,+EAA+E;AAC/E,sDAAsD;AACtD,yCAAyC;AACzC,2CAAgE;AAAvD,8HAAA,gCAAgC,OAAA"}
package/dist/node.d.ts CHANGED
@@ -9,7 +9,8 @@
9
9
  export * from './types';
10
10
  export { TopicStore, PRINCIPAL_DIR, TOPICS_DIR, LEGACY_TOPICS_BLOB, } from './storage/topicStore';
11
11
  export type { TopicIndexEntry, TopicCreate, TopicUpdate, MigrationResult, } from './storage/topicStore';
12
- export type { DraftTopic, TopicAsset, TopicCreator, TopicSubsystemModelRef, } from './storage/topic-types';
12
+ export type { DraftTopic, TopicAsset, TopicCreator, TopicSubsystemModelRef, TopicReference, } from './storage/topic-types';
13
+ export { formatTopicReferenceLink, parseTopicReferenceLink } from './storage/topic-reference-links';
13
14
  export { OpenCodeEventStore, defaultOpenCodeDBPath } from './opencode/OpenCodeEventStore';
14
15
  export type { OpenCodeRawEvent, OpenCodeSessionEvents, OpenCodeEventRetriever, OpenCodeStoreOptions, SessionSummary, SessionListResult, } from './opencode/types';
15
16
  export { listAgentSessions, detectAgent, fetchRawEvents } from './opencode/agent-sessions';
@@ -1 +1 @@
1
- {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,cAAc,SAAS,CAAC;AAGxB,OAAO,EACL,UAAU,EACV,aAAa,EACb,UAAU,EACV,kBAAkB,GACnB,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EACV,eAAe,EACf,WAAW,EACX,WAAW,EACX,eAAe,GAChB,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EACV,UAAU,EACV,UAAU,EACV,YAAY,EACZ,sBAAsB,GACvB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAC1F,YAAY,EACV,gBAAgB,EAChB,qBAAqB,EACrB,sBAAsB,EACtB,oBAAoB,EACpB,cAAc,EACd,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3F,YAAY,EACV,mBAAmB,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AAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,YAAY,EACV,mBAAmB,EACnB,uBAAuB,EACvB,uBAAuB,EACvB,0BAA0B,EAC1B,wBAAwB,EACxB,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EACL,4BAA4B,EAC5B,eAAe,EACf,0BAA0B,EAC1B,gBAAgB,EAChB,mBAAmB,EACnB,6BAA6B,GAC9B,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC"}
package/dist/node.js CHANGED
@@ -22,7 +22,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
22
22
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.opencodeRowsToUniversalEvents = exports.collectRepositories = exports.accumulateEvents = exports.normalizeEventsWithAdapter = exports.normalizeEvents = exports.NodePathNormalizationAdapter = exports.buildAgentSessionFixture = exports.fetchRawEvents = exports.detectAgent = exports.listAgentSessions = exports.defaultOpenCodeDBPath = exports.OpenCodeEventStore = exports.LEGACY_TOPICS_BLOB = exports.TOPICS_DIR = exports.PRINCIPAL_DIR = exports.TopicStore = void 0;
25
+ exports.opencodeRowsToUniversalEvents = exports.collectRepositories = exports.accumulateEvents = exports.normalizeEventsWithAdapter = exports.normalizeEvents = exports.NodePathNormalizationAdapter = exports.buildAgentSessionFixture = exports.fetchRawEvents = exports.detectAgent = exports.listAgentSessions = exports.defaultOpenCodeDBPath = exports.OpenCodeEventStore = exports.parseTopicReferenceLink = exports.formatTopicReferenceLink = exports.LEGACY_TOPICS_BLOB = exports.TOPICS_DIR = exports.PRINCIPAL_DIR = exports.TopicStore = void 0;
26
26
  __exportStar(require("./types"), exports);
27
27
  // File-per-topic store (~/.principal/topics) — node:fs only
28
28
  var topicStore_1 = require("./storage/topicStore");
@@ -30,6 +30,9 @@ Object.defineProperty(exports, "TopicStore", { enumerable: true, get: function (
30
30
  Object.defineProperty(exports, "PRINCIPAL_DIR", { enumerable: true, get: function () { return topicStore_1.PRINCIPAL_DIR; } });
31
31
  Object.defineProperty(exports, "TOPICS_DIR", { enumerable: true, get: function () { return topicStore_1.TOPICS_DIR; } });
32
32
  Object.defineProperty(exports, "LEGACY_TOPICS_BLOB", { enumerable: true, get: function () { return topicStore_1.LEGACY_TOPICS_BLOB; } });
33
+ var topic_reference_links_1 = require("./storage/topic-reference-links");
34
+ Object.defineProperty(exports, "formatTopicReferenceLink", { enumerable: true, get: function () { return topic_reference_links_1.formatTopicReferenceLink; } });
35
+ Object.defineProperty(exports, "parseTopicReferenceLink", { enumerable: true, get: function () { return topic_reference_links_1.parseTopicReferenceLink; } });
33
36
  // OpenCode event retrieval (Node.js only - SQLite)
34
37
  var OpenCodeEventStore_1 = require("./opencode/OpenCodeEventStore");
35
38
  Object.defineProperty(exports, "OpenCodeEventStore", { enumerable: true, get: function () { return OpenCodeEventStore_1.OpenCodeEventStore; } });
package/dist/node.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;;;;;;;;;;;;;AAEH,0CAAwB;AAExB,4DAA4D;AAC5D,mDAK8B;AAJ5B,wGAAA,UAAU,OAAA;AACV,2GAAA,aAAa,OAAA;AACb,wGAAA,UAAU,OAAA;AACV,gHAAA,kBAAkB,OAAA;AAepB,mDAAmD;AACnD,oEAA0F;AAAjF,wHAAA,kBAAkB,OAAA;AAAE,2HAAA,qBAAqB,OAAA;AAUlD,iEAAiE;AACjE,4DAA2F;AAAlF,mHAAA,iBAAiB,OAAA;AAAE,6GAAA,WAAW,OAAA;AAAE,gHAAA,cAAc,OAAA;AAOvD,mDAAmD;AACnD,oDAAoE;AAA3D,mHAAA,wBAAwB,OAAA;AAWjC,sEAAsE;AACtE,gDAO6B;AAN3B,wHAAA,4BAA4B,OAAA;AAC5B,2GAAA,eAAe,OAAA;AACf,sHAAA,0BAA0B,OAAA;AAC1B,4GAAA,gBAAgB,OAAA;AAChB,+GAAA,mBAAmB,OAAA;AACnB,yHAAA,6BAA6B,OAAA"}
1
+ {"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;;;;;;;;;;;;;AAEH,0CAAwB;AAExB,4DAA4D;AAC5D,mDAK8B;AAJ5B,wGAAA,UAAU,OAAA;AACV,2GAAA,aAAa,OAAA;AACb,wGAAA,UAAU,OAAA;AACV,gHAAA,kBAAkB,OAAA;AAepB,yEAAoG;AAA3F,iIAAA,wBAAwB,OAAA;AAAE,gIAAA,uBAAuB,OAAA;AAE1D,mDAAmD;AACnD,oEAA0F;AAAjF,wHAAA,kBAAkB,OAAA;AAAE,2HAAA,qBAAqB,OAAA;AAUlD,iEAAiE;AACjE,4DAA2F;AAAlF,mHAAA,iBAAiB,OAAA;AAAE,6GAAA,WAAW,OAAA;AAAE,gHAAA,cAAc,OAAA;AAOvD,mDAAmD;AACnD,oDAAoE;AAA3D,mHAAA,wBAAwB,OAAA;AAWjC,sEAAsE;AACtE,gDAO6B;AAN3B,wHAAA,4BAA4B,OAAA;AAC5B,2GAAA,eAAe,OAAA;AACf,sHAAA,0BAA0B,OAAA;AAC1B,4GAAA,gBAAgB,OAAA;AAChB,+GAAA,mBAAmB,OAAA;AACnB,yHAAA,6BAA6B,OAAA"}
@@ -0,0 +1,10 @@
1
+ import type { TopicReference } from './topic-types';
2
+ /** Format a Markdown-safe href. Does not attach or resolve the target. */
3
+ export declare function formatTopicReferenceLink(reference: TopicReference): string;
4
+ /**
5
+ * Parse a local topic/sketch/drawing href without I/O. Unknown kinds, malformed
6
+ * escapes, extra path segments, queries, and fragments return null. Target
7
+ * existence and navigation belong to the host. Parsing never creates an attachment.
8
+ */
9
+ export declare function parseTopicReferenceLink(href: string): TopicReference | null;
10
+ //# sourceMappingURL=topic-reference-links.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"topic-reference-links.d.ts","sourceRoot":"","sources":["../../src/storage/topic-reference-links.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAmBpD,0EAA0E;AAC1E,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,cAAc,GAAG,MAAM,CAQ1E;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc,GAAG,IAAI,CAkB3E"}
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseTopicReferenceLink = exports.formatTopicReferenceLink = void 0;
4
+ // IDs are opaque single path segments, not paths. Decode exactly once and
5
+ // reject traversal, separators, whitespace, and control characters.
6
+ function isReferenceId(id) {
7
+ return id.length > 0 && id !== '.' && id !== '..' && !/[/\\\s\u0000-\u001f\u007f]/.test(id);
8
+ }
9
+ function referenceId(reference) {
10
+ switch (reference.kind) {
11
+ case 'topic':
12
+ return reference.topicId;
13
+ case 'c4-sketch':
14
+ return reference.sketchId;
15
+ case 'drawing':
16
+ return reference.drawingId;
17
+ }
18
+ }
19
+ /** Format a Markdown-safe href. Does not attach or resolve the target. */
20
+ function formatTopicReferenceLink(reference) {
21
+ const id = referenceId(reference);
22
+ if (!isReferenceId(id))
23
+ throw new Error('Invalid topic reference id');
24
+ const encoded = encodeURIComponent(id).replace(/[!'()*]/g, (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`);
25
+ return `principal://${reference.kind}/${encoded}`;
26
+ }
27
+ exports.formatTopicReferenceLink = formatTopicReferenceLink;
28
+ /**
29
+ * Parse a local topic/sketch/drawing href without I/O. Unknown kinds, malformed
30
+ * escapes, extra path segments, queries, and fragments return null. Target
31
+ * existence and navigation belong to the host. Parsing never creates an attachment.
32
+ */
33
+ function parseTopicReferenceLink(href) {
34
+ const match = /^principal:\/\/(topic|c4-sketch|drawing)\/([^/?#]+)$/.exec(href);
35
+ if (!match)
36
+ return null;
37
+ let id;
38
+ try {
39
+ id = decodeURIComponent(match[2]);
40
+ }
41
+ catch {
42
+ return null;
43
+ }
44
+ if (!isReferenceId(id))
45
+ return null;
46
+ switch (match[1]) {
47
+ case 'topic':
48
+ return { kind: 'topic', topicId: id };
49
+ case 'c4-sketch':
50
+ return { kind: 'c4-sketch', sketchId: id };
51
+ default:
52
+ return { kind: 'drawing', drawingId: id };
53
+ }
54
+ }
55
+ exports.parseTopicReferenceLink = parseTopicReferenceLink;
56
+ //# sourceMappingURL=topic-reference-links.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"topic-reference-links.js","sourceRoot":"","sources":["../../src/storage/topic-reference-links.ts"],"names":[],"mappings":";;;AAEA,0EAA0E;AAC1E,oEAAoE;AACpE,SAAS,aAAa,CAAC,EAAU;IAC/B,OAAO,EAAE,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,EAAE,KAAK,IAAI,IAAI,CAAC,4BAA4B,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC9F,CAAC;AAED,SAAS,WAAW,CAAC,SAAyB;IAC5C,QAAQ,SAAS,CAAC,IAAI,EAAE;QACtB,KAAK,OAAO;YACV,OAAO,SAAS,CAAC,OAAO,CAAC;QAC3B,KAAK,WAAW;YACd,OAAO,SAAS,CAAC,QAAQ,CAAC;QAC5B,KAAK,SAAS;YACZ,OAAO,SAAS,CAAC,SAAS,CAAC;KAC9B;AACH,CAAC;AAED,0EAA0E;AAC1E,SAAgB,wBAAwB,CAAC,SAAyB;IAChE,MAAM,EAAE,GAAG,WAAW,CAAC,SAAS,CAAC,CAAC;IAClC,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;IACtE,MAAM,OAAO,GAAG,kBAAkB,CAAC,EAAE,CAAC,CAAC,OAAO,CAC5C,UAAU,EACV,CAAC,SAAS,EAAE,EAAE,CAAC,IAAI,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,WAAW,EAAE,EAAE,CACxE,CAAC;IACF,OAAO,eAAe,SAAS,CAAC,IAAI,IAAI,OAAO,EAAE,CAAC;AACpD,CAAC;AARD,4DAQC;AAED;;;;GAIG;AACH,SAAgB,uBAAuB,CAAC,IAAY;IAClD,MAAM,KAAK,GAAG,sDAAsD,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAChF,IAAI,CAAC,KAAK;QAAE,OAAO,IAAI,CAAC;IACxB,IAAI,EAAU,CAAC;IACf,IAAI;QACF,EAAE,GAAG,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;KACnC;IAAC,MAAM;QACN,OAAO,IAAI,CAAC;KACb;IACD,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC;QAAE,OAAO,IAAI,CAAC;IACpC,QAAQ,KAAK,CAAC,CAAC,CAAC,EAAE;QAChB,KAAK,OAAO;YACV,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;QACxC,KAAK,WAAW;YACd,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC;QAC7C;YACE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC;KAC7C;AACH,CAAC;AAlBD,0DAkBC"}
@@ -55,6 +55,22 @@ export interface TopicSubsystemModelRef {
55
55
  /** Specific runtime trails (flows) within the model, if narrower than the whole. */
56
56
  trailIds?: string[];
57
57
  }
58
+ /**
59
+ * A directional, local-only reference owned by a topic. Targets remain separate
60
+ * artifacts; their contents and titles are resolved by the host, not copied here.
61
+ * Referencing a C4 sketch does not make its authored hypothesis evidence.
62
+ * A drawing ID is the `.excalidraw` file stem under the host's drawings folder.
63
+ */
64
+ export type TopicReference = {
65
+ kind: 'c4-sketch';
66
+ sketchId: string;
67
+ } | {
68
+ kind: 'drawing';
69
+ drawingId: string;
70
+ } | {
71
+ kind: 'topic';
72
+ topicId: string;
73
+ };
58
74
  /**
59
75
  * Locally-authored topic — the shape desktop persists to
60
76
  * `~/.principal/topics/<id>.json` and edits.
@@ -91,5 +107,11 @@ export interface DraftTopic {
91
107
  * the referenced models are a local artifact.
92
108
  */
93
109
  subsystemModels?: TopicSubsystemModelRef[];
110
+ /**
111
+ * Attached topics, C4 sketches, and drawings. Whole-array replacement on update; `[]`
112
+ * clears attachments. Independent of links in `description`: neither edits
113
+ * the other. Hosts validate target existence and reject self-references.
114
+ */
115
+ references?: TopicReference[];
94
116
  }
95
117
  //# sourceMappingURL=topic-types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"topic-types.d.ts","sourceRoot":"","sources":["../../src/storage/topic-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;;;;;GAMG;AACH,MAAM,WAAW,UAAU;IACzB,kEAAkE;IAClE,EAAE,EAAE,MAAM,CAAC;IACX,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,4BAA4B;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,uEAAuE;IACvE,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,yBAAyB;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,sEAAsE;IACtE,MAAM,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACtD;AAED;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,sBAAsB;IACrC,kDAAkD;IAClD,OAAO,EAAE,MAAM,CAAC;IAChB,oFAAoF;IACpF,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,UAAU;IACzB,mDAAmD;IACnD,EAAE,EAAE,MAAM,CAAC;IACX,qBAAqB;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,8BAA8B;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,gBAAgB;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,sEAAsE;IACtE,SAAS,CAAC,EAAE,YAAY,CAAC;IACzB,mFAAmF;IACnF,MAAM,CAAC,EAAE,UAAU,EAAE,CAAC;IACtB;;;OAGG;IACH,eAAe,CAAC,EAAE,sBAAsB,EAAE,CAAC;CAC5C"}
1
+ {"version":3,"file":"topic-types.d.ts","sourceRoot":"","sources":["../../src/storage/topic-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;;;;;GAMG;AACH,MAAM,WAAW,UAAU;IACzB,kEAAkE;IAClE,EAAE,EAAE,MAAM,CAAC;IACX,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,4BAA4B;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,uEAAuE;IACvE,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,yBAAyB;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,sEAAsE;IACtE,MAAM,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACtD;AAED;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,sBAAsB;IACrC,kDAAkD;IAClD,OAAO,EAAE,MAAM,CAAC;IAChB,oFAAoF;IACpF,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB;AAED;;;;;GAKG;AACH,MAAM,MAAM,cAAc,GACtB;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GACvC;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GACtC;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvC;;;;;;;GAOG;AACH,MAAM,WAAW,UAAU;IACzB,mDAAmD;IACnD,EAAE,EAAE,MAAM,CAAC;IACX,qBAAqB;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,8BAA8B;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,gBAAgB;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,sEAAsE;IACtE,SAAS,CAAC,EAAE,YAAY,CAAC;IACzB,mFAAmF;IACnF,MAAM,CAAC,EAAE,UAAU,EAAE,CAAC;IACtB;;;OAGG;IACH,eAAe,CAAC,EAAE,sBAAsB,EAAE,CAAC;IAC3C;;;;OAIG;IACH,UAAU,CAAC,EAAE,cAAc,EAAE,CAAC;CAC/B"}
@@ -64,7 +64,7 @@ export interface MigrationResult {
64
64
  * Fields a caller may set when updating a topic. `id` and `createdAt` are set
65
65
  * once at creation and cannot be changed by an update.
66
66
  */
67
- export type TopicUpdate = Partial<Pick<DraftTopic, 'title' | 'description' | 'createdBy' | 'assets' | 'repos' | 'subsystemModels'>>;
67
+ export type TopicUpdate = Partial<Pick<DraftTopic, 'title' | 'description' | 'createdBy' | 'assets' | 'repos' | 'subsystemModels' | 'references'>>;
68
68
  /**
69
69
  * Fields a caller may set when creating a topic. `id`, `createdAt`,
70
70
  * `updatedAt` are filled in when omitted; passing `id` lets a caller control
@@ -1 +1 @@
1
- {"version":3,"file":"topicStore.d.ts","sourceRoot":"","sources":["../../src/storage/topicStore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAKH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAEhD,gEAAgE;AAChE,eAAO,MAAM,aAAa,QAAgC,CAAC;AAC3D,gCAAgC;AAChC,eAAO,MAAM,UAAU,QAAgC,CAAC;AACxD,wDAAwD;AACxD,eAAO,MAAM,kBAAkB,QAAgD,CAAC;AAKhF;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,kBAAkB,EAAE,MAAM,CAAC;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,CAAC;IACtD,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,kEAAkE;IAClE,QAAQ,EAAE,MAAM,CAAC;CAClB;AA0CD,MAAM,WAAW,eAAe;IAC9B,4DAA4D;IAC5D,QAAQ,EAAE,MAAM,CAAC;IACjB,mEAAmE;IACnE,OAAO,EAAE,MAAM,CAAC;IAChB,0EAA0E;IAC1E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,qDAAqD;IACrD,YAAY,EAAE,OAAO,CAAC;CACvB;AAED;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,OAAO,CAC/B,IAAI,CACF,UAAU,EACV,OAAO,GAAG,aAAa,GAAG,WAAW,GAAG,QAAQ,GAAG,OAAO,GAAG,iBAAiB,CAC/E,CACF,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,MAAM,WAAW,GAAG,IAA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Y;CAWrB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@principal-ai/subsystems-core",
3
- "version": "0.51.0",
3
+ "version": "0.51.2",
4
4
  "description": "Subsystem model types and agent-session tooling for Principal AI",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
package/src/index.ts CHANGED
@@ -13,6 +13,15 @@
13
13
 
14
14
  export * from './types';
15
15
 
16
+ export type {
17
+ DraftTopic,
18
+ TopicAsset,
19
+ TopicCreator,
20
+ TopicSubsystemModelRef,
21
+ TopicReference,
22
+ } from './storage/topic-types';
23
+ export { formatTopicReferenceLink, parseTopicReferenceLink } from './storage/topic-reference-links';
24
+
16
25
  // Cross-field validation (the rules the JSON Schema can't express). Structural
17
26
  // (schema) validation is enforced per surface against
18
27
  // `schemas/subsystem-model.schema.json`.
package/src/node.ts CHANGED
@@ -27,7 +27,9 @@ export type {
27
27
  TopicAsset,
28
28
  TopicCreator,
29
29
  TopicSubsystemModelRef,
30
+ TopicReference,
30
31
  } from './storage/topic-types';
32
+ export { formatTopicReferenceLink, parseTopicReferenceLink } from './storage/topic-reference-links';
31
33
 
32
34
  // OpenCode event retrieval (Node.js only - SQLite)
33
35
  export { OpenCodeEventStore, defaultOpenCodeDBPath } from './opencode/OpenCodeEventStore';
@@ -0,0 +1,53 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import { formatTopicReferenceLink, parseTopicReferenceLink } from './topic-reference-links';
3
+ import type { TopicReference } from './topic-types';
4
+
5
+ describe('topic reference links', () => {
6
+ test('formats topic, C4 sketch, and drawing links', () => {
7
+ expect(formatTopicReferenceLink({ kind: 'topic', topicId: 'topic-123-abc' }))
8
+ .toBe('principal://topic/topic-123-abc');
9
+ expect(formatTopicReferenceLink({ kind: 'c4-sketch', sketchId: 'c4s-123-abc' }))
10
+ .toBe('principal://c4-sketch/c4s-123-abc');
11
+ expect(formatTopicReferenceLink({ kind: 'drawing', drawingId: '0b6f7c1e-2a4d-4e8f-9c3b-1d2e3f4a5b6c' }))
12
+ .toBe('principal://drawing/0b6f7c1e-2a4d-4e8f-9c3b-1d2e3f4a5b6c');
13
+ });
14
+
15
+ test('round-trips opaque ids and escapes Markdown delimiters', () => {
16
+ const references: TopicReference[] = [
17
+ { kind: 'topic', topicId: 'topic-123-abc' },
18
+ { kind: 'c4-sketch', sketchId: 'c4s-123-abc' },
19
+ { kind: 'drawing', drawingId: 'drawing-123' },
20
+ { kind: 'topic', topicId: "topic-é(1)!'?#%" },
21
+ { kind: 'topic', topicId: 'topic-%2F' },
22
+ ];
23
+ for (const reference of references) {
24
+ const href = formatTopicReferenceLink(reference);
25
+ expect(href).not.toMatch(/[()!'?#]/);
26
+ expect(parseTopicReferenceLink(href)).toEqual(reference);
27
+ }
28
+ });
29
+
30
+ test('rejects invalid ids when formatting', () => {
31
+ for (const topicId of ['', '.', '..', 'a/b', 'a\\b', ' a', 'a b', 'a\n', 'a\u0000']) {
32
+ expect(() => formatTopicReferenceLink({ kind: 'topic', topicId })).toThrow();
33
+ }
34
+ expect(() => formatTopicReferenceLink({ kind: 'drawing', drawingId: '../d1' })).toThrow();
35
+ });
36
+
37
+ test('returns null for unsupported or malformed hrefs', () => {
38
+ for (const href of [
39
+ '', 'https://example.com', 'pkg:github/acme/repo',
40
+ 'principal://model/sg-1', 'principal://topic/', 'principal://topic',
41
+ 'principal://topic/a/b', 'principal://topic/a/', 'principal://topic/a?q=1',
42
+ 'principal://topic/a#heading', 'principal://topic/..', 'principal://topic/%2e%2e',
43
+ 'principal://topic/%2F', 'principal://topic/%5C', 'principal://topic/%00',
44
+ 'principal://topic/%20', 'principal://topic/a\n', 'principal://topic/%',
45
+ 'principal://topic/%GG', 'principal://topic/%FF',
46
+ ' principal://topic/a', 'principal://topic@host/a',
47
+ 'principal://drawing/', 'principal://drawing/a/b', 'principal://drawing/..',
48
+ 'principal://drawings/a',
49
+ ]) {
50
+ expect(parseTopicReferenceLink(href)).toBeNull();
51
+ }
52
+ });
53
+ });
@@ -0,0 +1,54 @@
1
+ import type { TopicReference } from './topic-types';
2
+
3
+ // IDs are opaque single path segments, not paths. Decode exactly once and
4
+ // reject traversal, separators, whitespace, and control characters.
5
+ function isReferenceId(id: string): boolean {
6
+ return id.length > 0 && id !== '.' && id !== '..' && !/[/\\\s\u0000-\u001f\u007f]/.test(id);
7
+ }
8
+
9
+ function referenceId(reference: TopicReference): string {
10
+ switch (reference.kind) {
11
+ case 'topic':
12
+ return reference.topicId;
13
+ case 'c4-sketch':
14
+ return reference.sketchId;
15
+ case 'drawing':
16
+ return reference.drawingId;
17
+ }
18
+ }
19
+
20
+ /** Format a Markdown-safe href. Does not attach or resolve the target. */
21
+ export function formatTopicReferenceLink(reference: TopicReference): string {
22
+ const id = referenceId(reference);
23
+ if (!isReferenceId(id)) throw new Error('Invalid topic reference id');
24
+ const encoded = encodeURIComponent(id).replace(
25
+ /[!'()*]/g,
26
+ (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`,
27
+ );
28
+ return `principal://${reference.kind}/${encoded}`;
29
+ }
30
+
31
+ /**
32
+ * Parse a local topic/sketch/drawing href without I/O. Unknown kinds, malformed
33
+ * escapes, extra path segments, queries, and fragments return null. Target
34
+ * existence and navigation belong to the host. Parsing never creates an attachment.
35
+ */
36
+ export function parseTopicReferenceLink(href: string): TopicReference | null {
37
+ const match = /^principal:\/\/(topic|c4-sketch|drawing)\/([^/?#]+)$/.exec(href);
38
+ if (!match) return null;
39
+ let id: string;
40
+ try {
41
+ id = decodeURIComponent(match[2]);
42
+ } catch {
43
+ return null;
44
+ }
45
+ if (!isReferenceId(id)) return null;
46
+ switch (match[1]) {
47
+ case 'topic':
48
+ return { kind: 'topic', topicId: id };
49
+ case 'c4-sketch':
50
+ return { kind: 'c4-sketch', sketchId: id };
51
+ default:
52
+ return { kind: 'drawing', drawingId: id };
53
+ }
54
+ }
@@ -56,6 +56,17 @@ export interface TopicSubsystemModelRef {
56
56
  trailIds?: string[];
57
57
  }
58
58
 
59
+ /**
60
+ * A directional, local-only reference owned by a topic. Targets remain separate
61
+ * artifacts; their contents and titles are resolved by the host, not copied here.
62
+ * Referencing a C4 sketch does not make its authored hypothesis evidence.
63
+ * A drawing ID is the `.excalidraw` file stem under the host's drawings folder.
64
+ */
65
+ export type TopicReference =
66
+ | { kind: 'c4-sketch'; sketchId: string }
67
+ | { kind: 'drawing'; drawingId: string }
68
+ | { kind: 'topic'; topicId: string };
69
+
59
70
  /**
60
71
  * Locally-authored topic — the shape desktop persists to
61
72
  * `~/.principal/topics/<id>.json` and edits.
@@ -92,4 +103,10 @@ export interface DraftTopic {
92
103
  * the referenced models are a local artifact.
93
104
  */
94
105
  subsystemModels?: TopicSubsystemModelRef[];
106
+ /**
107
+ * Attached topics, C4 sketches, and drawings. Whole-array replacement on update; `[]`
108
+ * clears attachments. Independent of links in `description`: neither edits
109
+ * the other. Hosts validate target existence and reject self-references.
110
+ */
111
+ references?: TopicReference[];
95
112
  }
@@ -7,6 +7,7 @@ import { mkdtempSync, rmSync, writeFileSync, existsSync, readFileSync } from 'fs
7
7
  import { tmpdir } from 'os';
8
8
  import { join } from 'path';
9
9
  import { TopicStore } from './topicStore';
10
+ import type { TopicReference } from './topic-types';
10
11
 
11
12
  let dir: string;
12
13
  let topicsDir: string;
@@ -91,6 +92,57 @@ describe('CRUD', () => {
91
92
  expect(updated.subsystemModels).toEqual([{ modelId: 'sg-2-def' }]);
92
93
  });
93
94
 
95
+ test('references survive fresh reads and unrelated updates, and can be replaced or cleared', async () => {
96
+ const store = makeStore();
97
+ const references: TopicReference[] = [
98
+ { kind: 'c4-sketch', sketchId: 'c4s-1-abc' },
99
+ { kind: 'drawing', drawingId: 'drawing-1' },
100
+ { kind: 'topic', topicId: 'topic-related' },
101
+ ];
102
+ const created = await store.createTopic({
103
+ id: 'topic-refs',
104
+ title: 'Architecture',
105
+ description: '[Related](principal://topic/topic-related)',
106
+ subsystemModels: [{ modelId: 'sg-1-abc' }],
107
+ references,
108
+ });
109
+ expect(created.references).toEqual(references);
110
+ expect((await makeStore().getTopic(created.id))?.references).toEqual(references);
111
+
112
+ await store.updateTopic(created.id, { title: 'Renamed' });
113
+ expect((await makeStore().getTopic(created.id))?.references).toEqual(references);
114
+
115
+ const replacement: TopicReference[] = [{ kind: 'topic', topicId: 'topic-other' }];
116
+ await store.updateTopic(created.id, { references: replacement });
117
+ expect((await makeStore().getTopic(created.id))?.references).toEqual(replacement);
118
+
119
+ await store.updateTopic(created.id, { references: [] });
120
+ expect(await makeStore().getTopic(created.id)).toMatchObject({
121
+ references: [],
122
+ description: created.description,
123
+ subsystemModels: created.subsystemModels,
124
+ });
125
+ });
126
+
127
+ test('existing topics need no references migration', async () => {
128
+ const store = makeStore();
129
+ const topic = await store.createTopic({ title: 'Without attachments' });
130
+ expect(topic).not.toHaveProperty('references');
131
+ await store.updateTopic(topic.id, { description: 'Still without attachments' });
132
+ expect(await makeStore().getTopic(topic.id)).not.toHaveProperty('references');
133
+ });
134
+
135
+ test('deleting a referenced topic preserves the attachment for missing-target display', async () => {
136
+ const store = makeStore();
137
+ const target = await store.createTopic({ title: 'Target' });
138
+ const owner = await store.createTopic({
139
+ title: 'Owner',
140
+ references: [{ kind: 'topic', topicId: target.id }],
141
+ });
142
+ await store.deleteTopic(target.id);
143
+ expect((await makeStore().getTopic(owner.id))?.references).toEqual(owner.references);
144
+ });
145
+
94
146
  test('delete removes the file and the index entry', async () => {
95
147
  const store = makeStore();
96
148
  await store.createTopic({ id: 'topic-d', title: 'D' });
@@ -155,37 +207,6 @@ describe('index rebuild', () => {
155
207
  'topic-external',
156
208
  );
157
209
  });
158
-
159
- test('legacy topic files preserve unknown fields verbatim', async () => {
160
- const store = makeStore();
161
- await store.createTopic({
162
- id: 'topic-legacy',
163
- title: 'Legacy',
164
- repos: ['pkg:github/a/b'],
165
- });
166
- const topicPath = join(topicsDir, 'topic-legacy.json');
167
- const legacy = {
168
- ...JSON.parse(readFileSync(topicPath, 'utf8')),
169
- trailIds: ['old-trail'],
170
- status: { state: 'working', label: 'in flight' },
171
- description: 'Keep this description',
172
- };
173
- writeFileSync(topicPath, JSON.stringify(legacy), 'utf8');
174
-
175
- const loaded = await store.getTopic('topic-legacy');
176
-
177
- expect(loaded).toMatchObject({
178
- id: 'topic-legacy',
179
- title: 'Legacy',
180
- description: 'Keep this description',
181
- repos: ['pkg:github/a/b'],
182
- });
183
- expect(loaded).toHaveProperty('trailIds', ['old-trail']);
184
- // `status` was removed from the contract; legacy files still load and are
185
- // read back verbatim until the desktop cleans them up.
186
- expect(loaded).toHaveProperty('status', { state: 'working', label: 'in flight' });
187
- expect(JSON.parse(readFileSync(topicPath, 'utf8'))).toHaveProperty('trailIds', ['old-trail']);
188
- });
189
210
  });
190
211
 
191
212
  describe('migration', () => {
@@ -115,7 +115,7 @@ export interface MigrationResult {
115
115
  export type TopicUpdate = Partial<
116
116
  Pick<
117
117
  DraftTopic,
118
- 'title' | 'description' | 'createdBy' | 'assets' | 'repos' | 'subsystemModels'
118
+ 'title' | 'description' | 'createdBy' | 'assets' | 'repos' | 'subsystemModels' | 'references'
119
119
  >
120
120
  >;
121
121