@tsplc/tmp-debug 0.0.0-stage → 0.6.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.
Files changed (78) hide show
  1. package/README.md +38 -2
  2. package/dist/api.js +744 -0
  3. package/dist/api.js.map +12 -0
  4. package/dist/constant.js +33 -0
  5. package/dist/constant.js.map +10 -0
  6. package/dist/explain-file.js +468 -0
  7. package/dist/explain-file.js.map +11 -0
  8. package/dist/explain.js +1310 -0
  9. package/dist/explain.js.map +19 -0
  10. package/dist/format.js +214 -0
  11. package/dist/format.js.map +12 -0
  12. package/dist/index.js +4066 -0
  13. package/dist/index.js.map +44 -0
  14. package/dist/manifest.js +240 -0
  15. package/dist/manifest.js.map +10 -0
  16. package/dist/paths.js +242 -0
  17. package/dist/paths.js.map +10 -0
  18. package/package.json +72 -4
  19. package/src/adapter.ts +77 -0
  20. package/src/api.ts +446 -0
  21. package/src/app-server.ts +151 -0
  22. package/src/build-files.ts +46 -0
  23. package/src/constant.ts +41 -0
  24. package/src/debugger.ts +237 -0
  25. package/src/errors.ts +13 -0
  26. package/src/explain-expressions.ts +484 -0
  27. package/src/explain-file.ts +325 -0
  28. package/src/explain-frames.ts +155 -0
  29. package/src/explain-loops.ts +151 -0
  30. package/src/explain-operations.ts +232 -0
  31. package/src/explain-tables.ts +345 -0
  32. package/src/explain-value.ts +203 -0
  33. package/src/explain-verdicts.ts +548 -0
  34. package/src/explain.ts +82 -0
  35. package/src/hardware.ts +159 -0
  36. package/src/http.ts +218 -0
  37. package/src/index.ts +44 -0
  38. package/src/jobs.ts +139 -0
  39. package/src/login-value.ts +165 -0
  40. package/src/login.ts +315 -0
  41. package/src/manifest.ts +328 -0
  42. package/src/opcua.ts +80 -0
  43. package/src/paths.ts +363 -0
  44. package/src/requests.ts +99 -0
  45. package/src/server-cursor.ts +22 -0
  46. package/src/shown.ts +229 -0
  47. package/src/telemetry.ts +125 -0
  48. package/src/timeline.ts +148 -0
  49. package/src/trace.ts +62 -0
  50. package/src/ui/bits.ts +48 -0
  51. package/src/ui/client.ts +87 -0
  52. package/src/ui/dom.ts +61 -0
  53. package/src/ui/explain-read.ts +105 -0
  54. package/src/ui/format.ts +261 -0
  55. package/src/ui/hardware.ts +425 -0
  56. package/src/ui/header.ts +207 -0
  57. package/src/ui/health.ts +64 -0
  58. package/src/ui/index.ts +27 -0
  59. package/src/ui/jobs.ts +263 -0
  60. package/src/ui/links.ts +59 -0
  61. package/src/ui/live-value.ts +85 -0
  62. package/src/ui/main.ts +76 -0
  63. package/src/ui/overview.ts +178 -0
  64. package/src/ui/page.ts +286 -0
  65. package/src/ui/pages.ts +64 -0
  66. package/src/ui/recent-window.ts +49 -0
  67. package/src/ui/recent.ts +355 -0
  68. package/src/ui/request-trace.ts +38 -0
  69. package/src/ui/requests.ts +80 -0
  70. package/src/ui/routes.ts +98 -0
  71. package/src/ui/store.ts +295 -0
  72. package/src/ui/success-cache.ts +11 -0
  73. package/src/ui/tree.ts +162 -0
  74. package/src/ui/variables.ts +597 -0
  75. package/src/ui/why-watch.ts +11 -0
  76. package/src/ui/why.ts +694 -0
  77. package/src/values.ts +170 -0
  78. package/src/watch.ts +32 -0
