@milaboratories/pl-middle-layer 2.3.0 → 2.4.1

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.
Files changed (30) hide show
  1. package/dist/middle_layer/block_status_fold.cjs +51 -6
  2. package/dist/middle_layer/block_status_fold.cjs.map +1 -1
  3. package/dist/middle_layer/block_status_fold.js +50 -7
  4. package/dist/middle_layer/block_status_fold.js.map +1 -1
  5. package/dist/middle_layer/block_status_tree.cjs +39 -9
  6. package/dist/middle_layer/block_status_tree.cjs.map +1 -1
  7. package/dist/middle_layer/block_status_tree.js +39 -9
  8. package/dist/middle_layer/block_status_tree.js.map +1 -1
  9. package/dist/middle_layer/block_status_tree_fold.cjs +96 -0
  10. package/dist/middle_layer/block_status_tree_fold.cjs.map +1 -0
  11. package/dist/middle_layer/block_status_tree_fold.js +95 -0
  12. package/dist/middle_layer/block_status_tree_fold.js.map +1 -0
  13. package/dist/middle_layer/build_stamp.cjs +1 -1
  14. package/dist/middle_layer/build_stamp.js +1 -1
  15. package/dist/middle_layer/project.cjs +22 -2
  16. package/dist/middle_layer/project.cjs.map +1 -1
  17. package/dist/middle_layer/project.d.ts +13 -1
  18. package/dist/middle_layer/project.d.ts.map +1 -1
  19. package/dist/middle_layer/project.js +22 -2
  20. package/dist/middle_layer/project.js.map +1 -1
  21. package/package.json +8 -8
  22. package/src/middle_layer/block_status_bench.test.ts +139 -48
  23. package/src/middle_layer/block_status_fold.test.ts +2 -61
  24. package/src/middle_layer/block_status_fold.ts +68 -6
  25. package/src/middle_layer/block_status_test_utils.ts +62 -0
  26. package/src/middle_layer/block_status_tree.test.ts +23 -0
  27. package/src/middle_layer/block_status_tree.ts +62 -10
  28. package/src/middle_layer/block_status_tree_fold.test.ts +198 -0
  29. package/src/middle_layer/block_status_tree_fold.ts +150 -0
  30. package/src/middle_layer/project.ts +37 -4
