From 0e356e8cbce82352f439586857c21094dc087b09 Mon Sep 17 00:00:00 2001 From: Hanrim <148833226+MerciHanrim@users.noreply.github.com> Date: Tue, 24 Nov 2026 01:30:12 +0900 Subject: [PATCH] A refused Apply or structure insert says which node cannot be read, and why InvalidRevisionContentError carries its cause (a non-finite number and where, an unreadable model node with the reader's reason, a model node in a version 1 document, an unknown kind) and a message for it. validateResultGraph and the structure insert's backstop give that reason instead of the non-finite sentence for every cause. Part of #619. --- .changes/register-refusal-names-cause.json | 1 + CHANGELOG.md | 1 + src/model/invalidContentReason.test.ts | 73 ++++++++++++++++++++++ src/model/moduleGraph.ts | 6 +- src/model/revision.ts | 56 ++++++++++++++--- 5 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 .changes/register-refusal-names-cause.json create mode 100644 src/model/invalidContentReason.test.ts diff --git a/.changes/register-refusal-names-cause.json b/.changes/register-refusal-names-cause.json new file mode 100644 index 00000000..31c7a299 --- /dev/null +++ b/.changes/register-refusal-names-cause.json @@ -0,0 +1 @@ +{"type":"user-facing","changelog":"- **A refused Apply or structure insert says which node cannot be read, and why.** A hand-edited file can hold a Register whose expression does not parse or is not text, or a Parameter whose value is not a number. Applying chosen changes in Review, or inserting a saved structure, into such a document was refused, rightly, but always with A node or edge holds a non-finite number (NaN / Infinity). It now names the node and its problem, for example Node r (register) cannot be read: expr does not parse: \"1 +\". A genuine non-finite number keeps its sentence and now says where it is. The revision export and the data import keep their general messages."} diff --git a/CHANGELOG.md b/CHANGELOG.md index df0c9818..56f4ec9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ a release yet is under **Unreleased** ([`docs/release-notes.md`](docs/release-no ## Unreleased +- **A refused Apply or structure insert says which node cannot be read, and why.** A hand-edited file can hold a Register whose expression does not parse or is not text, or a Parameter whose value is not a number. Applying chosen changes in Review, or inserting a saved structure, into such a document was refused, rightly, but always with A node or edge holds a non-finite number (NaN / Infinity). It now names the node and its problem, for example Node r (register) cannot be read: expr does not parse: "1 +". A genuine non-finite number keeps its sentence and now says where it is. The revision export and the data import keep their general messages. - **Renaming keeps the run.** A name is not part of what a run computes, and renaming a node already left the run where it was. Renaming an imported Parameter, renaming a data table in Manage bindings, and undoing or redoing any rename, a plain Parameter's included, still sent the run back to step 0 and marked a finished Monte Carlo result out of date, which turned off its export. They now keep both. The rename is still one Undo step, is saved, and marks an open project revision as changed. Changing a value still resets the run, as before. - **A file with a repeated or missing group frame id opens the same way every time.** A hand-edited or damaged file whose saved frames repeated an id, or left one out, gave that frame a new id made from the clock each time it was opened. Two opens of one file gave the frame two different ids, so the document no longer matched itself, and a project revision whose header holds no content digest showed as changed the moment it opened. The replacement id is now made from the file itself: a repeat of x becomes x_2, x_3, and a missing id takes the frame's place in the file, never an id another frame of the file already has. Files Loop Studio writes, and run results, are unchanged. - **Refreshing a table after a column was removed no longer blocks other tables.** When Data ▸ Refresh removed a column from a table while a row missing from the new data was left undecided (neither unlinked nor deleted), that row kept its value for the removed column. Until the document was reopened, refreshing any other table of the document was then refused with This data could not be stored safely, however good its data. The undecided row now keeps only the values of the columns the table still has, so the other tables refresh as before. A document saved in that state needs nothing: the value was already dropped when it was opened. diff --git a/src/model/invalidContentReason.test.ts b/src/model/invalidContentReason.test.ts new file mode 100644 index 00000000..f227dcec --- /dev/null +++ b/src/model/invalidContentReason.test.ts @@ -0,0 +1,73 @@ +import { describe, expect, it } from 'vitest' +import { insertGraph } from './moduleGraph' +import { canonicalContent, InvalidRevisionContentError, validateResultGraph } from './revision' +import type { LoopNode } from './types' + +// #619 item 5 — the canonical projection knows why it refuses content: a +// non-finite number at a place, or a model node (Parameter / Register) that +// cannot be read, with the reader's own reason. The result check (selective +// Apply, the data import commit) and the structure insert dropped that and said +// "a non-finite number" for every cause. They now say the cause; a genuine +// non-finite number keeps its sentence and gains where it is. + +const pool = (id: string, initial: number) => + ({ id, type: 'pool', position: { x: 0, y: 0 }, data: { kind: 'pool', label: id, activation: 'passive', initial, capacity: null, mode: 'pullAny' } }) as LoopNode +const model = (id: string, kind: 'parameter' | 'register', data: Record) => + ({ id, type: kind, position: { x: 0, y: 100 }, data: { kind, label: id.toUpperCase(), ...data } }) as unknown as LoopNode + +const CASES: { name: string; node: LoopNode; reason: string; message: RegExp }[] = [ + { + name: 'a Register whose expr is not a string', + node: model('r', 'register', { expr: 42 }), + reason: 'Node r (register) cannot be read: expr is not a string.', + message: /^node r \(register\) cannot be read: expr is not a string$/, + }, + { + name: 'a Register whose expr does not parse', + node: model('r', 'register', { expr: '1 +' }), + reason: 'Node r (register) cannot be read: expr does not parse: "1 +".', + message: /^node r \(register\) cannot be read: expr does not parse: "1 \+"$/, + }, + { + name: 'a Parameter whose value is not a number', + node: model('p', 'parameter', { value: 'abc' }), + reason: 'Node p (parameter) cannot be read: value is not a number.', + message: /^node p \(parameter\) cannot be read: value is not a number$/, + }, + { + name: 'a genuine non-finite number (control: the same meaning, now with where)', + node: pool('inf', Number.POSITIVE_INFINITY), + reason: 'A node or edge holds a non-finite number (NaN / Infinity): node inf data.initial.', + message: /^non-finite number in revision content at node inf data\.initial$/, + }, +] + +describe('a refusal of content the projection cannot read says why (#619 item 5)', () => { + for (const c of CASES) { + it(`${c.name}: the error, the result check and the structure insert`, () => { + const nodes = [pool('a', 1), c.node] + let thrown: unknown + try { + canonicalContent({ nodes, edges: [] }) + } catch (e) { + thrown = e + } + expect(thrown).toBeInstanceOf(InvalidRevisionContentError) + expect.soft((thrown as Error).message).toMatch(c.message) + + const v = validateResultGraph(nodes, []) + expect.soft(v.ok ? [] : v.reasons).toEqual([c.reason]) + + const r = insertGraph({ nodes, edges: [], modelVersion: 2 }, { nodes: [pool('m', 1)], edges: [], modelVersion: 2 }, { at: { x: 500, y: 0 } }) + expect(r.ok ? null : r.reason).toBe(`The merged graph is not valid: ${c.reason}`) + }) + } + + it('the reason names no stack and no source file', () => { + for (const c of CASES) { + const v = validateResultGraph([pool('a', 1), c.node], []) + const text = v.ok ? '' : v.reasons.join(' ') + expect(text).not.toMatch(/\bat \w+ \(|\.ts\b|InvalidRevisionContentError|Error:/) + } + }) +}) diff --git a/src/model/moduleGraph.ts b/src/model/moduleGraph.ts index 1bab42a5..a93a5320 100644 --- a/src/model/moduleGraph.ts +++ b/src/model/moduleGraph.ts @@ -25,7 +25,7 @@ import { canonicalRef } from './expr/canonical' import { parseActivatorExpr } from '../engine/stateExpr' import type { ExprNode } from './expr' import { nextId } from './factory' -import { canonicalContent, validateResultGraph } from './revision' +import { canonicalContent, invalidContentReason, InvalidRevisionContentError, validateResultGraph } from './revision' import { minimumModelVersion, normalizeGraph } from './serialize' import type { ModelSemanticsVersion } from './serialize' import { kindMergeConflicts, kindsDeclareUnits, mergeKinds, type ResourceKindDecl } from './kinds' @@ -323,7 +323,9 @@ export function insertGraph(host: GraphDocLike, mod: GraphDocLike, opts: InsertO } try { canonicalContent({ nodes, edges, kinds, time, timetables }, { modelVersion }) - } catch { + } catch (e) { + // #619 item 5 — the projection's own cause, not one sentence for all + if (e instanceof InvalidRevisionContentError) return { ok: false, reason: `The merged graph is not valid: ${invalidContentReason(e)}` } return { ok: false, reason: 'The merged graph holds a non-finite number.' } } diff --git a/src/model/revision.ts b/src/model/revision.ts index bea5f6f3..cc484343 100644 --- a/src/model/revision.ts +++ b/src/model/revision.ts @@ -63,13 +63,54 @@ export class SecureRandomUnavailableError extends Error { * name it (its id, label and the kind it carries). */ export class InvalidRevisionContentError extends Error { readonly node?: { id: string; label: string; kind: string } - constructor(where: string, node?: { id: string; label: string; kind: string }) { - super(node ? `node ${node.id} has kind "${node.kind}", which is not a node kind` : `non-finite number in revision content at ${where}`) + /** #619 item 5 — why the content was refused. Its message, and the reason a + * refusal shows (`invalidContentReason`), say that cause; before, every cause + * but an unknown kind read "non-finite number". */ + readonly contentCause: InvalidRevisionContentCause + constructor(where: string, node?: { id: string; label: string; kind: string }, cause?: InvalidRevisionContentCause) { + const c: InvalidRevisionContentCause = node ? { kind: 'unknown-kind' } : (cause ?? { kind: 'non-finite', where }) + super( + c.kind === 'unknown-kind' ? `node ${node!.id} has kind "${node!.kind}", which is not a node kind` + : c.kind === 'unreadable-model-node' ? `node ${c.nodeId} (${c.nodeKind}) cannot be read: ${c.detail}` + : c.kind === 'model-node-in-v1' ? `node ${c.nodeId}: a ${c.nodeKind} node is not loop-revision/1 content` + : c.kind === 'invariant' ? c.detail + : `non-finite number in revision content at ${c.where}`, + ) this.name = 'InvalidRevisionContentError' + this.contentCause = c if (node) this.node = node } } +/** #619 item 5 — what made the canonical projection refuse content */ +export type InvalidRevisionContentCause = + | { kind: 'non-finite'; where: string } + | { kind: 'unreadable-model-node'; nodeId: string; nodeKind: string; detail: string } + | { kind: 'model-node-in-v1'; nodeId: string; nodeKind: string } + | { kind: 'unknown-kind' } + | { kind: 'invariant'; detail: string } + +/** #619 item 5 — the refusal reason for content the projection cannot read, as + * a result check or a structure insert shows it (model-layer English, like the + * other reasons): the node and the reader's own reason, or for a genuine + * non-finite number the long-standing sentence and where it is. No stack, no + * error class name. */ +export function invalidContentReason(e: InvalidRevisionContentError): string { + const c = e.contentCause + switch (c.kind) { + case 'non-finite': + return `A node or edge holds a non-finite number (NaN / Infinity): ${c.where}.` + case 'unreadable-model-node': + return `Node ${c.nodeId} (${c.nodeKind}) cannot be read: ${c.detail}.` + case 'model-node-in-v1': + return `Node ${c.nodeId} is a ${c.nodeKind} node, which a version 1 document cannot hold.` + case 'unknown-kind': + return `Node ${e.node!.id}: unknown kind "${e.node!.kind}".` + case 'invariant': + return 'The graph cannot be read as revision content.' + } +} + // ── wire types (§R1) ─────────────────────────────────────────────────────── export type ProjectRole = 'revision' | 'proposal' @@ -442,10 +483,10 @@ function projectNode(n: LoopNode, modelLayer: boolean): CanonicalNode { // `project`, the graph still loads — R-INV-10 / R2-INV-9). if (MODEL_NODE_KINDS.has(kind)) { if (!modelLayer) { - throw new InvalidRevisionContentError(`node ${n.id}: a ${kind} node is not loop-revision/1 content`) + throw new InvalidRevisionContentError(`node ${n.id}`, undefined, { kind: 'model-node-in-v1', nodeId: n.id, nodeKind: kind }) } const read = kind === 'parameter' ? readParameterData(rawData) : readRegisterData(rawData) - if (!read.ok) throw new InvalidRevisionContentError(`node ${n.id} (${kind}): ${read.detail}`) + if (!read.ok) throw new InvalidRevisionContentError(`node ${n.id}`, undefined, { kind: 'unreadable-model-node', nodeId: n.id, nodeKind: kind, detail: read.detail }) const nd = read.data as unknown as Record const data: Record = {} for (const f of MODEL_NODE_FIELDS[kind as 'parameter' | 'register']) { @@ -1052,7 +1093,7 @@ export function readRevisionSide( // 5 — lift into the common compare model; R2-INV-2 / R3-INV-2 say the bytes match const lifted = canonicalContent(graph, { modelLayer: true }) if (canonicalJson(lifted) !== canonicalJson(v1)) { - throw new InvalidRevisionContentError('R2-INV-2: lifting a v1 side changed its canonical bytes') + throw new InvalidRevisionContentError('R2-INV-2', undefined, { kind: 'invariant', detail: 'R2-INV-2: lifting a v1 side changed its canonical bytes' }) } return { ok: true, version, content: lifted, digestVerified: storedDigest !== undefined } } @@ -2215,8 +2256,9 @@ export function validateResultGraph( if (kindsOk) { try { canonicalContent({ nodes, edges }) - } catch { - reasons.push('A node or edge holds a non-finite number (NaN / Infinity).') + } catch (e) { + // #619 item 5 — the projection's own cause, not one sentence for all + reasons.push(e instanceof InvalidRevisionContentError ? invalidContentReason(e) : 'A node or edge holds a non-finite number (NaN / Infinity).') } }