@mago-foundation/mago-ski 0.3.0 → 0.4.1

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 (54) hide show
  1. package/README.md +34 -10
  2. package/action.yml +21 -2
  3. package/dist/hosts/github/run.d.ts +9 -0
  4. package/dist/hosts/github/run.js +30 -16
  5. package/dist/hosts/pi/index.js +40 -18
  6. package/dist/src/admin.d.ts +2 -1
  7. package/dist/src/admin.js +3 -2
  8. package/dist/src/canonical-json.d.ts +6 -0
  9. package/dist/src/canonical-json.js +11 -0
  10. package/dist/src/cli.d.ts +13 -1
  11. package/dist/src/cli.js +426 -8
  12. package/dist/src/diff.d.ts +5 -0
  13. package/dist/src/diff.js +2 -1
  14. package/dist/src/dsse.js +2 -2
  15. package/dist/src/experimental/sigstore.d.ts +6 -0
  16. package/dist/src/experimental/sigstore.js +13 -5
  17. package/dist/src/gate/adapters/claude-code-settings.js +1 -1
  18. package/dist/src/gate/adapters/claude-code.js +1 -0
  19. package/dist/src/gate/adapters/codex-names.d.ts +1 -0
  20. package/dist/src/gate/adapters/codex-names.js +56 -0
  21. package/dist/src/gate/adapters/codex-requirements.d.ts +11 -0
  22. package/dist/src/gate/adapters/codex-requirements.js +56 -29
  23. package/dist/src/gate/adapters/codex.d.ts +3 -0
  24. package/dist/src/gate/adapters/codex.js +147 -20
  25. package/dist/src/gate/admin-client.d.ts +19 -0
  26. package/dist/src/gate/admin-client.js +62 -0
  27. package/dist/src/gate/generations.js +2 -0
  28. package/dist/src/gate/instance.d.ts +57 -0
  29. package/dist/src/gate/instance.js +97 -0
  30. package/dist/src/gate/proc.d.ts +6 -0
  31. package/dist/src/gate/proc.js +14 -3
  32. package/dist/src/gate/service.d.ts +13 -0
  33. package/dist/src/gate/service.js +344 -37
  34. package/dist/src/gate/setup.d.ts +210 -0
  35. package/dist/src/gate/setup.js +1038 -0
  36. package/dist/src/gate/trust-store.d.ts +14 -0
  37. package/dist/src/gate/trust-store.js +66 -0
  38. package/dist/src/index.d.ts +2 -1
  39. package/dist/src/index.js +3 -1
  40. package/dist/src/policy.d.ts +7 -0
  41. package/dist/src/policy.js +19 -2
  42. package/dist/src/recovery-admin.js +7 -4
  43. package/dist/src/root-events.d.ts +2 -0
  44. package/dist/src/root-events.js +61 -4
  45. package/dist/src/tree-digest.d.ts +6 -0
  46. package/dist/src/tree-digest.js +8 -0
  47. package/docs/hosts/adapter-interface.md +1 -1
  48. package/docs/hosts/claude-code.md +108 -0
  49. package/docs/hosts/codex.md +181 -0
  50. package/docs/spec/gui-admin-rack-v1.md +1 -1
  51. package/docs/spec/root-recovery-v1.md +3 -2
  52. package/docs/spec/trust-documents-v1.md +1 -1
  53. package/docs/threat-model.md +13 -1
  54. package/package.json +3 -1