@@ -0,0 +1,355 @@
1
+ import { appRecord } from "@tsplc/server/telemetry";
2
+ import {
3
+ type AppRecord,
4
+ type TimelineEntry,
5
+ type TimelineSource,
6
+ type TraceEvent,
7
+ timelineEntrySchema,
8
+ timelineSourceSchema,
9
+ } from "../api";
10
+ import { empty, h, type View } from "./dom";
11
+ import {
12
+ failureName,
13
+ formatClock,
14
+ formatDuration,
15
+ formatProperties,
16
+ formatValue,
17
+ } from "./format";
18
+ import { pathLink, recordSourceLink, sourceLink, whyLink } from "./links";
19
+ import { requestJobLink, requestStatus } from "./request-trace";
20
+ import type { Store } from "./store";
21
+
22
+ type Kind = AppRecord["kind"] | Exclude<TimelineEntry["kind"], "app">;
23
+
24
+ const SOURCES = timelineSourceSchema.options;
25
+ const KINDS: Kind[] = [
26
+ ...appRecord.options.map((option) => option.shape.kind.value),
27
+ ...timelineEntrySchema.options.flatMap((option) => {
28
+ const kind = option.shape.kind.value;
29
+ return kind === "app" ? [] : [kind];
30
+ }),
31
+ ];
32
+ const PAGE = 300;
33
+
34
+ const hiddenSources = new Set<TimelineSource>();
35
+ const hiddenKinds = new Set<Kind>();
36
+
37
+ const kindOf = (entry: TimelineEntry): Kind =>
38
+ entry.kind === "app" ? entry.record.kind : entry.kind;
39
+
40
+ /** A traced write, worded as what was observed: the value, the scan, and the guards captured with it. */
41
+ const traceBody = (store: Store, trace: TraceEvent) => {
42
+ const site = store.eventSites.get(trace.site);
43
+ if (!site) {
44
+ return [
45
+ h(
46
+ "div",
47
+ {},
48
+ `Site ${trace.site} is not in this build's manifest; value ${JSON.stringify(trace.value)} in scan ${trace.scan}.`,
49
+ ),
50
+ ];
51
+ }
52
+ return [
53
+ h(
54
+ "div",
55
+ {},
56
+ h("span", { class: "tag" }, site.kind),
57
+ " ",
58
+ pathLink(site.path),
59
+ whyLink(site.path),
60
+ " was ",
61
+ h(
62
+ "b",
63
+ { class: "val" },
64
+ formatValue(store.manifest, site.type, trace.value),
65
+ ),
66
+ ` in scan ${trace.scan}`,
67
+ h("span", { class: "dim" }, ` · ${trace.task}`),
68
+ ),
69
+ h("div", {}, h("code", {}, site.text), " ", sourceLink(store, site.range)),
70
+ trace.operands.length > 0 &&
71
+ h(
72
+ "div",
73
+ { class: "chips" },
74
+ h("span", { class: "dim" }, "guards in that scan "),
75
+ ...trace.operands.map((operand, index) => {
76
+ const declared = site.operands[index];
77
+ const value = declared
78
+ ? formatValue(store.manifest, declared.type, operand.value)
79
+ : JSON.stringify(operand.value);
80
+ return h(
81
+ "span",
82
+ {
83
+ class: `chip${operand.stable ? "" : " unstable"}`,
84
+ title: operand.stable
85
+ ? `${operand.text}, captured at the write`
86
+ : `${operand.text}, captured at the write; code between the guard and the write can change it, so the guard may have seen another value`,
87
+ },
88
+ declared
89
+ ? sourceLink(store, declared.range, operand.text)
90
+ : h("code", {}, operand.text),
91
+ " = ",
92
+ operand.path
93
+ ? pathLink(operand.path, value)
94
+ : h("span", { class: "val" }, value),
95
+ operand.path ? whyLink(operand.path) : "",
96
+ operand.stable ? "" : h("span", { class: "dim" }, " ~"),
97
+ );
98
+ }),
99
+ ),
100
+ ];
101
+ };
102
+
103
+ /** What the program reported: `part detected · tag=ITEM-0003… sensor=3 · level.ts:120`. */
104
+ const appBody = (store: Store, task: string, record: AppRecord) => {
105
+ const properties = Object.keys(record.properties).length
106
+ ? h(
107
+ "span",
108
+ { class: "mono", title: JSON.stringify(record.properties, null, 1) },
109
+ ` · ${formatProperties(record.properties)}`,
110
+ )
111
+ : "";
112
+ const origin = [
113
+ " · ",
114
+ recordSourceLink(store, record.source),
115
+ h("span", { class: "dim" }, ` · ${task}`),
116
+ ];
117
+ switch (record.kind) {
118
+ case "event":
119
+ return [h("div", {}, h("b", {}, record.event), properties, ...origin)];
120
+ case "log":
121
+ return [
122
+ h(
123
+ "div",
124
+ {},
125
+ h("span", { class: `tag lvl-${record.level}` }, record.level),
126
+ " ",
127
+ record.message,
128
+ properties,
129
+ ...origin,
130
+ ),
131
+ ];
132
+ case "exception":
133
+ return [
134
+ h(
135
+ "div",
136
+ {},
137
+ h("b", { class: "err" }, failureName(store.manifest, record)),
138
+ " · ",
139
+ record.handled
140
+ ? h("span", { class: "dim" }, "handled")
141
+ : h("b", { class: "err" }, "unhandled"),
142
+ record.function
143
+ ? h("span", { class: "mono" }, ` · ${record.function}`)
144
+ : "",
145
+ record.instance
146
+ ? h("span", {}, " on ", pathLink(record.instance))
147
+ : "",
148
+ properties,
149
+ ...origin,
150
+ ),
151
+ ];
152
+ }
153
+ };
154
+
155
+ const byWhom = (entry: { user?: string; host?: string }) =>
156
+ entry.user || entry.host
157
+ ? h(
158
+ "span",
159
+ { class: "dim" },
160
+ ` by ${entry.user ?? "unknown user"}${entry.host ? ` on ${entry.host}` : ""}`,
161
+ )
162
+ : "";
163
+
164
+ const entryBody = (store: Store, entry: TimelineEntry) => {
165
+ switch (entry.kind) {
166
+ case "app":
167
+ return appBody(store, entry.task, entry.record);
168
+ case "trace":
169
+ return traceBody(store, entry.trace);
170
+ case "request": {
171
+ const { request } = entry;
172
+ return [
173
+ h(
174
+ "div",
175
+ {},
176
+ h("b", { class: "mono" }, request.service),
177
+ " ",
178
+ requestStatus(request),
179
+ request.durationMs === undefined
180
+ ? ""
181
+ : ` in ${formatDuration(request.durationMs)}`,
182
+ request.job === undefined
183
+ ? ""
184
+ : h(
185
+ "span",
186
+ {},
187
+ " · from ",
188
+ requestJobLink(store, request.job, request.sequence),
189
+ ),
190
+ request.error ? h("div", { class: "err" }, request.error) : "",
191
+ ),
192
+ ];
193
+ }
194
+ case "deploy":
195
+ return [
196
+ h(
197
+ "div",
198
+ {},
199
+ `Release ${entry.release} · ${entry.operation}`,
200
+ entry.build
201
+ ? h("span", { class: "mono dim" }, ` build ${entry.build}`)
202
+ : "",
203
+ byWhom(entry),
204
+ ),
205
+ ];
206
+ case "audit":
207
+ return [
208
+ h(
209
+ "div",
210
+ {},
211
+ h("b", { class: "mono" }, `tsplc ${entry.command}`),
212
+ byWhom(entry),
213
+ entry.error ? h("span", { class: "err" }, ` · ${entry.error}`) : "",
214
+ ),
215
+ h(
216
+ "div",
217
+ { class: "chips" },
218
+ ...Object.entries(entry.values).map(([path, value]) =>
219
+ h(
220
+ "span",
221
+ { class: "chip" },
222
+ pathLink(path),
223
+ value === "" ? " unforced" : ` = ${value}`,
224
+ entry.previous?.[path] === undefined
225
+ ? ""
226
+ : h("span", { class: "dim" }, ` (was ${entry.previous[path]})`),
227
+ ),
228
+ ),
229
+ ),
230
+ ];
231
+ case "connection":
232
+ return [
233
+ h(
234
+ "div",
235
+ {},
236
+ `Reading values: ${entry.state}`,
237
+ entry.error ? h("span", { class: "err" }, ` · ${entry.error}`) : "",
238
+ ),
239
+ ];
240
+ }
241
+ };
242
+
243
+ export const recentEntry = (store: Store, entry: TimelineEntry) =>
244
+ h(
245
+ "div",
246
+ { class: "entry" },
247
+ h(
248
+ "span",
249
+ { class: "when mono" },
250
+ formatClock(entry.at),
251
+ entry.approximate
252
+ ? h(
253
+ "span",
254
+ {
255
+ class: "approx",
256
+ title:
257
+ "Placed through another clock, so its order against other sources is approximate",
258
+ },
259
+ "≈",
260
+ )
261
+ : "",
262
+ ),
263
+ h("span", { class: `source src-${entry.source}` }, entry.source),
264
+ h("div", { class: "body" }, ...entryBody(store, entry)),
265
+ );
266
+
267
+ const clockNote = (store: Store) => {
268
+ const clock = store.session.clock;
269
+ return `PLC records are placed on the app server's clock from their age when it received them${
270
+ clock ? `, within ±${clock.uncertaintyMs.toFixed(1)} ms` : ""
271
+ }, so their order against other sources is approximate (≈).`;
272
+ };
273
+
274
+ export const recentView = (store: Store): View => {
275
+ let shown = PAGE;
276
+ const list = h("div", { class: "entries" });
277
+ const more = h(
278
+ "button",
279
+ {
280
+ onclick: () => {
281
+ shown += PAGE;
282
+ render();
283
+ },
284
+ },
285
+ `Show ${PAGE} older`,
286
+ );
287
+ const note = h("p", { class: "hint" });
288
+
289
+ const toggles = <T extends string>(values: readonly T[], hidden: Set<T>) =>
290
+ values.map((value) => {
291
+ const input = h("input", {
292
+ type: "checkbox",
293
+ checked: !hidden.has(value),
294
+ });
295
+ input.addEventListener("change", () => {
296
+ if (input.checked) {
297
+ hidden.delete(value);
298
+ } else {
299
+ hidden.add(value);
300
+ }
301
+ render();
302
+ });
303
+ return h("label", { class: "check" }, input, value);
304
+ });
305
+
306
+ const render = () => {
307
+ const visible = store.timeline.filter(
308
+ (entry) =>
309
+ !hiddenSources.has(entry.source) && !hiddenKinds.has(kindOf(entry)),
310
+ );
311
+ list.replaceChildren(
312
+ ...(visible.length
313
+ ? visible
314
+ .slice(-shown)
315
+ .reverse()
316
+ .map((entry) => recentEntry(store, entry))
317
+ : [empty("Nothing in the recent window matches these filters.")]),
318
+ );
319
+ more.hidden = visible.length <= shown;
320
+ };
321
+
322
+ note.textContent = clockNote(store);
323
+ render();
324
+ return {
325
+ element: h(
326
+ "div",
327
+ { class: "page recent" },
328
+ h("h1", {}, "Recent"),
329
+ h(
330
+ "p",
331
+ { class: "hint" },
332
+ "The app server's recent window: what the program reported, its traced writes when the trace is on, and the requests it made, with the agent's deployments and audited commands. History lives in the app's telemetry sink, such as PostHog.",
333
+ ),
334
+ h(
335
+ "div",
336
+ { class: "toolbar" },
337
+ h("span", { class: "dim" }, "Source"),
338
+ ...toggles(SOURCES, hiddenSources),
339
+ h("span", { class: "dim sep" }, "Kind"),
340
+ ...toggles(KINDS, hiddenKinds),
341
+ ),
342
+ note,
343
+ list,
344
+ more,
345
+ ),
346
+ update(topics) {
347
+ if (topics.has("session")) {
348
+ note.textContent = clockNote(store);
349
+ }
350
+ if (topics.has("timeline")) {
351
+ render();
352
+ }
353
+ },
354
+ };
355
+ };
@@ -0,0 +1,38 @@
1
+ import type { RequestTrace } from "../api";
2
+ import { h } from "./dom";
3
+ import { isProgramJob } from "./format";
4
+ import { href } from "./routes";
5
+ import type { Store } from "./store";
6
+
7
+ /**
8
+ * A traced request's job, as a link: the user's job that awaits a compiler-made service job, which
9
+ * is the one whose id the request carries, or that job itself.
10
+ */
11
+ export const requestJobLink = (
12
+ store: Store,
13
+ id: number,
14
+ sequence: number | undefined,
15
+ ) => {
16
+ const { jobs } = store.manifest;
17
+ const job =
18
+ jobs.find(
19
+ (job) =>
20
+ isProgramJob(job) && job.requests.some((call) => call.job === id),
21
+ ) ?? jobs.find((job) => job.id === id);
22
+ return h(
23
+ "a",
24
+ { href: href({ route: "jobs", id: job?.id ?? id }) },
25
+ job
26
+ ? `${job.name}${job.instance ? ` · ${job.instance}` : ""}`
27
+ : `job ${id}`,
28
+ ` #${sequence ?? "?"}`,
29
+ );
30
+ };
31
+
32
+ export const requestStatus = (trace: RequestTrace) =>
33
+ h(
34
+ "span",
35
+ { class: `st st-${trace.status}` },
36
+ trace.status,
37
+ trace.httpStatus === undefined ? "" : ` ${trace.httpStatus}`,
38
+ );
@@ -0,0 +1,80 @@
1
+ import { empty, h, table, type View } from "./dom";
2
+ import { formatClock, formatDuration } from "./format";
3
+ import { requestJobLink, requestStatus } from "./request-trace";
4
+ import type { Place } from "./routes";
5
+ import type { Store } from "./store";
6
+
7
+ const LIMIT = 500;
8
+
9
+ export const requestsView = (
10
+ store: Store,
11
+ { seq: highlighted }: Extract<Place, { route: "requests" }>,
12
+ ): View => {
13
+ const body = h("div");
14
+ const render = () => {
15
+ const requests = store.timeline
16
+ .flatMap((entry) => (entry.kind === "request" ? [entry.request] : []))
17
+ .slice(-LIMIT)
18
+ .reverse();
19
+ // A restarted app server numbers its requests from 1 again, so a link's number may name an
20
+ // older request too; it means the newest.
21
+ const linked = requests.find((request) => request.seq === highlighted);
22
+ body.replaceChildren(
23
+ requests.length
24
+ ? table(
25
+ ["#", "Started", "Service", "Status", "Duration", "Job", "Error"],
26
+ h(
27
+ "tbody",
28
+ {},
29
+ ...requests.map((request) =>
30
+ h(
31
+ "tr",
32
+ {
33
+ class: request === linked ? "highlight" : undefined,
34
+ },
35
+ h("td", { class: "mono num" }, String(request.seq)),
36
+ h("td", { class: "mono" }, formatClock(request.startedAt)),
37
+ h("td", { class: "mono" }, request.service),
38
+ h("td", {}, requestStatus(request)),
39
+ h(
40
+ "td",
41
+ { class: "mono num" },
42
+ request.durationMs === undefined
43
+ ? "—"
44
+ : formatDuration(request.durationMs),
45
+ ),
46
+ h(
47
+ "td",
48
+ {},
49
+ request.job === undefined
50
+ ? h("span", { class: "dim" }, "not from a job")
51
+ : requestJobLink(store, request.job, request.sequence),
52
+ ),
53
+ h("td", { class: "err" }, request.error ?? ""),
54
+ ),
55
+ ),
56
+ ),
57
+ )
58
+ : empty("No service requests recorded yet."),
59
+ );
60
+ };
61
+ render();
62
+ return {
63
+ element: h(
64
+ "div",
65
+ { class: "page" },
66
+ h("h1", {}, "Requests"),
67
+ h(
68
+ "p",
69
+ { class: "hint" },
70
+ `Service calls the app server traced, newest first (at most ${LIMIT}).`,
71
+ ),
72
+ body,
73
+ ),
74
+ update(topics) {
75
+ if (topics.has("timeline")) {
76
+ render();
77
+ }
78
+ },
79
+ };
80
+ };
@@ -0,0 +1,98 @@
1
+ export const ROUTES = [
2
+ "overview",
3
+ "variables",
4
+ "hardware",
5
+ "jobs",
6
+ "recent",
7
+ "requests",
8
+ "why",
9
+ ] as const;
10
+ export type Route = (typeof ROUTES)[number];
11
+
12
+ export const jobFilters = ["all", "busy", "failed"] as const;
13
+ export type JobFilter = (typeof jobFilters)[number];
14
+
15
+ /** A page and what it is showing, as the hash `#/<route>/<rest>?<params>` names it. */
16
+ export type Place =
17
+ | { route: "overview" | "recent" }
18
+ | { route: "variables"; path?: string }
19
+ | { route: "hardware"; device?: string }
20
+ | { route: "jobs"; id?: number; filter?: JobFilter }
21
+ | { route: "requests"; seq?: number }
22
+ | { route: "why"; path?: string; trail?: readonly string[] };
23
+
24
+ const integer = (text: string | null) => {
25
+ const value = Number(text ?? Number.NaN);
26
+ return Number.isInteger(value) ? value : undefined;
27
+ };
28
+
29
+ export const parsePlace = (hash: string): Place => {
30
+ const [path = "", query = ""] = hash.replace(/^#\/?/, "").split("?");
31
+ const [name, ...rest] = path.split("/");
32
+ const params = new URLSearchParams(query);
33
+ const text = (key: string) => params.get(key) ?? undefined;
34
+ const route = ROUTES.find((candidate) => candidate === name) ?? "overview";
35
+ switch (route) {
36
+ case "overview":
37
+ case "recent":
38
+ return { route };
39
+ case "variables":
40
+ return { route, path: text("path") };
41
+ case "hardware": {
42
+ const [device] = rest;
43
+ return {
44
+ route,
45
+ device: device === undefined ? undefined : decodeURIComponent(device),
46
+ };
47
+ }
48
+ case "jobs":
49
+ return {
50
+ route,
51
+ id: integer(params.get("id")),
52
+ filter: jobFilters.find((filter) => filter === params.get("filter")),
53
+ };
54
+ case "requests":
55
+ return { route, seq: integer(params.get("seq")) };
56
+ case "why":
57
+ return {
58
+ route,
59
+ path: text("path"),
60
+ trail: (params.get("trail") ?? "").split(",").filter(Boolean),
61
+ };
62
+ }
63
+ };
64
+
65
+ const withParams = (
66
+ route: string,
67
+ params: Record<string, string | number | undefined>,
68
+ ) => {
69
+ const query = new URLSearchParams(
70
+ Object.entries(params).flatMap(([key, value]) =>
71
+ value === undefined ? [] : [[key, String(value)]],
72
+ ),
73
+ ).toString();
74
+ return `#/${route}${query ? `?${query}` : ""}`;
75
+ };
76
+
77
+ export const href = (place: Place): string => {
78
+ switch (place.route) {
79
+ case "overview":
80
+ case "recent":
81
+ return `#/${place.route}`;
82
+ case "variables":
83
+ return withParams(place.route, { path: place.path });
84
+ case "hardware":
85
+ return place.device === undefined
86
+ ? `#/${place.route}`
87
+ : `#/${place.route}/${encodeURIComponent(place.device)}`;
88
+ case "jobs":
89
+ return withParams(place.route, { id: place.id, filter: place.filter });
90
+ case "requests":
91
+ return withParams(place.route, { seq: place.seq });
92
+ case "why":
93
+ return withParams(place.route, {
94
+ path: place.path,
95
+ trail: place.trail?.length ? place.trail.join(",") : undefined,
96
+ });
97
+ }
98
+ };