@mago-foundation/mago-ski 0.2.0 → 0.3.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 (74) hide show
  1. package/README.md +30 -3
  2. package/dist/hosts/github/run.d.ts +3 -1
  3. package/dist/hosts/github/run.js +29 -9
  4. package/dist/hosts/pi/index.js +48 -5
  5. package/dist/src/admin.d.ts +136 -1
  6. package/dist/src/admin.js +221 -84
  7. package/dist/src/cli.js +335 -10
  8. package/dist/src/fs-safe.d.ts +15 -2
  9. package/dist/src/fs-safe.js +32 -3
  10. package/dist/src/gate/adapters/claude-code-hook.d.ts +1 -0
  11. package/dist/src/gate/adapters/claude-code-hook.js +4 -0
  12. package/dist/src/gate/adapters/claude-code-settings.d.ts +13 -0
  13. package/dist/src/gate/adapters/claude-code-settings.js +49 -0
  14. package/dist/src/gate/adapters/claude-code.d.ts +33 -0
  15. package/dist/src/gate/adapters/claude-code.js +193 -0
  16. package/dist/src/gate/adapters/client.d.ts +13 -0
  17. package/dist/src/gate/adapters/client.js +57 -0
  18. package/dist/src/gate/adapters/codex-hook.d.ts +1 -0
  19. package/dist/src/gate/adapters/codex-hook.js +5 -0
  20. package/dist/src/gate/adapters/codex-requirements.d.ts +19 -0
  21. package/dist/src/gate/adapters/codex-requirements.js +56 -0
  22. package/dist/src/gate/adapters/codex.d.ts +16 -0
  23. package/dist/src/gate/adapters/codex.js +574 -0
  24. package/dist/src/gate/adapters/hook-main.d.ts +7 -0
  25. package/dist/src/gate/adapters/hook-main.js +35 -0
  26. package/dist/src/gate/adapters/host-pid.d.ts +1 -0
  27. package/dist/src/gate/adapters/host-pid.js +20 -0
  28. package/dist/src/gate/admission.d.ts +54 -0
  29. package/dist/src/gate/admission.js +490 -0
  30. package/dist/src/gate/contract.d.ts +51 -0
  31. package/dist/src/gate/contract.js +94 -0
  32. package/dist/src/gate/decision-log.d.ts +38 -0
  33. package/dist/src/gate/decision-log.js +251 -0
  34. package/dist/src/gate/generations.d.ts +67 -0
  35. package/dist/src/gate/generations.js +518 -0
  36. package/dist/src/gate/proc.d.ts +31 -0
  37. package/dist/src/gate/proc.js +79 -0
  38. package/dist/src/gate/refresh.d.ts +11 -0
  39. package/dist/src/gate/refresh.js +30 -0
  40. package/dist/src/gate/run-verifier.d.ts +23 -0
  41. package/dist/src/gate/run-verifier.js +89 -0
  42. package/dist/src/gate/service.d.ts +80 -0
  43. package/dist/src/gate/service.js +722 -0
  44. package/dist/src/gate/strict-json.d.ts +11 -0
  45. package/dist/src/gate/strict-json.js +190 -0
  46. package/dist/src/gate/trust-store.d.ts +36 -0
  47. package/dist/src/gate/trust-store.js +251 -0
  48. package/dist/src/gate/verifier-worker.d.ts +8 -0
  49. package/dist/src/gate/verifier-worker.js +66 -0
  50. package/dist/src/index.d.ts +2 -0
  51. package/dist/src/index.js +2 -0
  52. package/dist/src/keys.d.ts +4 -0
  53. package/dist/src/keys.js +23 -14
  54. package/dist/src/policy.d.ts +145 -0
  55. package/dist/src/policy.js +554 -35
  56. package/dist/src/recovery-admin.d.ts +173 -0
  57. package/dist/src/recovery-admin.js +400 -0
  58. package/dist/src/root-events.d.ts +107 -0
  59. package/dist/src/root-events.js +376 -0
  60. package/dist/src/timestamp.d.ts +61 -0
  61. package/dist/src/timestamp.js +107 -0
  62. package/dist/src/tree-digest.d.ts +1 -0
  63. package/dist/src/tree-digest.js +1 -1
  64. package/dist/src/verify.d.ts +33 -0
  65. package/dist/src/verify.js +4 -2
  66. package/docs/hosts/adapter-interface.md +8 -2
  67. package/docs/hosts/github-action.md +1 -1
  68. package/docs/hosts/pi.md +2 -0
  69. package/docs/spec/gui-admin-rack-v1.md +289 -0
  70. package/docs/spec/root-recovery-v1.md +33 -12
  71. package/docs/spec/trust-documents-v1.md +8 -2
  72. package/docs/spec/verification.md +9 -2
  73. package/docs/threat-model.md +4 -2
  74. package/package.json +3 -1
