@atolis-hq/corum 0.1.43 → 0.1.45
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 +108 -7
- package/dist/src/graph/index.js +69 -37
- package/dist/src/graph/overlay.js +106 -36
- package/dist/src/import/runner.js +17 -3
- package/dist/src/linter/index.js +242 -106
- package/dist/src/loader/cluster-loader.js +36 -3
- package/dist/src/loader/edge-loader.js +8 -2
- package/dist/src/mcp/index.js +438 -12
- package/dist/src/mcp/prompts/usage-guide.js +61 -21
- package/dist/src/mutate/alias.js +68 -0
- package/dist/src/mutate/apply-cluster.js +255 -0
- package/dist/src/mutate/delete.js +47 -0
- package/dist/src/mutate/errors.js +14 -0
- package/dist/src/mutate/index.js +11 -0
- package/dist/src/mutate/rename.js +135 -0
- package/dist/src/mutate/session.js +513 -0
- package/dist/src/mutate/util.js +62 -0
- package/dist/src/reconcile/index.js +126 -3
- package/dist/src/source/file-source.js +64 -15
- package/dist/src/source/git-source.js +121 -31
- package/dist/src/writer/graph-writer.js +64 -10
- package/package.json +2 -1
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import { resolveAlias } from '../mutate/alias.js';
|
|
2
|
+
/**
|
|
3
|
+
* Properties preserved across `derivation: determined` re-imports. Holds both
|
|
4
|
+
* human-owned keys (state, stability, notes) and system-owned bookkeeping
|
|
5
|
+
* (`corum.identity.previousIds` rename trail, design §6a) — a re-import must never erase
|
|
6
|
+
* rename history.
|
|
7
|
+
*/
|
|
8
|
+
const PRESERVED_PROPERTIES = new Set(['state', 'stability', 'notes']);
|
|
2
9
|
export function diffNodes(incoming, existing, specPath) {
|
|
3
10
|
const toAdd = [];
|
|
4
11
|
const toUpdate = [];
|
|
@@ -12,6 +19,7 @@ export function diffNodes(incoming, existing, specPath) {
|
|
|
12
19
|
const merged = {
|
|
13
20
|
...current,
|
|
14
21
|
properties: mergeProperties(current.properties, node.properties, node.derivation),
|
|
22
|
+
corum: mergeCorum(current.corum, node.corum, node.derivation),
|
|
15
23
|
extractedFrom: node.extractedFrom,
|
|
16
24
|
derivation: node.derivation,
|
|
17
25
|
derivedBy: node.derivedBy,
|
|
@@ -33,11 +41,126 @@ export function diffNodes(incoming, existing, specPath) {
|
|
|
33
41
|
}
|
|
34
42
|
function mergeProperties(current, incoming, derivation) {
|
|
35
43
|
if (derivation === 'determined') {
|
|
36
|
-
const
|
|
37
|
-
return { ...incoming, ...
|
|
44
|
+
const preservedValues = Object.fromEntries(Object.entries(current).filter(([k]) => PRESERVED_PROPERTIES.has(k)));
|
|
45
|
+
return { ...incoming, ...preservedValues };
|
|
38
46
|
}
|
|
39
47
|
return { ...current, ...incoming };
|
|
40
48
|
}
|
|
41
49
|
function nodesEqual(a, b) {
|
|
42
50
|
return JSON.stringify(a) === JSON.stringify(b);
|
|
43
51
|
}
|
|
52
|
+
function mergeCorum(current, incoming, derivation) {
|
|
53
|
+
if (derivation === 'determined' && current?.identity?.previousIds !== undefined && incoming?.identity?.previousIds === undefined) {
|
|
54
|
+
return {
|
|
55
|
+
...(incoming ?? {}),
|
|
56
|
+
identity: {
|
|
57
|
+
...(incoming?.identity ?? {}),
|
|
58
|
+
previousIds: current.identity.previousIds,
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return incoming ?? current;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Design §6a / §14d: pass every incoming node ID through the working graph's
|
|
66
|
+
* alias map immediately before `diffNodes`, so a spec still using a retired
|
|
67
|
+
* name merges into the renamed node instead of appearing as add+remove.
|
|
68
|
+
*
|
|
69
|
+
* - Descendant IDs resolve through the alias map's longest-prefix rule; their
|
|
70
|
+
* `parentId`s are rewritten via the same mapping.
|
|
71
|
+
* - The name difference is reported as intentional in-flight drift — one
|
|
72
|
+
* warning per rewritten subtree root, not per descendant.
|
|
73
|
+
* - Ambiguity rule (§14d): when the batch contains both a literal ID X and
|
|
74
|
+
* another ID that resolves to X, the literal one stays authoritative — the
|
|
75
|
+
* resolution is skipped and a warning names both.
|
|
76
|
+
*/
|
|
77
|
+
export function resolveIncomingAliases(graph, aliasMap, incoming, incomingEdges, specPath) {
|
|
78
|
+
if (aliasMap.size === 0)
|
|
79
|
+
return { nodes: incoming, edges: incomingEdges, diagnostics: [] };
|
|
80
|
+
const diagnostics = [];
|
|
81
|
+
const literalIds = new Set(incoming.map(n => n.id));
|
|
82
|
+
const idMap = new Map();
|
|
83
|
+
for (const node of incoming) {
|
|
84
|
+
const resolved = resolveAlias(graph, aliasMap, node.id);
|
|
85
|
+
if (resolved === node.id)
|
|
86
|
+
continue;
|
|
87
|
+
if (literalIds.has(resolved)) {
|
|
88
|
+
diagnostics.push({
|
|
89
|
+
severity: 'warning',
|
|
90
|
+
file: specPath,
|
|
91
|
+
nodeId: node.id,
|
|
92
|
+
message: `ambiguous alias: incoming '${node.id}' resolves to '${resolved}', which is also present literally in this import — keeping the literal node authoritative and skipping the resolution`,
|
|
93
|
+
});
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
idMap.set(node.id, resolved);
|
|
97
|
+
}
|
|
98
|
+
if (idMap.size === 0)
|
|
99
|
+
return { nodes: incoming, edges: incomingEdges, diagnostics };
|
|
100
|
+
const nodes = incoming.map(node => {
|
|
101
|
+
const resolved = idMap.get(node.id);
|
|
102
|
+
const resolvedParent = node.parentId !== undefined ? idMap.get(node.parentId) : undefined;
|
|
103
|
+
if (resolved === undefined && resolvedParent === undefined)
|
|
104
|
+
return node;
|
|
105
|
+
return {
|
|
106
|
+
...node,
|
|
107
|
+
id: resolved ?? node.id,
|
|
108
|
+
...(resolvedParent !== undefined ? { parentId: resolvedParent } : {}),
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
// Rewrite edge endpoints that reference rewritten incoming nodes, and
|
|
112
|
+
// recompute edge IDs ({from}__{type}__{to}).
|
|
113
|
+
const edges = incomingEdges.map(edge => {
|
|
114
|
+
const from = idMap.get(edge.from);
|
|
115
|
+
const to = idMap.get(edge.to);
|
|
116
|
+
if (from === undefined && to === undefined)
|
|
117
|
+
return edge;
|
|
118
|
+
const newFrom = from ?? edge.from;
|
|
119
|
+
const newTo = to ?? edge.to;
|
|
120
|
+
return { ...edge, from: newFrom, to: newTo, id: `${newFrom}__${edge.type}__${newTo}` };
|
|
121
|
+
});
|
|
122
|
+
// In-flight drift: report per rewritten subtree root (a rewritten node whose
|
|
123
|
+
// parent was not itself rewritten), so a renamed schema yields one entry.
|
|
124
|
+
for (const node of incoming) {
|
|
125
|
+
const resolved = idMap.get(node.id);
|
|
126
|
+
if (resolved === undefined)
|
|
127
|
+
continue;
|
|
128
|
+
if (node.parentId !== undefined && idMap.has(node.parentId))
|
|
129
|
+
continue;
|
|
130
|
+
diagnostics.push({
|
|
131
|
+
severity: 'warning',
|
|
132
|
+
file: specPath,
|
|
133
|
+
nodeId: resolved,
|
|
134
|
+
message: `in-flight drift: spec still uses '${node.id}', which was renamed to '${resolved}' — merged into the renamed node (update the spec to match)`,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return { nodes, edges, diagnostics };
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Design §6a: rename suggestion heuristic (warning only). When a single
|
|
141
|
+
* re-import both removes a node and adds another under the same parent with
|
|
142
|
+
* the same template, suggest recording the rename explicitly. No automatic
|
|
143
|
+
* action — renames stay explicit via rename_node.
|
|
144
|
+
*/
|
|
145
|
+
export function detectPossibleRenames(diff, specPath) {
|
|
146
|
+
const diagnostics = [];
|
|
147
|
+
const addedByGroup = new Map();
|
|
148
|
+
const groupKey = (node) => `${node.template}|${node.parentId ?? node.component}`;
|
|
149
|
+
for (const added of diff.toAdd) {
|
|
150
|
+
const key = groupKey(added);
|
|
151
|
+
addedByGroup.set(key, [...(addedByGroup.get(key) ?? []), added]);
|
|
152
|
+
}
|
|
153
|
+
for (const removed of diff.toRemove) {
|
|
154
|
+
const candidates = addedByGroup.get(groupKey(removed));
|
|
155
|
+
if (!candidates?.length)
|
|
156
|
+
continue;
|
|
157
|
+
const names = candidates.map(n => `'${n.id}'`).join(', ');
|
|
158
|
+
diagnostics.push({
|
|
159
|
+
severity: 'warning',
|
|
160
|
+
file: specPath,
|
|
161
|
+
nodeId: removed.id,
|
|
162
|
+
message: `possible rename: '${removed.id}' was removed and ${names} added under the same parent with the same template — if this is a rename, record it with rename_node`,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return diagnostics;
|
|
166
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
2
3
|
import fs from 'node:fs';
|
|
3
4
|
import path from 'node:path';
|
|
4
5
|
import * as git from 'isomorphic-git';
|
|
@@ -57,7 +58,7 @@ export class FileGraphSource {
|
|
|
57
58
|
if (!existsSync(this.graphDir))
|
|
58
59
|
return map;
|
|
59
60
|
const excludeDirs = new Set(this.resolvePackDirs().map(d => path.resolve(d)));
|
|
60
|
-
|
|
61
|
+
walkManagedGraphYamlIntoMap(this.graphDir, this.graphDir, map, excludeDirs);
|
|
61
62
|
return map;
|
|
62
63
|
}
|
|
63
64
|
resolvePackDirs() {
|
|
@@ -74,18 +75,40 @@ export class FileGraphSource {
|
|
|
74
75
|
return [];
|
|
75
76
|
}
|
|
76
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Content hash of the graph directory's YAML files — a head marker for the
|
|
80
|
+
* moved-head check (design §10/§14e). Not a commit SHA: file sources keep
|
|
81
|
+
* no history.
|
|
82
|
+
*/
|
|
83
|
+
async head(branch) {
|
|
84
|
+
const content = await this.loadGraphContent(branch);
|
|
85
|
+
const hash = createHash('sha256');
|
|
86
|
+
for (const key of [...content.keys()].sort((a, b) => a.localeCompare(b))) {
|
|
87
|
+
hash.update(key);
|
|
88
|
+
hash.update('\0');
|
|
89
|
+
hash.update(content.get(key));
|
|
90
|
+
hash.update('\0');
|
|
91
|
+
}
|
|
92
|
+
return hash.digest('hex');
|
|
93
|
+
}
|
|
94
|
+
/** File sources keep no history: `[head]` when the content moved since `sinceSha`, else `[]`. */
|
|
95
|
+
async log(branch, sinceSha) {
|
|
96
|
+
const current = await this.head(branch);
|
|
97
|
+
return current === sinceSha ? [] : [current];
|
|
98
|
+
}
|
|
77
99
|
async commit(branch, changes, message, options = {}) {
|
|
78
100
|
const defaultBranch = await this.defaultBranch();
|
|
79
101
|
if (branch !== defaultBranch) {
|
|
80
102
|
throw new SourceError(`FileGraphSource only supports its local branch '${defaultBranch}', got '${branch}'`);
|
|
81
103
|
}
|
|
104
|
+
let touchedKeys;
|
|
82
105
|
if (options.replaceGraphContent) {
|
|
83
|
-
this.replaceGraphContent(changes);
|
|
106
|
+
touchedKeys = this.replaceGraphContent(changes);
|
|
84
107
|
}
|
|
85
108
|
else {
|
|
86
|
-
writeContentMap(this.graphDir, changes);
|
|
109
|
+
touchedKeys = writeContentMap(this.graphDir, changes);
|
|
87
110
|
}
|
|
88
|
-
await commitToGitIfRepo(this.graphDir, message);
|
|
111
|
+
await commitToGitIfRepo(this.graphDir, message, touchedKeys);
|
|
89
112
|
}
|
|
90
113
|
/**
|
|
91
114
|
* Replace the full graph directory contents with `changes`, closing the
|
|
@@ -106,13 +129,12 @@ export class FileGraphSource {
|
|
|
106
129
|
*/
|
|
107
130
|
replaceGraphContent(changes) {
|
|
108
131
|
if (!existsSync(this.graphDir)) {
|
|
109
|
-
writeContentMap(this.graphDir, changes);
|
|
110
|
-
return;
|
|
132
|
+
return writeContentMap(this.graphDir, changes);
|
|
111
133
|
}
|
|
112
134
|
if (existsSync(path.join(this.graphDir, '.git'))) {
|
|
113
|
-
writeContentMap(this.graphDir, changes);
|
|
114
|
-
|
|
115
|
-
return;
|
|
135
|
+
const written = writeContentMap(this.graphDir, changes);
|
|
136
|
+
const deleted = deleteStaleGraphYamlFiles(this.graphDir, changes, this.resolvePackDirs());
|
|
137
|
+
return new Set([...written, ...deleted]);
|
|
116
138
|
}
|
|
117
139
|
const parent = path.dirname(this.graphDir);
|
|
118
140
|
const base = path.basename(this.graphDir);
|
|
@@ -121,8 +143,8 @@ export class FileGraphSource {
|
|
|
121
143
|
const oldDir = path.join(parent, `${base}.corum-old-${suffix}`);
|
|
122
144
|
rmSync(tmpDir, { recursive: true, force: true });
|
|
123
145
|
cpSync(this.graphDir, tmpDir, { recursive: true });
|
|
124
|
-
writeContentMap(tmpDir, changes);
|
|
125
|
-
|
|
146
|
+
const written = writeContentMap(tmpDir, changes);
|
|
147
|
+
const deleted = deleteStaleGraphYamlFiles(tmpDir, changes, this.resolvePackDirs());
|
|
126
148
|
renameSync(this.graphDir, oldDir);
|
|
127
149
|
try {
|
|
128
150
|
renameSync(tmpDir, this.graphDir);
|
|
@@ -132,25 +154,33 @@ export class FileGraphSource {
|
|
|
132
154
|
throw err;
|
|
133
155
|
}
|
|
134
156
|
rmSync(oldDir, { recursive: true, force: true });
|
|
157
|
+
return new Set([...written, ...deleted]);
|
|
135
158
|
}
|
|
136
159
|
}
|
|
137
160
|
function writeContentMap(baseDir, changes) {
|
|
161
|
+
const touched = new Set();
|
|
138
162
|
for (const [key, content] of changes) {
|
|
139
163
|
const filePath = resolveContentPath(baseDir, key);
|
|
140
164
|
mkdirSync(path.dirname(filePath), { recursive: true });
|
|
141
165
|
writeFileSync(filePath, content);
|
|
166
|
+
touched.add(normalizeContentKey(key));
|
|
142
167
|
}
|
|
168
|
+
return touched;
|
|
143
169
|
}
|
|
144
|
-
function
|
|
170
|
+
function deleteStaleGraphYamlFiles(baseDir, changes, packDirs = []) {
|
|
145
171
|
const existingYaml = new Map();
|
|
146
|
-
|
|
172
|
+
const excludeDirs = new Set(packDirs.map(d => path.resolve(d)));
|
|
173
|
+
walkManagedGraphYamlIntoMap(baseDir, baseDir, existingYaml, excludeDirs);
|
|
174
|
+
const deleted = new Set();
|
|
147
175
|
for (const key of existingYaml.keys()) {
|
|
148
176
|
if (!changes.has(key)) {
|
|
149
177
|
rmSync(resolveContentPath(baseDir, key), { force: true });
|
|
178
|
+
deleted.add(normalizeContentKey(key));
|
|
150
179
|
}
|
|
151
180
|
}
|
|
181
|
+
return deleted;
|
|
152
182
|
}
|
|
153
|
-
async function commitToGitIfRepo(graphDir, message) {
|
|
183
|
+
async function commitToGitIfRepo(graphDir, message, touchedKeys) {
|
|
154
184
|
let repoRoot;
|
|
155
185
|
try {
|
|
156
186
|
repoRoot = await git.findRoot({ fs, filepath: path.resolve(graphDir) });
|
|
@@ -159,10 +189,15 @@ async function commitToGitIfRepo(graphDir, message) {
|
|
|
159
189
|
return; // Not inside a git repo — plain directory writes only.
|
|
160
190
|
}
|
|
161
191
|
const rel = path.relative(repoRoot, path.resolve(graphDir)).split(path.sep).join('/');
|
|
192
|
+
const filepaths = [...touchedKeys]
|
|
193
|
+
.map(key => rel === '' ? key : `${rel}/${key}`)
|
|
194
|
+
.sort((a, b) => a.localeCompare(b));
|
|
195
|
+
if (filepaths.length === 0)
|
|
196
|
+
return;
|
|
162
197
|
const matrix = await git.statusMatrix({
|
|
163
198
|
fs,
|
|
164
199
|
dir: repoRoot,
|
|
165
|
-
filepaths
|
|
200
|
+
filepaths,
|
|
166
201
|
});
|
|
167
202
|
let staged = false;
|
|
168
203
|
for (const [filepath, head, workdir] of matrix) {
|
|
@@ -208,3 +243,17 @@ function walkYamlFilesIntoMap(baseDir, currentDir, map, excludeDirs = new Set())
|
|
|
208
243
|
}
|
|
209
244
|
}
|
|
210
245
|
}
|
|
246
|
+
function walkManagedGraphYamlIntoMap(baseDir, currentDir, map, excludeDirs = new Set()) {
|
|
247
|
+
const allYaml = new Map();
|
|
248
|
+
walkYamlFilesIntoMap(baseDir, currentDir, allYaml, excludeDirs);
|
|
249
|
+
for (const [key, content] of allYaml) {
|
|
250
|
+
if (isManagedGraphYamlKey(key))
|
|
251
|
+
map.set(key, content);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
function isManagedGraphYamlKey(key) {
|
|
255
|
+
return key === 'graph.yaml' || key.startsWith('components/') || key.startsWith('edges/');
|
|
256
|
+
}
|
|
257
|
+
function normalizeContentKey(key) {
|
|
258
|
+
return key.split(path.sep).join('/');
|
|
259
|
+
}
|
|
@@ -172,6 +172,39 @@ export class GitGraphSource {
|
|
|
172
172
|
}
|
|
173
173
|
return map;
|
|
174
174
|
}
|
|
175
|
+
/** Current commit SHA of the branch head (design §14e). */
|
|
176
|
+
async head(branch) {
|
|
177
|
+
return this.resolveBranchOid(branch);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Commit SHAs from the branch head back to (and excluding) `sinceSha`,
|
|
181
|
+
* newest first, following first parents. If `sinceSha` is not an ancestor
|
|
182
|
+
* the walk stops at the root commit and returns everything seen — the
|
|
183
|
+
* squash guard treats that as "head moved externally".
|
|
184
|
+
*/
|
|
185
|
+
async log(branch, sinceSha) {
|
|
186
|
+
const dir = await this.dir();
|
|
187
|
+
const shas = [];
|
|
188
|
+
let oid = await this.resolveBranchOid(branch);
|
|
189
|
+
const MAX_WALK = 10_000;
|
|
190
|
+
while (oid !== sinceSha) {
|
|
191
|
+
shas.push(oid);
|
|
192
|
+
if (shas.length > MAX_WALK) {
|
|
193
|
+
throw new SourceError(`log walk on branch '${branch}' exceeded ${MAX_WALK} commits without reaching ${sinceSha}`);
|
|
194
|
+
}
|
|
195
|
+
let parents;
|
|
196
|
+
try {
|
|
197
|
+
parents = (await git.readCommit({ fs, dir, oid })).commit.parent;
|
|
198
|
+
}
|
|
199
|
+
catch (err) {
|
|
200
|
+
throw new SourceError(`cannot read commit ${oid} on branch '${branch}'`, err);
|
|
201
|
+
}
|
|
202
|
+
if (parents.length === 0)
|
|
203
|
+
break;
|
|
204
|
+
oid = parents[0];
|
|
205
|
+
}
|
|
206
|
+
return shas;
|
|
207
|
+
}
|
|
175
208
|
async commit(branch, changes, message, options = {}) {
|
|
176
209
|
const dir = await this.dir();
|
|
177
210
|
if (this.localPath) {
|
|
@@ -202,7 +235,9 @@ export class GitGraphSource {
|
|
|
202
235
|
return run;
|
|
203
236
|
}
|
|
204
237
|
async commitLocked(dir, branch, defaultBranch, changes, message, options) {
|
|
205
|
-
|
|
238
|
+
if (options.parentSha !== undefined) {
|
|
239
|
+
return this.commitWithExplicitParent(dir, branch, changes, message, options);
|
|
240
|
+
}
|
|
206
241
|
const MAX_ATTEMPTS = 3;
|
|
207
242
|
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
|
|
208
243
|
let parentSha;
|
|
@@ -222,28 +257,7 @@ export class GitGraphSource {
|
|
|
222
257
|
throw new SourceError(`cannot resolve default branch '${defaultBranch}' to create '${branch}'`, err2);
|
|
223
258
|
}
|
|
224
259
|
}
|
|
225
|
-
const
|
|
226
|
-
const blobMap = new Map();
|
|
227
|
-
for (const [key, content] of changes) {
|
|
228
|
-
const repoPath = `${prefix}${normalizeContentKey(key)}`;
|
|
229
|
-
const oid = await git.writeBlob({ fs, dir, blob: Buffer.from(content, 'utf-8') });
|
|
230
|
-
blobMap.set(repoPath, oid);
|
|
231
|
-
}
|
|
232
|
-
const newTreeOid = options.replaceGraphContent
|
|
233
|
-
? await buildReplacedGraphTree(fs, dir, parentCommit.tree, prefix, blobMap)
|
|
234
|
-
: await buildUpdatedTree(fs, dir, parentCommit.tree, blobMap);
|
|
235
|
-
const now = Math.floor(Date.now() / 1000);
|
|
236
|
-
const newCommitOid = await git.writeCommit({
|
|
237
|
-
fs,
|
|
238
|
-
dir,
|
|
239
|
-
commit: {
|
|
240
|
-
tree: newTreeOid,
|
|
241
|
-
parent: [parentSha],
|
|
242
|
-
message,
|
|
243
|
-
author: { name: 'corum', email: 'corum@localhost', timestamp: now, timezoneOffset: 0 },
|
|
244
|
-
committer: { name: 'corum', email: 'corum@localhost', timestamp: now, timezoneOffset: 0 },
|
|
245
|
-
},
|
|
246
|
-
});
|
|
260
|
+
const newCommitOid = await this.writeCommitObject(dir, changes, parentSha, message, options);
|
|
247
261
|
// Compare-and-swap: if the ref moved while we built the commit, rebuild
|
|
248
262
|
// on the new parent instead of silently discarding the other write.
|
|
249
263
|
let currentSha;
|
|
@@ -258,14 +272,8 @@ export class GitGraphSource {
|
|
|
258
272
|
await git.writeRef({ fs, dir, ref: `refs/heads/${branch}`, value: newCommitOid, force: true });
|
|
259
273
|
if (this.remoteUrl) {
|
|
260
274
|
try {
|
|
261
|
-
await
|
|
262
|
-
|
|
263
|
-
http: (await import('isomorphic-git/http/node')).default,
|
|
264
|
-
dir,
|
|
265
|
-
remote: 'origin',
|
|
266
|
-
ref: branch,
|
|
267
|
-
onAuth: this.onAuth(),
|
|
268
|
-
});
|
|
275
|
+
await this.push(dir, branch, options.force === true);
|
|
276
|
+
await this.updateRemoteTrackingRef(dir, branch, newCommitOid);
|
|
269
277
|
}
|
|
270
278
|
catch (err) {
|
|
271
279
|
// Roll the local ref back so the cached clone does not diverge from
|
|
@@ -283,6 +291,82 @@ export class GitGraphSource {
|
|
|
283
291
|
}
|
|
284
292
|
throw new SourceError(`concurrent updates to branch '${branch}' - retries exhausted`);
|
|
285
293
|
}
|
|
294
|
+
/**
|
|
295
|
+
* Commit with an explicitly pinned parent — the working-session squash path
|
|
296
|
+
* (design §14e). No compare-and-swap retry: the caller has already run the
|
|
297
|
+
* squash guard, and the whole point is to replace the WIP run above
|
|
298
|
+
* `parentSha`. On push failure the ref is restored to the pre-squash WIP
|
|
299
|
+
* head, never left pointing at an unpushed squash commit.
|
|
300
|
+
*/
|
|
301
|
+
async commitWithExplicitParent(dir, branch, changes, message, options) {
|
|
302
|
+
const parentSha = options.parentSha;
|
|
303
|
+
const newCommitOid = await this.writeCommitObject(dir, changes, parentSha, message, options);
|
|
304
|
+
let preRewriteSha;
|
|
305
|
+
try {
|
|
306
|
+
preRewriteSha = await this.resolveBranchOid(branch);
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
preRewriteSha = undefined;
|
|
310
|
+
}
|
|
311
|
+
await git.writeRef({ fs, dir, ref: `refs/heads/${branch}`, value: newCommitOid, force: true });
|
|
312
|
+
if (this.remoteUrl) {
|
|
313
|
+
try {
|
|
314
|
+
await this.push(dir, branch, options.force === true);
|
|
315
|
+
await this.updateRemoteTrackingRef(dir, branch, newCommitOid);
|
|
316
|
+
}
|
|
317
|
+
catch (err) {
|
|
318
|
+
// Restore the pre-squash WIP head so the branch never points at an
|
|
319
|
+
// unpushed squash commit (design §14e).
|
|
320
|
+
if (preRewriteSha !== undefined) {
|
|
321
|
+
await git.writeRef({ fs, dir, ref: `refs/heads/${branch}`, value: preRewriteSha, force: true });
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
await git.deleteRef({ fs, dir, ref: `refs/heads/${branch}` }).catch(() => { });
|
|
325
|
+
}
|
|
326
|
+
throw new SourceError(`failed to push branch '${branch}'`, err);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
async push(dir, branch, force) {
|
|
331
|
+
await git.push({
|
|
332
|
+
fs,
|
|
333
|
+
http: (await import('isomorphic-git/http/node')).default,
|
|
334
|
+
dir,
|
|
335
|
+
remote: 'origin',
|
|
336
|
+
ref: branch,
|
|
337
|
+
force,
|
|
338
|
+
onAuth: this.onAuth(),
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
async updateRemoteTrackingRef(dir, branch, oid) {
|
|
342
|
+
await git.writeRef({ fs, dir, ref: `refs/remotes/origin/${branch}`, value: oid, force: true });
|
|
343
|
+
}
|
|
344
|
+
/** Write blobs + tree + commit object for `changes` on top of `parentSha`; returns the new commit oid. */
|
|
345
|
+
async writeCommitObject(dir, changes, parentSha, message, options) {
|
|
346
|
+
const prefix = this.graphDir.endsWith('/') ? this.graphDir : `${this.graphDir}/`;
|
|
347
|
+
const { commit: parentCommit } = await git.readCommit({ fs, dir, oid: parentSha });
|
|
348
|
+
const blobMap = new Map();
|
|
349
|
+
for (const [key, content] of changes) {
|
|
350
|
+
const repoPath = `${prefix}${normalizeContentKey(key)}`;
|
|
351
|
+
const oid = await git.writeBlob({ fs, dir, blob: Buffer.from(content, 'utf-8') });
|
|
352
|
+
blobMap.set(repoPath, oid);
|
|
353
|
+
}
|
|
354
|
+
const newTreeOid = options.replaceGraphContent
|
|
355
|
+
? await buildReplacedGraphTree(fs, dir, parentCommit.tree, prefix, blobMap)
|
|
356
|
+
: await buildUpdatedTree(fs, dir, parentCommit.tree, blobMap);
|
|
357
|
+
const now = Math.floor(Date.now() / 1000);
|
|
358
|
+
return git.writeCommit({
|
|
359
|
+
fs,
|
|
360
|
+
dir,
|
|
361
|
+
commit: {
|
|
362
|
+
tree: newTreeOid,
|
|
363
|
+
parent: [parentSha],
|
|
364
|
+
message,
|
|
365
|
+
author: { name: 'corum', email: 'corum@localhost', timestamp: now, timezoneOffset: 0 },
|
|
366
|
+
committer: { name: 'corum', email: 'corum@localhost', timestamp: now, timezoneOffset: 0 },
|
|
367
|
+
},
|
|
368
|
+
});
|
|
369
|
+
}
|
|
286
370
|
async reloadSignature() {
|
|
287
371
|
const dir = await this.dir();
|
|
288
372
|
const branches = await this.listBranches();
|
|
@@ -300,6 +384,12 @@ export class GitGraphSource {
|
|
|
300
384
|
async resolveBranchOid(branch) {
|
|
301
385
|
const dir = await this.dir();
|
|
302
386
|
if (this.remoteUrl) {
|
|
387
|
+
try {
|
|
388
|
+
return await git.resolveRef({ fs, dir, ref: `refs/heads/${branch}` });
|
|
389
|
+
}
|
|
390
|
+
catch {
|
|
391
|
+
// Read-only remote branches may only exist as tracking refs.
|
|
392
|
+
}
|
|
303
393
|
try {
|
|
304
394
|
return await git.resolveRef({ fs, dir, ref: `refs/remotes/origin/${branch}` });
|
|
305
395
|
}
|
|
@@ -61,6 +61,7 @@ function normalizeContentKey(value) {
|
|
|
61
61
|
return value.split(path.sep).join('/');
|
|
62
62
|
}
|
|
63
63
|
function toClusterDocument(graph, root) {
|
|
64
|
+
const sourceDoc = loadSourceClusterDocument(graph, root);
|
|
64
65
|
const metadata = {
|
|
65
66
|
component: root.component,
|
|
66
67
|
state: root.state,
|
|
@@ -79,43 +80,65 @@ function toClusterDocument(graph, root) {
|
|
|
79
80
|
schemaVersion: root.schemaVersion,
|
|
80
81
|
metadata,
|
|
81
82
|
};
|
|
83
|
+
if (root.corum !== undefined) {
|
|
84
|
+
doc.corum = root.corum;
|
|
85
|
+
}
|
|
82
86
|
if (Object.keys(root.properties).length > 0) {
|
|
83
87
|
doc.properties = orderNodeProperties(graph, root);
|
|
84
88
|
}
|
|
85
|
-
appendOwnedSections(graph, root, doc);
|
|
89
|
+
appendOwnedSections(graph, root, doc, sourceDoc);
|
|
86
90
|
return doc;
|
|
87
91
|
}
|
|
88
|
-
function appendOwnedSections(graph, parent, target) {
|
|
92
|
+
function appendOwnedSections(graph, parent, target, sourceDoc) {
|
|
89
93
|
const template = graph.templates.get(parent.template);
|
|
90
94
|
if (!template)
|
|
91
95
|
return;
|
|
92
|
-
for (const
|
|
93
|
-
const
|
|
96
|
+
for (const sectionName of getOrderedOwnedSectionNames(template, sourceDoc)) {
|
|
97
|
+
const sourceSection = asRecord(sourceDoc?.[sectionName]);
|
|
98
|
+
const children = getDirectOwnedChildren(graph, parent.id, sectionName, sourceSection);
|
|
94
99
|
if (children.length === 0)
|
|
95
100
|
continue;
|
|
96
101
|
const section = {};
|
|
97
102
|
for (const child of children) {
|
|
103
|
+
const localName = getLocalName(child.id, parent.id, sectionName);
|
|
98
104
|
const childDoc = { ...orderNodeProperties(graph, child) };
|
|
99
105
|
if (child.state !== parent.state)
|
|
100
106
|
childDoc.state = child.state;
|
|
101
107
|
if (child.stability !== parent.stability)
|
|
102
108
|
childDoc.stability = child.stability;
|
|
103
|
-
|
|
104
|
-
|
|
109
|
+
if (child.corum !== undefined)
|
|
110
|
+
childDoc.corum = child.corum;
|
|
111
|
+
appendOwnedSections(graph, child, childDoc, asRecord(sourceSection?.[localName]));
|
|
112
|
+
section[localName] = childDoc;
|
|
105
113
|
}
|
|
106
114
|
target[sectionName] = section;
|
|
107
115
|
}
|
|
108
116
|
}
|
|
109
|
-
function getDirectOwnedChildren(graph, parentId, sectionName) {
|
|
117
|
+
function getDirectOwnedChildren(graph, parentId, sectionName, sourceSection) {
|
|
110
118
|
const prefix = `${parentId}.${sectionName}.`;
|
|
111
|
-
|
|
119
|
+
const children = [...graph.nodesById.values()]
|
|
112
120
|
.filter(node => {
|
|
113
121
|
if (!node.id.startsWith(prefix))
|
|
114
122
|
return false;
|
|
115
123
|
const remaining = node.id.slice(prefix.length);
|
|
116
124
|
return !remaining.includes('.');
|
|
117
|
-
})
|
|
118
|
-
|
|
125
|
+
});
|
|
126
|
+
if (!sourceSection) {
|
|
127
|
+
return children.sort((a, b) => a.id.localeCompare(b.id));
|
|
128
|
+
}
|
|
129
|
+
const byLocalName = new Map(children.map(node => [getLocalName(node.id, parentId, sectionName), node]));
|
|
130
|
+
const ordered = [];
|
|
131
|
+
for (const localName of Object.keys(sourceSection)) {
|
|
132
|
+
const node = byLocalName.get(localName);
|
|
133
|
+
if (!node)
|
|
134
|
+
continue;
|
|
135
|
+
ordered.push(node);
|
|
136
|
+
byLocalName.delete(localName);
|
|
137
|
+
}
|
|
138
|
+
const remaining = [...byLocalName.entries()]
|
|
139
|
+
.sort((a, b) => a[0].localeCompare(b[0]))
|
|
140
|
+
.map(([, node]) => node);
|
|
141
|
+
return [...ordered, ...remaining];
|
|
119
142
|
}
|
|
120
143
|
function getLocalName(childId, parentId, sectionName) {
|
|
121
144
|
return childId.slice(`${parentId}.${sectionName}.`.length);
|
|
@@ -123,6 +146,37 @@ function getLocalName(childId, parentId, sectionName) {
|
|
|
123
146
|
function stringifyGraphYaml(value) {
|
|
124
147
|
return stringifyYaml(value, YAML_STRINGIFY_OPTIONS);
|
|
125
148
|
}
|
|
149
|
+
function loadSourceClusterDocument(graph, root) {
|
|
150
|
+
const content = graph.sourceContent?.get(clusterPath(root));
|
|
151
|
+
if (!content)
|
|
152
|
+
return undefined;
|
|
153
|
+
return asRecord(parseYaml(content));
|
|
154
|
+
}
|
|
155
|
+
function getOrderedOwnedSectionNames(template, sourceDoc) {
|
|
156
|
+
const templateSectionNames = Object.keys(getOwnedSections(template));
|
|
157
|
+
if (!sourceDoc)
|
|
158
|
+
return templateSectionNames;
|
|
159
|
+
const ordered = [];
|
|
160
|
+
const remaining = new Set(templateSectionNames);
|
|
161
|
+
for (const key of Object.keys(sourceDoc)) {
|
|
162
|
+
if (!remaining.has(key))
|
|
163
|
+
continue;
|
|
164
|
+
ordered.push(key);
|
|
165
|
+
remaining.delete(key);
|
|
166
|
+
}
|
|
167
|
+
for (const key of templateSectionNames) {
|
|
168
|
+
if (!remaining.has(key))
|
|
169
|
+
continue;
|
|
170
|
+
ordered.push(key);
|
|
171
|
+
remaining.delete(key);
|
|
172
|
+
}
|
|
173
|
+
return ordered;
|
|
174
|
+
}
|
|
175
|
+
function asRecord(value) {
|
|
176
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
177
|
+
return undefined;
|
|
178
|
+
return value;
|
|
179
|
+
}
|
|
126
180
|
/**
|
|
127
181
|
* Canonical property key order at serialisation time: template-declared
|
|
128
182
|
* property order first, then any remaining (undeclared) keys alphabetically.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atolis-hq/corum",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.45",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"wireframes": "node design/wireframes/server.mjs",
|
|
20
20
|
"mcp": "node dist/src/mcp/index.js",
|
|
21
21
|
"mcp:smoke": "npm run build && node scripts/mcp-smoke.mjs",
|
|
22
|
+
"mcp:write-smoke": "npm run build && node scripts/mcp-write-smoke.mjs",
|
|
22
23
|
"web": "node dist/src/web/server.js"
|
|
23
24
|
},
|
|
24
25
|
"dependencies": {
|