@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.
- package/README.md +38 -2
- package/dist/api.js +744 -0
- package/dist/api.js.map +12 -0
- package/dist/constant.js +33 -0
- package/dist/constant.js.map +10 -0
- package/dist/explain-file.js +468 -0
- package/dist/explain-file.js.map +11 -0
- package/dist/explain.js +1310 -0
- package/dist/explain.js.map +19 -0
- package/dist/format.js +214 -0
- package/dist/format.js.map +12 -0
- package/dist/index.js +4066 -0
- package/dist/index.js.map +44 -0
- package/dist/manifest.js +240 -0
- package/dist/manifest.js.map +10 -0
- package/dist/paths.js +242 -0
- package/dist/paths.js.map +10 -0
- package/package.json +72 -4
- package/src/adapter.ts +77 -0
- package/src/api.ts +446 -0
- package/src/app-server.ts +151 -0
- package/src/build-files.ts +46 -0
- package/src/constant.ts +41 -0
- package/src/debugger.ts +237 -0
- package/src/errors.ts +13 -0
- package/src/explain-expressions.ts +484 -0
- package/src/explain-file.ts +325 -0
- package/src/explain-frames.ts +155 -0
- package/src/explain-loops.ts +151 -0
- package/src/explain-operations.ts +232 -0
- package/src/explain-tables.ts +345 -0
- package/src/explain-value.ts +203 -0
- package/src/explain-verdicts.ts +548 -0
- package/src/explain.ts +82 -0
- package/src/hardware.ts +159 -0
- package/src/http.ts +218 -0
- package/src/index.ts +44 -0
- package/src/jobs.ts +139 -0
- package/src/login-value.ts +165 -0
- package/src/login.ts +315 -0
- package/src/manifest.ts +328 -0
- package/src/opcua.ts +80 -0
- package/src/paths.ts +363 -0
- package/src/requests.ts +99 -0
- package/src/server-cursor.ts +22 -0
- package/src/shown.ts +229 -0
- package/src/telemetry.ts +125 -0
- package/src/timeline.ts +148 -0
- package/src/trace.ts +62 -0
- package/src/ui/bits.ts +48 -0
- package/src/ui/client.ts +87 -0
- package/src/ui/dom.ts +61 -0
- package/src/ui/explain-read.ts +105 -0
- package/src/ui/format.ts +261 -0
- package/src/ui/hardware.ts +425 -0
- package/src/ui/header.ts +207 -0
- package/src/ui/health.ts +64 -0
- package/src/ui/index.ts +27 -0
- package/src/ui/jobs.ts +263 -0
- package/src/ui/links.ts +59 -0
- package/src/ui/live-value.ts +85 -0
- package/src/ui/main.ts +76 -0
- package/src/ui/overview.ts +178 -0
- package/src/ui/page.ts +286 -0
- package/src/ui/pages.ts +64 -0
- package/src/ui/recent-window.ts +49 -0
- package/src/ui/recent.ts +355 -0
- package/src/ui/request-trace.ts +38 -0
- package/src/ui/requests.ts +80 -0
- package/src/ui/routes.ts +98 -0
- package/src/ui/store.ts +295 -0
- package/src/ui/success-cache.ts +11 -0
- package/src/ui/tree.ts +162 -0
- package/src/ui/variables.ts +597 -0
- package/src/ui/why-watch.ts +11 -0
- package/src/ui/why.ts +694 -0
- package/src/values.ts +170 -0
- package/src/watch.ts +32 -0
package/src/telemetry.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
TelemetryBatch,
|
|
3
|
+
TelemetryRecord,
|
|
4
|
+
TelemetryReply,
|
|
5
|
+
} from "@tsplc/server/telemetry";
|
|
6
|
+
import { recordedAt } from "@tsplc/server/telemetry-log";
|
|
7
|
+
import type { Session } from "./api";
|
|
8
|
+
import type { DebugManifest } from "./manifest";
|
|
9
|
+
import { createServerCursor } from "./server-cursor";
|
|
10
|
+
|
|
11
|
+
/** Clock estimates are kept this long, on the server's clock. */
|
|
12
|
+
const clockWindowMs = 60_000;
|
|
13
|
+
|
|
14
|
+
/** A record with the batch it came in, numbered in its task's sequence, at its time on the server's clock. */
|
|
15
|
+
export interface ReceivedRecord {
|
|
16
|
+
build: string;
|
|
17
|
+
task: string;
|
|
18
|
+
sequence: number;
|
|
19
|
+
at: string;
|
|
20
|
+
record: TelemetryRecord;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Follows the app server's telemetry ring (`GET /tsplc/telemetry`): it hands out each new record
|
|
25
|
+
* once, keeps the tasks' scan rates, and estimates the PLC's clock against the server's.
|
|
26
|
+
*/
|
|
27
|
+
export const createTelemetryFollower = (manifest: DebugManifest) => {
|
|
28
|
+
const intervals = new Map(
|
|
29
|
+
manifest.tasks.map(({ name, intervalMs }) => [name, intervalMs]),
|
|
30
|
+
);
|
|
31
|
+
const tasks = new Map<string, Session["tasks"][number]>(
|
|
32
|
+
manifest.tasks.map(({ name }) => [
|
|
33
|
+
name,
|
|
34
|
+
{ name, scan: null, scansPerSecond: null, dropped: 0 },
|
|
35
|
+
]),
|
|
36
|
+
);
|
|
37
|
+
const rates = new Map<string, { scan: number; now: number }>();
|
|
38
|
+
let estimates: { offsetMs: number; intervalMs: number; at: number }[] = [];
|
|
39
|
+
const cursor = createServerCursor();
|
|
40
|
+
let build: string | null = null;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A batch's `now` was read before it was sent, so `now − receivedAt` falls short of the true
|
|
44
|
+
* offset by its transit time: the largest recent estimate is the closest, and their spread,
|
|
45
|
+
* plus a scan, bounds how far off it may be.
|
|
46
|
+
*/
|
|
47
|
+
const clock = (): Session["clock"] => {
|
|
48
|
+
if (!estimates.length) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
const offsets = estimates.map(({ offsetMs }) => offsetMs);
|
|
52
|
+
const best = estimates.reduce((best, estimate) =>
|
|
53
|
+
estimate.offsetMs > best.offsetMs ? estimate : best,
|
|
54
|
+
);
|
|
55
|
+
return {
|
|
56
|
+
offsetMs: best.offsetMs,
|
|
57
|
+
uncertaintyMs: best.offsetMs - Math.min(...offsets) + best.intervalMs,
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const observe = (batch: TelemetryBatch, receivedAt: number) => {
|
|
62
|
+
const rate = rates.get(batch.task);
|
|
63
|
+
// The PLC's clock went back, by a restart or its wrap, so earlier offsets don't compare.
|
|
64
|
+
if (rate !== undefined && batch.now < rate.now) {
|
|
65
|
+
estimates = [];
|
|
66
|
+
}
|
|
67
|
+
estimates = estimates.filter(({ at }) => receivedAt - at < clockWindowMs);
|
|
68
|
+
estimates.push({
|
|
69
|
+
offsetMs: batch.now - receivedAt,
|
|
70
|
+
intervalMs: intervals.get(batch.task) ?? 0,
|
|
71
|
+
at: receivedAt,
|
|
72
|
+
});
|
|
73
|
+
const stats = {
|
|
74
|
+
name: batch.task,
|
|
75
|
+
scansPerSecond: tasks.get(batch.task)?.scansPerSecond ?? null,
|
|
76
|
+
scan: batch.scan,
|
|
77
|
+
dropped: batch.dropped,
|
|
78
|
+
};
|
|
79
|
+
// A restarted PLC counts scans from zero again. Its clock is a UDINT, so it wraps.
|
|
80
|
+
const restarted = !rate || batch.scan < rate.scan;
|
|
81
|
+
const elapsedMs = rate ? (batch.now - rate.now) >>> 0 : 0;
|
|
82
|
+
if (restarted) {
|
|
83
|
+
rates.set(batch.task, { scan: batch.scan, now: batch.now });
|
|
84
|
+
} else if (elapsedMs >= 1000) {
|
|
85
|
+
stats.scansPerSecond = ((batch.scan - rate.scan) * 1000) / elapsedMs;
|
|
86
|
+
rates.set(batch.task, { scan: batch.scan, now: batch.now });
|
|
87
|
+
}
|
|
88
|
+
tasks.set(batch.task, stats);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
/** Where the next pull starts. */
|
|
93
|
+
since: cursor.newest,
|
|
94
|
+
/** Takes a reply to `GET /tsplc/telemetry?since=<since()>`; a restarted server starts over. */
|
|
95
|
+
accept: (reply: TelemetryReply): ReceivedRecord[] => {
|
|
96
|
+
if (!cursor.continues(reply.latest)) {
|
|
97
|
+
return [];
|
|
98
|
+
}
|
|
99
|
+
const received: ReceivedRecord[] = [];
|
|
100
|
+
for (const batch of reply.batches) {
|
|
101
|
+
if (batch.seq <= cursor.newest()) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
cursor.take(batch.seq);
|
|
105
|
+
build = batch.build;
|
|
106
|
+
const receivedAt = new Date(batch.receivedAt);
|
|
107
|
+
observe(batch, receivedAt.getTime());
|
|
108
|
+
batch.records.forEach((record, index) => {
|
|
109
|
+
received.push({
|
|
110
|
+
build: batch.build,
|
|
111
|
+
task: batch.task,
|
|
112
|
+
sequence: batch.first + index,
|
|
113
|
+
at: recordedAt(batch, record, receivedAt).toISOString(),
|
|
114
|
+
record,
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return received;
|
|
119
|
+
},
|
|
120
|
+
/** The build the newest batch came from. */
|
|
121
|
+
build: () => build,
|
|
122
|
+
clock,
|
|
123
|
+
tasks: () => [...tasks.values()].map((task) => ({ ...task })),
|
|
124
|
+
};
|
|
125
|
+
};
|
package/src/timeline.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AppRecord,
|
|
3
|
+
ConnectionState,
|
|
4
|
+
RequestTrace,
|
|
5
|
+
TimelineEntry,
|
|
6
|
+
TraceEvent,
|
|
7
|
+
} from "./api";
|
|
8
|
+
|
|
9
|
+
export type Entry<Kind extends TimelineEntry["kind"]> = Extract<
|
|
10
|
+
TimelineEntry,
|
|
11
|
+
{ kind: Kind }
|
|
12
|
+
>;
|
|
13
|
+
type Details<Kind extends TimelineEntry["kind"]> = Omit<
|
|
14
|
+
Entry<Kind>,
|
|
15
|
+
"at" | "approximate" | "source" | "kind"
|
|
16
|
+
>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Timeline entries by source. The PLC's records are placed on the app server's clock from their
|
|
20
|
+
* age when the server received them, and the server keeps its own clock, so those are
|
|
21
|
+
* approximate. The agent's entries reach a debugger only when the agent hosts it, on the same clock.
|
|
22
|
+
*/
|
|
23
|
+
export const timelineEntries = {
|
|
24
|
+
app: (
|
|
25
|
+
at: string,
|
|
26
|
+
task: string,
|
|
27
|
+
sequence: number,
|
|
28
|
+
record: AppRecord,
|
|
29
|
+
): Entry<"app"> => ({
|
|
30
|
+
at,
|
|
31
|
+
approximate: true,
|
|
32
|
+
source: "plc",
|
|
33
|
+
kind: "app",
|
|
34
|
+
task,
|
|
35
|
+
sequence,
|
|
36
|
+
record,
|
|
37
|
+
}),
|
|
38
|
+
trace: (at: string, trace: TraceEvent): Entry<"trace"> => ({
|
|
39
|
+
at,
|
|
40
|
+
approximate: true,
|
|
41
|
+
source: "plc",
|
|
42
|
+
kind: "trace",
|
|
43
|
+
trace,
|
|
44
|
+
}),
|
|
45
|
+
request: (request: RequestTrace): Entry<"request"> => ({
|
|
46
|
+
at: request.startedAt,
|
|
47
|
+
approximate: true,
|
|
48
|
+
source: "server",
|
|
49
|
+
kind: "request",
|
|
50
|
+
request,
|
|
51
|
+
}),
|
|
52
|
+
deploy: (at: string, details: Details<"deploy">): Entry<"deploy"> => ({
|
|
53
|
+
at,
|
|
54
|
+
approximate: false,
|
|
55
|
+
source: "agent",
|
|
56
|
+
kind: "deploy",
|
|
57
|
+
...details,
|
|
58
|
+
}),
|
|
59
|
+
audit: (at: string, details: Details<"audit">): Entry<"audit"> => ({
|
|
60
|
+
at,
|
|
61
|
+
approximate: false,
|
|
62
|
+
source: "agent",
|
|
63
|
+
kind: "audit",
|
|
64
|
+
...details,
|
|
65
|
+
}),
|
|
66
|
+
connection: (
|
|
67
|
+
at: string,
|
|
68
|
+
state: ConnectionState,
|
|
69
|
+
error?: string,
|
|
70
|
+
): Entry<"connection"> => ({
|
|
71
|
+
at,
|
|
72
|
+
approximate: false,
|
|
73
|
+
source: "debugger",
|
|
74
|
+
kind: "connection",
|
|
75
|
+
state,
|
|
76
|
+
...(error && { error }),
|
|
77
|
+
}),
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Adds `entry` to `entries`, which are oldest first. Sources arrive late and out of order, so it
|
|
82
|
+
* goes after the last entry not later than it.
|
|
83
|
+
*/
|
|
84
|
+
export const insertInTimeOrder = <Placed extends { at: string }>(
|
|
85
|
+
entries: Placed[],
|
|
86
|
+
entry: Placed,
|
|
87
|
+
) => {
|
|
88
|
+
for (let index = entries.length; index >= 0; index--) {
|
|
89
|
+
const before = entries[index - 1];
|
|
90
|
+
if (before === undefined || before.at <= entry.at) {
|
|
91
|
+
entries.splice(index, 0, entry);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** `<host>.<taken>`: the timeline that issued it, and how many entries it had taken by then. */
|
|
98
|
+
const cursorPattern = /^([0-9a-f]{8})\.(\d+)$/;
|
|
99
|
+
|
|
100
|
+
export const isTimelineCursor = (text: string) => cursorPattern.test(text);
|
|
101
|
+
|
|
102
|
+
/** Where a client resumes the timeline: after the entry with `cursor` `after`, else after time `since`. */
|
|
103
|
+
export interface TimelineQuery {
|
|
104
|
+
since?: string;
|
|
105
|
+
after?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The most entries a timeline keeps; older ones make room. */
|
|
109
|
+
export const timelineLimit = 10_000;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Entries from every source in time order, keeping the newest `timelineLimit`. A cursor from
|
|
113
|
+
* another timeline, such as the one before a deploy or a restart, resumes from the start of this one.
|
|
114
|
+
*/
|
|
115
|
+
export const createTimeline = () => {
|
|
116
|
+
const host = crypto.randomUUID().slice(0, 8);
|
|
117
|
+
const entries: (TimelineEntry & { cursor: string })[] = [];
|
|
118
|
+
let taken = 0;
|
|
119
|
+
const takenAt = (cursor: string) => {
|
|
120
|
+
const [, issuer, count] = cursorPattern.exec(cursor) ?? [];
|
|
121
|
+
return issuer === host ? Number(count) : 0;
|
|
122
|
+
};
|
|
123
|
+
const stamp = (entry: TimelineEntry) => {
|
|
124
|
+
taken += 1;
|
|
125
|
+
return Object.assign(entry, { cursor: `${host}.${taken}` });
|
|
126
|
+
};
|
|
127
|
+
return {
|
|
128
|
+
/** Stamps `entry` with its `cursor` in place, because the request follower changes an entry after adding it. */
|
|
129
|
+
add: (entry: TimelineEntry) => {
|
|
130
|
+
const stamped = stamp(entry);
|
|
131
|
+
insertInTimeOrder(entries, stamped);
|
|
132
|
+
if (entries.length > timelineLimit) {
|
|
133
|
+
entries.splice(0, entries.length - timelineLimit);
|
|
134
|
+
}
|
|
135
|
+
return stamped;
|
|
136
|
+
},
|
|
137
|
+
/** Gives an added entry that has since changed the next cursor, so a client resuming after an earlier one receives the change. */
|
|
138
|
+
changed: stamp,
|
|
139
|
+
/** In time order; all of them without a query. */
|
|
140
|
+
since: ({ since, after }: TimelineQuery) => {
|
|
141
|
+
if (after !== undefined) {
|
|
142
|
+
const resumed = takenAt(after);
|
|
143
|
+
return entries.filter((entry) => takenAt(entry.cursor) > resumed);
|
|
144
|
+
}
|
|
145
|
+
return since ? entries.filter((entry) => entry.at > since) : [...entries];
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
};
|
package/src/trace.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { TelemetryRecord } from "@tsplc/server/telemetry";
|
|
2
|
+
import type { TraceEvent } from "./api";
|
|
3
|
+
import type { DebugJob, DebugManifest } from "./manifest";
|
|
4
|
+
import type { Paths } from "./paths";
|
|
5
|
+
import type { ReceivedRecord } from "./telemetry";
|
|
6
|
+
|
|
7
|
+
export type TraceRecord = Extract<TelemetryRecord, { kind: "trace" }>;
|
|
8
|
+
|
|
9
|
+
export type ReceivedTrace = Omit<ReceivedRecord, "record"> & {
|
|
10
|
+
record: TraceRecord;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Decodes the opt-in trace through the manifest's event sites (docs/03-guides/debugging.md). Records
|
|
15
|
+
* of another build decode to nothing: its site ids mean other code.
|
|
16
|
+
*/
|
|
17
|
+
export const createTraceDecoder = (manifest: DebugManifest, paths: Paths) => {
|
|
18
|
+
const sites = new Map(manifest.events.map((site) => [site.id, site]));
|
|
19
|
+
const jobs = new Map(manifest.jobs.map((job) => [job.id, job]));
|
|
20
|
+
const jobSites = new Map(
|
|
21
|
+
manifest.events.flatMap(({ id, job: jobId, phase }) => {
|
|
22
|
+
const job = jobId === undefined ? undefined : jobs.get(jobId);
|
|
23
|
+
return job && phase
|
|
24
|
+
? [[id, { job, started: phase === "started" }] as const]
|
|
25
|
+
: [];
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
const ours = ({ build, record }: ReceivedTrace) =>
|
|
29
|
+
build === manifest.build.id ? record : undefined;
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
decode: (received: ReceivedTrace): TraceEvent | undefined => {
|
|
33
|
+
const record = ours(received);
|
|
34
|
+
const site = record && sites.get(record.site);
|
|
35
|
+
if (!record || !site) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
const { task, sequence } = received;
|
|
39
|
+
return {
|
|
40
|
+
sequence,
|
|
41
|
+
task,
|
|
42
|
+
scan: record.scan,
|
|
43
|
+
plcTime: record.time,
|
|
44
|
+
site: site.id,
|
|
45
|
+
value: paths.decode(site.type, record.value),
|
|
46
|
+
operands: site.operands.map(({ text, path, stable, type }, index) => ({
|
|
47
|
+
text,
|
|
48
|
+
...(path && { path }),
|
|
49
|
+
stable,
|
|
50
|
+
value: paths.decode(type, record.operands[index]),
|
|
51
|
+
})),
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
/** The job a record starts or finishes, if its site is a job's. */
|
|
55
|
+
job: (
|
|
56
|
+
received: ReceivedTrace,
|
|
57
|
+
): { job: DebugJob; started: boolean } | undefined => {
|
|
58
|
+
const record = ours(received);
|
|
59
|
+
return record && jobSites.get(record.site);
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
};
|
package/src/ui/bits.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { DebugChannel, DebugIo } from "../manifest";
|
|
2
|
+
|
|
3
|
+
/** An unsigned little-endian field of an assembly, as EtherNet/IP lays them out; null past 32 bits or the end. */
|
|
4
|
+
export const readBits = (
|
|
5
|
+
bytes: number[],
|
|
6
|
+
offsetBits: number,
|
|
7
|
+
bits: number,
|
|
8
|
+
): number | null => {
|
|
9
|
+
if (bits > 32 || offsetBits + bits > bytes.length * 8) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
let value = 0;
|
|
13
|
+
for (let bit = 0; bit < bits; bit++) {
|
|
14
|
+
const at = offsetBits + bit;
|
|
15
|
+
if (((bytes[at >> 3] ?? 0) >> (at & 7)) & 1) {
|
|
16
|
+
value += 2 ** bit;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
interface MappedBit {
|
|
23
|
+
channel: DebugChannel;
|
|
24
|
+
paths: string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** What each bit of one assembly carries: the channel covering it and the TypeScript paths bound to that channel. */
|
|
28
|
+
export const mapBits = (
|
|
29
|
+
bytes: number,
|
|
30
|
+
channels: readonly DebugChannel[],
|
|
31
|
+
bound: ReadonlyMap<string, readonly DebugIo[]>,
|
|
32
|
+
): (MappedBit | undefined)[] => {
|
|
33
|
+
const bits: (MappedBit | undefined)[] = Array.from(
|
|
34
|
+
{ length: bytes * 8 },
|
|
35
|
+
() => undefined,
|
|
36
|
+
);
|
|
37
|
+
for (const channel of channels) {
|
|
38
|
+
const paths = (bound.get(channel.id) ?? []).map((entry) => entry.path);
|
|
39
|
+
for (
|
|
40
|
+
let bit = channel.offsetBits;
|
|
41
|
+
bit < channel.offsetBits + channel.bits && bit < bits.length;
|
|
42
|
+
bit++
|
|
43
|
+
) {
|
|
44
|
+
bits[bit] = { channel, paths };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return bits;
|
|
48
|
+
};
|
package/src/ui/client.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import {
|
|
3
|
+
type ApiResponses,
|
|
4
|
+
apiResponseSchemas,
|
|
5
|
+
dashboardHeader,
|
|
6
|
+
errorBodySchema,
|
|
7
|
+
tokenQueryParameter,
|
|
8
|
+
} from "../api";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The debugger API, by relative URL so the UI works under the agent's `/debug` prefix. A token on the
|
|
12
|
+
* page is passed on to every call, since EventSource cannot send headers.
|
|
13
|
+
*/
|
|
14
|
+
const token = new URLSearchParams(location.search).get(tokenQueryParameter);
|
|
15
|
+
|
|
16
|
+
type Method = "GET" | "PUT";
|
|
17
|
+
type RouteOf<M extends Method> = {
|
|
18
|
+
[K in keyof ApiResponses]: K extends `${M} /api/${infer Route}`
|
|
19
|
+
? Route
|
|
20
|
+
: never;
|
|
21
|
+
}[keyof ApiResponses];
|
|
22
|
+
|
|
23
|
+
/** Each route's schema typed by its response, so a lookup by a generic route keeps its type. */
|
|
24
|
+
const schemas: { [Key in keyof ApiResponses]: z.ZodType<ApiResponses[Key]> } =
|
|
25
|
+
apiResponseSchemas;
|
|
26
|
+
|
|
27
|
+
export const apiUrl = (route: string, params: Record<string, string> = {}) => {
|
|
28
|
+
const search = new URLSearchParams(params);
|
|
29
|
+
if (token) {
|
|
30
|
+
search.set(tokenQueryParameter, token);
|
|
31
|
+
}
|
|
32
|
+
const query = search.toString();
|
|
33
|
+
return `./api/${route}${query ? `?${query}` : ""}`;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** A response's body as `key` describes it, or the error the host replied with. */
|
|
37
|
+
const receive = async <Key extends keyof ApiResponses>(
|
|
38
|
+
key: Key,
|
|
39
|
+
call: string,
|
|
40
|
+
response: Response,
|
|
41
|
+
): Promise<ApiResponses[Key]> => {
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
const detail = response.headers.get("Content-Type")?.includes("json")
|
|
44
|
+
? errorBodySchema.parse(await response.json()).error
|
|
45
|
+
: await response.text();
|
|
46
|
+
throw new Error(`${call}: ${response.status} ${detail}`);
|
|
47
|
+
}
|
|
48
|
+
const parsed = schemas[key].safeParse(await response.json());
|
|
49
|
+
if (!parsed.success) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`${call}: the debugger sent a malformed response. ${z.prettifyError(parsed.error)}`,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
return parsed.data;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const getJson = async <R extends RouteOf<"GET">>(
|
|
58
|
+
route: R,
|
|
59
|
+
params: Record<string, string> = {},
|
|
60
|
+
) =>
|
|
61
|
+
receive(
|
|
62
|
+
`GET /api/${route}` as const,
|
|
63
|
+
`GET ${route}`,
|
|
64
|
+
await fetch(apiUrl(route, params)),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
/** As `getJson`, with null for a 404: the host has no such resource, such as a build's explanations. */
|
|
68
|
+
export const findJson = async <R extends RouteOf<"GET">>(route: R) => {
|
|
69
|
+
const response = await fetch(apiUrl(route));
|
|
70
|
+
return response.status === 404
|
|
71
|
+
? null
|
|
72
|
+
: receive(`GET /api/${route}` as const, `GET ${route}`, response);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export const putJson = async <R extends RouteOf<"PUT">>(
|
|
76
|
+
route: R,
|
|
77
|
+
body: unknown,
|
|
78
|
+
) =>
|
|
79
|
+
receive(
|
|
80
|
+
`PUT /api/${route}` as const,
|
|
81
|
+
`PUT ${route}`,
|
|
82
|
+
await fetch(apiUrl(route), {
|
|
83
|
+
method: "PUT",
|
|
84
|
+
headers: { [dashboardHeader]: "1", "Content-Type": "application/json" },
|
|
85
|
+
body: JSON.stringify(body),
|
|
86
|
+
}),
|
|
87
|
+
);
|
package/src/ui/dom.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Topic } from "./store";
|
|
2
|
+
|
|
3
|
+
type Child = Node | string | null | undefined | false;
|
|
4
|
+
type Attribute =
|
|
5
|
+
| string
|
|
6
|
+
| number
|
|
7
|
+
| boolean
|
|
8
|
+
| undefined
|
|
9
|
+
| ((event: Event) => void);
|
|
10
|
+
|
|
11
|
+
export const h = <K extends keyof HTMLElementTagNameMap>(
|
|
12
|
+
tag: K,
|
|
13
|
+
attributes: Record<string, Attribute> = {},
|
|
14
|
+
...children: Child[]
|
|
15
|
+
): HTMLElementTagNameMap[K] => {
|
|
16
|
+
const element = document.createElement(tag);
|
|
17
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
18
|
+
if (value === undefined || value === false) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (typeof value === "function") {
|
|
22
|
+
element.addEventListener(name.slice(2), value);
|
|
23
|
+
} else {
|
|
24
|
+
element.setAttribute(name, value === true ? "" : String(value));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return fill(element, ...children);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Replaces an element's children, skipping the absent ones. */
|
|
31
|
+
export const fill = <E extends Element>(element: E, ...children: Child[]) => {
|
|
32
|
+
element.replaceChildren(
|
|
33
|
+
...children.filter((child): child is Node | string => Boolean(child)),
|
|
34
|
+
);
|
|
35
|
+
return element;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** A page: built once per route, then told which store topics changed each frame. */
|
|
39
|
+
export interface View {
|
|
40
|
+
element: HTMLElement;
|
|
41
|
+
update(topics: Set<Topic>): void;
|
|
42
|
+
dispose?(): void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const empty = (text: string) => h("p", { class: "empty" }, text);
|
|
46
|
+
|
|
47
|
+
export const table = (columns: string[], body: HTMLElement) =>
|
|
48
|
+
h(
|
|
49
|
+
"table",
|
|
50
|
+
{ class: "grid" },
|
|
51
|
+
h("thead", {}, h("tr", {}, ...columns.map((label) => h("th", {}, label)))),
|
|
52
|
+
body,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
/** A path that wraps only between its segments. */
|
|
56
|
+
export const wrappingPath = (path: string) =>
|
|
57
|
+
h(
|
|
58
|
+
"span",
|
|
59
|
+
{},
|
|
60
|
+
...path.split(/(?=[.[])/).flatMap((segment) => [h("wbr"), segment]),
|
|
61
|
+
);
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { elementaryType } from "@tsplc/iec";
|
|
2
|
+
import { applyIntegerPolicy } from "@tsplc/iec/arithmetic";
|
|
3
|
+
import { clockSample, type JobState, type Sample, slotSample } from "../api";
|
|
4
|
+
import type { ExplainRead } from "../explain";
|
|
5
|
+
import {
|
|
6
|
+
type DebugManifest,
|
|
7
|
+
neverWrittenSlot,
|
|
8
|
+
scanClockType,
|
|
9
|
+
} from "../manifest";
|
|
10
|
+
|
|
11
|
+
const clockType = elementaryType(scanClockType);
|
|
12
|
+
|
|
13
|
+
/** The `writers` entry whose slot records who last wrote `path`, if the build has one. */
|
|
14
|
+
export const writerIndex = (manifest: DebugManifest, path: string) => {
|
|
15
|
+
const index = manifest.writers.findIndex((writer) => writer.output === path);
|
|
16
|
+
return index < 0 ? undefined : index;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* What an explanation of `target` reads from the stream: the values of the controller read that
|
|
21
|
+
* last returned `target`, since every value an evaluation uses must come from one scan. Samples
|
|
22
|
+
* without a read number come from an adapter that doesn't read in batches, and all count.
|
|
23
|
+
*/
|
|
24
|
+
export const explainRead = ({
|
|
25
|
+
manifest,
|
|
26
|
+
samples,
|
|
27
|
+
jobs,
|
|
28
|
+
forced,
|
|
29
|
+
target,
|
|
30
|
+
}: {
|
|
31
|
+
manifest: DebugManifest;
|
|
32
|
+
samples: ReadonlyMap<string, Sample>;
|
|
33
|
+
jobs: ReadonlyMap<number, JobState>;
|
|
34
|
+
forced: readonly string[] | null;
|
|
35
|
+
target: string;
|
|
36
|
+
}): ExplainRead => {
|
|
37
|
+
const latest = samples.get(target)?.read;
|
|
38
|
+
const inRead = (path: string) => {
|
|
39
|
+
const sample = samples.get(path);
|
|
40
|
+
return sample && (latest === undefined || sample.read === latest)
|
|
41
|
+
? sample
|
|
42
|
+
: undefined;
|
|
43
|
+
};
|
|
44
|
+
const good = (path: string) => {
|
|
45
|
+
const value = inRead(path);
|
|
46
|
+
return value?.quality === "good" ? value.value : undefined;
|
|
47
|
+
};
|
|
48
|
+
const intervalOf = (task: string) =>
|
|
49
|
+
BigInt(
|
|
50
|
+
Math.ceil(
|
|
51
|
+
manifest.tasks.find((item) => item.name === task)?.intervalMs ?? 0,
|
|
52
|
+
),
|
|
53
|
+
);
|
|
54
|
+
const clockOf = (task: string) => {
|
|
55
|
+
const value = good(clockSample(task));
|
|
56
|
+
if (typeof value !== "number") {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
return applyIntegerPolicy(BigInt(value), clockType, "wrap");
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
sample: (path) => {
|
|
63
|
+
const sample = inRead(path);
|
|
64
|
+
return sample && { value: sample.value, quality: sample.quality };
|
|
65
|
+
},
|
|
66
|
+
job: (id) => jobs.get(id),
|
|
67
|
+
// A task reads its own clock at the start of its scan; another task's is within both intervals.
|
|
68
|
+
clock: (task) => {
|
|
69
|
+
const own = clockOf(task);
|
|
70
|
+
if (own !== undefined) {
|
|
71
|
+
return { low: own, high: own + intervalOf(task) };
|
|
72
|
+
}
|
|
73
|
+
for (const other of manifest.tasks) {
|
|
74
|
+
const now = clockOf(other.name);
|
|
75
|
+
if (now !== undefined) {
|
|
76
|
+
return {
|
|
77
|
+
low: now - intervalOf(task),
|
|
78
|
+
high: now + intervalOf(other.name),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
},
|
|
84
|
+
forced: forced && new Set(forced),
|
|
85
|
+
slot: (path) => {
|
|
86
|
+
const index = writerIndex(manifest, path);
|
|
87
|
+
if (index === undefined) {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
const writer = manifest.writers[index];
|
|
91
|
+
const value = good(slotSample(index));
|
|
92
|
+
if (!writer || typeof value !== "number") {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
assignments:
|
|
97
|
+
value === neverWrittenSlot
|
|
98
|
+
? null
|
|
99
|
+
: (writer.sites.find((site) => site.id === value)?.assignments ??
|
|
100
|
+
[]),
|
|
101
|
+
exact: writer.exact === true,
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
};
|