@@ -1,14 +1,52 @@
1
+ import { type RecoverySet, type RootWalk } from './root-events.ts';
1
2
  import { type CertificateSource, type TrustContext, type TrustContextResult, type VerificationResult } from './verify.ts';
2
3
  export declare const POLICY_VERSION = "mago.policy/v1";
4
+ /** Adds root recovery and freshness; a v2 host reads v1 policies unchanged. */
5
+ export declare const POLICY_VERSION_V2 = "mago.policy/v2";
3
6
  export declare const STATE_TYPE = "mago.host-state/v1";
7
+ export declare const STATE_TYPE_V2 = "mago.host-state/v2";
4
8
  /** Per-Skill budget across all certificate candidates (explicit file, policy directories, in-Skill file). */
5
9
  export declare const MAX_CERTIFICATE_CANDIDATES = 64;
6
10
  export declare const MAX_CERTIFICATE_TOTAL_BYTES: number;
7
11
  export type Mode = 'shadow' | 'enforce';
12
+ interface RootState {
13
+ trust_root_version: number;
14
+ revocations_version: number;
15
+ }
16
+ /** Host state: versions per root key (v1), plus root event progress per pinned root and timestamp versions per key (v2). */
17
+ export interface HostState {
18
+ roots: Record<string, RootState>;
19
+ /** `revoked` is set when the last applied event left the root revoked (written only when true). */
20
+ rootEvents: Record<string, {
21
+ sequence: number;
22
+ event_digest: string;
23
+ revoked?: true;
24
+ }>;
25
+ timestamps: Record<string, {
26
+ version: number;
27
+ }>;
28
+ /** The state was read as v2; it is never written back as v1. */
29
+ v2: boolean;
30
+ }
8
31
  export declare function defaultPolicyPath(env?: NodeJS.ProcessEnv, home?: string): string;
