@jam-overture/loom 0.1.0 → 0.2.0
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 +3 -1
- package/dist/application/application.d.ts +109 -0
- package/dist/application/application.js +97 -0
- package/dist/application/memory.d.ts +20 -0
- package/dist/application/memory.js +79 -0
- package/dist/application/store.d.ts +101 -0
- package/dist/application/store.js +25 -0
- package/dist/audience/audience.d.ts +36 -0
- package/dist/audience/audience.js +41 -0
- package/dist/cli/templates.js +19 -0
- package/dist/data/plan.js +0 -0
- package/dist/data/resolution.d.ts +23 -1
- package/dist/data/resolve.d.ts +30 -3
- package/dist/data/resolve.js +11 -3
- package/dist/ids.d.ts +10 -0
- package/dist/ids.js +11 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/patch.d.ts +54 -0
- package/dist/patch.js +44 -0
- package/dist/project/closing.d.ts +51 -0
- package/dist/project/closing.js +53 -0
- package/dist/project/memory.d.ts +20 -0
- package/dist/project/memory.js +97 -0
- package/dist/project/project.d.ts +90 -0
- package/dist/project/project.js +91 -0
- package/dist/project/store.d.ts +135 -0
- package/dist/project/store.js +41 -0
- package/dist/registered.d.ts +64 -0
- package/dist/registered.js +118 -0
- package/dist/render/behaviour-adjust.d.ts +11 -19
- package/dist/render/behaviour-adjust.js +75 -21
- package/dist/render/behaviour-present.d.ts +8 -0
- package/dist/render/behaviour-present.js +57 -6
- package/dist/render/behaviour.d.ts +98 -17
- package/dist/render/behaviour.js +91 -26
- package/dist/render/control-name.d.ts +68 -0
- package/dist/render/control-name.js +37 -0
- package/dist/render/diagnostics.d.ts +72 -0
- package/dist/render/diagnostics.js +7 -0
- package/dist/render/index.d.ts +2 -0
- package/dist/render/index.js +2 -0
- package/dist/render/reads.d.ts +11 -0
- package/dist/render/reads.js +12 -1
- package/dist/render/render.js +102 -7
- package/dist/render/slots.d.ts +52 -0
- package/dist/render/slots.js +24 -0
- package/dist/render/theme.d.ts +54 -1
- package/dist/render/theme.js +23 -1
- package/dist/render/unshown.d.ts +152 -0
- package/dist/render/unshown.js +80 -0
- package/dist/runtime/analysis.d.ts +94 -9
- package/dist/runtime/analysis.js +54 -9
- package/dist/runtime/assessment.d.ts +36 -2
- package/dist/runtime/assessment.js +27 -2
- package/dist/runtime/checks.d.ts +148 -0
- package/dist/runtime/checks.js +112 -0
- package/dist/runtime/disposition.d.ts +96 -0
- package/dist/runtime/disposition.js +46 -0
- package/dist/runtime/gate.js +22 -0
- package/dist/runtime/index.d.ts +3 -0
- package/dist/runtime/index.js +3 -0
- package/dist/runtime/irreversibility.d.ts +64 -0
- package/dist/runtime/irreversibility.js +45 -0
- package/dist/runtime/pipeline.d.ts +43 -0
- package/dist/runtime/pipeline.js +10 -2
- package/dist/runtime/policy-change.d.ts +93 -0
- package/dist/runtime/policy-change.js +299 -0
- package/dist/runtime/policy-log.d.ts +406 -0
- package/dist/runtime/policy-log.js +285 -0
- package/dist/runtime/reversibility.d.ts +11 -9
- package/dist/runtime/stakes.d.ts +129 -1
- package/dist/runtime/stakes.js +240 -26
- package/dist/runtime/vocabulary.d.ts +164 -7
- package/dist/runtime/vocabulary.js +181 -8
- package/dist/sdk/audit.d.ts +174 -3
- package/dist/sdk/audit.js +176 -17
- package/dist/sdk/catalogue.d.ts +9 -1
- package/dist/sdk/conformance.d.ts +37 -3
- package/dist/sdk/conformance.js +25 -4
- package/dist/sdk/definition.d.ts +65 -0
- package/dist/sdk/definition.js +22 -0
- package/dist/sdk/registry.d.ts +29 -2
- package/dist/sdk/registry.js +63 -1
- package/dist/signals/action-change.d.ts +507 -0
- package/dist/signals/action-change.js +265 -0
- package/dist/signals/action.d.ts +331 -0
- package/dist/signals/action.js +182 -0
- package/dist/signals/applied-window.contract.d.ts +23 -0
- package/dist/signals/applied-window.contract.js +160 -0
- package/dist/signals/arriving.d.ts +210 -0
- package/dist/signals/arriving.js +133 -0
- package/dist/signals/audiences.d.ts +528 -0
- package/dist/signals/audiences.js +255 -0
- package/dist/signals/broadcast.d.ts +8 -2
- package/dist/signals/broadcast.js +58 -8
- package/dist/signals/change.d.ts +378 -0
- package/dist/signals/change.js +327 -0
- package/dist/signals/collect.d.ts +41 -0
- package/dist/signals/collect.js +88 -7
- package/dist/signals/copy-change.d.ts +553 -0
- package/dist/signals/copy-change.js +377 -0
- package/dist/signals/copy.d.ts +337 -0
- package/dist/signals/copy.js +211 -0
- package/dist/signals/counters.d.ts +46 -0
- package/dist/signals/counters.js +102 -0
- package/dist/signals/deliver.d.ts +23 -4
- package/dist/signals/deliver.js +34 -6
- package/dist/signals/deployment-change.d.ts +371 -0
- package/dist/signals/deployment-change.js +268 -0
- package/dist/signals/deployment.d.ts +265 -0
- package/dist/signals/deployment.js +152 -0
- package/dist/signals/door.d.ts +92 -0
- package/dist/signals/door.js +53 -0
- package/dist/signals/fold.d.ts +10 -0
- package/dist/signals/fold.js +5 -1
- package/dist/signals/form-action.d.ts +302 -0
- package/dist/signals/form-action.js +169 -0
- package/dist/signals/form-copy.d.ts +523 -0
- package/dist/signals/form-copy.js +346 -0
- package/dist/signals/forms.d.ts +247 -0
- package/dist/signals/forms.js +147 -0
- package/dist/signals/funnel-change.d.ts +548 -0
- package/dist/signals/funnel-change.js +361 -0
- package/dist/signals/funnel.d.ts +532 -0
- package/dist/signals/funnel.js +216 -0
- package/dist/signals/health.d.ts +497 -0
- package/dist/signals/health.js +361 -0
- package/dist/signals/index.d.ts +41 -0
- package/dist/signals/index.js +41 -0
- package/dist/signals/ingest.d.ts +56 -1
- package/dist/signals/ingest.js +91 -1
- package/dist/signals/journal.d.ts +11 -0
- package/dist/signals/kinds.d.ts +8 -6
- package/dist/signals/kinds.js +14 -6
- package/dist/signals/library-action.d.ts +430 -0
- package/dist/signals/library-action.js +336 -0
- package/dist/signals/library-change.d.ts +477 -0
- package/dist/signals/library-change.js +335 -0
- package/dist/signals/library-copy-change.d.ts +498 -0
- package/dist/signals/library-copy-change.js +476 -0
- package/dist/signals/library-pace.d.ts +319 -0
- package/dist/signals/library-pace.js +305 -0
- package/dist/signals/library-reach-change.d.ts +636 -0
- package/dist/signals/library-reach-change.js +534 -0
- package/dist/signals/library-reach.d.ts +377 -0
- package/dist/signals/library-reach.js +227 -0
- package/dist/signals/library-word-reach-change.d.ts +567 -0
- package/dist/signals/library-word-reach-change.js +568 -0
- package/dist/signals/library.d.ts +306 -0
- package/dist/signals/library.js +178 -0
- package/dist/signals/memory.d.ts +9 -0
- package/dist/signals/memory.js +105 -14
- package/dist/signals/migrate.d.ts +17 -2
- package/dist/signals/migrate.js +122 -5
- package/dist/signals/notice.d.ts +334 -0
- package/dist/signals/notice.js +228 -0
- package/dist/signals/pace-change.d.ts +501 -0
- package/dist/signals/pace-change.js +310 -0
- package/dist/signals/pace.d.ts +296 -0
- package/dist/signals/pace.js +291 -0
- package/dist/signals/page-views.d.ts +365 -0
- package/dist/signals/page-views.js +218 -0
- package/dist/signals/parts.d.ts +344 -0
- package/dist/signals/parts.js +273 -0
- package/dist/signals/postgres.d.ts +16 -2
- package/dist/signals/postgres.js +390 -87
- package/dist/signals/progress.d.ts +212 -0
- package/dist/signals/progress.js +150 -0
- package/dist/signals/ratios.d.ts +108 -0
- package/dist/signals/ratios.js +40 -0
- package/dist/signals/reach.d.ts +309 -0
- package/dist/signals/reach.js +93 -0
- package/dist/signals/readable.d.ts +34 -0
- package/dist/signals/readable.js +34 -0
- package/dist/signals/readership.d.ts +381 -0
- package/dist/signals/readership.js +308 -0
- package/dist/signals/region-pages.d.ts +173 -0
- package/dist/signals/region-pages.js +62 -0
- package/dist/signals/region.d.ts +287 -0
- package/dist/signals/region.js +198 -0
- package/dist/signals/retention.d.ts +141 -0
- package/dist/signals/retention.js +58 -0
- package/dist/signals/rollup.d.ts +67 -3
- package/dist/signals/rollup.js +0 -0
- package/dist/signals/schema.d.ts +432 -1
- package/dist/signals/schema.js +157 -3
- package/dist/signals/signal.d.ts +237 -11
- package/dist/signals/signal.js +70 -10
- package/dist/signals/silence-census.d.ts +205 -0
- package/dist/signals/silence-census.js +87 -0
- package/dist/signals/silences.d.ts +848 -0
- package/dist/signals/silences.js +753 -0
- package/dist/signals/tally.d.ts +140 -11
- package/dist/signals/window-figures.d.ts +148 -0
- package/dist/signals/window-figures.js +42 -0
- package/dist/signals/window.d.ts +866 -0
- package/dist/signals/window.js +693 -0
- package/dist/signals/words.d.ts +24 -0
- package/dist/signals/words.js +24 -0
- package/dist/store/index.d.ts +19 -0
- package/dist/store/index.js +19 -0
- package/dist/store/memory.d.ts +2 -1
- package/dist/store/memory.js +48 -12
- package/dist/store/migrate.d.ts +80 -0
- package/dist/store/migrate.js +259 -7
- package/dist/store/postgres-applications.d.ts +4 -0
- package/dist/store/postgres-applications.js +197 -0
- package/dist/store/postgres-holds.d.ts +10 -1
- package/dist/store/postgres-holds.js +116 -101
- package/dist/store/postgres-policy-log.d.ts +14 -0
- package/dist/store/postgres-policy-log.js +240 -0
- package/dist/store/postgres-projects.d.ts +4 -0
- package/dist/store/postgres-projects.js +217 -0
- package/dist/store/postgres.d.ts +16 -1
- package/dist/store/postgres.js +59 -9
- package/dist/store/replay.d.ts +102 -1
- package/dist/store/replay.js +190 -19
- package/dist/store/reseed.d.ts +238 -0
- package/dist/store/reseed.js +308 -0
- package/dist/store/schema.d.ts +450 -2
- package/dist/store/schema.js +141 -5
- package/dist/store/store.d.ts +29 -6
- package/dist/submit/resolve.js +2 -1
- package/dist/telemetry/calibration.d.ts +25 -0
- package/dist/telemetry/calibration.js +27 -1
- package/dist/telemetry/event.d.ts +391 -33
- package/dist/telemetry/event.js +84 -20
- package/dist/telemetry/index.d.ts +1 -0
- package/dist/telemetry/index.js +1 -0
- package/dist/telemetry/journal.d.ts +72 -3
- package/dist/telemetry/journal.js +9 -0
- package/dist/telemetry/lookup.d.ts +27 -0
- package/dist/telemetry/lookup.js +18 -0
- package/dist/telemetry/memory.js +23 -0
- package/dist/telemetry/migrate.js +22 -1
- package/dist/telemetry/postgres.d.ts +10 -1
- package/dist/telemetry/postgres.js +172 -90
- package/dist/telemetry/remeasure.d.ts +78 -0
- package/dist/telemetry/remeasure.js +83 -0
- package/dist/telemetry/retention.d.ts +2 -2
- package/dist/telemetry/schema.d.ts +17 -0
- package/dist/telemetry/schema.js +27 -1
- package/dist/telemetry/sink.d.ts +2 -2
- package/dist/testing/application-contract.d.ts +14 -0
- package/dist/testing/application-contract.js +270 -0
- package/dist/testing/contracts.d.ts +12 -7
- package/dist/testing/contracts.js +12 -7
- package/dist/testing/definitions.d.ts +78 -0
- package/dist/testing/definitions.js +134 -9
- package/dist/testing/doubles.d.ts +103 -0
- package/dist/testing/doubles.js +143 -0
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/index.js +2 -2
- package/dist/testing/journal-contract.js +140 -2
- package/dist/testing/model-contract.d.ts +110 -0
- package/dist/testing/model-contract.js +150 -0
- package/dist/testing/policy-log-contract.d.ts +26 -0
- package/dist/testing/policy-log-contract.js +344 -0
- package/dist/testing/project-contract.d.ts +14 -0
- package/dist/testing/project-contract.js +266 -0
- package/dist/testing/reader-signal-contract.d.ts +27 -2
- package/dist/testing/reader-signal-contract.js +475 -5
- package/dist/testing/store-contract.d.ts +7 -1
- package/dist/testing/store-contract.js +84 -3
- package/dist/theme/apply.d.ts +20 -1
- package/dist/theme/apply.js +5 -3
- package/dist/theme/derive.d.ts +22 -0
- package/dist/theme/derive.js +54 -1
- package/dist/theme/library.js +1 -1
- package/dist/theme/measure.d.ts +34 -0
- package/dist/theme/measure.js +30 -1
- package/dist/theme/palettes.js +7 -7
- package/dist/theme/ramp.d.ts +64 -0
- package/dist/theme/ramp.js +114 -0
- package/dist/theme/registry.d.ts +9 -0
- package/dist/theme/registry.js +11 -1
- package/dist/theme/separation.d.ts +97 -3
- package/dist/theme/separation.js +75 -0
- package/dist/tree/compare.d.ts +9 -0
- package/dist/tree/compare.js +18 -0
- package/dist/write/computed.d.ts +60 -0
- package/dist/write/computed.js +61 -0
- package/dist/write/held.d.ts +59 -0
- package/dist/write/index.d.ts +5 -2
- package/dist/write/index.js +5 -2
- package/dist/write/reseed.d.ts +151 -0
- package/dist/write/reseed.js +184 -0
- package/dist/write/revert.js +2 -16
- package/package.json +98 -19
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { and, asc, desc, eq, gt, lt } from "drizzle-orm";
|
|
1
|
+
import { and, asc, desc, eq, gt, inArray, lt, sql } from "drizzle-orm";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { treeIdSchema } from "../ids.js";
|
|
4
|
+
import { DEFAULT_APPLICATION_ID } from "../application/application.js";
|
|
4
5
|
import { cursorPosition, pageEnds } from "../paging.js";
|
|
5
6
|
import { err, ok } from "../result.js";
|
|
6
|
-
import { telemetryEventSchema } from "./event.js";
|
|
7
|
+
import { assessmentSummarySchema, telemetryEventSchema } from "./event.js";
|
|
7
8
|
import { clampTelemetryLimit, } from "./journal.js";
|
|
9
|
+
import { splitLookup } from "./lookup.js";
|
|
8
10
|
import { loomTelemetry } from "./schema.js";
|
|
9
11
|
export * from "./migrate.js";
|
|
10
12
|
export * from "./schema.js";
|
|
@@ -42,98 +44,178 @@ const recordedSchema = z.object({
|
|
|
42
44
|
.transform((value) => value.toISOString()),
|
|
43
45
|
event: telemetryEventSchema,
|
|
44
46
|
});
|
|
47
|
+
/**
|
|
48
|
+
* The three JSON paths the assessment lookup reads, named once.
|
|
49
|
+
*
|
|
50
|
+
* `event` is one document and its type is not also a column, which 0022's table
|
|
51
|
+
* comment argued for and this is the first query to pay for: the discriminant
|
|
52
|
+
* has to be read out of the JSON rather than compared against a column. That is
|
|
53
|
+
* the trade the comment described — an index on a path, available the day a
|
|
54
|
+
* query needs one — and not a duplicated column that can disagree.
|
|
55
|
+
*/
|
|
56
|
+
const ASSESSED_TYPE = sql `${loomTelemetry.event} ->> 'type'`;
|
|
57
|
+
const ASSESSED_PROPOSAL_ID = sql `${loomTelemetry.event} -> 'assessment' ->> 'proposalId'`;
|
|
58
|
+
const ASSESSMENT = sql `${loomTelemetry.event} -> 'assessment'`;
|
|
45
59
|
const failure = (cause, detail) => ({
|
|
46
60
|
code: "unavailable",
|
|
47
61
|
detail: `${detail}: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
48
62
|
});
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Every query is filtered by the project this handle speaks for (0260), and
|
|
65
|
+
* every write sets it.
|
|
66
|
+
*
|
|
67
|
+
* `seq` is not scoped and does not need to be — see `schema.ts`. What is scoped
|
|
68
|
+
* is every row this handle will ever see, so one project's gate decisions
|
|
69
|
+
* cannot appear on another's screen.
|
|
70
|
+
*/
|
|
71
|
+
export const postgresTelemetryJournal = (db, scope = {}) => {
|
|
72
|
+
const project = scope.application ?? DEFAULT_APPLICATION_ID;
|
|
73
|
+
const mine = (...rest) => and(eq(loomTelemetry.applicationId, project), ...rest);
|
|
74
|
+
return {
|
|
75
|
+
record: async (batch) => {
|
|
76
|
+
if (batch.length === 0)
|
|
77
|
+
return ok(undefined);
|
|
78
|
+
try {
|
|
79
|
+
await db.insert(loomTelemetry).values(batch.map((entry) => ({
|
|
80
|
+
applicationId: project,
|
|
81
|
+
treeId: entry.treeId,
|
|
82
|
+
occurredAt: entry.occurredAt,
|
|
83
|
+
event: entry.event,
|
|
84
|
+
})));
|
|
85
|
+
return ok(undefined);
|
|
86
|
+
}
|
|
87
|
+
catch (cause) {
|
|
88
|
+
return err(failure(cause, `could not record ${batch.length} telemetry events`));
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
read: async (request) => {
|
|
92
|
+
const limit = clampTelemetryLimit(request?.limit);
|
|
93
|
+
const from = cursorPosition(request?.cursor);
|
|
94
|
+
const direction = request?.direction ?? "newer";
|
|
95
|
+
try {
|
|
96
|
+
/**
|
|
97
|
+
* `older` scans descending so the newest page costs one index seek rather
|
|
98
|
+
* than a walk from the start of the journal, then the rows are put back in
|
|
99
|
+
* arrival order — the contract's promise that a page's order never depends
|
|
100
|
+
* on which end it came from.
|
|
101
|
+
*/
|
|
102
|
+
const rows = await db
|
|
103
|
+
.select({
|
|
104
|
+
seq: loomTelemetry.seq,
|
|
105
|
+
treeId: loomTelemetry.treeId,
|
|
106
|
+
occurredAt: loomTelemetry.occurredAt,
|
|
107
|
+
recordedAt: loomTelemetry.recordedAt,
|
|
108
|
+
event: loomTelemetry.event,
|
|
109
|
+
})
|
|
110
|
+
.from(loomTelemetry)
|
|
111
|
+
.where(mine(request?.treeId === undefined ? undefined : eq(loomTelemetry.treeId, request.treeId), from === undefined
|
|
112
|
+
? undefined
|
|
113
|
+
: direction === "older"
|
|
114
|
+
? lt(loomTelemetry.seq, from)
|
|
115
|
+
: gt(loomTelemetry.seq, from)))
|
|
116
|
+
.orderBy(direction === "older" ? desc(loomTelemetry.seq) : asc(loomTelemetry.seq))
|
|
117
|
+
/** One extra row answers "is there another page" without a count. */
|
|
118
|
+
.limit(limit + 1);
|
|
119
|
+
const taken = rows.slice(0, limit);
|
|
120
|
+
const parsed = z
|
|
121
|
+
.array(recordedSchema)
|
|
122
|
+
.safeParse(direction === "older" ? taken.slice().reverse() : taken);
|
|
123
|
+
if (!parsed.success) {
|
|
124
|
+
return err({
|
|
125
|
+
code: "unavailable",
|
|
126
|
+
detail: `a stored telemetry record did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
const page = parsed.data;
|
|
130
|
+
return ok({
|
|
131
|
+
records: page,
|
|
132
|
+
...pageEnds(page.map((record) => record.seq), direction, {
|
|
133
|
+
beyond: rows.length > limit,
|
|
134
|
+
resumed: from !== undefined,
|
|
135
|
+
}),
|
|
101
136
|
});
|
|
102
137
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
});
|
|
138
|
+
catch (cause) {
|
|
139
|
+
return err(failure(cause, "could not read telemetry"));
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
/**
|
|
143
|
+
* One statement, one row per proposal, and the index the table's own comment
|
|
144
|
+
* said would arrive the day a query needed it.
|
|
145
|
+
*
|
|
146
|
+
* `DISTINCT ON` is what makes this bounded by construction rather than by a
|
|
147
|
+
* limit: the row count *is* the number of ids asked about, whatever the
|
|
148
|
+
* journal holds, so a proposal narrated twice cannot widen the read. Ordering
|
|
149
|
+
* by `seq DESC` within each id picks the latest assessment, which is the same
|
|
150
|
+
* answer the in-memory journal folds its way to.
|
|
151
|
+
*
|
|
152
|
+
* The two JSON paths are the shape of a `change-assessed` record and nothing
|
|
153
|
+
* else, which is why the index below them is partial: the predicate is the
|
|
154
|
+
* discriminant, so the index holds one entry per assessed proposal rather than
|
|
155
|
+
* one per row of a journal that is mostly other event types.
|
|
156
|
+
*/
|
|
157
|
+
assessments: async ({ proposalIds, treeId }) => {
|
|
158
|
+
const { asked, unasked } = splitLookup(proposalIds);
|
|
159
|
+
if (asked.length === 0) {
|
|
160
|
+
return ok({ assessments: new Map(), unasked });
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
const rows = await db
|
|
164
|
+
.selectDistinctOn([ASSESSED_PROPOSAL_ID], { assessment: ASSESSMENT })
|
|
165
|
+
.from(loomTelemetry)
|
|
166
|
+
.where(mine(eq(ASSESSED_TYPE, "change-assessed"), inArray(ASSESSED_PROPOSAL_ID, asked), treeId === undefined ? undefined : eq(loomTelemetry.treeId, treeId)))
|
|
167
|
+
.orderBy(ASSESSED_PROPOSAL_ID, desc(loomTelemetry.seq));
|
|
168
|
+
const parsed = z.array(assessmentSummarySchema).safeParse(rows.map((row) => row.assessment));
|
|
169
|
+
if (!parsed.success) {
|
|
170
|
+
return err({
|
|
171
|
+
code: "unavailable",
|
|
172
|
+
detail: `a stored assessment did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
const assessments = parsed.data;
|
|
176
|
+
return ok({
|
|
177
|
+
assessments: new Map(assessments.map((assessment) => [assessment.proposalId, assessment])),
|
|
178
|
+
unasked,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
catch (cause) {
|
|
182
|
+
return err(failure(cause, `could not look up ${asked.length} assessments`));
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
/**
|
|
186
|
+
* One statement, and the count comes from the rows it returns rather than
|
|
187
|
+
* from a driver-specific `rowCount` field — the two drivers this runs on
|
|
188
|
+
* report that differently, and a number that means something else under
|
|
189
|
+
* PGlite than under postgres.js is worse than no number.
|
|
190
|
+
*
|
|
191
|
+
* Returning only `seq` keeps the payload to one integer per row, and the
|
|
192
|
+
* caller has already bounded how many rows there can be: `applyRetention`
|
|
193
|
+
* derives `before` from a scan it capped itself.
|
|
194
|
+
*/
|
|
195
|
+
forget: async ({ before }) => {
|
|
196
|
+
try {
|
|
197
|
+
const removed = await db
|
|
198
|
+
.delete(loomTelemetry)
|
|
199
|
+
/**
|
|
200
|
+
* Scoped like every read on this handle, which it was not when the
|
|
201
|
+
* application dimension landed (0269) — `forget` was the one statement
|
|
202
|
+
* in this file that did not go through `mine`.
|
|
203
|
+
*
|
|
204
|
+
* It matters more here than anywhere else in the file. `seq` is a
|
|
205
|
+
* `bigserial`, so it counts across every application the database
|
|
206
|
+
* holds: retention run for one application was deleting every other
|
|
207
|
+
* application's records below the same sequence number, and a deleted
|
|
208
|
+
* telemetry row is not recoverable from anything. Nothing had reached
|
|
209
|
+
* it, because nothing constructs a second handle yet.
|
|
210
|
+
*/
|
|
211
|
+
.where(mine(lt(loomTelemetry.seq, before)))
|
|
212
|
+
.returning({ seq: loomTelemetry.seq });
|
|
213
|
+
return ok({ removed: removed.length });
|
|
214
|
+
}
|
|
215
|
+
catch (cause) {
|
|
216
|
+
return err(failure(cause, `could not forget telemetry below ${before}`));
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
};
|
|
139
221
|
//# sourceMappingURL=postgres.js.map
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { type GatePolicy, type StakeFactorCode, type StakeLevel } from "../runtime/index.js";
|
|
2
|
+
import type { AssessmentSummary } from "./event.js";
|
|
3
|
+
/**
|
|
4
|
+
* What these stakes would have been, under a policy that was not in force.
|
|
5
|
+
*
|
|
6
|
+
* ## The question, and why only a record can answer it
|
|
7
|
+
*
|
|
8
|
+
* A host that wants to change its Gate policy has one thing worth consulting:
|
|
9
|
+
* the changes it really judged. `/portal/rules/what-if` replays the eight rungs
|
|
10
|
+
* of the escalation ladder against an edited policy and reproduces every recorded
|
|
11
|
+
* verdict before it is believed — but only seven of a policy's seventeen fields
|
|
12
|
+
* could be moved, because the other ten move the **measurement** and the
|
|
13
|
+
* measurement was a function of a page as it stood that day. The level was
|
|
14
|
+
* recorded; how it was arrived at was not.
|
|
15
|
+
*
|
|
16
|
+
* `remeasureStakes` is the missing half. It takes a journalled
|
|
17
|
+
* `AssessmentSummary` and a policy, re-runs the Gate's own measuring rules
|
|
18
|
+
* against it, and says what the stakes would have been.
|
|
19
|
+
*
|
|
20
|
+
* ## It is the Gate's rules, not a copy of them
|
|
21
|
+
*
|
|
22
|
+
* The seven policy-dependent rules are `measureStakes`, exactly as
|
|
23
|
+
* `assessStakes` runs them, against a `StakeMeasurement` this module assembles
|
|
24
|
+
* from the record instead of from a delta. Nothing here knows what a large
|
|
25
|
+
* removal is. A simulation that reimplemented the thresholds would be confidently
|
|
26
|
+
* wrong about the one subject a person came to it to be sure of, and it would go
|
|
27
|
+
* wrong silently, months after the copy was made.
|
|
28
|
+
*
|
|
29
|
+
* The other seven rules — an unreachable target, an unregistered type, props a
|
|
30
|
+
* primitive refuses, a question nothing reads, a redirected form, a repointed
|
|
31
|
+
* region, discarded work — cannot be re-run at all, because each reads a list of
|
|
32
|
+
* specifics that names parts of a particular page and does not cross the
|
|
33
|
+
* telemetry boundary (0023). They do not need to be: no field of a policy moves
|
|
34
|
+
* them, so the recorded code is the whole of the answer and `fixedStakeLevel`
|
|
35
|
+
* turns it back into a level. Those come back `source: "recorded"`.
|
|
36
|
+
*
|
|
37
|
+
* ## What it refuses to guess
|
|
38
|
+
*
|
|
39
|
+
* Four of the fields this needs are optional on the record, because 0045 says a
|
|
40
|
+
* telemetry field added after records exist is optional and never defaulted. So
|
|
41
|
+
* an older record may genuinely not say whether a protected prop was configured.
|
|
42
|
+
* **Defaulting that to "no" would silently lower the stakes of exactly the
|
|
43
|
+
* changes an operator is asking about**, and the answer would look like a result.
|
|
44
|
+
*
|
|
45
|
+
* Instead the rule is named in `unreadable` and left out of the level, which
|
|
46
|
+
* makes `level` a floor rather than an answer — the true stakes are that level or
|
|
47
|
+
* higher. A caller showing a figure to a person must say so, or set the row
|
|
48
|
+
* aside; `/portal/rules/what-if` already sets aside every change it cannot
|
|
49
|
+
* reproduce, and this is the same discipline one step earlier.
|
|
50
|
+
*
|
|
51
|
+
* A rule is only unreadable when the policy would actually have consulted the
|
|
52
|
+
* missing field. A host with nothing protected is not asking about protected
|
|
53
|
+
* types, and a record whose change removed nothing cannot have removed a
|
|
54
|
+
* protected type whatever it failed to write down — so the common case, an
|
|
55
|
+
* ordinary record under an ordinary policy, is readable in full.
|
|
56
|
+
*/
|
|
57
|
+
/** One rule's contribution, and whether it was run again or taken as it stood. */
|
|
58
|
+
export type RemeasuredFactor = {
|
|
59
|
+
readonly code: StakeFactorCode;
|
|
60
|
+
readonly level: StakeLevel;
|
|
61
|
+
/**
|
|
62
|
+
* `remeasured` — this rule was run against the policy given here.
|
|
63
|
+
* `recorded` — no field of a policy can move it, so its code is its level.
|
|
64
|
+
*/
|
|
65
|
+
readonly source: "remeasured" | "recorded";
|
|
66
|
+
};
|
|
67
|
+
export type RemeasuredStakes = {
|
|
68
|
+
/** A floor, not an answer, whenever `unreadable` is not empty. */
|
|
69
|
+
readonly level: StakeLevel;
|
|
70
|
+
readonly factors: readonly RemeasuredFactor[];
|
|
71
|
+
/**
|
|
72
|
+
* Rules this record cannot answer under this policy, because a field they read
|
|
73
|
+
* was not written down. Empty is the claim that `level` is exact.
|
|
74
|
+
*/
|
|
75
|
+
readonly unreadable: readonly StakeFactorCode[];
|
|
76
|
+
};
|
|
77
|
+
export declare const remeasureStakes: (summary: AssessmentSummary, policy: GatePolicy) => RemeasuredStakes;
|
|
78
|
+
//# sourceMappingURL=remeasure.d.ts.map
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { FIXED_STAKE_FACTOR_CODES, fixedStakeLevel, highestStake, isFixedStakeFactor, measureStakes, } from "../runtime/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* The largest `affectedNodeIds` could have been, from counts every record
|
|
4
|
+
* carries.
|
|
5
|
+
*
|
|
6
|
+
* Breadth is the one measuring rule whose input is a length, and a record
|
|
7
|
+
* written before `affectedNodeCount` existed does not carry it. It is still
|
|
8
|
+
* bounded: an insert or a remove contributes its whole subtree to both the
|
|
9
|
+
* affected set and its own node count, a move and a configure contribute one
|
|
10
|
+
* node to both, and the affected set is a set — so it can never be larger than
|
|
11
|
+
* the four counts added up. When even that ceiling is under the threshold the
|
|
12
|
+
* rule provably cannot fire, and the missing field does not matter.
|
|
13
|
+
*
|
|
14
|
+
* This is what makes the lever answerable over a journal written before today
|
|
15
|
+
* rather than only over records written after it.
|
|
16
|
+
*/
|
|
17
|
+
const affectedNodeCeiling = (summary) => summary.insertedNodeCount +
|
|
18
|
+
summary.removedNodeCount +
|
|
19
|
+
summary.movedNodeCount +
|
|
20
|
+
summary.configuredNodeCount;
|
|
21
|
+
/**
|
|
22
|
+
* Whether a rule's missing input can change its answer.
|
|
23
|
+
*
|
|
24
|
+
* Absent is not empty (0045), so the question is never "what shall we assume" —
|
|
25
|
+
* it is whether this policy, on this record, could have reached the field at
|
|
26
|
+
* all. Each entry is a fact about the record and not a default: a change that
|
|
27
|
+
* removed no nodes removed no types, a change that configured no nodes set no
|
|
28
|
+
* prop keys, and a policy protecting nothing never looks at either list.
|
|
29
|
+
*/
|
|
30
|
+
const unreadableUnder = (summary, policy) => {
|
|
31
|
+
const protects = policy.protectedPrimitiveTypes.length > 0;
|
|
32
|
+
return [
|
|
33
|
+
...(protects && summary.removedPrimitiveTypes === undefined && summary.removedNodeCount > 0
|
|
34
|
+
? ["protected-type-removed"]
|
|
35
|
+
: []),
|
|
36
|
+
...(protects && summary.relocatedPrimitiveTypes === undefined && summary.movedNodeCount > 0
|
|
37
|
+
? ["protected-type-relocated"]
|
|
38
|
+
: []),
|
|
39
|
+
...(policy.protectedPropKeys.length > 0 &&
|
|
40
|
+
summary.configuredPropKeys === undefined &&
|
|
41
|
+
summary.configuredNodeCount > 0
|
|
42
|
+
? ["protected-prop-configured"]
|
|
43
|
+
: []),
|
|
44
|
+
...(summary.affectedNodeCount === undefined &&
|
|
45
|
+
affectedNodeCeiling(summary) >= policy.breadthThreshold
|
|
46
|
+
? ["broad-change"]
|
|
47
|
+
: []),
|
|
48
|
+
...(summary.stakeFactorCodes === undefined ? FIXED_STAKE_FACTOR_CODES : []),
|
|
49
|
+
];
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* The record as a measurement, with every absent list read as absent.
|
|
53
|
+
*
|
|
54
|
+
* `[]` here is not a claim that nothing happened — it is what the rule has to be
|
|
55
|
+
* handed to produce no factor, and `unreadableUnder` is what says the resulting
|
|
56
|
+
* silence may be ignorance rather than a measurement. The two halves are
|
|
57
|
+
* separate on purpose: a measurement cannot hold "unknown", and a level cannot
|
|
58
|
+
* carry a caveat.
|
|
59
|
+
*/
|
|
60
|
+
const measurementFrom = (summary) => ({
|
|
61
|
+
insertedNodeCount: summary.insertedNodeCount,
|
|
62
|
+
removedNodeCount: summary.removedNodeCount,
|
|
63
|
+
movedNodeCount: summary.movedNodeCount,
|
|
64
|
+
affectedNodeCount: summary.affectedNodeCount ?? 0,
|
|
65
|
+
shallowestAffectedDepth: summary.shallowestAffectedDepth,
|
|
66
|
+
touchedPrimitiveTypes: summary.touchedPrimitiveTypes,
|
|
67
|
+
removedPrimitiveTypes: summary.removedPrimitiveTypes ?? [],
|
|
68
|
+
relocatedPrimitiveTypes: summary.relocatedPrimitiveTypes ?? [],
|
|
69
|
+
configuredPropKeys: summary.configuredPropKeys ?? [],
|
|
70
|
+
});
|
|
71
|
+
export const remeasureStakes = (summary, policy) => {
|
|
72
|
+
const remeasured = measureStakes(measurementFrom(summary), policy).map((factor) => ({ code: factor.code, level: factor.level, source: "remeasured" }));
|
|
73
|
+
const carried = (summary.stakeFactorCodes ?? [])
|
|
74
|
+
.filter(isFixedStakeFactor)
|
|
75
|
+
.map((code) => ({ code, level: fixedStakeLevel(code), source: "recorded" }));
|
|
76
|
+
const factors = [...remeasured, ...carried];
|
|
77
|
+
return {
|
|
78
|
+
level: highestStake(factors.map((factor) => factor.level)),
|
|
79
|
+
factors,
|
|
80
|
+
unreadable: unreadableUnder(summary, policy),
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
//# sourceMappingURL=remeasure.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { type Clock } from "../runtime/events.js";
|
|
3
|
-
import type { RecordedTelemetry, TelemetryError,
|
|
3
|
+
import type { RecordedTelemetry, TelemetryError, TelemetryPruner } from "./journal.js";
|
|
4
4
|
/**
|
|
5
5
|
* How a journal is allowed to forget.
|
|
6
6
|
*
|
|
@@ -125,7 +125,7 @@ export type RetentionOutcome = {
|
|
|
125
125
|
* forget is an operational choice, and a framework that quietly deleted a
|
|
126
126
|
* host's history on a timer it chose would be making it.
|
|
127
127
|
*/
|
|
128
|
-
export declare const applyRetention: (journal:
|
|
128
|
+
export declare const applyRetention: (journal: TelemetryPruner, request: RetentionRequest) => Promise<RetentionOutcome>;
|
|
129
129
|
/** One line for an operator, because an outcome nobody can read is an outcome nobody acts on. */
|
|
130
130
|
export declare const describeRetention: (outcome: RetentionOutcome) => string;
|
|
131
131
|
//# sourceMappingURL=retention.d.ts.map
|
|
@@ -22,6 +22,23 @@ export declare const loomTelemetry: import("drizzle-orm/pg-core").PgTableWithCol
|
|
|
22
22
|
name: "loom_telemetry";
|
|
23
23
|
schema: undefined;
|
|
24
24
|
columns: {
|
|
25
|
+
applicationId: import("drizzle-orm/pg-core").PgColumn<{
|
|
26
|
+
name: "application_id";
|
|
27
|
+
tableName: "loom_telemetry";
|
|
28
|
+
dataType: "string";
|
|
29
|
+
columnType: "PgText";
|
|
30
|
+
data: string;
|
|
31
|
+
driverParam: string;
|
|
32
|
+
notNull: true;
|
|
33
|
+
hasDefault: true;
|
|
34
|
+
isPrimaryKey: false;
|
|
35
|
+
isAutoincrement: false;
|
|
36
|
+
hasRuntimeDefault: false;
|
|
37
|
+
enumValues: [string, ...string[]];
|
|
38
|
+
baseColumn: never;
|
|
39
|
+
identity: undefined;
|
|
40
|
+
generated: undefined;
|
|
41
|
+
}, {}, {}>;
|
|
25
42
|
seq: import("drizzle-orm/pg-core").PgColumn<{
|
|
26
43
|
name: "seq";
|
|
27
44
|
tableName: "loom_telemetry";
|
package/dist/telemetry/schema.js
CHANGED
|
@@ -21,6 +21,17 @@ import { bigserial, index, jsonb, pgTable, text, timestamp } from "drizzle-orm/p
|
|
|
21
21
|
* duplicated column is a migration away from being wrong.
|
|
22
22
|
*/
|
|
23
23
|
export const loomTelemetry = pgTable("loom_telemetry", {
|
|
24
|
+
/**
|
|
25
|
+
* The project the record belongs to (0260).
|
|
26
|
+
*
|
|
27
|
+
* **The key does not widen here, and that is the difference from the other
|
|
28
|
+
* tables.** `seq` is a surrogate from a sequence rather than anything a
|
|
29
|
+
* caller names: it is the journal's order and its cursor, and a cursor that
|
|
30
|
+
* only orders within a project would stop being a single thread of what
|
|
31
|
+
* happened. So the column is added, the indexes lead with it, and every
|
|
32
|
+
* read filters on it — while `seq` stays unique across the deployment.
|
|
33
|
+
*/
|
|
34
|
+
applicationId: text("application_id").notNull().default("app_default"),
|
|
24
35
|
seq: bigserial("seq", { mode: "number" }).primaryKey(),
|
|
25
36
|
treeId: text("tree_id").notNull(),
|
|
26
37
|
/** When the runtime said it happened, which is not when the row was written. */
|
|
@@ -29,5 +40,20 @@ export const loomTelemetry = pgTable("loom_telemetry", {
|
|
|
29
40
|
recordedAt: timestamp("recorded_at", { withTimezone: true })
|
|
30
41
|
.notNull()
|
|
31
42
|
.default(sql `now()`),
|
|
32
|
-
}, (table) => [
|
|
43
|
+
}, (table) => [
|
|
44
|
+
index("loom_telemetry_tree_seq_idx").on(table.applicationId, table.treeId, table.seq),
|
|
45
|
+
/**
|
|
46
|
+
* The JSON-path index the comment above promised, partial on the one event
|
|
47
|
+
* type that carries an assessment. `assessments` on the journal looks a set
|
|
48
|
+
* of proposals up by this path; without it that is a sequential scan of
|
|
49
|
+
* every record the deployment has ever narrated.
|
|
50
|
+
*
|
|
51
|
+
* Partial rather than whole because the predicate is the discriminant: only
|
|
52
|
+
* `change-assessed` records have the path at all, and an index over the rest
|
|
53
|
+
* would be an entry per row holding nothing.
|
|
54
|
+
*/
|
|
55
|
+
index("loom_telemetry_assessed_proposal_idx")
|
|
56
|
+
.on(table.applicationId, sql `(${table.event} -> 'assessment' ->> 'proposalId')`)
|
|
57
|
+
.where(sql `${table.event} ->> 'type' = 'change-assessed'`),
|
|
58
|
+
]);
|
|
33
59
|
//# sourceMappingURL=schema.js.map
|
package/dist/telemetry/sink.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Result } from "../result.js";
|
|
2
2
|
import type { EventSink } from "../runtime/events.js";
|
|
3
3
|
import { type TelemetryRecord } from "./event.js";
|
|
4
|
-
import type { TelemetryError,
|
|
4
|
+
import type { TelemetryError, TelemetryWriter } from "./journal.js";
|
|
5
5
|
/**
|
|
6
6
|
* The seam between a runtime narrating itself and a journal that outlives the
|
|
7
7
|
* request (0024).
|
|
@@ -38,5 +38,5 @@ export type TelemetryCollector = {
|
|
|
38
38
|
*/
|
|
39
39
|
readonly flush: () => Promise<Result<void, TelemetryError>>;
|
|
40
40
|
};
|
|
41
|
-
export declare const collectTelemetry: (journal:
|
|
41
|
+
export declare const collectTelemetry: (journal: TelemetryWriter) => TelemetryCollector;
|
|
42
42
|
//# sourceMappingURL=sink.d.ts.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Application } from "../application/application.js";
|
|
2
|
+
import type { ApplicationStore } from "../application/store.js";
|
|
3
|
+
/**
|
|
4
|
+
* One suite, two implementations.
|
|
5
|
+
*
|
|
6
|
+
* `memoryApplicationStore` calls itself the reference implementation; this is what
|
|
7
|
+
* makes that claim checkable rather than aspirational, on the same terms
|
|
8
|
+
* `store-contract.ts` and `hold-contract.ts` already set. The alternative —
|
|
9
|
+
* each store asserted against its own expectations — is how two stores that
|
|
10
|
+
* disagree both stay green.
|
|
11
|
+
*/
|
|
12
|
+
export declare const applicationFixture: (over?: Partial<Application>) => Application;
|
|
13
|
+
export declare const describeApplicationStoreContract: (name: string, make: () => Promise<ApplicationStore> | ApplicationStore) => void;
|
|
14
|
+
//# sourceMappingURL=application-contract.d.ts.map
|