@vxil/feature-configs 0.4.1 → 0.5.1
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/apiState.d.ts +179 -0
- package/dist/apiState.js +219 -0
- package/dist/canonicalJson.d.ts +7 -0
- package/dist/canonicalJson.js +16 -0
- package/dist/index.d.ts +88 -5
- package/dist/index.js +303 -11
- package/package.json +1 -1
- package/src/apiState.ts +347 -0
- package/src/canonicalJson.ts +15 -0
- package/src/index.ts +335 -30
package/src/apiState.ts
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
// DECLARED API STATE — the pure declared-vs-live planner (roadmap §4.11 P0-3,
|
|
2
|
+
// 2026-09-23).
|
|
3
|
+
//
|
|
4
|
+
// Three datums used to be API state OUTSIDE vxil.config — rows their own /v1
|
|
5
|
+
// route created, that `vxil push` never converged and a rollback never
|
|
6
|
+
// restored — so every tenant that wanted a reproducible second environment
|
|
7
|
+
// wrote an `ensure-*.ts` script and a nightly assert (the cvskit evaluation,
|
|
8
|
+
// §8 P1-7). They are now DECLARED in the feature's config block and CONVERGED
|
|
9
|
+
// by `vxil push` and `POST /v1/apply`:
|
|
10
|
+
//
|
|
11
|
+
// rate-limits.policies[] — keyed by `name` (POST/PUT/DELETE /v1/rate-limits/policies)
|
|
12
|
+
// webhooks.subscriptions[] — keyed by `target_url` (POST/DELETE /v1/webhooks/subscriptions)
|
|
13
|
+
// ai.templates[] — keyed by `template` (POST /v1/ai/templates, by content hash)
|
|
14
|
+
//
|
|
15
|
+
// ONE WRITER PER DATUM, AND THE WRITER IS THE REPOSITORY. The planner below is
|
|
16
|
+
// PURE (no I/O, no clock): it takes the declared list and the live rows and
|
|
17
|
+
// returns the exact operation set. ONE executor drives it: the control-plane
|
|
18
|
+
// (handlers/apiState.ts convergeApiState — the feature's own routes with the
|
|
19
|
+
// caller's scopes, or its own table in-process), after every config write and
|
|
20
|
+
// as the api-state step of POST /v1/apply. The CLI (packages/cli/src/
|
|
21
|
+
// apiState.ts) never writes these rows: `vxil push` asks the server, and
|
|
22
|
+
// `vxil plan` / `vxil diff` run this planner READ-ONLY when the server's dry
|
|
23
|
+
// run could not plan. packages/cli/src/apiState.e2e.test.ts drives the real
|
|
24
|
+
// CLI binary against the real convergeApiState.
|
|
25
|
+
//
|
|
26
|
+
// THE NON-DESTRUCTIVE DEFAULT. A live row the config does NOT declare is
|
|
27
|
+
// LEFT IN PLACE and reported as `undeclared` — deleted only when the caller
|
|
28
|
+
// passes allow-destructive — so adopting a block on an existing tenant is a
|
|
29
|
+
// report, never a wipe. A change that can only be applied by delete+recreate
|
|
30
|
+
// (a policy's `key_template`; a subscription's prefix set) is likewise
|
|
31
|
+
// destructive-SHAPED: it changes the row's id, which a caller may have
|
|
32
|
+
// cached, so it is reported and applied only under the same ack.
|
|
33
|
+
//
|
|
34
|
+
// Two shapes this file deliberately does NOT cover, with the reason:
|
|
35
|
+
// notifications.templates — templates are CODE with per-locale `overrides`
|
|
36
|
+
// already in config (templates.overrides); there is no stored-template row.
|
|
37
|
+
// notifications.campaigns — a campaign is a lifecycle row (draft → scheduled
|
|
38
|
+
// → sending → …) with runtime state; declaring it would re-send.
|
|
39
|
+
import type { DeclaredRlPolicy, DeclaredWebhookSubscription } from './index.js';
|
|
40
|
+
|
|
41
|
+
/** The target_url path markers of the platform's function-delivery lanes
|
|
42
|
+
* (control-plane fnCmsHooks.ts FN_CMSHOOK_PATH / FN_AUTHHOOK_PATH /
|
|
43
|
+
* FN_WEBHOOK_TRIGGER_PATH — mirrored here because this package is
|
|
44
|
+
* typebox-only). A subscription on one of them is derived from the functions
|
|
45
|
+
* manifest by the deploy reconciler: never declarable, never "undeclared". */
|
|
46
|
+
export const FN_TRIGGER_TARGET_MARKERS = [
|
|
47
|
+
'/v1/internal/fn/cms-hook/',
|
|
48
|
+
'/v1/internal/fn/auth-hook/',
|
|
49
|
+
'/v1/internal/fn/trigger/',
|
|
50
|
+
] as const;
|
|
51
|
+
export function isFnTriggerSubscriptionUrl(targetUrl: string): boolean {
|
|
52
|
+
return FN_TRIGGER_TARGET_MARKERS.some((m) => targetUrl.includes(m));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type ApiStateDatum = 'policies' | 'subscriptions' | 'templates';
|
|
56
|
+
|
|
57
|
+
/** feature → the declared-API-state datum its config block may carry. */
|
|
58
|
+
export const API_STATE_DATUMS: ReadonlyArray<{ feature: string; datum: ApiStateDatum }> = [
|
|
59
|
+
{ feature: 'rate-limits', datum: 'policies' },
|
|
60
|
+
{ feature: 'webhooks', datum: 'subscriptions' },
|
|
61
|
+
{ feature: 'ai', datum: 'templates' },
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
export function apiStateDatumOf(feature: string): ApiStateDatum | null {
|
|
65
|
+
return API_STATE_DATUMS.find((d) => d.feature === feature)?.datum ?? null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The declared list a validated manifest carries for its feature's datum, or
|
|
69
|
+
* null when the feature has no datum / the block is absent. An absent block
|
|
70
|
+
* means "not managed by config" (nothing converges, nothing is reported) —
|
|
71
|
+
* distinct from an EMPTY array, which declares "there should be none". */
|
|
72
|
+
export function declaredApiState(
|
|
73
|
+
feature: string, manifest: Record<string, unknown> | null | undefined,
|
|
74
|
+
): { datum: ApiStateDatum; declared: unknown[] } | null {
|
|
75
|
+
const datum = apiStateDatumOf(feature);
|
|
76
|
+
if (!datum || !manifest) return null;
|
|
77
|
+
const list = manifest[datum];
|
|
78
|
+
if (!Array.isArray(list)) return null;
|
|
79
|
+
return { datum, declared: list };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ── rate-limits policies ─────────────────────────────────────────────────────
|
|
83
|
+
|
|
84
|
+
/** ONE row of GET /v1/rate-limits/policies (rate-limits-v1 core.ts Policy). */
|
|
85
|
+
export interface LiveRlPolicy {
|
|
86
|
+
policy_id: string;
|
|
87
|
+
name: string;
|
|
88
|
+
key_template: string;
|
|
89
|
+
limit: number;
|
|
90
|
+
window_seconds: number;
|
|
91
|
+
behavior: string;
|
|
92
|
+
/** absent on a policy stored before the token-bucket pass → 'sliding_window' */
|
|
93
|
+
algorithm?: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The PUT /v1/rate-limits/policies/:id body the converge sends (core.ts
|
|
97
|
+
* UpdateBody minus `name`, which is the key). */
|
|
98
|
+
export interface RlPolicyPatch {
|
|
99
|
+
limit?: number;
|
|
100
|
+
window_seconds?: number;
|
|
101
|
+
behavior?: 'block' | 'shape';
|
|
102
|
+
algorithm?: 'sliding_window' | 'token_bucket';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export const RL_DEFAULT_BEHAVIOR = 'block' as const;
|
|
106
|
+
export const RL_DEFAULT_ALGORITHM = 'sliding_window' as const;
|
|
107
|
+
|
|
108
|
+
export interface RlPolicyPlan {
|
|
109
|
+
create: DeclaredRlPolicy[];
|
|
110
|
+
update: Array<{ policy_id: string; name: string; patch: RlPolicyPatch; changed: string[] }>;
|
|
111
|
+
/** `key_template` differs — the update route cannot change it, so the only
|
|
112
|
+
* fix is delete+recreate (a NEW policy_id): destructive-shaped. */
|
|
113
|
+
recreate: Array<{ policy_id: string; name: string; declared: DeclaredRlPolicy }>;
|
|
114
|
+
unchanged: string[];
|
|
115
|
+
/** live rows the config does not declare (left in place by default) */
|
|
116
|
+
undeclared: Array<{ policy_id: string; name: string }>;
|
|
117
|
+
/** a declared name matched by MORE THAN ONE live row — names are not a
|
|
118
|
+
* unique key on the route, so the converge cannot pick; nothing is touched */
|
|
119
|
+
ambiguous: Array<{ name: string; count: number }>;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function planRlPolicies(
|
|
123
|
+
declared: readonly DeclaredRlPolicy[], live: readonly LiveRlPolicy[],
|
|
124
|
+
): RlPolicyPlan {
|
|
125
|
+
const plan: RlPolicyPlan = { create: [], update: [], recreate: [], unchanged: [], undeclared: [], ambiguous: [] };
|
|
126
|
+
const byName = new Map<string, LiveRlPolicy[]>();
|
|
127
|
+
for (const row of live) byName.set(row.name, [...(byName.get(row.name) ?? []), row]);
|
|
128
|
+
const declaredNames = new Set(declared.map((d) => d.name));
|
|
129
|
+
for (const d of declared) {
|
|
130
|
+
const rows = byName.get(d.name) ?? [];
|
|
131
|
+
if (rows.length === 0) { plan.create.push(d); continue; }
|
|
132
|
+
if (rows.length > 1) { plan.ambiguous.push({ name: d.name, count: rows.length }); continue; }
|
|
133
|
+
const l = rows[0]!;
|
|
134
|
+
if (l.key_template !== d.key_template) { plan.recreate.push({ policy_id: l.policy_id, name: d.name, declared: d }); continue; }
|
|
135
|
+
const patch: RlPolicyPatch = {};
|
|
136
|
+
const changed: string[] = [];
|
|
137
|
+
if (d.limit !== undefined && d.limit !== l.limit) { patch.limit = d.limit; changed.push('limit'); }
|
|
138
|
+
if (d.window_seconds !== undefined && d.window_seconds !== l.window_seconds) { patch.window_seconds = d.window_seconds; changed.push('window_seconds'); }
|
|
139
|
+
const wantBehavior: 'block' | 'shape' = d.behavior ?? RL_DEFAULT_BEHAVIOR;
|
|
140
|
+
if (wantBehavior !== l.behavior) { patch.behavior = wantBehavior; changed.push('behavior'); }
|
|
141
|
+
const wantAlgorithm: 'sliding_window' | 'token_bucket' = d.algorithm ?? RL_DEFAULT_ALGORITHM;
|
|
142
|
+
if (wantAlgorithm !== (l.algorithm ?? RL_DEFAULT_ALGORITHM)) { patch.algorithm = wantAlgorithm; changed.push('algorithm'); }
|
|
143
|
+
if (changed.length) plan.update.push({ policy_id: l.policy_id, name: d.name, patch, changed });
|
|
144
|
+
else plan.unchanged.push(d.name);
|
|
145
|
+
}
|
|
146
|
+
for (const row of live) {
|
|
147
|
+
if (!declaredNames.has(row.name)) plan.undeclared.push({ policy_id: row.policy_id, name: row.name });
|
|
148
|
+
}
|
|
149
|
+
return plan;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ── webhooks subscriptions ───────────────────────────────────────────────────
|
|
153
|
+
|
|
154
|
+
/** ONE row of GET /v1/webhooks/subscriptions (state 'deleted' never listed). */
|
|
155
|
+
export interface LiveWebhookSubscription {
|
|
156
|
+
sub_id: string;
|
|
157
|
+
target_url: string;
|
|
158
|
+
event_prefixes: string[];
|
|
159
|
+
state?: string;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export interface WebhookSubscriptionPlan {
|
|
163
|
+
create: DeclaredWebhookSubscription[];
|
|
164
|
+
/** the prefix SET differs — there is no update route, so delete+recreate (a
|
|
165
|
+
* NEW sub_id, and the watermark restarts at "now"): destructive-shaped. */
|
|
166
|
+
recreate: Array<{ sub_id: string; target_url: string; declared: DeclaredWebhookSubscription }>;
|
|
167
|
+
unchanged: string[];
|
|
168
|
+
/** live rows the config does not declare, EXCLUDING the function-delivery
|
|
169
|
+
* lanes (those derive from the functions manifest) — left in place by default */
|
|
170
|
+
undeclared: Array<{ sub_id: string; target_url: string }>;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function sameStringSet(a: readonly string[], b: readonly string[]): boolean {
|
|
174
|
+
const A = new Set(a); const B = new Set(b);
|
|
175
|
+
return A.size === B.size && [...A].every((x) => B.has(x));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function planWebhookSubscriptions(
|
|
179
|
+
declared: readonly DeclaredWebhookSubscription[], live: readonly LiveWebhookSubscription[],
|
|
180
|
+
): WebhookSubscriptionPlan {
|
|
181
|
+
const plan: WebhookSubscriptionPlan = { create: [], recreate: [], unchanged: [], undeclared: [] };
|
|
182
|
+
const declaredUrls = new Set(declared.map((d) => d.target_url));
|
|
183
|
+
const matched = new Set<string>();
|
|
184
|
+
for (const d of declared) {
|
|
185
|
+
const want = d.event_prefixes ?? [];
|
|
186
|
+
const rows = live.filter((l) => l.target_url === d.target_url);
|
|
187
|
+
if (rows.length === 0) { plan.create.push(d); continue; }
|
|
188
|
+
// Prefer an EXACT prefix-set match among duplicates (a hand-made row equal
|
|
189
|
+
// to the declaration is adopted as-is — no churn); else the first row is
|
|
190
|
+
// the one recreated and the rest fall through as undeclared duplicates.
|
|
191
|
+
const exact = rows.find((l) => sameStringSet(l.event_prefixes ?? [], want));
|
|
192
|
+
const chosen = exact ?? rows[0]!;
|
|
193
|
+
matched.add(chosen.sub_id);
|
|
194
|
+
if (exact) plan.unchanged.push(d.target_url);
|
|
195
|
+
else plan.recreate.push({ sub_id: chosen.sub_id, target_url: d.target_url, declared: d });
|
|
196
|
+
}
|
|
197
|
+
for (const l of live) {
|
|
198
|
+
if (matched.has(l.sub_id)) continue;
|
|
199
|
+
if (isFnTriggerSubscriptionUrl(l.target_url)) continue; // manifest-derived, never ours
|
|
200
|
+
if (declaredUrls.has(l.target_url)) {
|
|
201
|
+
// a duplicate live row for a declared url — surplus, not the adopted one
|
|
202
|
+
plan.undeclared.push({ sub_id: l.sub_id, target_url: l.target_url });
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
plan.undeclared.push({ sub_id: l.sub_id, target_url: l.target_url });
|
|
206
|
+
}
|
|
207
|
+
return plan;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// ── ai templates ─────────────────────────────────────────────────────────────
|
|
211
|
+
|
|
212
|
+
/** ONE row of GET /v1/ai/templates. `content_sha256` is the canonical hash of
|
|
213
|
+
* the LATEST version's {system,user,schema} (@vxil/runtime
|
|
214
|
+
* aiTemplateContentSha256), stamped by ai-v1 since 2026-09-23; absent on an
|
|
215
|
+
* older server. */
|
|
216
|
+
export interface LiveAiTemplate {
|
|
217
|
+
name: string;
|
|
218
|
+
latest_version: number;
|
|
219
|
+
content_sha256?: string;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** A declared template with its content hash pre-computed by the caller (the
|
|
223
|
+
* hash is async WebCrypto; this planner stays pure/sync). */
|
|
224
|
+
export interface HashedDeclaredAiTemplate {
|
|
225
|
+
template: string;
|
|
226
|
+
content_sha256: string;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export interface AiTemplatePlan {
|
|
230
|
+
create: string[];
|
|
231
|
+
/** content differs → POST a new version (monotonic per name) */
|
|
232
|
+
update: string[];
|
|
233
|
+
unchanged: string[];
|
|
234
|
+
/** stored names the config does not declare — there is NO delete route for
|
|
235
|
+
* templates, so these are only ever reported, never removed */
|
|
236
|
+
undeclared: string[];
|
|
237
|
+
/** live rows with no `content_sha256` (an older server): the content cannot
|
|
238
|
+
* be compared, so each is re-put as `update` and named here */
|
|
239
|
+
unknownContent: string[];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function planAiTemplates(
|
|
243
|
+
declared: readonly HashedDeclaredAiTemplate[], live: readonly LiveAiTemplate[],
|
|
244
|
+
): AiTemplatePlan {
|
|
245
|
+
const plan: AiTemplatePlan = { create: [], update: [], unchanged: [], undeclared: [], unknownContent: [] };
|
|
246
|
+
const byName = new Map(live.map((l) => [l.name, l]));
|
|
247
|
+
const declaredNames = new Set(declared.map((d) => d.template));
|
|
248
|
+
for (const d of declared) {
|
|
249
|
+
const l = byName.get(d.template);
|
|
250
|
+
if (!l) { plan.create.push(d.template); continue; }
|
|
251
|
+
if (l.content_sha256 === undefined) { plan.unknownContent.push(d.template); plan.update.push(d.template); continue; }
|
|
252
|
+
if (l.content_sha256 === d.content_sha256) plan.unchanged.push(d.template);
|
|
253
|
+
else plan.update.push(d.template);
|
|
254
|
+
}
|
|
255
|
+
for (const l of live) if (!declaredNames.has(l.name)) plan.undeclared.push(l.name);
|
|
256
|
+
return plan;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// ── the converge SUMMARY (what `vxil push` prints, what the journal stores) ──
|
|
260
|
+
|
|
261
|
+
export interface ApiStateSummary {
|
|
262
|
+
datum: ApiStateDatum;
|
|
263
|
+
created: string[];
|
|
264
|
+
updated: string[];
|
|
265
|
+
/** destructive-shaped changes APPLIED under allow-destructive (delete+recreate) */
|
|
266
|
+
recreated: string[];
|
|
267
|
+
unchanged: string[];
|
|
268
|
+
/** live rows the config does not declare — left in place (or, under
|
|
269
|
+
* allow-destructive, listed in `deleted` instead) */
|
|
270
|
+
undeclared: string[];
|
|
271
|
+
/** undeclared rows DELETED under allow-destructive */
|
|
272
|
+
deleted: string[];
|
|
273
|
+
/** destructive-shaped changes NOT applied (need allow-destructive) */
|
|
274
|
+
pending_destructive: string[];
|
|
275
|
+
/** declared keys the converge could not resolve (duplicate live rows) */
|
|
276
|
+
ambiguous: string[];
|
|
277
|
+
/** rows a route refused (a 4xx) — the converge continues past them */
|
|
278
|
+
failed: Array<{ key: string; error: string }>;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export function emptyApiStateSummary(datum: ApiStateDatum): ApiStateSummary {
|
|
282
|
+
return {
|
|
283
|
+
datum, created: [], updated: [], recreated: [], unchanged: [], undeclared: [],
|
|
284
|
+
deleted: [], pending_destructive: [], ambiguous: [], failed: [],
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** The `api_state` object a config-write response carries (`PUT /v1/config/
|
|
289
|
+
* :feature` and its dashboard twin, dry-run included — control-plane
|
|
290
|
+
* handlers/apiState.ts apiStateResponseField) and the shape `vxil push` /
|
|
291
|
+
* `vxil plan` print. `ok:false` + `deferred:true` = not converged for an
|
|
292
|
+
* INDEFINITE reason — the feature worker has not seen the config yet, or a
|
|
293
|
+
* transient failure (a push asks again through POST /v1/apply after its
|
|
294
|
+
* wait); `ok:false` otherwise = a DEFINITE, named refusal (nothing to retry);
|
|
295
|
+
* `note` = converged nothing on purpose (a disabled feature). */
|
|
296
|
+
export interface ApiStateResponseField extends ApiStateSummary {
|
|
297
|
+
ok: boolean;
|
|
298
|
+
dry_run?: boolean;
|
|
299
|
+
deferred?: boolean;
|
|
300
|
+
error?: string;
|
|
301
|
+
note?: string;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** The note every executor reports for a DISABLED feature whose block still
|
|
305
|
+
* declares rows: the declaration is kept in the manifest and nothing is
|
|
306
|
+
* converged (a parked feature is not a propagation window — review finding 2). */
|
|
307
|
+
export const API_STATE_DISABLED_NOTE = 'feature disabled — declaration kept, not converged';
|
|
308
|
+
|
|
309
|
+
/** `GET /v1/rate-limits/policies` is a 100-row HARD cap with no cursor. A list
|
|
310
|
+
* AT the cap is not a complete live set, so planning creates against it would
|
|
311
|
+
* re-POST every declared name the cap hid (review finding 5). The server's
|
|
312
|
+
* converge and the CLI's read-only plan refuse with this message instead. */
|
|
313
|
+
export function rlPolicyListCapError(liveCount: number): string | null {
|
|
314
|
+
if (liveCount < RL_POLICY_LIST_CAP) return null;
|
|
315
|
+
return `the policy list is at its ${RL_POLICY_LIST_CAP}-row hard cap (no cursor) — the live set may be incomplete; delete undeclared policies before converging`;
|
|
316
|
+
}
|
|
317
|
+
export const RL_POLICY_LIST_CAP = 100;
|
|
318
|
+
|
|
319
|
+
/** `+2 created · ~1 updated · =3 unchanged · 1 undeclared (left in place)` —
|
|
320
|
+
* the one-line summary `vxil push` prints and the apply journal notes. */
|
|
321
|
+
export function formatApiStateSummary(s: ApiStateSummary): string {
|
|
322
|
+
const parts: string[] = [];
|
|
323
|
+
if (s.created.length) parts.push(`+${s.created.length} created`);
|
|
324
|
+
if (s.updated.length) parts.push(`~${s.updated.length} updated`);
|
|
325
|
+
if (s.recreated.length) parts.push(`↻${s.recreated.length} recreated`);
|
|
326
|
+
parts.push(`=${s.unchanged.length} unchanged`);
|
|
327
|
+
if (s.deleted.length) parts.push(`-${s.deleted.length} deleted`);
|
|
328
|
+
if (s.undeclared.length) parts.push(`${s.undeclared.length} undeclared (left in place)`);
|
|
329
|
+
if (s.pending_destructive.length) parts.push(`${s.pending_destructive.length} destructive-shaped NOT applied (--allow-destructive)`);
|
|
330
|
+
if (s.ambiguous.length) parts.push(`${s.ambiguous.length} ambiguous (duplicate live rows — not touched)`);
|
|
331
|
+
if (s.failed.length) parts.push(`${s.failed.length} failed`);
|
|
332
|
+
return parts.join(' · ');
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** The multi-line detail under the summary: one line per named row. */
|
|
336
|
+
export function formatApiStateDetail(s: ApiStateSummary): string[] {
|
|
337
|
+
const lines: string[] = [];
|
|
338
|
+
for (const k of s.created) lines.push(` + ${k}`);
|
|
339
|
+
for (const k of s.updated) lines.push(` ~ ${k}`);
|
|
340
|
+
for (const k of s.recreated) lines.push(` ↻ ${k} (recreated — new id)`);
|
|
341
|
+
for (const k of s.deleted) lines.push(` - ${k} (deleted: undeclared, --allow-destructive)`);
|
|
342
|
+
for (const k of s.undeclared) lines.push(` · ${k} — live but not declared (left in place; --allow-destructive deletes it)`);
|
|
343
|
+
for (const k of s.pending_destructive) lines.push(` ! ${k} — needs delete+recreate (new id); re-run with --allow-destructive`);
|
|
344
|
+
for (const k of s.ambiguous) lines.push(` ! ${k} — more than one live row carries this name; delete the extras first`);
|
|
345
|
+
for (const f of s.failed) lines.push(` ✗ ${f.key} — ${f.error}`);
|
|
346
|
+
return lines;
|
|
347
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** JSON.stringify with object keys sorted at every depth (arrays keep their
|
|
2
|
+
* order; undefined-valued keys are dropped, as JSON.stringify drops them).
|
|
3
|
+
* Two values with equal output are equal in CONTENT. A stored manifest comes
|
|
4
|
+
* back from storage with its object keys re-ordered, so every declared-vs-
|
|
5
|
+
* stored config compare (the server's shallowDiff, the CLI's diffManifest)
|
|
6
|
+
* must go through this, never through plain JSON.stringify. */
|
|
7
|
+
export function canonicalJson(v: unknown): string | undefined {
|
|
8
|
+
if (Array.isArray(v)) return `[${v.map((x) => canonicalJson(x) ?? 'null').join(',')}]`;
|
|
9
|
+
if (v !== null && typeof v === 'object') {
|
|
10
|
+
const o = v as Record<string, unknown>;
|
|
11
|
+
const keys = Object.keys(o).filter((k) => o[k] !== undefined).sort();
|
|
12
|
+
return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalJson(o[k])}`).join(',')}}`;
|
|
13
|
+
}
|
|
14
|
+
return JSON.stringify(v);
|
|
15
|
+
}
|