Repository navigation
Expand file tree
/
Copy pathschema.ts
More file actions
403 lines (378 loc) · 15.9 KB
/
Copy pathschema.ts
File metadata and controls
403 lines (378 loc) · 15.9 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
* Playproof milestone-contract schema — the game-agnostic progression language.
*
* A check is a statement about a game STATE. A state/save/frame path or a log
* event states it in the open; a hash states the same kind of thing opaquely,
* naming one exact state without saying which. Both are earnable by playing.
* Prefer a path or an event, because a reader can then judge what the contract
* asks for.
*/
import { createHash } from 'node:crypto'
export type EvidenceTier = 'engine-state' | 'save-file' | 'log-event' | 'screen-frame'
export type GlitchLegality = 'legal' | 'forbidden' | 'unclassified'
export type NumericOperator = '>=' | '>' | '=='
export type MilestoneCheck =
| { kind: 'state-path'; path: string; op: NumericOperator; value: number }
| { kind: 'save-path'; path: string; op: NumericOperator; value: number }
| { kind: 'save-hash'; hash: string }
| { kind: 'log-contains'; event: string }
| { kind: 'frame-path'; path: string; op: NumericOperator; value: number }
| { kind: 'frame-hash'; hash: string }
export interface Milestone {
id: string
tier: EvidenceTier
/** Milestones this one depends on (partial order over the contract). */
requires: string[]
check: MilestoneCheck
glitchClass: GlitchLegality
}
export interface MilestoneContract {
schemaVersion: 1
gameId: string
milestones: Milestone[]
}
const TIER_FOR_CHECK: Record<MilestoneCheck['kind'], EvidenceTier> = {
'state-path': 'engine-state',
'save-path': 'save-file',
'save-hash': 'save-file',
'log-contains': 'log-event',
'frame-path': 'screen-frame',
'frame-hash': 'screen-frame',
}
/** Pure validator: returns every violation; empty array means the contract is sound. */
export function validateContract(c: MilestoneContract): string[] {
const v: string[] = []
if (c.schemaVersion !== 1) v.push(`schemaVersion must be 1, got ${c.schemaVersion}`)
const ids = new Set<string>()
for (const m of c.milestones) {
if (ids.has(m.id)) v.push(`duplicate milestone id ${m.id}`)
ids.add(m.id)
if (TIER_FOR_CHECK[m.check.kind] !== m.tier) {
v.push(`${m.id}: check kind ${m.check.kind} does not match tier ${m.tier}`)
}
if (m.glitchClass === 'unclassified') v.push(`${m.id}: glitch legality must be classified (legal|forbidden)`)
if (m.glitchClass !== 'legal' && m.glitchClass !== 'forbidden') {
v.push(`${m.id}: unknown glitchClass ${String(m.glitchClass)}`)
}
}
for (const m of c.milestones) {
for (const r of m.requires) {
if (!ids.has(r)) v.push(`${m.id} requires unknown milestone ${r}`)
}
}
if (c.milestones.length === 0) v.push('contract has no milestones')
const declared = new Set<string>()
for (const m of c.milestones) {
for (const r of m.requires) {
if (!declared.has(r)) v.push(`${m.id} requires ${r}, which is declared later — milestones must be listed in dependency order`)
}
declared.add(m.id)
}
const WHITE = 0, GREY = 1, BLACK = 2
const color = new Map<string, number>()
const hasCycle = (id: string, path: string[]): boolean => {
const c0 = color.get(id) ?? WHITE
if (c0 === GREY) { v.push(`milestone graph has a cycle: ${[...path, id].join(' -> ')}`); return true }
if (c0 === BLACK) return false
color.set(id, GREY)
const m = c.milestones.find((x) => x.id === id)
for (const r of m?.requires ?? []) {
if (hasCycle(r, [...path, id])) return true
}
color.set(id, BLACK)
return false
}
for (const m of c.milestones) {
if (hasCycle(m.id, [])) break
}
return v
}
/** Stable JSON field order so hashes are reproducible across builds. */
export function canonicalContractJson(c: MilestoneContract): string {
const canon = (m: Milestone): unknown => ({
id: m.id,
tier: m.tier,
requires: [...m.requires].sort(),
check: m.check,
glitchClass: m.glitchClass,
})
return JSON.stringify({ schemaVersion: c.schemaVersion, gameId: c.gameId, milestones: [...c.milestones].map(canon) })
}
export function contractHash(c: MilestoneContract): string {
return createHash('sha256').update(canonicalContractJson(c)).digest('hex')
}
/**
* How a check states its requirement.
*
* `legible` — a threshold, a normalized field, or an event. A reader sees what
* the milestone demands and can judge whether reaching it is progress.
*
* `opaque` — a hash. It demands one exact game state, and a reader cannot tell
* which state, nor how many trajectories reach it. It is an achievement like
* any other: a hash identifies a STATE, not a trajectory, so an independent
* policy that reaches that state earns it without ever seeing the reference.
* Measured on ALE Breakout: of 96 single-input substitutions of the reference
* over the 32-turn prefix, 40 still reproduced both pinned hashes, at 16 of
* the 32 turns; all 16 applied at once reproduced them too.
*
* Do not confuse an opaque check with replay attestation. The input-log hash
* chain is what proves a replay reproduced a recorded run. A milestone hash
* proves only that some run stood in one state.
*/
export type CheckLegibility = 'legible' | 'opaque'
const LEGIBILITY_FOR_CHECK: Record<MilestoneCheck['kind'], CheckLegibility> = {
'state-path': 'legible',
'save-path': 'legible',
'save-hash': 'opaque',
'log-contains': 'legible',
'frame-path': 'legible',
'frame-hash': 'opaque',
}
/**
* Whether a reader can see what a check demands, derived from its kind.
*
* The value is derived rather than declared so that an existing contract keeps
* its bytes and its hash, and so that an author cannot forget to set it. A
* hash check is opaque whatever the author intended.
*/
export function checkLegibility(check: MilestoneCheck): CheckLegibility {
return LEGIBILITY_FOR_CHECK[check.kind]
}
/** Which milestones of a contract state their requirement in the open. */
export interface ContractLegibility {
/** Milestone ids whose requirement a reader can read off the contract. */
legible: string[]
/** Milestone ids whose requirement is a hash, or is gated behind one. */
opaque: string[]
/** Why each opaque milestone cannot be read, keyed by milestone id. */
reasons: Record<string, string>
}
/**
* Split a contract into the milestones a reader can understand and the ones
* stated as a hash.
*
* Opacity propagates through `requires`: `MilestoneTracker` admits a milestone
* only after every prerequisite passed, so a legible check gated behind a hash
* still demands something a reader cannot see. A missing or cyclic requirement
* is not judged here — `validateContract` reports those.
*/
export function contractLegibility(contract: MilestoneContract): ContractLegibility {
const byId = new Map(contract.milestones.map((m) => [m.id, m]))
const decided = new Map<string, string | null>()
const visiting = new Set<string>()
const reasonFor = (m: Milestone): string | null => {
const cached = decided.get(m.id)
if (cached !== undefined) return cached
if (visiting.has(m.id)) return null
visiting.add(m.id)
let reason: string | null = null
if (checkLegibility(m.check) === 'opaque') {
reason = `its ${m.check.kind} check states its requirement as a hash, so a reader cannot see what it demands`
} else {
for (const required of m.requires) {
const prerequisite = byId.get(required)
if (prerequisite !== undefined && reasonFor(prerequisite) !== null) {
reason = `requires ${required}, whose requirement is opaque`
break
}
}
}
visiting.delete(m.id)
decided.set(m.id, reason)
return reason
}
const legible: string[] = []
const opaque: string[] = []
const reasons: Record<string, string> = {}
for (const m of contract.milestones) {
const reason = reasonFor(m)
if (reason === null) legible.push(m.id)
else {
opaque.push(m.id)
reasons[m.id] = reason
}
}
return { legible, opaque, reasons }
}
/** A run's progress against a contract. Every milestone counts, hashes included. */
export interface MilestoneScore {
/** Milestones the run verified. */
verified: number
/** Milestones of the contract. */
total: number
}
/** Score a verified milestone set against its contract. */
export function scoreMilestones(contract: MilestoneContract, verified: readonly string[]): MilestoneScore {
return { verified: verified.length, total: contract.milestones.length }
}
/** One line for a report or a log. */
export function formatMilestoneScore(score: MilestoneScore): string {
return `${score.verified} of ${score.total}`
}
/**
* What a milestone says about the run that earned it.
*
* `achievement` — the run made something happen. A score threshold, a level
* reached, a state a player has to play towards.
*
* `attrition` — the run let a resource run down. Lives, health, shields, time
* remaining. Such a milestone marks progress REACHED and never competence
* shown, because the shortest path to it is to play badly. Measured on ALE
* Breakout: a `life-lost` milestone is `lives == 4`, so a program that never
* dies cannot score it, and two hand-written controls that differ only in how
* well they steer ranked in the wrong order because of it. The packaged
* Breakout contract states no such milestone for that reason.
*
* Recording an attrition milestone is legitimate — "the reference got far
* enough to lose a life" is a real fact about a trajectory. Scoring it as
* competence is not, which is why `scoreAchievements` exists next to
* `scoreMilestones`.
*/
export type ProgressionKind = 'achievement' | 'attrition'
/**
* How one numeric evidence channel moved across the measured trajectories.
*
* A channel is one key of one evidence map, named `engineState.lives` or
* `frameState.activeCells`. The counts are over every step of every measured
* trajectory, so `rises` 0 with `falls` above 0 is the measured statement
* "this channel is a resource that only runs down".
*/
export interface ChannelMotion {
/** `<map>.<key>`, for example `engineState.lives`. */
channel: string
/** Value at the initial state, or null when the trajectories disagreed. */
initial: number | null
/** Steps where the value was greater than the step before. */
rises: number
/** Steps where the value was less than the step before. */
falls: number
min: number
max: number
/** Trajectories that published the channel at least once. */
trajectories: number
/** Snapshots the counts are over. */
samples: number
}
/**
* Every milestone of one contract, split by what earning it demonstrates.
*
* The split is MEASURED, never declared in the contract, for the same reason
* legibility is derived: an existing contract keeps its bytes and its hash, and
* an author cannot forget to set it. `measureProgressions` in calibration.ts
* produces this from the motion of the channels the engine already publishes.
* Nothing here reads a field name, so an adapter that spells its life counter
* `shields` or `hull` is classified on the same evidence as one that spells it
* `lives`.
*/
export interface ProgressionProfile {
gameId: string
/** Every milestone of the contract, classified. */
kinds: Record<string, ProgressionKind>
/** Milestones that demonstrate something, in contract order. */
achievement: string[]
/** Milestones earned by a resource going down, in contract order. */
attrition: string[]
/**
* Milestones no numeric channel could speak for — a hash, a log event, or a
* path the measured trajectories never published. They count as achievements,
* because attrition is a positive finding and this is the absence of one.
* The list exists so a reader sees the limit of the measurement.
*/
unmeasured: string[]
/** Why each milestone is classified as it is, keyed by milestone id. */
reasons: Record<string, string>
/** Every channel the measurement watched, in first-seen order. */
motion: ChannelMotion[]
}
function assertProfileMatches(contract: MilestoneContract, profile: ProgressionProfile): void {
if (profile.gameId !== contract.gameId) {
throw new Error(`progression profile is for "${profile.gameId}" but the contract is for "${contract.gameId}"`)
}
const missing = contract.milestones.filter((m) => profile.kinds[m.id] === undefined).map((m) => m.id)
if (missing.length > 0) {
throw new Error(`progression profile does not classify ${missing.join(', ')} — re-measure it against this contract`)
}
}
/**
* Score a run over the achievement milestones alone.
*
* Both the numerator and the denominator drop the attrition milestones, so two
* runs compared on this number are compared on what they made happen. A run
* that also lost a life is neither rewarded nor punished for it. Read
* `scoreMilestones` for the whole contract when the question is how far the run
* got rather than how well it played.
*/
export function scoreAchievements(
contract: MilestoneContract,
profile: ProgressionProfile,
verified: readonly string[],
): MilestoneScore {
assertProfileMatches(contract, profile)
const achievement = new Set(contract.milestones.map((m) => m.id).filter((id) => profile.kinds[id] === 'achievement'))
return { verified: verified.filter((id) => achievement.has(id)).length, total: achievement.size }
}
/**
* How much of a contract hangs off one milestone.
*
* `requires` is a partial order, so a contract can state five progressions and
* still demand exactly one event: if every other milestone requires the first,
* a run that misses the first scores zero however well it played. That is one
* bit of resolution wearing five milestones. Measured on the packaged
* contracts: stable-retro Airstriker gates 5 of 5 milestones behind its
* `score-opened`. The packaged ALE Breakout contract gated 6 of 6 behind its
* own and now chains nothing: its checks read a counter that never falls, so a
* `requires` edge would restate the check and report a collapse that a
* seven-rung ladder does not have.
*
* This structure says nothing about how hard the prerequisite is. Whether a
* trivial baseline earns it is a measurement, and `calibrateContract` reports
* that as `collapse`.
*/
export interface ContractGate {
/** The milestone the largest share of the contract requires, transitively. */
prerequisite: string | null
/** Milestones that require it, transitively, counting the prerequisite itself. */
gated: number
/** Those milestone ids, in contract order. */
gatedMilestones: string[]
total: number
}
/**
* The milestone the largest share of a contract depends on.
*
* Ties are broken by contract order, so the answer is stable. A contract where
* no milestone requires another has no prerequisite and `gated` 0: nothing is
* chained, so nothing can collapse. A missing or cyclic requirement is not
* judged here — `validateContract` reports those, and the walk below stops on a
* cycle instead of looping.
*/
export function contractGate(contract: MilestoneContract): ContractGate {
const total = contract.milestones.length
const byId = new Map(contract.milestones.map((m) => [m.id, m]))
const closure = new Map<string, Set<string>>()
const requiredBy = (id: string, seen: Set<string>): Set<string> => {
const cached = closure.get(id)
if (cached !== undefined) return cached
if (seen.has(id)) return new Set()
seen.add(id)
const all = new Set<string>()
for (const required of byId.get(id)?.requires ?? []) {
if (!byId.has(required)) continue
all.add(required)
for (const deeper of requiredBy(required, seen)) all.add(deeper)
}
seen.delete(id)
closure.set(id, all)
return all
}
let best: ContractGate = { prerequisite: null, gated: 0, gatedMilestones: [], total }
for (const candidate of contract.milestones) {
const dependents = contract.milestones
.filter((m) => m.id === candidate.id || requiredBy(m.id, new Set()).has(candidate.id))
.map((m) => m.id)
if (dependents.length > best.gated && dependents.length > 1) {
best = { prerequisite: candidate.id, gated: dependents.length, gatedMilestones: dependents, total }
}
}
return best
}