@zanii/decisions 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 +159 -0
- package/README.md +49 -0
- package/dist/index.d.ts +56 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +96 -0
- package/dist/index.js.map +1 -0
- package/package.json +42 -0
- package/src/index.ts +141 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
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,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work; and
|
|
103
|
+
|
|
104
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
105
|
+
distribution, then any Derivative Works that You distribute must
|
|
106
|
+
include a readable copy of the attribution notices contained
|
|
107
|
+
within such NOTICE file.
|
|
108
|
+
|
|
109
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
110
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
111
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
112
|
+
this License, without any additional terms or conditions.
|
|
113
|
+
|
|
114
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
115
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
116
|
+
except as required for reasonable and customary use in describing the
|
|
117
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
118
|
+
|
|
119
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed
|
|
120
|
+
to in writing, Licensor provides the Work (and each Contributor
|
|
121
|
+
provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES
|
|
122
|
+
OR CONDITIONS OF ANY KIND, either express or implied, including,
|
|
123
|
+
without limitation, any warranties or conditions of TITLE,
|
|
124
|
+
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
|
|
125
|
+
|
|
126
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
127
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
128
|
+
unless required by applicable law (such as deliberate and grossly
|
|
129
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
130
|
+
liable to You for damages, including any direct, indirect, special,
|
|
131
|
+
incidental, or consequential damages of any character arising as a
|
|
132
|
+
result of this License or out of the use or inability to use the
|
|
133
|
+
Work.
|
|
134
|
+
|
|
135
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
136
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
137
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
138
|
+
or other liability obligations and/or rights consistent with this
|
|
139
|
+
License. However, in accepting such obligations, You may act only
|
|
140
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
141
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
142
|
+
defend, and hold each Contributor harmless for any liability
|
|
143
|
+
incurred by, or claims asserted against, such Contributor.
|
|
144
|
+
|
|
145
|
+
END OF TERMS AND CONDITIONS
|
|
146
|
+
|
|
147
|
+
Copyright 2026 Zanii
|
|
148
|
+
|
|
149
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
150
|
+
you may not use this file except in compliance with the License.
|
|
151
|
+
You may obtain a copy of the License at
|
|
152
|
+
|
|
153
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
154
|
+
|
|
155
|
+
Unless required by applicable law or agreed to in writing, software
|
|
156
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
157
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
158
|
+
See the License for the specific language governing permissions and
|
|
159
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @zanii/decisions
|
|
2
|
+
|
|
3
|
+
**Auditable algorithmic decisions about a person.** A driver, seller, or creator can't
|
|
4
|
+
audit the decisions platforms' algorithms make about them — dispatch, ranking,
|
|
5
|
+
demonetization, payout. `@zanii/subject` already gives them *their slice*; this adds the
|
|
6
|
+
decision-shaped receipt and the one fairness claim cryptography can actually make.
|
|
7
|
+
|
|
8
|
+
**What it truthfully proves:**
|
|
9
|
+
1. **Completeness** — every decision about me is on the record, ordered, tamper-evident.
|
|
10
|
+
2. **Rule-consistency** — every decision carries `manifest_hash` (which algorithm/policy
|
|
11
|
+
version governed it; the builder **rejects** a decision without a rulebook). A worker —
|
|
12
|
+
or a regulator across many workers — verifies the **same rulebook decided everyone**,
|
|
13
|
+
and exactly when it changed. `ruleConsistency` flags interleaved rulebooks: rules that
|
|
14
|
+
differed between people at the same time.
|
|
15
|
+
3. **Factor commitment** — the inputs the algorithm used are salt-committed at decision
|
|
16
|
+
time; in a dispute the platform discloses *my* factors to me alone, provably what was
|
|
17
|
+
committed — not a post-hoc rationalization.
|
|
18
|
+
|
|
19
|
+
**What it cannot prove (read this):** that the algorithm is *fair* — no receipt makes a
|
|
20
|
+
bad rule good. It converts "trust our algorithm" into **same rules, complete record,
|
|
21
|
+
committed inputs** — which is what platform-work transparency regulation actually asks for.
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install @zanii/decisions @zanii/subject @zanii/core
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { buildDecisionReceipt, ruleConsistency } from '@zanii/decisions';
|
|
29
|
+
|
|
30
|
+
// platform side — at decision time:
|
|
31
|
+
const d = buildDecisionReceipt({
|
|
32
|
+
subjectDid: driver.did, platform: 'gig.example.com',
|
|
33
|
+
kind: 'dispatch', manifestHash: deployedRulesHash, // required — no rulebook, no receipt
|
|
34
|
+
outcome: { assigned: false }, factors: { rating: 4.9, distance_km: 2.1 }, ts,
|
|
35
|
+
});
|
|
36
|
+
await zanii.record({ target: d.target, payload: d.payload, subjectTag: d.subjectTag });
|
|
37
|
+
// keep d.nonce — in a dispute, disclose the committed factors with verifyPayload
|
|
38
|
+
|
|
39
|
+
// auditor side — over a slice (one worker, or many):
|
|
40
|
+
ruleConsistency(payloads); // rulebook windows + the interleaving red flag
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Reference kinds: `dispatch`, `rank`, `payout` — any string works.
|
|
44
|
+
|
|
45
|
+
## Changelog
|
|
46
|
+
- **0.1.0** — initial release: `buildDecisionReceipt`, `verifyDecision`, `ruleConsistency`.
|
|
47
|
+
|
|
48
|
+
## License
|
|
49
|
+
Apache-2.0.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** Reference kinds; any string works. */
|
|
2
|
+
export type DecisionKind = 'dispatch' | 'rank' | 'payout' | (string & {});
|
|
3
|
+
export interface DecisionInput {
|
|
4
|
+
/** The worker/creator the decision is about (their did:key)… */
|
|
5
|
+
subjectDid?: string;
|
|
6
|
+
platform?: string;
|
|
7
|
+
/** …or a precomputed subject tag. */
|
|
8
|
+
tag?: string;
|
|
9
|
+
kind: DecisionKind;
|
|
10
|
+
/** REQUIRED: hash of the algorithm/policy version that governed this decision. */
|
|
11
|
+
manifestHash: string;
|
|
12
|
+
/** The decision's outcome (visible on the receipt payload, e.g. `{ assigned: true }`). */
|
|
13
|
+
outcome: Record<string, unknown>;
|
|
14
|
+
/** The inputs the algorithm used — salt-committed, never raw on-ledger. */
|
|
15
|
+
factors?: Record<string, unknown>;
|
|
16
|
+
ts: string;
|
|
17
|
+
}
|
|
18
|
+
export interface DecisionReceipt {
|
|
19
|
+
/** `decision.<kind>` — pass to `agent.record({ target, payload, subjectTag })`. */
|
|
20
|
+
target: string;
|
|
21
|
+
payload: Record<string, unknown>;
|
|
22
|
+
/** Stamp this as the receipt's `subjectTag` so the worker's slice includes it. */
|
|
23
|
+
subjectTag: string;
|
|
24
|
+
/** REQUIRED for `provenance.manifestHash` on `record` too, if you want it receipt-level. */
|
|
25
|
+
manifestHash: string;
|
|
26
|
+
/** Keep to disclose the factors in a dispute (`verifyPayload`). Empty if no factors. */
|
|
27
|
+
nonce: string;
|
|
28
|
+
}
|
|
29
|
+
/** Build a decision receipt. A decision without a rulebook (`manifestHash`) is rejected. */
|
|
30
|
+
export declare function buildDecisionReceipt(input: DecisionInput): DecisionReceipt;
|
|
31
|
+
export interface DecisionCheck {
|
|
32
|
+
ok: boolean;
|
|
33
|
+
reasons: string[];
|
|
34
|
+
}
|
|
35
|
+
/** Structurally validate a decision payload (rulebook hash present, ts valid). */
|
|
36
|
+
export declare function verifyDecision(payload: Record<string, unknown> | undefined): DecisionCheck;
|
|
37
|
+
export interface RulebookWindow {
|
|
38
|
+
manifest_hash: string;
|
|
39
|
+
count: number;
|
|
40
|
+
first_ts: string;
|
|
41
|
+
last_ts: string;
|
|
42
|
+
}
|
|
43
|
+
export interface ConsistencyReport {
|
|
44
|
+
/** Rulebook windows ordered by first use. */
|
|
45
|
+
rulebooks: RulebookWindow[];
|
|
46
|
+
/** true iff rulebook usage windows don't interleave — one rulebook at a time, clean cutovers. */
|
|
47
|
+
consistent: boolean;
|
|
48
|
+
reasons: string[];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The audit artifact: group decision payloads by `manifest_hash` over time. Consistent =
|
|
52
|
+
* one rulebook at a time with clean cutovers (no per-person rule switching); an
|
|
53
|
+
* interleaved window is the red flag a regulator looks for.
|
|
54
|
+
*/
|
|
55
|
+
export declare function ruleConsistency(payloads: Array<Record<string, unknown>>): ConsistencyReport;
|
|
56
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAyBA,yCAAyC;AACzC,MAAM,MAAM,YAAY,GAAG,UAAU,GAAG,MAAM,GAAG,QAAQ,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;AAE1E,MAAM,WAAW,aAAa;IAC5B,gEAAgE;IAChE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,qCAAqC;IACrC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,YAAY,CAAC;IACnB,kFAAkF;IAClF,YAAY,EAAE,MAAM,CAAC;IACrB,0FAA0F;IAC1F,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,2EAA2E;IAC3E,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,MAAM,WAAW,eAAe;IAC9B,mFAAmF;IACnF,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,kFAAkF;IAClF,UAAU,EAAE,MAAM,CAAC;IACnB,4FAA4F;IAC5F,YAAY,EAAE,MAAM,CAAC;IACrB,wFAAwF;IACxF,KAAK,EAAE,MAAM,CAAC;CACf;AAED,4FAA4F;AAC5F,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,aAAa,GAAG,eAAe,CAkB1E;AAED,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,OAAO,CAAC;IACZ,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB;AAED,kFAAkF;AAClF,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,GAAG,aAAa,CAQ1F;AAED,MAAM,WAAW,cAAc;IAC7B,aAAa,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB;IAChC,6CAA6C;IAC7C,SAAS,EAAE,cAAc,EAAE,CAAC;IAC5B,iGAAiG;IACjG,UAAU,EAAE,OAAO,CAAC;IACpB,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,iBAAiB,CA2B3F"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zanii/decisions — gig/creator algorithmic fairness: auditable decisions about a person.
|
|
3
|
+
*
|
|
4
|
+
* A driver, seller, or creator can't audit the algorithmic decisions made about them —
|
|
5
|
+
* dispatch, ranking, demonetization, payout. Per-subject auditability (`@zanii/subject`)
|
|
6
|
+
* already gives them *their slice*; this adds the decision-shaped receipt and the one
|
|
7
|
+
* fairness claim cryptography can actually make.
|
|
8
|
+
*
|
|
9
|
+
* What it truthfully proves:
|
|
10
|
+
* 1. **Completeness** — every decision about me is on the record, ordered, tamper-evident.
|
|
11
|
+
* 2. **Rule-consistency** — every decision receipt MUST carry `manifest_hash` (which
|
|
12
|
+
* algorithm/policy version governed it — the builder rejects a decision without a
|
|
13
|
+
* rulebook). A worker, or a regulator across many workers, can verify the SAME
|
|
14
|
+
* rulebook decided everyone in a period, and exactly when it changed.
|
|
15
|
+
* 3. **Factor commitment** — the inputs the platform claims it used are salt-committed at
|
|
16
|
+
* decision time; in a dispute it discloses my factors to me alone (`@zanii/redact` /
|
|
17
|
+
* `verifyPayload`) — provably what was committed, not a post-hoc rationalization.
|
|
18
|
+
*
|
|
19
|
+
* What it cannot prove (say it): that the algorithm is **fair** — no receipt makes a bad
|
|
20
|
+
* rule good. It converts "trust our algorithm" into "same rules, complete record,
|
|
21
|
+
* committed inputs" — which is what platform-work transparency regulation actually asks.
|
|
22
|
+
*/
|
|
23
|
+
import { saltedPayloadHash } from '@zanii/core';
|
|
24
|
+
import { subjectTag } from '@zanii/subject';
|
|
25
|
+
/** Build a decision receipt. A decision without a rulebook (`manifestHash`) is rejected. */
|
|
26
|
+
export function buildDecisionReceipt(input) {
|
|
27
|
+
if (!input.kind)
|
|
28
|
+
throw new Error('kind is required');
|
|
29
|
+
if (!input.manifestHash)
|
|
30
|
+
throw new Error('manifestHash is required — a decision receipt without the governing rulebook is not auditable');
|
|
31
|
+
if (Number.isNaN(Date.parse(input.ts)))
|
|
32
|
+
throw new Error('ts must be an ISO timestamp');
|
|
33
|
+
const tag = input.tag ?? subjectTag(input.subjectDid ?? '', input.platform ?? '');
|
|
34
|
+
const { payload_hash, nonce } = input.factors !== undefined
|
|
35
|
+
? saltedPayloadHash(JSON.stringify(input.factors))
|
|
36
|
+
: { payload_hash: undefined, nonce: '' };
|
|
37
|
+
const payload = {
|
|
38
|
+
_zr_kind: 'decision',
|
|
39
|
+
v: 1,
|
|
40
|
+
kind: input.kind,
|
|
41
|
+
manifest_hash: input.manifestHash,
|
|
42
|
+
outcome: input.outcome,
|
|
43
|
+
...(payload_hash ? { factors_commitment: payload_hash } : {}),
|
|
44
|
+
ts: input.ts,
|
|
45
|
+
};
|
|
46
|
+
return { target: `decision.${input.kind}`, payload, subjectTag: tag, manifestHash: input.manifestHash, nonce };
|
|
47
|
+
}
|
|
48
|
+
/** Structurally validate a decision payload (rulebook hash present, ts valid). */
|
|
49
|
+
export function verifyDecision(payload) {
|
|
50
|
+
const reasons = [];
|
|
51
|
+
const p = payload ?? {};
|
|
52
|
+
if (p._zr_kind !== 'decision' || p.v !== 1)
|
|
53
|
+
reasons.push('not a decision receipt (v1)');
|
|
54
|
+
if (!p.kind)
|
|
55
|
+
reasons.push('missing kind');
|
|
56
|
+
if (typeof p.manifest_hash !== 'string' || !p.manifest_hash)
|
|
57
|
+
reasons.push('missing manifest_hash (no governing rulebook)');
|
|
58
|
+
if (typeof p.ts !== 'string' || Number.isNaN(Date.parse(p.ts)))
|
|
59
|
+
reasons.push('missing/invalid ts');
|
|
60
|
+
return { ok: reasons.length === 0, reasons };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The audit artifact: group decision payloads by `manifest_hash` over time. Consistent =
|
|
64
|
+
* one rulebook at a time with clean cutovers (no per-person rule switching); an
|
|
65
|
+
* interleaved window is the red flag a regulator looks for.
|
|
66
|
+
*/
|
|
67
|
+
export function ruleConsistency(payloads) {
|
|
68
|
+
const reasons = [];
|
|
69
|
+
const windows = new Map();
|
|
70
|
+
const ordered = [...payloads].sort((a, b) => String(a.ts).localeCompare(String(b.ts)));
|
|
71
|
+
for (const p of ordered) {
|
|
72
|
+
const check = verifyDecision(p);
|
|
73
|
+
if (!check.ok) {
|
|
74
|
+
reasons.push(`invalid decision: ${check.reasons.join('; ')}`);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const h = p.manifest_hash;
|
|
78
|
+
const ts = p.ts;
|
|
79
|
+
const w = windows.get(h);
|
|
80
|
+
if (w) {
|
|
81
|
+
w.count += 1;
|
|
82
|
+
w.last_ts = ts;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
windows.set(h, { manifest_hash: h, count: 1, first_ts: ts, last_ts: ts });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const rulebooks = [...windows.values()].sort((a, b) => a.first_ts.localeCompare(b.first_ts));
|
|
89
|
+
for (let i = 1; i < rulebooks.length; i++) {
|
|
90
|
+
if (rulebooks[i].first_ts < rulebooks[i - 1].last_ts) {
|
|
91
|
+
reasons.push(`rulebooks ${rulebooks[i - 1].manifest_hash} and ${rulebooks[i].manifest_hash} interleave — rules differed between people at the same time`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return { rulebooks, consistent: reasons.length === 0, reasons };
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAiC5C,4FAA4F;AAC5F,MAAM,UAAU,oBAAoB,CAAC,KAAoB;IACvD,IAAI,CAAC,KAAK,CAAC,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;IACrD,IAAI,CAAC,KAAK,CAAC,YAAY;QAAE,MAAM,IAAI,KAAK,CAAC,+FAA+F,CAAC,CAAC;IAC1I,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACvF,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,UAAU,IAAI,EAAE,EAAE,KAAK,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC;IAClF,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC,OAAO,KAAK,SAAS;QACzD,CAAC,CAAC,iBAAiB,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,CA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C;IACD,MAAM,SAAS,GAAG,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC7F,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAC1C,IAAI,SAAS,CAAC,CAAC,CAAE,CAAC,QAAQ,GAAG,SAAS,CAAC,CAAC,GAAG,CAAC,CAAE,CAAC,OAAO,EAAE,CAAC;YACvD,OAAO,CAAC,IAAI,CAAC,aAAa,SAAS,CAAC,CAAC,GAAG,CAAC,CAAE,CAAC,aAAa,QAAQ,SAAS,CAAC,CAAC,CAAE,CAAC,aAAa,8DAA8D,CAAC,CAAC;QAC9J,CAAC;IACH,CAAC;IACD,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,OAAO,EAAE,CAAC;AAClE,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zanii/decisions",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Gig/creator algorithmic fairness: decision receipts a worker can audit — completeness (every decision about me, tamper-evident), rule-consistency (the same rulebook decided everyone, via manifest_hash), and committed factors (no post-hoc rationalization). Never claims 'the algorithm is fair'.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"homepage": "https://ledger.zanii.agency",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^26.1.0",
|
|
30
|
+
"typescript": "^5.8.2",
|
|
31
|
+
"vitest": "^3.0.9"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@zanii/subject": "0.1.0",
|
|
35
|
+
"@zanii/core": "0.4.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc -p tsconfig.build.json",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"typecheck": "tsc --noEmit"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zanii/decisions — gig/creator algorithmic fairness: auditable decisions about a person.
|
|
3
|
+
*
|
|
4
|
+
* A driver, seller, or creator can't audit the algorithmic decisions made about them —
|
|
5
|
+
* dispatch, ranking, demonetization, payout. Per-subject auditability (`@zanii/subject`)
|
|
6
|
+
* already gives them *their slice*; this adds the decision-shaped receipt and the one
|
|
7
|
+
* fairness claim cryptography can actually make.
|
|
8
|
+
*
|
|
9
|
+
* What it truthfully proves:
|
|
10
|
+
* 1. **Completeness** — every decision about me is on the record, ordered, tamper-evident.
|
|
11
|
+
* 2. **Rule-consistency** — every decision receipt MUST carry `manifest_hash` (which
|
|
12
|
+
* algorithm/policy version governed it — the builder rejects a decision without a
|
|
13
|
+
* rulebook). A worker, or a regulator across many workers, can verify the SAME
|
|
14
|
+
* rulebook decided everyone in a period, and exactly when it changed.
|
|
15
|
+
* 3. **Factor commitment** — the inputs the platform claims it used are salt-committed at
|
|
16
|
+
* decision time; in a dispute it discloses my factors to me alone (`@zanii/redact` /
|
|
17
|
+
* `verifyPayload`) — provably what was committed, not a post-hoc rationalization.
|
|
18
|
+
*
|
|
19
|
+
* What it cannot prove (say it): that the algorithm is **fair** — no receipt makes a bad
|
|
20
|
+
* rule good. It converts "trust our algorithm" into "same rules, complete record,
|
|
21
|
+
* committed inputs" — which is what platform-work transparency regulation actually asks.
|
|
22
|
+
*/
|
|
23
|
+
import { saltedPayloadHash } from '@zanii/core';
|
|
24
|
+
import { subjectTag } from '@zanii/subject';
|
|
25
|
+
|
|
26
|
+
/** Reference kinds; any string works. */
|
|
27
|
+
export type DecisionKind = 'dispatch' | 'rank' | 'payout' | (string & {});
|
|
28
|
+
|
|
29
|
+
export interface DecisionInput {
|
|
30
|
+
/** The worker/creator the decision is about (their did:key)… */
|
|
31
|
+
subjectDid?: string;
|
|
32
|
+
platform?: string;
|
|
33
|
+
/** …or a precomputed subject tag. */
|
|
34
|
+
tag?: string;
|
|
35
|
+
kind: DecisionKind;
|
|
36
|
+
/** REQUIRED: hash of the algorithm/policy version that governed this decision. */
|
|
37
|
+
manifestHash: string;
|
|
38
|
+
/** The decision's outcome (visible on the receipt payload, e.g. `{ assigned: true }`). */
|
|
39
|
+
outcome: Record<string, unknown>;
|
|
40
|
+
/** The inputs the algorithm used — salt-committed, never raw on-ledger. */
|
|
41
|
+
factors?: Record<string, unknown>;
|
|
42
|
+
ts: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface DecisionReceipt {
|
|
46
|
+
/** `decision.<kind>` — pass to `agent.record({ target, payload, subjectTag })`. */
|
|
47
|
+
target: string;
|
|
48
|
+
payload: Record<string, unknown>;
|
|
49
|
+
/** Stamp this as the receipt's `subjectTag` so the worker's slice includes it. */
|
|
50
|
+
subjectTag: string;
|
|
51
|
+
/** REQUIRED for `provenance.manifestHash` on `record` too, if you want it receipt-level. */
|
|
52
|
+
manifestHash: string;
|
|
53
|
+
/** Keep to disclose the factors in a dispute (`verifyPayload`). Empty if no factors. */
|
|
54
|
+
nonce: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Build a decision receipt. A decision without a rulebook (`manifestHash`) is rejected. */
|
|
58
|
+
export function buildDecisionReceipt(input: DecisionInput): DecisionReceipt {
|
|
59
|
+
if (!input.kind) throw new Error('kind is required');
|
|
60
|
+
if (!input.manifestHash) throw new Error('manifestHash is required — a decision receipt without the governing rulebook is not auditable');
|
|
61
|
+
if (Number.isNaN(Date.parse(input.ts))) throw new Error('ts must be an ISO timestamp');
|
|
62
|
+
const tag = input.tag ?? subjectTag(input.subjectDid ?? '', input.platform ?? '');
|
|
63
|
+
const { payload_hash, nonce } = input.factors !== undefined
|
|
64
|
+
? saltedPayloadHash(JSON.stringify(input.factors))
|
|
65
|
+
: { payload_hash: undefined, nonce: '' };
|
|
66
|
+
const payload: Record<string, unknown> = {
|
|
67
|
+
_zr_kind: 'decision',
|
|
68
|
+
v: 1,
|
|
69
|
+
kind: input.kind,
|
|
70
|
+
manifest_hash: input.manifestHash,
|
|
71
|
+
outcome: input.outcome,
|
|
72
|
+
...(payload_hash ? { factors_commitment: payload_hash } : {}),
|
|
73
|
+
ts: input.ts,
|
|
74
|
+
};
|
|
75
|
+
return { target: `decision.${input.kind}`, payload, subjectTag: tag, manifestHash: input.manifestHash, nonce };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface DecisionCheck {
|
|
79
|
+
ok: boolean;
|
|
80
|
+
reasons: string[];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Structurally validate a decision payload (rulebook hash present, ts valid). */
|
|
84
|
+
export function verifyDecision(payload: Record<string, unknown> | undefined): DecisionCheck {
|
|
85
|
+
const reasons: string[] = [];
|
|
86
|
+
const p = payload ?? {};
|
|
87
|
+
if (p._zr_kind !== 'decision' || p.v !== 1) reasons.push('not a decision receipt (v1)');
|
|
88
|
+
if (!p.kind) reasons.push('missing kind');
|
|
89
|
+
if (typeof p.manifest_hash !== 'string' || !p.manifest_hash) reasons.push('missing manifest_hash (no governing rulebook)');
|
|
90
|
+
if (typeof p.ts !== 'string' || Number.isNaN(Date.parse(p.ts))) reasons.push('missing/invalid ts');
|
|
91
|
+
return { ok: reasons.length === 0, reasons };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface RulebookWindow {
|
|
95
|
+
manifest_hash: string;
|
|
96
|
+
count: number;
|
|
97
|
+
first_ts: string;
|
|
98
|
+
last_ts: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface ConsistencyReport {
|
|
102
|
+
/** Rulebook windows ordered by first use. */
|
|
103
|
+
rulebooks: RulebookWindow[];
|
|
104
|
+
/** true iff rulebook usage windows don't interleave — one rulebook at a time, clean cutovers. */
|
|
105
|
+
consistent: boolean;
|
|
106
|
+
reasons: string[];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The audit artifact: group decision payloads by `manifest_hash` over time. Consistent =
|
|
111
|
+
* one rulebook at a time with clean cutovers (no per-person rule switching); an
|
|
112
|
+
* interleaved window is the red flag a regulator looks for.
|
|
113
|
+
*/
|
|
114
|
+
export function ruleConsistency(payloads: Array<Record<string, unknown>>): ConsistencyReport {
|
|
115
|
+
const reasons: string[] = [];
|
|
116
|
+
const windows = new Map<string, RulebookWindow>();
|
|
117
|
+
const ordered = [...payloads].sort((a, b) => String(a.ts).localeCompare(String(b.ts)));
|
|
118
|
+
for (const p of ordered) {
|
|
119
|
+
const check = verifyDecision(p);
|
|
120
|
+
if (!check.ok) {
|
|
121
|
+
reasons.push(`invalid decision: ${check.reasons.join('; ')}`);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const h = p.manifest_hash as string;
|
|
125
|
+
const ts = p.ts as string;
|
|
126
|
+
const w = windows.get(h);
|
|
127
|
+
if (w) {
|
|
128
|
+
w.count += 1;
|
|
129
|
+
w.last_ts = ts;
|
|
130
|
+
} else {
|
|
131
|
+
windows.set(h, { manifest_hash: h, count: 1, first_ts: ts, last_ts: ts });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const rulebooks = [...windows.values()].sort((a, b) => a.first_ts.localeCompare(b.first_ts));
|
|
135
|
+
for (let i = 1; i < rulebooks.length; i++) {
|
|
136
|
+
if (rulebooks[i]!.first_ts < rulebooks[i - 1]!.last_ts) {
|
|
137
|
+
reasons.push(`rulebooks ${rulebooks[i - 1]!.manifest_hash} and ${rulebooks[i]!.manifest_hash} interleave — rules differed between people at the same time`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return { rulebooks, consistent: reasons.length === 0, reasons };
|
|
141
|
+
}
|