Skip to content

Commit e606bab

Browse files
fix(spec): dropped-refinements census walks every reachable node instead of stopping at depth 14 (#38115)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} --> _Requested by **Elon** · [project thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DDypKu7B9nwbu5WuV7AEio8)_ Fixes #34442 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户无感:构建时的"被丢弃规则"清点改为走遍每一层,发布的三个已安装包结构文件多列出了它们原先漏报的校验规则。 - 属于规格工具链的完整性收口,替所有依赖已发布结构文件的作者与 AI 解决"清单说齐了其实没齐"的问题。 - 出错时构建会直接报错并点名哪个结构、哪条路径,不再悄悄截断;可整个撤回此 PR,不影响任何运行时行为。 - 无。 ## What changes The dropped-refinements census (`packages/spec/scripts/lib/dropped-refinements.ts`) stopped descending at `MAX_DEPTH = 14` and still printed "all declared". It now follows the card's Expected ruling verbatim: "The census either walks every reachable schema node, with cycles guarded by identity rather than by a depth cap, or fails loudly when it hits the cap, so that \"all declared\" is never a false complete." It does both: - The walk has no depth cap. The identity-keyed `visited` set it already carried is what terminates it; every node is queued at most once per export. - `RUNAWAY_WALK_DEPTH = 256` is not a cap but a refusal: a path that long means the identity guard failed (e.g. a `lazy` getter building a fresh node per call), and the census throws naming the export and path instead of returning a short list. - The proxy-unwrap loop in `identityOf` reused `MAX_DEPTH`; it now has its own `MAX_PROXY_HOPS` (same value, a prototype-chain bound, not a graph bound). ### Measured site counts (acceptance item 1), every published schema, `gen:schema`, base f72f335 vs this branch | Walk | dropped sites | schemas with drops | projected sites | undecidable | |:--|--:|--:|--:|--:| | depth 14 (base) | 1225 | 281 | 812 | 0 | | unbounded, identity-guarded | 1239 | 281 | 830 | 0 | +14 dropped (all in three installed-package envelopes, no new schema), +18 projected. Ledger rows added (acceptance item 2): - `api/InstalledPackageAtEitherStage` (+1): `options[1].manifest.translations.element.valueType.flows.valueType.records.valueType.fields.valueType` (the card's `flows.records.fields.valueType`). - `api/GetInstalledPackageResponse` (+5): `translations…flows.valueType.{notices.valueType.message, records.valueType.fields.valueType, refusals.valueType.message, screens.valueType.description, screens.valueType.title}` under `data.options[1].manifest`. - `api/ListInstalledPackagesResponse` (+8): the same five under `data.packages.element.options[1].manifest`, plus `aiContexts.element.related.element.where.lazy.lazy`, `views.element.list.emptyState.link.url`, `views.element.list.grouping.fields.element.field`. `measured.droppedRefinementSites` 1225 to 1239. A `patch` changeset covers the published `json-schema/api/*` annotation growth (`x-` keywords; no accepted document changes). ## Tests `scripts/dropped-refinements.test.ts`, new `describe('the walk is bounded by identity, never by depth')`: - a refinement 40 objects deep is reported at its full path; - lit control: the same leaf one level deep is one site; - a `lazy` getter returning a fresh node per call makes the census throw naming the export. Ablation: the base version of `dropped-refinements.ts` restored on disk turns the deep and runaway pins red (2 failed / 28 passed), control stays green; restored byte-identical (`git hash-object` a6f5f82 = HEAD blob). ## Acceptance notes - `refinementSitesThatDidProject` in the ledger header reads 728 while the build measures 812 at base and 830 now; no test reads that field. Left untouched (outside this card's file-surface rows). ## Verification Read first: `packages/spec/scripts/lib/dropped-refinements.ts` (the walk loop and `RUNAWAY_WALK_DEPTH`), then the ledger diff. Two-minute check: `pnpm --filter @objectstack/spec gen:schema` prints `1239 refinement site(s) across 281 published schema(s) ... all declared`, and `json-schema/api/InstalledPackageAtEitherStage.json` names `...records.valueType.fields.valueType` in `x-dropped-refinements`. <details><summary>Local readings (head e096f21)</summary> - `pnpm gen:schema`: exit 0, census line above. - `vitest run scripts/dropped-refinements.test.ts scripts/refinement-projection.test.ts scripts/published-projection-choke-point.test.ts`: 3 files, 124 tests passed. - `pnpm --filter @objectstack/spec test`: 909 files passed / 1 skipped, 28814 tests passed. - `pnpm --filter @objectstack/spec typecheck`: exit 0. - Derived gate families (`dispatch-gates --commands`): 69 run at e096f21, `--ran` reconciliation green; 64 exit 0; 5 exit 3 PREREQUISITE NOT MET, read as NOT MEASURED (`check:dts-closure`, `check:dual-build-cjs-loads`, `check:lean-entry-closure`, `check:sourcemap-no-sources-content`: no workspace `dist/`; `check-plugin-teardown-shape.mjs --self-test`: shallow clone). CI runs them on this PR. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_016WV4cxRDKprCtjpEdtiPFW)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5b8ca36 commit e606bab

4 files changed

Lines changed: 90 additions & 5 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/spec': patch
3+
---
4+
5+
fix(spec): the published JSON Schema names every refinement it drops, however deeply it is nested
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
The generator's dropped-refinements census stopped walking a schema after 14 hops and still printed "all declared". A rule nested deeper was missing from the file's `x-dropped-refinements` annotation. The walk now has no depth limit; its identity-keyed visited set ends it. A walk that does not end now fails the build with the export and path named, instead of stopping early in silence.
11+
12+
Three published files under `json-schema/api/` now list sites they missed before: `InstalledPackageAtEitherStage` (1 more), `GetInstalledPackageResponse` (5 more) and `ListInstalledPackagesResponse` (8 more). The set of documents any schema accepts is unchanged, because `x-` keywords are ignored by every validator. Nothing to migrate.

‎packages/spec/dropped-refinements.baseline.json‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"measured": {
44
"zod": "4.4.3",
55
"publishedSchemasWithDroppedRefinements": 281,
6-
"droppedRefinementSites": 1225,
6+
"droppedRefinementSites": 1239,
77
"refinementSitesThatDidProject": 728,
88
"refinementSitesWithNoJsonFormToCompare": 0
99
},
@@ -421,6 +421,11 @@
421421
"data.options[1].manifest.skills.element.triggerConditions.element",
422422
"data.options[1].manifest.translations.element",
423423
"data.options[1].manifest.translations.element.valueType.flows.valueType.errorMessage",
424+
"data.options[1].manifest.translations.element.valueType.flows.valueType.notices.valueType.message",
425+
"data.options[1].manifest.translations.element.valueType.flows.valueType.records.valueType.fields.valueType",
426+
"data.options[1].manifest.translations.element.valueType.flows.valueType.refusals.valueType.message",
427+
"data.options[1].manifest.translations.element.valueType.flows.valueType.screens.valueType.description",
428+
"data.options[1].manifest.translations.element.valueType.flows.valueType.screens.valueType.title",
424429
"data.options[1].manifest.translations.element.valueType.flows.valueType.successMessage",
425430
"data.options[1].manifest.views.element.form",
426431
"data.options[1].manifest.views.element.list",
@@ -601,6 +606,7 @@
601606
"options[1].manifest.translations.element",
602607
"options[1].manifest.translations.element.valueType.flows.valueType.errorMessage",
603608
"options[1].manifest.translations.element.valueType.flows.valueType.notices.valueType.message",
609+
"options[1].manifest.translations.element.valueType.flows.valueType.records.valueType.fields.valueType",
604610
"options[1].manifest.translations.element.valueType.flows.valueType.refusals.valueType.message",
605611
"options[1].manifest.translations.element.valueType.flows.valueType.screens.valueType.description",
606612
"options[1].manifest.translations.element.valueType.flows.valueType.screens.valueType.title",
@@ -640,6 +646,7 @@
640646
"data.packages.element.options[1].manifest.aiContexts.element.related",
641647
"data.packages.element.options[1].manifest.aiContexts.element.related.element.orderBy",
642648
"data.packages.element.options[1].manifest.aiContexts.element.related.element.where.lazy",
649+
"data.packages.element.options[1].manifest.aiContexts.element.related.element.where.lazy.lazy",
643650
"data.packages.element.options[1].manifest.analyticsCubes.element.measures.valueType",
644651
"data.packages.element.options[1].manifest.apps.element",
645652
"data.packages.element.options[1].manifest.assignmentRules.element",
@@ -706,13 +713,20 @@
706713
"data.packages.element.options[1].manifest.skills.element.triggerConditions.element",
707714
"data.packages.element.options[1].manifest.translations.element",
708715
"data.packages.element.options[1].manifest.translations.element.valueType.flows.valueType.errorMessage",
716+
"data.packages.element.options[1].manifest.translations.element.valueType.flows.valueType.notices.valueType.message",
717+
"data.packages.element.options[1].manifest.translations.element.valueType.flows.valueType.records.valueType.fields.valueType",
718+
"data.packages.element.options[1].manifest.translations.element.valueType.flows.valueType.refusals.valueType.message",
719+
"data.packages.element.options[1].manifest.translations.element.valueType.flows.valueType.screens.valueType.description",
720+
"data.packages.element.options[1].manifest.translations.element.valueType.flows.valueType.screens.valueType.title",
709721
"data.packages.element.options[1].manifest.translations.element.valueType.flows.valueType.successMessage",
710722
"data.packages.element.options[1].manifest.views.element.form",
711723
"data.packages.element.options[1].manifest.views.element.list",
712724
"data.packages.element.options[1].manifest.views.element.list.bulkActionDefs.element",
725+
"data.packages.element.options[1].manifest.views.element.list.emptyState.link.url",
713726
"data.packages.element.options[1].manifest.views.element.list.filter.element",
714727
"data.packages.element.options[1].manifest.views.element.list.filter.element.options[0]",
715728
"data.packages.element.options[1].manifest.views.element.list.gantt.groupByField",
729+
"data.packages.element.options[1].manifest.views.element.list.grouping.fields.element.field",
716730
"data.packages.element.options[1].manifest.views.element.list.kanban.groupByField",
717731
"data.packages.element.options[1].manifest.views.element.list.timeline.groupByField"
718732
]

