@crvouga/homelab 0.23.13 → 0.24.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 (177) hide show
  1. package/.mise/locks/npm-vercel/{68.0.0 → 68.2.0}/aube-lock.yaml +73 -297
  2. package/.mise/locks/npm-vercel/{68.0.0 → 68.2.0}/package.json +1 -1
  3. package/.mise/locks/npm-wrangler/{4.169.0 → 4.170.0}/aube-lock.yaml +42 -42
  4. package/.mise/locks/npm-wrangler/{4.169.0 → 4.170.0}/package.json +1 -1
  5. package/README.md +54 -76
  6. package/ansible/ubuntu/agent-tool.py +106 -1
  7. package/ansible/ubuntu/heavy.py +142 -0
  8. package/ansible/ubuntu/idle-reboot.py +125 -0
  9. package/ansible/ubuntu/policy.json +58 -3
  10. package/ansible/ubuntu/t3-desktop.yml +7 -0
  11. package/ansible/ubuntu/t3-hang-snapshot.sh +75 -0
  12. package/ansible/ubuntu/t3-headless.yml +7 -0
  13. package/ansible/ubuntu/t3-health.sh +71 -3
  14. package/ansible/ubuntu/t3-policy.yml +14 -5
  15. package/ansible/ubuntu/tasks/packages.yml +2 -0
  16. package/ansible/ubuntu/tasks/recovery.yml +16 -2
  17. package/ansible/ubuntu/tasks/resources.yml +25 -2
  18. package/ansible/ubuntu/tasks/unattended-upgrades.yml +24 -6
  19. package/ansible/ubuntu/tasks/updates.yml +59 -0
  20. package/ansible/ubuntu/wip-guard.py +141 -14
  21. package/bootstrap/doctor.ts +0 -4
  22. package/bootstrap/project-action.sh +4 -5
  23. package/bootstrap/shell-path.ts +17 -19
  24. package/cliproxy/README.md +5 -6
  25. package/cliproxy/config.template.yaml +1 -15
  26. package/cliproxy/docs/health-workflow.example.yml +6 -6
  27. package/cliproxy/docs/model-health.md +8 -8
  28. package/cliproxy/scripts/accounts.py +3 -4
  29. package/cliproxy/scripts/fleet_sync.py +2 -13
  30. package/cliproxy/scripts/ops +3 -21
  31. package/docs/authentication.md +21 -26
  32. package/docs/convergence.md +28 -32
  33. package/docs/declared.md +30 -31
  34. package/docs/durability.md +9 -0
  35. package/docs/github.md +0 -9
  36. package/docs/machine-setup.md +16 -17
  37. package/docs/manager-recovery.md +0 -2
  38. package/docs/operations.md +12 -11
  39. package/docs/releasing.md +18 -21
  40. package/docs/runbooks.md +22 -78
  41. package/docs/support-matrix.md +1 -1
  42. package/docs/ubuntu-sync.md +71 -65
  43. package/docs/worker-contract.md +112 -15
  44. package/examples/fleet.json +0 -1
  45. package/llms.txt +0 -2
  46. package/mise.lock +206 -780
  47. package/mise.toml +12 -27
  48. package/package.json +5 -6
  49. package/schema/check-ids.json +41 -482
  50. package/schema/fleet.schema.json +6 -57
  51. package/schema/roster.schema.json +0 -2
  52. package/src/agent-session-limit.ts +2 -7
  53. package/src/auth-checks.ts +1 -3
  54. package/src/check-owners.ts +0 -13
  55. package/src/check-registry.ts +13 -72
  56. package/src/cli/agent-guide.ts +3 -16
  57. package/src/cli/command-index.ts +0 -12
  58. package/src/cli/fleet-sync-manager.ts +4 -146
  59. package/src/cli/fleet-sync-output.ts +1 -4
  60. package/src/cli/fleet-sync-plan.ts +1 -24
  61. package/src/cli/fleet-sync-remote.ts +2 -1
  62. package/src/cli/fleet-sync-vault.ts +1 -2
  63. package/src/cli/fleet-sync.ts +8 -44
  64. package/src/cli/fleet.ts +1 -2
  65. package/src/cli/program.ts +3 -14
  66. package/src/cli/register-fleet.ts +1 -55
  67. package/src/cli/registry.ts +1 -43
  68. package/src/config.ts +0 -2
  69. package/src/definition.ts +8 -83
  70. package/src/fleet-config.ts +1 -1
  71. package/src/ide/definition.ts +2 -29
  72. package/src/ide/super.ts +4 -35
  73. package/src/ide/t3.ts +9 -38
  74. package/src/manager-sync.ts +1 -1
  75. package/src/node-converge-linux.ts +4 -47
  76. package/src/node-converge-shared.ts +1 -22
  77. package/src/{agents/toolchain.ts → pinned-path.ts} +0 -17
  78. package/src/platform/agent-oom-check.ts +104 -0
  79. package/src/platform/t3-policy.ts +23 -36
  80. package/src/platform/t3-service.ts +117 -0
  81. package/src/platform/wip-guard-check.ts +65 -0
  82. package/src/remote-entry.ts +2 -41
  83. package/src/repair-catalog.ts +0 -30
  84. package/src/schedule-check.ts +17 -4
  85. package/src/service-auth.ts +0 -50
  86. package/src/state/litestream.ts +1 -1
  87. package/src/t3/retire-providers.ts +79 -0
  88. package/src/t3/version.ts +1 -1
  89. package/src/transport.ts +2 -6
  90. package/src/types.ts +0 -2
  91. package/src/version-parity.ts +0 -8
  92. package/src/worker-access-checks.ts +0 -75
  93. package/src/worker-host-checks.ts +78 -1
  94. package/src/worker-policy.ts +0 -2
  95. package/src/worker-readiness.ts +2 -0
  96. package/src/worker-sizing.ts +25 -0
  97. package/.mise/locks/azure-cli/2.93.0~a837e014/pyproject.toml +0 -5
  98. package/.mise/locks/azure-cli/2.93.0~a837e014/uv.lock +0 -2841
  99. package/.mise/locks/gemini/0.71.1~84aac8e6/aube-lock.yaml +0 -124
  100. package/.mise/locks/gemini/0.71.1~84aac8e6/package.json +0 -12
  101. package/agents/antigravity-proxy.ts +0 -3
  102. package/agents/audit.md +0 -88
  103. package/agents/bin/agy +0 -10
  104. package/agents/bin/antigravity +0 -10
  105. package/agents/bin/claude +0 -10
  106. package/agents/bin/codex +0 -10
  107. package/agents/bin/grok +0 -10
  108. package/agents/bin/opencode +0 -10
  109. package/agents/claude-proxy +0 -5
  110. package/agents/claude-proxy.ts +0 -3
  111. package/agents/codex-proxy.ts +0 -3
  112. package/agents/grok-proxy.ts +0 -3
  113. package/agents/managed-proxy.ts +0 -109
  114. package/agents/opencode-proxy.ts +0 -3
  115. package/agents/registry.json +0 -209
  116. package/agents/registry.schema.json +0 -149
  117. package/agents/setup.md +0 -158
  118. package/agents/t3.json +0 -40
  119. package/ansible/agents/agents.yml +0 -135
  120. package/ansible/agents/commands.yml +0 -32
  121. package/ansible/agents/receipts.yml +0 -19
  122. package/ansible/agents/templates/antigravity.settings.json.j2 +0 -3
  123. package/ansible/agents/templates/claude.settings.json.j2 +0 -3
  124. package/ansible/agents/templates/codex.config.toml.j2 +0 -11
  125. package/ansible/agents/templates/gemini.settings.json.j2 +0 -14
  126. package/ansible/agents/templates/grok.config.toml.j2 +0 -25
  127. package/ansible/agents/templates/opencode.json.j2 +0 -24
  128. package/ansible/agents/templates/receipt.json.j2 +0 -10
  129. package/ansible/t3/provider-keys.yml +0 -24
  130. package/docs/accounts.md +0 -256
  131. package/src/accounts/checks.ts +0 -226
  132. package/src/accounts/context.ts +0 -77
  133. package/src/accounts/propagate.ts +0 -62
  134. package/src/accounts/roster.ts +0 -112
  135. package/src/accounts/selection.ts +0 -67
  136. package/src/accounts/sessions.ts +0 -354
  137. package/src/agents/account-relay.ts +0 -279
  138. package/src/agents/accounts.ts +0 -94
  139. package/src/agents/admission.ts +0 -65
  140. package/src/agents/audit.ts +0 -77
  141. package/src/agents/cli.ts +0 -13
  142. package/src/agents/command.ts +0 -257
  143. package/src/agents/commands.ts +0 -127
  144. package/src/agents/converge.ts +0 -339
  145. package/src/agents/doctor.ts +0 -107
  146. package/src/agents/errors.ts +0 -2
  147. package/src/agents/files.ts +0 -43
  148. package/src/agents/fleet-verify.ts +0 -195
  149. package/src/agents/launch.ts +0 -361
  150. package/src/agents/native-verify.ts +0 -738
  151. package/src/agents/provider-errors.ts +0 -57
  152. package/src/agents/registry.ts +0 -61
  153. package/src/agents/render.ts +0 -121
  154. package/src/agents/round-trip.ts +0 -192
  155. package/src/agents/secrets.ts +0 -48
  156. package/src/agents/verify-cache.ts +0 -164
  157. package/src/agents/verify.ts +0 -129
  158. package/src/cli/fleet-accounts.ts +0 -294
  159. package/src/cli/fleet-sync-accounts.ts +0 -62
  160. package/src/cli/node-deployment.ts +0 -34
  161. package/src/cli/node-sync.ts +0 -121
  162. package/src/cli/node-t3.ts +0 -35
  163. package/src/cli/t3/command.ts +0 -91
  164. package/src/cli/t3/register.ts +0 -56
  165. package/src/configure.ts +0 -271
  166. package/src/github/copilot.ts +0 -54
  167. package/src/t3/accounts.ts +0 -90
  168. package/src/t3/antigravity.ts +0 -93
  169. package/src/t3/desired.ts +0 -174
  170. package/src/t3/diagnostics.ts +0 -42
  171. package/src/t3/models.ts +0 -354
  172. package/src/t3/names-check.ts +0 -53
  173. package/src/t3/names.ts +0 -155
  174. package/src/t3/plan.ts +0 -106
  175. package/src/t3/provider-keys.ts +0 -49
  176. package/src/t3/providers.ts +0 -102
  177. package/src/t3/reconcile.ts +0 -133
