gdp-wired 0.31.1 → 0.33.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.
Files changed (84) hide show
  1. package/README.md +95 -6
  2. package/dist/core/c/reader.d.ts +86 -0
  3. package/dist/core/c/reader.js +860 -0
  4. package/dist/core/check.js +20 -6
  5. package/dist/core/cli.d.ts +76 -0
  6. package/dist/core/cli.js +636 -0
  7. package/dist/core/dsl.d.ts +6 -4
  8. package/dist/core/dsl.js +30 -16
  9. package/dist/core/effect.d.ts +6 -4
  10. package/dist/core/effect.js +48 -10
  11. package/dist/core/ex/harness.exs +177 -0
  12. package/dist/core/ex/json.exs +46 -0
  13. package/dist/core/ex/reader.exs +462 -0
  14. package/dist/core/exists.d.ts +3 -0
  15. package/dist/core/exists.js +19 -5
  16. package/dist/core/extract-c.d.ts +35 -0
  17. package/dist/core/extract-c.js +424 -0
  18. package/dist/core/extract-check.js +27 -6
  19. package/dist/core/extract-ex.d.ts +16 -0
  20. package/dist/core/extract-ex.js +33 -0
  21. package/dist/core/extract-go-check.d.ts +28 -0
  22. package/dist/core/extract-go-check.js +639 -0
  23. package/dist/core/extract-go.d.ts +231 -0
  24. package/dist/core/extract-go.js +1166 -0
  25. package/dist/core/extract-jvm.d.ts +23 -0
  26. package/dist/core/extract-jvm.js +220 -0
  27. package/dist/core/extract-print.js +17 -7
  28. package/dist/core/extract-py.d.ts +15 -0
  29. package/dist/core/extract-py.js +31 -0
  30. package/dist/core/extract-rb.d.ts +17 -0
  31. package/dist/core/extract-rb.js +31 -0
  32. package/dist/core/extract-read.d.ts +20 -0
  33. package/dist/core/extract-read.js +46 -6
  34. package/dist/core/extract-rs.d.ts +12 -0
  35. package/dist/core/extract-rs.js +306 -0
  36. package/dist/core/extract-shapes.d.ts +2 -0
  37. package/dist/core/extract-shapes.js +6 -5
  38. package/dist/core/extract-step.d.ts +806 -0
  39. package/dist/core/extract-step.js +4141 -0
  40. package/dist/core/extract-swift.d.ts +27 -0
  41. package/dist/core/extract-swift.js +484 -0
  42. package/dist/core/extract-xstate.d.ts +10 -3
  43. package/dist/core/extract-xstate.js +50 -4
  44. package/dist/core/extract.d.ts +22 -3
  45. package/dist/core/extract.js +107 -18
  46. package/dist/core/go/go.mod +3 -0
  47. package/dist/core/go/reader.go +901 -0
  48. package/dist/core/infer.d.ts +4 -0
  49. package/dist/core/infer.js +1838 -69
  50. package/dist/core/jvm/Harness.java +361 -0
  51. package/dist/core/jvm/JavaReader.java +825 -0
  52. package/dist/core/jvm/Jvm.java +332 -0
  53. package/dist/core/jvm/KtReader.java +960 -0
  54. package/dist/core/jvm/Rt.java +10 -0
  55. package/dist/core/laws.d.ts +83 -0
  56. package/dist/core/laws.js +623 -0
  57. package/dist/core/machine.d.ts +2 -0
  58. package/dist/core/machine.js +87 -0
  59. package/dist/core/py/harness.py +328 -0
  60. package/dist/core/py/reader.py +447 -0
  61. package/dist/core/rb/harness.rb +233 -0
  62. package/dist/core/rb/reader.rb +375 -0
  63. package/dist/core/relational.d.ts +49 -0
  64. package/dist/core/relational.js +345 -0
  65. package/dist/core/rs/Cargo.lock +108 -0
  66. package/dist/core/rs/Cargo.toml +14 -0
  67. package/dist/core/rs/src/main.rs +681 -0
  68. package/dist/core/swift/Harness.swift +124 -0
  69. package/dist/core/swift/Main.swift +34 -0
  70. package/dist/core/swift/Package.swift +13 -0
  71. package/dist/core/swift/Sources/gdp-swiftreader/main.swift +1341 -0
  72. package/dist/interchange/check.d.ts +16 -0
  73. package/dist/interchange/check.js +111 -0
  74. package/dist/interchange/format.d.ts +150 -0
  75. package/dist/interchange/format.js +678 -0
  76. package/dist/kernel/epr.d.ts +40 -0
  77. package/dist/kernel/epr.js +1882 -0
  78. package/dist/kernel/kernel.d.ts +45 -1
  79. package/dist/kernel/kernel.js +138 -13
  80. package/dist/kernel/prove.d.ts +45 -0
  81. package/dist/kernel/prove.js +658 -51
  82. package/dist/lean-kernel/emit.d.ts +17 -2
  83. package/dist/lean-kernel/emit.js +87 -7
  84. package/package.json +57 -2