‎packages/spec/scripts/dropped-refinements.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,35 @@ describe('the differential isolates the refinement, not the node', () => {
224224
});
225225
});
226226

227+
describe('the walk is bounded by identity, never by depth', () => {
228+
// Nest a refined leaf `levels` object hops below the export: a.a.a…a.leaf.
229+
const nested = (levels: number): z.ZodType => {
230+
let node: z.ZodType = z.object({ leaf: z.string().refine((v) => v !== 'x', 'not x') });
231+
for (let i = 0; i < levels; i += 1) node = z.object({ a: node });
232+
return node;
233+
};
234+
235+
it('reports a refinement nested FAR past the old 14-hop cap', () => {
236+
// 40 objects deep, each `shape` + key = two def hops, so the leaf sits ~80
237+
// hops down. The capped walk stopped at 14 and reported nothing here.
238+
const entry = collectDroppedRefinements('t/Deep', nested(40));
239+
expect(entry.dropped.map((s) => s.path)).toEqual([`${'a.'.repeat(40)}leaf`]);
240+
});
241+
242+
it('LIT CONTROL — the same leaf at a shallow depth is the same single site', () => {
243+
const entry = collectDroppedRefinements('t/Shallow', nested(1));
244+
expect(entry.dropped.map((s) => s.path)).toEqual(['a.leaf']);
245+
});
246+
247+
it('REFUSES a walk its identity guard cannot close, instead of truncating it', () => {
248+
// A `lazy` getter that builds a FRESH node per call: every hop is a new
249+
// identity, so nothing terminates the walk. The census must throw and name
250+
// the export, never return a short list that reads as complete.
251+
const fresh = (): z.ZodType => z.lazy(() => z.object({ next: fresh() }));
252+
expect(() => collectDroppedRefinements('t/Runaway', fresh())).toThrow(/t\/Runaway/);
253+
});
254+
});
255+
227256
describe('the ratchet adjudicates against the ledger', () => {
228257
const site = (path: string) => ({
229258
path,

‎packages/spec/scripts/lib/dropped-refinements.ts‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,27 @@ export const DROPPED_REFINEMENTS_BASELINE_FILE = 'dropped-refinements.baseline.j
109109
*/
110110
export { CUSTOM_CHECK_KIND };
111111

112-
/** How deep the graph walk goes before it stops descending. */
113-
const MAX_DEPTH = 14;
112+
/**
113+
* How many `lazySchema()` Proxy layers `identityOf` unwraps. A bound on a
114+
* prototype chain, not on the graph walk — the walk has no depth cap at all.
115+
*/
116+
const MAX_PROXY_HOPS = 14;
117+
118+
/**
119+
* The depth at which the walk stops trusting itself and THROWS.
120+
*
121+
* ⛔ Not a cap. The walk used to stop descending at 14 hops and say nothing, so
122+
* a refinement nested deeper held no ledger row while the census printed "all
123+
* declared" — measured on `api/InstalledPackageAtEitherStage`, whose
124+
* `flows.records.fields.valueType` leaf sits past hop 14 behind an `options[1]`
125+
* edge. Termination is the identity-keyed `visited` set's job: every node is
126+
* queued at most once per export, so a finite graph ends on its own however
127+
* deep it nests. A path this long means that guarantee failed — a `lazy`
128+
* getter building a FRESH node on every call, say — and the only honest answer
129+
* then is a refusal naming where it happened, never a truncated census that
130+
* reads as complete.
131+
*/
132+
export const RUNAWAY_WALK_DEPTH = 256;
114133

115134
/** One node that carries at least one refinement, and what became of it. */
116135
export interface RefinementSite {
@@ -464,7 +483,7 @@ function readablePath(raw: string): string {
464483
function identityOf(schema: z.ZodType): unknown {
465484
let internals = (schema as unknown as { _zod?: object })._zod;
466485
if (!internals || typeof internals !== 'object') return schema;
467-
for (let hop = 0; hop < MAX_DEPTH; hop += 1) {
486+
for (let hop = 0; hop < MAX_PROXY_HOPS; hop += 1) {
468487
const proto = Object.getPrototypeOf(internals) as object | null;
469488
if (!proto || proto === Object.prototype) break;
470489
if (!Object.prototype.hasOwnProperty.call(proto, 'def')) break;
@@ -476,6 +495,9 @@ function identityOf(schema: z.ZodType): unknown {
476495
/**
477496
* Walk one published schema and classify every refinement under it.
478497
*
498+
* The walk is UNBOUNDED in depth: the visited set is what terminates it (see
499+
* {@link RUNAWAY_WALK_DEPTH} for what happens when it does not).
500+
*
479501
* The visited set is per export and holds node IDENTITIES, so a shared
480502
* sub-schema is reported once per published file that reaches it — which is the
481503
* census question ("which published files does the gap land on"), not "how many
@@ -512,7 +534,15 @@ export function collectDroppedRefinements(defKey: string, root: z.ZodType): Refi
512534
else undecidable.push(site);
513535
}
514536

515-
if (depth >= MAX_DEPTH) continue;
537+
if (depth >= RUNAWAY_WALK_DEPTH) {
538+
throw new Error(
539+
`dropped-refinements census: the walk under ${defKey} reached depth ${depth} at ` +
540+
`"${readablePath(path) || '<root>'}" without its identity guard closing it. The census ` +
541+
`refuses rather than truncate: a truncated walk would print "all declared" over sites ` +
542+
`it never reached. A node reached this deep is almost certainly a \`lazy\` getter ` +
543+
`that builds a new schema instance on every call — return one cached instance instead.`,
544+
);
545+
}
516546
for (const child of labelledChildren(schema)) {
517547
if (visited.has(identityOf(child.schema))) continue;
518548
queue.push({

0 commit comments

Comments
 (0)