32
+ export interface FreshnessPolicy {
33
+ timestampPath: string;
34
+ /** The timestamp key trusted before any root event. */
35
+ keyId: string;
36
+ maxLifetimeMs: number;
37
+ }
38
+ export interface RecoveryPolicy {
39
+ /** The recovery set trusted before any root event. */
40
+ set: RecoverySet;
41
+ rootEventsPath: string;
42
+ /** How many events root_fingerprint, recovery and freshness.key_id already reflect. */
43
+ eventsAfter: number;
44
+ freshness: FreshnessPolicy | null;
45
+ }
9
46
  export interface Policy {
10
47
  path: string;
11
48
  mode: Mode;
49
+ /** The pinned root key: the one trusted before any root event (it does not change after a rotation). */
12
50
  rootKeyId: string;
13
51
  trustRootPath: string;
14
52
  revocationsPath: string;
@@ -16,17 +54,105 @@ export interface Policy {
16
54
  skillDirs: string[];
17
55
  statePath: string | null;
18
56
  decisionLogPath: string | null;
57
+ /** Root recovery (policy v2 with a "recovery" block); null behaves exactly as a v1 policy. */
58
+ recovery: RecoveryPolicy | null;
19
59
  }
20
60
  /** Policy files are hand-edited JSON; paths are relative to the policy file's directory. */
21
61
  export declare function loadPolicy(policyPath: string): Promise<Policy>;
62
+ /** The host state file exists and was read, but its content or kind is wrong. Every other read error is not this. */
63
+ export declare class HostStateFormatError extends Error {
64
+ constructor(message: string);
65
+ }
66
+ /** The host state file as it is now (a missing file is an empty state). The GUI memory uses this format too. */
67
+ export declare function readHostState(statePath: string): Promise<HostState>;
68
+ /**
69
+ * Combines this load's state with the state file as it is now, so two hosts sharing one state file (two Pi sessions,
70
+ * Pi and the CLI) cannot erase each other's progress: every counter keeps its highest value, the root event entry
71
+ * with the higher sequence wins (on a tie with different digests, the one already on disk stays, so the fork is
72
+ * reported on the next load), and v2 is never written back as v1.
73
+ */
74
+ export declare function mergeHostState(ours: HostState, disk: HostState): HostState;
75
+ /**
76
+ * Records document versions in a host state file (the GUI memory), merging with what is on disk: every counter keeps
77
+ * its highest value, so this never lowers anything within the retry budget of `persistState` (it fails closed otherwise). A root entry takes each given half and `1` (the lowest valid
78
+ * value) for a missing one. Throws when the file cannot be read or written.
79
+ */
80
+ export declare function recordDocumentVersions(statePath: string, v: {
81
+ rootKeyId?: string;
82
+ trustRoot?: number;
83
+ revocations?: number;
84
+ timestamp?: {
85
+ keyId: string;
86
+ version: number;
87
+ };
88
+ }): Promise<void>;
89
+ /** The lowest versions this computer's memory allows for each document; 0 where nothing is recorded. */
90
+ export declare function memoryMinimums(state: HostState, rootKeyId: string, timestampKeyId: string | null): {
91
+ trustRoot: number;
92
+ revocations: number;
93
+ timestamp: number;
94
+ };
95
+ export interface ForbiddenKey {
96
+ keyId: string;
97
+ why: 'root' | 'recovery' | 'timestamp' | 'retired' | 'formerRecovery' | 'revoked';
98
+ }
99
+ /**
100
+ * Keys that must not be approvers in this organisation: the root, the recovery set, the timestamp key, keys that left
101
+ * the root or timestamp role, keys that left a recovery set, and every revoked key. Neither reads nor writes host state.
102
+ * Throws when the root event log or the revocation list cannot be checked, and `StaleDocumentError` when the revocation
103
+ * list is older than `minimumRevocations` (the caller's memory; the CLI passes none).
104
+ */
105
+ export declare function forbiddenApproverKeys(policy: Policy, now: Date, o?: {
106
+ minimumRevocations?: number;
107
+ }): Promise<ForbiddenKey[]>;
108
+ export type StateErrorKind = 'corrupt' | 'unreadable' | 'write';
109
+ export type PolicyRootResult = {
110
+ ok: true;
111
+ walk: RootWalk | null;
112
+ activeRootKeyId: string;
113
+ } | {
114
+ ok: false;
115
+ reason: string;
116
+ rootRevoked: boolean;
117
+ walk: RootWalk | null;
118
+ rollback?: true;
119
+ stateError?: StateErrorKind;
120
+ };
121
+ /**
122
+ * Only the root-event part of `loadPolicyTrust`: which root key is active, whether it is revoked, and the walk. It
123
+ * reads the state and the log, checks rollback and forks the same way, and with `recordVersions` (the default) and a
124
+ * state file records the progress with the same merge-before-write. A policy without `recovery` reads nothing.
125
+ */
126
+ export declare function loadPolicyRoot(policy: Policy, now: Date, options?: {
127
+ recordVersions?: boolean;
128
+ }): Promise<PolicyRootResult>;
22
129
  /**
23
130
  * Loads trust documents for a policy, enforcing rollback protection through the state file.
24
131
  * With `recordVersions: false` the state file is still read (the same rollback minimums apply) but never written:
25
132
  * for observers such as the GUI, which must not move a host's rollback memory.
133
+ *
134
+ * With root recovery (policy v2), the order is: walk the root event log, check rollback and forks, stop if the active
135
+ * root is revoked (`rootRevoked`, which blocks in every mode), check the timestamp, then the trust root and revocation
136
+ * list against the active root, then record the new state.
26
137
  */
27
138
  export declare function loadPolicyTrust(policy: Policy, now: Date, options?: {
28
139
  recordVersions?: boolean;
29
140
  }): Promise<TrustContextResult>;
141
+ /** Documents a preview substitutes for the files on disk (the GUI's "what would hosts say after this write"). */
142
+ export interface PreviewOverride {
143
+ trustRoot?: Buffer;
144
+ revocations?: Buffer;
145
+ timestamp?: Buffer;
146
+ }
147
+ /**
148
+ * The host's own trust load, for a preview: never records versions, may replace the documents read from disk with
149
+ * in-memory bytes (`override`, subject to the same size limits), and with `skipFreshness` skips reading and checking
150
+ * the timestamp and its binding to the documents (`context.root.timestamp` is then null). Only the GUI service uses it.
151
+ */
152
+ export declare function loadTrustForPreview(policy: Policy, now: Date, o?: {
153
+ override?: PreviewOverride;
154
+ skipFreshness?: boolean;
155
+ }): Promise<TrustContextResult>;
30
156
  export declare function iterateCertificates(skillName: string, skillRoot: string, certificateDirs: string[], explicit?: string): AsyncGenerator<CertificateSource>;
31
157
  /** Collects every candidate (within the same budget). Prefer `iterateCertificates`, which retains nothing. */
32
158
  export declare function discoverCertificates(skillName: string, skillRoot: string, certificateDirs: string[], explicit?: string): Promise<CertificateSource[]>;
@@ -69,6 +195,24 @@ export interface DecisionRecord {
69
195
  verdict: string;
70
196
  reason: string;
71
197
  }
198
+ /** Event names for a failure of the trust documents, reported once per host instead of once per Skill. */
199
+ export type TrustFailureEvent = 'root-revoked' | 'trust-unavailable';
200
+ export declare function trustFailureEvent(failure: {
201
+ rootRevoked?: boolean | undefined;
202
+ }): TrustFailureEvent;
203
+ /**
204
+ * The mode a host acts in for this trust state. A revoked root is a signed statement that the trust anchor is not
205
+ * sound, so it blocks in shadow mode too; no policy setting changes that. Every other failure follows the policy.
206
+ */
207
+ export declare function effectiveMode(mode: Mode, trust: TrustContextResult | {
208
+ ok: false;
209
+ rootRevoked?: boolean | undefined;
210
+ } | null | undefined): Mode;
211
+ /** The single decision-log record for a trust failure: empty skill and skill_dir, null digest and certificate_id. */
212
+ export declare function trustFailureRecord(failure: {
213
+ reason: string;
214
+ rootRevoked?: boolean | undefined;
215
+ }, mode: Mode, now: Date, host?: string): DecisionRecord;
72
216
  export declare function actionFor(mode: Mode, verified: boolean): DecisionRecord['action'];
73
217
  export declare function appendDecision(logPath: string | null, record: DecisionRecord): Promise<void>;
74
218
  export declare function decisionRecord(result: VerificationResult, mode: Mode, event: string, host?: string): DecisionRecord;
@@ -79,3 +223,4 @@ export declare function verifyWithPolicy(policy: Policy, skillDir: string, optio
79
223
  certificate?: string;
80
224
  context?: TrustContext;
81
225
  }): Promise<VerificationResult>;
226
+ export {};