@ours.network/fleet 1.2.0-nightly.5 → 1.2.0-nightly.51
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/README.md +263 -53
- package/dist/account-origin.d.ts +2 -0
- package/dist/account-origin.js +6 -0
- package/dist/agent-ours/bridge.js +9 -0
- package/dist/agent-ours/correspondence.d.ts +10 -0
- package/dist/agent-ours/correspondence.js +1 -0
- package/dist/agent-ours/harness.js +8 -0
- package/dist/agent-ours/runtime.d.ts +6 -0
- package/dist/agent-ours/runtime.js +36 -0
- package/dist/agent-ours/service.js +33 -13
- package/dist/application/codex-models.d.ts +15 -0
- package/dist/application/codex-models.js +56 -0
- package/dist/application/conversation-history.d.ts +25 -0
- package/dist/application/conversation-history.js +189 -0
- package/dist/application/fleet-query-service.d.ts +1 -1
- package/dist/application/fleet-query-service.js +2 -2
- package/dist/application/legacy-supervisor-identity.d.ts +7 -0
- package/dist/application/legacy-supervisor-identity.js +43 -0
- package/dist/application/model-catalog.d.ts +1 -1
- package/dist/application/model-catalog.js +5 -2
- package/dist/application/preset-provenance.d.ts +19 -0
- package/dist/application/preset-provenance.js +59 -0
- package/dist/application/role-creation-service.js +1 -1
- package/dist/application/role-removal-service.js +2 -0
- package/dist/application/role-repository.js +2 -2
- package/dist/application/room-layout-definitions.d.ts +45 -0
- package/dist/application/room-layout-definitions.js +130 -0
- package/dist/application/session-control.d.ts +30 -0
- package/dist/application/session-control.js +72 -3
- package/dist/application/supervisor-ours-tools.d.ts +159 -0
- package/dist/application/supervisor-ours-tools.js +113 -0
- package/dist/application/task-layouts.d.ts +91 -0
- package/dist/application/task-layouts.js +249 -0
- package/dist/application/task-room-service.d.ts +99 -3
- package/dist/application/task-room-service.js +296 -31
- package/dist/atomic-file.d.ts +5 -0
- package/dist/atomic-file.js +62 -1
- package/dist/briefing.d.ts +2 -0
- package/dist/briefing.js +8 -1
- package/dist/build-info.json +7 -4
- package/dist/capabilities.d.ts +6 -1
- package/dist/capabilities.js +8 -0
- package/dist/cli.js +376 -34
- package/dist/client-profile.d.ts +5 -17
- package/dist/client-profile.js +4 -127
- package/dist/config.d.ts +28 -0
- package/dist/config.js +41 -6
- package/dist/creation.js +2 -2
- package/dist/daemon-recovery.d.ts +1 -8
- package/dist/daemon-recovery.js +8 -113
- package/dist/docs.d.ts +3 -3
- package/dist/docs.js +126 -22
- package/dist/doctor.d.ts +1 -1
- package/dist/doctor.js +55 -60
- package/dist/erased-resources.d.ts +9 -0
- package/dist/erased-resources.js +87 -0
- package/dist/fleet-command-audit.d.ts +7 -0
- package/dist/fleet-command-audit.js +26 -9
- package/dist/harness/claude-code.js +9 -1
- package/dist/harness/claude-runtime.d.ts +12 -0
- package/dist/harness/claude-runtime.js +43 -0
- package/dist/harness/codex-app-server-proxy.d.ts +4 -1
- package/dist/harness/codex-app-server-proxy.js +41 -5
- package/dist/harness/codex-reasoning.d.ts +5 -0
- package/dist/harness/codex-reasoning.js +12 -0
- package/dist/harness/codex-runtime.d.ts +11 -1
- package/dist/harness/codex-runtime.js +31 -4
- package/dist/harness/codex.d.ts +3 -2
- package/dist/harness/codex.js +20 -11
- package/dist/init-wizard.d.ts +2 -0
- package/dist/init-wizard.js +38 -19
- package/dist/isolation/policy.js +2 -0
- package/dist/isolation/types.d.ts +5 -0
- package/dist/managed-cli.d.ts +253 -0
- package/dist/managed-cli.js +923 -0
- package/dist/minimal-setup.d.ts +2 -0
- package/dist/minimal-setup.js +44 -0
- package/dist/monitor.d.ts +1 -34
- package/dist/monitor.js +24 -151
- package/dist/notifications/fleet-producer.d.ts +19 -0
- package/dist/notifications/fleet-producer.js +208 -0
- package/dist/notifications/outbox.d.ts +46 -0
- package/dist/notifications/outbox.js +158 -0
- package/dist/notifications/target-cleanup.d.ts +8 -0
- package/dist/notifications/target-cleanup.js +41 -0
- package/dist/ops.js +5 -1
- package/dist/owner-channel/channel.d.ts +3 -0
- package/dist/owner-channel/channel.js +34 -6
- package/dist/owner-channel/commands.d.ts +4 -0
- package/dist/owner-channel/commands.js +8 -0
- package/dist/owner-channel/lifecycle-outbox.js +4 -3
- package/dist/owner-channel/ours-client.js +2 -17
- package/dist/paths.d.ts +2 -0
- package/dist/paths.js +2 -0
- package/dist/preset-migration.js +25 -0
- package/dist/resolved-plan.js +1 -0
- package/dist/rooms-tasks/add-member.d.ts +48 -0
- package/dist/rooms-tasks/add-member.js +83 -0
- package/dist/rooms-tasks/archived-absence.d.ts +8 -0
- package/dist/rooms-tasks/archived-absence.js +48 -0
- package/dist/rooms-tasks/cli.js +34 -5
- package/dist/rooms-tasks/close.d.ts +5 -2
- package/dist/rooms-tasks/close.js +208 -28
- package/dist/rooms-tasks/config.d.ts +1 -1
- package/dist/rooms-tasks/config.js +11 -4
- package/dist/rooms-tasks/cowork-adapter.d.ts +6 -4
- package/dist/rooms-tasks/cowork-adapter.js +8 -121
- package/dist/rooms-tasks/deletion.d.ts +1 -1
- package/dist/rooms-tasks/deletion.js +74 -5
- package/dist/rooms-tasks/erasure.d.ts +6 -0
- package/dist/rooms-tasks/erasure.js +169 -0
- package/dist/rooms-tasks/external-worker.d.ts +1 -1
- package/dist/rooms-tasks/external-worker.js +3 -4
- package/dist/rooms-tasks/layout-binding-client.d.ts +11 -0
- package/dist/rooms-tasks/layout-binding-client.js +56 -0
- package/dist/rooms-tasks/layout-binding-grants.d.ts +21 -0
- package/dist/rooms-tasks/layout-binding-grants.js +128 -0
- package/dist/rooms-tasks/layout-cli.d.ts +4 -0
- package/dist/rooms-tasks/layout-cli.js +107 -0
- package/dist/rooms-tasks/layout-config.d.ts +11 -0
- package/dist/rooms-tasks/layout-config.js +89 -0
- package/dist/rooms-tasks/layout-control.d.ts +21 -0
- package/dist/rooms-tasks/layout-control.js +52 -0
- package/dist/rooms-tasks/layout-service.d.ts +42 -0
- package/dist/rooms-tasks/layout-service.js +185 -0
- package/dist/rooms-tasks/layout.d.ts +114 -0
- package/dist/rooms-tasks/layout.js +250 -0
- package/dist/rooms-tasks/live-readiness.d.ts +18 -0
- package/dist/rooms-tasks/live-readiness.js +85 -0
- package/dist/rooms-tasks/member-startup.d.ts +1 -0
- package/dist/rooms-tasks/member-startup.js +2 -0
- package/dist/rooms-tasks/provision.d.ts +14 -0
- package/dist/rooms-tasks/provision.js +173 -46
- package/dist/rooms-tasks/room-state.d.ts +4 -2
- package/dist/rooms-tasks/room-state.js +53 -8
- package/dist/rooms-tasks/task-state.d.ts +42 -7
- package/dist/rooms-tasks/task-state.js +153 -23
- package/dist/rooms-tasks/types.d.ts +28 -0
- package/dist/rooms-tasks/workspace-artifacts.d.ts +6 -0
- package/dist/rooms-tasks/workspace-artifacts.js +93 -0
- package/dist/rooms-tasks/workspace.d.ts +26 -0
- package/dist/rooms-tasks/workspace.js +195 -0
- package/dist/runner.d.ts +19 -3
- package/dist/runner.js +128 -45
- package/dist/session/acp.d.ts +9 -1
- package/dist/session/acp.js +31 -6
- package/dist/session/arbiter.d.ts +5 -0
- package/dist/session/arbiter.js +28 -2
- package/dist/session/codex-app-server.d.ts +5 -0
- package/dist/session/codex-app-server.js +36 -3
- package/dist/session/control.d.ts +12 -1
- package/dist/session/control.js +68 -3
- package/dist/session/conversation-normalizer.d.ts +2 -1
- package/dist/session/conversation-normalizer.js +1 -1
- package/dist/session/conversation-store.d.ts +1 -1
- package/dist/session/conversation-store.js +7 -11
- package/dist/session/conversation-types.d.ts +7 -3
- package/dist/session/task-notice.d.ts +14 -0
- package/dist/session/task-notice.js +13 -0
- package/dist/session/types.d.ts +6 -0
- package/dist/setup-tunnel-preflight.d.ts +34 -0
- package/dist/setup-tunnel-preflight.js +62 -0
- package/dist/socket-path.d.ts +2 -0
- package/dist/socket-path.js +22 -0
- package/dist/spawn.js +8 -1
- package/dist/startup-readiness.d.ts +13 -0
- package/dist/startup-readiness.js +38 -0
- package/dist/subscriptions/cli.d.ts +91 -0
- package/dist/subscriptions/cli.js +223 -0
- package/dist/subscriptions/launch.d.ts +37 -0
- package/dist/subscriptions/launch.js +93 -0
- package/dist/subscriptions/login.d.ts +75 -0
- package/dist/subscriptions/login.js +182 -0
- package/dist/subscriptions/observed.d.ts +7 -0
- package/dist/subscriptions/observed.js +44 -0
- package/dist/subscriptions/probe.d.ts +10 -0
- package/dist/subscriptions/probe.js +73 -0
- package/dist/subscriptions/service.d.ts +102 -0
- package/dist/subscriptions/service.js +364 -0
- package/dist/subscriptions/store.d.ts +71 -0
- package/dist/subscriptions/store.js +140 -0
- package/dist/supervisor/launchd.js +2 -1
- package/dist/supervisor/systemd.js +9 -23
- package/dist/temp-idle.d.ts +17 -0
- package/dist/temp-idle.js +42 -0
- package/dist/temp-lifecycle.d.ts +6 -1
- package/dist/temp-lifecycle.js +29 -13
- package/dist/tunnel-setup-completion.d.ts +7 -0
- package/dist/tunnel-setup-completion.js +13 -0
- package/dist/tunnel-setup-input.d.ts +19 -0
- package/dist/tunnel-setup-input.js +124 -0
- package/dist/web/agent-attachments.d.ts +29 -0
- package/dist/web/agent-attachments.js +162 -0
- package/dist/web/auth.d.ts +29 -1
- package/dist/web/auth.js +82 -7
- package/dist/web/available-port.d.ts +2 -0
- package/dist/web/available-port.js +23 -0
- package/dist/web/control.d.ts +3 -3
- package/dist/web/control.js +10 -8
- package/dist/web/fleet-config-service.d.ts +4 -1
- package/dist/web/fleet-config-service.js +46 -15
- package/dist/web/prefix-gateway.d.ts +32 -0
- package/dist/web/prefix-gateway.js +234 -0
- package/dist/web/runtime.d.ts +3 -1
- package/dist/web/runtime.js +116 -31
- package/dist/web/server.d.ts +16 -0
- package/dist/web/server.js +592 -53
- package/dist/web/service.d.ts +11 -2
- package/dist/web/service.js +30 -4
- package/dist/web/workspace-devices.d.ts +36 -0
- package/dist/web/workspace-devices.js +101 -0
- package/dist/web/workspace-tunnel.d.ts +1 -0
- package/dist/web/workspace-tunnel.js +34 -0
- package/dist/workspace-enrollment.d.ts +82 -0
- package/dist/workspace-enrollment.js +566 -0
- package/dist/workspace-replacement.d.ts +66 -0
- package/dist/workspace-replacement.js +220 -0
- package/docs/live-voice.md +13 -0
- package/docs/room-layouts.md +227 -0
- package/docs/task-workspaces.md +130 -0
- package/docs/validation/managed-cli-permissions.md +421 -0
- package/examples/room-layouts/fresh.yaml +27 -0
- package/examples/room-layouts/scoped.yaml +21 -0
- package/examples/room-layouts/shared.yaml +19 -0
- package/package.json +15 -18
- package/presets/README.md +78 -0
- package/presets/brain-catalog.json +8 -2
- package/presets/fleet/agent_templates/Critic.yaml +1 -1
- package/presets/fleet/agent_templates/Developer.yaml +1 -1
- package/presets/fleet/agent_templates/Engineer.yaml +5 -0
- package/presets/fleet/agent_templates/LocalCoordinator.yaml +1 -1
- package/presets/fleet/agents/FleetCoordinator.yaml +1 -1
- package/presets/fleet/brains/claude-fable-5-1-high.yaml +5 -0
- package/presets/fleet/brains/claude-fable-5-1-low.yaml +5 -0
- package/presets/fleet/brains/claude-fable-5-1-max.yaml +5 -0
- package/presets/fleet/brains/claude-fable-5-1-medium.yaml +5 -0
- package/presets/fleet/brains/claude-fable-5-1-xhigh.yaml +5 -0
- package/presets/fleet/brains/claude-fable-5-high.yaml +1 -1
- package/presets/fleet/brains/claude-fable-5-low.yaml +1 -1
- package/presets/fleet/brains/claude-fable-5-max.yaml +1 -1
- package/presets/fleet/brains/claude-fable-5-medium.yaml +1 -1
- package/presets/fleet/brains/claude-fable-5-xhigh.yaml +1 -1
- package/presets/fleet/brains/claude-haiku-4-5-20251001-default.yaml +4 -0
- package/presets/fleet/brains/claude-opus-5-5-high.yaml +5 -0
- package/presets/fleet/brains/claude-opus-5-5-low.yaml +5 -0
- package/presets/fleet/brains/claude-opus-5-5-max.yaml +5 -0
- package/presets/fleet/brains/claude-opus-5-5-medium.yaml +5 -0
- package/presets/fleet/brains/claude-opus-5-5-xhigh.yaml +5 -0
- package/presets/fleet/brains/claude-opus-5-high.yaml +1 -1
- package/presets/fleet/brains/claude-opus-5-low.yaml +1 -1
- package/presets/fleet/brains/claude-opus-5-max.yaml +1 -1
- package/presets/fleet/brains/claude-opus-5-medium.yaml +1 -1
- package/presets/fleet/brains/claude-opus-5-xhigh.yaml +1 -1
- package/presets/fleet/brains/claude-sonnet-5-high.yaml +1 -1
- package/presets/fleet/brains/claude-sonnet-5-low.yaml +1 -1
- package/presets/fleet/brains/claude-sonnet-5-max.yaml +1 -1
- package/presets/fleet/brains/claude-sonnet-5-medium.yaml +1 -1
- package/presets/fleet/brains/claude-sonnet-5-xhigh.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-3-codex-spark-high.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-3-codex-spark-low.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-3-codex-spark-medium.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-3-codex-spark-xhigh.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-4-high.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-4-low.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-4-medium.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-4-mini-high.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-4-mini-low.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-4-mini-medium.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-4-mini-xhigh.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-4-xhigh.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-5-high.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-5-low.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-5-medium.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-5-xhigh.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-luna-high.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-luna-low.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-luna-max.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-luna-medium.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-luna-xhigh.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-sol-high.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-sol-low.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-sol-max.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-sol-medium.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-sol-ultra.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-sol-xhigh.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-terra-high.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-terra-low.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-terra-max.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-terra-medium.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-terra-ultra.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-5-6-terra-xhigh.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-6-1-sol-high.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-1-sol-low.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-1-sol-max.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-1-sol-medium.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-1-sol-ultra.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-1-sol-xhigh.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-astra-high.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-6-astra-low.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-6-astra-max.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-6-astra-medium.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-6-astra-ultra.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-6-astra-xhigh.yaml +1 -1
- package/presets/fleet/brains/codex-gpt-6-luna-high.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-luna-low.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-luna-max.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-luna-medium.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-luna-xhigh.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-sol-high.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-sol-low.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-sol-max.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-sol-medium.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-sol-ultra.yaml +5 -0
- package/presets/fleet/brains/codex-gpt-6-sol-xhigh.yaml +5 -0
- package/presets/fleet/roles/Coordinator.yaml +34 -11
- package/presets/fleet/roles/Engineer.yaml +40 -0
- package/presets/fleet/room_layouts/engineering.yaml +25 -0
- package/presets/fleet/room_layouts/pair.yaml +24 -0
- package/presets/fleet/room_layouts/single.yaml +17 -0
- package/presets/fleet/room_layouts/team.yaml +30 -0
- package/presets/fleet/room_templates/engineering.yaml +19 -0
- package/presets/manifest.json +2 -2
- package/dist/web-app/assets/index-59GF-gsZ.css +0 -1
- package/dist/web-app/assets/index-C0h3ALvs.js +0 -10
- package/dist/web-app/icons/ours-fleet-maskable.svg +0 -4
- package/dist/web-app/icons/ours-fleet.svg +0 -4
- package/dist/web-app/index.html +0 -17
- package/dist/web-app/manifest.webmanifest +0 -15
- package/dist/web-app/offline.html +0 -18
- package/dist/web-app/sw.js +0 -51
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { FleetError } from '../application/errors.js';
|
|
4
|
+
import { replaceFileAtomically } from '../atomic-file.js';
|
|
5
|
+
import { claudeAuthStatus, claudeLoginExpiry, codexUsageWindows, defaultBinaries, profileEnv, withCodex, } from './cli.js';
|
|
6
|
+
import { HOME_ENV } from './launch.js';
|
|
7
|
+
import { ClaudeLogin, CodexDeviceLogin } from './login.js';
|
|
8
|
+
import { recordClaudeRateLimit } from './observed.js';
|
|
9
|
+
import { probeClaudeRateLimit } from './probe.js';
|
|
10
|
+
import { DEFAULT_PROFILE_ID, OBSERVED_USAGE_FILE, SUBSCRIPTION_PROVIDERS, createProfileHome, profileHome, readPin, readSubscriptionState, removeProfileHome, subscriptionsRoot, updateSubscriptionState, } from './store.js';
|
|
11
|
+
const EXPIRING_MS = 3 * 24 * 3600_000;
|
|
12
|
+
const STALE_CHECK_MS = 10 * 60_000;
|
|
13
|
+
export class SubscriptionService {
|
|
14
|
+
options;
|
|
15
|
+
bins;
|
|
16
|
+
now;
|
|
17
|
+
checks = new Map();
|
|
18
|
+
running = new Map();
|
|
19
|
+
logins = new Map();
|
|
20
|
+
probes = new Map();
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.options = options;
|
|
23
|
+
this.bins = options.binaries ?? defaultBinaries();
|
|
24
|
+
this.now = options.now ?? Date.now;
|
|
25
|
+
}
|
|
26
|
+
async list() {
|
|
27
|
+
const state = readSubscriptionState();
|
|
28
|
+
const pins = await this.pins();
|
|
29
|
+
return SUBSCRIPTION_PROVIDERS.map(provider => {
|
|
30
|
+
const p = state.providers[provider];
|
|
31
|
+
const profiles = p.profiles.map(profile => {
|
|
32
|
+
const key = `${provider}/${profile.id}`;
|
|
33
|
+
const cached = this.checks.get(key);
|
|
34
|
+
if (!cached || this.now() - Date.parse(cached.health.checkedAt ?? '0') > STALE_CHECK_MS)
|
|
35
|
+
void this.check(provider, profile.id).catch(() => { });
|
|
36
|
+
const observed = provider === 'claude' ? this.observedUsage(pins, profile.id) : undefined;
|
|
37
|
+
const probed = provider === 'claude' ? this.probedUsage(profile.id) : undefined;
|
|
38
|
+
const usage = observed && probed
|
|
39
|
+
? (observed.observedAt ?? '') >= (probed.observedAt ?? '') ? observed : probed
|
|
40
|
+
: observed ?? probed;
|
|
41
|
+
return {
|
|
42
|
+
id: profile.id, label: profile.label, active: profile.id === p.activeProfileId,
|
|
43
|
+
account: cached?.account,
|
|
44
|
+
health: cached?.health ?? { state: 'unknown' },
|
|
45
|
+
usage: usage ?? cached?.usage ?? { windows: [], source: 'none' },
|
|
46
|
+
agents: pins.filter(x => x.running && x.provider === provider && x.profileId === profile.id).map(x => x.roleId),
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
return {
|
|
50
|
+
provider, activeProfileId: p.activeProfileId, profiles,
|
|
51
|
+
staleAgents: pins.filter(x => x.running && x.provider === provider && x.profileId !== p.activeProfileId)
|
|
52
|
+
.map(x => ({ roleId: x.roleId, profileId: x.profileId })),
|
|
53
|
+
unmanagedAgents: pins.filter(x => x.running && x.provider === provider && x.unmanaged?.length)
|
|
54
|
+
.map(x => ({ roleId: x.roleId, via: x.unmanaged })),
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/** Health + account + (Codex) usage for one profile. Concurrent calls share one run. */
|
|
59
|
+
check(provider, profileId) {
|
|
60
|
+
this.profile(provider, profileId);
|
|
61
|
+
const key = `${provider}/${profileId}`;
|
|
62
|
+
const inflight = this.running.get(key);
|
|
63
|
+
if (inflight)
|
|
64
|
+
return inflight;
|
|
65
|
+
const run = (provider === 'claude' ? this.checkClaude(profileId) : this.checkCodex(profileId))
|
|
66
|
+
.catch((error) => ({
|
|
67
|
+
health: { state: 'error', checkedAt: new Date(this.now()).toISOString(), detail: error?.message?.includes('prerequisite') ? 'subscription state unreadable' : 'health check failed' },
|
|
68
|
+
}))
|
|
69
|
+
.then(result => { this.checks.set(key, result); this.options.onChange?.(); return result; })
|
|
70
|
+
.finally(() => this.running.delete(key));
|
|
71
|
+
this.running.set(key, run);
|
|
72
|
+
return run;
|
|
73
|
+
}
|
|
74
|
+
async checkClaude(profileId) {
|
|
75
|
+
const home = profileHome('claude', profileId);
|
|
76
|
+
const status = await claudeAuthStatus(this.bins.claude, profileEnv(HOME_ENV.claude, profileId, home));
|
|
77
|
+
const checkedAt = new Date(this.now()).toISOString();
|
|
78
|
+
const { refreshExpiresAt } = claudeLoginExpiry(home);
|
|
79
|
+
const loginExpiresAt = refreshExpiresAt ? new Date(refreshExpiresAt).toISOString() : undefined;
|
|
80
|
+
let state = status.loggedIn ? 'ok' : 'signed_out';
|
|
81
|
+
// The CLI reports the credential it would actually use (env, settings, helper…).
|
|
82
|
+
if (status.loggedIn && status.authMethod && status.authMethod !== 'claude.ai') {
|
|
83
|
+
return {
|
|
84
|
+
health: { state: 'error', checkedAt, detail: `not using a Claude subscription login (${status.authMethod.slice(0, 40)})` },
|
|
85
|
+
account: { email: status.email, plan: status.authMethod, org: status.org },
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (status.loggedIn && refreshExpiresAt !== undefined) {
|
|
89
|
+
if (refreshExpiresAt <= this.now())
|
|
90
|
+
state = 'expired';
|
|
91
|
+
else if (refreshExpiresAt - this.now() < EXPIRING_MS)
|
|
92
|
+
state = 'expiring';
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
health: { state, checkedAt, loginExpiresAt },
|
|
96
|
+
account: status.loggedIn ? { email: status.email, plan: status.plan, org: status.org } : undefined,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
async checkCodex(profileId) {
|
|
100
|
+
const home = profileHome('codex', profileId);
|
|
101
|
+
return withCodex(this.bins.codex, profileEnv(HOME_ENV.codex, profileId, home), async (server) => {
|
|
102
|
+
const checkedAt = new Date(this.now()).toISOString();
|
|
103
|
+
// refreshToken:false — a health check must not rotate a token agents use.
|
|
104
|
+
const read = await server.call('account/read', { refreshToken: false });
|
|
105
|
+
if (!read.account)
|
|
106
|
+
return { health: { state: 'signed_out', checkedAt } };
|
|
107
|
+
if (read.account.type !== 'chatgpt') {
|
|
108
|
+
return {
|
|
109
|
+
health: { state: 'error', checkedAt, detail: 'not a ChatGPT subscription login' },
|
|
110
|
+
account: { plan: read.account.type === 'apiKey' ? 'API key' : read.account.type },
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const account = { email: read.account.email ?? undefined, plan: read.account.planType ?? 'chatgpt' };
|
|
114
|
+
try {
|
|
115
|
+
const limits = await server.call('account/rateLimits/read', {});
|
|
116
|
+
return {
|
|
117
|
+
health: { state: 'ok', checkedAt }, account,
|
|
118
|
+
usage: { windows: codexUsageWindows(limits), observedAt: checkedAt, source: 'pull' },
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
const expired = /401|unauthori[sz]ed|expired|sign in again|log in again/i.test(String(error.message));
|
|
123
|
+
return { health: { state: expired ? 'expired' : 'error', checkedAt, detail: expired ? 'login expired: sign in again' : 'usage limits unavailable' }, account };
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
async startLogin(provider, owner) {
|
|
128
|
+
for (const [id, login] of this.logins) {
|
|
129
|
+
const view = login.view();
|
|
130
|
+
if (view.provider !== provider)
|
|
131
|
+
continue;
|
|
132
|
+
if (view.state === 'starting' || view.state === 'awaiting_user' || view.state === 'verifying')
|
|
133
|
+
login.cancel();
|
|
134
|
+
this.logins.delete(id);
|
|
135
|
+
}
|
|
136
|
+
const { id: profileId, home } = createProfileHome(provider);
|
|
137
|
+
const login = provider === 'claude'
|
|
138
|
+
? new ClaudeLogin(owner, this.bins, home, profileId)
|
|
139
|
+
: new CodexDeviceLogin(owner, this.bins, home, profileId);
|
|
140
|
+
const loginId = login.view().loginId;
|
|
141
|
+
this.logins.set(loginId, login);
|
|
142
|
+
void this.completeLogin(provider, profileId, login);
|
|
143
|
+
// Give the CLI a moment to print its URL so the first response is useful.
|
|
144
|
+
const deadline = this.now() + 8_000;
|
|
145
|
+
while (login.view().state === 'starting' && this.now() < deadline)
|
|
146
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
147
|
+
return login.view();
|
|
148
|
+
}
|
|
149
|
+
async completeLogin(provider, profileId, login) {
|
|
150
|
+
let label;
|
|
151
|
+
let email;
|
|
152
|
+
let error = 'sign-in did not complete';
|
|
153
|
+
try {
|
|
154
|
+
if (login instanceof ClaudeLogin) {
|
|
155
|
+
const code = await login.exited;
|
|
156
|
+
if (code === 0 && login.view().state === 'verifying') {
|
|
157
|
+
const status = await claudeAuthStatus(this.bins.claude, profileEnv(HOME_ENV.claude, profileId, profileHome('claude', profileId)));
|
|
158
|
+
if (status.loggedIn) {
|
|
159
|
+
email = status.email;
|
|
160
|
+
label = status.email ?? 'Claude account';
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else if (login.view().state === 'verifying')
|
|
164
|
+
error = 'the sign-in code was not accepted';
|
|
165
|
+
}
|
|
166
|
+
else if (await login.completed) {
|
|
167
|
+
const result = await this.checkCodex(profileId);
|
|
168
|
+
// Only a verified ChatGPT subscription login becomes a profile.
|
|
169
|
+
if (result.health.state === 'ok') {
|
|
170
|
+
email = result.account?.email;
|
|
171
|
+
label = email ?? 'Codex account';
|
|
172
|
+
this.checks.set(`${provider}/${profileId}`, result);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (label !== undefined) {
|
|
176
|
+
if (!email?.trim()) {
|
|
177
|
+
label = undefined;
|
|
178
|
+
error = 'the CLI did not provide an account email';
|
|
179
|
+
}
|
|
180
|
+
else if (await this.accountAlreadyAdded(provider, email)) {
|
|
181
|
+
label = undefined;
|
|
182
|
+
error = 'this account is already added';
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
label = undefined;
|
|
188
|
+
error = 'sign-in could not be verified';
|
|
189
|
+
}
|
|
190
|
+
let registered = false;
|
|
191
|
+
if (label !== undefined) {
|
|
192
|
+
try {
|
|
193
|
+
// Registration and the login's terminal state change together inside the
|
|
194
|
+
// lock callback (synchronous), so a cancel or timeout that ran during the
|
|
195
|
+
// verification awaits wins and nothing is registered.
|
|
196
|
+
await updateSubscriptionState(state => {
|
|
197
|
+
if (login.view().state !== 'verifying' || !existsSync(profileHome(provider, profileId)))
|
|
198
|
+
return;
|
|
199
|
+
state.providers[provider].profiles.push({ id: profileId, label: label.slice(0, 80), createdAt: new Date(this.now()).toISOString() });
|
|
200
|
+
login.complete(true);
|
|
201
|
+
registered = true;
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
error = 'sign-in could not be saved';
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (!registered) {
|
|
209
|
+
login.complete(false, error);
|
|
210
|
+
removeProfileHome(provider, profileId);
|
|
211
|
+
this.checks.delete(`${provider}/${profileId}`);
|
|
212
|
+
}
|
|
213
|
+
this.options.onChange?.();
|
|
214
|
+
}
|
|
215
|
+
/** Query the official CLI for each existing profile, including the adopted default. */
|
|
216
|
+
async accountAlreadyAdded(provider, email) {
|
|
217
|
+
const wanted = email.trim().toLowerCase();
|
|
218
|
+
for (const profile of readSubscriptionState().providers[provider].profiles) {
|
|
219
|
+
const home = profileHome(provider, profile.id);
|
|
220
|
+
let existing;
|
|
221
|
+
if (provider === 'claude') {
|
|
222
|
+
const status = await claudeAuthStatus(this.bins.claude, profileEnv(HOME_ENV.claude, profile.id, home));
|
|
223
|
+
if (status.loggedIn)
|
|
224
|
+
existing = status.email;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
existing = await withCodex(this.bins.codex, profileEnv(HOME_ENV.codex, profile.id, home), async (server) => {
|
|
228
|
+
const read = await server.call('account/read', { refreshToken: false });
|
|
229
|
+
return read.account?.type === 'chatgpt' ? read.account.email ?? undefined : undefined;
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
if (existing?.trim().toLowerCase() === wanted)
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
login(loginId, owner) {
|
|
238
|
+
const login = this.logins.get(loginId);
|
|
239
|
+
// Unknown and foreign logins look the same: no probing of other sessions' logins.
|
|
240
|
+
if (!login || login.owner !== owner)
|
|
241
|
+
throw new FleetError('resource_not_found', 'login not found');
|
|
242
|
+
return login;
|
|
243
|
+
}
|
|
244
|
+
async setActive(provider, profileId) {
|
|
245
|
+
this.profile(provider, profileId);
|
|
246
|
+
await updateSubscriptionState(state => {
|
|
247
|
+
if (!state.providers[provider].profiles.some(p => p.id === profileId))
|
|
248
|
+
throw new FleetError('resource_not_found', 'profile not found');
|
|
249
|
+
state.providers[provider].activeProfileId = profileId;
|
|
250
|
+
});
|
|
251
|
+
this.options.onChange?.();
|
|
252
|
+
return (await this.list()).find(p => p.provider === provider);
|
|
253
|
+
}
|
|
254
|
+
async rename(provider, profileId, label) {
|
|
255
|
+
const clean = label.replace(/[\u0000-\u001f\u007f]/g, '').trim().slice(0, 80);
|
|
256
|
+
if (!clean)
|
|
257
|
+
throw new FleetError('invalid_request', 'label is required');
|
|
258
|
+
await updateSubscriptionState(state => {
|
|
259
|
+
const profile = state.providers[provider].profiles.find(p => p.id === profileId);
|
|
260
|
+
if (!profile)
|
|
261
|
+
throw new FleetError('resource_not_found', 'profile not found');
|
|
262
|
+
profile.label = clean;
|
|
263
|
+
});
|
|
264
|
+
this.options.onChange?.();
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Remove a profile and its login. Refused while it is active or while any
|
|
268
|
+
* running agent is still pinned to it; the adopted default is never removed.
|
|
269
|
+
*/
|
|
270
|
+
async remove(provider, profileId) {
|
|
271
|
+
if (profileId === DEFAULT_PROFILE_ID)
|
|
272
|
+
throw new FleetError('conflict', 'the default profile is your existing CLI login and cannot be removed here');
|
|
273
|
+
const pinned = (await this.pins()).filter(x => x.running && x.provider === provider && x.profileId === profileId);
|
|
274
|
+
if (pinned.length)
|
|
275
|
+
throw new FleetError('conflict', `profile is in use by running agents: ${pinned.map(x => x.roleId).join(', ')}; restart or stop them first`);
|
|
276
|
+
await updateSubscriptionState(state => {
|
|
277
|
+
const p = state.providers[provider];
|
|
278
|
+
if (!p.profiles.some(x => x.id === profileId))
|
|
279
|
+
throw new FleetError('resource_not_found', 'profile not found');
|
|
280
|
+
if (p.activeProfileId === profileId)
|
|
281
|
+
throw new FleetError('conflict', 'switch to another profile before removing the active one');
|
|
282
|
+
p.profiles = p.profiles.filter(x => x.id !== profileId);
|
|
283
|
+
});
|
|
284
|
+
removeProfileHome(provider, profileId);
|
|
285
|
+
if (provider === 'claude')
|
|
286
|
+
rmSync(this.probeUsageDir(profileId), { recursive: true, force: true });
|
|
287
|
+
this.checks.delete(`${provider}/${profileId}`);
|
|
288
|
+
this.options.onChange?.();
|
|
289
|
+
}
|
|
290
|
+
profile(provider, profileId) {
|
|
291
|
+
if (!readSubscriptionState().providers[provider].profiles.some(p => p.id === profileId))
|
|
292
|
+
throw new FleetError('resource_not_found', 'profile not found');
|
|
293
|
+
}
|
|
294
|
+
/** An explicit, bounded Haiku request can refresh Claude usage for an idle profile. */
|
|
295
|
+
probeClaudeUsage(profileId) {
|
|
296
|
+
this.profile('claude', profileId);
|
|
297
|
+
const inFlight = this.probes.get(profileId);
|
|
298
|
+
if (inFlight)
|
|
299
|
+
return inFlight;
|
|
300
|
+
const run = (async () => {
|
|
301
|
+
const dir = this.probeUsageDir(profileId);
|
|
302
|
+
const result = await probeClaudeRateLimit(this.bins.claude, profileId);
|
|
303
|
+
if (result) {
|
|
304
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
305
|
+
const observedAt = new Date(this.now()).toISOString();
|
|
306
|
+
const windows = result.rateLimit && recordClaudeRateLimit(dir, result.rateLimit, new Date(this.now()))
|
|
307
|
+
? readObservedClaudeUsage(dir, this.now())?.windows ?? [] : [];
|
|
308
|
+
if (windows.length || result.exhausted) {
|
|
309
|
+
const usage = { windows, observedAt, source: 'probe', ...(result.exhausted ? { exhausted: true } : {}) };
|
|
310
|
+
replaceFileAtomically(join(dir, OBSERVED_USAGE_FILE), `${JSON.stringify(usage)}\n`, 0o600);
|
|
311
|
+
this.options.onChange?.();
|
|
312
|
+
return usage;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
rmSync(join(dir, OBSERVED_USAGE_FILE), { force: true });
|
|
316
|
+
this.options.onChange?.();
|
|
317
|
+
return { windows: [], source: 'none' };
|
|
318
|
+
})().catch(() => ({ windows: [], source: 'none' }))
|
|
319
|
+
.finally(() => this.probes.delete(profileId));
|
|
320
|
+
this.probes.set(profileId, run);
|
|
321
|
+
return run;
|
|
322
|
+
}
|
|
323
|
+
probeUsageDir(profileId) {
|
|
324
|
+
return join(subscriptionsRoot(), 'observed', 'claude', profileId);
|
|
325
|
+
}
|
|
326
|
+
probedUsage(profileId) {
|
|
327
|
+
const usage = readObservedClaudeUsage(this.probeUsageDir(profileId), this.now());
|
|
328
|
+
return usage && (usage.windows.length || usage.exhausted) ? { ...usage, source: 'probe' } : undefined;
|
|
329
|
+
}
|
|
330
|
+
async pins() {
|
|
331
|
+
const result = [];
|
|
332
|
+
for (const agent of await this.options.agents()) {
|
|
333
|
+
if (!agent.stateDir)
|
|
334
|
+
continue;
|
|
335
|
+
const pin = readPin(agent.stateDir);
|
|
336
|
+
if (pin)
|
|
337
|
+
result.push({ roleId: agent.roleId, stateDir: agent.stateDir, running: agent.running, provider: pin.provider, profileId: pin.profileId, unmanaged: pin.unmanaged });
|
|
338
|
+
}
|
|
339
|
+
return result;
|
|
340
|
+
}
|
|
341
|
+
/** Latest Claude rate-limit snapshot any agent on this profile reported. */
|
|
342
|
+
observedUsage(pins, profileId) {
|
|
343
|
+
let best;
|
|
344
|
+
for (const pin of pins) {
|
|
345
|
+
if (pin.provider !== 'claude' || pin.profileId !== profileId)
|
|
346
|
+
continue;
|
|
347
|
+
const observed = readObservedClaudeUsage(pin.stateDir);
|
|
348
|
+
if (observed && (!best || (observed.observedAt ?? '') > (best.observedAt ?? '')))
|
|
349
|
+
best = observed;
|
|
350
|
+
}
|
|
351
|
+
return best;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/** Read what `recordClaudeRateLimit` stored, dropping windows that already reset. */
|
|
355
|
+
export function readObservedClaudeUsage(stateDir, now = Date.now()) {
|
|
356
|
+
try {
|
|
357
|
+
const raw = JSON.parse(readFileSync(join(stateDir, OBSERVED_USAGE_FILE), 'utf8'));
|
|
358
|
+
const windows = (raw.windows ?? []).filter(w => !w.resetsAt || Date.parse(w.resetsAt) > now);
|
|
359
|
+
return { windows, observedAt: raw.observedAt, source: 'agent', ...(raw.exhausted === true ? { exhausted: true } : {}) };
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
return undefined;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fleet-wide Claude Code / Codex subscription profiles.
|
|
3
|
+
*
|
|
4
|
+
* A profile is one CLI home directory holding one account's login. Fleet never
|
|
5
|
+
* reads or returns token values; the official CLIs own the credential files.
|
|
6
|
+
* The `default` profile adopts the operator's existing `~/.claude` / `~/.codex`
|
|
7
|
+
* in place, so a fleet that never adds a profile launches exactly as before.
|
|
8
|
+
* Exactly one profile per provider is active; switching only affects agents
|
|
9
|
+
* launched afterwards (running sessions stay pinned until restarted).
|
|
10
|
+
*/
|
|
11
|
+
export type SubscriptionProvider = 'claude' | 'codex';
|
|
12
|
+
export declare const SUBSCRIPTION_PROVIDERS: SubscriptionProvider[];
|
|
13
|
+
export declare const DEFAULT_PROFILE_ID = "default";
|
|
14
|
+
export interface SubscriptionProfile {
|
|
15
|
+
id: string;
|
|
16
|
+
label: string;
|
|
17
|
+
createdAt: string;
|
|
18
|
+
}
|
|
19
|
+
export interface ProviderState {
|
|
20
|
+
activeProfileId: string;
|
|
21
|
+
profiles: SubscriptionProfile[];
|
|
22
|
+
}
|
|
23
|
+
export interface SubscriptionState {
|
|
24
|
+
version: 1;
|
|
25
|
+
providers: Record<SubscriptionProvider, ProviderState>;
|
|
26
|
+
}
|
|
27
|
+
/** Recorded in an agent's state dir at launch: which profile the session uses. */
|
|
28
|
+
export interface SubscriptionPin {
|
|
29
|
+
provider: SubscriptionProvider;
|
|
30
|
+
profileId: string;
|
|
31
|
+
/** Explicit CLI home, absent for the adopted default (the CLI's own default). */
|
|
32
|
+
home?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Default profile only: credential env (role or supervisor) that makes this
|
|
35
|
+
* session authenticate outside Fleet's profiles. Reported, not managed.
|
|
36
|
+
*/
|
|
37
|
+
unmanaged?: string[];
|
|
38
|
+
pinnedAt: string;
|
|
39
|
+
}
|
|
40
|
+
export declare const PIN_FILE = ".subscription-pin.json";
|
|
41
|
+
export declare const OBSERVED_USAGE_FILE = ".subscription-usage.json";
|
|
42
|
+
export declare const PROFILE_ID_RE: RegExp;
|
|
43
|
+
export declare const subscriptionsRoot: () => string;
|
|
44
|
+
/**
|
|
45
|
+
* The CLI's own default home, adopted as the `default` profile. This follows
|
|
46
|
+
* `$HOME` like the CLIs do, not OURS_FLEET_HOME (which may relocate Fleet state).
|
|
47
|
+
*/
|
|
48
|
+
export declare const baseHome: (provider: SubscriptionProvider) => string;
|
|
49
|
+
export declare function isProvider(value: unknown): value is SubscriptionProvider;
|
|
50
|
+
export declare function profileHome(provider: SubscriptionProvider, profileId: string): string;
|
|
51
|
+
/**
|
|
52
|
+
* Read the state. The file is only ever replaced by an atomic rename, so a
|
|
53
|
+
* reader (including a launching agent) sees either the old or the new active
|
|
54
|
+
* profile, never a torn write.
|
|
55
|
+
*/
|
|
56
|
+
export declare function readSubscriptionState(): SubscriptionState;
|
|
57
|
+
/** Serialised read-modify-write of the state under a cross-process lock. */
|
|
58
|
+
export declare function updateSubscriptionState<T>(fn: (state: SubscriptionState) => T): Promise<T>;
|
|
59
|
+
/**
|
|
60
|
+
* Create an empty profile home (not yet registered). Shared, non-history
|
|
61
|
+
* configuration is symlinked from the base home; credentials and session
|
|
62
|
+
* history stay per profile.
|
|
63
|
+
*/
|
|
64
|
+
export declare function createProfileHome(provider: SubscriptionProvider): {
|
|
65
|
+
id: string;
|
|
66
|
+
home: string;
|
|
67
|
+
};
|
|
68
|
+
/** Remove a non-default profile home. Never touches the adopted default. */
|
|
69
|
+
export declare function removeProfileHome(provider: SubscriptionProvider, profileId: string): void;
|
|
70
|
+
export declare function readPin(stateDir: string, provider?: SubscriptionProvider): SubscriptionPin | undefined;
|
|
71
|
+
export declare function writePin(stateDir: string, pin: SubscriptionPin): void;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, symlinkSync } from 'node:fs';
|
|
2
|
+
import { randomBytes } from 'node:crypto';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { replaceFileAtomically, withFileLock } from '../atomic-file.js';
|
|
5
|
+
import { FleetError } from '../application/errors.js';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { stateRoot } from '../paths.js';
|
|
8
|
+
export const SUBSCRIPTION_PROVIDERS = ['claude', 'codex'];
|
|
9
|
+
export const DEFAULT_PROFILE_ID = 'default';
|
|
10
|
+
export const PIN_FILE = '.subscription-pin.json';
|
|
11
|
+
export const OBSERVED_USAGE_FILE = '.subscription-usage.json';
|
|
12
|
+
/** Non-history configuration shared from the base home into each new profile. */
|
|
13
|
+
const SHARED_CONFIG = {
|
|
14
|
+
claude: ['settings.json', 'CLAUDE.md', 'plugins', 'skills', 'agents', 'commands'],
|
|
15
|
+
codex: ['config.toml', 'AGENTS.md', 'plugins', 'skills', 'rules'],
|
|
16
|
+
};
|
|
17
|
+
export const PROFILE_ID_RE = /^[a-z0-9][a-z0-9-]{0,40}$/;
|
|
18
|
+
export const subscriptionsRoot = () => join(stateRoot(), 'subscriptions');
|
|
19
|
+
const stateFile = () => join(subscriptionsRoot(), 'state.json');
|
|
20
|
+
const lockPath = () => join(subscriptionsRoot(), '.lock');
|
|
21
|
+
/**
|
|
22
|
+
* The CLI's own default home, adopted as the `default` profile. This follows
|
|
23
|
+
* `$HOME` like the CLIs do, not OURS_FLEET_HOME (which may relocate Fleet state).
|
|
24
|
+
*/
|
|
25
|
+
export const baseHome = (provider) => join(homedir(), provider === 'claude' ? '.claude' : '.codex');
|
|
26
|
+
export function isProvider(value) {
|
|
27
|
+
return value === 'claude' || value === 'codex';
|
|
28
|
+
}
|
|
29
|
+
export function profileHome(provider, profileId) {
|
|
30
|
+
if (profileId === DEFAULT_PROFILE_ID)
|
|
31
|
+
return baseHome(provider);
|
|
32
|
+
if (!PROFILE_ID_RE.test(profileId))
|
|
33
|
+
throw new FleetError('invalid_request', 'invalid profile id');
|
|
34
|
+
return join(subscriptionsRoot(), provider, profileId);
|
|
35
|
+
}
|
|
36
|
+
function initialState() {
|
|
37
|
+
const provider = () => ({
|
|
38
|
+
activeProfileId: DEFAULT_PROFILE_ID,
|
|
39
|
+
profiles: [{ id: DEFAULT_PROFILE_ID, label: 'Default (existing login)', createdAt: new Date(0).toISOString() }],
|
|
40
|
+
});
|
|
41
|
+
return { version: 1, providers: { claude: provider(), codex: provider() } };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Read the state. The file is only ever replaced by an atomic rename, so a
|
|
45
|
+
* reader (including a launching agent) sees either the old or the new active
|
|
46
|
+
* profile, never a torn write.
|
|
47
|
+
*/
|
|
48
|
+
export function readSubscriptionState() {
|
|
49
|
+
let text;
|
|
50
|
+
try {
|
|
51
|
+
text = readFileSync(stateFile(), 'utf8');
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
if (error.code === 'ENOENT')
|
|
55
|
+
return initialState();
|
|
56
|
+
throw unreadable();
|
|
57
|
+
}
|
|
58
|
+
// Fail closed: a damaged state file must not silently move launches to another account.
|
|
59
|
+
let raw;
|
|
60
|
+
try {
|
|
61
|
+
raw = JSON.parse(text);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
throw unreadable();
|
|
65
|
+
}
|
|
66
|
+
const state = initialState();
|
|
67
|
+
const providers = raw?.providers;
|
|
68
|
+
if (!providers || typeof providers !== 'object')
|
|
69
|
+
throw unreadable();
|
|
70
|
+
for (const provider of SUBSCRIPTION_PROVIDERS) {
|
|
71
|
+
const p = providers[provider];
|
|
72
|
+
if (!p || !Array.isArray(p.profiles) || typeof p.activeProfileId !== 'string')
|
|
73
|
+
throw unreadable();
|
|
74
|
+
const profiles = p.profiles;
|
|
75
|
+
if (profiles.some(x => !x || typeof x.id !== 'string' || (x.id !== DEFAULT_PROFILE_ID && !PROFILE_ID_RE.test(x.id))))
|
|
76
|
+
throw unreadable();
|
|
77
|
+
if (!profiles.some(x => x.id === DEFAULT_PROFILE_ID))
|
|
78
|
+
profiles.unshift(state.providers[provider].profiles[0]);
|
|
79
|
+
if (!profiles.some(x => x.id === p.activeProfileId))
|
|
80
|
+
throw unreadable();
|
|
81
|
+
const active = p.activeProfileId;
|
|
82
|
+
state.providers[provider] = {
|
|
83
|
+
activeProfileId: active,
|
|
84
|
+
profiles: profiles.map(x => ({ id: x.id, label: String(x.label ?? x.id).slice(0, 80), createdAt: String(x.createdAt ?? '') })),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
return state;
|
|
88
|
+
}
|
|
89
|
+
const unreadable = () => new FleetError('prerequisite_unavailable', `subscription state ${stateFile()} is unreadable or invalid; fix or remove it before starting agents`);
|
|
90
|
+
/** Serialised read-modify-write of the state under a cross-process lock. */
|
|
91
|
+
export async function updateSubscriptionState(fn) {
|
|
92
|
+
mkdirSync(subscriptionsRoot(), { recursive: true, mode: 0o700 });
|
|
93
|
+
return withFileLock(lockPath(), () => {
|
|
94
|
+
const state = readSubscriptionState();
|
|
95
|
+
const result = fn(state);
|
|
96
|
+
replaceFileAtomically(stateFile(), `${JSON.stringify(state, null, 2)}\n`, 0o600);
|
|
97
|
+
return result;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Create an empty profile home (not yet registered). Shared, non-history
|
|
102
|
+
* configuration is symlinked from the base home; credentials and session
|
|
103
|
+
* history stay per profile.
|
|
104
|
+
*/
|
|
105
|
+
export function createProfileHome(provider) {
|
|
106
|
+
const id = `p-${randomBytes(6).toString('hex')}`;
|
|
107
|
+
const dir = profileHome(provider, id);
|
|
108
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
109
|
+
const base = baseHome(provider);
|
|
110
|
+
for (const name of SHARED_CONFIG[provider]) {
|
|
111
|
+
const src = join(base, name);
|
|
112
|
+
if (existsSync(src)) {
|
|
113
|
+
try {
|
|
114
|
+
symlinkSync(src, join(dir, name));
|
|
115
|
+
}
|
|
116
|
+
catch { /* optional convenience */ }
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return { id, home: dir };
|
|
120
|
+
}
|
|
121
|
+
/** Remove a non-default profile home. Never touches the adopted default. */
|
|
122
|
+
export function removeProfileHome(provider, profileId) {
|
|
123
|
+
if (profileId === DEFAULT_PROFILE_ID)
|
|
124
|
+
return;
|
|
125
|
+
rmSync(profileHome(provider, profileId), { recursive: true, force: true });
|
|
126
|
+
}
|
|
127
|
+
export function readPin(stateDir, provider) {
|
|
128
|
+
try {
|
|
129
|
+
const pin = JSON.parse(readFileSync(join(stateDir, PIN_FILE), 'utf8'));
|
|
130
|
+
if (provider)
|
|
131
|
+
return pin[provider];
|
|
132
|
+
return Object.values(pin)[0];
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export function writePin(stateDir, pin) {
|
|
139
|
+
replaceFileAtomically(join(stateDir, PIN_FILE), `${JSON.stringify({ [pin.provider]: pin })}\n`, 0o600);
|
|
140
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { clientConfigPath } from '../client-profile.js';
|
|
1
2
|
import { existsSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
|
|
2
3
|
import { join } from 'node:path';
|
|
3
4
|
import { home, logsRoot } from '../paths.js';
|
|
@@ -22,7 +23,7 @@ const xml = (value) => value
|
|
|
22
23
|
.replaceAll("'", ''');
|
|
23
24
|
function plist(name, binPath) {
|
|
24
25
|
const log = join(logsRoot(), `${name}.log`);
|
|
25
|
-
const config = process.env
|
|
26
|
+
const config = clientConfigPath(process.env);
|
|
26
27
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
27
28
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
28
29
|
<plist version="1.0">
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { clientConfigPath } from '../client-profile.js';
|
|
1
2
|
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
2
3
|
import { delimiter, dirname, join } from 'node:path';
|
|
3
4
|
import { userInfo } from 'node:os';
|
|
@@ -69,30 +70,10 @@ export function makeSystemdBackend(exec = realExec) {
|
|
|
69
70
|
dirname(process.execPath),
|
|
70
71
|
...(process.env.PATH ?? '').split(delimiter),
|
|
71
72
|
].filter(Boolean))].join(delimiter);
|
|
72
|
-
//
|
|
73
|
-
//
|
|
74
|
-
//
|
|
75
|
-
// ours-fleet resolves its daemon from OURS_CONFIG / OURS_PORT / OURS_STATE_DIR
|
|
76
|
-
// and otherwise falls back to ~/.ours and port 3050 (src/monitor.ts). A host
|
|
77
|
-
// set up against a non-default daemon — the installer's multi-daemon profiles
|
|
78
|
-
// do exactly this — passes that selection to `ours-fleet init` in the
|
|
79
|
-
// environment, and it died there: nothing persisted it, so every runner
|
|
80
|
-
// systemd started at boot resolved the default daemon again, and on a host
|
|
81
|
-
// where only the non-default daemon exists that is a daemon that is not there.
|
|
82
|
-
//
|
|
83
|
-
// ONLY OURS_CONFIG is baked, deliberately. It names which daemon, and leaves
|
|
84
|
-
// that daemon's own config file authoritative for port and state directory, so
|
|
85
|
-
// a later edit to it still wins. Baking OURS_PORT/OURS_STATE_DIR would freeze
|
|
86
|
-
// those into a unit file that outranks the config for ever after — the failure
|
|
87
|
-
// mode @ours.network/cli avoids for the same reason (packages/cli/src/
|
|
88
|
-
// service.ts: "The port is deliberately NOT baked").
|
|
89
|
-
//
|
|
90
|
-
// Absent from init's environment ⇒ no line, and the unit is byte-for-byte what
|
|
91
|
-
// it has always been. This teaches fleet nothing about installer profiles; it
|
|
92
|
-
// persists the selection fleet was initialised with.
|
|
73
|
+
// Persist one profile path, never a URL or service port. Profile updates
|
|
74
|
+
// are picked up by every fresh process, including lingering user services.
|
|
93
75
|
const unitEnv = ['PATH=' + servicePath];
|
|
94
|
-
|
|
95
|
-
unitEnv.push('OURS_CONFIG=' + process.env.OURS_CONFIG);
|
|
76
|
+
unitEnv.push('OURS_CONFIG=' + clientConfigPath(process.env));
|
|
96
77
|
const environmentLines = unitEnv
|
|
97
78
|
.map(value => `Environment="${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/%/g, '%%')}"`)
|
|
98
79
|
.join('\n');
|
|
@@ -100,10 +81,15 @@ export function makeSystemdBackend(exec = realExec) {
|
|
|
100
81
|
writeFileSync(join(unitDir, UNIT_TEMPLATE), `[Unit]
|
|
101
82
|
Description=ours-fleet agent %i
|
|
102
83
|
After=default.target
|
|
84
|
+
StartLimitIntervalSec=0
|
|
103
85
|
|
|
104
86
|
[Service]
|
|
105
87
|
Type=simple
|
|
106
88
|
${environmentLines}
|
|
89
|
+
# Wait outside the runner so boot-time daemon unavailability cannot consume
|
|
90
|
+
# the agent crash budget. Selection comes from the same role and profile.
|
|
91
|
+
ExecStartPre=${unitArg(process.execPath)} ${unitArg(binPath)} _wait-daemon %i
|
|
92
|
+
TimeoutStartSec=270
|
|
107
93
|
ExecStart=${unitArg(process.execPath)} ${unitArg(binPath)} _run %i
|
|
108
94
|
# The RUNNER owns the child-session restart loop, with a counted, backed-off
|
|
109
95
|
# circuit breaker. systemd must only recover the runner PROCESS crashing —
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { SessionSnapshot } from './session/types.js';
|
|
2
|
+
export declare const TEMP_CHAT_IDLE_MS: number;
|
|
3
|
+
export type ChatIdlePolicy = {
|
|
4
|
+
version: 1;
|
|
5
|
+
timeoutMs: number;
|
|
6
|
+
lastActivityAt: number;
|
|
7
|
+
};
|
|
8
|
+
export declare function readChatIdle(dir: string): ChatIdlePolicy | undefined;
|
|
9
|
+
/** Only a standalone temporary supervisor owns this clock; browser reads never touch it. */
|
|
10
|
+
export declare class TemporaryChatIdle {
|
|
11
|
+
private dir;
|
|
12
|
+
private value;
|
|
13
|
+
private busy;
|
|
14
|
+
constructor(dir: string, now: number);
|
|
15
|
+
private save;
|
|
16
|
+
observe(snapshot: SessionSnapshot, queueDepth: number, now: number): boolean;
|
|
17
|
+
}
|