@bitkyc08/opencodex 2.99.0 → 2.100.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.
- package/AGENTS_INSTALL.md +19 -1
- package/README.md +37 -33
- package/SPONSORS.md +5 -1
- package/bin/ocx.mjs +1 -1
- package/gui/dist/assets/{App-C1hGwaN1.css → App-COo8Yopd.css} +1 -1
- package/gui/dist/assets/App-CitvEefy.js +59 -0
- package/gui/dist/assets/Tray-CeKVWQyB.js +1 -0
- package/gui/dist/assets/{brand-mark-wzPe3R-J.js → brand-mark-M92UnxX2.js} +1 -1
- package/gui/dist/assets/index-BP47do6-.js +94 -0
- package/gui/dist/assets/index-DmDooSAG.css +2 -0
- package/gui/dist/assets/{segment-thumb-features-fYEMxBaW.js → segment-thumb-features-CcxZBnxv.js} +1 -1
- package/gui/dist/classic/assets/App-aYgIQMxI.js +58 -0
- package/gui/dist/classic/assets/{Tray-BqZueZOx.js → Tray-D8jfUXpQ.js} +1 -1
- package/gui/dist/classic/assets/index-CfGqHooN.js +94 -0
- package/gui/dist/classic/assets/quota-summary-R-ih7OlS.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/native/ocx-child/Cargo.lock +215 -0
- package/native/ocx-child/Cargo.toml +31 -0
- package/native/ocx-child/src/cli.rs +1901 -0
- package/native/ocx-child/src/codex_home.rs +429 -0
- package/native/ocx-child/src/codex_settings.rs +231 -0
- package/native/ocx-child/src/daemon.rs +363 -0
- package/native/ocx-child/src/http_wire.rs +619 -0
- package/native/ocx-child/src/journal.rs +977 -0
- package/native/ocx-child/src/lib.rs +22 -0
- package/native/ocx-child/src/main.rs +9 -0
- package/native/ocx-child/src/parity.rs +102 -0
- package/native/ocx-child/src/platform.rs +1487 -0
- package/native/ocx-child/src/relay.rs +1288 -0
- package/native/ocx-child/src/relay_auth.rs +166 -0
- package/native/ocx-child/src/routes.rs +236 -0
- package/native/ocx-child/src/state.rs +190 -0
- package/native/ocx-child/src/test_hooks.rs +58 -0
- package/native/ocx-child/src/toml_surgeon.rs +1156 -0
- package/package.json +8 -2
- package/src/adapters/anthropic.ts +28 -2
- package/src/adapters/claude-cli/adapter.ts +5 -25
- package/src/adapters/claude-cli/instruction-images.ts +31 -0
- package/src/adapters/claude-cli/stable-replay.ts +42 -14
- package/src/adapters/claude-cli/usage-admission.ts +53 -64
- package/src/adapters/coding-agent/protocol.ts +1 -1
- package/src/adapters/devin/live-models.ts +3 -0
- package/src/adapters/google-http.ts +3 -0
- package/src/adapters/google-vertex-key-project.ts +10 -2
- package/src/adapters/google.ts +40 -8
- package/src/adapters/kiro/usage.ts +3 -13
- package/src/adapters/openai-responses/passthrough.ts +5 -2
- package/src/adapters/openai-responses/request-strips.ts +32 -0
- package/src/adapters/openai-responses/tool-schema.ts +9 -4
- package/src/chat/inbound.ts +1 -1
- package/src/claude/auth-detect.ts +26 -8
- package/src/claude/inbound-content-options.ts +18 -3
- package/src/claude/inbound.ts +5 -2
- package/src/cli/bundled-bun-diagnostic.ts +91 -0
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-observe-system.ts +60 -0
- package/src/cli/combo-input.ts +6 -0
- package/src/cli/combo-result.ts +1 -1
- package/src/cli/combo.ts +4 -0
- package/src/cli/config-command.ts +16 -2
- package/src/cli/connect.ts +12 -10
- package/src/cli/dispatch.ts +4 -0
- package/src/cli/doctor.ts +11 -0
- package/src/cli/help.ts +1 -0
- package/src/cli/init.ts +4 -0
- package/src/cli/link-enrollment.ts +152 -25
- package/src/cli/link-local-control.ts +49 -0
- package/src/cli/link.ts +37 -10
- package/src/cli/provider-lifecycle-runtime.ts +4 -2
- package/src/cli/registry.ts +13 -0
- package/src/cli/star-prompt.ts +2 -0
- package/src/cli/status.ts +1 -1
- package/src/cli/telemetry-profile.ts +206 -0
- package/src/cli/telemetry.ts +260 -0
- package/src/client/hub-relay.ts +26 -0
- package/src/client/machine-api.ts +3 -0
- package/src/clients/config-export/t3code.ts +44 -5
- package/src/clients/omo-role-models.ts +1 -1
- package/src/codex/catalog/bundled.ts +44 -2
- package/src/codex/runtime.ts +12 -0
- package/src/codex/shim-restore-lock.ts +67 -30
- package/src/combos/types.ts +21 -1
- package/src/config/atomic-write.ts +24 -1
- package/src/config/diagnostics.ts +15 -0
- package/src/config/live-reconcile.ts +17 -0
- package/src/config/load-degrade.ts +16 -0
- package/src/config/provider-validation.ts +29 -0
- package/src/config/schema/config-schema.ts +24 -0
- package/src/config/schema/leaf-validators.ts +17 -0
- package/src/config/telemetry-carry.ts +30 -0
- package/src/config.ts +13 -1
- package/src/generated/compatibility-version.json +405 -153
- package/src/generated/model-metadata.ts +3 -3
- package/src/integrations/aside-profiles.ts +7 -1
- package/src/integrations/owned-refresh.ts +9 -2
- package/src/lab/artifacts/secure-fs.ts +12 -11
- package/src/lab/fabric/scratch.ts +11 -9
- package/src/lab/public/community.ts +2 -3
- package/src/lab/public/file-safety.ts +18 -20
- package/src/lab/public/mutation-lock.ts +109 -337
- package/src/lab/public/private-file.ts +4 -3
- package/src/lib/bun-runtime-preflight.ts +1 -1
- package/src/lib/codex-catalog-read-only.ts +122 -0
- package/src/lib/config-ownership.ts +49 -18
- package/src/lib/errors.ts +11 -0
- package/src/lib/gcp-adc.ts +167 -48
- package/src/lib/jsonc.ts +4 -2
- package/src/lib/local-control-socket.ts +88 -0
- package/src/lib/privacy.ts +15 -0
- package/src/lib/provider-client-headers.ts +3 -0
- package/src/lib/token-estimate.ts +13 -2
- package/src/link/child-binary-source.ts +228 -0
- package/src/link/ephemeral-child.ts +45 -0
- package/src/link/remote-plant.ts +306 -0
- package/src/link/remote-windows.ts +59 -5
- package/src/link/ssh-argv.ts +71 -8
- package/src/link/status-projection.ts +2 -0
- package/src/link/store.ts +5 -1
- package/src/oauth/claude-keychain-read.ts +165 -0
- package/src/oauth/local-token-detect.ts +14 -17
- package/src/protocols/envelope.ts +20 -2
- package/src/providers/api-key-selection.ts +8 -2
- package/src/providers/claude-cli-usage-state.ts +93 -0
- package/src/providers/github-copilot-auto.ts +25 -5
- package/src/providers/meta-muse-plan-refusal.ts +94 -0
- package/src/providers/model-rename-fields.ts +3 -0
- package/src/providers/quota/vendor-probes-key.ts +23 -4
- package/src/providers/quota/vendor-probes-oauth.ts +59 -0
- package/src/providers/quota/workbuddy.ts +2 -2
- package/src/providers/quota.ts +25 -18
- package/src/providers/registry/entries-core.ts +7 -4
- package/src/providers/registry/entries-extended.ts +39 -7
- package/src/providers/registry/model-seeds.ts +10 -2
- package/src/providers/resolved-model-policy.ts +4 -0
- package/src/providers/routed-config.ts +30 -3
- package/src/providers/stale-no-vision-seed-migration.ts +7 -0
- package/src/providers/vertex-environment-authority.ts +50 -0
- package/src/providers/workbuddy-credit-balance.ts +38 -6
- package/src/quota-pool/assemble.ts +14 -6
- package/src/router.ts +1 -0
- package/src/server/adapter-resolve.ts +11 -6
- package/src/server/audio-dictation.ts +21 -7
- package/src/server/auth-cors.ts +19 -0
- package/src/server/background-lifecycle.ts +71 -0
- package/src/server/chat-completions.ts +2 -0
- package/src/server/claude-messages.ts +30 -3
- package/src/server/index/live-sideband.ts +15 -1
- package/src/server/index/local-control-listener.ts +515 -0
- package/src/server/index/optional-listeners.ts +21 -1
- package/src/server/index/serve-options.ts +4 -3
- package/src/server/index/websocket-handler.ts +2 -0
- package/src/server/index.ts +3 -0
- package/src/server/management/combo-routes.ts +6 -1
- package/src/server/management/context.ts +1 -0
- package/src/server/management/link-child-preflight.ts +74 -0
- package/src/server/management/link-enrollment.ts +190 -25
- package/src/server/management/link-route-state.ts +6 -1
- package/src/server/management/link-routes.ts +27 -9
- package/src/server/management/model-rows.ts +4 -0
- package/src/server/management/oauth-account-routes.ts +9 -4
- package/src/server/management/provider-overwrite-carry.ts +9 -2
- package/src/server/management/provider-routes.ts +14 -0
- package/src/server/management/provider-thinking-display-patch.ts +54 -0
- package/src/server/management/route-registry.ts +7 -0
- package/src/server/management/session-routes.ts +4 -1
- package/src/server/management/telemetry-routes.ts +37 -0
- package/src/server/management/usage-aggregate-cache.ts +290 -67
- package/src/server/management-api.ts +11 -2
- package/src/server/messages-native-preflight.ts +166 -0
- package/src/server/messages-native.ts +143 -43
- package/src/server/messages-shared.ts +14 -0
- package/src/server/proxy-liveness.ts +99 -6
- package/src/server/request-log-conversation.ts +18 -2
- package/src/server/request-log.ts +7 -2
- package/src/server/responses/adapter-continuation.ts +1 -0
- package/src/server/responses/adapter-dispatch.ts +11 -28
- package/src/server/responses/adapter-failure-message.ts +68 -0
- package/src/server/responses/combo-stream-preflight.ts +14 -2
- package/src/server/responses/core-auth.ts +26 -5
- package/src/server/responses/core-codex-account.ts +8 -1
- package/src/server/responses/core-combo-failure.ts +6 -1
- package/src/server/responses/core-combo-native.ts +150 -37
- package/src/server/responses/core-combo.ts +15 -9
- package/src/server/responses/core-normalize.ts +1 -0
- package/src/server/responses/core-options.ts +7 -1
- package/src/server/responses/core-pipeline.ts +21 -1
- package/src/server/responses/fetch-helpers.ts +5 -0
- package/src/server/responses/opengateway-completion-guard.ts +7 -3
- package/src/server/responses/passthrough-delivery.ts +8 -1
- package/src/server/responses/passthrough-error.ts +22 -0
- package/src/server/responses/request-prepare.ts +7 -1
- package/src/server/responses/request-transport.ts +28 -5
- package/src/server/responses/run-turn-execution.ts +13 -0
- package/src/server/sse-frame-buffer.ts +5 -0
- package/src/server/ws-bridge.ts +10 -0
- package/src/service/launchd.ts +78 -4
- package/src/telemetry/aggregate.ts +348 -0
- package/src/telemetry/consent.ts +128 -0
- package/src/telemetry/contract.ts +403 -0
- package/src/telemetry/disclosure.ts +19 -0
- package/src/telemetry/install-id.ts +226 -0
- package/src/telemetry/payload.ts +129 -0
- package/src/telemetry/profile/client.ts +150 -0
- package/src/telemetry/profile/contract.ts +288 -0
- package/src/telemetry/profile/disclosure.ts +14 -0
- package/src/telemetry/profile/paths.ts +13 -0
- package/src/telemetry/profile/routes.ts +99 -0
- package/src/telemetry/profile/service.ts +234 -0
- package/src/telemetry/profile/state.ts +189 -0
- package/src/telemetry/profile/sync.ts +276 -0
- package/src/telemetry/routes.ts +183 -0
- package/src/telemetry/scheduler.ts +386 -0
- package/src/telemetry/sender.ts +97 -0
- package/src/telemetry/state.ts +334 -0
- package/src/types/combo-jev-effort-mode.ts +11 -0
- package/src/types/config.ts +3 -0
- package/src/types/provider.ts +11 -1
- package/src/types/request.ts +2 -0
- package/src/types/telemetry.ts +15 -0
- package/src/types/wire.ts +10 -0
- package/src/types.ts +5 -0
- package/src/update/index.ts +7 -1
- package/src/update/minisign.d.mts +17 -0
- package/src/update/minisign.mjs +88 -0
- package/src/update/signed-supply.d.mts +59 -0
- package/src/update/signed-supply.mjs +289 -0
- package/src/update/trust-root.json +38 -0
- package/src/update/update-bounded-json.d.mts +4 -0
- package/src/update/update-bounded-json.mjs +113 -0
- package/src/update/update-bytes.d.mts +2 -0
- package/src/update/update-bytes.mjs +23 -0
- package/src/update/update-candidate-id.d.mts +2 -0
- package/src/update/update-candidate-id.mjs +23 -0
- package/src/update/update-checkpoint-codec.d.mts +33 -0
- package/src/update/update-checkpoint-codec.mjs +217 -0
- package/src/update/update-contracts.d.mts +24 -0
- package/src/update/update-contracts.mjs +207 -0
- package/src/update/update-signature-threshold.d.mts +12 -0
- package/src/update/update-signature-threshold.mjs +47 -0
- package/src/update/update-signed-version.d.mts +5 -0
- package/src/update/update-signed-version.mjs +25 -0
- package/src/update/update-trust-anchor.d.mts +2 -0
- package/src/update/update-trust-anchor.mjs +2 -0
- package/src/usage/cache-diagnostic.ts +195 -50
- package/src/usage/expected-prices.ts +20 -2
- package/src/usage/filter-index.ts +1469 -0
- package/src/usage/ledger-scanner.ts +245 -4
- package/src/usage/source-filter.ts +10 -2
- package/src/usage/summary.ts +16 -0
- package/src/web-search/parse.ts +244 -38
- package/gui/dist/assets/App-BepstgEN.js +0 -59
- package/gui/dist/assets/Tray-BinxbEZo.js +0 -1
- package/gui/dist/assets/index-C-9X2yaI.js +0 -94
- package/gui/dist/assets/index-bnUWypyz.css +0 -2
- package/gui/dist/classic/assets/App-DX88_GCW.js +0 -58
- package/gui/dist/classic/assets/index-fSP71pLB.js +0 -94
- package/gui/dist/classic/assets/quota-summary-y2fl0WBv.js +0 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { jsonResponse } from "../../server/auth-cors";
|
|
2
|
+
import { hasHumanDashboardSession } from "../../server/management/consent-session";
|
|
3
|
+
import type { ManagementContext } from "../../server/management/context";
|
|
4
|
+
import {
|
|
5
|
+
ProfileActionError,
|
|
6
|
+
buildProfileStatusSnapshot,
|
|
7
|
+
connectTelemetryProfile,
|
|
8
|
+
disconnectTelemetryProfile,
|
|
9
|
+
fetchConnectedProfileRank,
|
|
10
|
+
} from "./service";
|
|
11
|
+
import { isProfileSchedulerRunning, nudgeProfileScheduler, startProfileScheduler } from "./sync";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Management routes for the opt-in public profile (`/api/telemetry/profile`). Connecting needs a
|
|
15
|
+
* human dashboard session, like the telemetry decision; reading and disconnecting do not, because
|
|
16
|
+
* leaving must always be possible. No response ever carries the profile secret.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
function errorResponse(ctx: ManagementContext, error: ProfileActionError): Response {
|
|
20
|
+
return jsonResponse({ ok: false, code: error.code, error: error.message }, error.status, ctx.req, ctx.config);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function handleGetProfileRoute(ctx: ManagementContext): Promise<Response> {
|
|
24
|
+
const snapshot = await buildProfileStatusSnapshot({ cachePreview: true });
|
|
25
|
+
// A profile connected from the CLI while this proxy runs: start its loop on the next read.
|
|
26
|
+
if ((snapshot.connected || snapshot.unreadable) && !isProfileSchedulerRunning()) startProfileScheduler();
|
|
27
|
+
return jsonResponse({ ok: true, ...snapshot }, 200, ctx.req, ctx.config);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function handleGetProfileRankRoute(ctx: ManagementContext): Promise<Response> {
|
|
31
|
+
const result = await fetchConnectedProfileRank();
|
|
32
|
+
if (!result.ok) {
|
|
33
|
+
const status = result.code === "not_connected" ? 404 : result.code === "revoked" ? 410 : 502;
|
|
34
|
+
return jsonResponse({ ok: false, code: result.code, error: result.error }, status, ctx.req, ctx.config);
|
|
35
|
+
}
|
|
36
|
+
return jsonResponse({ ok: true, rank: result.rank }, 200, ctx.req, ctx.config);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function handlePostProfileRoute(ctx: ManagementContext): Promise<Response> {
|
|
40
|
+
if (!hasHumanDashboardSession(ctx)) {
|
|
41
|
+
return jsonResponse(
|
|
42
|
+
{
|
|
43
|
+
ok: false,
|
|
44
|
+
code: "agent_consent_required",
|
|
45
|
+
error: "Connecting a public profile requires a human dashboard session.",
|
|
46
|
+
message:
|
|
47
|
+
"Refused: this request carries no human dashboard session. A public profile must be connected by the user in the dashboard or an interactive terminal.",
|
|
48
|
+
},
|
|
49
|
+
403,
|
|
50
|
+
ctx.req,
|
|
51
|
+
ctx.config,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
let body: unknown;
|
|
55
|
+
try {
|
|
56
|
+
body = await ctx.req.json();
|
|
57
|
+
} catch {
|
|
58
|
+
return jsonResponse({ ok: false, code: "invalid_json", error: "invalid JSON body" }, 400, ctx.req, ctx.config);
|
|
59
|
+
}
|
|
60
|
+
const record = body && typeof body === "object" && !Array.isArray(body) ? body as Record<string, unknown> : null;
|
|
61
|
+
if (!record || record.acknowledgePublic !== true) {
|
|
62
|
+
return jsonResponse(
|
|
63
|
+
{ ok: false, code: "acknowledgement_required", error: "acknowledgePublic must be true: the profile is public." },
|
|
64
|
+
400,
|
|
65
|
+
ctx.req,
|
|
66
|
+
ctx.config,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const profile = await connectTelemetryProfile({
|
|
71
|
+
handle: record.handle,
|
|
72
|
+
displayName: record.displayName,
|
|
73
|
+
avatar: record.avatar,
|
|
74
|
+
});
|
|
75
|
+
nudgeProfileScheduler();
|
|
76
|
+
const snapshot = await buildProfileStatusSnapshot();
|
|
77
|
+
return jsonResponse({ ok: true, ...snapshot, profile }, 200, ctx.req, ctx.config);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
if (error instanceof ProfileActionError) return errorResponse(ctx, error);
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function handleDeleteProfileRoute(ctx: ManagementContext): Promise<Response> {
|
|
85
|
+
try {
|
|
86
|
+
const outcome = await disconnectTelemetryProfile();
|
|
87
|
+
if (outcome.status === "deletion_pending") nudgeProfileScheduler();
|
|
88
|
+
return jsonResponse(
|
|
89
|
+
{ ok: true, status: outcome.status, deleted: outcome.status === "deleted" || outcome.status === "not_connected" },
|
|
90
|
+
200,
|
|
91
|
+
ctx.req,
|
|
92
|
+
ctx.config,
|
|
93
|
+
);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error instanceof ProfileActionError) return errorResponse(ctx, error);
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { getConfigDir } from "../../config/paths";
|
|
2
|
+
import { isTelemetryEnvOptOut } from "../consent";
|
|
3
|
+
import { formatUtcDay } from "../state";
|
|
4
|
+
import { aggregateTelemetryForDay } from "../aggregate";
|
|
5
|
+
import { createProfile, deleteProfile, fetchOwnProfileRank, type ProfileClientOptions } from "./client";
|
|
6
|
+
import {
|
|
7
|
+
PROFILE_SCHEMA_VERSION,
|
|
8
|
+
validateProfileCreateV1,
|
|
9
|
+
type ProfileDayV1,
|
|
10
|
+
type ProfileRankV1,
|
|
11
|
+
} from "./contract";
|
|
12
|
+
import {
|
|
13
|
+
deleteTelemetryProfileFile,
|
|
14
|
+
readTelemetryProfileFile,
|
|
15
|
+
toPublicProfileState,
|
|
16
|
+
writeTelemetryProfileState,
|
|
17
|
+
type TelemetryProfilePublicState,
|
|
18
|
+
} from "./state";
|
|
19
|
+
import { buildProfileDay, readProfileTelemetryGate, resolveNextProfileDay } from "./sync";
|
|
20
|
+
|
|
21
|
+
export type ProfileErrorCode =
|
|
22
|
+
| "telemetry_off"
|
|
23
|
+
| "already_connected"
|
|
24
|
+
| "deletion_pending"
|
|
25
|
+
| "invalid"
|
|
26
|
+
| "handle_taken"
|
|
27
|
+
| "rate_limited"
|
|
28
|
+
| "collector_unavailable"
|
|
29
|
+
| "not_connected"
|
|
30
|
+
| "profile_unreadable";
|
|
31
|
+
|
|
32
|
+
export class ProfileActionError extends Error {
|
|
33
|
+
constructor(readonly code: ProfileErrorCode, message: string, readonly status: number) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "ProfileActionError";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ProfileStatusSnapshot {
|
|
40
|
+
connected: boolean;
|
|
41
|
+
/** The file exists but cannot be read: nothing is sent; disconnect is still offered. */
|
|
42
|
+
unreadable: boolean;
|
|
43
|
+
profile: TelemetryProfilePublicState | null;
|
|
44
|
+
/** Anonymous telemetry is effectively on (required for connecting and for sending). */
|
|
45
|
+
telemetryEffective: boolean;
|
|
46
|
+
nextDay: string | null;
|
|
47
|
+
nextDayReason: string;
|
|
48
|
+
/** Exactly what the next send would contain, or null. */
|
|
49
|
+
preview: ProfileDayV1 | null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ProfileServiceOptions extends ProfileClientOptions {
|
|
53
|
+
configDir?: string;
|
|
54
|
+
now?: number;
|
|
55
|
+
env?: Record<string, string | undefined>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const PREVIEW_CACHE_MS = 10 * 60 * 1000;
|
|
59
|
+
let previewCache: { configDir: string; connectedAt: string; day: string; at: number; preview: ProfileDayV1 | null } | null = null;
|
|
60
|
+
const RANK_CACHE_MS = 5 * 60 * 1000;
|
|
61
|
+
let rankCache: { configDir: string; handle: string; at: number; rank: ProfileRankV1 } | null = null;
|
|
62
|
+
|
|
63
|
+
export function resetProfileServiceCachesForTests(): void {
|
|
64
|
+
previewCache = null;
|
|
65
|
+
rankCache = null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function buildProfileStatusSnapshot(
|
|
69
|
+
options: ProfileServiceOptions & { cachePreview?: boolean } = {},
|
|
70
|
+
): Promise<ProfileStatusSnapshot> {
|
|
71
|
+
const configDir = options.configDir ?? getConfigDir();
|
|
72
|
+
const now = options.now ?? Date.now();
|
|
73
|
+
const env = options.env ?? process.env;
|
|
74
|
+
const gate = readProfileTelemetryGate({ configDir, env });
|
|
75
|
+
const telemetryEffective = gate.status.effective && isTelemetryEnvOptOut(env) === null;
|
|
76
|
+
const file = readTelemetryProfileFile(configDir);
|
|
77
|
+
const base = { connected: file.state !== null, unreadable: file.unreadable, telemetryEffective };
|
|
78
|
+
if (!file.state) {
|
|
79
|
+
return { ...base, profile: null, nextDay: null, nextDayReason: file.unreadable ? "state-unreadable" : "not-connected", preview: null };
|
|
80
|
+
}
|
|
81
|
+
const profile = file.state;
|
|
82
|
+
const next = profile.pendingDelete
|
|
83
|
+
? { day: null, reason: "deletion-pending" }
|
|
84
|
+
: profile.revoked
|
|
85
|
+
? { day: null, reason: "revoked" }
|
|
86
|
+
: resolveNextProfileDay(profile, gate, now);
|
|
87
|
+
let preview: ProfileDayV1 | null = null;
|
|
88
|
+
if (next.day) {
|
|
89
|
+
const hit = previewCache;
|
|
90
|
+
if (options.cachePreview && hit && hit.configDir === configDir && hit.connectedAt === profile.connectedAt && hit.day === next.day && now - hit.at >= 0 && now - hit.at < PREVIEW_CACHE_MS) {
|
|
91
|
+
preview = hit.preview;
|
|
92
|
+
} else {
|
|
93
|
+
preview = buildProfileDay(await aggregateTelemetryForDay(next.day));
|
|
94
|
+
if (options.cachePreview) previewCache = { configDir, connectedAt: profile.connectedAt, day: next.day, at: now, preview };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return { ...base, profile: toPublicProfileState(profile), nextDay: next.day, nextDayReason: next.reason, preview };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface ConnectProfileInput {
|
|
101
|
+
handle: unknown;
|
|
102
|
+
displayName: unknown;
|
|
103
|
+
avatar: unknown;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Create the public profile and store its secret. Callers have already established that a person
|
|
108
|
+
* asked for this (a human dashboard session or an interactive, non-agent terminal).
|
|
109
|
+
*/
|
|
110
|
+
export async function connectTelemetryProfile(
|
|
111
|
+
input: ConnectProfileInput,
|
|
112
|
+
options: ProfileServiceOptions = {},
|
|
113
|
+
): Promise<TelemetryProfilePublicState> {
|
|
114
|
+
const configDir = options.configDir ?? getConfigDir();
|
|
115
|
+
const now = options.now ?? Date.now();
|
|
116
|
+
const env = options.env ?? process.env;
|
|
117
|
+
const existing = readTelemetryProfileFile(configDir);
|
|
118
|
+
if (existing.unreadable) {
|
|
119
|
+
throw new ProfileActionError(
|
|
120
|
+
"profile_unreadable",
|
|
121
|
+
"The local profile file cannot be read. Remove it first with 'ocx telemetry profile disconnect' (or Disconnect on the dashboard Home).",
|
|
122
|
+
409,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
if (existing.state?.pendingDelete) throw new ProfileActionError("deletion_pending", "The previous profile is still being deleted; try again once deletion is confirmed.", 409);
|
|
126
|
+
if (existing.state) throw new ProfileActionError("already_connected", "A public profile is already connected; disconnect it first.", 409);
|
|
127
|
+
const gate = readProfileTelemetryGate({ configDir, env });
|
|
128
|
+
if (!gate.status.effective || isTelemetryEnvOptOut(env) !== null) {
|
|
129
|
+
throw new ProfileActionError("telemetry_off", "A public profile needs anonymous telemetry to be on: its totals come from the same daily aggregate.", 409);
|
|
130
|
+
}
|
|
131
|
+
const validated = validateProfileCreateV1({
|
|
132
|
+
schemaVersion: PROFILE_SCHEMA_VERSION,
|
|
133
|
+
handle: input.handle,
|
|
134
|
+
displayName: input.displayName,
|
|
135
|
+
avatar: input.avatar,
|
|
136
|
+
});
|
|
137
|
+
if (!validated.ok) throw new ProfileActionError("invalid", validated.error, 400);
|
|
138
|
+
const created = await createProfile(validated.value, options);
|
|
139
|
+
if (!created.ok) {
|
|
140
|
+
if (created.status === 409) throw new ProfileActionError("handle_taken", "That handle is already taken.", 409);
|
|
141
|
+
if (created.status === 400) throw new ProfileActionError("invalid", created.error, 400);
|
|
142
|
+
if (created.status === 429) throw new ProfileActionError("rate_limited", "Too many profiles were created from this network recently; try again later.", 429);
|
|
143
|
+
throw new ProfileActionError("collector_unavailable", "The telemetry service could not be reached; nothing was saved.", 502);
|
|
144
|
+
}
|
|
145
|
+
// Another connect may have finished while this one waited on the collector.
|
|
146
|
+
const raced = readTelemetryProfileFile(configDir);
|
|
147
|
+
if (raced.state || raced.unreadable) {
|
|
148
|
+
await deleteProfile(created.body.secret, options);
|
|
149
|
+
throw new ProfileActionError("already_connected", "A public profile is already connected; disconnect it first.", 409);
|
|
150
|
+
}
|
|
151
|
+
const state = {
|
|
152
|
+
version: 1 as const,
|
|
153
|
+
handle: validated.value.handle,
|
|
154
|
+
displayName: validated.value.displayName,
|
|
155
|
+
avatar: validated.value.avatar,
|
|
156
|
+
secret: created.body.secret,
|
|
157
|
+
connectedAt: new Date(now).toISOString(),
|
|
158
|
+
// The collector refuses days on or before its own creation day; never start below it.
|
|
159
|
+
connectedDay: created.body.createdDay && created.body.createdDay > formatUtcDay(now) ? created.body.createdDay : formatUtcDay(now),
|
|
160
|
+
sentDays: {},
|
|
161
|
+
lastSentDay: null,
|
|
162
|
+
lastSentAt: null,
|
|
163
|
+
retryAfterUntil: null,
|
|
164
|
+
pendingDelete: false,
|
|
165
|
+
revoked: false,
|
|
166
|
+
};
|
|
167
|
+
writeTelemetryProfileState(state, configDir, now);
|
|
168
|
+
rankCache = null;
|
|
169
|
+
previewCache = null;
|
|
170
|
+
return toPublicProfileState(state);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export type DisconnectOutcome =
|
|
174
|
+
| { status: "deleted" }
|
|
175
|
+
| { status: "not_connected" }
|
|
176
|
+
| { status: "local_removed" }
|
|
177
|
+
| { status: "deletion_pending"; error: string };
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Disconnect: mark the deletion as pending, ask the collector to delete the profile and every row,
|
|
181
|
+
* and remove the local file once it confirms. Never gated: leaving is always allowed, with
|
|
182
|
+
* anonymous telemetry on or off. A deletion that cannot reach the collector stays pending and the
|
|
183
|
+
* proxy's profile loop retries it.
|
|
184
|
+
*/
|
|
185
|
+
export async function disconnectTelemetryProfile(options: ProfileServiceOptions = {}): Promise<DisconnectOutcome> {
|
|
186
|
+
const configDir = options.configDir ?? getConfigDir();
|
|
187
|
+
const now = options.now ?? Date.now();
|
|
188
|
+
const file = readTelemetryProfileFile(configDir);
|
|
189
|
+
if (file.unreadable) {
|
|
190
|
+
// Its secret cannot be read, so the server profile cannot be deleted from here; removing the
|
|
191
|
+
// local file is still the person's way out. A profile that stops submitting is pruned by the
|
|
192
|
+
// collector after 400 days and drops out of every ranked period at once.
|
|
193
|
+
if (!deleteTelemetryProfileFile(configDir)) {
|
|
194
|
+
throw new ProfileActionError("profile_unreadable", "The local profile file cannot be read or removed (it may be a symlink or a directory).", 409);
|
|
195
|
+
}
|
|
196
|
+
rankCache = null;
|
|
197
|
+
return { status: "local_removed" };
|
|
198
|
+
}
|
|
199
|
+
const state = file.state;
|
|
200
|
+
if (!state) return { status: "not_connected" };
|
|
201
|
+
if (!state.pendingDelete) writeTelemetryProfileState({ ...state, pendingDelete: true, retryAfterUntil: null }, configDir, now);
|
|
202
|
+
rankCache = null;
|
|
203
|
+
previewCache = null;
|
|
204
|
+
const result = await deleteProfile(state.secret, options);
|
|
205
|
+
if (result.deleted) {
|
|
206
|
+
if (deleteTelemetryProfileFile(configDir)) return { status: "deleted" };
|
|
207
|
+
// The server profile is gone, but the local file (with its now useless secret) is still
|
|
208
|
+
// there: say so instead of reporting a clean disconnect. The loop keeps retrying the removal.
|
|
209
|
+
return { status: "deletion_pending", error: "the local profile file could not be removed" };
|
|
210
|
+
}
|
|
211
|
+
return { status: "deletion_pending", error: result.error ?? "collector unavailable" };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** The connected profile's ranks, from the collector, cached for a few minutes. */
|
|
215
|
+
export async function fetchConnectedProfileRank(
|
|
216
|
+
options: ProfileServiceOptions = {},
|
|
217
|
+
): Promise<{ ok: true; rank: ProfileRankV1 } | { ok: false; code: ProfileErrorCode | "revoked"; error: string }> {
|
|
218
|
+
const configDir = options.configDir ?? getConfigDir();
|
|
219
|
+
const now = options.now ?? Date.now();
|
|
220
|
+
const file = readTelemetryProfileFile(configDir);
|
|
221
|
+
if (!file.state || file.state.pendingDelete) return { ok: false, code: "not_connected", error: "No public profile is connected." };
|
|
222
|
+
const hit = rankCache;
|
|
223
|
+
if (hit && hit.configDir === configDir && hit.handle === file.state.handle && now - hit.at >= 0 && now - hit.at < RANK_CACHE_MS) {
|
|
224
|
+
return { ok: true, rank: hit.rank };
|
|
225
|
+
}
|
|
226
|
+
const result = await fetchOwnProfileRank(file.state.secret, options);
|
|
227
|
+
if (!result.ok) {
|
|
228
|
+
if (result.status === 401) return { ok: false, code: "revoked", error: "The telemetry service no longer knows this profile." };
|
|
229
|
+
return { ok: false, code: "collector_unavailable", error: "Rank is unavailable right now." };
|
|
230
|
+
}
|
|
231
|
+
rankCache = { configDir, handle: file.state.handle, at: now, rank: result.body };
|
|
232
|
+
return { ok: true, rank: result.body };
|
|
233
|
+
}
|
|
234
|
+
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { lstatSync, mkdirSync, readFileSync, unlinkSync } from "node:fs";
|
|
2
|
+
import { atomicWriteFileNoFollow } from "../../config/atomic-write";
|
|
3
|
+
import { getConfigDir } from "../../config/paths";
|
|
4
|
+
import { recordOwnedConfigPath } from "../../lib/config-ownership";
|
|
5
|
+
import { parseCalendarUtcDayMs } from "../contract";
|
|
6
|
+
import {
|
|
7
|
+
PROFILE_SECRET_RE,
|
|
8
|
+
isProfileAvatar,
|
|
9
|
+
validateProfileDisplayName,
|
|
10
|
+
validateProfileHandle,
|
|
11
|
+
type ProfileAvatar,
|
|
12
|
+
type PublicProfileV1,
|
|
13
|
+
} from "./contract";
|
|
14
|
+
import { telemetryProfilePath } from "./paths";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* `telemetry-profile.json`: the connected public profile, its bearer secret and its own send
|
|
18
|
+
* bookkeeping. Independent of the anonymous telemetry state and identity files: connecting or
|
|
19
|
+
* disconnecting never touches them, and turning anonymous telemetry off never touches this one.
|
|
20
|
+
*/
|
|
21
|
+
export interface TelemetryProfileState {
|
|
22
|
+
version: 1;
|
|
23
|
+
handle: string;
|
|
24
|
+
displayName: string;
|
|
25
|
+
avatar: ProfileAvatar;
|
|
26
|
+
/** Bearer secret issued by the collector. Never leaves this module except towards the collector. */
|
|
27
|
+
secret: string;
|
|
28
|
+
connectedAt: string;
|
|
29
|
+
/** UTC day of the connect; only later days are ever sent. */
|
|
30
|
+
connectedDay: string;
|
|
31
|
+
sentDays: Record<string, string>;
|
|
32
|
+
lastSentDay: string | null;
|
|
33
|
+
lastSentAt: string | null;
|
|
34
|
+
retryAfterUntil: string | null;
|
|
35
|
+
/** Set by a disconnect whose deletion has not been confirmed by the collector yet. */
|
|
36
|
+
pendingDelete: boolean;
|
|
37
|
+
/** Set when the collector no longer knows the secret (401): nothing is sent until a reconnect. */
|
|
38
|
+
revoked: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** What may be shown anywhere: the state without its secret. */
|
|
42
|
+
export interface TelemetryProfilePublicState extends PublicProfileV1 {
|
|
43
|
+
connectedAt: string;
|
|
44
|
+
connectedDay: string;
|
|
45
|
+
lastSentDay: string | null;
|
|
46
|
+
lastSentAt: string | null;
|
|
47
|
+
pendingDelete: boolean;
|
|
48
|
+
revoked: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const MAX_PROFILE_FILE_BYTES = 64 * 1024;
|
|
52
|
+
const SENT_DAYS_RETENTION_DAYS = 30;
|
|
53
|
+
const DAY_MS = 86_400_000;
|
|
54
|
+
const DAY_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
55
|
+
|
|
56
|
+
function validIso(value: unknown): string | null {
|
|
57
|
+
return typeof value === "string" && !Number.isNaN(Date.parse(value)) ? value : null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Read the profile file. A file that exists but cannot be trusted (a symlink, oversized or
|
|
62
|
+
* malformed) is reported as unreadable rather than as "not connected", so a disconnect is still
|
|
63
|
+
* offered and nothing is sent over it.
|
|
64
|
+
*/
|
|
65
|
+
export function readTelemetryProfileFile(
|
|
66
|
+
configDir = getConfigDir(),
|
|
67
|
+
): { state: TelemetryProfileState | null; unreadable: boolean } {
|
|
68
|
+
const path = telemetryProfilePath(configDir);
|
|
69
|
+
let stat;
|
|
70
|
+
try {
|
|
71
|
+
stat = lstatSync(path);
|
|
72
|
+
} catch {
|
|
73
|
+
return { state: null, unreadable: false };
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.size > MAX_PROFILE_FILE_BYTES) return { state: null, unreadable: true };
|
|
77
|
+
const raw = JSON.parse(readFileSync(path, "utf8")) as Record<string, unknown>;
|
|
78
|
+
const handle = validateProfileHandle(raw?.handle);
|
|
79
|
+
const displayName = validateProfileDisplayName(raw?.displayName);
|
|
80
|
+
const connectedAt = validIso(raw?.connectedAt);
|
|
81
|
+
if (
|
|
82
|
+
raw?.version !== 1
|
|
83
|
+
|| !handle.ok
|
|
84
|
+
|| !displayName.ok
|
|
85
|
+
|| !isProfileAvatar(raw.avatar)
|
|
86
|
+
|| typeof raw.secret !== "string"
|
|
87
|
+
|| !PROFILE_SECRET_RE.test(raw.secret)
|
|
88
|
+
|| connectedAt === null
|
|
89
|
+
|| typeof raw.connectedDay !== "string"
|
|
90
|
+
|| parseCalendarUtcDayMs(raw.connectedDay) === null
|
|
91
|
+
) {
|
|
92
|
+
return { state: null, unreadable: true };
|
|
93
|
+
}
|
|
94
|
+
const sentDays: Record<string, string> = {};
|
|
95
|
+
if (raw.sentDays && typeof raw.sentDays === "object" && !Array.isArray(raw.sentDays)) {
|
|
96
|
+
for (const [day, at] of Object.entries(raw.sentDays as Record<string, unknown>)) {
|
|
97
|
+
if (DAY_RE.test(day) && typeof at === "string") sentDays[day] = at;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
unreadable: false,
|
|
102
|
+
state: {
|
|
103
|
+
version: 1,
|
|
104
|
+
handle: handle.value,
|
|
105
|
+
displayName: displayName.value,
|
|
106
|
+
avatar: raw.avatar,
|
|
107
|
+
secret: raw.secret,
|
|
108
|
+
connectedAt,
|
|
109
|
+
connectedDay: raw.connectedDay,
|
|
110
|
+
sentDays,
|
|
111
|
+
lastSentDay: typeof raw.lastSentDay === "string" && DAY_RE.test(raw.lastSentDay) ? raw.lastSentDay : null,
|
|
112
|
+
lastSentAt: validIso(raw.lastSentAt),
|
|
113
|
+
retryAfterUntil: validIso(raw.retryAfterUntil),
|
|
114
|
+
pendingDelete: raw.pendingDelete === true,
|
|
115
|
+
revoked: raw.revoked === true,
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
} catch {
|
|
119
|
+
return { state: null, unreadable: true };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function writeTelemetryProfileState(
|
|
124
|
+
state: TelemetryProfileState,
|
|
125
|
+
configDir = getConfigDir(),
|
|
126
|
+
now = Date.now(),
|
|
127
|
+
): void {
|
|
128
|
+
mkdirSync(configDir, { recursive: true, mode: 0o700 });
|
|
129
|
+
const path = telemetryProfilePath(configDir);
|
|
130
|
+
try { recordOwnedConfigPath(configDir, path); } catch { /* best-effort */ }
|
|
131
|
+
const minKeepDay = new Date(now - SENT_DAYS_RETENTION_DAYS * DAY_MS).toISOString().slice(0, 10);
|
|
132
|
+
const sentDays: Record<string, string> = {};
|
|
133
|
+
for (const [day, at] of Object.entries(state.sentDays)) {
|
|
134
|
+
if (day >= minKeepDay) sentDays[day] = at;
|
|
135
|
+
}
|
|
136
|
+
const payload: TelemetryProfileState = { ...state, version: 1, sentDays };
|
|
137
|
+
atomicWriteFileNoFollow(path, `${JSON.stringify(payload, null, 2)}\n`);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Bookkeeping after a network call. The file is re-read and the update applied to what is on disk
|
|
142
|
+
* now, and only while it still belongs to the same profile (same secret): a disconnect that landed
|
|
143
|
+
* meanwhile (pendingDelete set, or the file removed) or a reconnect is never overwritten. The
|
|
144
|
+
* update cannot clear pendingDelete. Returns false when nothing was written.
|
|
145
|
+
*/
|
|
146
|
+
export function updateTelemetryProfileState(
|
|
147
|
+
secret: string,
|
|
148
|
+
update: (state: TelemetryProfileState) => TelemetryProfileState,
|
|
149
|
+
options: { configDir?: string; now?: number; whilePendingDelete?: boolean } = {},
|
|
150
|
+
): boolean {
|
|
151
|
+
const configDir = options.configDir ?? getConfigDir();
|
|
152
|
+
const current = readTelemetryProfileFile(configDir).state;
|
|
153
|
+
if (!current || current.secret !== secret) return false;
|
|
154
|
+
if (current.pendingDelete && !options.whilePendingDelete) return false;
|
|
155
|
+
const next = update(current);
|
|
156
|
+
writeTelemetryProfileState({ ...next, secret: current.secret, pendingDelete: current.pendingDelete || next.pendingDelete }, configDir, options.now);
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Delete the profile file. Refuses to follow a symlink. A file that cannot be read (corrupt) can
|
|
162
|
+
* still be removed; returns true when no file remains.
|
|
163
|
+
*/
|
|
164
|
+
export function deleteTelemetryProfileFile(configDir = getConfigDir()): boolean {
|
|
165
|
+
const path = telemetryProfilePath(configDir);
|
|
166
|
+
try {
|
|
167
|
+
const stat = lstatSync(path);
|
|
168
|
+
if (stat.isSymbolicLink() || !stat.isFile()) return false;
|
|
169
|
+
unlinkSync(path);
|
|
170
|
+
return true;
|
|
171
|
+
} catch (error) {
|
|
172
|
+
return (error as NodeJS.ErrnoException)?.code === "ENOENT";
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function toPublicProfileState(state: TelemetryProfileState): TelemetryProfilePublicState {
|
|
177
|
+
return {
|
|
178
|
+
handle: state.handle,
|
|
179
|
+
displayName: state.displayName,
|
|
180
|
+
avatar: state.avatar,
|
|
181
|
+
connectedAt: state.connectedAt,
|
|
182
|
+
connectedDay: state.connectedDay,
|
|
183
|
+
lastSentDay: state.lastSentDay,
|
|
184
|
+
lastSentAt: state.lastSentAt,
|
|
185
|
+
pendingDelete: state.pendingDelete,
|
|
186
|
+
revoked: state.revoked,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|