@@ -0,0 +1,150 @@
1
+ /**
2
+ * Builds the {@link BlockStatusTree} of a block run from the {@link SubtreeFold} of its root.
3
+ *
4
+ * Every counter it shows (contexts, active contexts, compute time, the first failure) is already
5
+ * merged into the fold bottom up, so building the tree touches only the root, its top-level
6
+ * contexts and the children of the expanded contexts. It reads nothing from the action-context
7
+ * tree itself.
8
+ *
9
+ * Source: docs/text/work/projects/sdk/block-status/README_teo.md, "Developer debug panel".
10
+ */
11
+
12
+ import type {
13
+ BlockStatusSummary,
14
+ BlockStatusTree,
15
+ BlockStatusTreeBranch,
16
+ BlockStatusTreeNode,
17
+ BlockStatusTreeReading,
18
+ BlockStatusTreeTransition,
19
+ } from "@milaboratories/pl-model-middle-layer";
20
+ import { BlockStatusTreeHeaviestCount } from "@milaboratories/pl-model-middle-layer";
21
+ import type { ActionContextNode, ContextFold, SubtreeFold } from "./block_status_fold";
22
+ import {
23
+ compareNs,
24
+ ExecNamespace,
25
+ foldSubtree,
26
+ nsToMs,
27
+ summarize,
28
+ WorkdirNamespace,
29
+ } from "./block_status_fold";
30
+
31
+ /** A tree with no context under it. */
32
+ export function emptyBlockStatusTree(summary: BlockStatusSummary): BlockStatusTree {
33
+ return { summary, heaviest: { byCompute: [], byContexts: [] } };
34
+ }
35
+
36
+ /**
37
+ * The tree of a block run.
38
+ *
39
+ * @param expanded context ids whose children the reader has opened. The root's children are
40
+ * always present.
41
+ */
42
+ export function blockStatusTree(
43
+ root: SubtreeFold,
44
+ summary: BlockStatusSummary,
45
+ expanded: ReadonlySet<string>,
46
+ ): BlockStatusTree {
47
+ // A command still running is counted up to the run's latest report, not to now: the fold has no
48
+ // clock, and the Desktop App extrapolates the same way for the run's own time.
49
+ const computeMs = (fold: SubtreeFold): number => {
50
+ const { closedNs, open, openSinceSumNs } = fold.compute;
51
+ const lastNs = root.lastNs === undefined ? 0n : BigInt(root.lastNs);
52
+ return Number((closedNs + BigInt(open) * lastNs - openSinceSumNs) / 1_000_000n);
53
+ };
54
+
55
+ const node = (fold: SubtreeFold, isRoot: boolean): BlockStatusTreeNode => {
56
+ const { own } = fold;
57
+ const { holding } = own.folded;
58
+ return {
59
+ contextId: own.id,
60
+ name: nameOf(own),
61
+ kind: kindOf(own),
62
+ baggage: baggageOf(own),
63
+ state: holding?.state,
64
+ onMachine: own.folded.onMachine,
65
+ enteredAt: holding === undefined ? undefined : nsToMs(holding.ns),
66
+ ended: own.folded.terminal,
67
+ failure: own.folded.failure || undefined,
68
+ transitions: transitionsOf(own),
69
+ readings: readingsOf(own),
70
+ childCount: fold.children.length,
71
+ subtreeContexts: fold.contexts,
72
+ subtreeActiveContexts: fold.activeContexts,
73
+ subtreeComputeMs: computeMs(fold),
74
+ children:
75
+ isRoot || expanded.has(own.id)
76
+ ? fold.children.map((child) => node(child, false))
77
+ : undefined,
78
+ };
79
+ };
80
+
81
+ /** The root's top-level contexts, ranked by one measure. One with nothing to rank is left out. */
82
+ const rank = (value: (fold: SubtreeFold) => number): BlockStatusTreeBranch[] =>
83
+ root.children
84
+ .map((child) => ({ contextId: child.own.id, name: nameOf(child.own), value: value(child) }))
85
+ .filter((b) => b.value > 0)
86
+ .sort((a, b) => b.value - a.value || a.contextId.localeCompare(b.contextId))
87
+ .slice(0, BlockStatusTreeHeaviestCount);
88
+
89
+ return {
90
+ summary,
91
+ heaviest: {
92
+ byCompute: rank(computeMs),
93
+ byContexts: rank((fold) => fold.contexts),
94
+ // No reporter writes file sizes yet.
95
+ byFileSize: undefined,
96
+ },
97
+ root: node(root, true),
98
+ firstFailurePath: root.failure?.path,
99
+ };
100
+ }
101
+
102
+ /** {@link blockStatusTree} over a tree that is read once. */
103
+ export function foldBlockStatusTree(
104
+ root: ActionContextNode,
105
+ expanded: ReadonlySet<string>,
106
+ ): BlockStatusTree {
107
+ const fold = foldSubtree(root);
108
+ return blockStatusTree(fold, summarize(fold), expanded);
109
+ }
110
+
111
+ function nameOf(own: ContextFold): string {
112
+ const name = own.data?.name;
113
+ return typeof name === "string" && name !== "" ? name : own.id;
114
+ }
115
+
116
+ function kindOf(own: ContextFold): BlockStatusTreeNode["kind"] {
117
+ const all = own.reports.all;
118
+ if (all.some((t) => t.namespace === ExecNamespace)) return "command";
119
+ if (all.length > 0 && all.every((t) => t.namespace === WorkdirNamespace)) return "workdir";
120
+ return "render";
121
+ }
122
+
123
+ function baggageOf(own: ContextFold): Record<string, string> {
124
+ const out: Record<string, string> = {};
125
+ for (const [key, value] of Object.entries(own.data ?? {}))
126
+ if (key !== "name") out[key] = typeof value === "string" ? value : JSON.stringify(value);
127
+ return out;
128
+ }
129
+
130
+ function transitionsOf(own: ContextFold): BlockStatusTreeTransition[] {
131
+ return [...own.reports.all]
132
+ .sort((a, b) => compareNs(a.ns, b.ns))
133
+ .map((t) => ({
134
+ namespace: t.namespace,
135
+ reporter: t.reporter,
136
+ state: t.state,
137
+ detail: t.detail || undefined,
138
+ reason: t.reason || undefined,
139
+ at: nsToMs(t.ns),
140
+ }));
141
+ }
142
+
143
+ function readingsOf(own: ContextFold): BlockStatusTreeReading[] {
144
+ return [...own.readings].sort(
145
+ (a, b) =>
146
+ a.namespace.localeCompare(b.namespace) ||
147
+ a.resource.localeCompare(b.resource) ||
148
+ a.key.localeCompare(b.key),
149
+ );
150
+ }
@@ -47,7 +47,7 @@ import type { BlockActionContextMode, BlockActionContextNode } from "./block_act
47
47
  import { readBlockActionContextTree } from "./block_action_context";
