@tinkered/devtools 0.0.0-stage → 0.34.6

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 CHANGED
@@ -1,3 +1,99 @@
1
- # Temporary Holding Version
1
+ # @tinkered/devtools
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Devtools keeps a record of what a scope did.
4
+ A tool reads the record as one plain object.
5
+
6
+ It is a plain capsule.
7
+ You add it to a scope's `capsules`.
8
+ The scope lists it under the label `tinker.devtools`.
9
+ There is no global hook.
10
+
11
+ ```ts
12
+ const dt = devtools();
13
+ const scope = createScope({ capsules: [dt] });
14
+ dt.snapshot();
15
+ ```
16
+
17
+ ## One devtools per scope
18
+
19
+ Each call to `devtools()` makes a new capsule with its own state.
20
+ So give each scope its own call.
21
+
22
+ ## Options
23
+
24
+ - `limit`: the size of the span ring and the session list.
25
+ The default is 1000.
26
+ - `values`: keep the last value of each write.
27
+ The default is off.
28
+ With it off, a write shows only its label and count.
29
+
30
+ ## The snapshot
31
+
32
+ `dt.snapshot()` gives a fresh copy each call.
33
+ Changing the copy changes nothing that devtools keeps.
34
+
35
+ - `scope`: the scope's state, and its status once closed.
36
+ The state goes `starting`, `ready`, `closing`, then `closed`.
37
+ Before the scope starts, the state is `starting`.
38
+ - `sessions`: each session, by id, in the order it opened.
39
+ Its `runs` are the spans opened in it.
40
+ - `runs`: the spans opened outside any session.
41
+ - `resources`: the resource spans, in any session.
42
+ - `writes`: the count of writes to each cell label.
43
+ - `exits`: the count of each exit, keyed `label<tab>exit`.
44
+
45
+ A snapshot span holds only span fields.
46
+ Its `traceId` and `spanId` are W3C trace ids.
47
+ Each span keeps its attributes and its events.
48
+ A span that did not fail has no `error` key.
49
+ No key of the scope, a session, or a write is set to `undefined`.
50
+
51
+ The snapshot is plain JSON.
52
+ A JSON round trip gives the same object.
53
+
54
+ The span ring keeps the newest `limit` spans.
55
+ The exit counts keep every run, even after a span leaves the ring.
56
+
57
+ ## Sessions
58
+
59
+ - The session list keeps at most `limit` sessions.
60
+ - It drops the oldest closed session first.
61
+ - With no closed session, it drops the oldest open one.
62
+ - A span of a dropped session leaves the snapshot.
63
+ - A session is `open` while it runs.
64
+ Then it is `closed`, with its status.
65
+ - A session whose function throws ends `failed`.
66
+
67
+ ## Exits
68
+
69
+ - An operation that answers a `kind` counts as `label<tab>kind`.
70
+ - A refusal counts as `label<tab>refused`.
71
+ - A plain value counts as `label<tab>value`.
72
+ - A failed run counts no exit.
73
+
74
+ ## Failures
75
+
76
+ - A resource whose factory throws leaves a `failed` span in `resources`.
77
+ - A failed operation keeps its error as text, such as `Error: boom`.
78
+ - A thrown value that is not an `Error` is kept as its JSON, such as `"nope"`.
79
+ - If JSON cannot write it, such as a BigInt, it is kept as its type, such as `bigint`.
80
+
81
+ ## Writes
82
+
83
+ - A write still lands in the cell.
84
+ - Devtools counts it by the cell label.
85
+
86
+ ## Listeners
87
+
88
+ `dt.moves(listener)` calls `listener` once for each new record.
89
+ A record is a span, a session that opens or closes, a scope state change, or a write.
90
+ It returns a function. Call it to stop the listener.
91
+
92
+ A listener that throws does not stop the run.
93
+ Its error is rethrown in a microtask, so the host sees it.
94
+
95
+ ## Not in it yet
96
+
97
+ - The CLI command `tinker state`.
98
+ - An IndexedDB span log.
99
+ - A React panel.
@@ -0,0 +1,56 @@
1
+ import { Observe, Scope } from "@tinkered/core";
2
+ //#region src/index.d.ts
3
+ /** What devtools keeps and what a tool reads from it. */
4
+ export declare namespace Devtools {
5
+ /**
6
+ * `limit` sizes the span ring and the session list (default 1000). `values` keeps the last
7
+ * value of each write (default off: the snapshot then holds no value).
8
+ */
9
+ type Options = {
10
+ readonly limit?: number;
11
+ readonly values?: boolean;
12
+ };
13
+ /** How a scope or a session ended: Core's own close status. */
14
+ type Status = Scope.Result["status"];
15
+ /** One session the capsule saw open. `runs` holds the spans opened in it. */
16
+ type Session = {
17
+ id: number;
18
+ state: "open" | "closed";
19
+ status?: Status;
20
+ runs: Observe.Span[];
21
+ };
22
+ /** The writes to one cell label. `value` is in it only when `values` is on. */
23
+ type Write = {
24
+ label: string;
25
+ count: number;
26
+ value?: unknown;
27
+ };
28
+ /**
29
+ * The state a tool reads. Each call gives fresh objects, so a caller cannot change what devtools
30
+ * keeps.
31
+ */
32
+ type Snapshot = {
33
+ scope: {
34
+ state: "starting" | "ready" | "closing" | "closed";
35
+ status?: Status;
36
+ };
37
+ sessions: Session[];
38
+ /** The spans opened outside any session, except resource spans. */
39
+ runs: Observe.Span[];
40
+ /** Every resource span, in any session. */
41
+ resources: Observe.Span[];
42
+ writes: Write[];
43
+ /** Keys are `label\texit`. The count lives outside the ring, so it keeps every exit. */
44
+ exits: Record<string, number>;
45
+ };
46
+ }
47
+ /**
48
+ * A capsule that keeps what Core tells it. Its state lives in each call, so give each scope its
49
+ * own `devtools()`. A `moves` listener that throws does not stop the others or the app: the error
50
+ * is rethrown in a microtask, as a DOM event listener's error is reported.
51
+ */
52
+ export declare function devtools(options?: Devtools.Options): import("@tinkered/core").Capsule.Handle<{}, {
53
+ readonly snapshot: () => Devtools.Snapshot;
54
+ readonly moves: (listener: () => void) => () => void;
55
+ }, void>;
56
+ //#endregion
package/dist/index.mjs ADDED
@@ -0,0 +1,196 @@
1
+ import { capsule } from "@tinkered/core";
2
+ //#region src/index.ts
3
+ /**
4
+ * A capsule that keeps what Core tells it. Its state lives in each call, so give each scope its
5
+ * own `devtools()`. A `moves` listener that throws does not stop the others or the app: the error
6
+ * is rethrown in a microtask, as a DOM event listener's error is reported.
7
+ */
8
+ function devtools(options) {
9
+ const limit = options?.limit ?? 1e3;
10
+ const values = options?.values ?? false;
11
+ const spans = [];
12
+ const sessions = /* @__PURE__ */ new Map();
13
+ const writes = /* @__PURE__ */ new Map();
14
+ const exits = /* @__PURE__ */ new Map();
15
+ const listeners = /* @__PURE__ */ new Set();
16
+ let scope = { state: "starting" };
17
+ /** Tell each listener of a new record. A listener removed during the loop is skipped; one added
18
+ * during the loop is also told. */
19
+ function notify() {
20
+ for (const listener of listeners) try {
21
+ listener();
22
+ } catch (error) {
23
+ queueMicrotask(() => {
24
+ throw error;
25
+ });
26
+ }
27
+ }
28
+ function keepSpan(span) {
29
+ spans.push(span);
30
+ if (spans.length > limit) spans.shift();
31
+ if (span.kind === "operation" && span.status === "ok") {
32
+ const exit = span.attributes["tinker.exit"];
33
+ const key = `${span.name}\t${typeof exit === "string" ? exit : "value"}`;
34
+ exits.set(key, (exits.get(key) ?? 0) + 1);
35
+ }
36
+ notify();
37
+ }
38
+ function setScope(state, status) {
39
+ scope = status === void 0 ? { state } : {
40
+ state,
41
+ status
42
+ };
43
+ notify();
44
+ }
45
+ function openSession(id) {
46
+ sessions.set(id, { state: "open" });
47
+ if (sessions.size > limit) dropOldestSession();
48
+ notify();
49
+ }
50
+ /** Drop the oldest closed session. If none is closed, drop the oldest open one, so the list
51
+ * stays at `limit`. */
52
+ function dropOldestSession() {
53
+ const [oldest] = sessions.keys();
54
+ for (const [id, line] of sessions) if (line.state === "closed") {
55
+ sessions.delete(id);
56
+ return;
57
+ }
58
+ sessions.delete(oldest);
59
+ }
60
+ /** A session the list dropped while it was open has no line left, so its close is skipped. */
61
+ function closeSession(id, status) {
62
+ const line = sessions.get(id);
63
+ if (line === void 0) return;
64
+ line.state = "closed";
65
+ line.status = status;
66
+ notify();
67
+ }
68
+ function countWrite(label, value) {
69
+ const count = (writes.get(label)?.count ?? 0) + 1;
70
+ writes.set(label, values ? {
71
+ count,
72
+ value
73
+ } : { count });
74
+ notify();
75
+ }
76
+ function snapshot() {
77
+ /** Keyed by `unknown`, so a span's `session.id` reads as is: no session has a key that is
78
+ * not a number, so a span with one is dropped, as is a span of a dropped session. */
79
+ const byId = /* @__PURE__ */ new Map();
80
+ const list = [];
81
+ for (const [id, line] of sessions) {
82
+ const session = {
83
+ id,
84
+ state: line.state,
85
+ ...line.status === void 0 ? {} : { status: line.status },
86
+ runs: []
87
+ };
88
+ list.push(session);
89
+ byId.set(id, session);
90
+ }
91
+ const runs = [];
92
+ const resources = [];
93
+ for (const span of spans) {
94
+ const copy = copySpan(span);
95
+ if (span.kind === "resource") {
96
+ resources.push(copy);
97
+ continue;
98
+ }
99
+ const id = span.attributes["session.id"];
100
+ if (id === void 0) runs.push(copy);
101
+ else byId.get(id)?.runs.push(copy);
102
+ }
103
+ return {
104
+ scope: { ...scope },
105
+ sessions: list,
106
+ runs,
107
+ resources,
108
+ writes: [...writes].map(([label, write]) => values ? {
109
+ label,
110
+ count: write.count,
111
+ value: write.value
112
+ } : {
113
+ label,
114
+ count: write.count
115
+ }),
116
+ exits: Object.fromEntries(exits)
117
+ };
118
+ }
119
+ function moves(listener) {
120
+ listeners.add(listener);
121
+ return () => {
122
+ listeners.delete(listener);
123
+ };
124
+ }
125
+ return capsule({
126
+ label: "tinker.devtools",
127
+ exports: {
128
+ snapshot,
129
+ moves
130
+ },
131
+ hooks: {
132
+ start: async ({ tap, next }) => {
133
+ setScope("starting");
134
+ tap({ export: keepSpan });
135
+ await next();
136
+ setScope("ready");
137
+ },
138
+ close: async ({ next }) => {
139
+ setScope("closing");
140
+ const result = await next();
141
+ setScope("closed", result.status);
142
+ return result;
143
+ },
144
+ session: async ({ handle, next }) => {
145
+ const id = handle.id;
146
+ if (id === void 0) return next();
147
+ openSession(id);
148
+ const result = await next();
149
+ closeSession(id, result.status);
150
+ return result;
151
+ },
152
+ write: ({ cell, value, next }) => {
153
+ next();
154
+ countWrite(cell.label, value);
155
+ }
156
+ }
157
+ });
158
+ }
159
+ /**
160
+ * A copy of a span, built field by field: Core's span is a class whose ids are getters and whose
161
+ * private fields must not leak. Attributes and events are copied one level down. `error` becomes
162
+ * text: an `Error` turns into `{}` in JSON, so a copy holds `String(error)`.
163
+ */
164
+ function copySpan(span) {
165
+ return {
166
+ id: span.id,
167
+ parentId: span.parentId,
168
+ traceId: span.traceId,
169
+ spanId: span.spanId,
170
+ parentSpanId: span.parentSpanId,
171
+ sampled: span.sampled,
172
+ name: span.name,
173
+ kind: span.kind,
174
+ start: span.start,
175
+ end: span.end,
176
+ status: span.status,
177
+ ...span.error === void 0 ? {} : { error: textOf(span.error) },
178
+ attributes: { ...span.attributes },
179
+ events: span.events.map((event) => ({
180
+ name: event.name,
181
+ time: event.time,
182
+ attributes: { ...event.attributes }
183
+ }))
184
+ };
185
+ }
186
+ /** A failure as text: an `Error` as `name: message`; anything else as its JSON, or its type. */
187
+ function textOf(error) {
188
+ if (error instanceof Error) return `${error.name}: ${error.message}`;
189
+ try {
190
+ return JSON.stringify(error) ?? typeof error;
191
+ } catch {
192
+ return typeof error;
193
+ }
194
+ }
195
+ //#endregion
196
+ export { devtools };
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "@tinkered/devtools",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.34.6",
4
+ "description": "Tinkered devtools: a plain capsule that keeps a scope's spans, sessions, exits and writes, and gives a snapshot a tool can read.",
5
+ "keywords": [
6
+ "tanstack-intent"
7
+ ],
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/lagz0ne/tinkered.git",
12
+ "directory": "packages/devtools"
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "skills"
17
+ ],
18
+ "type": "module",
19
+ "sideEffects": false,
20
+ "exports": {
21
+ ".": "./dist/index.mjs",
22
+ "./package.json": "./package.json"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "devDependencies": {
28
+ "@stryker-mutator/core": "^10.0.0",
29
+ "@stryker-mutator/vitest-runner": "^10.0.0",
30
+ "@tinkered/core": "^0.34.6",
31
+ "@types/node": "^26.1.1",
32
+ "vite-plus": "1.0.0",
33
+ "vitest": "5.0.1"
34
+ },
35
+ "peerDependencies": {
36
+ "@tinkered/core": "^0.34.6"
37
+ },
38
+ "engines": {
39
+ "bun": ">=1.4",
40
+ "node": ">=22.18.0"
41
+ },
42
+ "scripts": {
43
+ "build": "vp pack",
44
+ "size": "vp pack && node ../../scripts/check-size.mjs 2160",
45
+ "dev": "vp pack --watch",
46
+ "test": "vp test",
47
+ "mutate": "stryker run",
48
+ "check": "vp check"
49
+ }
50
+ }
@@ -0,0 +1,151 @@
1
+ ---
2
+ name: devtools
3
+ description: >
4
+ Use when you want to see what a Tinker scope
5
+ did: its spans, sessions, exits or writes. Also
6
+ when a tool reads a scope's state, or listens
7
+ for each new record.
8
+ metadata:
9
+ purpose: >
10
+ How to add devtools to a scope, read its
11
+ snapshot, and listen for new records.
12
+ type: sub-skill
13
+ library: '@tinkered/devtools'
14
+ library_version: '0.34.6'
15
+ requires:
16
+ - '@tinkered/core#core'
17
+ sources:
18
+ - 'lagz0ne/tinkered:packages/devtools/src/index.ts'
19
+ ---
20
+
21
+ # Devtools
22
+
23
+ Devtools is a plain capsule. It keeps what a scope
24
+ did, and gives it back as one plain object.
25
+
26
+ Read the `@tinkered/core` skill `core` first. This
27
+ skill adds devtools only.
28
+
29
+ ## Setup
30
+
31
+ ```ts
32
+ import { createScope, operation } from "@tinkered/core";
33
+ import { devtools } from "@tinkered/devtools";
34
+
35
+ const dt = devtools();
36
+ const scope = await createScope({ capsules: [dt] }).ready;
37
+
38
+ const hello = operation({
39
+ label: "hello",
40
+ run: () => "hi",
41
+ });
42
+
43
+ scope.run(hello);
44
+ export const { runs, exits } = dt.snapshot();
45
+ await scope.close({ graceful: true });
46
+ ```
47
+
48
+ - Add the capsule to `capsules`. There is no
49
+ global hook.
50
+ - `dt.snapshot()` gives the state now.
51
+ - `exits` counts `hello\tvalue` once.
52
+
53
+ ## One devtools per scope
54
+
55
+ Each call to `devtools()` makes a new capsule with
56
+ its own state. Give each scope its own call.
57
+
58
+ ```ts no-check
59
+ import { createScope } from "@tinkered/core";
60
+ import { devtools } from "@tinkered/devtools";
61
+
62
+ const shared = devtools();
63
+ const a = createScope({ capsules: [shared] });
64
+ const b = createScope({ capsules: [shared] });
65
+ ```
66
+
67
+ Two scopes that share one capsule share its state.
68
+ So use `devtools()` once for each scope.
69
+
70
+ ## Options
71
+
72
+ ```ts
73
+ import { devtools } from "@tinkered/devtools";
74
+
75
+ export const dt = devtools({ limit: 500, values: true });
76
+ ```
77
+
78
+ - `limit` sizes the span ring and the session list.
79
+ The default is 1000.
80
+ - `values: true` keeps the last value of each
81
+ write. The default is off.
82
+
83
+ ## The snapshot
84
+
85
+ ```ts
86
+ import { createScope, operation } from "@tinkered/core";
87
+ import { devtools } from "@tinkered/devtools";
88
+
89
+ const dt = devtools();
90
+ const scope = await createScope({ capsules: [dt] }).ready;
91
+
92
+ const ping = operation({
93
+ label: "ping",
94
+ run: () => "pong",
95
+ });
96
+
97
+ await scope.session(async (inner) => {
98
+ inner.run(ping);
99
+ });
100
+
101
+ export const state = dt.snapshot();
102
+ await scope.close({ graceful: true });
103
+ ```
104
+
105
+ - `scope`: the state, and the status once closed.
106
+ - `sessions`: each session by id, in open order.
107
+ Its `runs` hold the spans opened in it.
108
+ - `runs`: the spans opened outside any session.
109
+ - `resources`: the resource spans, in any session.
110
+ - `writes`: the count for each cell label.
111
+ - `exits`: the count for each `label\texit` key.
112
+
113
+ Each call gives fresh objects. A change to the
114
+ copy changes nothing devtools keeps.
115
+
116
+ ## Listeners
117
+
118
+ ```ts
119
+ import { createScope, operation } from "@tinkered/core";
120
+ import { devtools } from "@tinkered/devtools";
121
+
122
+ const dt = devtools();
123
+ export let latest = dt.snapshot();
124
+ const stop = dt.moves(() => {
125
+ latest = dt.snapshot();
126
+ });
127
+
128
+ const scope = await createScope({ capsules: [dt] }).ready;
129
+ const ping = operation({
130
+ label: "ping",
131
+ run: () => "pong",
132
+ });
133
+ scope.run(ping);
134
+ stop();
135
+ await scope.close({ graceful: true });
136
+ ```
137
+
138
+ - `dt.moves(listener)` calls `listener` once for
139
+ each new record: a span, a session that opens
140
+ or closes, a scope state change, or a write.
141
+ - It returns a function. Call it to stop the
142
+ listener.
143
+ - A listener that throws does not stop the run.
144
+ Its error is rethrown in a microtask.
145
+
146
+ ## Completion
147
+
148
+ - `dt.snapshot().scope.state` is `closed` after
149
+ the scope closes.
150
+ - Each op you ran shows in `runs`, or in a
151
+ session's `runs` when it ran in one.