@jam-overture/loom 0.1.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/LICENSE +21 -0
- package/README.md +743 -0
- package/dist/catalogue.d.ts +70 -0
- package/dist/catalogue.js +88 -0
- package/dist/cli/args.d.ts +17 -0
- package/dist/cli/args.js +83 -0
- package/dist/cli/filesystem.d.ts +15 -0
- package/dist/cli/filesystem.js +31 -0
- package/dist/cli/index.d.ts +12 -0
- package/dist/cli/index.js +12 -0
- package/dist/cli/main.d.ts +3 -0
- package/dist/cli/main.js +30 -0
- package/dist/cli/namespace.d.ts +40 -0
- package/dist/cli/namespace.js +40 -0
- package/dist/cli/plan.d.ts +84 -0
- package/dist/cli/plan.js +113 -0
- package/dist/cli/run.d.ts +17 -0
- package/dist/cli/run.js +75 -0
- package/dist/cli/templates.d.ts +43 -0
- package/dist/cli/templates.js +134 -0
- package/dist/closed-set.d.ts +34 -0
- package/dist/closed-set.js +34 -0
- package/dist/data/adapter.d.ts +123 -0
- package/dist/data/adapter.js +91 -0
- package/dist/data/binding.d.ts +48 -0
- package/dist/data/binding.js +39 -0
- package/dist/data/catalogue.d.ts +26 -0
- package/dist/data/catalogue.js +6 -0
- package/dist/data/index.d.ts +14 -0
- package/dist/data/index.js +14 -0
- package/dist/data/plan.d.ts +58 -0
- package/dist/data/plan.js +0 -0
- package/dist/data/resolution.d.ts +91 -0
- package/dist/data/resolution.js +96 -0
- package/dist/data/resolve.d.ts +46 -0
- package/dist/data/resolve.js +37 -0
- package/dist/data/source.d.ts +40 -0
- package/dist/data/source.js +45 -0
- package/dist/deadline.d.ts +57 -0
- package/dist/deadline.js +78 -0
- package/dist/frame/catalogue.d.ts +24 -0
- package/dist/frame/catalogue.js +5 -0
- package/dist/frame/index.d.ts +12 -0
- package/dist/frame/index.js +12 -0
- package/dist/frame/origin.d.ts +87 -0
- package/dist/frame/origin.js +114 -0
- package/dist/frame/resolution.d.ts +85 -0
- package/dist/frame/resolution.js +79 -0
- package/dist/grammar.d.ts +26 -0
- package/dist/grammar.js +28 -0
- package/dist/ids.d.ts +57 -0
- package/dist/ids.js +104 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +26 -0
- package/dist/interactivity.d.ts +50 -0
- package/dist/interactivity.js +22 -0
- package/dist/interpretation/anthropic.d.ts +22 -0
- package/dist/interpretation/anthropic.js +76 -0
- package/dist/interpretation/client.d.ts +83 -0
- package/dist/interpretation/client.js +2 -0
- package/dist/interpretation/draft.d.ts +234 -0
- package/dist/interpretation/draft.js +84 -0
- package/dist/interpretation/index.d.ts +15 -0
- package/dist/interpretation/index.js +15 -0
- package/dist/interpretation/interpreter.d.ts +62 -0
- package/dist/interpretation/interpreter.js +151 -0
- package/dist/interpretation/materialize.d.ts +42 -0
- package/dist/interpretation/materialize.js +107 -0
- package/dist/interpretation/prompt.d.ts +229 -0
- package/dist/interpretation/prompt.js +285 -0
- package/dist/interpretation/render.d.ts +76 -0
- package/dist/interpretation/render.js +272 -0
- package/dist/interpretation/schema.d.ts +49 -0
- package/dist/interpretation/schema.js +131 -0
- package/dist/json.d.ts +35 -0
- package/dist/json.js +11 -0
- package/dist/paging.d.ts +60 -0
- package/dist/paging.js +49 -0
- package/dist/primitive-type.d.ts +32 -0
- package/dist/primitive-type.js +39 -0
- package/dist/render/addressing.d.ts +59 -0
- package/dist/render/addressing.js +60 -0
- package/dist/render/anchor.d.ts +105 -0
- package/dist/render/anchor.js +84 -0
- package/dist/render/behaviour-adjust.d.ts +62 -0
- package/dist/render/behaviour-adjust.js +78 -0
- package/dist/render/behaviour-copy.d.ts +53 -0
- package/dist/render/behaviour-copy.js +75 -0
- package/dist/render/behaviour-disclose.d.ts +74 -0
- package/dist/render/behaviour-disclose.js +77 -0
- package/dist/render/behaviour-present.d.ts +123 -0
- package/dist/render/behaviour-present.js +173 -0
- package/dist/render/behaviour.d.ts +243 -0
- package/dist/render/behaviour.js +284 -0
- package/dist/render/control.d.ts +81 -0
- package/dist/render/control.js +80 -0
- package/dist/render/decorative.d.ts +62 -0
- package/dist/render/decorative.js +2 -0
- package/dist/render/diagnostics.d.ts +260 -0
- package/dist/render/diagnostics.js +61 -0
- package/dist/render/disclosed.d.ts +37 -0
- package/dist/render/disclosed.js +37 -0
- package/dist/render/editable.d.ts +34 -0
- package/dist/render/editable.js +35 -0
- package/dist/render/frame.d.ts +42 -0
- package/dist/render/frame.js +31 -0
- package/dist/render/index.d.ts +35 -0
- package/dist/render/index.js +35 -0
- package/dist/render/inline-variables.d.ts +77 -0
- package/dist/render/inline-variables.js +220 -0
- package/dist/render/presented.d.ts +82 -0
- package/dist/render/presented.js +82 -0
- package/dist/render/primitive.d.ts +228 -0
- package/dist/render/primitive.js +36 -0
- package/dist/render/props.d.ts +48 -0
- package/dist/render/props.js +2 -0
- package/dist/render/reads.d.ts +73 -0
- package/dist/render/reads.js +45 -0
- package/dist/render/render.d.ts +164 -0
- package/dist/render/render.js +571 -0
- package/dist/render/request.d.ts +111 -0
- package/dist/render/request.js +60 -0
- package/dist/render/text.d.ts +91 -0
- package/dist/render/text.js +44 -0
- package/dist/render/theme.d.ts +61 -0
- package/dist/render/theme.js +52 -0
- package/dist/reserved-props.d.ts +35 -0
- package/dist/reserved-props.js +45 -0
- package/dist/result.d.ts +27 -0
- package/dist/result.js +30 -0
- package/dist/role.d.ts +71 -0
- package/dist/role.js +22 -0
- package/dist/runtime/analysis.d.ts +149 -0
- package/dist/runtime/analysis.js +187 -0
- package/dist/runtime/assessment.d.ts +35 -0
- package/dist/runtime/assessment.js +13 -0
- package/dist/runtime/disposition.d.ts +102 -0
- package/dist/runtime/disposition.js +59 -0
- package/dist/runtime/events.d.ts +187 -0
- package/dist/runtime/events.js +7 -0
- package/dist/runtime/gate.d.ts +43 -0
- package/dist/runtime/gate.js +169 -0
- package/dist/runtime/index.d.ts +27 -0
- package/dist/runtime/index.js +27 -0
- package/dist/runtime/intent.d.ts +60 -0
- package/dist/runtime/intent.js +36 -0
- package/dist/runtime/interpreter.d.ts +117 -0
- package/dist/runtime/interpreter.js +46 -0
- package/dist/runtime/inverse-interpreter.d.ts +90 -0
- package/dist/runtime/inverse-interpreter.js +56 -0
- package/dist/runtime/inverse.d.ts +87 -0
- package/dist/runtime/inverse.js +48 -0
- package/dist/runtime/narration.d.ts +24 -0
- package/dist/runtime/narration.js +36 -0
- package/dist/runtime/nesting.d.ts +31 -0
- package/dist/runtime/nesting.js +46 -0
- package/dist/runtime/pipeline.d.ts +124 -0
- package/dist/runtime/pipeline.js +178 -0
- package/dist/runtime/policy-fingerprint.d.ts +29 -0
- package/dist/runtime/policy-fingerprint.js +119 -0
- package/dist/runtime/policy-source.d.ts +42 -0
- package/dist/runtime/policy-source.js +8 -0
- package/dist/runtime/policy.d.ts +156 -0
- package/dist/runtime/policy.js +107 -0
- package/dist/runtime/proposal.d.ts +423 -0
- package/dist/runtime/proposal.js +109 -0
- package/dist/runtime/redirection.d.ts +53 -0
- package/dist/runtime/redirection.js +38 -0
- package/dist/runtime/repointing.d.ts +71 -0
- package/dist/runtime/repointing.js +0 -0
- package/dist/runtime/reversibility.d.ts +36 -0
- package/dist/runtime/reversibility.js +39 -0
- package/dist/runtime/stake-level.d.ts +33 -0
- package/dist/runtime/stake-level.js +58 -0
- package/dist/runtime/stakes.d.ts +47 -0
- package/dist/runtime/stakes.js +305 -0
- package/dist/runtime/vocabulary.d.ts +122 -0
- package/dist/runtime/vocabulary.js +87 -0
- package/dist/sdk/audit.d.ts +195 -0
- package/dist/sdk/audit.js +128 -0
- package/dist/sdk/catalogue.d.ts +37 -0
- package/dist/sdk/catalogue.js +16 -0
- package/dist/sdk/conformance.d.ts +342 -0
- package/dist/sdk/conformance.js +494 -0
- package/dist/sdk/copy.d.ts +105 -0
- package/dist/sdk/copy.js +90 -0
- package/dist/sdk/definition.d.ts +242 -0
- package/dist/sdk/definition.js +64 -0
- package/dist/sdk/index.d.ts +19 -0
- package/dist/sdk/index.js +19 -0
- package/dist/sdk/interactivity.d.ts +29 -0
- package/dist/sdk/interactivity.js +34 -0
- package/dist/sdk/pairings.d.ts +124 -0
- package/dist/sdk/pairings.js +81 -0
- package/dist/sdk/registry.d.ts +162 -0
- package/dist/sdk/registry.js +329 -0
- package/dist/sdk/selection.d.ts +46 -0
- package/dist/sdk/selection.js +24 -0
- package/dist/sdk/text.d.ts +100 -0
- package/dist/sdk/text.js +105 -0
- package/dist/sdk/vocabulary.d.ts +39 -0
- package/dist/sdk/vocabulary.js +36 -0
- package/dist/signals/broadcast.d.ts +134 -0
- package/dist/signals/broadcast.js +451 -0
- package/dist/signals/collect.d.ts +184 -0
- package/dist/signals/collect.js +196 -0
- package/dist/signals/deliver.d.ts +64 -0
- package/dist/signals/deliver.js +85 -0
- package/dist/signals/fold.d.ts +112 -0
- package/dist/signals/fold.js +76 -0
- package/dist/signals/index.d.ts +31 -0
- package/dist/signals/index.js +31 -0
- package/dist/signals/ingest.d.ts +51 -0
- package/dist/signals/ingest.js +70 -0
- package/dist/signals/intake.d.ts +149 -0
- package/dist/signals/intake.js +154 -0
- package/dist/signals/journal.d.ts +94 -0
- package/dist/signals/journal.js +11 -0
- package/dist/signals/kinds.d.ts +22 -0
- package/dist/signals/kinds.js +22 -0
- package/dist/signals/ledger.d.ts +46 -0
- package/dist/signals/ledger.js +81 -0
- package/dist/signals/memory.d.ts +17 -0
- package/dist/signals/memory.js +99 -0
- package/dist/signals/migrate.d.ts +27 -0
- package/dist/signals/migrate.js +78 -0
- package/dist/signals/postgres.d.ts +7 -0
- package/dist/signals/postgres.js +295 -0
- package/dist/signals/rollup.d.ts +142 -0
- package/dist/signals/rollup.js +0 -0
- package/dist/signals/schema.d.ts +534 -0
- package/dist/signals/schema.js +92 -0
- package/dist/signals/signal.d.ts +381 -0
- package/dist/signals/signal.js +150 -0
- package/dist/signals/tally.d.ts +70 -0
- package/dist/signals/tally.js +2 -0
- package/dist/signals/view.d.ts +51 -0
- package/dist/signals/view.js +54 -0
- package/dist/store/attribution.d.ts +125 -0
- package/dist/store/attribution.js +116 -0
- package/dist/store/database.d.ts +14 -0
- package/dist/store/database.js +2 -0
- package/dist/store/driver.d.ts +29 -0
- package/dist/store/driver.js +39 -0
- package/dist/store/errors.d.ts +56 -0
- package/dist/store/errors.js +37 -0
- package/dist/store/index.d.ts +16 -0
- package/dist/store/index.js +16 -0
- package/dist/store/memory.d.ts +3 -0
- package/dist/store/memory.js +115 -0
- package/dist/store/migrate.d.ts +54 -0
- package/dist/store/migrate.js +122 -0
- package/dist/store/postgres-holds.d.ts +4 -0
- package/dist/store/postgres-holds.js +192 -0
- package/dist/store/postgres.d.ts +8 -0
- package/dist/store/postgres.js +262 -0
- package/dist/store/replay.d.ts +94 -0
- package/dist/store/replay.js +82 -0
- package/dist/store/revert.d.ts +116 -0
- package/dist/store/revert.js +243 -0
- package/dist/store/schema.d.ts +371 -0
- package/dist/store/schema.js +78 -0
- package/dist/store/source.d.ts +23 -0
- package/dist/store/source.js +37 -0
- package/dist/store/store.d.ts +231 -0
- package/dist/store/store.js +52 -0
- package/dist/store/undone.d.ts +18 -0
- package/dist/store/undone.js +89 -0
- package/dist/submit/catalogue.d.ts +20 -0
- package/dist/submit/catalogue.js +5 -0
- package/dist/submit/declaration.d.ts +39 -0
- package/dist/submit/declaration.js +30 -0
- package/dist/submit/endpoint.d.ts +131 -0
- package/dist/submit/endpoint.js +160 -0
- package/dist/submit/index.d.ts +13 -0
- package/dist/submit/index.js +13 -0
- package/dist/submit/plan.d.ts +49 -0
- package/dist/submit/plan.js +47 -0
- package/dist/submit/resolution.d.ts +61 -0
- package/dist/submit/resolution.js +64 -0
- package/dist/submit/resolve.d.ts +45 -0
- package/dist/submit/resolve.js +36 -0
- package/dist/telemetry/calibration.d.ts +132 -0
- package/dist/telemetry/calibration.js +151 -0
- package/dist/telemetry/episode.d.ts +140 -0
- package/dist/telemetry/episode.js +272 -0
- package/dist/telemetry/event.d.ts +2310 -0
- package/dist/telemetry/event.js +405 -0
- package/dist/telemetry/index.d.ts +15 -0
- package/dist/telemetry/index.js +15 -0
- package/dist/telemetry/journal.d.ts +116 -0
- package/dist/telemetry/journal.js +11 -0
- package/dist/telemetry/memory.d.ts +24 -0
- package/dist/telemetry/memory.js +75 -0
- package/dist/telemetry/migrate.d.ts +26 -0
- package/dist/telemetry/migrate.js +40 -0
- package/dist/telemetry/postgres.d.ts +6 -0
- package/dist/telemetry/postgres.js +139 -0
- package/dist/telemetry/retention.d.ts +131 -0
- package/dist/telemetry/retention.js +217 -0
- package/dist/telemetry/schema.d.ts +113 -0
- package/dist/telemetry/schema.js +33 -0
- package/dist/telemetry/sink.d.ts +42 -0
- package/dist/telemetry/sink.js +61 -0
- package/dist/testing/contracts.d.ts +34 -0
- package/dist/testing/contracts.js +34 -0
- package/dist/testing/definitions.d.ts +15 -0
- package/dist/testing/definitions.js +48 -0
- package/dist/testing/doubles.d.ts +94 -0
- package/dist/testing/doubles.js +141 -0
- package/dist/testing/episode-harness.d.ts +49 -0
- package/dist/testing/episode-harness.js +65 -0
- package/dist/testing/filesystem.d.ts +17 -0
- package/dist/testing/filesystem.js +17 -0
- package/dist/testing/fixtures.d.ts +57 -0
- package/dist/testing/fixtures.js +84 -0
- package/dist/testing/hold-contract.d.ts +19 -0
- package/dist/testing/hold-contract.js +374 -0
- package/dist/testing/index.d.ts +41 -0
- package/dist/testing/index.js +41 -0
- package/dist/testing/journal-contract.d.ts +17 -0
- package/dist/testing/journal-contract.js +353 -0
- package/dist/testing/model-replies.d.ts +34 -0
- package/dist/testing/model-replies.js +101 -0
- package/dist/testing/primitives.d.ts +7 -0
- package/dist/testing/primitives.js +29 -0
- package/dist/testing/reader-signal-contract.d.ts +38 -0
- package/dist/testing/reader-signal-contract.js +318 -0
- package/dist/testing/row-security.d.ts +17 -0
- package/dist/testing/row-security.js +21 -0
- package/dist/testing/store-contract.d.ts +25 -0
- package/dist/testing/store-contract.js +500 -0
- package/dist/theme/apply.d.ts +17 -0
- package/dist/theme/apply.js +60 -0
- package/dist/theme/contrast.d.ts +197 -0
- package/dist/theme/contrast.js +259 -0
- package/dist/theme/derive.d.ts +77 -0
- package/dist/theme/derive.js +167 -0
- package/dist/theme/faces.d.ts +73 -0
- package/dist/theme/faces.js +119 -0
- package/dist/theme/font-packs.d.ts +25 -0
- package/dist/theme/font-packs.js +257 -0
- package/dist/theme/index.d.ts +20 -0
- package/dist/theme/index.js +20 -0
- package/dist/theme/lab.d.ts +30 -0
- package/dist/theme/lab.js +46 -0
- package/dist/theme/library.d.ts +114 -0
- package/dist/theme/library.js +300 -0
- package/dist/theme/measure.d.ts +144 -0
- package/dist/theme/measure.js +145 -0
- package/dist/theme/palettes.d.ts +64 -0
- package/dist/theme/palettes.js +497 -0
- package/dist/theme/registry.d.ts +59 -0
- package/dist/theme/registry.js +60 -0
- package/dist/theme/separation.d.ts +177 -0
- package/dist/theme/separation.js +203 -0
- package/dist/theme/style-presets.d.ts +60 -0
- package/dist/theme/style-presets.js +124 -0
- package/dist/theme/theme.d.ts +253 -0
- package/dist/theme/theme.js +171 -0
- package/dist/tree/apply.d.ts +16 -0
- package/dist/tree/apply.js +104 -0
- package/dist/tree/builders.d.ts +19 -0
- package/dist/tree/builders.js +20 -0
- package/dist/tree/compare.d.ts +68 -0
- package/dist/tree/compare.js +75 -0
- package/dist/tree/configuration.d.ts +25 -0
- package/dist/tree/configuration.js +108 -0
- package/dist/tree/delta.d.ts +288 -0
- package/dist/tree/delta.js +88 -0
- package/dist/tree/errors.d.ts +69 -0
- package/dist/tree/errors.js +34 -0
- package/dist/tree/identity.d.ts +117 -0
- package/dist/tree/identity.js +145 -0
- package/dist/tree/index.d.ts +21 -0
- package/dist/tree/index.js +21 -0
- package/dist/tree/inverse.d.ts +24 -0
- package/dist/tree/inverse.js +112 -0
- package/dist/tree/mutation.d.ts +18 -0
- package/dist/tree/mutation.js +49 -0
- package/dist/tree/naming.d.ts +4 -0
- package/dist/tree/naming.js +35 -0
- package/dist/tree/navigation.d.ts +38 -0
- package/dist/tree/navigation.js +83 -0
- package/dist/tree/node.d.ts +103 -0
- package/dist/tree/node.js +46 -0
- package/dist/tree/outline.d.ts +28 -0
- package/dist/tree/outline.js +10 -0
- package/dist/tree/tree.d.ts +83 -0
- package/dist/tree/tree.js +62 -0
- package/dist/write/commit.d.ts +124 -0
- package/dist/write/commit.js +234 -0
- package/dist/write/held.d.ts +674 -0
- package/dist/write/held.js +185 -0
- package/dist/write/index.d.ts +11 -0
- package/dist/write/index.js +11 -0
- package/dist/write/liveness.d.ts +132 -0
- package/dist/write/liveness.js +91 -0
- package/dist/write/revert.d.ts +79 -0
- package/dist/write/revert.js +134 -0
- package/package.json +160 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { LoomDatabase } from "../store/database.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creating the table the telemetry journal writes to, with its row level
|
|
4
|
+
* security switched on.
|
|
5
|
+
*
|
|
6
|
+
* The same shape as the store's migration, kept separate because a deployment
|
|
7
|
+
* may keep its journal somewhere its trees are not.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* The journal's schema, as statements a host can run.
|
|
11
|
+
*
|
|
12
|
+
* One statement per entry for the same reason the store's DDL is a list: a
|
|
13
|
+
* driver may refuse several commands in a single prepared statement, and
|
|
14
|
+
* transaction-mode pooling — the configuration 0022 requires on serverless — is
|
|
15
|
+
* exactly where that bites.
|
|
16
|
+
*
|
|
17
|
+
* The journal is the table where RLS matters most and is least likely to be
|
|
18
|
+
* remembered: it holds every utterance a person typed and every rationale a
|
|
19
|
+
* model wrote back, and it arrived after the deployment instructions were
|
|
20
|
+
* written. See `src/store/migrate.ts` for why the statement is here rather than
|
|
21
|
+
* in a document.
|
|
22
|
+
*/
|
|
23
|
+
export declare const TELEMETRY_DDL: readonly string[];
|
|
24
|
+
/** Idempotent, so running it against a populated database is a no-op. */
|
|
25
|
+
export declare const ensureTelemetrySchema: (db: LoomDatabase) => Promise<void>;
|
|
26
|
+
//# sourceMappingURL=migrate.d.ts.map
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { sql } from "drizzle-orm";
|
|
2
|
+
/**
|
|
3
|
+
* Creating the table the telemetry journal writes to, with its row level
|
|
4
|
+
* security switched on.
|
|
5
|
+
*
|
|
6
|
+
* The same shape as the store's migration, kept separate because a deployment
|
|
7
|
+
* may keep its journal somewhere its trees are not.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* The journal's schema, as statements a host can run.
|
|
11
|
+
*
|
|
12
|
+
* One statement per entry for the same reason the store's DDL is a list: a
|
|
13
|
+
* driver may refuse several commands in a single prepared statement, and
|
|
14
|
+
* transaction-mode pooling — the configuration 0022 requires on serverless — is
|
|
15
|
+
* exactly where that bites.
|
|
16
|
+
*
|
|
17
|
+
* The journal is the table where RLS matters most and is least likely to be
|
|
18
|
+
* remembered: it holds every utterance a person typed and every rationale a
|
|
19
|
+
* model wrote back, and it arrived after the deployment instructions were
|
|
20
|
+
* written. See `src/store/migrate.ts` for why the statement is here rather than
|
|
21
|
+
* in a document.
|
|
22
|
+
*/
|
|
23
|
+
export const TELEMETRY_DDL = [
|
|
24
|
+
`CREATE TABLE IF NOT EXISTS loom_telemetry (
|
|
25
|
+
seq bigserial PRIMARY KEY,
|
|
26
|
+
tree_id text NOT NULL,
|
|
27
|
+
occurred_at text NOT NULL,
|
|
28
|
+
event jsonb NOT NULL,
|
|
29
|
+
recorded_at timestamptz NOT NULL DEFAULT now()
|
|
30
|
+
)`,
|
|
31
|
+
`CREATE INDEX IF NOT EXISTS loom_telemetry_tree_seq_idx ON loom_telemetry (tree_id, seq)`,
|
|
32
|
+
`ALTER TABLE loom_telemetry ENABLE ROW LEVEL SECURITY`,
|
|
33
|
+
];
|
|
34
|
+
/** Idempotent, so running it against a populated database is a no-op. */
|
|
35
|
+
export const ensureTelemetrySchema = async (db) => {
|
|
36
|
+
for (const statement of TELEMETRY_DDL) {
|
|
37
|
+
await db.execute(sql.raw(statement));
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=migrate.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { LoomDatabase } from "../store/database.js";
|
|
2
|
+
import { type TelemetryJournal } from "./journal.js";
|
|
3
|
+
export * from "./migrate.js";
|
|
4
|
+
export * from "./schema.js";
|
|
5
|
+
export declare const postgresTelemetryJournal: (db: LoomDatabase) => TelemetryJournal;
|
|
6
|
+
//# sourceMappingURL=postgres.d.ts.map
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { and, asc, desc, eq, gt, lt } from "drizzle-orm";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { treeIdSchema } from "../ids.js";
|
|
4
|
+
import { cursorPosition, pageEnds } from "../paging.js";
|
|
5
|
+
import { err, ok } from "../result.js";
|
|
6
|
+
import { telemetryEventSchema } from "./event.js";
|
|
7
|
+
import { clampTelemetryLimit, } from "./journal.js";
|
|
8
|
+
import { loomTelemetry } from "./schema.js";
|
|
9
|
+
export * from "./migrate.js";
|
|
10
|
+
export * from "./schema.js";
|
|
11
|
+
/**
|
|
12
|
+
* The journal, backed by Postgres — the same database the store uses (0022),
|
|
13
|
+
* and the same rule about how it is reached: plain SQL through Drizzle, never
|
|
14
|
+
* PostgREST.
|
|
15
|
+
*
|
|
16
|
+
* The write is one multi-row insert rather than a transaction. A batch that
|
|
17
|
+
* lands partially would be worse than one that fails, and a single statement is
|
|
18
|
+
* already atomic in Postgres, so the guarantee comes free — there is no second
|
|
19
|
+
* table to keep in step, which is the whole reason the store needed a
|
|
20
|
+
* transaction and this does not.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Rows are parsed on the way out, never cast. A record written months ago by an
|
|
24
|
+
* older narrowing must not reach a fold unvalidated; the journal is a boundary
|
|
25
|
+
* like any other, and the one where data most reliably outlives its code.
|
|
26
|
+
*/
|
|
27
|
+
const recordedSchema = z.object({
|
|
28
|
+
seq: z.number().int().nonnegative(),
|
|
29
|
+
treeId: treeIdSchema,
|
|
30
|
+
occurredAt: z.string().datetime(),
|
|
31
|
+
/**
|
|
32
|
+
* A driver hands back `timestamptz` as a `Date`, and everything above this
|
|
33
|
+
* boundary speaks ISO strings. Normalising here rather than at each reader
|
|
34
|
+
* keeps "an instant is a string" true for the whole codebase; accepting a
|
|
35
|
+
* string as well means a driver configured to return one is not a parse
|
|
36
|
+
* failure at 3am.
|
|
37
|
+
*/
|
|
38
|
+
recordedAt: z
|
|
39
|
+
.union([z.date(), z.string()])
|
|
40
|
+
.transform((value) => (value instanceof Date ? value : new Date(value)))
|
|
41
|
+
.refine((value) => Number.isFinite(value.getTime()), "recorded_at is not an instant")
|
|
42
|
+
.transform((value) => value.toISOString()),
|
|
43
|
+
event: telemetryEventSchema,
|
|
44
|
+
});
|
|
45
|
+
const failure = (cause, detail) => ({
|
|
46
|
+
code: "unavailable",
|
|
47
|
+
detail: `${detail}: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
48
|
+
});
|
|
49
|
+
export const postgresTelemetryJournal = (db) => ({
|
|
50
|
+
record: async (batch) => {
|
|
51
|
+
if (batch.length === 0)
|
|
52
|
+
return ok(undefined);
|
|
53
|
+
try {
|
|
54
|
+
await db.insert(loomTelemetry).values(batch.map((entry) => ({
|
|
55
|
+
treeId: entry.treeId,
|
|
56
|
+
occurredAt: entry.occurredAt,
|
|
57
|
+
event: entry.event,
|
|
58
|
+
})));
|
|
59
|
+
return ok(undefined);
|
|
60
|
+
}
|
|
61
|
+
catch (cause) {
|
|
62
|
+
return err(failure(cause, `could not record ${batch.length} telemetry events`));
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
read: async (request) => {
|
|
66
|
+
const limit = clampTelemetryLimit(request?.limit);
|
|
67
|
+
const from = cursorPosition(request?.cursor);
|
|
68
|
+
const direction = request?.direction ?? "newer";
|
|
69
|
+
try {
|
|
70
|
+
/**
|
|
71
|
+
* `older` scans descending so the newest page costs one index seek rather
|
|
72
|
+
* than a walk from the start of the journal, then the rows are put back in
|
|
73
|
+
* arrival order — the contract's promise that a page's order never depends
|
|
74
|
+
* on which end it came from.
|
|
75
|
+
*/
|
|
76
|
+
const rows = await db
|
|
77
|
+
.select({
|
|
78
|
+
seq: loomTelemetry.seq,
|
|
79
|
+
treeId: loomTelemetry.treeId,
|
|
80
|
+
occurredAt: loomTelemetry.occurredAt,
|
|
81
|
+
recordedAt: loomTelemetry.recordedAt,
|
|
82
|
+
event: loomTelemetry.event,
|
|
83
|
+
})
|
|
84
|
+
.from(loomTelemetry)
|
|
85
|
+
.where(and(request?.treeId === undefined ? undefined : eq(loomTelemetry.treeId, request.treeId), from === undefined
|
|
86
|
+
? undefined
|
|
87
|
+
: direction === "older"
|
|
88
|
+
? lt(loomTelemetry.seq, from)
|
|
89
|
+
: gt(loomTelemetry.seq, from)))
|
|
90
|
+
.orderBy(direction === "older" ? desc(loomTelemetry.seq) : asc(loomTelemetry.seq))
|
|
91
|
+
/** One extra row answers "is there another page" without a count. */
|
|
92
|
+
.limit(limit + 1);
|
|
93
|
+
const taken = rows.slice(0, limit);
|
|
94
|
+
const parsed = z
|
|
95
|
+
.array(recordedSchema)
|
|
96
|
+
.safeParse(direction === "older" ? taken.slice().reverse() : taken);
|
|
97
|
+
if (!parsed.success) {
|
|
98
|
+
return err({
|
|
99
|
+
code: "unavailable",
|
|
100
|
+
detail: `a stored telemetry record did not parse: ${parsed.error.issues[0]?.path.join(".") ?? "unknown"}`,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
const page = parsed.data;
|
|
104
|
+
return ok({
|
|
105
|
+
records: page,
|
|
106
|
+
...pageEnds(page.map((record) => record.seq), direction, {
|
|
107
|
+
beyond: rows.length > limit,
|
|
108
|
+
resumed: from !== undefined,
|
|
109
|
+
}),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
catch (cause) {
|
|
113
|
+
return err(failure(cause, "could not read telemetry"));
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
/**
|
|
117
|
+
* One statement, and the count comes from the rows it returns rather than
|
|
118
|
+
* from a driver-specific `rowCount` field — the two drivers this runs on
|
|
119
|
+
* report that differently, and a number that means something else under
|
|
120
|
+
* PGlite than under postgres.js is worse than no number.
|
|
121
|
+
*
|
|
122
|
+
* Returning only `seq` keeps the payload to one integer per row, and the
|
|
123
|
+
* caller has already bounded how many rows there can be: `applyRetention`
|
|
124
|
+
* derives `before` from a scan it capped itself.
|
|
125
|
+
*/
|
|
126
|
+
forget: async ({ before }) => {
|
|
127
|
+
try {
|
|
128
|
+
const removed = await db
|
|
129
|
+
.delete(loomTelemetry)
|
|
130
|
+
.where(lt(loomTelemetry.seq, before))
|
|
131
|
+
.returning({ seq: loomTelemetry.seq });
|
|
132
|
+
return ok({ removed: removed.length });
|
|
133
|
+
}
|
|
134
|
+
catch (cause) {
|
|
135
|
+
return err(failure(cause, `could not forget telemetry below ${before}`));
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
//# sourceMappingURL=postgres.js.map
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type Clock } from "../runtime/events.js";
|
|
3
|
+
import type { RecordedTelemetry, TelemetryError, TelemetryJournal } from "./journal.js";
|
|
4
|
+
/**
|
|
5
|
+
* How a journal is allowed to forget.
|
|
6
|
+
*
|
|
7
|
+
* 0016 made the revision log the truth and 0023 made telemetry a narrowing of a
|
|
8
|
+
* stream the log already holds, and together those are what make this safe to
|
|
9
|
+
* build at all: forgetting telemetry loses the account of what was proposed and
|
|
10
|
+
* judged, and cannot lose a tree, a revision, or a held proposal. That is why
|
|
11
|
+
* retention exists here and nowhere near `src/store/`.
|
|
12
|
+
*
|
|
13
|
+
* Two rules shape everything below.
|
|
14
|
+
*
|
|
15
|
+
* **Forgetting is a prefix.** A journal drops its oldest records or none; it
|
|
16
|
+
* never punches holes. A hole would make `seq` — an opaque increasing position,
|
|
17
|
+
* never a count — start meaning something a reader could misread, and it would
|
|
18
|
+
* make "is this window complete" unanswerable.
|
|
19
|
+
*
|
|
20
|
+
* **An episode is never cut in half.** A window that kept a commit but forgot
|
|
21
|
+
* the proposal it committed reads as a change nobody proposed. `episodesOf`
|
|
22
|
+
* already reports records it cannot attribute, so the damage would be visible —
|
|
23
|
+
* but a journal that manufactures the exact fault its own fold exists to detect
|
|
24
|
+
* is not a journal anyone should trust.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* The floor is not a style choice. A policy of zero would empty the journal on
|
|
28
|
+
* the next run, and the most likely way to write one is a units mistake — days
|
|
29
|
+
* where milliseconds were wanted. An hour is short enough to never obstruct a
|
|
30
|
+
* host that means it and long enough that `maxAgeMs: 7` is refused rather than
|
|
31
|
+
* obeyed.
|
|
32
|
+
*/
|
|
33
|
+
export declare const MIN_RETENTION_MS: number;
|
|
34
|
+
export declare const retentionPolicySchema: z.ZodObject<{
|
|
35
|
+
maxAgeMs: z.ZodNumber;
|
|
36
|
+
}, "strip", z.ZodTypeAny, {
|
|
37
|
+
maxAgeMs: number;
|
|
38
|
+
}, {
|
|
39
|
+
maxAgeMs: number;
|
|
40
|
+
}>;
|
|
41
|
+
export type RetentionPolicy = z.infer<typeof retentionPolicySchema>;
|
|
42
|
+
/**
|
|
43
|
+
* How much of the journal one run will look at.
|
|
44
|
+
*
|
|
45
|
+
* Retention is incremental on purpose: a first run against a journal that has
|
|
46
|
+
* never been pruned forgets a bounded chunk, and the next run forgets more.
|
|
47
|
+
* That keeps the operation's cost predictable — an unbounded scan to fix an
|
|
48
|
+
* unbounded journal is the problem restated — and it makes the operation safe
|
|
49
|
+
* to run on a schedule without knowing how far behind it is.
|
|
50
|
+
*/
|
|
51
|
+
export declare const DEFAULT_RETENTION_SCAN = 5000;
|
|
52
|
+
export declare const MAX_RETENTION_SCAN = 50000;
|
|
53
|
+
export type RetentionPlan = {
|
|
54
|
+
/**
|
|
55
|
+
* Forget every record below this position. `null` when nothing may go yet —
|
|
56
|
+
* either the journal holds nothing old enough, or the oldest thing in it is
|
|
57
|
+
* an episode still waiting on someone.
|
|
58
|
+
*/
|
|
59
|
+
readonly before: number | null;
|
|
60
|
+
readonly forgets: number;
|
|
61
|
+
/** Older than the horizon, kept because their own episode is unsettled. */
|
|
62
|
+
readonly keptUnsettled: number;
|
|
63
|
+
/**
|
|
64
|
+
* Older than the horizon and settled, kept only because an unsettled episode
|
|
65
|
+
* sits in front of them. Reported separately because it is the price of the
|
|
66
|
+
* prefix rule, and a host watching this number stay large has learned that
|
|
67
|
+
* something very old is still waiting on an answer.
|
|
68
|
+
*/
|
|
69
|
+
readonly keptBehind: number;
|
|
70
|
+
readonly unsettledEpisodes: number;
|
|
71
|
+
};
|
|
72
|
+
export declare const horizonOf: (policy: RetentionPolicy, now: string) => string | null;
|
|
73
|
+
/**
|
|
74
|
+
* What may be forgotten, as a pure function of records and an instant.
|
|
75
|
+
*
|
|
76
|
+
* Takes the journal from its oldest record forward, in arrival order. It finds
|
|
77
|
+
* its own ceiling rather than trusting the caller to have filtered: the first
|
|
78
|
+
* record at or after the horizon ends the candidate window, and everything from
|
|
79
|
+
* there on is young enough to keep regardless. Doing that here means the whole
|
|
80
|
+
* decision is testable against a list of records, with no journal, no clock and
|
|
81
|
+
* no paging in the way.
|
|
82
|
+
*
|
|
83
|
+
* A record whose episode this window never saw begin is forgettable. The window
|
|
84
|
+
* starts at the journal's oldest record, so nothing earlier exists to attribute
|
|
85
|
+
* it to — it is already an orphan, and keeping it only preserves the orphan.
|
|
86
|
+
*/
|
|
87
|
+
export declare const retentionPlanOf: (records: readonly RecordedTelemetry[], horizon: string) => RetentionPlan;
|
|
88
|
+
export type RetentionRequest = {
|
|
89
|
+
readonly policy: RetentionPolicy;
|
|
90
|
+
readonly clock?: Clock;
|
|
91
|
+
readonly scanLimit?: number;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* What a run did, in the shape of `SnapshotAudit`: an outcome a host can log,
|
|
95
|
+
* show, or ignore, and never an exception. Nothing downstream depends on
|
|
96
|
+
* retention having happened, so a run that could not read the journal is a
|
|
97
|
+
* reported non-event rather than a fault anyone has to handle.
|
|
98
|
+
*/
|
|
99
|
+
export type RetentionOutcome = {
|
|
100
|
+
readonly outcome: "forgot";
|
|
101
|
+
readonly plan: RetentionPlan;
|
|
102
|
+
readonly removed: number;
|
|
103
|
+
} | {
|
|
104
|
+
readonly outcome: "nothing-to-forget";
|
|
105
|
+
readonly plan: RetentionPlan;
|
|
106
|
+
}
|
|
107
|
+
/** The policy or the clock was not usable. Nothing was read and nothing was deleted. */
|
|
108
|
+
| {
|
|
109
|
+
readonly outcome: "refused";
|
|
110
|
+
readonly reason: string;
|
|
111
|
+
} | {
|
|
112
|
+
readonly outcome: "unavailable";
|
|
113
|
+
readonly error: TelemetryError;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Reads the oldest end of a journal, decides what may go, and drops it.
|
|
117
|
+
*
|
|
118
|
+
* The judgment is `retentionPlanOf`'s and the deletion is the journal's; this
|
|
119
|
+
* only carries a decision from one to the other. That split is what lets the
|
|
120
|
+
* rule be tested exhaustively against a list of records while the two storage
|
|
121
|
+
* implementations are held to a contract that says nothing about episodes.
|
|
122
|
+
*
|
|
123
|
+
* Nothing schedules this. A host runs it — from a cron route, a deploy hook, or
|
|
124
|
+
* by hand — for the same reason nothing schedules `auditSnapshot`: when to
|
|
125
|
+
* forget is an operational choice, and a framework that quietly deleted a
|
|
126
|
+
* host's history on a timer it chose would be making it.
|
|
127
|
+
*/
|
|
128
|
+
export declare const applyRetention: (journal: TelemetryJournal, request: RetentionRequest) => Promise<RetentionOutcome>;
|
|
129
|
+
/** One line for an operator, because an outcome nobody can read is an outcome nobody acts on. */
|
|
130
|
+
export declare const describeRetention: (outcome: RetentionOutcome) => string;
|
|
131
|
+
//# sourceMappingURL=retention.d.ts.map
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { clampLimit } from "../paging.js";
|
|
3
|
+
import { systemClock } from "../runtime/events.js";
|
|
4
|
+
import { episodesOf } from "./episode.js";
|
|
5
|
+
import { intentIdOf, proposalIdOf } from "./event.js";
|
|
6
|
+
/**
|
|
7
|
+
* How a journal is allowed to forget.
|
|
8
|
+
*
|
|
9
|
+
* 0016 made the revision log the truth and 0023 made telemetry a narrowing of a
|
|
10
|
+
* stream the log already holds, and together those are what make this safe to
|
|
11
|
+
* build at all: forgetting telemetry loses the account of what was proposed and
|
|
12
|
+
* judged, and cannot lose a tree, a revision, or a held proposal. That is why
|
|
13
|
+
* retention exists here and nowhere near `src/store/`.
|
|
14
|
+
*
|
|
15
|
+
* Two rules shape everything below.
|
|
16
|
+
*
|
|
17
|
+
* **Forgetting is a prefix.** A journal drops its oldest records or none; it
|
|
18
|
+
* never punches holes. A hole would make `seq` — an opaque increasing position,
|
|
19
|
+
* never a count — start meaning something a reader could misread, and it would
|
|
20
|
+
* make "is this window complete" unanswerable.
|
|
21
|
+
*
|
|
22
|
+
* **An episode is never cut in half.** A window that kept a commit but forgot
|
|
23
|
+
* the proposal it committed reads as a change nobody proposed. `episodesOf`
|
|
24
|
+
* already reports records it cannot attribute, so the damage would be visible —
|
|
25
|
+
* but a journal that manufactures the exact fault its own fold exists to detect
|
|
26
|
+
* is not a journal anyone should trust.
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* The floor is not a style choice. A policy of zero would empty the journal on
|
|
30
|
+
* the next run, and the most likely way to write one is a units mistake — days
|
|
31
|
+
* where milliseconds were wanted. An hour is short enough to never obstruct a
|
|
32
|
+
* host that means it and long enough that `maxAgeMs: 7` is refused rather than
|
|
33
|
+
* obeyed.
|
|
34
|
+
*/
|
|
35
|
+
export const MIN_RETENTION_MS = 60 * 60 * 1000;
|
|
36
|
+
export const retentionPolicySchema = z.object({
|
|
37
|
+
maxAgeMs: z.number().int().min(MIN_RETENTION_MS),
|
|
38
|
+
});
|
|
39
|
+
/**
|
|
40
|
+
* How much of the journal one run will look at.
|
|
41
|
+
*
|
|
42
|
+
* Retention is incremental on purpose: a first run against a journal that has
|
|
43
|
+
* never been pruned forgets a bounded chunk, and the next run forgets more.
|
|
44
|
+
* That keeps the operation's cost predictable — an unbounded scan to fix an
|
|
45
|
+
* unbounded journal is the problem restated — and it makes the operation safe
|
|
46
|
+
* to run on a schedule without knowing how far behind it is.
|
|
47
|
+
*/
|
|
48
|
+
export const DEFAULT_RETENTION_SCAN = 5_000;
|
|
49
|
+
export const MAX_RETENTION_SCAN = 50_000;
|
|
50
|
+
/** Read in pages like every other log walk (0026), so memory is bounded by the page. */
|
|
51
|
+
const SCAN_PAGE = 500;
|
|
52
|
+
/**
|
|
53
|
+
* Resolutions that mean the window may not have seen the end of the story.
|
|
54
|
+
*
|
|
55
|
+
* `awaiting-answer` is a proposal the Gate held: someone may confirm it
|
|
56
|
+
* tomorrow, and forgetting how it came to be held would leave that answer
|
|
57
|
+
* hanging off nothing. `open` is the fold saying it cannot tell — a window that
|
|
58
|
+
* stops mid-episode, which is exactly what a horizon cutting through one looks
|
|
59
|
+
* like. Every other resolution is an ending, and an ended episode gains no
|
|
60
|
+
* further records.
|
|
61
|
+
*/
|
|
62
|
+
const UNSETTLED = ["awaiting-answer", "open"];
|
|
63
|
+
const EMPTY_PLAN = {
|
|
64
|
+
before: null,
|
|
65
|
+
forgets: 0,
|
|
66
|
+
keptUnsettled: 0,
|
|
67
|
+
keptBehind: 0,
|
|
68
|
+
unsettledEpisodes: 0,
|
|
69
|
+
};
|
|
70
|
+
export const horizonOf = (policy, now) => {
|
|
71
|
+
const instant = Date.parse(now);
|
|
72
|
+
return Number.isFinite(instant) ? new Date(instant - policy.maxAgeMs).toISOString() : null;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The episode each record belongs to, built in one forward pass.
|
|
76
|
+
*
|
|
77
|
+
* An event names its intent directly or names a proposal, and a proposal names
|
|
78
|
+
* its intent on the record that created it — so a forward walk can resolve
|
|
79
|
+
* every record, and the ones it cannot are the records `episodesOf` calls
|
|
80
|
+
* unattributed. Both readings come from `event.ts` rather than being derived
|
|
81
|
+
* again here: two implementations of "which episode is this" would eventually
|
|
82
|
+
* disagree, and the disagreement would be a journal that forgot half a story.
|
|
83
|
+
*/
|
|
84
|
+
const intentsByRecord = (records) => {
|
|
85
|
+
const intentOfProposal = new Map();
|
|
86
|
+
const byRecord = new Map();
|
|
87
|
+
for (const { seq, event } of records) {
|
|
88
|
+
const direct = intentIdOf(event);
|
|
89
|
+
const proposalId = proposalIdOf(event);
|
|
90
|
+
if (direct !== undefined && proposalId !== undefined)
|
|
91
|
+
intentOfProposal.set(proposalId, direct);
|
|
92
|
+
const intentId = direct ?? (proposalId === undefined ? undefined : intentOfProposal.get(proposalId));
|
|
93
|
+
if (intentId !== undefined)
|
|
94
|
+
byRecord.set(seq, intentId);
|
|
95
|
+
}
|
|
96
|
+
return byRecord;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* What may be forgotten, as a pure function of records and an instant.
|
|
100
|
+
*
|
|
101
|
+
* Takes the journal from its oldest record forward, in arrival order. It finds
|
|
102
|
+
* its own ceiling rather than trusting the caller to have filtered: the first
|
|
103
|
+
* record at or after the horizon ends the candidate window, and everything from
|
|
104
|
+
* there on is young enough to keep regardless. Doing that here means the whole
|
|
105
|
+
* decision is testable against a list of records, with no journal, no clock and
|
|
106
|
+
* no paging in the way.
|
|
107
|
+
*
|
|
108
|
+
* A record whose episode this window never saw begin is forgettable. The window
|
|
109
|
+
* starts at the journal's oldest record, so nothing earlier exists to attribute
|
|
110
|
+
* it to — it is already an orphan, and keeping it only preserves the orphan.
|
|
111
|
+
*/
|
|
112
|
+
export const retentionPlanOf = (records, horizon) => {
|
|
113
|
+
const ceiling = records.findIndex((record) => record.recordedAt >= horizon);
|
|
114
|
+
const candidates = ceiling === -1 ? records : records.slice(0, ceiling);
|
|
115
|
+
if (candidates.length === 0)
|
|
116
|
+
return EMPTY_PLAN;
|
|
117
|
+
const byRecord = intentsByRecord(candidates);
|
|
118
|
+
const unsettled = new Set(episodesOf(candidates)
|
|
119
|
+
.episodes.filter((episode) => UNSETTLED.includes(episode.resolution.kind))
|
|
120
|
+
.map((episode) => episode.intentId));
|
|
121
|
+
const isUnsettled = (record) => {
|
|
122
|
+
const intentId = byRecord.get(record.seq);
|
|
123
|
+
return intentId !== undefined && unsettled.has(intentId);
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* The lowest position an unsettled episode reaches. Everything below it is
|
|
127
|
+
* both old enough and finished, so it is the highest cut the prefix rule
|
|
128
|
+
* permits.
|
|
129
|
+
*/
|
|
130
|
+
const firstKept = candidates.find(isUnsettled)?.seq;
|
|
131
|
+
const beyond = ceiling === -1 ? undefined : records[ceiling]?.seq;
|
|
132
|
+
const before = firstKept ?? beyond ?? (candidates.at(-1)?.seq ?? 0) + 1;
|
|
133
|
+
const forgets = candidates.filter((record) => record.seq < before).length;
|
|
134
|
+
const kept = candidates.filter((record) => record.seq >= before);
|
|
135
|
+
return {
|
|
136
|
+
before: forgets === 0 ? null : before,
|
|
137
|
+
forgets,
|
|
138
|
+
keptUnsettled: kept.filter(isUnsettled).length,
|
|
139
|
+
keptBehind: kept.filter((record) => !isUnsettled(record)).length,
|
|
140
|
+
unsettledEpisodes: unsettled.size,
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
const scanFrom = async (journal, horizon, scanLimit) => {
|
|
144
|
+
const collected = [];
|
|
145
|
+
let cursor;
|
|
146
|
+
while (collected.length < scanLimit) {
|
|
147
|
+
const page = await journal.read({
|
|
148
|
+
direction: "newer",
|
|
149
|
+
limit: Math.min(SCAN_PAGE, scanLimit - collected.length),
|
|
150
|
+
...(cursor ? { cursor } : {}),
|
|
151
|
+
});
|
|
152
|
+
if (!page.ok)
|
|
153
|
+
return { ok: false, error: page.error };
|
|
154
|
+
collected.push(...page.value.records);
|
|
155
|
+
/**
|
|
156
|
+
* Stops at the horizon rather than reading to the end of the journal. Every
|
|
157
|
+
* record from here on is young enough to keep, and `retentionPlanOf` needs
|
|
158
|
+
* only the first of them to know where the candidate window closes.
|
|
159
|
+
*/
|
|
160
|
+
if (page.value.records.some((record) => record.recordedAt >= horizon))
|
|
161
|
+
break;
|
|
162
|
+
if (page.value.newer === null)
|
|
163
|
+
break;
|
|
164
|
+
cursor = page.value.newer;
|
|
165
|
+
}
|
|
166
|
+
return { ok: true, records: collected };
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Reads the oldest end of a journal, decides what may go, and drops it.
|
|
170
|
+
*
|
|
171
|
+
* The judgment is `retentionPlanOf`'s and the deletion is the journal's; this
|
|
172
|
+
* only carries a decision from one to the other. That split is what lets the
|
|
173
|
+
* rule be tested exhaustively against a list of records while the two storage
|
|
174
|
+
* implementations are held to a contract that says nothing about episodes.
|
|
175
|
+
*
|
|
176
|
+
* Nothing schedules this. A host runs it — from a cron route, a deploy hook, or
|
|
177
|
+
* by hand — for the same reason nothing schedules `auditSnapshot`: when to
|
|
178
|
+
* forget is an operational choice, and a framework that quietly deleted a
|
|
179
|
+
* host's history on a timer it chose would be making it.
|
|
180
|
+
*/
|
|
181
|
+
export const applyRetention = async (journal, request) => {
|
|
182
|
+
const policy = retentionPolicySchema.safeParse(request.policy);
|
|
183
|
+
if (!policy.success) {
|
|
184
|
+
return { outcome: "refused", reason: policy.error.issues[0]?.message ?? "invalid policy" };
|
|
185
|
+
}
|
|
186
|
+
const now = (request.clock ?? systemClock).now();
|
|
187
|
+
const horizon = horizonOf(policy.data, now);
|
|
188
|
+
if (horizon === null)
|
|
189
|
+
return { outcome: "refused", reason: `the clock read "${now}"` };
|
|
190
|
+
const scanned = await scanFrom(journal, horizon, clampLimit(request.scanLimit, { fallback: DEFAULT_RETENTION_SCAN, max: MAX_RETENTION_SCAN }));
|
|
191
|
+
if (!scanned.ok)
|
|
192
|
+
return { outcome: "unavailable", error: scanned.error };
|
|
193
|
+
const plan = retentionPlanOf(scanned.records, horizon);
|
|
194
|
+
if (plan.before === null)
|
|
195
|
+
return { outcome: "nothing-to-forget", plan };
|
|
196
|
+
const forgotten = await journal.forget({ before: plan.before });
|
|
197
|
+
return forgotten.ok
|
|
198
|
+
? { outcome: "forgot", plan, removed: forgotten.value.removed }
|
|
199
|
+
: { outcome: "unavailable", error: forgotten.error };
|
|
200
|
+
};
|
|
201
|
+
const describePlan = (plan) => plan.keptUnsettled + plan.keptBehind === 0
|
|
202
|
+
? ""
|
|
203
|
+
: `, keeping ${plan.keptUnsettled + plan.keptBehind} older records for ${plan.unsettledEpisodes} unfinished ${plan.unsettledEpisodes === 1 ? "episode" : "episodes"}`;
|
|
204
|
+
/** One line for an operator, because an outcome nobody can read is an outcome nobody acts on. */
|
|
205
|
+
export const describeRetention = (outcome) => {
|
|
206
|
+
switch (outcome.outcome) {
|
|
207
|
+
case "forgot":
|
|
208
|
+
return `forgot ${outcome.removed} telemetry ${outcome.removed === 1 ? "record" : "records"}${describePlan(outcome.plan)}`;
|
|
209
|
+
case "nothing-to-forget":
|
|
210
|
+
return `nothing to forget${describePlan(outcome.plan)}`;
|
|
211
|
+
case "refused":
|
|
212
|
+
return `retention refused: ${outcome.reason}`;
|
|
213
|
+
case "unavailable":
|
|
214
|
+
return `retention could not run: ${outcome.error.detail}`;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
//# sourceMappingURL=retention.js.map
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The table the telemetry journal is stored in, as Drizzle sees it.
|
|
3
|
+
*
|
|
4
|
+
* The column choices here are the ones the journal's promises rest on — arrival
|
|
5
|
+
* order, and one copy of every fact.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* One table, because a journal is one thing: an append-only list of what the
|
|
9
|
+
* runtime narrated.
|
|
10
|
+
*
|
|
11
|
+
* `seq` is a `bigserial` rather than a timestamp because arrival order is the
|
|
12
|
+
* only order that pages correctly — two serverless instances disagree about the
|
|
13
|
+
* clock by more than the gap between two events in one request.
|
|
14
|
+
*
|
|
15
|
+
* `event` is the whole narrowed event as JSON, and its type and its proposal id
|
|
16
|
+
* are *not* also columns. They would be a second copy of a fact that already
|
|
17
|
+
* lives in the document, and two copies of a fact are two things that can
|
|
18
|
+
* disagree. An index on a JSON path is available the day a query needs one; a
|
|
19
|
+
* duplicated column is a migration away from being wrong.
|
|
20
|
+
*/
|
|
21
|
+
export declare const loomTelemetry: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
22
|
+
name: "loom_telemetry";
|
|
23
|
+
schema: undefined;
|
|
24
|
+
columns: {
|
|
25
|
+
seq: import("drizzle-orm/pg-core").PgColumn<{
|
|
26
|
+
name: "seq";
|
|
27
|
+
tableName: "loom_telemetry";
|
|
28
|
+
dataType: "number";
|
|
29
|
+
columnType: "PgBigSerial53";
|
|
30
|
+
data: number;
|
|
31
|
+
driverParam: number;
|
|
32
|
+
notNull: true;
|
|
33
|
+
hasDefault: true;
|
|
34
|
+
isPrimaryKey: true;
|
|
35
|
+
isAutoincrement: false;
|
|
36
|
+
hasRuntimeDefault: false;
|
|
37
|
+
enumValues: undefined;
|
|
38
|
+
baseColumn: never;
|
|
39
|
+
identity: undefined;
|
|
40
|
+
generated: undefined;
|
|
41
|
+
}, {}, {}>;
|
|
42
|
+
treeId: import("drizzle-orm/pg-core").PgColumn<{
|
|
43
|
+
name: "tree_id";
|
|
44
|
+
tableName: "loom_telemetry";
|
|
45
|
+
dataType: "string";
|
|
46
|
+
columnType: "PgText";
|
|
47
|
+
data: string;
|
|
48
|
+
driverParam: string;
|
|
49
|
+
notNull: true;
|
|
50
|
+
hasDefault: false;
|
|
51
|
+
isPrimaryKey: false;
|
|
52
|
+
isAutoincrement: false;
|
|
53
|
+
hasRuntimeDefault: false;
|
|
54
|
+
enumValues: [string, ...string[]];
|
|
55
|
+
baseColumn: never;
|
|
56
|
+
identity: undefined;
|
|
57
|
+
generated: undefined;
|
|
58
|
+
}, {}, {}>;
|
|
59
|
+
occurredAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
60
|
+
name: "occurred_at";
|
|
61
|
+
tableName: "loom_telemetry";
|
|
62
|
+
dataType: "string";
|
|
63
|
+
columnType: "PgText";
|
|
64
|
+
data: string;
|
|
65
|
+
driverParam: string;
|
|
66
|
+
notNull: true;
|
|
67
|
+
hasDefault: false;
|
|
68
|
+
isPrimaryKey: false;
|
|
69
|
+
isAutoincrement: false;
|
|
70
|
+
hasRuntimeDefault: false;
|
|
71
|
+
enumValues: [string, ...string[]];
|
|
72
|
+
baseColumn: never;
|
|
73
|
+
identity: undefined;
|
|
74
|
+
generated: undefined;
|
|
75
|
+
}, {}, {}>;
|
|
76
|
+
event: import("drizzle-orm/pg-core").PgColumn<{
|
|
77
|
+
name: "event";
|
|
78
|
+
tableName: "loom_telemetry";
|
|
79
|
+
dataType: "json";
|
|
80
|
+
columnType: "PgJsonb";
|
|
81
|
+
data: unknown;
|
|
82
|
+
driverParam: unknown;
|
|
83
|
+
notNull: true;
|
|
84
|
+
hasDefault: false;
|
|
85
|
+
isPrimaryKey: false;
|
|
86
|
+
isAutoincrement: false;
|
|
87
|
+
hasRuntimeDefault: false;
|
|
88
|
+
enumValues: undefined;
|
|
89
|
+
baseColumn: never;
|
|
90
|
+
identity: undefined;
|
|
91
|
+
generated: undefined;
|
|
92
|
+
}, {}, {}>;
|
|
93
|
+
recordedAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
94
|
+
name: "recorded_at";
|
|
95
|
+
tableName: "loom_telemetry";
|
|
96
|
+
dataType: "date";
|
|
97
|
+
columnType: "PgTimestamp";
|
|
98
|
+
data: Date;
|
|
99
|
+
driverParam: string;
|
|
100
|
+
notNull: true;
|
|
101
|
+
hasDefault: true;
|
|
102
|
+
isPrimaryKey: false;
|
|
103
|
+
isAutoincrement: false;
|
|
104
|
+
hasRuntimeDefault: false;
|
|
105
|
+
enumValues: undefined;
|
|
106
|
+
baseColumn: never;
|
|
107
|
+
identity: undefined;
|
|
108
|
+
generated: undefined;
|
|
109
|
+
}, {}, {}>;
|
|
110
|
+
};
|
|
111
|
+
dialect: "pg";
|
|
112
|
+
}>;
|
|
113
|
+
//# sourceMappingURL=schema.d.ts.map
|