@spaceteams/weft-layer-provenance 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Spaceteams
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,35 @@
1
+ import { KeyId, LayerEvaluator } from "@spaceteams/weft";
2
+
3
+ //#region src/provenance.d.ts
4
+ /**
5
+ * Provenance tracks the source and confidence of a value in the computation model.
6
+ */
7
+ type Provenance = {
8
+ /** The source of this value — e.g., "user-input", "GPS", "estimate", "derived" */readonly source: string; /** Confidence score 0..1 (1 = fully trusted, 0 = no confidence). Default: 1. */
9
+ readonly confidence: number; /** Optional tags for additional classification (e.g., "audited", "provisional") */
10
+ readonly tags?: readonly string[];
11
+ };
12
+ /**
13
+ * Convenience factory for creating a `Provenance` value.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * m.annotate(sensor, "provenance", provenance("GPS", 0.9, ["field-measured"]));
18
+ * ```
19
+ */
20
+ declare function provenance(source: string, confidence?: number, tags?: string[]): Provenance;
21
+ /**
22
+ * Creates derived provenance from a set of dependency provenance values.
23
+ * The confidence is the minimum across all deps.
24
+ */
25
+ declare function derived(deps: ReadonlyMap<KeyId, Provenance>): Provenance;
26
+ /**
27
+ * A source-tracking layer with confidence scores.
28
+ *
29
+ * Annotate inputs with `m.annotate(key, "provenance", provenance("user-input", 0.95))`.
30
+ * Computed values automatically receive `source: "derived"` with the minimum confidence
31
+ * of their dependencies.
32
+ */
33
+ declare const provenanceLayer: LayerEvaluator<Provenance>;
34
+ //#endregion
35
+ export { type Provenance, derived, provenance, provenanceLayer };
package/dist/index.mjs ADDED
@@ -0,0 +1,67 @@
1
+ //#region src/provenance.ts
2
+ /**
3
+ * Convenience factory for creating a `Provenance` value.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * m.annotate(sensor, "provenance", provenance("GPS", 0.9, ["field-measured"]));
8
+ * ```
9
+ */
10
+ function provenance(source, confidence = 1, tags) {
11
+ return tags?.length ? {
12
+ source,
13
+ confidence,
14
+ tags
15
+ } : {
16
+ source,
17
+ confidence
18
+ };
19
+ }
20
+ /**
21
+ * Creates derived provenance from a set of dependency provenance values.
22
+ * The confidence is the minimum across all deps.
23
+ */
24
+ function derived(deps) {
25
+ if (deps.size === 0) return {
26
+ source: "unknown",
27
+ confidence: 0
28
+ };
29
+ let min = 1;
30
+ for (const p of deps.values()) if (p.confidence < min) min = p.confidence;
31
+ return {
32
+ source: "derived",
33
+ confidence: min
34
+ };
35
+ }
36
+ /**
37
+ * A source-tracking layer with confidence scores.
38
+ *
39
+ * Annotate inputs with `m.annotate(key, "provenance", provenance("user-input", 0.95))`.
40
+ * Computed values automatically receive `source: "derived"` with the minimum confidence
41
+ * of their dependencies.
42
+ */
43
+ const provenanceLayer = {
44
+ name: "provenance",
45
+ version: "1",
46
+ eval(_op, deps) {
47
+ return derived(deps);
48
+ },
49
+ default(deps) {
50
+ return derived(deps);
51
+ },
52
+ codec: {
53
+ encode(value) {
54
+ const obj = {
55
+ confidence: value.confidence,
56
+ source: value.source
57
+ };
58
+ if (value.tags?.length) obj.tags = [...value.tags];
59
+ return obj;
60
+ },
61
+ decode(value) {
62
+ return value;
63
+ }
64
+ }
65
+ };
66
+ //#endregion
67
+ export { derived, provenance, provenanceLayer };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@spaceteams/weft-layer-provenance",
3
+ "version": "0.2.0",
4
+ "description": "Source tracking and confidence scoring layer for @spaceteams/weft",
5
+ "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "exports": {
10
+ ".": "./dist/index.mjs",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/spaceteams/weft.git",
19
+ "directory": "layers/provenance"
20
+ },
21
+ "keywords": [
22
+ "weft",
23
+ "layer",
24
+ "provenance",
25
+ "confidence",
26
+ "source-tracking"
27
+ ],
28
+ "license": "MIT",
29
+ "dependencies": {
30
+ "@spaceteams/weft": "0.5.0"
31
+ },
32
+ "scripts": {
33
+ "build": "tsdown",
34
+ "dev": "tsdown --watch",
35
+ "typecheck": "tsc --noEmit",
36
+ "test": "vitest run",
37
+ "test:watch": "vitest",
38
+ "lint": "biome check .",
39
+ "lint:fix": "biome check --write .",
40
+ "format": "biome format --write ."
41
+ }
42
+ }