forked from nexu-io/open-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandoff.ts
More file actions
330 lines (307 loc) · 12.4 KB
/
Copy pathhandoff.ts
File metadata and controls
330 lines (307 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Phase 7-8 entry slice / spec §10 / §11.5.1 / §21.5 — handoff atom.
//
// SKILL.md fragment lives at plugins/_official/atoms/handoff/. The
// daemon-side helper updates an ArtifactManifest's provenance +
// distribution metadata so subsequent runs (and the CLI's
// `od plugin export`) can reverse-resolve the artifact's lineage
// without mutating any prior fields. The contract is append-only:
//
// - sourcePluginSnapshotId NEVER changes after first write.
// - exportTargets[] / deployTargets[] only ever GROW.
// - handoffKind can be promoted (e.g. 'patch' → 'deployable-app')
// when build-test signals + diff-review acceptance combine.
import path from 'node:path';
import { promises as fsp } from 'node:fs';
import type {
ArtifactDeployTarget,
ArtifactExportTarget,
ArtifactManifest,
ArtifactProvenanceHandoffKind,
} from '@open-design/contracts';
export interface RecordHandoffInput {
manifest: ArtifactManifest;
exportTarget?: ArtifactExportTarget;
deployTarget?: ArtifactDeployTarget;
handoffKind?: ArtifactProvenanceHandoffKind;
// When true (default), refuse to demote handoffKind back along the
// axis 'design-only' < 'implementation-plan' < 'patch' < 'deployable-app'.
// Setting false lets a roll-back path explicitly downgrade after a
// failed deploy.
enforceMonotonicHandoff?: boolean;
}
export interface RecordHandoffResult {
manifest: ArtifactManifest;
changed: Array<'exportTargets' | 'deployTargets' | 'handoffKind'>;
}
const HANDOFF_RANK: Record<ArtifactProvenanceHandoffKind, number> = {
'design-only': 0,
'implementation-plan': 1,
'patch': 2,
'deployable-app': 3,
};
export function recordHandoff(input: RecordHandoffInput): RecordHandoffResult {
const changed: RecordHandoffResult['changed'] = [];
// Clone shallowly so the caller's reference isn't mutated; arrays
// we touch get fresh copies before we push.
const next: ArtifactManifest = { ...input.manifest };
if (input.exportTarget) {
const incoming = input.exportTarget;
const existing = next.exportTargets ?? [];
// Idempotency: a (surface, target) pair only ever lands once.
const already = existing.some(
(t: ArtifactExportTarget) => t.surface === incoming.surface && t.target === incoming.target,
);
if (!already) {
next.exportTargets = [...existing, incoming];
changed.push('exportTargets');
}
}
if (input.deployTarget) {
const incoming = input.deployTarget;
const existing = next.deployTargets ?? [];
const already = existing.some(
(t: ArtifactDeployTarget) => t.provider === incoming.provider && t.location === incoming.location,
);
if (!already) {
next.deployTargets = [...existing, incoming];
changed.push('deployTargets');
}
}
if (input.handoffKind) {
const enforce = input.enforceMonotonicHandoff ?? true;
const current = next.handoffKind;
if (!current) {
next.handoffKind = input.handoffKind;
changed.push('handoffKind');
} else {
const incomingRank = HANDOFF_RANK[input.handoffKind] ?? 0;
const currentRank = HANDOFF_RANK[current] ?? 0;
if (!enforce || incomingRank >= currentRank) {
if (current !== input.handoffKind) {
next.handoffKind = input.handoffKind;
changed.push('handoffKind');
}
}
}
}
return { manifest: next, changed };
}
// Spec §11.5.1 promotion rule for the deployable-app tier:
// `handoffKind: 'deployable-app'` requires:
// - build.passing (the build-test atom emitted true)
// - tests.passing (same)
// - at least one exportTargets[] entry on a 'docker' or 'cli' surface
// (i.e. the patch was actually packaged for delivery)
//
// This helper computes the eligibility flag so the handoff atom's
// caller can promote in one place rather than leaking the rule into
// every plugin.
export function isDeployableAppEligible(args: {
manifest: ArtifactManifest;
buildPassing?: boolean;
testsPassing?: boolean;
}): boolean {
if (args.buildPassing !== true) return false;
if (args.testsPassing !== true) return false;
const exports = args.manifest.exportTargets ?? [];
return exports.some((t: ArtifactExportTarget) => t.surface === 'docker' || t.surface === 'cli');
}
// Plan §3.S1 — pipeline-driven handoff bridge.
//
// Reads the on-disk state previous atoms wrote (critique/build-test.json,
// review/decision.json) and returns the updated manifest with the right
// handoffKind / exportTargets[] / signals attached. The function is
// pure relative to its inputs (it reads files, never writes back). The
// caller decides where to persist the updated manifest (typically
// `<cwd>/<manifest-path>` or `.od/artifacts/<id>/manifest.json`).
//
// Promotion ladder (spec §11.5.1):
// 1. decision='reject' → handoffKind='design-only'
// 2. decision='accept'/'partial' AND no build-test → handoffKind='implementation-plan'
// 3. (2) + build.passing OR tests.passing → handoffKind='patch'
// 4. (3) + build.passing && tests.passing + docker/cli exportTarget
// → handoffKind='deployable-app'
//
// Monotonicity is enforced via recordHandoff() — a subsequent run
// can only advance, never demote.
export interface RunHandoffAtomInput {
cwd: string;
manifest: ArtifactManifest;
// Optional explicit export target the caller is recording (e.g.
// 'cli' when od plugin export wrote to disk; 'docker' when the
// tools-pack image build completes; 'figma' when Figma export
// wrote a frame back).
exportTarget?: ArtifactExportTarget;
exportTargets?: ArtifactExportTarget[];
deployTarget?: ArtifactDeployTarget;
deployTargets?: ArtifactDeployTarget[];
}
export interface RunHandoffAtomResult {
manifest: ArtifactManifest;
changed: Array<'exportTargets' | 'deployTargets' | 'handoffKind'>;
signals: {
decision?: 'accept' | 'reject' | 'partial';
buildPassing?: boolean;
testsPassing?: boolean;
deployable: boolean;
};
}
export async function runHandoffAtom(input: RunHandoffAtomInput): Promise<RunHandoffAtomResult> {
const cwd = path.resolve(input.cwd);
const decision = await readDiffReviewDecision(cwd);
const buildTest = await readBuildTestSignals(cwd);
// Start by appending whatever explicit export/deploy targets the
// caller passed in. Idempotency is enforced inside recordHandoff().
let next = input.manifest;
let changed: RunHandoffAtomResult['changed'] = [];
const targets: ArtifactExportTarget[] = [
...(input.exportTarget ? [input.exportTarget] : []),
...(input.exportTargets ?? []),
];
for (const t of targets) {
const out = recordHandoff({ manifest: next, exportTarget: t });
next = out.manifest;
for (const c of out.changed) if (!changed.includes(c)) changed.push(c);
}
const deploys: ArtifactDeployTarget[] = [
...(input.deployTarget ? [input.deployTarget] : []),
...(input.deployTargets ?? []),
];
for (const d of deploys) {
const out = recordHandoff({ manifest: next, deployTarget: d });
next = out.manifest;
for (const c of out.changed) if (!changed.includes(c)) changed.push(c);
}
// Compute the target handoff kind from the on-disk state.
let handoffKind: ArtifactProvenanceHandoffKind | undefined;
if (decision === 'reject') {
handoffKind = 'design-only';
} else if (decision === 'accept' || decision === 'partial') {
handoffKind = 'implementation-plan';
if (buildTest && (buildTest.buildPassing || buildTest.testsPassing)) {
handoffKind = 'patch';
}
if (buildTest && isDeployableAppEligible({
manifest: next,
buildPassing: buildTest.buildPassing,
testsPassing: buildTest.testsPassing,
})) {
handoffKind = 'deployable-app';
}
}
if (handoffKind) {
const out = recordHandoff({ manifest: next, handoffKind });
next = out.manifest;
for (const c of out.changed) if (!changed.includes(c)) changed.push(c);
}
const signals: RunHandoffAtomResult['signals'] = {
deployable: handoffKind === 'deployable-app',
};
if (decision) signals.decision = decision;
if (buildTest) {
signals.buildPassing = buildTest.buildPassing;
signals.testsPassing = buildTest.testsPassing;
}
return { manifest: next, changed, signals };
}
async function readDiffReviewDecision(cwd: string): Promise<'accept' | 'reject' | 'partial' | undefined> {
const p = path.join(cwd, 'review', 'decision.json');
try {
const raw = await fsp.readFile(p, 'utf8');
const obj = JSON.parse(raw) as { decision?: unknown };
if (obj.decision === 'accept' || obj.decision === 'reject' || obj.decision === 'partial') {
return obj.decision;
}
return undefined;
} catch {
return undefined;
}
}
// Plan §3.T1 — `<cwd>/handoff/manifest.json` round-trip.
//
// runAndPersistHandoff() is the on-disk shell around runHandoffAtom():
//
// 1. Read `<cwd>/handoff/manifest.json` (or fall back to
// `manifestSeed`, or finally a minimal default) so promotions
// stay monotonic across re-runs.
// 2. Call runHandoffAtom() with the chosen seed + the caller's
// explicit export/deploy targets.
// 3. Write the updated manifest back to
// `<cwd>/handoff/manifest.json`.
// 4. Return the report shape the caller can forward to SSE / CLI
// audit logs.
//
// The bridge fires from the diff-review GenUI flow and from any
// pipeline runner that wants the manifest computed declaratively.
export interface RunAndPersistHandoffInput {
cwd: string;
// When the manifest file is missing AND no exportTargets are
// declared, fall back to this seed. Useful for the first pipeline
// run: the agent hasn't produced a manifest yet but we still want
// to record the promotion ladder progress.
manifestSeed?: ArtifactManifest;
exportTarget?: ArtifactExportTarget;
exportTargets?: ArtifactExportTarget[];
deployTarget?: ArtifactDeployTarget;
deployTargets?: ArtifactDeployTarget[];
}
export interface RunAndPersistHandoffResult extends RunHandoffAtomResult {
manifestPath: string;
// 'created' = no on-disk manifest existed; 'updated' = round-tripped;
// 'skipped' = nothing changed (re-run was a no-op).
persistMode: 'created' | 'updated' | 'skipped';
}
const DEFAULT_MANIFEST_SEED: ArtifactManifest = {
version: 1,
kind: 'react-component',
title: 'Pipeline output',
entry: '',
renderer: 'react-component',
exports: [],
};
export async function runAndPersistHandoff(
input: RunAndPersistHandoffInput,
): Promise<RunAndPersistHandoffResult> {
const cwd = path.resolve(input.cwd);
const manifestPath = path.join(cwd, 'handoff', 'manifest.json');
const existing = await readManifestFile(manifestPath);
const seed: ArtifactManifest = existing ?? input.manifestSeed ?? DEFAULT_MANIFEST_SEED;
const handoffInput: RunHandoffAtomInput = { cwd, manifest: seed };
if (input.exportTarget) handoffInput.exportTarget = input.exportTarget;
if (input.exportTargets) handoffInput.exportTargets = input.exportTargets;
if (input.deployTarget) handoffInput.deployTarget = input.deployTarget;
if (input.deployTargets) handoffInput.deployTargets = input.deployTargets;
const report = await runHandoffAtom(handoffInput);
let persistMode: RunAndPersistHandoffResult['persistMode'];
if (!existing) {
await fsp.mkdir(path.dirname(manifestPath), { recursive: true });
await fsp.writeFile(manifestPath, JSON.stringify(report.manifest, null, 2) + '\n', 'utf8');
persistMode = 'created';
} else if (report.changed.length > 0) {
await fsp.writeFile(manifestPath, JSON.stringify(report.manifest, null, 2) + '\n', 'utf8');
persistMode = 'updated';
} else {
persistMode = 'skipped';
}
return { ...report, manifestPath, persistMode };
}
async function readManifestFile(p: string): Promise<ArtifactManifest | undefined> {
try {
const raw = await fsp.readFile(p, 'utf8');
return JSON.parse(raw) as ArtifactManifest;
} catch {
return undefined;
}
}
async function readBuildTestSignals(cwd: string): Promise<{ buildPassing: boolean; testsPassing: boolean } | undefined> {
const p = path.join(cwd, 'critique', 'build-test.json');
try {
const raw = await fsp.readFile(p, 'utf8');
const obj = JSON.parse(raw) as { signals?: { 'build.passing'?: unknown; 'tests.passing'?: unknown } };
const buildPassing = obj.signals?.['build.passing'] === true;
const testsPassing = obj.signals?.['tests.passing'] === true;
return { buildPassing, testsPassing };
} catch {
return undefined;
}
}