@tidal-ts/terminal 0.2.0 → 0.2.2

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.
@@ -181,7 +181,7 @@ export type TerminalEvent = {
181
181
  | {
182
182
  type: 'series.patch';
183
183
  id: string;
184
- patch: Partial<Omit<SeriesConfig, 'axis' | 'axisGroup'>>;
184
+ patch: Partial<Omit<SeriesConfig, 'axis' | 'axisGroup' | 'parentIds'>>;
185
185
  } | {
186
186
  type: 'series.toggleVisible';
187
187
  id: string;
@@ -410,8 +410,10 @@ export declare function columnLabel(scope: EditScope, column: string): string;
410
410
  *
411
411
  * It has to run the whole propagation, because an edit is refusable for a
412
412
  * reason that isn't visible on the study itself: re-speccing it rewrites
413
- * everything built on it, and one of THOSE can land on a column another config
414
- * already holds — two lines computing the same thing. Before this was exported,
413
+ * everything built on it, and one of THOSE rewrites can be a spec the registry
414
+ * rejects, or move a unit incoherently. (Landing on a column another config
415
+ * already computes used to be a reason too; it is not since `TDL-CMPDUP` — two
416
+ * configs may compute the same thing.) Before this was exported,
415
417
  * such an edit simply did nothing when clicked, with no way to find out why
416
418
  * (PR #140 review, MEDIUM).
417
419
  */
@@ -503,7 +505,7 @@ export declare const terminalMachine: import("xstate").StateMachine<TerminalCont
503
505
  } | {
504
506
  type: "series.patch";
505
507
  id: string;
506
- patch: Partial<Omit<SeriesConfig, "axis" | "axisGroup">>;
508
+ patch: Partial<Omit<SeriesConfig, "axis" | "axisGroup" | "parentIds">>;
507
509
  } | {
508
510
  type: "series.toggleVisible";
509
511
  id: string;
@@ -31,7 +31,7 @@ function hidesPointer(context, toggledId, pointer) {
31
31
  : new Set([toggledId]);
32
32
  return cascade.has(pointer);
33
33
  }
34
- import { axisRangeIsEmpty, logAllowed } from '@tidal-ts/chart';
34
+ import { axisRangeIsEmpty, logAllowed, readsFrom, sourcesOf, } from '@tidal-ts/chart';
35
35
  import { configAxisId, seriesAxisId, sharedAxisId, } from '@tidal-ts/chart';
36
36
  import { axisForAdd, canAddUnit } from './axisPolicy.js';
37
37
  export const PRESET_SLOTS = 5;
@@ -114,19 +114,19 @@ const flatConfigs = (rows) => rows.flatMap((r) => r.configs);
114
114
  * raw series is removed. */
115
115
  function derivedClosure(configs, rootId) {
116
116
  const doomed = new Set([rootId]);
117
- // Edges resolve by COLUMN (what a spec input names); the set itself holds
118
- // IDS, because that is what callers filter configs and re-point pointers by.
119
- const byColumn = new Map(configs.map((c) => [c.column, c]));
117
+ // The set holds IDS, because that is what callers filter configs and re-point
118
+ // pointers by — and the edges now resolve by id too (`sourcesOf`). They used
119
+ // to go through a `Map` keyed by column, which with a metric seated twice
120
+ // kept only the LAST config per column: removing one copy cascaded through
121
+ // the other's studies.
120
122
  for (let grew = true; grew;) {
121
123
  grew = false;
122
124
  for (const c of configs) {
123
125
  if (doomed.has(c.id) || !c.derive)
124
126
  continue;
125
127
  // EVERY input is an edge — a pair's B leg is as load-bearing as its A leg
126
- // (PR #128 review, HIGH). An input names a raw feed column or a nested
127
- // spec, and either way the config that produces it carries that name as
128
- // its `column`.
129
- const srcs = inputNames(c.derive).map((n) => byColumn.get(n));
128
+ // (PR #128 review, HIGH).
129
+ const srcs = sourcesOf(configs, c);
130
130
  if (srcs.some((src) => src?.derive && doomed.has(src.id))) {
131
131
  doomed.add(c.id); // derives from a doomed study ⇒ its source column will vanish
132
132
  grew = true;
@@ -164,10 +164,9 @@ function derivedClosure(configs, rootId) {
164
164
  * A study is subordinate to what it is built on; a leg is a peer of its partner.
165
165
  * Same graph, opposite rule, so they get their own walks.
166
166
  */
167
- /** Whether a study already has a study built on it — the no-branching test.
168
- * Edges resolve by COLUMN, which is what a spec input names. */
167
+ /** Whether a study already has a study built on it — the no-branching test. */
169
168
  function hasStudyChild(configs, target) {
170
- return configs.some((c) => c.id !== target.id && c.derive && inputNames(c.derive).includes(target.column));
169
+ return configs.some((c) => c.id !== target.id && readsFrom(configs, c, target));
171
170
  }
172
171
  function studiesOn(configs, rootId, isCatalogColumn) {
173
172
  const set = new Set([rootId]);
@@ -176,7 +175,7 @@ function studiesOn(configs, rootId, isCatalogColumn) {
176
175
  for (const c of configs) {
177
176
  if (set.has(c.id) || !c.derive || isCatalogColumn(c.column))
178
177
  continue;
179
- if (configs.some((p) => set.has(p.id) && inputNames(c.derive).includes(p.column))) {
178
+ if (configs.some((p) => set.has(p.id) && readsFrom(configs, c, p))) {
180
179
  set.add(c.id);
181
180
  grew = true;
182
181
  }
@@ -202,9 +201,11 @@ function layeredOn(configs, rootId, isCatalogColumn) {
202
201
  }
203
202
  if (!c.derive || isCatalogColumn(c.column))
204
203
  continue;
205
- // A study names its sources by COLUMN — never by the id of the config
206
- // that happens to display them.
207
- if (configs.some((p) => set.has(p.id) && inputNames(c.derive).includes(p.column))) {
204
+ // A study's SPEC names its sources by column, which is the data; which
205
+ // CONFIG it travels with is its recorded parent (`sourcesOf`). With one
206
+ // config per column those were the same answer, and with a metric seated
207
+ // twice they are not.
208
+ if (configs.some((p) => set.has(p.id) && readsFrom(configs, c, p))) {
208
209
  set.add(c.id);
209
210
  grew = true;
210
211
  }
@@ -265,8 +266,7 @@ export function rowBlocks(configs) {
265
266
  // tunable op had one param called `period` (PR #181 review, MEDIUM).
266
267
  if (!c.derive || opParams(c.derive.op).length === 0)
267
268
  continue;
268
- const names = inputNames(c.derive);
269
- const parent = configs.find((x) => x.id !== c.id && names.includes(x.column));
269
+ const parent = sourcesOf(configs, c)[0];
270
270
  if (parent)
271
271
  parentOf.set(c.id, parent.id);
272
272
  }
@@ -330,7 +330,7 @@ export function studyChainTail(configs, rootId) {
330
330
  !seen.has(c.id) &&
331
331
  !!c.derive &&
332
332
  opParams(c.derive.op).length > 0 &&
333
- inputNames(c.derive).includes(cur.column));
333
+ readsFrom(configs, c, cur));
334
334
  if (!next)
335
335
  return cur.id;
336
336
  seen.add(next.id);
@@ -473,6 +473,15 @@ function studyConfig(target, op, period, id) {
473
473
  value: null,
474
474
  unit: shares ? target.unit : own,
475
475
  derive,
476
+ // WHICH config this study is on — recorded now, because this is the one
477
+ // moment it is known for certain: the user pressed THIS config's button.
478
+ // The spec above names the target by column, which is the data; this
479
+ // names it by id, which is the graph (`sourcesOf`). Index-ALIGNED with the
480
+ // spec's inputs, and only where an input actually reads the target: an
481
+ // ATR's inputs are the bar's high/low/close, whatever it was added from,
482
+ // and recording the target there made an ATR added from an SMA read as
483
+ // that SMA's child (PR #16 review, MEDIUM).
484
+ ...recordParents(derive, [target]),
476
485
  };
477
486
  }
478
487
  /** Legend label per pair op — the spread reads as its construction. */
@@ -513,6 +522,7 @@ const resolveLeg = (context, id) => {
513
522
  const cfg = legSeat(context, id);
514
523
  if (cfg)
515
524
  return {
525
+ seatId: cfg.id,
516
526
  label: cfg.label,
517
527
  unit: cfg.unit ?? '',
518
528
  column: cfg.column,
@@ -636,8 +646,23 @@ palette = [], taken = [], spreadColor) {
636
646
  // diff keeps the unit, a ratio is unitless, a log-ratio reads `log`.
637
647
  unit: specUnit(derive, unitOf),
638
648
  derive,
649
+ // A leg that resolved to a seated config is that config's child. Only an
650
+ // input that reads the seat AS IS is recorded: a per-leg study or a compare
651
+ // rebind makes the input its own computation, which no seated config
652
+ // publishes.
653
+ ...recordParents(derive, [a, b].flatMap((l) => (l.seatId ? [{ id: l.seatId, column: l.column }] : []))),
639
654
  };
640
655
  }
656
+ /**
657
+ * `parentIds` for a derived config about to be minted: at each input position,
658
+ * the id of the `seat` that publishes that input's column, else nothing.
659
+ * Returns `{}` when nothing was recorded, so a config with no seated parents
660
+ * carries no empty array.
661
+ */
662
+ function recordParents(derive, seats) {
663
+ const parentIds = inputNames(derive).map((name) => seats.find((s) => s.column === name)?.id);
664
+ return parentIds.some((id) => id != null) ? { parentIds } : {};
665
+ }
641
666
  /**
642
667
  * A pair PART's display name, rebuilt from the spec — the same construction
643
668
  * `boundLeg` writes, read back the other way: the metric's catalog name, its
@@ -716,7 +741,8 @@ function pairSeats(context, event) {
716
741
  return !!pairRow(context, event);
717
742
  }
718
743
  /** The column a study takes if one of its params changes — its spec re-encoded.
719
- * The identity does NOT change with it; this guards column collisions only. */
744
+ * The identity does NOT change with it; this only tells a real edit from a
745
+ * no-op (the same column back). */
720
746
  const restudyColumn = (study, name, value) => deriveId({ ...study.derive, params: { ...study.derive.params, [name]: value } });
721
747
  /**
722
748
  * Whether a study's param `name` can move to `value` — shared by the guard and
@@ -725,8 +751,10 @@ const restudyColumn = (study, name, value) => deriveId({ ...study.derive, params
725
751
  *
726
752
  * It has to run the whole propagation, because an edit is refusable for a
727
753
  * reason that isn't visible on the study itself: re-speccing it rewrites
728
- * everything built on it, and one of THOSE can land on a column another config
729
- * already holds — two lines computing the same thing. Before this was exported,
754
+ * everything built on it, and one of THOSE rewrites can be a spec the registry
755
+ * rejects, or move a unit incoherently. (Landing on a column another config
756
+ * already computes used to be a reason too; it is not since `TDL-CMPDUP` — two
757
+ * configs may compute the same thing.) Before this was exported,
730
758
  * such an edit simply did nothing when clicked, with no way to find out why
731
759
  * (PR #140 review, MEDIUM).
732
760
  */
@@ -808,8 +836,12 @@ adding = false) {
808
836
  const { column, derive } = legParts(input);
809
837
  // Adoption looks only in the pair's OWN row: a group whose legs sit in two
810
838
  // rows has no single Pair node to render (the panel is per-row), so it
811
- // would draw as two one-legged pairs.
812
- const seated = row.find((c) => c.id !== pair.id && c.column === column);
839
+ // would draw as two one-legged pairs. The RECORDED leg first: with a metric
840
+ // seated twice in one row, a compare added from the second copy must adopt
841
+ // the second copy, not whichever the column finds first (PR #16 review).
842
+ const byId = pair.parentIds?.[i];
843
+ const seated = (byId != null ? row.find((c) => c.id === byId && c.column === column) : undefined) ??
844
+ row.find((c) => c.id !== pair.id && c.column === column);
813
845
  if (seated) {
814
846
  // …and never STEAL a leg from another pair. Two pairs sharing a leg
815
847
  // column would leave the first group with one member, which nothing can
@@ -820,10 +852,11 @@ adding = false) {
820
852
  plan.adopt.push({ id: seated.id, group: { id: pair.id, side } });
821
853
  return;
822
854
  }
823
- // The column is free in this row but taken in ANOTHER — creating here would
824
- // put two configs on one column, which every dependency walk resolves by.
825
- if (configs.some((c) => c.id !== pair.id && c.column === column))
826
- refuse = true;
855
+ // The column may be seated on ANOTHER row already, and a leg is created
856
+ // here regardless: two configs on one column is legal now that the walks
857
+ // resolve by recorded parent. This refusal is what made an unjoined
858
+ // compare from Bottom silently do nothing when its metric lived on Top
859
+ // (Peter, 2026-09-28: "why can't I add this compare?").
827
860
  const id = pairIdFree ? ((pairIdFree = false), pair.id) : mintId(seq++);
828
861
  if (id !== pair.id)
829
862
  plan.mints += 1;
@@ -848,6 +881,9 @@ adding = false) {
848
881
  unit,
849
882
  value: null,
850
883
  group: { id: pair.id, side },
884
+ // A leg is not a child of the pair's parents: they were the pair's
885
+ // inputs, and a leg's own inputs (if it is derived) are its spec's.
886
+ parentIds: undefined,
851
887
  });
852
888
  });
853
889
  // Both legs adopted ⇒ the pair's own config has nothing left to be, and
@@ -973,6 +1009,13 @@ function propagateRespec(configs, rootId, root, unitOf) {
973
1009
  const moved = dep.id === c.id ? undefined : next.get(dep.id);
974
1010
  if (!moved || moved.column === dep.column)
975
1011
  continue;
1012
+ // Only a config BUILT ON `dep` follows its retune. The substitution
1013
+ // itself is by column, and with a metric seated twice the other copy's
1014
+ // studies read that same column: without this, retuning one SMA
1015
+ // re-pointed the OTHER copy's children at it, silently changing what a
1016
+ // line the user never touched computes.
1017
+ if (!readsFrom(configs, c, dep))
1018
+ continue;
976
1019
  spec = substituteInput(spec, dep.column, moved.derive);
977
1020
  }
978
1021
  if (spec === before)
@@ -995,10 +1038,15 @@ function propagateRespec(configs, rootId, root, unitOf) {
995
1038
  // way through an edit. Refuse rather than apply it.
996
1039
  if (!settled)
997
1040
  return null;
998
- // One column, one config — including against the configs that did NOT move.
999
- const columns = configs.map((c) => next.get(c.id)?.column ?? c.column);
1000
- if (new Set(columns).size !== columns.length)
1001
- return null;
1041
+ // There used to be a check here that every column on the chart is unique
1042
+ // after the edit — "one column, one config". It encoded the model this
1043
+ // function no longer has, and it was wrong in the worst way: `canAdd` has
1044
+ // allowed a metric to be seated twice for a while, and the moment one was,
1045
+ // this refused EVERY retune on the chart, silently, including of studies with
1046
+ // nothing to do with the duplicate (verified against the released machine,
1047
+ // 2026-09-28). Two configs may share a column now; they are told apart by id
1048
+ // and by recorded parent (`sourcesOf`), and the substitution above only
1049
+ // follows a config's own children.
1002
1050
  return next;
1003
1051
  }
1004
1052
  /**
@@ -1299,10 +1347,9 @@ export const terminalMachine = setup({
1299
1347
  // Row cap (the control-panel spec: 1–3). The UI also gates on "current bottom has
1300
1348
  // content"; this is the hard invariant.
1301
1349
  canAddRow: ({ context }) => context.rows.length < MAX_ROWS,
1302
- // A study may be added if its target exists and the exact spec isn't already
1303
- // on the chart. One column, one config: a duplicate spec would fold to the
1304
- // same column, drawing one line twice and making the dependency walks
1305
- // (which resolve edges by column) ambiguous about which config owns it.
1350
+ // A study may be added if its target exists and can take it. The same spec
1351
+ // may already be on the chart — on another copy of the metric, or on this
1352
+ // one (see the end of the guard).
1306
1353
  canAddStudy: ({ context, event }) => {
1307
1354
  if (event.type !== 'series.addStudy')
1308
1355
  return false;
@@ -1343,17 +1390,20 @@ export const terminalMachine = setup({
1343
1390
  // errors the whole actor (PR #130 review, HIGH). The machine is
1344
1391
  // host-driven — this guard IS the validation.
1345
1392
  const spec = studySpec(target, event.op, event.period);
1346
- if (!isValidSpec(spec))
1347
- return false;
1348
- const column = deriveId(spec);
1349
- return !flatConfigs(context.rows).some((c) => c.column === column);
1393
+ // The same study may sit on two configs — an SMA on the price in each of
1394
+ // two rows, each copy then growing its own chain. That was refused
1395
+ // because every walk resolved by column and could not tell the two
1396
+ // apart; they resolve by recorded parent now (`sourcesOf`), so it is the
1397
+ // user's to decide whether two of something makes sense (Peter,
1398
+ // 2026-09-28: "It's up to them to make it make sense, not us").
1399
+ return isValidSpec(spec);
1350
1400
  },
1351
1401
  // A pair may be added if both legs exist, differ, and read the SAME data
1352
1402
  // source — the derive fold runs per series, so both columns must live on
1353
1403
  // one series (cross-source/cross-entity pairs need the join step; see
1354
1404
  // Tidal's pairs plan). The spread arrives UNLINKED (its own axis), so there is
1355
- // no unit gate — only a target row must exist and the exact spec must not
1356
- // already be on the chart.
1405
+ // no unit gate — only a target row must exist. The same spec may already be
1406
+ // on the chart (`TDL-CMPDUP`).
1357
1407
  canAddPair: ({ context, event }) => {
1358
1408
  if (event.type !== 'series.addPair')
1359
1409
  return false;
@@ -1392,8 +1442,7 @@ export const terminalMachine = setup({
1392
1442
  const row = pairRow(context, event);
1393
1443
  return !!(row && planAddGroup(context, event, row));
1394
1444
  }
1395
- if (flatConfigs(context.rows).some((c) => c.column === deriveId(spec)))
1396
- return false;
1445
+ // The same spread may be seated twice, on two rows — see `canAddStudy`.
1397
1446
  // …and a row to land on. Delegated to `pairSeats` — which the sibling
1398
1447
  // assigners already use — so the guard, the rows assigner and the id
1399
1448
  // counter cannot disagree about where a spread goes (a named row, else
@@ -1806,7 +1855,11 @@ export const terminalMachine = setup({
1806
1855
  rows: ({ context, event }) => event.type === 'series.patch'
1807
1856
  ? context.rows.map((r) => ({
1808
1857
  ...r,
1809
- configs: r.configs.map((c) => (c.id === event.id ? { ...c, ...event.patch } : c)),
1858
+ configs: r.configs.map((c) =>
1859
+ // `parentIds` is the graph, not a presentation field: a patch
1860
+ // may not rewrite which config a study is on (an untyped host
1861
+ // could still send one).
1862
+ c.id === event.id ? { ...c, ...event.patch, parentIds: c.parentIds } : c),
1810
1863
  }))
1811
1864
  : context.rows,
1812
1865
  }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tidal-ts/terminal",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "private": false,
5
5
  "description": "The terminal machine — an XState v5 statechart that owns a chart's configuration (rows, series, studies, pairs, axes, presets) and never its data.",
6
6
  "license": "MIT",
@@ -36,19 +36,20 @@
36
36
  "xstate": "^5.28.0"
37
37
  },
38
38
  "dependencies": {
39
- "@tidal-ts/chart": "0.2.0",
40
- "@tidal-ts/core": "0.2.0"
39
+ "@tidal-ts/chart": "0.2.2",
40
+ "@tidal-ts/core": "0.2.2"
41
41
  },
42
42
  "devDependencies": {
43
- "@pond-ts/financial": "^0.69.0",
44
- "@pond-ts/process": "0.69.0",
45
- "pond-ts": "^0.69.0",
46
- "typescript": "^5.7.2",
47
- "vitest": "^2.1.8",
48
- "@pond-ts/charts": "^0.69.0",
43
+ "@pond-ts/charts": "^0.70.0",
44
+ "@pond-ts/financial": "^0.70.0",
45
+ "@pond-ts/process": "0.70.0",
46
+ "@pond-ts/react": "^0.70.0",
49
47
  "@types/react": "^18.3.18",
50
- "react": "^18.3.1",
51
48
  "@xstate/react": "^6.1.0",
49
+ "pond-ts": "^0.70.0",
50
+ "react": "^18.3.1",
51
+ "typescript": "^5.7.2",
52
+ "vitest": "^2.1.8",
52
53
  "xstate": "^5.32.4"
53
54
  },
54
55
  "scripts": {