@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
@@ -0,0 +1,23 @@
1
+ import { type Verdict } from '../verify.ts';
2
+ import type { GateErrorCode } from './contract.ts';
3
+ export interface VerifierRequest {
4
+ request_id: string;
5
+ skill_dir: string;
6
+ skill_name: string;
7
+ policy_path: string;
8
+ }
9
+ export interface VerifierOptions {
10
+ nodePath: string;
11
+ deadlineMs: number;
12
+ /** Tests only: run this script instead of the real worker. */
13
+ workerPathForTests?: string;
14
+ }
15
+ export type VerifierOutcome = {
16
+ kind: 'verdict';
17
+ verdict: Verdict;
18
+ digest?: string;
19
+ } | {
20
+ kind: 'gate-error';
21
+ code: GateErrorCode;
22
+ };
23
+ export declare function runVerifier(request: VerifierRequest, options: VerifierOptions): Promise<VerifierOutcome>;
@@ -0,0 +1,89 @@
1
+ // Parent side of the verifier: runs the worker as an isolated child and maps every failure to a closed
2
+ // gate-error. The child gets an empty environment (no NODE_OPTIONS, LD_PRELOAD, ...), its own process
3
+ // group (killed as a whole on deadline or overflow), and capped output.
4
+ import { spawn } from 'node:child_process';
5
+ import path from 'node:path';
6
+ import { fileURLToPath } from 'node:url';
7
+ import { isRecord } from "../canonical-json.js";
8
+ import { validateDigest } from "../names.js";
9
+ import { VERDICTS } from "../verify.js";
10
+ import { parseStrictJson } from "./strict-json.js";
11
+ const MAX_STDOUT = 16 * 1024;
12
+ const MAX_STDERR = 4 * 1024;
13
+ const here = fileURLToPath(import.meta.url);
14
+ const WORKER = path.join(path.dirname(here), `verifier-worker${path.extname(here)}`);
15
+ function parseLine(bytes, requestId) {
16
+ if (bytes.length < 2 || bytes[bytes.length - 1] !== 0x0a || bytes.subarray(0, -1).includes(0x0a))
17
+ return undefined;
18
+ try {
19
+ const raw = parseStrictJson(bytes.subarray(0, -1));
20
+ if (!isRecord(raw) || raw['v'] !== 1 || raw['request_id'] !== requestId)
21
+ return undefined;
22
+ const keys = Object.keys(raw).sort().join();
23
+ if (keys !== 'request_id,v,verdict' && keys !== 'digest,request_id,v,verdict')
24
+ return undefined;
25
+ const verdict = raw['verdict'];
26
+ if (typeof verdict !== 'string' || !VERDICTS.includes(verdict))
27
+ return undefined;
28
+ if (!Object.hasOwn(raw, 'digest'))
29
+ return verdict === 'VERIFIED' ? undefined : { kind: 'verdict', verdict: verdict };
30
+ return { kind: 'verdict', verdict: verdict, digest: validateDigest(raw['digest']) };
31
+ }
32
+ catch {
33
+ return undefined;
34
+ }
35
+ }
36
+ export function runVerifier(request, options) {
37
+ if (!path.isAbsolute(options.nodePath))
38
+ throw new Error('nodePath must be absolute');
39
+ if (!Number.isInteger(options.deadlineMs) || options.deadlineMs <= 0)
40
+ throw new Error('deadlineMs must be a positive integer');
41
+ return new Promise((resolve) => {
42
+ // Empty env on purpose: the worker needs nothing from the parent's environment.
43
+ const child = spawn(options.nodePath, ['--no-addons', options.workerPathForTests ?? WORKER], {
44
+ shell: false, cwd: '/', env: {}, detached: true, stdio: ['pipe', 'pipe', 'pipe'],
45
+ });
46
+ const out = [];
47
+ let outSize = 0, errSize = 0;
48
+ let settled = false;
49
+ const settle = (outcome) => {
50
+ if (settled)
51
+ return;
52
+ settled = true;
53
+ clearTimeout(timer);
54
+ resolve(outcome);
55
+ };
56
+ // Resolve immediately: a descendant that escaped the group may hold the pipes open, so 'close' may never fire.
57
+ const fail = (code) => {
58
+ settle({ kind: 'gate-error', code });
59
+ try {
60
+ process.kill(-child.pid, 'SIGKILL');
61
+ }
62
+ catch { /* already gone */ }
63
+ child.stdout.destroy();
64
+ child.stderr.destroy();
65
+ };
66
+ const timer = setTimeout(() => fail('verifier-timeout'), options.deadlineMs);
67
+ child.once('error', () => fail('verifier-failed'));
68
+ child.stdin.on('error', () => { });
69
+ child.stdin.end(JSON.stringify({ v: 1, ...request }));
70
+ child.stdout.on('data', (chunk) => {
71
+ if (settled)
72
+ return; // late output is discarded
73
+ outSize += chunk.length;
74
+ if (outSize > MAX_STDOUT)
75
+ return fail('verifier-failed');
76
+ out.push(chunk);
77
+ });
78
+ child.stderr.on('data', (chunk) => {
79
+ errSize += chunk.length;
80
+ if (errSize > MAX_STDERR)
81
+ fail('verifier-failed');
82
+ });
83
+ // The deadline timer stays armed through 'exit' and is only cleared by settle().
84
+ child.once('close', (code, signal) => {
85
+ const parsed = code === 0 && signal === null ? parseLine(Buffer.concat(out), request.request_id) : undefined;
86
+ settle(parsed ?? { kind: 'gate-error', code: 'verifier-failed' });
87
+ });
88
+ });
89
+ }
@@ -0,0 +1,80 @@
1
+ import type { Mode } from '../policy.ts';
2
+ import { type GateRequest, type GateResult } from './contract.ts';
3
+ import { processFacts, processInfo } from './proc.ts';
4
+ export interface GateServiceConfig {
5
+ /** Holds trust/, admission/, certs/ and staging/. */
6
+ stateDir: string;
7
+ generationsDir: string;
8
+ /** Host-visible Skill folder (root-owned, not writable by employees): `<managedDir>/<name>` links to the current generation. */
9
+ managedDir: string;
10
+ /** Directory of the decision socket (`decision.sock`, mode 0666) and of the admin socket (`admin.sock`, 0600). */
11
+ decisionSocketDir: string;
12
+ adminSocketDir: string;
13
+ rootFingerprint: string;
14
+ mode: Mode;
15
+ /** Absolute paths of the real host binaries (claude, codex); the host process's /proc/<pid>/exe must match exactly. */
16
+ hostExes: readonly string[];
17
+ /** Directories the service creates and checks must be owned by this uid: 0 in production, the test user's uid in tests. */
18
+ rootUid: number;
19
+ maxListAgeMs?: number;
20
+ /** Tests: injectable wall clock for trust and certificate checks. */
21
+ now?: () => Date;
22
+ /** Idle limit for a request line: decision socket default 300 ms, admin socket default 2000 ms. */
23
+ deadlineMs?: number;
24
+ /** Decision socket connection cap (default 1024, lowered to the fd limit); the admin socket allows 4. */
25
+ maxConnections?: number;
26
+ /** Deadline for processing a complete request (default 5000 ms; admin socket 60000 ms). */
27
+ processingMs?: number;
28
+ /** Minimum time between full /proc sweeps (default 1000 ms). */
29
+ sweepMinIntervalMs?: number;
30
+ /** Tests: replaces the RLIMIT_NOFILE soft limit read from /proc/self/limits. */
31
+ nofileLimit?: number;
32
+ /** Tests: replaces the /proc reader used by sweeps. */
33
+ processFacts?: typeof processFacts;
34
+ /** Open live sessions allowed per uid (default 256). */
35
+ maxSessionsPerUid?: number;
36
+ /** Decision requests in flight per uid (default 8). */
37
+ maxInflightPerUid?: number;
38
+ /** Decision requests per second per uid, burst 3x (default 10); see RATE LIMIT. */
39
+ maxRequestsPerSecPerUid?: number;
40
+ /** Tests: replaces the /proc reader for a request's session pid. */
41
+ processInfo?: typeof processInfo;
42
+ /** Tests: replaces the reader of net.core.somaxconn (default /proc/sys/net/core/somaxconn; unreadable = no limit). */
43
+ somaxconn?: () => Promise<number>;
44
+ /** Root-only directory the customer's distribution drops `trust-root.json` and `revocations.json` into (see refresh.ts). */
45
+ inboxDir?: string;
46
+ /** Inbox check period (default 60000 ms); only used when inboxDir is set. */
47
+ refreshIntervalMs?: number;
48
+ /** Decision log directory (default `<stateDir>/log`), build/profile id written into every record, retention. */
49
+ logDir?: string;
50
+ buildId?: string;
51
+ logMaxSegments?: number;
52
+ logMaxSegmentBytes?: number;
53
+ /** Tests: return true to make the next log append fail. */
54
+ logFault?: () => boolean;
55
+ /** Tests: fault hook passed to the admission store (see admission.ts). */
56
+ admissionFault?: (op: 'read' | 'write') => string | undefined;
57
+ }
58
+ export type AdminResult = {
59
+ ok: true;
60
+ [key: string]: unknown;
61
+ } | {
62
+ ok: false;
63
+ error: string;
64
+ };
65
+ export interface GateService {
66
+ start(): Promise<void>;
67
+ stop(): Promise<void>;
68
+ handleDecision(request: GateRequest, ctx?: {
69
+ cancelled: boolean;
70
+ }): Promise<GateResult>;
71
+ handleAdmin(command: unknown): Promise<AdminResult>;
72
+ /** Socket paths, valid after start(). */
73
+ readonly sockets: {
74
+ decision: string;
75
+ admin: string;
76
+ };
77
+ /** Effective decision connection cap, valid after start(). */
78
+ readonly decisionCap: number;
79
+ }
80
+ export declare function createGateService(config: GateServiceConfig): GateService;