@floless/app 0.135.0 → 0.136.0

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.
@@ -6,7 +6,7 @@
6
6
  * control to a world point. Nothing here infers a view kind or substitutes missing registration data.
7
7
  */
8
8
 
9
- import { parseSpacings, axisLabelsFor, mmPerPx } from './grid-core.js';
9
+ import { parseSpacings, mmPerPx } from './grid-core.js';
10
10
 
11
11
  const ROUND = 1e9;
12
12
  const clean = (n) => {
@@ -14,6 +14,15 @@ const clean = (n) => {
14
14
  return Object.is(q, -0) ? 0 : q;
15
15
  };
16
16
  const finite2 = (p) => Array.isArray(p) && p.length === 2 && p.every(Number.isFinite);
17
+ const round2 = (n) => Math.round(n * 100) / 100;
18
+
19
+ /**
20
+ * How far the drawn control span may differ from the span its grid intersections imply, in POINTS of
21
+ * drawn distance. The takeoff skill has always told the reader to check exactly this by hand ("verify
22
+ * that their display span differs by at most 2 pt from `worldSpanMm * pt_per_ft / 304.8`"); #935 is
23
+ * what happens when only the prose enforces it.
24
+ */
25
+ export const REGISTRATION_SPAN_TOLERANCE_PT = 2;
17
26
 
18
27
  export class PlanTransformError extends Error {
19
28
  constructor(code, message, viewKind = null) {
@@ -29,33 +38,94 @@ const refuse = (code, message, plan) => {
29
38
  };
30
39
 
31
40
  /**
32
- * Resolve one labelled model-grid intersection. Labels are evidence, not identity, so a duplicate is
33
- * ambiguous and refuses instead of quietly selecting the first matching axis.
41
+ * Contract text quoted back into a refusal. `sheet` and the axis labels are schema-unbounded strings an
42
+ * author supplies, and these messages now travel further than the console — the read audit reports them
43
+ * and the request log keeps them (#925) — so a value is identified, never reproduced whole.
44
+ */
45
+ const MAX_QUOTED_CHARS = 48;
46
+ // Built from a string so no literal control character sits in this source file.
47
+ const CONTROL_CHARS = new RegExp('[\\u0000-\\u001f\\u007f]', 'g');
48
+ // A surrogate that was ALREADY unpaired in the contract, not one this clip broke. Repaired either way:
49
+ // an audit report and a request log are not places to reproduce invalid text.
50
+ // LOOKBEHIND, not a consuming prefix group: `[^\ud800-\udbff][\udc00-\udfff]` eats the character before
51
+ // the low surrogate, so a global replace skips past the next one and `\udc00\udc01` repairs only half.
52
+ const LONE_SURROGATE = new RegExp('[\\ud800-\\udbff](?![\\udc00-\\udfff])|(?<![\\ud800-\\udbff])[\\udc00-\\udfff]', 'g');
53
+ const wellFormed = (text) => typeof text.toWellFormed === 'function'
54
+ ? text.toWellFormed() : text.replace(LONE_SURROGATE, '�');
55
+ /**
56
+ * EXPORTED, and the server's diagnosis notes use this one rather than a mirror of it (#925). The audit
57
+ * side of this same change deletes a duplicated reason list on the grounds that two lists which must
58
+ * agree are one list waiting to disagree; two clips which must agree are the same thing, and the
59
+ * server module already imports from here.
60
+ *
61
+ * `max` may be shortened by a caller that has its own budget; a non-positive one yields the empty
62
+ * string rather than looping, so the bound-enforcing helper honours its own contract.
34
63
  */
35
- function gridIntersection(grid, ref, plan) {
36
- if (!grid || typeof grid !== 'object' || !finite2(grid.origin)) {
37
- return refuse('grid-unavailable', `sheet "${plan.sheet || '?'}" has no model grid to resolve its registration controls`, plan);
64
+ export const clipQuoted = (value, max = MAX_QUOTED_CHARS) => {
65
+ const text = wellFormed(typeof value === 'string' && value ? value : '?').replace(CONTROL_CHARS, ' ');
66
+ if (max <= 0) return '';
67
+ if (text.length <= max) return text;
68
+ // Never cut a surrogate pair in half: a lone surrogate is not text, and this string is serialized
69
+ // into an audit report and a request log, not only shown in a console.
70
+ let end = max - 1;
71
+ const code = text.charCodeAt(end - 1);
72
+ if (end > 0 && code >= 0xd800 && code <= 0xdbff) end--;
73
+ return `${text.slice(0, end)}…`;
74
+ };
75
+ const quote = (value) => clipQuoted(value);
76
+
77
+ /**
78
+ * Resolve one model-grid intersection from a pair of AXIS IDS.
79
+ *
80
+ * It resolved LABELS until #935, and that was a silent-metres-wrong bug: `axisLabelsFor` falls back to
81
+ * a positional auto-name (`1,2,3…` across X, `A,B,C…` up Y) for any axis carrying no label of its own,
82
+ * so a contract that identified its gridlines only by `id` still resolved `1`/`C` — against the FIRST
83
+ * and THIRD authored axes. Both refs resolved uniquely, nothing complained, and a 250-member model
84
+ * landed a median of 18 m from where it belonged. An id is the field the schema already calls identity;
85
+ * a label is editable display text and stays out of this.
86
+ *
87
+ * A duplicate id is still ambiguous and still refuses rather than quietly taking the first match.
88
+ */
89
+ function gridIntersection(grid, ref, plan, field) {
90
+ if (!grid || typeof grid !== 'object') {
91
+ return refuse('grid-unavailable', `sheet "${quote(plan.sheet)}" has no model grid to resolve its registration controls`, plan);
92
+ }
93
+ // A grid that carries axes, spacings and labels but no `origin` used to share the message above, and
94
+ // it sent authors back to the half they had already filled in (#925). `$.grid.origin` is optional in
95
+ // the schema and unmentioned by every other check, so its name has to be in the refusal or nothing
96
+ // says it. Same CODE deliberately: `_PLAN_XF_UNREGISTERED` in steel-editor.html classifies by code.
97
+ if (!finite2(grid.origin)) {
98
+ return refuse(
99
+ 'grid-unavailable',
100
+ `sheet "${quote(plan.sheet)}" cannot resolve its registration controls: $.grid.origin is missing or unreadable — it must be two finite world-mm numbers giving where the first x and y grid axes cross`,
101
+ plan,
102
+ );
38
103
  }
39
- if (!ref || typeof ref !== 'object' || typeof ref.axis_x !== 'string' || typeof ref.axis_y !== 'string') {
40
- return refuse('control-ref-invalid', `sheet "${plan.sheet || '?'}" has an unreadable registration control`, plan);
104
+ if (!ref || typeof ref !== 'object' || typeof ref.xAxisId !== 'string' || typeof ref.yAxisId !== 'string') {
105
+ return refuse('control-ref-invalid', `sheet "${quote(plan.sheet)}" has an unreadable ${field}: a registration control needs string xAxisId and yAxisId grid axis ids`, plan);
41
106
  }
42
107
 
43
108
  const X = parseSpacings(grid.x), Y = parseSpacings(grid.y);
44
109
  if (X.err || Y.err) {
45
- return refuse('grid-spacing-invalid', `sheet "${plan.sheet || '?'}" cannot use an invalid model-grid spacing string`, plan);
110
+ const bad = X.err ? '$.grid.x' : '$.grid.y';
111
+ return refuse('grid-spacing-invalid', `sheet "${quote(plan.sheet)}" cannot use ${bad}: ${quote(X.err || Y.err)}`, plan);
46
112
  }
47
- // The SAME resolver the bubbles are drawn from, so a control ref matches the name on the sheet (#740).
48
- const xs = axisLabelsFor(grid, 'x', 'num', X.offsets.length);
49
- const ys = axisLabelsFor(grid, 'y', 'alpha', Y.offsets.length);
50
- const uniqueIndex = (labels, wanted) => {
51
- const hits = labels.map((label, i) => label === wanted ? i : -1).filter((i) => i >= 0);
113
+ // Authored order per direction, which is the order `parseSpacings` produces offsets in — the axis at
114
+ // slot i owns offset i. A direction whose axis rows and offsets disagree in count is not describable,
115
+ // so no id in it resolves; `checkModelReferences` (`grid-axis-count`) refuses such a document upstream.
116
+ const idsFor = (dir) => (Array.isArray(grid.axes) ? grid.axes.filter((a) => a && a.dir === dir) : [])
117
+ .map((a) => typeof a.id === 'string' ? a.id : null);
118
+ const xs = idsFor('x'), ys = idsFor('y');
119
+ const uniqueIndex = (ids, wanted, count) => {
120
+ if (ids.length !== count) return -1;
121
+ const hits = ids.map((id, i) => id === wanted ? i : -1).filter((i) => i >= 0);
52
122
  return hits.length === 1 ? hits[0] : -1;
53
123
  };
54
- const xi = uniqueIndex(xs, ref.axis_x), yi = uniqueIndex(ys, ref.axis_y);
124
+ const xi = uniqueIndex(xs, ref.xAxisId, X.offsets.length), yi = uniqueIndex(ys, ref.yAxisId, Y.offsets.length);
55
125
  if (xi < 0 || yi < 0) {
56
126
  return refuse(
57
127
  'control-ref-unresolved',
58
- `sheet "${plan.sheet || '?'}" registration control ${ref.axis_x}/${ref.axis_y} does not resolve uniquely on the model grid`,
128
+ `sheet "${quote(plan.sheet)}" ${field} names ${quote(ref.xAxisId)}/${quote(ref.yAxisId)}, which is not exactly one x and one y axis id on the model grid`,
59
129
  plan,
60
130
  );
61
131
  }
@@ -71,30 +141,30 @@ function gridIntersection(grid, ref, plan) {
71
141
  export function planTransform(plan) {
72
142
  const kind = plan && plan.viewKind;
73
143
  if (kind !== 'plan') {
74
- const named = typeof kind === 'string' ? kind : 'unknown';
144
+ const named = typeof kind === 'string' && kind ? quote(kind) : 'unknown';
75
145
  return refuse(
76
146
  'view-kind-unsupported',
77
- `sheet "${plan && plan.sheet || '?'}" is a ${named} view; ${named} views are not supported by the plan transform`,
147
+ `sheet "${quote(plan && plan.sheet)}" is a ${named} view; ${named} views are not supported by the plan transform`,
78
148
  plan,
79
149
  );
80
150
  }
81
151
  if (!finite2(plan.datum_at) || !finite2(plan.control2_at) || !plan.control2_ref) {
82
152
  return refuse(
83
153
  'registration-incomplete',
84
- `sheet "${plan.sheet || '?'}" needs two resolved registration controls before it can be edited`,
154
+ `sheet "${quote(plan.sheet)}" needs $.datum_at, $.control2_at and $.control2_ref before it can be edited`,
85
155
  plan,
86
156
  );
87
157
  }
88
158
  const ptPerFt = Number(plan.pt_per_ft);
89
159
  if (!(ptPerFt > 0) || !Number.isFinite(ptPerFt)) {
90
- return refuse('scale-invalid', `sheet "${plan.sheet || '?'}" has no positive drawing scale`, plan);
160
+ return refuse('scale-invalid', `sheet "${quote(plan.sheet)}" has no positive $.pt_per_ft, so it has no drawing scale`, plan);
91
161
  }
92
162
 
93
163
  // Editor projections expose a display-space `grid` because grid-core draws in pixels. The canonical
94
164
  // world grid rides beside it as `modelGrid`; standalone callers and stored-plan fixtures use `grid`.
95
165
  const registrationGrid = plan.modelGrid || plan.grid;
96
- const world0 = plan.datum_ref ? gridIntersection(registrationGrid, plan.datum_ref, plan) : [0, 0];
97
- const world1 = gridIntersection(registrationGrid, plan.control2_ref, plan);
166
+ const world0 = plan.datum_ref ? gridIntersection(registrationGrid, plan.datum_ref, plan, '$.datum_ref') : [0, 0];
167
+ const world1 = gridIntersection(registrationGrid, plan.control2_ref, plan, '$.control2_ref');
98
168
  // The shared plan scale. Its zero/negative fallback is unreachable here — the guard above already
99
169
  // refused any sheet whose `pt_per_ft` is not a positive finite number — so this is the same value
100
170
  // the inline `FT_MM / ptPerFt` produced, from the one implementation.
@@ -106,15 +176,43 @@ export function planTransform(plan) {
106
176
  const worldDelta = [world1[0] - world0[0], world1[1] - world0[1]];
107
177
  const localLen = Math.hypot(local1[0], local1[1]);
108
178
  const worldLen = Math.hypot(worldDelta[0], worldDelta[1]);
109
- if (!(localLen > 1e-9) || !(worldLen > 1e-9)) {
110
- return refuse('registration-degenerate', `sheet "${plan.sheet || '?'}" has coincident registration controls`, plan);
179
+ // TWO different defects, and one recovery cannot serve both: the display points can coincide while the
180
+ // refs are fine, or the refs can resolve to one intersection while the display points differ. Naming
181
+ // the wrong pair sends the author to fields that are already correct (#925's own failure mode).
182
+ if (!(localLen > 1e-9)) {
183
+ return refuse('registration-degenerate', `sheet "${quote(plan.sheet)}" has coincident registration controls: $.datum_at and $.control2_at are the same point on the sheet`, plan);
184
+ }
185
+ if (!(worldLen > 1e-9)) {
186
+ // `datum_ref` is ABSENT for the "datum_at is the model origin" case, and naming a field that is
187
+ // not there is the same wrong-half misdirection this refusal exists to avoid.
188
+ const datum = plan.datum_ref ? '$.datum_ref and $.control2_ref resolve to the same grid intersection'
189
+ : '$.control2_ref resolves to the model origin, which is where an absent $.datum_ref already puts the datum';
190
+ return refuse('registration-degenerate', `sheet "${quote(plan.sheet)}" has coincident registration controls: ${datum}`, plan);
191
+ }
192
+ // THE SPAN INVARIANT. `fitScale` corrects a sub-point pick error; it is not evidence the registration
193
+ // is right, and a RATIO band cannot be made into that evidence. On a 14-axis grid there are 13 bays,
194
+ // so resolving a control one bay out moves the run by ~7.7% — inside any band loose enough for real
195
+ // drawings, and #935's own defect would have passed it. Meanwhile a short but perfectly good
196
+ // registration can be several percent off from one pixel of pick error. So compare an ABSOLUTE
197
+ // distance, in the units the drawing is in: the drawn span between the two controls against the span
198
+ // their grid intersections imply, within 2 pt. That is the check the takeoff skill already tells the
199
+ // reader to do by hand — it was simply never enforced, so a whole-bay misresolution sailed through
200
+ // and put a 250-member model a median of 18 m out with no error at any layer.
201
+ const actualSpanPt = localLen / scale;
202
+ const expectedSpanPt = worldLen / scale;
203
+ if (Math.abs(actualSpanPt - expectedSpanPt) > REGISTRATION_SPAN_TOLERANCE_PT) {
204
+ return refuse(
205
+ 'registration-span-mismatch',
206
+ `sheet "${quote(plan.sheet)}" registration controls span ${round2(actualSpanPt)} pt on the sheet, but ${plan.datum_ref ? '$.datum_ref' : 'the model origin'} and $.control2_ref are ${round2(worldLen)} mm apart, which at ${plan.pt_per_ft} pt/ft is ${round2(expectedSpanPt)} pt — a difference of ${round2(Math.abs(actualSpanPt - expectedSpanPt))} pt, past the ${REGISTRATION_SPAN_TOLERANCE_PT} pt allowed. Either a control names the wrong grid axis or $.pt_per_ft is wrong`,
207
+ plan,
208
+ );
111
209
  }
112
210
  const fitScale = worldLen / localLen;
113
211
  const rotation = Math.atan2(worldDelta[1], worldDelta[0]) - Math.atan2(local1[1], local1[0]);
114
212
  const c = Math.cos(rotation) * fitScale;
115
213
  const s = Math.sin(rotation) * fitScale;
116
214
  if (![c, s, fitScale, world0[0], world0[1]].every(Number.isFinite)) {
117
- return refuse('registration-invalid', `sheet "${plan.sheet || '?'}" has a non-finite registration transform`, plan);
215
+ return refuse('registration-invalid', `sheet "${quote(plan.sheet)}" has a non-finite registration transform`, plan);
118
216
  }
119
217
  return { datumPx: plan.datum_at, world0, mmPerPx: scale, fitScale, rotation, c, s };
120
218
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@floless/app",
3
- "version": "0.135.0",
3
+ "version": "0.136.0",
4
4
  "type": "module",
5
5
  "description": "Thin localhost host for floless.app — serves web/ and shells the aware CLI. No engine, no LLM.",
6
6
  "bin": {