@@ -0,0 +1,97 @@
1
+ // One gate instance per host (design docs/design/gate-cli.md, section 1, G1, G7). This module is the single place that knows
2
+ // where an instance keeps its files, what its config file holds and how that maps to the service's own config.
3
+ import { lstat, readFile } from 'node:fs/promises';
4
+ import { createRequire } from 'node:module';
5
+ import path from 'node:path';
6
+ import { isRecord } from "../canonical-json.js";
7
+ import { validateKeyId } from "../keys.js";
8
+ import { MODES } from "./contract.js";
9
+ import { parseStrictJson } from "./strict-json.js";
10
+ export const HOSTS = ['claude-code', 'codex'];
11
+ export const isHost = (value) => typeof value === 'string' && HOSTS.includes(value);
12
+ /** Every path of one instance. `base` is `/` in production; tests pass a temporary folder. */
13
+ export function instancePaths(host, base = '/') {
14
+ const at = (...parts) => path.join(base, ...parts);
15
+ const configDir = at('etc', 'mago-ski', 'gate');
16
+ const stateRoot = at('var', 'lib', 'mago-ski', 'gate', host);
17
+ const decisionSocketDir = at('run', 'mago-ski', 'gate', host);
18
+ const dropinDir = at('etc', 'systemd', 'system', `mago-ski-gate@${host}.service.d`);
19
+ return {
20
+ configDir,
21
+ instance: path.join(configDir, `${host}.json`),
22
+ hook: path.join(configDir, `${host}-hook.json`),
23
+ manifest: path.join(configDir, `${host}.manifest.json`),
24
+ sharedManifest: path.join(configDir, 'shared.manifest.json'),
25
+ stateRoot,
26
+ stateDir: path.join(stateRoot, 'state'),
27
+ generationsDir: path.join(stateRoot, 'generations'),
28
+ decisionSocketDir,
29
+ adminSocketDir: path.join(decisionSocketDir, 'admin'),
30
+ managedDir: host === 'claude-code' ? at('etc', 'claude-code', '.claude', 'skills') : at('etc', 'codex', 'skills'),
31
+ hostConfig: host === 'claude-code' ? at('etc', 'claude-code', 'managed-settings.json') : at('etc', 'codex', 'requirements.toml'),
32
+ managedConfig: host === 'codex' ? at('etc', 'codex', 'managed_config.toml') : undefined,
33
+ unit: at('etc', 'systemd', 'system', 'mago-ski-gate@.service'),
34
+ dropinDir,
35
+ dropin: path.join(dropinDir, 'paths.conf'),
36
+ };
37
+ }
38
+ const require = createRequire(import.meta.url);
39
+ /** `mago-ski <version>` from package.json, not a constant. */
40
+ export function buildId() {
41
+ const version = require('#package.json').version; // throws when package.json cannot be read
42
+ if (typeof version !== 'string' || version === '')
43
+ throw new Error('cannot read the version from package.json');
44
+ return `mago-ski ${version}`;
45
+ }
46
+ export const missingInstanceMessage = (host) => `no gate instance for ${host}; run mago-ski gate init --host ${host}`;
47
+ const KEYS = ['v', 'host', 'mode', 'root_fingerprint', 'host_exes', 'state_dir', 'generations_dir', 'managed_dir',
48
+ 'decision_socket_dir', 'admin_socket_dir', 'inbox_dir', 'build_id'];
49
+ const abs = (v) => typeof v === 'string' && path.isAbsolute(v) && !v.includes('\0');
50
+ /** Strict validation of a parsed instance config: unknown keys, missing keys and wrong types are refused. */
51
+ export function parseInstanceConfig(raw) {
52
+ if (!isRecord(raw))
53
+ throw new Error('instance config must be an object');
54
+ const extra = Object.keys(raw).filter((k) => !KEYS.includes(k));
55
+ if (extra.length > 0)
56
+ throw new Error(`instance config has unknown keys: ${extra.join(', ')}`);
57
+ const missing = KEYS.filter((k) => !Object.hasOwn(raw, k));
58
+ if (missing.length > 0)
59
+ throw new Error(`instance config is missing keys: ${missing.join(', ')}`);
60
+ if (raw['v'] !== 1)
61
+ throw new Error('instance config v must be 1');
62
+ if (!isHost(raw['host']))
63
+ throw new Error(`instance config host must be one of ${HOSTS.join(', ')}`);
64
+ if (!MODES.includes(raw['mode']))
65
+ throw new Error('instance config mode must be shadow or enforce');
66
+ validateKeyId(raw['root_fingerprint'], 'instance config root_fingerprint');
67
+ const exes = raw['host_exes'];
68
+ if (!Array.isArray(exes) || exes.length === 0 || !exes.every(abs))
69
+ throw new Error('instance config host_exes must be a non-empty list of absolute paths');
70
+ for (const k of ['state_dir', 'generations_dir', 'managed_dir', 'decision_socket_dir', 'admin_socket_dir'])
71
+ if (!abs(raw[k]))
72
+ throw new Error(`instance config ${k} must be an absolute path`);
73
+ if (raw['inbox_dir'] !== null && !abs(raw['inbox_dir']))
74
+ throw new Error('instance config inbox_dir must be an absolute path or null');
75
+ if (typeof raw['build_id'] !== 'string' || raw['build_id'] === '')
76
+ throw new Error('instance config build_id must be a string');
77
+ return raw;
78
+ }
79
+ /** Reads the instance config. It must be a regular file owned by `rootUid` and not writable by group or others. */
80
+ export async function readInstanceConfig(file, rootUid = 0) {
81
+ const info = await lstat(file);
82
+ if (!info.isFile())
83
+ throw new Error(`${file} must be a regular file`);
84
+ if (info.uid !== rootUid || (info.mode & 0o022) !== 0)
85
+ throw new Error(`${file} must be owned by root and not writable by group or others`);
86
+ // nosemgrep: mago-no-raw-read-of-signed-input -- the instance config, checked just above as root-owned and not writable by others
87
+ return parseInstanceConfig(parseStrictJson(await readFile(file)));
88
+ }
89
+ /** The service's own config for an instance. */
90
+ export function instanceToServiceConfig(cfg, rootUid = 0) {
91
+ return {
92
+ stateDir: cfg.state_dir, generationsDir: cfg.generations_dir, managedDir: cfg.managed_dir,
93
+ decisionSocketDir: cfg.decision_socket_dir, adminSocketDir: cfg.admin_socket_dir,
94
+ rootFingerprint: cfg.root_fingerprint, mode: cfg.mode, hostExes: cfg.host_exes, rootUid, buildId: cfg.build_id,
95
+ ...(cfg.inbox_dir !== null ? { inboxDir: cfg.inbox_dir } : {}),
96
+ };
97
+ }
@@ -14,6 +14,12 @@ export declare function assertGatePlatform(platform?: NodeJS.Platform): void;
14
14
  export declare function bootId(): Promise<string>;
