@eyeoverthink/sva-gate 2.0.2 → 2.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/README.md +19 -5
- package/bin/sva-gate.js +1 -1
- package/package.json +79 -79
- package/src/index.d.ts +7 -1
- package/src/index.js +2 -0
- package/src/lib/binder-induction.d.ts +17 -14
- package/src/lib/binder-induction.js +78 -15
- package/src/lib/membrane-core.js +156 -130
- package/src/lib/sentinel.js +16 -1
- package/src/lib/stream-guard.js +228 -166
- package/src/mcp-server.js +10 -5
- package/src/receipt.js +1 -1
- package/test/review-2026-12-04.test.js +84 -0
- package/test/types.test.js +31 -31
package/README.md
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
# `@eyeoverthink/sva-gate` (`
|
|
1
|
+
# `@eyeoverthink/sva-gate` (`v2.1.0`)
|
|
2
|
+
|
|
3
|
+
> **2.1.0 — privacy membrane and skill crystallizer fixes** (synced with `@eyeoverthink/privacy-membrane` 1.4.0 and
|
|
4
|
+
> `@eyeoverthink/skill-crystallizer` 1.3.0). `StreamSealGuard` no longer leaks a JWT, PEM key, email, password or
|
|
5
|
+
> titled name whose fragment arrives as its own chunk; `SealSession.unseal(text, { strict: true })` restores only the
|
|
6
|
+
> exact `⟦TYPE_n⟧` placeholders the session issued; detection is linear on adversarial input. The skill binder now
|
|
7
|
+
> refuses quantity and negation words its examples never used ("Two $240 coats…", "…is not 25% off…") and tasks on
|
|
8
|
+
> which two rules that both fit the examples disagree; `explainApply` says why. MCP `serverInfo.version` now reports
|
|
9
|
+
> the package version.
|
|
2
10
|
|
|
3
11
|
> **2.0.0 — breaking: verification now asks whose signature counts.** A receipt is `ok` only when it is intact
|
|
4
12
|
> **and** signed by a key you name in `expectedPublicKeys` (CLI: `--key <hex>`). Without it an intact receipt is
|
|
@@ -34,12 +42,12 @@ Gives any agent **6 deterministic verification tools** over stdio JSON-RPC 2.0:
|
|
|
34
42
|
|
|
35
43
|
| MCP Tool | Layer | What It Enforces |
|
|
36
44
|
| --- | --- | --- |
|
|
37
|
-
| `sva_seal_text` | Layer 2 (`EMA/1`) | Seals PII/secrets (emails, SSNs, API keys, phones) into typed placeholders + `StreamSealGuard` cross-token split-secret sealing + zero-width prompt-injection scan |
|
|
45
|
+
| `sva_seal_text` | Layer 2 (`EMA/1`) | Seals PII/secrets (emails, SSNs, API keys, phones) into typed placeholders + `StreamSealGuard` cross-token split-secret sealing + keyword/zero-width prompt-injection scan |
|
|
38
46
|
| `sva_mint_receipt` | Layer 1 (`SVA-ER/1`) | Mints an Ed25519-signed, RFC 6962 `0x00`/`0x01` Merkle-proven receipt + matching `OPEG/1` `S4_COMMIT` envelope |
|
|
39
|
-
| `sva_verify_receipt` | Layer 1 (`SVA-ER/1` + `AER-1`) | Verifies **`SVA-ER/1`** receipts
|
|
47
|
+
| `sva_verify_receipt` | Layer 1 (`SVA-ER/1` + `AER-1`) | Verifies **`SVA-ER/1`** receipts against a key you pin, and checks **`AER-1` v0.4** receipts (an individual Internet-Draft, `draft-zambo-aer1-26`, not an IETF standard) offline. AER-1 receipts carry no issuer signature, so they are reported as `integrity_only` |
|
|
40
48
|
| `sva_opeg_check` | Layer 2 (`OPEG/1`) | Fail-closed pre-commit gate: blocks `DIRECT_INFERENCE`, tampered `R_san` digests, and invalid receipts (`sva-er/1` or `aer-1`) |
|
|
41
|
-
| `sva_judge_audit` | Layer 3 (`SVA-JEV/1`) |
|
|
42
|
-
| `sva_crystallize_skill` | Layer 1 (`VSC/1`) | Zero-LLM deterministic parameter binder induction with leave-one-out verification
|
|
49
|
+
| `sva_judge_audit` | Layer 3 (`SVA-JEV/1`) | Rule checks over judge results you supply (no model call): Wilson 95% CI > 2/3 on your counts, author self-vote exclusion, mandatory verbatim `>= 12`-char quotes, hidden planted-fake traps, and zero-LLM `new_terms` invention scanner |
|
|
50
|
+
| `sva_crystallize_skill` | Layer 1 (`VSC/1`) | Zero-LLM deterministic parameter binder induction with leave-one-out verification; refuses novel wording, unseen quantity/negation words and ambiguous rules (`testResult.refused` says why) |
|
|
43
51
|
|
|
44
52
|
---
|
|
45
53
|
|
|
@@ -91,6 +99,8 @@ console.log(verifyAnyReceipt(receipt, { deliverable })); // { ok: false,
|
|
|
91
99
|
// 2. Seal split-token API keys across streaming chunks
|
|
92
100
|
const sg = new StreamSealGuard();
|
|
93
101
|
const safe = ['Key sk-', 'ant-api03-abcdefghijklmnop1234567890 ok'].map((c) => sg.push(c)).join('') + sg.flush();
|
|
102
|
+
// -> 'Key ⟦SECRET_1⟧ ok'. Put values back only in text a person you trust will read; strict ignores bare EMAIL_1 etc.
|
|
103
|
+
const restored = sg.session.unseal(modelReply, { strict: true });
|
|
94
104
|
|
|
95
105
|
// 3. Enforce SVA-JEV/1 epistemic judge gates
|
|
96
106
|
const verdict = evaluateSvaJev1({
|
|
@@ -129,6 +139,10 @@ res = evaluate_sva_jev1({
|
|
|
129
139
|
|
|
130
140
|
## Honest Limits
|
|
131
141
|
|
|
142
|
+
- **The privacy membrane is regex-based.** It misses IPv6 addresses, SSNs written without dashes and names without a title. Default (lenient) `unseal` also restores `[EMAIL_1]`, `<EMAIL_1>` and the bare word `EMAIL_1`, so any text that names a placeholder receives the real value; use `{ strict: true }` for text shown to others. The streaming guard emits each word when the next whitespace arrives, and only holds what a detector could recognise.
|
|
143
|
+
- **The skill binder gates a fixed list of quantity and negation words before numbers**, not every meaning-changing phrase; treat a routed answer as a draft for templated tasks only.
|
|
144
|
+
- **The prompt-injection scan is a keyword list.** Paraphrases, other languages and homoglyphs score 0.
|
|
145
|
+
|
|
132
146
|
- **Signatures prove post-signing integrity and key possession**, not that a compromised host didn't fabricate data prior to signing. Local unsynced clocks are explicitly labeled `unattested`.
|
|
133
147
|
- **`SVA-JEV/1` verdicts are stamped `stability-signal-not-correctness`** — multi-model consensus is never proof of truth.
|
|
134
148
|
- **Deterministic `new_terms` catches invented numbers and code identifiers**, not subtle semantic paraphrasing.
|
package/bin/sva-gate.js
CHANGED
|
@@ -26,7 +26,7 @@ Commands:
|
|
|
26
26
|
sva-gate seal <text> Seal PII/secrets & scan for prompt injection
|
|
27
27
|
sva-gate stream-demo Demonstrate StreamSealGuard cross-token sealing
|
|
28
28
|
sva-gate mint <json-file | inline-json> Mint SVA-ER/1 receipt + OPEG/1 S4_COMMIT envelope
|
|
29
|
-
sva-gate verify <receipt.json> [deliverable] --key <hex> Verify SVA-ER/1 or
|
|
29
|
+
sva-gate verify <receipt.json> [deliverable] --key <hex> Verify SVA-ER/1 or AER-1 v0.4 receipt offline
|
|
30
30
|
sva-gate gate <opeg-envelope.json> --key <hex> Evaluate OPEG/1 S4_COMMIT gate fail-closed
|
|
31
31
|
|
|
32
32
|
--key <hex> Ed25519 public key allowed to have signed (repeatable). Without it an intact
|
package/package.json
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@eyeoverthink/sva-gate",
|
|
3
|
-
"version": "2.0
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "Zero-dependency 3-layer verification SDK, CLI, and stdio MCP server for AI agents: SVA-ER/1 &
|
|
6
|
-
"license": "Apache-2.0",
|
|
7
|
-
"main": "./src/index.js",
|
|
8
|
-
"types": "./src/index.d.ts",
|
|
9
|
-
"exports": {
|
|
10
|
-
".": {
|
|
11
|
-
"types": "./src/index.d.ts",
|
|
12
|
-
"import": "./src/index.js"
|
|
13
|
-
},
|
|
14
|
-
"./receipt": {
|
|
15
|
-
"types": "./src/receipt.d.ts",
|
|
16
|
-
"import": "./src/receipt.js"
|
|
17
|
-
},
|
|
18
|
-
"./judge": {
|
|
19
|
-
"types": "./src/lib/judge-verify.d.ts",
|
|
20
|
-
"import": "./src/lib/judge-verify.js"
|
|
21
|
-
},
|
|
22
|
-
"./membrane": {
|
|
23
|
-
"types": "./src/membrane.d.ts",
|
|
24
|
-
"import": "./src/membrane.js"
|
|
25
|
-
},
|
|
26
|
-
"./skill": {
|
|
27
|
-
"types": "./src/lib/binder-induction.d.ts",
|
|
28
|
-
"import": "./src/lib/binder-induction.js"
|
|
29
|
-
},
|
|
30
|
-
"./mcp": {
|
|
31
|
-
"types": "./src/mcp-server.d.ts",
|
|
32
|
-
"import": "./src/mcp-server.js"
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
"bin": {
|
|
36
|
-
"sva-gate": "bin/sva-gate.js"
|
|
37
|
-
},
|
|
38
|
-
"files": [
|
|
39
|
-
"bin/",
|
|
40
|
-
"src/",
|
|
41
|
-
"test/",
|
|
42
|
-
"README.md",
|
|
43
|
-
"LICENSE"
|
|
44
|
-
],
|
|
45
|
-
"keywords": [
|
|
46
|
-
"mcp",
|
|
47
|
-
"mcp-server",
|
|
48
|
-
"ai-agents",
|
|
49
|
-
"sva-standard",
|
|
50
|
-
"sva-er-1",
|
|
51
|
-
"aer-1",
|
|
52
|
-
"opeg-1",
|
|
53
|
-
"sva-jev-1",
|
|
54
|
-
"ed25519",
|
|
55
|
-
"merkle",
|
|
56
|
-
"privacy-membrane",
|
|
57
|
-
"llm-judge",
|
|
58
|
-
"zero-dependency"
|
|
59
|
-
],
|
|
60
|
-
"repository": {
|
|
61
|
-
"type": "git",
|
|
62
|
-
"url": "git+https://github.com/eyeoverthink/sva-gate.git"
|
|
63
|
-
},
|
|
64
|
-
"homepage": "https://eyeoverthink.github.io/sva-standard/",
|
|
65
|
-
"bugs": {
|
|
66
|
-
"url": "https://github.com/eyeoverthink/sva-gate/issues"
|
|
67
|
-
},
|
|
68
|
-
"scripts": {
|
|
69
|
-
"test": "node --test \"test/*.test.js\"",
|
|
70
|
-
"mcp": "node ./bin/sva-gate.js mcp",
|
|
71
|
-
"self-test": "node ./bin/sva-gate.js self-test"
|
|
72
|
-
},
|
|
73
|
-
"publishConfig": {
|
|
74
|
-
"access": "public"
|
|
75
|
-
},
|
|
76
|
-
"engines": {
|
|
77
|
-
"node": ">=22.0.0"
|
|
78
|
-
}
|
|
79
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@eyeoverthink/sva-gate",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Zero-dependency 3-layer verification SDK, CLI, and stdio MCP server for AI agents: SVA-ER/1 & AER-1 (draft-zambo-aer1) receipts, OPEG/1 execution gates, StreamSealGuard split-token privacy membrane, SVA-JEV/1 rule-based judge checks, and zero-LLM skill crystallization.",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"main": "./src/index.js",
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./src/index.d.ts",
|
|
12
|
+
"import": "./src/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./receipt": {
|
|
15
|
+
"types": "./src/receipt.d.ts",
|
|
16
|
+
"import": "./src/receipt.js"
|
|
17
|
+
},
|
|
18
|
+
"./judge": {
|
|
19
|
+
"types": "./src/lib/judge-verify.d.ts",
|
|
20
|
+
"import": "./src/lib/judge-verify.js"
|
|
21
|
+
},
|
|
22
|
+
"./membrane": {
|
|
23
|
+
"types": "./src/membrane.d.ts",
|
|
24
|
+
"import": "./src/membrane.js"
|
|
25
|
+
},
|
|
26
|
+
"./skill": {
|
|
27
|
+
"types": "./src/lib/binder-induction.d.ts",
|
|
28
|
+
"import": "./src/lib/binder-induction.js"
|
|
29
|
+
},
|
|
30
|
+
"./mcp": {
|
|
31
|
+
"types": "./src/mcp-server.d.ts",
|
|
32
|
+
"import": "./src/mcp-server.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"bin": {
|
|
36
|
+
"sva-gate": "bin/sva-gate.js"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"bin/",
|
|
40
|
+
"src/",
|
|
41
|
+
"test/",
|
|
42
|
+
"README.md",
|
|
43
|
+
"LICENSE"
|
|
44
|
+
],
|
|
45
|
+
"keywords": [
|
|
46
|
+
"mcp",
|
|
47
|
+
"mcp-server",
|
|
48
|
+
"ai-agents",
|
|
49
|
+
"sva-standard",
|
|
50
|
+
"sva-er-1",
|
|
51
|
+
"aer-1",
|
|
52
|
+
"opeg-1",
|
|
53
|
+
"sva-jev-1",
|
|
54
|
+
"ed25519",
|
|
55
|
+
"merkle",
|
|
56
|
+
"privacy-membrane",
|
|
57
|
+
"llm-judge",
|
|
58
|
+
"zero-dependency"
|
|
59
|
+
],
|
|
60
|
+
"repository": {
|
|
61
|
+
"type": "git",
|
|
62
|
+
"url": "git+https://github.com/eyeoverthink/sva-gate.git"
|
|
63
|
+
},
|
|
64
|
+
"homepage": "https://eyeoverthink.github.io/sva-standard/",
|
|
65
|
+
"bugs": {
|
|
66
|
+
"url": "https://github.com/eyeoverthink/sva-gate/issues"
|
|
67
|
+
},
|
|
68
|
+
"scripts": {
|
|
69
|
+
"test": "node --test \"test/*.test.js\"",
|
|
70
|
+
"mcp": "node ./bin/sva-gate.js mcp",
|
|
71
|
+
"self-test": "node ./bin/sva-gate.js self-test"
|
|
72
|
+
},
|
|
73
|
+
"publishConfig": {
|
|
74
|
+
"access": "public"
|
|
75
|
+
},
|
|
76
|
+
"engines": {
|
|
77
|
+
"node": ">=22.0.0"
|
|
78
|
+
}
|
|
79
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -87,7 +87,8 @@ export class SealSession {
|
|
|
87
87
|
placeholder(type: string, value: string): string;
|
|
88
88
|
apply(text: string, findings: unknown[]): string;
|
|
89
89
|
values(): Array<{ type: string; value: string }>;
|
|
90
|
-
|
|
90
|
+
/** Lenient by default (also restores [EMAIL_1], <EMAIL_1>, bare EMAIL_1); strict restores only exact ⟦TYPE_n⟧ tokens issued by this session. */
|
|
91
|
+
unseal(text: string, opts?: { json?: boolean; strict?: boolean }): string;
|
|
91
92
|
audit(outbound: string): { clean: boolean; leaks: string[] };
|
|
92
93
|
}
|
|
93
94
|
export class MembraneLeak extends Error {
|
|
@@ -130,6 +131,11 @@ export function leaveOneOut(
|
|
|
130
131
|
): Array<{ ok: boolean; [k: string]: unknown }>;
|
|
131
132
|
export function applySpec(spec: Record<string, unknown>, task: string): Record<string, unknown> | null;
|
|
132
133
|
export function novelWording(pos: Array<{ task: string }>, task: string): string[];
|
|
134
|
+
export function explainApply(
|
|
135
|
+
spec: Record<string, unknown>,
|
|
136
|
+
task: string,
|
|
137
|
+
): { input: Record<string, unknown> | null; refused: null | 'shape' | 'block-word' | 'context-block' | 'modifier' | 'ambiguous' | 'missing'; detail: unknown };
|
|
138
|
+
export function modifierWords(task: string): string[];
|
|
133
139
|
|
|
134
140
|
export const MCP_TOOLS: Array<{ name: string; description: string; inputSchema: Record<string, unknown> }>;
|
|
135
141
|
export function createMcpHandler(): (req: Record<string, unknown>) => Record<string, unknown> | null;
|
package/src/index.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
export {
|
|
2
|
-
induce,
|
|
3
|
-
leaveOneOut,
|
|
4
|
-
applySpec,
|
|
5
|
-
novelWording,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export function
|
|
11
|
-
export
|
|
12
|
-
export function
|
|
13
|
-
export function
|
|
14
|
-
export function
|
|
1
|
+
export {
|
|
2
|
+
induce,
|
|
3
|
+
leaveOneOut,
|
|
4
|
+
applySpec,
|
|
5
|
+
novelWording,
|
|
6
|
+
explainApply,
|
|
7
|
+
modifierWords,
|
|
8
|
+
} from '../index.js';
|
|
9
|
+
|
|
10
|
+
export function numberTokens(task: string): Array<Record<string, unknown>>;
|
|
11
|
+
export const shapeOf: (toks: Array<{ kind: string }>) => string;
|
|
12
|
+
export function questionWords(task: string): string[];
|
|
13
|
+
export function learnBlocks(pos: Array<{ task: string }>, neg: Array<{ task: string }>): { blockWords: string[]; contextBlock: { kinds: string[]; words: string[] } };
|
|
14
|
+
export function contested(pos: Array<{ task: string }>, task: string): { words: string[]; examples: Array<{ task: string }>; tooMany?: number };
|
|
15
|
+
export function wordingOf(task: string, kinds: string[]): string[];
|
|
16
|
+
/** Source text of a self-contained `function bind(task)`. */
|
|
17
|
+
export function compileSpec(spec: Record<string, unknown>): string;
|
|
@@ -18,19 +18,41 @@ const round = (x) => Math.round(x * 1e9) / 1e9;
|
|
|
18
18
|
export function numberTokens(task) {
|
|
19
19
|
const s = String(task), out = [];
|
|
20
20
|
const STOPC = ['and', 'then', 'the', 'that', 'this', 'for', 'with', 'from', 'are', 'was', 'its', 'your'];
|
|
21
|
-
|
|
21
|
+
// 1.3.0: "$1,200" is one number (thousands separators), and EUR/GBP/JPY signs mark currency like "$".
|
|
22
|
+
const re = /([$\u20ac\u00a3\u00a5]\s?)?(?<![\w.])(-?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?)(\s*%|\s?percent\b)?(?:\s?([a-z]+(?:\/[a-z]+)?))?/gi;
|
|
22
23
|
let m;
|
|
23
24
|
while ((m = re.exec(s))) {
|
|
24
25
|
const end = m.index + (m[1] || '').length + m[2].length + (m[3] || '').length;
|
|
25
26
|
const rest = s.slice(end).split(/[,;:?!]|\.(?!\d)|\d/)[0].toLowerCase();
|
|
26
27
|
const ctx = (rest.match(/[a-z]{3,}/g) || []).filter((w) => !STOPC.includes(w)).slice(0, 3);
|
|
27
|
-
out.push({ v: Number(m[2]), kind: m[1] ? 'currency' : m[3] ? 'percent' : 'plain', unit: !m[1] && !m[3] && m[4] ? m[4].toLowerCase() : null, ctx });
|
|
28
|
+
out.push({ v: Number(m[2].replace(/,/g, '')), kind: m[1] ? 'currency' : m[3] ? 'percent' : 'plain', unit: !m[1] && !m[3] && m[4] ? m[4].toLowerCase() : null, ctx });
|
|
28
29
|
}
|
|
29
30
|
return out;
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
export const shapeOf = (toks) => KINDS.map((k) => toks.filter((t) => t.kind === k).length).join(',');
|
|
33
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Words that change what the numbers mean without being numbers: quantities written as words ("Two $240 coats"),
|
|
37
|
+
* distributive words ("each", "per"), and negations ("is not 25% off"). 1.2.0 never gated words BEFORE a number, so
|
|
38
|
+
* "Two $240 coats are each 25% off..." routed and answered 162 (correct: 324). A task that uses a modifier no positive
|
|
39
|
+
* example used is refused. Self-contained so compileSpec can inline it.
|
|
40
|
+
*/
|
|
41
|
+
export function modifierWords(task) {
|
|
42
|
+
const MOD = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve',
|
|
43
|
+
'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'thirty', 'forty',
|
|
44
|
+
'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred', 'thousand', 'million', 'dozen', 'dozens', 'half',
|
|
45
|
+
'halves', 'quarter', 'double', 'doubled', 'twice', 'triple', 'tripled', 'thrice', 'couple', 'several', 'both', 'each', 'every', 'per', 'apiece',
|
|
46
|
+
'not', 'no', 'never', 'none', 'nor', 'neither', 'without', 'except', 'instead', 'excluding'];
|
|
47
|
+
const words = String(task).toLowerCase().replace(/[\u2019']/g, "'").match(/[a-z]+(?:'[a-z]+)?/g) || [];
|
|
48
|
+
const out = new Set();
|
|
49
|
+
for (const w of words) {
|
|
50
|
+
if (MOD.includes(w)) out.add(w);
|
|
51
|
+
else if (/n't$/.test(w)) out.add("n't");
|
|
52
|
+
}
|
|
53
|
+
return [...out].sort();
|
|
54
|
+
}
|
|
55
|
+
|
|
34
56
|
/** Content words of the question sentence (the last sentence with a "?", else the last sentence). Self-contained. */
|
|
35
57
|
export function questionWords(task) {
|
|
36
58
|
const STOPW = ['the', 'and', 'for', 'with', 'that', 'this', 'what', 'how', 'much', 'many', 'does', 'did', 'was', 'are', 'its', 'their', 'your', 'you', 'now', 'then', 'after', 'from', 'into', 'over', 'whole', 'all'];
|
|
@@ -61,6 +83,7 @@ export function induce(pos, neg, skill) {
|
|
|
61
83
|
if (shapes.length > 2) return { error: `examples have ${shapes.length} different number shapes` };
|
|
62
84
|
const maxN = Math.max(...toks.map((t) => t.length));
|
|
63
85
|
const params = {};
|
|
86
|
+
const alternatives = {};
|
|
64
87
|
for (const key of keys) {
|
|
65
88
|
const p = props[key] || {};
|
|
66
89
|
const scales = declaresDecimal(p) ? [0.01, 1] : [1];
|
|
@@ -69,17 +92,29 @@ export function induce(pos, neg, skill) {
|
|
|
69
92
|
if (p.type === 'array' || Array.isArray(pos[0].input[key])) cands.push({ kind, all: true, scale });
|
|
70
93
|
else for (let k = 0; k < maxN; k++) cands.push({ kind, k, scale }, { kind, k: -(k + 1), scale });
|
|
71
94
|
}
|
|
72
|
-
const
|
|
73
|
-
if (!
|
|
95
|
+
const fits = cands.filter((s) => pos.every((e, i) => eq(pick(toks[i], s), e.input[key])));
|
|
96
|
+
if (!fits.length) return { error: `no single rule finds ${key} in every example` };
|
|
74
97
|
// Plain numbers carry their units: measured, positions alone could not tell "40 mph ... 60 mph" from
|
|
75
98
|
// "120 miles in 2 hours", so the speed binder was (correctly) refused until units were learned.
|
|
76
|
-
|
|
99
|
+
for (const hit of fits) {
|
|
100
|
+
if (hit.all || (hit.kind !== 'plain' && hit.kind !== 'any')) continue;
|
|
77
101
|
const units = toks.map((t) => { const l = t.filter((x) => hit.kind === 'any' || x.kind === hit.kind); return (hit.k >= 0 ? l[hit.k] : l[l.length + hit.k])?.unit; });
|
|
78
102
|
if (units.every(Boolean)) hit.units = [...new Set(units)].sort();
|
|
79
103
|
}
|
|
104
|
+
// 1.3.0 ambiguity check. 1.2.0 kept the first rule that fit, silently: with examples where d1 = d2, "first %"
|
|
105
|
+
// and "second %" both fit, and "40% off, then 10% off" was bound as {d1: 0.4, d2: 0.4}. A rule that picks a
|
|
106
|
+
// DIFFERENT number in some example (equal values by coincidence) is kept as an alternative; applySpec refuses a
|
|
107
|
+
// task on which the alternatives disagree. Rules that pick the same number in every example are the same rule.
|
|
108
|
+
const which = (t, s) => { const l = t.filter((x) => s.kind === 'any' || x.kind === s.kind); return s.all ? l : (s.k >= 0 ? l[s.k] : l[l.length + s.k]); };
|
|
109
|
+
const same = (a, b) => toks.every((t) => { const x = which(t, a), y = which(t, b); return Array.isArray(x) ? Array.isArray(y) && x.length === y.length && x.every((v, j) => v === y[j]) : x === y; });
|
|
110
|
+
const [hit, ...rest] = fits;
|
|
111
|
+
const alts = [];
|
|
112
|
+
for (const c of rest) if (!same(c, hit) && !alts.some((a) => same(a, c))) alts.push(c);
|
|
80
113
|
params[key] = hit;
|
|
114
|
+
if (alts.length) alternatives[key] = alts;
|
|
81
115
|
}
|
|
82
|
-
|
|
116
|
+
const modifiers = [...new Set(pos.flatMap((e) => modifierWords(e.task)))].sort();
|
|
117
|
+
return { spec: { shapes, params, alternatives, modifiers, ...learnBlocks(pos, neg) } };
|
|
83
118
|
}
|
|
84
119
|
|
|
85
120
|
/**
|
|
@@ -152,20 +187,45 @@ export function wordingOf(task, kinds) {
|
|
|
152
187
|
export function novelWording(pos, task) {
|
|
153
188
|
const { kinds } = learnBlocks(pos, []).contextBlock;
|
|
154
189
|
const known = new Set(pos.flatMap((e) => wordingOf(e.task, kinds)));
|
|
155
|
-
|
|
190
|
+
const knownMods = new Set(pos.flatMap((e) => modifierWords(e.task)));
|
|
191
|
+
return [
|
|
192
|
+
...wordingOf(task, kinds).filter((w) => !known.has(w)),
|
|
193
|
+
...modifierWords(task).filter((w) => !knownMods.has(w)).map((w) => `${w} (quantity or negation)`),
|
|
194
|
+
];
|
|
156
195
|
}
|
|
157
196
|
|
|
158
|
-
/**
|
|
159
|
-
|
|
197
|
+
/**
|
|
198
|
+
* Apply a spec and say why when it refuses: `{ input, refused }`, where `refused` is null when the task binds, else one
|
|
199
|
+
* of 'shape', 'block-word', 'context-block', 'modifier', 'ambiguous', 'missing'. Same logic the compiled binder runs.
|
|
200
|
+
*/
|
|
201
|
+
export function explainApply(spec, task) {
|
|
202
|
+
const no = (refused, detail) => ({ input: null, refused, detail });
|
|
160
203
|
const toks = numberTokens(task);
|
|
161
|
-
if (!spec.shapes.includes(shapeOf(toks))) return
|
|
204
|
+
if (!spec.shapes.includes(shapeOf(toks))) return no('shape', shapeOf(toks));
|
|
162
205
|
const q = questionWords(task);
|
|
163
|
-
|
|
206
|
+
const bw = spec.blockWords.filter((w) => q.includes(w));
|
|
207
|
+
if (bw.length) return no('block-word', bw);
|
|
164
208
|
const cb = spec.contextBlock;
|
|
165
|
-
if (cb && toks.some((t) => cb.kinds.includes(t.kind) && t.ctx.some((w) => cb.words.includes(w)))) return
|
|
209
|
+
if (cb && toks.some((t) => cb.kinds.includes(t.kind) && t.ctx.some((w) => cb.words.includes(w)))) return no('context-block', cb.words);
|
|
210
|
+
// Specs induced before 1.3.0 carry no `modifiers`; they are treated as having seen none, which is the safe side.
|
|
211
|
+
const mods = modifierWords(task).filter((w) => !(spec.modifiers || []).includes(w));
|
|
212
|
+
if (mods.length) return no('modifier', mods);
|
|
166
213
|
const out = {};
|
|
167
|
-
for (const [k, s] of Object.entries(spec.params)) {
|
|
168
|
-
|
|
214
|
+
for (const [k, s] of Object.entries(spec.params)) {
|
|
215
|
+
const v = pick(toks, s);
|
|
216
|
+
if (v === undefined) return no('missing', k);
|
|
217
|
+
for (const a of (spec.alternatives || {})[k] || []) {
|
|
218
|
+
const w = pick(toks, a);
|
|
219
|
+
if (w === undefined || JSON.stringify(w) !== JSON.stringify(v)) return no('ambiguous', k);
|
|
220
|
+
}
|
|
221
|
+
out[k] = v;
|
|
222
|
+
}
|
|
223
|
+
return { input: out, refused: null, detail: null };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Apply a spec to a task; null when it refuses (see explainApply for the reason). */
|
|
227
|
+
export function applySpec(spec, task) {
|
|
228
|
+
return explainApply(spec, task).input;
|
|
169
229
|
}
|
|
170
230
|
|
|
171
231
|
/** Leave-one-out: every example must be predicted by the spec induced from all the others. */
|
|
@@ -178,7 +238,8 @@ export function leaveOneOut(pos, neg, skill) {
|
|
|
178
238
|
// A miss only because the held-out example has a unit no other example has ("knots") is novelty, not contradiction.
|
|
179
239
|
let novel = false;
|
|
180
240
|
if (!ok && r.spec) {
|
|
181
|
-
|
|
241
|
+
// A modifier word ("each") only the held-out example uses is novelty too.
|
|
242
|
+
const loose = { ...r.spec, modifiers: [...(r.spec.modifiers || []), ...modifierWords(e.task)], params: Object.fromEntries(Object.entries(r.spec.params).map(([k, p]) => [k, { ...p, units: undefined }])) };
|
|
182
243
|
const g2 = applySpec(loose, e.task);
|
|
183
244
|
novel = Boolean(g2) && Object.keys(e.input).every((k) => eq(g2[k], e.input[k]));
|
|
184
245
|
}
|
|
@@ -196,7 +257,9 @@ export function compileSpec(spec) {
|
|
|
196
257
|
` ${numberTokens.toString()}`,
|
|
197
258
|
` ${questionWords.toString()}`,
|
|
198
259
|
` const shapeOf = ${shapeOf.toString()};`,
|
|
260
|
+
` ${modifierWords.toString()}`,
|
|
199
261
|
` ${pick.toString()}`,
|
|
262
|
+
` ${explainApply.toString()}`,
|
|
200
263
|
` ${applySpec.toString()}`,
|
|
201
264
|
' return applySpec(SPEC, task);',
|
|
202
265
|
'}',
|