package/README.md CHANGED
@@ -9,6 +9,10 @@ An automatic prover for stateful rules: budgets, caps, rate limits, workflows, a
9
9
  the laws it must keep. gdp-wired proves them with no proof to write, shows a real run that breaks a law when it's false
10
10
  (found by searching on the GPU), has every proof checked again by Lean, and ships the rule as JavaScript.
11
11
 
12
+ [![How gdp-wired works: certificates, the kernel, the frontier (14 min)](https://raw.githubusercontent.com/monotykamary/gdp-wired/main/static/explainer.jpg)](https://gdpwired.dev/#explainer)
13
+
14
+ How it works, in 14 minutes: what a certificate is, why the kernel is small, and the frontier. [Watch it on gdpwired.dev](https://gdpwired.dev/#explainer), with captions.
15
+
12
16
  ```sh
13
17
  npm install gdp-wired # or bun add gdp-wired; plain JavaScript with types, Node 20+, Bun or a browser
14
18
  ```
@@ -38,6 +42,86 @@ protocols, commerce, supply chains, games, education, healthcare, energy, networ
38
42
  governance, insurance, bookings, safety and IoT, mobility, media), each checked three ways: the kernel proves every law
39
43
  and refutes every broken variant, random runs of both runtimes agree, and Lean checks the trees.
40
44
 
45
+ ## First proof in your language
46
+
47
+ The rules already in your code (Python, Ruby, Elixir, Java, Kotlin, Go, Rust, Swift, C, C++, TypeScript, Effect,
48
+ XState) are read with nothing to write: the language, the class or functions, and what other code is answered or left
49
+ out are inferred.
50
+
51
+ ```sh
52
+ npx gdp-wired-cli check src/limiter.py # or .rb .ex .java .kt .go .rs .swift .c .cpp .ts (Effect, XState), a Go package, a crate, a Swift package
53
+ ```
54
+
55
+ It prints the rule in the code's names, checks it against the code by running both, and proposes laws from the rule's
56
+ own comparisons, each proven, broken (with the run, as calls of your code) or unknown, with the comment that states it.
57
+ State a law with one comment over the code's names; on the line above a method it is a law of that method's step:
58
+
59
+ ```python
60
+ class Limiter:
61
+ def __init__(self, limit: int):
62
+ self.limit, self.used = limit, 0
63
+
64
+ # gdp: law a counted hit adds its cost: result implies used == old(used) + cost
65
+ # gdp: law used never passes the limit: used <= limit
66
+ def hit(self, cost: int) -> bool:
67
+ if cost <= 0 or self.used + cost > self.limit:
68
+ return False
69
+ self.used += cost
70
+ return True
71
+ ```
72
+
73
+ ```
74
+ limiter.py: Limiter (Python)
75
+ checked against the code: 2154 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 3 of 3 branch arms
76
+ the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
77
+ names (the code's, then the rule's):
78
+ limit state limit_
79
+ used state used
80
+ limit made with limit
81
+ hit(cost) request cost
82
+ hit() result result hit_result
83
+ laws stated in the code:
84
+ proven a counted hit adds its cost (limiter.py:5)
85
+ broken used never passes the limit (limiter.py:6)
86
+ used <= limit
87
+ run: made with limit = -3
88
+ hit(cost=-3)
89
+ laws proposed (nothing written; state one with the comment under it):
90
+ proven used is never negative
91
+ # gdp: law used is never negative: used >= 0
92
+ ...
93
+ ```
94
+
95
+ From code, the same in every language: one import, the file (a path or a URL; a Go package or a Rust crate by any file
96
+ of it), a law. The class, service or machine is inferred (`{entry}` names one), and Effect and XState run on the
97
+ packages the file's own project has:
98
+
99
+ ```ts
100
+ import {extractEffect, proveRule} from 'gdp-wired/effect' // or extractPython from 'gdp-wired/extract-py', extract, ...
101
+ const x = extractEffect('./limiter.ts')
102
+ proveRule(x.rule!, [...x.invariants ?? [], x.rule!.law('never below zero', ({tokens}) => tokens.atLeast(0))])
103
+ ```
104
+
105
+ `//` in Go, Rust, C, C++, Java, Kotlin and TypeScript (an Effect service's laws above its make effect, an XState
106
+ machine's over its context, `value` and `event`); `@GdpLaw("<name>: <expr>")` on a Java or Kotlin method (with
107
+ the annotation declared in your code), `@gdp_law("...")` in Python, `@gdp_law "..."` in Elixir, `#[doc = "gdp: law
108
+ ..."]` in Rust. The expression: comparisons, `+ - * k`, `and or not implies`, the state after the step by its fields
109
+ (`self.`, `this.`, `@` or bare; `after.x`), `old(x)` or `before.x` before it, the request's arguments by name,
110
+ `result`, `failed`, a call of other code answered by the request as `f(...)`, `x is None`, maps as the code reads them
111
+ (`counts[user]`, `user in counts`, `cache["rl:" + user]`), `forall k in m: ...`, a choice by its value (`mode ==
112
+ "closed"`). Anything else is refused with why, never dropped. Modelling choices a rule needs (`failures = "kept"` for
113
+ laws about when it raises, a store's attributes, an untyped result) go in a `gdp.toml` beside the code (or `[tool.gdp]`
114
+ in pyproject.toml, `"gdp"` in package.json), per rule under `[rules.<name>]`. The exit code is 1 when a stated law is
115
+ broken, 2 when one couldn't be checked: `uses: monotykamary/gdp-wired@main` with `paths:` runs it in CI.
116
+
117
+ Laws about two runs read the same way: `diagnosis never reaches status, result` (two runs differing only in the
118
+ secret keep what is named the same, and fail alike), `isolated by user_id` (what the caller whose `user_id` is B sees
119
+ is the same whatever the others do), `agrees with LimiterV1` (a migration: another class of the target, the same state
120
+ after every history). A duration is linear over the request's time: `now - received < 30 days` (seconds, minutes,
121
+ hours, days, weeks; a month, a year or a business day is refused). `gdp: cite <law name>: <clause>` ties a law to the
122
+ clause it answers: printed with the law, and with `--cert <dir>` kept in the meta of each proven law's certificate,
123
+ never in what is checked (`research/regulated/DESIGN.md`).
124
+
41
125
  ## Where it fits
42
126
 
43
127
  <img src="https://raw.githubusercontent.com/monotykamary/gdp-wired/main/static/venn.svg" alt="What Lean, Bend and gdp-wired each cover, and where they overlap" width="640" />
@@ -73,21 +157,21 @@ All of these are proven automatically, and each proof is also a Lean theorem:
73
157
  firing are events from outside, taken only where they can come (each once a visit; one timer at a time), and
74
158
  eventless transitions and entry and exit actions that write the context are read into each transition as XState's
75
159
  macrostep.
76
- - **Rules read from the code** (`gdp-wired/extract`): `extract(code, {entry})` reads a reducer, `useReducer`'s, functions
160
+ - **Rules read from the code** (`gdp-wired/extract`): `extract('./bucket.ts')` reads a reducer, `useReducer`'s, functions
77
161
  over one record, a class whose methods write its fields (with the classes it extends), or the object of functions a
78
162
  factory returns, and returns the rule that steps the same way, as a builder value and as source to keep
79
- (`toSource`); `extractXState` reads an XState machine, or a function that makes one, its guards and actions named or
80
- inline, for `fromXState`. The extractor is untrusted: each rule is run beside the code it was read from (requests
163
+ (`toSource`); `extractXState('./machine.ts')` reads an XState machine, or a function that makes one, its guards and
164
+ actions named or inline, into a chart and its rule (xstate the one the file's project has). The extractor is untrusted: each rule is run beside the code it was read from (requests
81
165
  from the start, steps from drawn states and from the prover's counterexamples, every state, cell, list, result and
82
166
  refusal compared, the code's branches counted), and a difference refuses it. Its subset (integers, booleans, strings
83
167
  and string-literal unions, values that may be null, records, Map and Record keyed state, arrays as lists, Dates as
84
168
  their time, branches and early returns, throws as refusals, `Date.now()` as an input) and what it refuses, each with
85
169
  the place and the reason, are in `core/extract.ts`.
86
- - **Effect services** (`gdp-wired/effect`, `effect` 3.x or 4.x): `extractEffect(code, {entry, effect})` reads a service
170
+ - **Effect services** (`gdp-wired/effect`, `effect` 3.x or 4.x): `extractEffect('./limiter.ts')` reads a service
87
171
  whose make effect creates Refs (Ref, SynchronizedRef, SubscriptionRef, 4.x's TxRef) and returns an object of methods
88
172
  (`Effect.fn` bodies, arrows returning an effect), with typed failures as its refusals and `Clock.currentTimeMillis` as
89
- an input; the rule is checked by running the service on Effect's runtime (`Effect.runSyncExit`, the clock fixed at
90
- each request's time), every Ref, result and failure compared. `conforms(rule, service)` runs a rule written by hand
173
+ an input; the rule is checked by running the service on Effect's runtime (the `effect` the file's project has,
174
+ `Effect.runSyncExit`, the clock fixed at each request's time), every Ref, result and failure compared. `conforms(rule, service)` runs a rule written by hand
91
175
  beside a running service (Effect's own Semaphore, a store), and `fromSchema(schema)` is a Schema's kind
92
176
  (`examples/effect.ts`; what is read and refused, with what Effect projects write, in `research/effect/README.md`).
93
177
  - **What nobody stated, looked for when the prover alone stops**: where `proveRule` or `proveSteps` doesn't prove the laws
@@ -159,6 +243,11 @@ All of these are proven automatically, and each proof is also a Lean theorem:
159
243
  - **Pure functions and two-run laws**: laws about what a function returns, and laws comparing two calls, like "the
160
244
  secret never changes the answer". `examples/jev-fabric/` ports a Bend project's policy core and proves all 59 laws its
161
245
  Bend proofs state.
246
+ - **Laws about two runs of a stateful rule** (`gdp-wired/relational`): noninterference (`noninterference(rule, {secret,
247
+ visible})`: what a party sees never depends on the secret, nor which requests fail), tenant isolation (`isolation(rule,
248
+ {owner})`: what tenant B sees is the same whatever the others do) and migration agreement (`agreement(old, next)`),
249
+ on rules read from code as on written ones; `proveTwoRuns` proves the law of the two runs side by side as one rule, a
250
+ refutation the two runs (`research/regulated/`).
162
251
 
163
252
  When a law is false, the GPU search finds a real run that breaks it. When a law needs an invariant nobody wrote,
164
253
  `inferLaws` finds one on the CPU (and the GPU synthesizer, at scale, in `research/synth/`) and the kernel proves it.
@@ -0,0 +1,86 @@
1
+ import type { GoPkg } from '../extract-go.js';
2
+ type CN = {
3
+ id?: string;
4
+ kind: string;
5
+ name?: string;
6
+ inner?: CN[];
7
+ type?: {
8
+ qualType: string;
9
+ desugaredQualType?: string;
10
+ };
11
+ value?: string;
12
+ opcode?: string;
13
+ castKind?: string;
14
+ isArrow?: boolean;
15
+ isPostfix?: boolean;
16
+ referencedDecl?: {
17
+ id: string;
18
+ kind: string;
19
+ name: string;
20
+ type?: {
21
+ qualType: string;
22
+ };
23
+ };
24
+ referencedMemberDecl?: string;
25
+ storageClass?: string;
26
+ isImplicit?: boolean;
27
+ completeDefinition?: boolean;
28
+ tagUsed?: string;
29
+ hasElse?: boolean;
30
+ computeLHSType?: {
31
+ qualType: string;
32
+ desugaredQualType?: string;
33
+ };
34
+ init?: string;
35
+ decl?: {
36
+ id: string;
37
+ kind: string;
38
+ };
39
+ loc?: L;
40
+ range?: {
41
+ begin: L;
42
+ end: L;
43
+ };
44
+ $at?: At;
45
+ $b?: At;
46
+ $e?: At;
47
+ [k: string]: unknown;
48
+ };
49
+ type L = {
50
+ offset?: number;
51
+ file?: string;
52
+ line?: number;
53
+ col?: number;
54
+ tokLen?: number;
55
+ spellingLoc?: L;
56
+ expansionLoc?: L;
57
+ includedFrom?: unknown;
58
+ };
59
+ type At = {
60
+ file: string;
61
+ line: number;
62
+ col: number;
63
+ off: number;
64
+ tok: number;
65
+ };
66
+ export type CTarget = {
67
+ int: number;
68
+ long: number;
69
+ short: number;
70
+ llong: number;
71
+ charUnsigned: boolean;
72
+ };
73
+ export type CRead = {
74
+ pkg: GoPkg;
75
+ methods: string[];
76
+ entry: string;
77
+ cname: string;
78
+ };
79
+ export declare const node: <T>(id: string) => T;
80
+ export declare function clangDump(file: string, flags: string[], clang?: string, cpp?: boolean, filter?: string): {
81
+ tu: CN;
82
+ target: CTarget;
83
+ };
84
+ export declare function flagsFor(file: string, compileCommands?: string): string[];
85
+ export declare function readC(file: string, entry: string, tu: CN, target: CTarget, cpp?: boolean, root?: string, picks?: Record<string, number>): CRead;
86
+ export {};