15
15
  /** Milliseconds since boot (CLOCK_BOOTTIME, so suspend counts and wall-clock steps do not matter). */
16
16
  export declare function bootMs(): Promise<number>;
17
+ /**
18
+ * The first field of /proc/uptime (`seconds.hundredths`) in milliseconds, with integer arithmetic. `Number("2.01") * 1000`
19
+ * is 2009.9999999999998, which floors to 2009: one millisecond before the same instant as processFacts computes it from
20
+ * clock ticks. A host that asked in the tick it started in was then refused as "started in the future".
21
+ */
22
+ export declare function parseUptimeMs(text: string): number;
17
23
  /**
18
24
  * Start time and uid of a PROCESS (a thread-group leader), without its exe. Throws if gone, unreadable, a thread id
19
25
  * (a thread starts later than its process, which would shorten a session's generation set) or a process of a nested
@@ -23,10 +23,21 @@ export async function bootId() {
23
23
  }
24
24
  /** Milliseconds since boot (CLOCK_BOOTTIME, so suspend counts and wall-clock steps do not matter). */
25
25
  export async function bootMs() {
26
- const first = (await readFile('/proc/uptime', 'utf8')).split(' ')[0] ?? '';
27
- if (!/^\d+\.\d+$/u.test(first))
26
+ return parseUptimeMs(await readFile('/proc/uptime', 'utf8'));
27
+ }
28
+ /**
29
+ * The first field of /proc/uptime (`seconds.hundredths`) in milliseconds, with integer arithmetic. `Number("2.01") * 1000`
30
+ * is 2009.9999999999998, which floors to 2009: one millisecond before the same instant as processFacts computes it from
31
+ * clock ticks. A host that asked in the tick it started in was then refused as "started in the future".
32
+ */
33
+ export function parseUptimeMs(text) {
34
+ const m = /^(\d+)\.(\d+)$/u.exec(text.split(' ')[0] ?? '');
35
+ if (!m)
36
+ throw new Error('unexpected /proc/uptime format');
37
+ const ms = Number(m[1]) * 1000 + Number(m[2].padEnd(3, '0').slice(0, 3));
38
+ if (!Number.isSafeInteger(ms))
28
39
  throw new Error('unexpected /proc/uptime format');
29
- return Math.floor(Number(first) * 1000);
40
+ return ms;
30
41
  }
31
42
  /**
32
43
  * Start time and uid of a PROCESS (a thread-group leader), without its exe. Throws if gone, unreadable, a thread id
@@ -45,6 +45,8 @@ export interface GateServiceConfig {
45
45
  inboxDir?: string;
46
46
  /** Inbox check period (default 60000 ms); only used when inboxDir is set. */
47
47
  refreshIntervalMs?: number;
48
+ /** Period of the reconcile that runs inside the admin queue whether or not an inbox is set (default 60000 ms). */
49
+ reconcileIntervalMs?: number;
48
50
  /** Decision log directory (default `<stateDir>/log`), build/profile id written into every record, retention. */
49
51
  logDir?: string;
50
52
  buildId?: string;
@@ -76,5 +78,16 @@ export interface GateService {
76
78
  };
77
79
  /** Effective decision connection cap, valid after start(). */
78
80
  readonly decisionCap: number;
81
+ /** Why the stores could not be opened at start(); undefined when they opened. The service stays up and answers trust-unavailable. */
82
+ readonly startError: string | undefined;
79
83
  }
84
+ export declare const REMOVED_NAMES_FILE = ".mago-ski-removed-names.json";
85
+ export declare const REMOVED_NAMES_TYPE = "mago-ski.gate.removed-names.v1";
86
+ export declare const REMOVED_NAMES_CAP = 4096;
87
+ /**
88
+ * The trust store of one root: `<stateDir>/trust/<64 hex of the root fingerprint>` (design gate-new-root.md N1). Each root has
89
+ * its own documents and its own rollback memory, so moving an instance to a new root never reads, changes or removes the
90
+ * old root's state, and moving back finds it as it was.
91
+ */
92
+ export declare function trustStoreDir(stateDir: string, rootFingerprint: string): string;
80
93
  export declare function createGateService(config: GateServiceConfig): GateService;