package/src/t3/names.ts DELETED
@@ -1,155 +0,0 @@
1
- import { readFile } from 'node:fs/promises';
2
- import { homedir } from 'node:os';
3
- import { join } from 'node:path';
4
- import { object, read, write } from './files';
5
-
6
- /** One naming convention for every provider T3 lists, on every machine:
7
- * `<Provider> CLI Proxy` for a fleet-managed gateway instance and
8
- * `<Provider> <account email>` for a native login.
9
- */
10
- export const PROVIDER_NAMES: Record<string, string> = {
11
- claudeAgent: 'Claude',
12
- codex: 'Codex',
13
- antigravity: 'AntiGravity',
14
- grok: 'Grok',
15
- opencode: 'OpenCode',
16
- cursor: 'Cursor',
17
- };
18
-
19
- export const proxyName = (driver: string): string | undefined =>
20
- PROVIDER_NAMES[driver] && `${PROVIDER_NAMES[driver]} CLI Proxy`;
21
-
22
- /** The account email is the only identity shown; without one the provider
23
- * name stands alone rather than a guess.
24
- */
25
- export const accountName = (
26
- driver: string,
27
- email?: string
28
- ): string | undefined =>
29
- PROVIDER_NAMES[driver] &&
30
- (email ? `${PROVIDER_NAMES[driver]} ${email}` : PROVIDER_NAMES[driver]);
31
-
32
- export type Accounts = (
33
- driver: string,
34
- homePath?: string
35
- ) => Promise<string | undefined>;
36
-
37
- const EMAIL = /^[^\s@]{1,64}@[^\s@]{1,255}$/;
38
- const email = (value: unknown): string | undefined =>
39
- typeof value === 'string' && EMAIL.test(value) ? value : undefined;
40
-
41
- async function json(path: string): Promise<Record<string, unknown>> {
42
- return object(await readFile(path, 'utf8'));
43
- }
44
-
45
- /** Reads only the signed-in address from each client's own login record; a
46
- * token is never returned, logged or sent anywhere.
47
- */
48
- export const localAccounts: Accounts = async (driver, homePath) => {
49
- try {
50
- if (driver === 'claudeAgent') {
51
- const account = (await json(join(homePath ?? homedir(), '.claude.json')))[
52
- 'oauthAccount'
53
- ];
54
- return email((account as Record<string, unknown>)?.['emailAddress']);
55
- }
56
- if (driver === 'codex') {
57
- const home =
58
- homePath ?? process.env['CODEX_HOME'] ?? join(homedir(), '.codex');
59
- const tokens = (await json(join(home, 'auth.json')))['tokens'];
60
- const claims = String(
61
- (tokens as Record<string, unknown>)?.['id_token']
62
- ).split('.')[1];
63
- return email(
64
- object(Buffer.from(claims ?? '', 'base64url').toString('utf8'))['email']
65
- );
66
- }
67
- } catch {
68
- /* Signed out or unreadable: the provider name stands alone. */
69
- }
70
- return undefined;
71
- };
72
-
73
- type Instance = Record<string, unknown>;
74
- const homeOf = (instance: Instance): string | undefined => {
75
- const home = (instance['config'] as Record<string, unknown> | undefined)?.[
76
- 'homePath'
77
- ];
78
- return typeof home === 'string' ? home : undefined;
79
- };
80
-
81
- /** Every instance T3 lists gets its conventional name. Managed instances
82
- * (`managed`: ID to name) are named by their definition; every other instance is renamed after its own
83
- * signed-in account, and a signed-in default login T3 lists without a settings
84
- * entry gains one. Only `displayName` is ever written.
85
- */
86
- export async function conformNames(
87
- current: Record<string, unknown>,
88
- managed: Record<string, string>,
89
- accounts: Accounts = localAccounts
90
- ): Promise<Record<string, unknown>> {
91
- const next: Record<string, unknown> = { ...current };
92
- const named = new Set<string>();
93
- for (const [id, value] of Object.entries(current)) {
94
- // A worker's policy owns its managed instances; a copy left in its
95
- // settings still shows until the policy loads, so it conforms too.
96
- const renamed = Object.hasOwn(managed, id)
97
- ? rename(value, () => managed[id])
98
- : rename(value, accountName, await account(value, accounts));
99
- if (!renamed) continue;
100
- named.add(String(renamed['displayName']));
101
- next[id] = renamed;
102
- }
103
- // T3 lists a driver's default instance (its ID is the driver) without an entry.
104
- for (const driver of Object.keys(PROVIDER_NAMES)) {
105
- const account = await accounts(driver);
106
- const name = account && accountName(driver, account);
107
- if (!name || named.has(name) || Object.hasOwn(current, driver)) continue;
108
- next[driver] = { driver, displayName: name };
109
- }
110
- return next;
111
- }
112
-
113
- const account = (value: unknown, accounts: Accounts) =>
114
- value && typeof value === 'object'
115
- ? accounts(String((value as Instance)['driver']), homeOf(value as Instance))
116
- : undefined;
117
-
118
- /** The instance under its conventional name; undefined for an unknown driver. */
119
- function rename(
120
- value: unknown,
121
- convention: (driver: string, email?: string) => string | undefined,
122
- email?: string
123
- ): Instance | undefined {
124
- if (!value || typeof value !== 'object') return undefined;
125
- const instance = value as Instance;
126
- const name = convention(String(instance['driver']), email);
127
- if (!name) return undefined;
128
- return instance['displayName'] === name
129
- ? instance
130
- : { ...instance, displayName: name };
131
- }
132
-
133
- /** Applies the convention to this machine's T3 settings. T3 watches the file,
134
- * so names change live; nothing is restarted. Returns whether it changed.
135
- */
136
- export async function conformT3Names(
137
- settingsPath: string,
138
- managed: Record<string, string>,
139
- options: { apply: boolean; accounts?: Accounts }
140
- ): Promise<{ changed: boolean }> {
141
- const raw = await read(settingsPath);
142
- const settings = object(raw);
143
- const current = object(JSON.stringify(settings['providerInstances'] ?? {}));
144
- const next = await conformNames(current, managed, options.accounts);
145
- if (JSON.stringify(next) === JSON.stringify(current))
146
- return { changed: false };
147
- if (!options.apply) return { changed: true };
148
- // T3 also writes this file: never overwrite a change made since the read.
149
- if ((await read(settingsPath)) !== raw) throw new Error('T3_SETTINGS_BUSY');
150
- await write(
151
- settingsPath,
152
- JSON.stringify({ ...settings, providerInstances: next }, null, 2) + '\n'
153
- );
154
- return { changed: true };
155
- }
package/src/t3/plan.ts DELETED
@@ -1,106 +0,0 @@
1
- import { FleetUsageError } from '../errors';
2
- import { canonical, hash, object, read } from './files';
3
-
4
- export type Change = {
5
- id: string;
6
- action: 'create' | 'update' | 'delete' | 'unchanged' | 'conflict';
7
- };
8
- export type T3Paths = { settings: string; state: string };
9
-
10
- export async function planT3(paths: T3Paths, desired: Record<string, unknown>) {
11
- const raw = await read(paths.settings);
12
- const stateRaw = await read(paths.state);
13
- const settings = object(raw);
14
- const current = object(JSON.stringify(settings['providerInstances'] ?? {}));
15
- const owned = readOwnership(stateRaw, paths.settings);
16
- const next = { ...current };
17
- const changes: Change[] = [];
18
- for (const [id, instance] of Object.entries(desired)) {
19
- const exists = Object.hasOwn(current, id);
20
- const action = actionFor(exists, owned.includes(id), current[id], instance);
21
- changes.push({ id, action });
22
- if (action !== 'conflict') next[id] = instance;
23
- }
24
- const retired = owned.filter((id) => !Object.hasOwn(desired, id));
25
- for (const id of retired) {
26
- changes.push({
27
- id,
28
- action: Object.hasOwn(current, id) ? 'delete' : 'unchanged',
29
- });
30
- delete next[id];
31
- }
32
- const kept = withoutPreferences(settings, retired);
33
- const settingsChanged =
34
- canonical(next) !== canonical(current) || kept !== settings;
35
- const state = ownership(paths.settings, Object.keys(desired));
36
- return {
37
- raw,
38
- stateRaw,
39
- settings: settingsChanged ? { ...kept, providerInstances: next } : settings,
40
- state,
41
- settingsChanged,
42
- stateChanged: canonical(object(stateRaw)) !== canonical(state),
43
- changes,
44
- planHash: hash(
45
- canonical({ target: paths.settings, raw, stateRaw, desired })
46
- ),
47
- };
48
- }
49
-
50
- /** An instance the fleet removes takes what T3 was told to show for it
51
- * along (src/t3/models.ts writes that for an account's instance).
52
- */
53
- function withoutPreferences(
54
- settings: Record<string, unknown>,
55
- ids: string[]
56
- ): Record<string, unknown> {
57
- const preferences = settings['providerModelPreferences'];
58
- if (
59
- !preferences ||
60
- typeof preferences !== 'object' ||
61
- Array.isArray(preferences) ||
62
- !ids.some((id) => Object.hasOwn(preferences, id))
63
- )
64
- return settings;
65
- return {
66
- ...settings,
67
- providerModelPreferences: Object.fromEntries(
68
- Object.entries(preferences).filter(([id]) => !ids.includes(id))
69
- ),
70
- };
71
- }
72
-
73
- export function ownership(target: string, ids: string[]) {
74
- return { version: 1, target, instances: [...ids].sort() };
75
- }
76
-
77
- export function readOwnership(raw: string | null, target: string): string[] {
78
- if (raw === null) return [];
79
- const state = object(raw);
80
- const ids = state['instances'];
81
- if (
82
- state['version'] !== 1 ||
83
- state['target'] !== target ||
84
- !Array.isArray(ids) ||
85
- ids.some(
86
- (id) =>
87
- typeof id !== 'string' || !/^[a-zA-Z][a-zA-Z0-9_-]{0,63}$/.test(id)
88
- ) ||
89
- new Set(ids).size !== ids.length
90
- )
91
- throw new FleetUsageError(
92
- 'Invalid T3 ownership state; restore its backup before reconciliation.'
93
- );
94
- return ids as string[];
95
- }
96
-
97
- function actionFor(
98
- exists: boolean,
99
- owned: boolean,
100
- current: unknown,
101
- desired: unknown
102
- ): Change['action'] {
103
- if (exists && !owned) return 'conflict';
104
- if (!exists) return 'create';
105
- return canonical(current) === canonical(desired) ? 'unchanged' : 'update';
106
- }
@@ -1,49 +0,0 @@
1
- import { dirname, join } from 'node:path';
2
- import { proxyKey } from '../agents/secrets';
3
- import { record } from '../config';
4
- import { FleetUsageError } from '../errors';
5
- import { PACKAGE_ROOT } from '../package-info';
6
- import { runAnsible } from '../platform/ansible';
7
- import { runCommand } from '../runner';
8
- import type { Change } from './plan';
9
-
10
- /** Authored: a provider check may repeat it. It names no path and no secret. */
11
- export const PROVIDER_KEYS_UNRECONCILED =
12
- 'Could not reconcile the local T3 provider credentials.';
13
-
14
- // T3 encodes each segment of its provider-config secret name independently.
15
- function slot(id: string) {
16
- return (
17
- 'provider-config-' +
18
- [id, 'antigravity', 'apiKey']
19
- .map((value) => Buffer.from(value).toString('base64url'))
20
- .join('-') +
21
- '.bin'
22
- );
23
- }
24
-
25
- export async function reconcileProviderKeys(
26
- settings: string,
27
- instances: Record<string, unknown>,
28
- changes: Change[]
29
- ) {
30
- const active = Object.entries(instances)
31
- .filter(([, v]) => record(v) && v['driver'] === 'antigravity')
32
- .map(([id]) => slot(id));
33
- const retired = changes
34
- .filter(
35
- (c) => c.action === 'delete' && c.id.startsWith('personal_antigravity_')
36
- )
37
- .map((c) => slot(c.id));
38
- if (!active.length && !retired.length) return;
39
- const result = await runAnsible(runCommand, 'provider-keys.yml', {
40
- cwd: join(PACKAGE_ROOT, 'ansible/t3'),
41
- input: JSON.stringify({
42
- t3_keys_dir: join(dirname(settings), 'secrets'),
43
- t3_client_key: active.length ? await proxyKey() : '',
44
- t3_active_keys: active,
45
- t3_retired_keys: retired,
46
- }),
47
- });
48
- if (!result.ok) throw new FleetUsageError(PROVIDER_KEYS_UNRECONCILED);
49
- }
@@ -1,102 +0,0 @@
1
- import type { reconcileProviderKeys } from './provider-keys';
2
- import { AgentConfigurationError } from '../agents/errors';
3
- import { checkedProviders, type refreshAntigravity } from './antigravity';
4
- import { T3DiagnosticError } from './diagnostics';
5
- import { FleetUsageError } from '../errors';
6
- import type { Check } from '../types';
7
- import { desired, nodeId, paths } from './desired';
8
- import type { Roster } from './accounts';
9
- import { reconcileT3 } from './reconcile';
10
- import { stablePath, stableWorkspace } from '../platform/t3-policy';
11
-
12
- /** Every machine's T3 settings hold exactly the managed provider instances:
13
- * the fixed ones plus one per account signed in on the CLI proxy. T3 watches
14
- * its settings, so providers appear (and retired ones disappear) at once,
15
- * without a restart. Workers additionally lock the same instances through T3
16
- * administrator policy. The ownership-safe t3-reconcile engine preserves unrelated
17
- * settings and unmanaged instances; an existing unowned instance of the same
18
- * ID is reported, never overwritten.
19
- */
20
- export async function machineProviders(
21
- id: string,
22
- workspace: string,
23
- apply: boolean,
24
- options: {
25
- t3Home?: string;
26
- roster?: Roster;
27
- refresh?: typeof refreshAntigravity;
28
- keys?: typeof reconcileProviderKeys;
29
- } = {}
30
- ): Promise<Check> {
31
- const remedy = {
32
- action:
33
- 'Run homelab t3-reconcile --dry-run on this machine and review any conflicting instance; adopt it with t3-reconcile --import ID only if it should be managed.',
34
- };
35
- try {
36
- const target = paths(options, workspace);
37
- // Providers launch from the stable path of the applied run checkout, not
38
- // from this revision's own directory, which a later sync prunes.
39
- const home = apply
40
- ? await stableWorkspace(workspace)
41
- : stablePath(workspace);
42
- const instances = await desired(
43
- target.definition,
44
- home,
45
- nodeId(id),
46
- options.roster
47
- );
48
- const plan = await reconcileT3(target, instances, { apply: false });
49
- const result =
50
- apply && plan.ok
51
- ? await reconcileT3(target, instances, {
52
- apply: true,
53
- ...(options.keys ? { keys: options.keys } : {}),
54
- planHash: plan.planHash,
55
- })
56
- : plan;
57
- if (!result.ok)
58
- return {
59
- id: 't3:providers',
60
- status: 'fail',
61
- summary: 'T3_PROVIDER_CONFLICT',
62
- remedy,
63
- };
64
- if (apply)
65
- return checkedProviders(instances, options.t3Home, options.refresh);
66
- return result.changed
67
- ? {
68
- id: 't3:providers',
69
- status: 'warn',
70
- summary: 'T3_PROVIDERS_PENDING',
71
- remedy,
72
- }
73
- : {
74
- id: 't3:providers',
75
- status: 'pass',
76
- summary:
77
- 'T3 on this machine offers exactly the managed provider instances every machine gets.',
78
- };
79
- } catch (error) {
80
- if (error instanceof T3DiagnosticError) return error.check;
81
- return unverified(error, remedy);
82
- }
83
- }
84
-
85
- /** An unexpected message can carry a credential or a response. An authored
86
- * failure (a stale plan, a held lock, the credential playbook) cannot, so
87
- * the check repeats it. */
88
- function unverified(
89
- error: unknown,
90
- remedy: NonNullable<Check['remedy']>
91
- ): Check {
92
- const cause =
93
- error instanceof FleetUsageError || error instanceof AgentConfigurationError
94
- ? error.message
95
- : undefined;
96
- return {
97
- id: 't3:providers',
98
- status: 'fail',
99
- summary: 'T3_PROVIDERS_UNVERIFIED',
100
- remedy: cause ? { action: cause } : remedy,
101
- };
102
- }
@@ -1,133 +0,0 @@
1
- import { reconcileProviderKeys } from './provider-keys';
2
- import { mkdir, rm } from 'node:fs/promises';
3
- import { dirname } from 'node:path';
4
- import { FleetUsageError } from '../errors';
5
- import { canonical, object, read, safePath, write } from './files';
6
- import { ownership, planT3, readOwnership, type T3Paths } from './plan';
7
-
8
- export async function reconcileT3(
9
- paths: T3Paths,
10
- desired: Record<string, unknown>,
11
- options: {
12
- apply: boolean;
13
- planHash?: string;
14
- keys?: typeof reconcileProviderKeys;
15
- }
16
- ) {
17
- if (!options.apply) return report(await planT3(paths, desired), false);
18
- return locked(paths, async () => {
19
- const plan = await planT3(paths, desired);
20
- if (options.planHash && options.planHash !== plan.planHash)
21
- throw new FleetUsageError(
22
- 'T3 plan is stale. Run homelab t3-reconcile --dry-run again.'
23
- );
24
- if (plan.changes.some((change) => change.action === 'conflict'))
25
- throw new FleetUsageError(
26
- 'T3 instance already exists without ownership. Review it and use homelab t3-reconcile --import ID.'
27
- );
28
- await (options.keys ?? reconcileProviderKeys)(
29
- paths.settings,
30
- desired,
31
- plan.changes
32
- );
33
- if (!plan.settingsChanged && !plan.stateChanged) return report(plan, true);
34
- await persist(paths, desired, plan);
35
- const verified = await planT3(paths, desired);
36
- if (verified.settingsChanged || verified.stateChanged)
37
- throw new FleetUsageError(
38
- 'T3 changed after apply; reconciliation is incomplete. Replan and retry.'
39
- );
40
- return report(plan, true);
41
- });
42
- }
43
-
44
- export async function importT3(paths: T3Paths, id: string) {
45
- return locked(paths, async () => {
46
- const raw = await read(paths.settings);
47
- const instances = object(
48
- JSON.stringify(object(raw)['providerInstances'] ?? {})
49
- );
50
- if (!Object.hasOwn(instances, id))
51
- throw new FleetUsageError('T3 import requires an existing instance ID.');
52
- const state = await read(paths.state);
53
- const owned = readOwnership(state, paths.settings);
54
- if (!owned.includes(id)) {
55
- if (state !== null) await write(`${paths.state}.backup`, state);
56
- if ((await read(paths.settings)) !== raw)
57
- throw new FleetUsageError('T3 changed during import; retry.');
58
- await write(
59
- paths.state,
60
- canonical(ownership(paths.settings, [...owned, id])) + '\n'
61
- );
62
- }
63
- return { version: 1, ok: true, imported: id, changed: !owned.includes(id) };
64
- });
65
- }
66
-
67
- async function locked<T>(paths: T3Paths, action: () => Promise<T>): Promise<T> {
68
- await safePath(paths.state);
69
- await mkdir(dirname(paths.state), { recursive: true, mode: 0o700 });
70
- const lock = `${paths.state}.lock`;
71
- try {
72
- await mkdir(lock, { mode: 0o700 });
73
- } catch {
74
- throw new FleetUsageError(
75
- 'Another personal T3 operation holds the lock. Inspect interrupted operations before removing a stale lock.'
76
- );
77
- }
78
- try {
79
- return await action();
80
- } finally {
81
- await rm(lock, { recursive: true });
82
- }
83
- }
84
-
85
- function report(plan: Awaited<ReturnType<typeof planT3>>, applied: boolean) {
86
- return {
87
- version: 1,
88
- ok: !plan.changes.some((change) => change.action === 'conflict'),
89
- applied,
90
- changed: plan.settingsChanged || plan.stateChanged,
91
- planHash: plan.planHash,
92
- changes: plan.changes,
93
- runtime:
94
- 'T3 watches settings.json; new sessions use the reconciled instances. Active sessions are not restarted.',
95
- };
96
- }
97
-
98
- async function persist(
99
- paths: T3Paths,
100
- desired: Record<string, unknown>,
101
- plan: Awaited<ReturnType<typeof planT3>>
102
- ) {
103
- // Keep a private recovery copy. Contents, environment and secret references never enter reports.
104
- if (plan.raw !== null && plan.settingsChanged)
105
- await write(`${paths.state}.settings-backup`, plan.raw);
106
- if (plan.stateRaw !== null)
107
- await write(`${paths.state}.backup`, plan.stateRaw);
108
- if (
109
- (await read(paths.settings)) !== plan.raw ||
110
- (await read(paths.state)) !== plan.stateRaw
111
- )
112
- throw new FleetUsageError(
113
- 'T3 configuration changed during apply. Replan and retry.'
114
- );
115
- // Intent first: retry after interruption can finish creates/deletes without importing them.
116
- const previous = readOwnership(plan.stateRaw, paths.settings);
117
- await write(
118
- paths.state,
119
- canonical(
120
- ownership(paths.settings, [
121
- ...new Set([...previous, ...Object.keys(desired)]),
122
- ])
123
- ) + '\n'
124
- );
125
- if (plan.settingsChanged) {
126
- if ((await read(paths.settings)) !== plan.raw)
127
- throw new FleetUsageError(
128
- 'T3 changed during apply. Ownership intent was saved; replan and retry.'
129
- );
130
- await write(paths.settings, JSON.stringify(plan.settings, null, 2) + '\n');
131
- }
132
- await write(paths.state, canonical(plan.state) + '\n');
133
- }