@principal-ai/subsystems-react 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/dist/index.d.ts +6 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -4
- package/dist/index.js.map +1 -1
- package/dist/pierre/PierreTrailCodeView.d.ts +1 -1
- package/dist/pierre/PierreTrailCodeView.d.ts.map +1 -1
- package/dist/pierre/PierreTrailCodeView.js +44 -21
- package/dist/pierre/PierreTrailCodeView.js.map +1 -1
- package/dist/stories/Subsystem/C4Graph/c4ProposedElements.fixture.d.ts +493 -0
- package/dist/stories/Subsystem/C4Graph/c4ProposedElements.fixture.d.ts.map +1 -0
- package/dist/stories/Subsystem/C4Graph/c4ProposedElements.fixture.js +329 -0
- package/dist/stories/Subsystem/C4Graph/c4ProposedElements.fixture.js.map +1 -0
- package/dist/subsystem/C4Graph.d.ts +2 -2
- package/dist/subsystem/C4Graph.d.ts.map +1 -1
- package/dist/subsystem/C4Graph.js +25 -16
- package/dist/subsystem/C4Graph.js.map +1 -1
- package/dist/subsystem/C4NodeCard.d.ts +85 -35
- package/dist/subsystem/C4NodeCard.d.ts.map +1 -1
- package/dist/subsystem/C4NodeCard.js +112 -83
- package/dist/subsystem/C4NodeCard.js.map +1 -1
- package/dist/subsystem/FileDrawer.d.ts.map +1 -1
- package/dist/subsystem/FileDrawer.js +20 -21
- package/dist/subsystem/FileDrawer.js.map +1 -1
- package/dist/subsystem/IssueList.d.ts.map +1 -1
- package/dist/subsystem/IssueList.js +12 -3
- package/dist/subsystem/IssueList.js.map +1 -1
- package/dist/subsystem/SubsystemComponentGraph.d.ts.map +1 -1
- package/dist/subsystem/SubsystemComponentGraph.js +85 -7
- package/dist/subsystem/SubsystemComponentGraph.js.map +1 -1
- package/dist/subsystem/c4.d.ts +273 -0
- package/dist/subsystem/c4.d.ts.map +1 -0
- package/dist/subsystem/c4.js +322 -0
- package/dist/subsystem/c4.js.map +1 -0
- package/dist/subsystem/c4Evidence.d.ts +143 -0
- package/dist/subsystem/c4Evidence.d.ts.map +1 -0
- package/dist/subsystem/c4Evidence.js +240 -0
- package/dist/subsystem/c4Evidence.js.map +1 -0
- package/dist/subsystem/model.d.ts +2 -6
- package/dist/subsystem/model.d.ts.map +1 -1
- package/dist/subsystem/model.js.map +1 -1
- package/package.json +5 -4
- package/src/index.ts +26 -28
- package/src/pierre/PierreTrailCodeView.tsx +49 -21
- package/src/stories/Subsystem/C4Graph/C4NodeGallery.stories.tsx +171 -80
- package/src/stories/Subsystem/C4Graph/ProposedElements.stories.tsx +293 -0
- package/src/stories/Subsystem/C4Graph/c4ProposedElements.fixture.ts +366 -0
- package/src/stories/Subsystem/ComponentGraph/IssueOverlay.stories.tsx +1 -1
- package/src/stories/Subsystem/ComponentGraph/Issues.stories.tsx +169 -3
- package/src/subsystem/C4Graph.tsx +39 -24
- package/src/subsystem/C4NodeCard.test.ts +204 -62
- package/src/subsystem/C4NodeCard.tsx +146 -101
- package/src/subsystem/FileDrawer.tsx +32 -34
- package/src/subsystem/IssueList.tsx +13 -2
- package/src/subsystem/SubsystemComponentGraph.tsx +98 -7
- package/src/subsystem/c4.test.ts +573 -0
- package/src/subsystem/c4.ts +559 -0
- package/src/subsystem/c4Evidence.test.ts +386 -0
- package/src/subsystem/c4Evidence.ts +329 -0
- package/src/subsystem/model.ts +2 -6
- package/dist/stories/Subsystem/C4Graph/c4Associations.fixture.d.ts +0 -95
- package/dist/stories/Subsystem/C4Graph/c4Associations.fixture.d.ts.map +0 -1
- package/dist/stories/Subsystem/C4Graph/c4Associations.fixture.js +0 -340
- package/dist/stories/Subsystem/C4Graph/c4Associations.fixture.js.map +0 -1
- package/dist/stories/Subsystem/C4Graph/c4Fixture.d.ts +0 -7
- package/dist/stories/Subsystem/C4Graph/c4Fixture.d.ts.map +0 -1
- package/dist/stories/Subsystem/C4Graph/c4Fixture.js +0 -7335
- package/dist/stories/Subsystem/C4Graph/c4Fixture.js.map +0 -1
- package/dist/stories/Subsystem/C4Graph/generateFixture.d.ts +0 -2
- package/dist/stories/Subsystem/C4Graph/generateFixture.d.ts.map +0 -1
- package/dist/stories/Subsystem/C4Graph/generateFixture.js +0 -145
- package/dist/stories/Subsystem/C4Graph/generateFixture.js.map +0 -1
- package/dist/subsystem/c4Associations.d.ts +0 -186
- package/dist/subsystem/c4Associations.d.ts.map +0 -1
- package/dist/subsystem/c4Associations.js +0 -284
- package/dist/subsystem/c4Associations.js.map +0 -1
- package/dist/subsystem/toC4.d.ts +0 -214
- package/dist/subsystem/toC4.d.ts.map +0 -1
- package/dist/subsystem/toC4.js +0 -282
- package/dist/subsystem/toC4.js.map +0 -1
- package/src/stories/Subsystem/C4Graph/C4Associations.stories.tsx +0 -273
- package/src/stories/Subsystem/C4Graph/C4Container.stories.tsx +0 -101
- package/src/stories/Subsystem/C4Graph/c4Associations.fixture.ts +0 -344
- package/src/stories/Subsystem/C4Graph/c4Fixture.ts +0 -7339
- package/src/stories/Subsystem/C4Graph/generateFixture.ts +0 -161
- package/src/subsystem/c4Associations.test.ts +0 -484
- package/src/subsystem/c4Associations.ts +0 -398
- package/src/subsystem/toC4.test.ts +0 -114
- package/src/subsystem/toC4.ts +0 -478
|
@@ -1,398 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* C4 associations — the confirmed, human-approved layer.
|
|
3
|
-
*
|
|
4
|
-
* `toC4.ts` derives a C4 projection from the flat document, but a derivation
|
|
5
|
-
* cannot be right about architecture on its own. The document says *what a
|
|
6
|
-
* symbol is* and *where it runs*; it does not say whether two runs are one
|
|
7
|
-
* deployable unit, whether a library is a container, or whether a store is a
|
|
8
|
-
* C4 data store.
|
|
9
|
-
*
|
|
10
|
-
* So the derivation proposes, and this module records what a person (or an
|
|
11
|
-
* agent a person approved) concluded:
|
|
12
|
-
*
|
|
13
|
-
* process key derived key association
|
|
14
|
-
* ─────────────────────────────────────────────────────────
|
|
15
|
-
* subsystems-studio/host ─┐
|
|
16
|
-
* ├─────► container:studio-host
|
|
17
|
-
* principal-studio/host ─┘ (state: accepted)
|
|
18
|
-
*
|
|
19
|
-
* The association is the join point. It names one C4 element and lists the
|
|
20
|
-
* derived keys that belong to it, which is how dedup works without inventing
|
|
21
|
-
* anything: several keys claimed by one id collapse into one box.
|
|
22
|
-
*
|
|
23
|
-
* ## Why this is not in the subsystem document
|
|
24
|
-
*
|
|
25
|
-
* A `C4Association` is a claim about *architecture*, not about *code*. Putting
|
|
26
|
-
* it on the document would mean every re-audit had to re-derive and re-defend
|
|
27
|
-
* it, and it would make the shared standard carry an opinion the models have
|
|
28
|
-
* no evidence for. Keeping it separate means the portable document stays
|
|
29
|
-
* source-anchored and this layer stays revisable on its own.
|
|
30
|
-
*
|
|
31
|
-
* ## What an agent may and may not do
|
|
32
|
-
*
|
|
33
|
-
* An agent may *propose* associations — `state: 'proposed'` — including merges,
|
|
34
|
-
* including retyping a store as a data store, including writing `technology`
|
|
35
|
-
* and `description` from the source it read. It may not set `state:
|
|
36
|
-
* 'accepted'`. That field is the human's, and it is the only field that
|
|
37
|
-
* changes what is drawn: a proposed association shows as a proposal, an
|
|
38
|
-
* accepted one is part of the architecture, and a rejected one is not drawn.
|
|
39
|
-
*/
|
|
40
|
-
|
|
41
|
-
import type { C4Association, C4ElementType, C4Model } from './toC4';
|
|
42
|
-
|
|
43
|
-
/** The sidecar: a set of associations for one repo (or one composed graph). */
|
|
44
|
-
export interface C4AssociationSet {
|
|
45
|
-
/** Repo key this set describes, e.g. `pkg:github/owner/name`. */
|
|
46
|
-
repoKey: string;
|
|
47
|
-
/** ISO timestamp of the last change. */
|
|
48
|
-
updatedAt?: string;
|
|
49
|
-
associations: C4Association[];
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** Why an agent proposes what it proposes — the reviewable part. */
|
|
53
|
-
export interface C4AssociationReason {
|
|
54
|
-
/** Stable id of the derived key or association this concerns. */
|
|
55
|
-
target: string;
|
|
56
|
-
kind: C4ConcernKind;
|
|
57
|
-
message: string;
|
|
58
|
-
/** Aliases of the components that triggered the concern. */
|
|
59
|
-
members?: string[];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* The questions worth a human's attention, derived mechanically.
|
|
64
|
-
*
|
|
65
|
-
* These are the checks from the C4 notation rules that the document cannot
|
|
66
|
-
* answer on its own. An agent turns each into a proposal; a person answers.
|
|
67
|
-
*/
|
|
68
|
-
export type C4ConcernKind =
|
|
69
|
-
/** two or more process keys may be the same deployable unit */
|
|
70
|
-
| 'merge_candidate'
|
|
71
|
-
/** exactly one member — a lone function is not a deployable unit */
|
|
72
|
-
| 'singleton'
|
|
73
|
-
/** no member that executes at runtime — a library is not a container */
|
|
74
|
-
| 'library_shaped'
|
|
75
|
-
/** C4 requires technology on every container */
|
|
76
|
-
| 'missing_technology'
|
|
77
|
-
/** members claim more than one repo — a container cannot straddle systems */
|
|
78
|
-
| 'spans_repos'
|
|
79
|
-
/** a store whose storage kind was never decided */
|
|
80
|
-
| 'store_unclassified'
|
|
81
|
-
/** external with no resolvable identity */
|
|
82
|
-
| 'external_unidentified'
|
|
83
|
-
/** a runtime component with no process at all */
|
|
84
|
-
| 'unassigned';
|
|
85
|
-
|
|
86
|
-
/** Construct that executes at runtime, i.e. can be a deployable unit's code. */
|
|
87
|
-
const RUNTIME_CONSTRUCTS = new Set(['function', 'class', 'custom_entity']);
|
|
88
|
-
|
|
89
|
-
/** The fields `deriveConcerns` reads off a component. Structural on purpose:
|
|
90
|
-
* it accepts a real `SubsystemComponent` as well as a hand-built literal.
|
|
91
|
-
* `declaration` is the full construct-declaration union, so a component
|
|
92
|
-
* passes straight through without the caller reshaping it. */
|
|
93
|
-
export interface C4MemberView {
|
|
94
|
-
alias: string;
|
|
95
|
-
construct: string;
|
|
96
|
-
purl: string;
|
|
97
|
-
process?: string;
|
|
98
|
-
framework?: string;
|
|
99
|
-
declaration?: unknown;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** A store's declared storage kind, or undefined when it never declared one. */
|
|
103
|
-
function storageOf(m: C4MemberView): string | undefined {
|
|
104
|
-
const d = m.declaration as { kind?: string; storage?: string } | undefined;
|
|
105
|
-
return d?.kind === 'store' ? d.storage : undefined;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Derive the questions worth asking about one container key.
|
|
110
|
-
*
|
|
111
|
-
* Pure over the rollup, so it runs identically against a single model, the 40
|
|
112
|
-
* composed models, or a fixture.
|
|
113
|
-
*/
|
|
114
|
-
export function deriveConcerns(
|
|
115
|
-
key: string,
|
|
116
|
-
members: readonly C4MemberView[],
|
|
117
|
-
tech: { frameworks: readonly string[] } = { frameworks: [] },
|
|
118
|
-
): C4AssociationReason[] {
|
|
119
|
-
const out: C4AssociationReason[] = [];
|
|
120
|
-
const aliases = members.map((m) => m.alias);
|
|
121
|
-
const runtime = members.filter((m) => RUNTIME_CONSTRUCTS.has(m.construct));
|
|
122
|
-
|
|
123
|
-
if (members.length === 1) {
|
|
124
|
-
out.push({
|
|
125
|
-
target: key,
|
|
126
|
-
kind: 'singleton',
|
|
127
|
-
message: `process "${key}" has exactly one member (${aliases[0]}) — a lone function is not a deployable unit`,
|
|
128
|
-
members: aliases,
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// No member executes at runtime ⇒ this cannot be a deployable unit,
|
|
133
|
-
// regardless of how many members it has. A lone interface and a package of
|
|
134
|
-
// interfaces fail the same way.
|
|
135
|
-
if (members.length > 0 && runtime.length === 0) {
|
|
136
|
-
out.push({
|
|
137
|
-
target: key,
|
|
138
|
-
kind: 'library_shaped',
|
|
139
|
-
message: `process "${key}" has ${members.length} member(s) but none execute at runtime (${[...new Set(members.map((m) => m.construct))].join(', ')}) — this reads as a library, not a container`,
|
|
140
|
-
members: aliases,
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (runtime.length > 0 && tech.frameworks.length === 0) {
|
|
145
|
-
out.push({
|
|
146
|
-
target: key,
|
|
147
|
-
kind: 'missing_technology',
|
|
148
|
-
message: `container "${key}" has no framework signal — C4 requires a technology on every container`,
|
|
149
|
-
members: aliases,
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const repos = new Set(members.map((m) => (m.purl ?? '').split('#')[0]).filter((r) => r && !r.startsWith('external:')));
|
|
154
|
-
if (repos.size > 1) {
|
|
155
|
-
out.push({
|
|
156
|
-
target: key,
|
|
157
|
-
kind: 'spans_repos',
|
|
158
|
-
message: `process "${key}" spans ${repos.size} repo keys (${[...repos].join(', ')}) — a C4 container cannot straddle a system boundary`,
|
|
159
|
-
members: aliases,
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const stores = members.filter((m) => m.construct === 'store');
|
|
164
|
-
const unclassified = stores.filter((m) => !storageOf(m));
|
|
165
|
-
if (unclassified.length > 0) {
|
|
166
|
-
out.push({
|
|
167
|
-
target: key,
|
|
168
|
-
kind: 'store_unclassified',
|
|
169
|
-
message: `${unclassified.length} store(s) in "${key}" declare no storage kind (${unclassified.map((m) => m.alias).join(', ')}) — decide whether each is a C4 data store or an internal detail`,
|
|
170
|
-
members: unclassified.map((m) => m.alias),
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
return out;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Suggest merges between container keys that share a trailing role.
|
|
179
|
-
*
|
|
180
|
-
* `subsystems-studio/host` and `principal-studio/host` are the same role under
|
|
181
|
-
* two app names. This is a *suggestion*, never a merge — the whole question is
|
|
182
|
-
* whether they are one process or two, and only the user knows.
|
|
183
|
-
*/
|
|
184
|
-
export function suggestMerges(
|
|
185
|
-
rollup: ReadonlyArray<{ key: string; memberCount: number }>,
|
|
186
|
-
): Array<{ role: string; keys: string[]; reason: string }> {
|
|
187
|
-
const byRole = new Map<string, string[]>();
|
|
188
|
-
for (const { key } of rollup) {
|
|
189
|
-
const slash = key.lastIndexOf('/');
|
|
190
|
-
if (slash <= 0) continue; // a bare key has no app/role shape
|
|
191
|
-
const role = key.slice(slash + 1);
|
|
192
|
-
const list = byRole.get(role) ?? [];
|
|
193
|
-
list.push(key);
|
|
194
|
-
byRole.set(role, list);
|
|
195
|
-
}
|
|
196
|
-
const out: Array<{ role: string; keys: string[]; reason: string }> = [];
|
|
197
|
-
for (const [role, keys] of [...byRole].sort((a, b) => a[0].localeCompare(b[0]))) {
|
|
198
|
-
if (keys.length < 2) continue;
|
|
199
|
-
out.push({
|
|
200
|
-
role,
|
|
201
|
-
keys: [...keys].sort(),
|
|
202
|
-
reason: `${keys.length} process keys end in "/${role}" (${keys.sort().join(', ')}) — one deployable unit under two names, or two units?`,
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
return out;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Compose a suggested association set from a derived rollup.
|
|
210
|
-
*
|
|
211
|
-
* This is the agent's starting point: every derived key becomes one proposed
|
|
212
|
-
* association, shaped by the mechanical evidence. Nothing here is accepted —
|
|
213
|
-
* `buildAssociations` is a convenience for authoring, not an oracle.
|
|
214
|
-
*/
|
|
215
|
-
export function buildAssociations(
|
|
216
|
-
options: {
|
|
217
|
-
repoKey: string;
|
|
218
|
-
rollup: ReadonlyArray<{
|
|
219
|
-
key: string;
|
|
220
|
-
members: readonly C4MemberView[];
|
|
221
|
-
frameworks: readonly string[];
|
|
222
|
-
}>;
|
|
223
|
-
/** Merges a person has already decided on: groups of keys → one id. */
|
|
224
|
-
merges?: ReadonlyArray<{ id: string; keys: string[] }>;
|
|
225
|
-
author?: string;
|
|
226
|
-
},
|
|
227
|
-
): C4AssociationSet {
|
|
228
|
-
const merged = new Map<string, string>();
|
|
229
|
-
for (const m of options.merges ?? []) {
|
|
230
|
-
for (const k of m.keys) merged.set(k, m.id);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const byId = new Map<string, C4Association>();
|
|
234
|
-
for (const entry of options.rollup) {
|
|
235
|
-
const assocId = merged.get(entry.key);
|
|
236
|
-
const id = assocId ?? `container:${entry.key}`;
|
|
237
|
-
const concerns = deriveConcerns(entry.key, entry.members, { frameworks: entry.frameworks });
|
|
238
|
-
const hasStore = entry.members.some((m) => m.construct === 'store');
|
|
239
|
-
const tech = entry.frameworks.length > 0 ? entry.frameworks.join(' + ') : undefined;
|
|
240
|
-
|
|
241
|
-
const existing = byId.get(id);
|
|
242
|
-
if (existing) {
|
|
243
|
-
// Folded into an existing merge group — keep one element, add the key.
|
|
244
|
-
if (!existing.sourceKeys.includes(entry.key)) existing.sourceKeys.push(entry.key);
|
|
245
|
-
continue;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
byId.set(id, {
|
|
249
|
-
id,
|
|
250
|
-
level: 'container',
|
|
251
|
-
sourceKeys: [entry.key],
|
|
252
|
-
label: entry.key,
|
|
253
|
-
type: inferType(entry.members, concerns),
|
|
254
|
-
technology: tech,
|
|
255
|
-
state: 'proposed',
|
|
256
|
-
rationale: concerns.map((c) => c.message).join(' ') || undefined,
|
|
257
|
-
author: options.author,
|
|
258
|
-
});
|
|
259
|
-
void hasStore;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return {
|
|
263
|
-
repoKey: options.repoKey,
|
|
264
|
-
associations: [...byId.values()].sort((a, b) => a.id.localeCompare(b.id)),
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* How much evidence backs one association.
|
|
270
|
-
*
|
|
271
|
-
* Computed from the rollup rather than stored, so it never goes stale as
|
|
272
|
-
* models change. Used to pick the survivor of a merge.
|
|
273
|
-
*/
|
|
274
|
-
export function associationWeight(
|
|
275
|
-
a: C4Association,
|
|
276
|
-
memberCount: ReadonlyMap<string, number>,
|
|
277
|
-
): number {
|
|
278
|
-
let n = 0;
|
|
279
|
-
for (const k of a.sourceKeys) n += memberCount.get(k) ?? 0;
|
|
280
|
-
return n;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/** Best mechanical guess at a C4 element type, from the evidence available. */
|
|
284
|
-
function inferType(
|
|
285
|
-
members: readonly C4MemberView[],
|
|
286
|
-
concerns: readonly C4AssociationReason[],
|
|
287
|
-
): C4ElementType {
|
|
288
|
-
if (concerns.some((c) => c.kind === 'library_shaped')) return 'library';
|
|
289
|
-
const stores = members.filter((m) => m.construct === 'store');
|
|
290
|
-
if (stores.length > 0) {
|
|
291
|
-
// Only call it a data store when the storage kind says so; otherwise the
|
|
292
|
-
// store may just be an in-memory detail of the application.
|
|
293
|
-
const persists = stores.some((m) => storageOf(m) === 'external' || storageOf(m) === 'disk');
|
|
294
|
-
return persists ? 'data-store' : 'application';
|
|
295
|
-
}
|
|
296
|
-
return 'application';
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Fold a decided merge into an association set.
|
|
301
|
-
*
|
|
302
|
-
* Two derived keys that are really one container have to become ONE
|
|
303
|
-
* association claiming BOTH keys — not two associations, and not one
|
|
304
|
-
* association whose key was rewritten to the other's id. `toC4` collapses a
|
|
305
|
-
* box per association id, so a key must stay a key all the way through or the
|
|
306
|
-
* merge silently does nothing.
|
|
307
|
-
*
|
|
308
|
-
* ## Which side survives
|
|
309
|
-
*
|
|
310
|
-
* The **largest** side wins, not the caller's choice. `subsystems-studio/host`
|
|
311
|
-
* holds 113 components and `principal-studio/host` holds 1; folding the big
|
|
312
|
-
* one into the small one would leave the box labelled after an almost-empty
|
|
313
|
-
* process and lose its confirmed technology. Merging is a dedup, so the
|
|
314
|
-
* survivor should be the one carrying the evidence.
|
|
315
|
-
*
|
|
316
|
-
* Returns a new set; the input is untouched. Idempotent: merging an already
|
|
317
|
-
* merged set changes nothing.
|
|
318
|
-
*/
|
|
319
|
-
export function mergeAssociations(
|
|
320
|
-
associations: readonly C4Association[],
|
|
321
|
-
keys: readonly string[],
|
|
322
|
-
/**
|
|
323
|
-
* Optional caller override — skip it to get the evidence-driven default.
|
|
324
|
-
*/
|
|
325
|
-
preferKey?: string,
|
|
326
|
-
/**
|
|
327
|
-
* Components per derived key, from the rollup. This is what decides the
|
|
328
|
-
* survivor when every side claims a single key, which is the common case:
|
|
329
|
-
* `subsystems-studio/host` and `principal-studio/host` each hold exactly one
|
|
330
|
-
* derived key, so key count cannot break the tie.
|
|
331
|
-
*/
|
|
332
|
-
memberCount?: ReadonlyMap<string, number>,
|
|
333
|
-
): C4Association[] {
|
|
334
|
-
const wanted = [...new Set(keys.map((k) => k.trim()).filter(Boolean))];
|
|
335
|
-
if (wanted.length < 2) return [...associations];
|
|
336
|
-
|
|
337
|
-
const involved = associations.filter((a) => a.sourceKeys.some((k) => wanted.includes(k)));
|
|
338
|
-
if (involved.length < 2) return [...associations];
|
|
339
|
-
|
|
340
|
-
const survivor =
|
|
341
|
-
(preferKey ? involved.find((a) => a.sourceKeys.includes(preferKey.trim())) : undefined) ??
|
|
342
|
-
[...involved].sort((a, b) => {
|
|
343
|
-
const wa = associationWeight(a, memberCount ?? new Map());
|
|
344
|
-
const wb = associationWeight(b, memberCount ?? new Map());
|
|
345
|
-
if (wa !== wb) return wb - wa;
|
|
346
|
-
// No evidence either way: stable, and least surprising.
|
|
347
|
-
return a.id.localeCompare(b.id);
|
|
348
|
-
})[0]!;
|
|
349
|
-
|
|
350
|
-
const absorbed = involved.filter((a) => a.id !== survivor.id);
|
|
351
|
-
const absorbedIds = new Set(absorbed.map((a) => a.id));
|
|
352
|
-
|
|
353
|
-
const merged: C4Association = {
|
|
354
|
-
...survivor,
|
|
355
|
-
sourceKeys: [...new Set([...survivor.sourceKeys, ...absorbed.flatMap((a) => a.sourceKeys)])].sort(),
|
|
356
|
-
rationale:
|
|
357
|
-
[survivor.rationale, ...absorbed.map((a) => a.rationale)].filter(Boolean).join(' | ') || undefined,
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
return [...associations.filter((a) => !absorbedIds.has(a.id) && a.id !== survivor.id), merged].sort(
|
|
361
|
-
(a, b) => a.id.localeCompare(b.id),
|
|
362
|
-
);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
/** Summary counts for the confirm UI: how much is proposed vs accepted. */
|
|
366
|
-
export function summarizeAssociations(
|
|
367
|
-
model: C4Model,
|
|
368
|
-
associations: readonly C4Association[],
|
|
369
|
-
): {
|
|
370
|
-
confirmed: number;
|
|
371
|
-
proposed: number;
|
|
372
|
-
rejected: number;
|
|
373
|
-
mergedAway: number;
|
|
374
|
-
missingTechnology: number;
|
|
375
|
-
needsDecision: number;
|
|
376
|
-
} {
|
|
377
|
-
const accepted = associations.filter((a) => a.state === 'accepted');
|
|
378
|
-
const proposed = associations.filter((a) => a.state === 'proposed');
|
|
379
|
-
const rejected = associations.filter((a) => a.state === 'rejected');
|
|
380
|
-
const confirmedIds = new Set(accepted.map((a) => a.id));
|
|
381
|
-
const mergedAway = accepted.reduce((n, a) => n + (a.sourceKeys.length - 1), 0);
|
|
382
|
-
|
|
383
|
-
// A raw derived key with no association is unconfirmed, not wrong.
|
|
384
|
-
const derivedKeys = new Set<string>();
|
|
385
|
-
for (const n of model.nodes) if (n.kind === 'container') derivedKeys.add(n.key ?? n.id);
|
|
386
|
-
const unconfirmed = [...derivedKeys].filter((k) => ![...confirmedIds].some((id) => associations.find((a) => a.id === id)?.sourceKeys.includes(k))).length;
|
|
387
|
-
|
|
388
|
-
const missingTechnology = model.nodes.filter((n) => n.kind === 'container' && !n.decoration?.technology).length;
|
|
389
|
-
|
|
390
|
-
return {
|
|
391
|
-
confirmed: accepted.length,
|
|
392
|
-
proposed: proposed.length,
|
|
393
|
-
rejected: rejected.length,
|
|
394
|
-
mergedAway,
|
|
395
|
-
missingTechnology,
|
|
396
|
-
needsDecision: proposed.length + unconfirmed,
|
|
397
|
-
};
|
|
398
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'bun:test';
|
|
2
|
-
import {
|
|
3
|
-
toC4,
|
|
4
|
-
deriveRepoKey,
|
|
5
|
-
labelFromPurl,
|
|
6
|
-
isGroundedComponent,
|
|
7
|
-
} from './toC4';
|
|
8
|
-
import type { SubsystemModelDocument } from './model';
|
|
9
|
-
|
|
10
|
-
const REPO = 'pkg:github/acme/repo';
|
|
11
|
-
|
|
12
|
-
const doc: SubsystemModelDocument = {
|
|
13
|
-
components: [
|
|
14
|
-
{ alias: 'a', name: 'Alpha', construct: 'function', file: 'src/a.ts', purl: `${REPO}#src/a.ts`, process: 'p1' },
|
|
15
|
-
{ alias: 'b', name: 'Beta', construct: 'function', file: 'src/b.ts', purl: `${REPO}#src/b.ts`, process: 'p1' },
|
|
16
|
-
{ alias: 's', name: 'Cache', construct: 'store', file: 'src/s.ts', purl: `${REPO}#src/s.ts`, process: 'p1' },
|
|
17
|
-
{ alias: 'c', name: 'Gamma', construct: 'function', file: 'src/c.ts', purl: `${REPO}#src/c.ts`, process: 'p2' },
|
|
18
|
-
{ alias: 'x', name: 'Gist API', construct: 'external', file: '', purl: 'external:api.github.com/gists' },
|
|
19
|
-
{ alias: 'agent', name: 'Maintenance agent', construct: 'custom_entity', file: '', purl: 'external' },
|
|
20
|
-
],
|
|
21
|
-
trails: [
|
|
22
|
-
{
|
|
23
|
-
id: 'w1',
|
|
24
|
-
title: 'flow',
|
|
25
|
-
steps: [
|
|
26
|
-
{ from: 'a', to: 'b', mechanism: 'calls', file: 'src/a.ts', line: 1, purl: `${REPO}#src/a.ts`, symbol: 'Alpha' },
|
|
27
|
-
{ from: 'b', to: 'c', mechanism: 'calls', file: 'src/b.ts', line: 1, purl: `${REPO}#src/b.ts`, symbol: 'Beta' },
|
|
28
|
-
{ from: 'a', to: 'c', mechanism: 'calls', file: 'src/a.ts', line: 2, purl: `${REPO}#src/a.ts`, symbol: 'Alpha' },
|
|
29
|
-
{ from: 'a', to: 'agent', mechanism: 'calls', file: 'src/a.ts', line: 3, purl: `${REPO}#src/a.ts`, symbol: 'Alpha' },
|
|
30
|
-
],
|
|
31
|
-
},
|
|
32
|
-
],
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
describe('helpers', () => {
|
|
36
|
-
test('deriveRepoKey ignores external purls', () => {
|
|
37
|
-
expect(deriveRepoKey(doc)).toBe(REPO);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test('labelFromPurl trims scheme and keeps owner/name', () => {
|
|
41
|
-
expect(labelFromPurl('pkg:github/acme/repo#src/a.ts')).toBe('acme/repo');
|
|
42
|
-
expect(labelFromPurl('external:api.github.com/gists')).toBe('api.github.com/gists');
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test('isGroundedComponent excludes externals and entities', () => {
|
|
46
|
-
expect(isGroundedComponent(doc.components[0]!)).toBe(true);
|
|
47
|
-
expect(isGroundedComponent(doc.components[4]!)).toBe(false);
|
|
48
|
-
expect(isGroundedComponent(doc.components[5]!)).toBe(false);
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
describe('container view', () => {
|
|
53
|
-
const model = toC4(doc, { view: 'container', systemLabel: 'Acme' });
|
|
54
|
-
|
|
55
|
-
test('system is the repo, containers are processes', () => {
|
|
56
|
-
expect(model.system.id).toBe(`system:${REPO}`);
|
|
57
|
-
expect(model.system.label).toBe('Acme');
|
|
58
|
-
expect(model.nodes.map((n) => n.id).sort()).toEqual(
|
|
59
|
-
[`container:p1`, `container:p2`, `external:external:api.github.com/gists`, `actor:agent`].sort(),
|
|
60
|
-
);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test('container members and store flag', () => {
|
|
64
|
-
const p1 = model.nodes.find((n) => n.id === 'container:p1')!;
|
|
65
|
-
expect(p1.members.sort()).toEqual(['a', 'b', 's']);
|
|
66
|
-
expect(p1.isStore).toBe(true);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test('one system group holds the container nodes', () => {
|
|
70
|
-
expect(model.groups).toHaveLength(1);
|
|
71
|
-
expect(model.groups[0]!.kind).toBe('system');
|
|
72
|
-
expect(model.groups[0]!.memberIds.sort()).toEqual(['container:p1', 'container:p2']);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('flow edges roll up to containers; only flow edges exist', () => {
|
|
76
|
-
const byId = Object.fromEntries(model.edges.map((e) => [e.id, e]));
|
|
77
|
-
// a->b is intra-p1 — dropped.
|
|
78
|
-
expect(model.edges.some((e) => e.source === e.target)).toBe(false);
|
|
79
|
-
const flow = byId['flow:container:p1\u0000container:p2']!;
|
|
80
|
-
expect(flow.kind).toBe('flow');
|
|
81
|
-
expect(flow.count).toBe(2); // a->c + b->c
|
|
82
|
-
expect(byId['flow:container:p1\u0000actor:agent']).toBeDefined();
|
|
83
|
-
expect(model.edges.every((e) => e.kind === 'flow')).toBe(true);
|
|
84
|
-
expect(model.edges).toHaveLength(2);
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
describe('component view', () => {
|
|
89
|
-
const model = toC4(doc, { view: 'component' });
|
|
90
|
-
|
|
91
|
-
test('components parent to their container; externals stay root', () => {
|
|
92
|
-
const a = model.nodes.find((n) => n.id === 'component:a')!;
|
|
93
|
-
expect(a.parentId).toBe('container:p1');
|
|
94
|
-
const c = model.nodes.find((n) => n.id === 'component:c')!;
|
|
95
|
-
expect(c.parentId).toBe('container:p2');
|
|
96
|
-
expect(model.nodes.map((n) => n.id).sort()).toEqual(
|
|
97
|
-
['component:a', 'component:b', 'component:s', 'component:c', 'external:external:api.github.com/gists', 'actor:agent'].sort(),
|
|
98
|
-
);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
test('nested groups: containers nest in the system', () => {
|
|
102
|
-
const p1 = model.groups.find((g) => g.id === 'container:p1')!;
|
|
103
|
-
expect(p1.parentId).toBe(`system:${REPO}`);
|
|
104
|
-
expect(p1.memberIds.sort()).toEqual(['component:a', 'component:b', 'component:s']);
|
|
105
|
-
const system = model.groups.find((g) => g.kind === 'system')!;
|
|
106
|
-
expect(system.memberIds.sort()).toEqual(['container:p1', 'container:p2']);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
test('intra-container but cross-component edges survive', () => {
|
|
110
|
-
// a->b (same container, different components) survives at component level.
|
|
111
|
-
expect(model.edges.some((e) => e.source === 'component:a' && e.target === 'component:b')).toBe(true);
|
|
112
|
-
expect(model.edges.some((e) => e.source === e.target)).toBe(false);
|
|
113
|
-
});
|
|
114
|
-
});
|