-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.ts
More file actions
266 lines (241 loc) · 8.37 KB
/
migrate.ts
File metadata and controls
266 lines (241 loc) · 8.37 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
// .klickd v3.x → v4 GA payload migrator (TypeScript)
// SPDX-License-Identifier: CC0-1.0
//
// Implements the R4-P0-5 normative migrator contract documented at
// docs/spec/MIGRATION_V3_TO_V4.md. Cross-impl parity with
// packages/pypi/klickd/src/klickd/migrate.py.
//
// Pure / non-destructive / idempotent. Operates on the decrypted
// payload only — the encrypted wire envelope is left untouched.
import { KlickdError, HTTP_STATUS } from './errors.js';
const V3_SCHEMA_VERSIONS = new Set<string>([
'3.0',
'3.1',
'3.2',
'3.3',
'3.4',
'3.5',
]);
const V4_SCHEMA_VERSIONS = new Set<string>(['4.0', '4.0.0-preview.1']);
// Reserved profile_kind discriminator values per
// schemas/klickd-payload-v4.schema.json#/properties/profile_kind.
const RESERVED_PROFILE_KINDS = new Set<string>([
'learner',
'agent',
'team',
'robot',
'creator',
]);
export interface MigrateOptions {
/** Override the recorded source_version. Defaults to the input's
* payload_schema_version, or "3.x" when absent. */
sourceVersion?: string;
/** RFC 3339 UTC timestamp (must end with `Z`). Defaults to now().
* Tests SHOULD pin this for reproducibility. */
migratedAt?: string;
/** Default "learner" (v3.x is implicitly "learner"). */
profileKind?: string;
/** Optional pointer (URI / path) to a human-authored migration report. */
migrationReportRef?: string;
/** Optional pointer to a backup of the pre-migration file. */
backupRef?: string;
}
export interface MigrationWarning {
/** JSON-pointer-ish path; `<root>` for the top-level object. */
path: string;
/** Human-readable warning message. */
message: string;
}
type JsonObject = Record<string, unknown>;
function isPlainObject(value: unknown): value is JsonObject {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function utcNowIso(): string {
// Trim millisecond precision to match the v4 GA schema pattern
// ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$ (millis allowed but
// not required; we omit them for stable, comparable timestamps).
const now = new Date();
const pad = (n: number) => String(n).padStart(2, '0');
return (
`${now.getUTCFullYear()}-${pad(now.getUTCMonth() + 1)}-${pad(now.getUTCDate())}` +
`T${pad(now.getUTCHours())}:${pad(now.getUTCMinutes())}:${pad(now.getUTCSeconds())}Z`
);
}
function deepClone<T>(value: T): T {
// structuredClone is available on Node >=17 and all modern browsers;
// the @klickd/core package targets Node >=18 (see package.json).
return structuredClone(value);
}
/**
* Return true iff `payload` is a v3.x payload that should be lifted to v4 GA.
*
* Mirrors Python `needs_migration`.
*/
export function needsMigration(payload: unknown): boolean {
if (!isPlainObject(payload)) return false;
const ver = payload['payload_schema_version'];
if (ver === undefined || ver === null) return true;
if (typeof ver !== 'string') return false;
if (V4_SCHEMA_VERSIONS.has(ver)) return false;
if (V3_SCHEMA_VERSIONS.has(ver)) return true;
return false;
}
/**
* Lift a v3.x .klickd payload to the v4 GA payload shape.
*
* Pure: the input is never mutated; a structurally cloned result is
* returned. Idempotent on already-v4 inputs (unchanged unless the caller
* passes pointer refs).
*
* Throws `KlickdError(KLICKD_E_SCHEMA)` when the input is not a plain
* object or carries an unrecognized `payload_schema_version`.
*/
export function migratePayload(
payload: unknown,
options: MigrateOptions = {},
): JsonObject {
if (!isPlainObject(payload)) {
throw new KlickdError(
'KLICKD_E_SCHEMA',
`migratePayload requires a plain object payload; got ${typeof payload}`,
HTTP_STATUS['KLICKD_E_SCHEMA'],
);
}
const incomingVersion = payload['payload_schema_version'];
if (
incomingVersion !== undefined &&
typeof incomingVersion === 'string' &&
!V3_SCHEMA_VERSIONS.has(incomingVersion) &&
!V4_SCHEMA_VERSIONS.has(incomingVersion)
) {
throw new KlickdError(
'KLICKD_E_SCHEMA',
`migratePayload does not recognize payload_schema_version=` +
`${JSON.stringify(incomingVersion)}; expected v3.x (3.0..3.5) or ` +
`v4 (4.0 / 4.0.0-preview.1)`,
HTTP_STATUS['KLICKD_E_SCHEMA'],
);
}
const out = deepClone(payload) as JsonObject;
const {
sourceVersion,
migratedAt,
profileKind = 'learner',
migrationReportRef,
backupRef,
} = options;
// R8 — already-v4 payloads round-trip unchanged unless pointer refs
// are supplied (which we splice into the migration block).
if (typeof incomingVersion === 'string' && V4_SCHEMA_VERSIONS.has(incomingVersion)) {
if (migrationReportRef === undefined && backupRef === undefined) {
return out;
}
const existing = isPlainObject(out['migration']) ? (out['migration'] as JsonObject) : {};
if (migrationReportRef !== undefined) {
existing['migration_report_ref'] = migrationReportRef;
}
if (backupRef !== undefined) {
existing['backup_ref'] = backupRef;
}
if (existing['source_version'] === undefined) {
existing['source_version'] = incomingVersion;
}
existing['migrated_at'] = migratedAt ?? utcNowIso();
out['migration'] = existing;
return out;
}
// R1 — stamp the payload version to the GA canonical value.
out['payload_schema_version'] = '4.0';
// R2 — default profile_kind when absent.
if (out['profile_kind'] === undefined) {
out['profile_kind'] = profileKind;
}
// R4 — record the migration provenance block.
const migrationBlock: JsonObject = {
source_version:
sourceVersion ??
(typeof incomingVersion === 'string' ? incomingVersion : '3.x'),
migrated_at: migratedAt ?? utcNowIso(),
};
if (migrationReportRef !== undefined) {
migrationBlock['migration_report_ref'] = migrationReportRef;
}
if (backupRef !== undefined) {
migrationBlock['backup_ref'] = backupRef;
}
out['migration'] = migrationBlock;
return out;
}
/**
* Return manual-review warnings without mutating `payload`. Mirrors
* Python `migrate_payload_iter_warnings`.
*/
export function migratePayloadIterWarnings(payload: unknown): MigrationWarning[] {
const warnings: MigrationWarning[] = [];
if (!isPlainObject(payload)) {
warnings.push({ path: '<root>', message: 'payload is not a JSON object' });
return warnings;
}
const ver = payload['payload_schema_version'];
if (ver === undefined) {
if (payload['domain_schema_version'] === undefined) {
warnings.push({
path: '<root>',
message:
'no payload_schema_version and no domain_schema_version; ' +
'pin sourceVersion explicitly when migrating',
});
}
} else if (
typeof ver === 'string' &&
!V3_SCHEMA_VERSIONS.has(ver) &&
!V4_SCHEMA_VERSIONS.has(ver)
) {
warnings.push({
path: '/payload_schema_version',
message: `unknown payload_schema_version ${JSON.stringify(ver)}; migrator will refuse`,
});
}
const ctx = payload['context'];
if (isPlainObject(ctx)) {
const decisions = ctx['decisions_locked'];
if (Array.isArray(decisions)) {
decisions.forEach((d, i) => {
if (typeof d === 'string' && d.length > 1024) {
warnings.push({
path: `/context/decisions_locked/${i}`,
message: `entry exceeds 1024 chars (${d.length}); some readers will truncate`,
});
}
});
}
}
const ethics = payload['ethics'];
const veto = payload['human_veto_policy'];
if (isPlainObject(ethics) && isPlainObject(veto)) {
const locked = ethics['locked_actions'];
const appliesTo = veto['applies_to'];
if (Array.isArray(locked) && Array.isArray(appliesTo)) {
const lockedSet = new Set<string>(locked.filter((x): x is string => typeof x === 'string'));
const overlap = appliesTo
.filter((x): x is string => typeof x === 'string' && lockedSet.has(x))
.sort();
if (overlap.length > 0) {
warnings.push({
path: '/human_veto_policy/applies_to',
message:
'overlaps with /ethics/locked_actions: ' +
overlap.map((x) => JSON.stringify(x)).join(', '),
});
}
}
}
const pk = payload['profile_kind'];
if (typeof pk === 'string' && !RESERVED_PROFILE_KINDS.has(pk)) {
warnings.push({
path: '/profile_kind',
message: `non-reserved profile_kind ${JSON.stringify(pk)}; readers MAY treat as extension`,
});
}
return warnings;
}