48
48
  import { getBlockPackInfo } from "./util";
49
49
  import { BlockStatusWatch } from "./block_status_tree";
50
- import type { BlockStatusSummary } from "@milaboratories/pl-model-middle-layer";
50
+ import type { BlockStatusSummary, BlockStatusTree } from "@milaboratories/pl-model-middle-layer";
51
51
  import type { FrontendData } from "../model/frontend";
52
52
  import type { ProjectId, ProjectStructure } from "../model/project_model";
53
53
  import {
@@ -860,6 +860,39 @@ export class Project {
860
860
  blockId: string,
861
861
  mode: BlockActionContextMode = "prod",
862
862
  ): Promise<Computable<BlockStatusSummary>> {
863
+ return (await this.retainBlockStatusWatch(blockId, mode)).summary;
864
+ }
865
+
866
+ /**
867
+ * Starts watching the action-context tree of a block render, context by context. Takes the same
868
+ * hold as {@link watchBlockStatus}, released by {@link unwatchBlockStatus}, and shares its
869
+ * synchronized tree.
870
+ */
871
+ public async watchBlockStatusTree(
872
+ blockId: string,
873
+ mode: BlockActionContextMode = "prod",
874
+ ): Promise<Computable<BlockStatusTree>> {
875
+ return (await this.retainBlockStatusWatch(blockId, mode)).tree;
876
+ }
877
+
878
+ /**
879
+ * Sets which contexts of {@link watchBlockStatusTree} show their children. A no-op while nobody
880
+ * watches the block: the expansion lives with the watch.
881
+ */
882
+ public async setBlockStatusTreeExpanded(
883
+ blockId: string,
884
+ contextIds: readonly string[],
885
+ mode: BlockActionContextMode = "prod",
886
+ ): Promise<void> {
887
+ const pending = this.blockStatusWatches.get(blockStatusKey(blockId, mode));
888
+ if (pending === undefined) return;
889
+ (await pending).setExpanded(contextIds);
890
+ }
891
+
892
+ private async retainBlockStatusWatch(
893
+ blockId: string,
894
+ mode: BlockActionContextMode,
895
+ ): Promise<BlockStatusWatch> {
863
896
  if (this.destroyed)
864
897
  throw new Error(`cannot watch the status of ${blockId}: the project is closed`);
865
898
 
@@ -868,7 +901,7 @@ export class Project {
868
901
  if (existing !== undefined) {
869
902
  const watch = await existing;
870
903
  watch.retain();
871
- return watch.summary;
904
+ return watch;
872
905
  }
873
906
 
874
907
  const started = BlockStatusWatch.start({
@@ -885,7 +918,7 @@ export class Project {
885
918
  started.catch(() => {
886
919
  if (this.blockStatusWatches.get(key) === started) this.blockStatusWatches.delete(key);
887
920
  });
888
- return (await started).summary;
921
+ return await started;
889
922
  }
890
923
 
891
924
  /**
@@ -905,7 +938,7 @@ export class Project {
905
938
  watch = await pending;
906
939
  } catch {
907
940
  // The start failed; its own catch handler drops the entry. Nothing to release, and a
908
- // panel-close handler must not see that failure again.
941
+ // caller letting go must not see that failure again.
909
942
  return;
910
943
  }
911
944