@kontourai/lookout 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 +55 -0
- package/README.md +163 -0
- package/bin/lookout.mjs +5 -0
- package/dist/src/canonical-value.d.ts +29 -0
- package/dist/src/canonical-value.js +109 -0
- package/dist/src/check-result.d.ts +32 -0
- package/dist/src/check-result.js +1 -0
- package/dist/src/check-runner.d.ts +18 -0
- package/dist/src/check-runner.js +90 -0
- package/dist/src/cli.d.ts +14 -0
- package/dist/src/cli.js +173 -0
- package/dist/src/index.d.ts +20 -0
- package/dist/src/index.js +10 -0
- package/dist/src/observation-store.d.ts +55 -0
- package/dist/src/observation-store.js +241 -0
- package/dist/src/proposal-diff.d.ts +80 -0
- package/dist/src/proposal-diff.js +208 -0
- package/dist/src/provider-resolution.d.ts +4 -0
- package/dist/src/provider-resolution.js +2 -0
- package/dist/src/registry.d.ts +29 -0
- package/dist/src/registry.js +141 -0
- package/dist/src/snapshot-store.d.ts +2 -0
- package/dist/src/snapshot-store.js +5 -0
- package/dist/src/structural-diff.d.ts +24 -0
- package/dist/src/structural-diff.js +94 -0
- package/dist/src/survey-emission.d.ts +68 -0
- package/dist/src/survey-emission.js +138 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
10
|
+
|
|
11
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
12
|
+
|
|
13
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
14
|
+
|
|
15
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
16
|
+
|
|
17
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
18
|
+
|
|
19
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
20
|
+
|
|
21
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work.
|
|
22
|
+
|
|
23
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on or derived from the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link or bind by name to the interfaces of, the Work and Derivative Works thereof.
|
|
24
|
+
|
|
25
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems managed by, or on behalf of, the Licensor for discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
26
|
+
|
|
27
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
28
|
+
|
|
29
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
30
|
+
|
|
31
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution alone or by combination of their Contribution with the Work to which such Contribution was submitted. If You institute patent litigation against any entity, including a cross-claim or counterclaim in a lawsuit, alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
32
|
+
|
|
33
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
34
|
+
|
|
35
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
36
|
+
|
|
37
|
+
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
38
|
+
|
|
39
|
+
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
40
|
+
|
|
41
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
|
42
|
+
|
|
43
|
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
44
|
+
|
|
45
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement You may have executed with Licensor regarding such Contributions.
|
|
46
|
+
|
|
47
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
48
|
+
|
|
49
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
50
|
+
|
|
51
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort, including negligence, contract, or otherwise, unless required by applicable law or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work, including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses, even if such Contributor has been advised of the possibility of such damages.
|
|
52
|
+
|
|
53
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer and charge a fee for acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
54
|
+
|
|
55
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# @kontourai/lookout
|
|
2
|
+
|
|
3
|
+
A small **source registry** and **non-throwing drift-check runner** built on
|
|
4
|
+
[Traverse](https://github.com/kontourai/traverse) snapshots. Lookout answers one
|
|
5
|
+
question per registered source, cheaply and honestly: *did this source change
|
|
6
|
+
since we last looked?*
|
|
7
|
+
|
|
8
|
+
It composes Traverse for fetching and snapshot storage, provides deterministic
|
|
9
|
+
proposal diffing, and authors provenance-bearing Survey inputs. It does **not**
|
|
10
|
+
extract fields, project to Surface, review claims, notify, crawl, or schedule.
|
|
11
|
+
Operational failures are returned as typed results.
|
|
12
|
+
|
|
13
|
+
## Requirements
|
|
14
|
+
|
|
15
|
+
- Node.js `>= 22`.
|
|
16
|
+
- `@kontourai/traverse` `>= 0.14.1`. This is a hard floor, not a preference:
|
|
17
|
+
trustworthy `unchanged-304` classification depends on the validator-scoping fix
|
|
18
|
+
from [traverse#49](https://github.com/kontourai/traverse/issues/49), first
|
|
19
|
+
released in `0.14.1`. Lookout exact-pins `0.14.1`. On an earlier Traverse a
|
|
20
|
+
conditional request could reuse a prior snapshot's validators against a
|
|
21
|
+
different URL and report a **false** `304`, so do not downgrade.
|
|
22
|
+
|
|
23
|
+
## Registry
|
|
24
|
+
|
|
25
|
+
Sources live in a versioned JSON document (default `<cwd>/lookout.sources.json`;
|
|
26
|
+
override with the library path argument or the CLI `--registry` flag):
|
|
27
|
+
|
|
28
|
+
```json
|
|
29
|
+
{
|
|
30
|
+
"version": 1,
|
|
31
|
+
"sources": [
|
|
32
|
+
{
|
|
33
|
+
"id": "docs-home",
|
|
34
|
+
"kind": "web-page",
|
|
35
|
+
"url": "https://example.com/docs",
|
|
36
|
+
"targetSchema": [{ "path": "title", "type": "string", "required": true }],
|
|
37
|
+
"cadenceHint": "daily",
|
|
38
|
+
"renderPolicy": "never"
|
|
39
|
+
}
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Fields per source:
|
|
45
|
+
|
|
46
|
+
| Field | Meaning |
|
|
47
|
+
| --- | --- |
|
|
48
|
+
| `id` | Non-empty, unique within the document. Used for exact lookup and snapshot identity. |
|
|
49
|
+
| `kind` | `web-page` or `api-record`. |
|
|
50
|
+
| `url` | Absolute HTTP(S) URL. |
|
|
51
|
+
| `targetSchema` | Traverse `TargetFieldSchema[]`. Stored inert at L1 (no extraction yet). |
|
|
52
|
+
| `cadenceHint` | Non-empty string; advisory only — Lookout does not schedule. |
|
|
53
|
+
| `renderPolicy` | `never` \| `on-shell-warning` \| `always`. **Inert** at L1 (never mapped to a fetch/render policy). |
|
|
54
|
+
|
|
55
|
+
Validation reports **every** deterministic issue at once (with index/id context)
|
|
56
|
+
and never reads the network. Lookup is exact-id; listing preserves file order —
|
|
57
|
+
no merging, discovery, or remote registries.
|
|
58
|
+
|
|
59
|
+
## Check results
|
|
60
|
+
|
|
61
|
+
A check classifies each source into exactly one of four kinds. Every result
|
|
62
|
+
carries `sourceId`, the registered `sourceUrl`, `checkedAt`, and Traverse
|
|
63
|
+
`warnings`.
|
|
64
|
+
|
|
65
|
+
| `kind` | When | Extra fields |
|
|
66
|
+
| --- | --- | --- |
|
|
67
|
+
| `unchanged-304` | A validator-backed conditional request returned `304`; zero body transfer; the prior snapshot is not re-persisted. | `snapshotRef` |
|
|
68
|
+
| `unchanged-hash` | A full body was fetched and persisted, but its sha256 `bodyHash` equals the prior, **and** it came from the same resource URL — established by Lookout's own comparison. | `priorSnapshotRef`, `currentSnapshotRef` |
|
|
69
|
+
| `changed` | The fresh body differs from the prior (`changeBasis: "hash"`), **or** it is the first successful observation (`changeBasis: "initial"`, `priorSnapshotRef: null`). | `priorSnapshotRef` (nullable), `currentSnapshotRef`, `changeBasis` |
|
|
70
|
+
| `error` | Any operational failure — contained so the runner never rejects. | `origin` (`traverse` \| `lookout`), `error` |
|
|
71
|
+
|
|
72
|
+
`unchanged-hash` asserts same-resource continuity, so it requires **both** an
|
|
73
|
+
identical `bodyHash` **and** the same resource URL as the prior snapshot. If a
|
|
74
|
+
source has moved — the prior snapshot's URL differs from this fetch's — the
|
|
75
|
+
result is `changed` even when the bytes are byte-identical, and the fresh
|
|
76
|
+
snapshot is persisted as the new baseline. (The `unchanged-304` path is already
|
|
77
|
+
resource-scoped by Traverse's validators, so only the hash path needs this
|
|
78
|
+
guard.)
|
|
79
|
+
|
|
80
|
+
`error` results preserve provenance: `origin: "traverse"` carries Traverse's
|
|
81
|
+
discriminated `FetchError` verbatim (its `kind`, and `status` when present);
|
|
82
|
+
`origin: "lookout"` carries a typed `kind` — `prior-read`, `persistence`,
|
|
83
|
+
`dependency-contract`, or `unexpected`.
|
|
84
|
+
|
|
85
|
+
Snapshot references (`snapshotRef` / `priorSnapshotRef` / `currentSnapshotRef`)
|
|
86
|
+
are portable logical refs from Traverse's `buildSnapshotSourceRef` — never
|
|
87
|
+
filesystem paths. Snapshots are stored via Traverse's filesystem store, rooted by
|
|
88
|
+
default at `<cwd>/.kontourai/lookout/snapshots` (override with the CLI
|
|
89
|
+
`--snapshot-root` flag or by injecting a store in library use). Lookout adds no
|
|
90
|
+
custom filenames or retention.
|
|
91
|
+
|
|
92
|
+
## CLI
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
lookout check <id> [--registry <path>] [--snapshot-root <path>]
|
|
96
|
+
lookout check --all [--registry <path>] [--snapshot-root <path>]
|
|
97
|
+
lookout emit-survey <id> --observation <path|-> [--registry <path>] [--observation-root <path>]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
- Emits **exactly one compact JSON object per checked source, per stdout line**
|
|
101
|
+
(JSON Lines), in registry order. Snapshot bodies and resolved secrets are never
|
|
102
|
+
serialized. Human/fatal diagnostics go to stderr.
|
|
103
|
+
- `<id>` and `--all` are mutually exclusive.
|
|
104
|
+
- **Exit codes:** `0` when the requested checks were attempted — *including*
|
|
105
|
+
per-source `error` results (a network failure on one source is a normal,
|
|
106
|
+
emitted result, not a CLI failure). Non-zero only when an invocation or
|
|
107
|
+
registry problem prevents producing the requested result set at all (unknown
|
|
108
|
+
id, invalid arguments, or an unreadable/invalid registry).
|
|
109
|
+
|
|
110
|
+
This exit/output contract is what makes `lookout check --all` safe to drive from
|
|
111
|
+
an external scheduler.
|
|
112
|
+
|
|
113
|
+
`emit-survey` is a separate composable command: its input is a JSON object with
|
|
114
|
+
`observation` (a `ProposalSetObservation`) and its matching `check` anchor.
|
|
115
|
+
Extraction is supplied by the caller. The first successful input commits a
|
|
116
|
+
baseline fact and returns `surveyInput: null`; a genuine later change returns
|
|
117
|
+
one unreviewed SurveyInput batch. State defaults to
|
|
118
|
+
`.kontourai/lookout/observations`, uses immutable digest-addressed records and
|
|
119
|
+
an atomic per-source pointer, and retains the latest two valid observations.
|
|
120
|
+
Consumers pass the batch to Survey when they want Surface projection.
|
|
121
|
+
Store paths refuse symbolic links. An existing source lock is not automatically
|
|
122
|
+
broken: after confirming no writer is active, an operator may remove an
|
|
123
|
+
abandoned `.lock` file and retry.
|
|
124
|
+
|
|
125
|
+
## Library
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
import {
|
|
129
|
+
loadRegistry,
|
|
130
|
+
createCheckRunner,
|
|
131
|
+
createLookoutSnapshotStore,
|
|
132
|
+
} from "@kontourai/lookout";
|
|
133
|
+
|
|
134
|
+
const registry = await loadRegistry(); // <cwd>/lookout.sources.json
|
|
135
|
+
const runner = createCheckRunner({ store: createLookoutSnapshotStore() });
|
|
136
|
+
const results = await runner.checkAll(registry.list());
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`createCheckRunner` accepts injected seams — `store`, `fetchSource` (defaults to
|
|
140
|
+
Traverse), `fetchOptions`, and `clock` — so checks run with no live network or
|
|
141
|
+
timers in tests.
|
|
142
|
+
|
|
143
|
+
## Non-goals
|
|
144
|
+
|
|
145
|
+
Extraction, Surface projection, notifications, crawling, review/escalation or
|
|
146
|
+
authority policy, rendered-fetch wiring
|
|
147
|
+
(`renderPolicy` is inert pending traverse#50), and scheduling. Traverse retains
|
|
148
|
+
all fetch politeness, robots, redirects, retries, timeouts, headers, user-agent,
|
|
149
|
+
and rendered-fetch behavior.
|
|
150
|
+
|
|
151
|
+
## Development
|
|
152
|
+
|
|
153
|
+
```sh
|
|
154
|
+
npm ci
|
|
155
|
+
npm run verify # content-boundary + decisions + typecheck + test + pack sanity
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Individual gates: `npm test`, `npm run typecheck`, `npm run check:pack`,
|
|
159
|
+
`npm run check:decisions`, `npm run check:content-boundary`.
|
|
160
|
+
|
|
161
|
+
## License
|
|
162
|
+
|
|
163
|
+
Apache-2.0.
|
package/bin/lookout.mjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare const canonicalValueKeyBrand: unique symbol;
|
|
2
|
+
export type CanonicalValueFormatVersion = "lookout-canonical-v1";
|
|
3
|
+
export type CanonicalValueKey = string & {
|
|
4
|
+
readonly [canonicalValueKeyBrand]: CanonicalValueFormatVersion;
|
|
5
|
+
};
|
|
6
|
+
export type DiffKernelErrorKind = "unsupported-value" | "cyclic-value" | "callback-threw";
|
|
7
|
+
export interface DiffKernelError {
|
|
8
|
+
readonly kind: DiffKernelErrorKind;
|
|
9
|
+
readonly message: string;
|
|
10
|
+
readonly path?: string;
|
|
11
|
+
readonly cause?: unknown;
|
|
12
|
+
}
|
|
13
|
+
export type DiffResult<T> = {
|
|
14
|
+
readonly ok: true;
|
|
15
|
+
readonly value: T;
|
|
16
|
+
} | {
|
|
17
|
+
readonly ok: false;
|
|
18
|
+
readonly error: DiffKernelError;
|
|
19
|
+
};
|
|
20
|
+
export type IdentityResult<K extends string = string> = {
|
|
21
|
+
readonly ok: true;
|
|
22
|
+
readonly key: K;
|
|
23
|
+
} | {
|
|
24
|
+
readonly ok: false;
|
|
25
|
+
readonly error: DiffKernelError;
|
|
26
|
+
};
|
|
27
|
+
export type CanonicalValueResult = IdentityResult<CanonicalValueKey>;
|
|
28
|
+
export declare function canonicalValueKey(value: unknown): CanonicalValueResult;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
function frame(tag, value = "") {
|
|
2
|
+
return `${tag}${value.length}:${value}`;
|
|
3
|
+
}
|
|
4
|
+
function unsupported(message, path) {
|
|
5
|
+
return { ok: false, error: { kind: "unsupported-value", message, path } };
|
|
6
|
+
}
|
|
7
|
+
function encode(value, path, ancestors) {
|
|
8
|
+
if (value === undefined)
|
|
9
|
+
return { ok: true, key: frame("u") };
|
|
10
|
+
if (value === null)
|
|
11
|
+
return { ok: true, key: frame("n") };
|
|
12
|
+
switch (typeof value) {
|
|
13
|
+
case "boolean":
|
|
14
|
+
return { ok: true, key: frame("b", value ? "1" : "0") };
|
|
15
|
+
case "string":
|
|
16
|
+
return { ok: true, key: frame("s", value) };
|
|
17
|
+
case "bigint":
|
|
18
|
+
return { ok: true, key: frame("i", value.toString(10)) };
|
|
19
|
+
case "number": {
|
|
20
|
+
const encoded = Number.isNaN(value)
|
|
21
|
+
? "nan"
|
|
22
|
+
: value === Number.POSITIVE_INFINITY
|
|
23
|
+
? "+inf"
|
|
24
|
+
: value === Number.NEGATIVE_INFINITY
|
|
25
|
+
? "-inf"
|
|
26
|
+
: Object.is(value, -0)
|
|
27
|
+
? "-0"
|
|
28
|
+
: String(value);
|
|
29
|
+
return { ok: true, key: frame("d", encoded) };
|
|
30
|
+
}
|
|
31
|
+
case "symbol":
|
|
32
|
+
case "function":
|
|
33
|
+
return unsupported(`Cannot canonicalize ${typeof value}`, path);
|
|
34
|
+
case "object":
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
const objectValue = value;
|
|
38
|
+
if (ancestors.has(objectValue)) {
|
|
39
|
+
return { ok: false, error: { kind: "cyclic-value", message: "Cannot canonicalize a cyclic value", path } };
|
|
40
|
+
}
|
|
41
|
+
ancestors.add(objectValue);
|
|
42
|
+
try {
|
|
43
|
+
if (Array.isArray(value)) {
|
|
44
|
+
const parts = [];
|
|
45
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
46
|
+
if (!Object.prototype.hasOwnProperty.call(value, index)) {
|
|
47
|
+
parts.push(frame("h"));
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, String(index));
|
|
51
|
+
if (!descriptor || !("value" in descriptor)) {
|
|
52
|
+
return unsupported("Cannot canonicalize an array accessor", `${path}[${index}]`);
|
|
53
|
+
}
|
|
54
|
+
const item = encode(descriptor.value, `${path}[${index}]`, ancestors);
|
|
55
|
+
if (!item.ok)
|
|
56
|
+
return item;
|
|
57
|
+
parts.push(frame("e", item.key));
|
|
58
|
+
}
|
|
59
|
+
const extraKeys = Reflect.ownKeys(value).filter((key) => {
|
|
60
|
+
if (key === "length")
|
|
61
|
+
return false;
|
|
62
|
+
return typeof key !== "string" || !/^(0|[1-9]\d*)$/.test(key) || Number(key) >= value.length;
|
|
63
|
+
});
|
|
64
|
+
if (extraKeys.length > 0)
|
|
65
|
+
return unsupported("Cannot canonicalize an array with extra properties", path);
|
|
66
|
+
return { ok: true, key: frame("a", parts.join("")) };
|
|
67
|
+
}
|
|
68
|
+
const prototype = Object.getPrototypeOf(value);
|
|
69
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
70
|
+
return unsupported("Cannot canonicalize an exotic object", path);
|
|
71
|
+
}
|
|
72
|
+
const ownKeys = Reflect.ownKeys(value);
|
|
73
|
+
if (ownKeys.some((key) => typeof key === "symbol")) {
|
|
74
|
+
return unsupported("Cannot canonicalize an object with symbol keys", path);
|
|
75
|
+
}
|
|
76
|
+
const keys = ownKeys.sort();
|
|
77
|
+
const parts = [];
|
|
78
|
+
for (const key of keys) {
|
|
79
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
80
|
+
if (!descriptor || !("value" in descriptor)) {
|
|
81
|
+
return unsupported("Cannot canonicalize an object accessor", `${path}.${key}`);
|
|
82
|
+
}
|
|
83
|
+
const item = encode(descriptor.value, `${path}.${key}`, ancestors);
|
|
84
|
+
if (!item.ok)
|
|
85
|
+
return item;
|
|
86
|
+
parts.push(frame("k", key), frame("v", item.key));
|
|
87
|
+
}
|
|
88
|
+
return { ok: true, key: frame("o", parts.join("")) };
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
ancestors.delete(objectValue);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
export function canonicalValueKey(value) {
|
|
95
|
+
try {
|
|
96
|
+
return encode(value, "$", new Set());
|
|
97
|
+
}
|
|
98
|
+
catch (cause) {
|
|
99
|
+
return {
|
|
100
|
+
ok: false,
|
|
101
|
+
error: {
|
|
102
|
+
kind: "unsupported-value",
|
|
103
|
+
message: "Canonical value could not be inspected",
|
|
104
|
+
path: "$",
|
|
105
|
+
cause,
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { FetchError } from "@kontourai/traverse/fetch";
|
|
2
|
+
export interface CheckResultCommon {
|
|
3
|
+
sourceId: string;
|
|
4
|
+
sourceUrl: string;
|
|
5
|
+
checkedAt: string;
|
|
6
|
+
warnings: string[];
|
|
7
|
+
}
|
|
8
|
+
export interface Unchanged304Result extends CheckResultCommon {
|
|
9
|
+
kind: "unchanged-304";
|
|
10
|
+
snapshotRef: string;
|
|
11
|
+
}
|
|
12
|
+
export interface UnchangedHashResult extends CheckResultCommon {
|
|
13
|
+
kind: "unchanged-hash";
|
|
14
|
+
priorSnapshotRef: string;
|
|
15
|
+
currentSnapshotRef: string;
|
|
16
|
+
}
|
|
17
|
+
export interface ChangedResult extends CheckResultCommon {
|
|
18
|
+
kind: "changed";
|
|
19
|
+
priorSnapshotRef: string | null;
|
|
20
|
+
currentSnapshotRef: string;
|
|
21
|
+
changeBasis: "initial" | "hash";
|
|
22
|
+
}
|
|
23
|
+
export type LookoutErrorKind = "prior-read" | "persistence" | "dependency-contract" | "unexpected";
|
|
24
|
+
export interface ErrorResult extends CheckResultCommon {
|
|
25
|
+
kind: "error";
|
|
26
|
+
origin: "traverse" | "lookout";
|
|
27
|
+
error: FetchError | {
|
|
28
|
+
kind: LookoutErrorKind;
|
|
29
|
+
message: string;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export type CheckResult = Unchanged304Result | UnchangedHashResult | ChangedResult | ErrorResult;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { FetchResult, FetchSourceOptions, SnapshotStore, SourceConfig } from "@kontourai/traverse/fetch";
|
|
2
|
+
import type { CheckResult } from "./check-result.js";
|
|
3
|
+
import type { ProviderResolver } from "./provider-resolution.js";
|
|
4
|
+
import type { LookoutSource } from "./registry.js";
|
|
5
|
+
export type FetchSource = (config: SourceConfig, options?: FetchSourceOptions) => Promise<FetchResult>;
|
|
6
|
+
export interface CreateCheckRunnerOptions {
|
|
7
|
+
store: SnapshotStore;
|
|
8
|
+
fetchSource?: FetchSource;
|
|
9
|
+
fetchOptions?: Omit<FetchSourceOptions, "store">;
|
|
10
|
+
clock?: () => string;
|
|
11
|
+
/** Reserved for L2. Deliberately never called by L1. */
|
|
12
|
+
providerResolver?: ProviderResolver;
|
|
13
|
+
}
|
|
14
|
+
export interface CheckRunner {
|
|
15
|
+
check(source: LookoutSource): Promise<CheckResult>;
|
|
16
|
+
checkAll(sources: readonly LookoutSource[]): Promise<CheckResult[]>;
|
|
17
|
+
}
|
|
18
|
+
export declare function createCheckRunner(options: CreateCheckRunnerOptions): CheckRunner;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { buildSnapshotSourceRef, fetchSource as traverseFetchSource, } from "@kontourai/traverse/fetch";
|
|
2
|
+
export function createCheckRunner(options) {
|
|
3
|
+
const fetchImpl = options.fetchSource ?? traverseFetchSource;
|
|
4
|
+
const clock = options.clock ?? (() => new Date().toISOString());
|
|
5
|
+
async function check(source) {
|
|
6
|
+
const common = () => ({
|
|
7
|
+
sourceId: source.id,
|
|
8
|
+
sourceUrl: source.url,
|
|
9
|
+
checkedAt: clock(),
|
|
10
|
+
warnings: [],
|
|
11
|
+
});
|
|
12
|
+
let prior;
|
|
13
|
+
try {
|
|
14
|
+
prior = await options.store.latest(source.id);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
return lookoutError(common(), "prior-read", error);
|
|
18
|
+
}
|
|
19
|
+
let fetched;
|
|
20
|
+
try {
|
|
21
|
+
fetched = await fetchImpl({ id: source.id, url: source.url, revalidate: true }, { ...options.fetchOptions, store: options.store });
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
return lookoutError(common(), "unexpected", error);
|
|
25
|
+
}
|
|
26
|
+
const base = { ...common(), warnings: Array.isArray(fetched?.warnings) ? [...fetched.warnings] : [] };
|
|
27
|
+
if (!isFetchResult(fetched)) {
|
|
28
|
+
return lookoutError(base, "dependency-contract", "Traverse returned neither exactly one snapshot nor exactly one error");
|
|
29
|
+
}
|
|
30
|
+
if (fetched.error) {
|
|
31
|
+
return { ...base, kind: "error", origin: "traverse", error: fetched.error };
|
|
32
|
+
}
|
|
33
|
+
// Defense-in-depth: even if a future guard gap let a malformed snapshot
|
|
34
|
+
// through, any stray throw in classification/ref-building becomes a typed
|
|
35
|
+
// `unexpected` lookout error rather than a rejection (R2 never-throw).
|
|
36
|
+
try {
|
|
37
|
+
const snapshot = fetched.snapshot;
|
|
38
|
+
if (snapshot.notModified === true) {
|
|
39
|
+
return { ...base, kind: "unchanged-304", snapshotRef: buildSnapshotSourceRef(snapshot) };
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
await options.store.put(snapshot);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
return lookoutError(base, "persistence", error);
|
|
46
|
+
}
|
|
47
|
+
const currentSnapshotRef = buildSnapshotSourceRef(snapshot);
|
|
48
|
+
if (!prior) {
|
|
49
|
+
return {
|
|
50
|
+
...base,
|
|
51
|
+
kind: "changed",
|
|
52
|
+
priorSnapshotRef: null,
|
|
53
|
+
currentSnapshotRef,
|
|
54
|
+
changeBasis: "initial",
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const priorSnapshotRef = buildSnapshotSourceRef(prior);
|
|
58
|
+
// Same-resource continuity requires BOTH the resource URL and the body to
|
|
59
|
+
// match. A moved resource (prior's final URL differs from this fetch's)
|
|
60
|
+
// re-baselines as `changed` even when the bytes are identical — "unchanged"
|
|
61
|
+
// must not claim continuity across a URL change.
|
|
62
|
+
if (prior.url === snapshot.url && prior.bodyHash === snapshot.bodyHash) {
|
|
63
|
+
return { ...base, kind: "unchanged-hash", priorSnapshotRef, currentSnapshotRef };
|
|
64
|
+
}
|
|
65
|
+
return { ...base, kind: "changed", priorSnapshotRef, currentSnapshotRef, changeBasis: "hash" };
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
return lookoutError(base, "unexpected", error);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function checkAll(sources) {
|
|
72
|
+
const results = [];
|
|
73
|
+
for (const source of sources)
|
|
74
|
+
results.push(await check(source));
|
|
75
|
+
return results;
|
|
76
|
+
}
|
|
77
|
+
return { check, checkAll };
|
|
78
|
+
}
|
|
79
|
+
function isFetchResult(value) {
|
|
80
|
+
if (typeof value !== "object" || value === null)
|
|
81
|
+
return false;
|
|
82
|
+
const result = value;
|
|
83
|
+
const hasSnapshot = result.snapshot != null;
|
|
84
|
+
const hasError = result.error != null;
|
|
85
|
+
return hasSnapshot !== hasError;
|
|
86
|
+
}
|
|
87
|
+
function lookoutError(common, kind, cause) {
|
|
88
|
+
const message = cause instanceof Error ? cause.message : String(cause);
|
|
89
|
+
return { ...common, kind: "error", origin: "lookout", error: { kind, message } };
|
|
90
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Writable } from "node:stream";
|
|
2
|
+
import { type CheckRunner } from "./check-runner.js";
|
|
3
|
+
import { type LookoutRegistry } from "./registry.js";
|
|
4
|
+
import { type EmissionResult } from "./survey-emission.js";
|
|
5
|
+
export interface RunCliOptions {
|
|
6
|
+
argv?: string[];
|
|
7
|
+
stdout?: Pick<Writable, "write">;
|
|
8
|
+
stderr?: Pick<Writable, "write">;
|
|
9
|
+
loadRegistry?: (path?: string) => Promise<LookoutRegistry>;
|
|
10
|
+
runner?: CheckRunner;
|
|
11
|
+
readObservation?: (path: string) => Promise<unknown>;
|
|
12
|
+
emitSurvey?: (sourceId: string, value: unknown, registry: LookoutRegistry, observationRoot?: string) => Promise<EmissionResult>;
|
|
13
|
+
}
|
|
14
|
+
export declare function runCli(options?: RunCliOptions): Promise<number>;
|