@bitkyc08/opencodex 2.99.0 → 2.100.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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-g3Cm2xhZ.js +59 -0
- package/gui/dist/assets/Tray-BMDkAiXy.js +1 -0
- package/gui/dist/assets/brand-mark-nr3ziR-T.js +1 -0
- package/gui/dist/assets/index-Bziu8gTy.css +2 -0
- package/gui/dist/assets/index-OYzhQUNP.js +94 -0
- package/gui/dist/assets/{segment-thumb-features-fYEMxBaW.js → segment-thumb-features-DKCVUUTi.js} +1 -1
- package/gui/dist/classic/assets/App-CgPBKUP-.js +58 -0
- package/gui/dist/classic/assets/{Tray-BqZueZOx.js → Tray-BedmOsE7.js} +1 -1
- package/gui/dist/classic/assets/index-BR-HhNrp.js +94 -0
- package/gui/dist/classic/assets/quota-summary-CXKhvSmQ.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-image-normalize.ts +1 -1
- 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/client-fingerprint.ts +1 -1
- 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-chat/mistral-image-limit.ts +28 -15
- package/src/adapters/openai-chat-images.ts +100 -0
- package/src/adapters/openai-chat.ts +6 -9
- 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 +422 -170
- 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 +56 -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/anthropic-model-quota.ts +12 -0
- package/src/oauth/anthropic-routing.ts +11 -4
- package/src/oauth/claude-keychain-read.ts +165 -0
- package/src/oauth/kiro-iam-identity-center.ts +4 -1
- 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-identity.ts +9 -3
- package/src/providers/claude-cli-usage-state.ts +93 -0
- package/src/providers/claude-cli-usage.ts +3 -2
- 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/anthropic-family-headers.ts +8 -1
- package/src/providers/quota/anthropic-usage-windows.ts +49 -2
- 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-types.ts +7 -0
- package/src/providers/quota.ts +25 -18
- package/src/providers/registry/entries-core.ts +7 -4
- package/src/providers/registry/entries-extended.ts +42 -8
- package/src/providers/registry/model-ids.ts +1 -0
- package/src/providers/registry/model-seeds.ts +10 -2
- package/src/providers/registry/types.ts +6 -0
- 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/responses/retained-compaction.ts +31 -5
- 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/compact.ts +13 -1
- 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 +265 -38
- package/gui/dist/assets/App-BepstgEN.js +0 -59
- package/gui/dist/assets/Tray-BinxbEZo.js +0 -1
- package/gui/dist/assets/brand-mark-wzPe3R-J.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
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
unlinkSync,
|
|
11
11
|
} from "node:fs";
|
|
12
12
|
import { basename, dirname, join } from "node:path";
|
|
13
|
+
import { sameFileIdentity } from "../../lib/file-identity";
|
|
13
14
|
import { isPrivateFileStageName } from "./private-file";
|
|
14
15
|
import { PublicEvidenceValidationError } from "./validate";
|
|
15
16
|
|
|
@@ -38,8 +39,8 @@ function sizeError(options: PrivateRegularFileReadOptions): PublicEvidenceValida
|
|
|
38
39
|
*/
|
|
39
40
|
function recoverPublishedPrivateFileStage(path: string): void {
|
|
40
41
|
if (process.platform === "win32") return;
|
|
41
|
-
const finalStats = lstatSync(path);
|
|
42
|
-
if (finalStats.isSymbolicLink() || !finalStats.isFile() || finalStats.nlink <=
|
|
42
|
+
const finalStats = lstatSync(path, { bigint: true });
|
|
43
|
+
if (finalStats.isSymbolicLink() || !finalStats.isFile() || finalStats.nlink <= 1n) return;
|
|
43
44
|
|
|
44
45
|
const dir = dirname(path);
|
|
45
46
|
const prefix = `.${basename(path)}.`;
|
|
@@ -48,12 +49,11 @@ function recoverPublishedPrivateFileStage(path: string): void {
|
|
|
48
49
|
if (!name.startsWith(prefix) || !isPrivateFileStageName(name)) continue;
|
|
49
50
|
const stagePath = join(dir, name);
|
|
50
51
|
try {
|
|
51
|
-
const stageStats = lstatSync(stagePath);
|
|
52
|
+
const stageStats = lstatSync(stagePath, { bigint: true });
|
|
52
53
|
if (
|
|
53
54
|
stageStats.isFile()
|
|
54
55
|
&& !stageStats.isSymbolicLink()
|
|
55
|
-
&& stageStats
|
|
56
|
-
&& stageStats.ino === finalStats.ino
|
|
56
|
+
&& sameFileIdentity(stageStats, finalStats)
|
|
57
57
|
) {
|
|
58
58
|
candidates.push(stagePath);
|
|
59
59
|
}
|
|
@@ -75,12 +75,11 @@ function recoverPublishedPrivateFileStage(path: string): void {
|
|
|
75
75
|
|
|
76
76
|
for (const stagePath of candidates) {
|
|
77
77
|
try {
|
|
78
|
-
const stageStats = lstatSync(stagePath);
|
|
78
|
+
const stageStats = lstatSync(stagePath, { bigint: true });
|
|
79
79
|
if (
|
|
80
80
|
stageStats.isFile()
|
|
81
81
|
&& !stageStats.isSymbolicLink()
|
|
82
|
-
&& stageStats
|
|
83
|
-
&& stageStats.ino === finalStats.ino
|
|
82
|
+
&& sameFileIdentity(stageStats, finalStats)
|
|
84
83
|
) {
|
|
85
84
|
unlinkSync(stagePath);
|
|
86
85
|
}
|
|
@@ -95,32 +94,31 @@ function withPrivateRegularFile<T>(
|
|
|
95
94
|
options: PrivateRegularFileReadOptions,
|
|
96
95
|
consume: (fd: number, size: number) => T,
|
|
97
96
|
): T {
|
|
98
|
-
let pathStats = lstatSync(path);
|
|
99
|
-
if (!pathStats.isSymbolicLink() && pathStats.isFile() && pathStats.nlink > 1) {
|
|
97
|
+
let pathStats = lstatSync(path, { bigint: true });
|
|
98
|
+
if (!pathStats.isSymbolicLink() && pathStats.isFile() && Number(pathStats.nlink) > 1) {
|
|
100
99
|
recoverPublishedPrivateFileStage(path);
|
|
101
|
-
pathStats = lstatSync(path);
|
|
100
|
+
pathStats = lstatSync(path, { bigint: true });
|
|
102
101
|
}
|
|
103
|
-
if (pathStats.isSymbolicLink() || !pathStats.isFile() || pathStats.nlink !== 1) {
|
|
102
|
+
if (pathStats.isSymbolicLink() || !pathStats.isFile() || Number(pathStats.nlink) !== 1) {
|
|
104
103
|
throw new PublicEvidenceValidationError(options.errorCode, options.errorMessage);
|
|
105
104
|
}
|
|
106
|
-
if (pathStats.size > options.maxBytes) throw sizeError(options);
|
|
105
|
+
if (Number(pathStats.size) > options.maxBytes) throw sizeError(options);
|
|
107
106
|
|
|
108
107
|
const fd = openSync(path, fsConstants.O_RDONLY | O_NOFOLLOW);
|
|
109
108
|
try {
|
|
110
|
-
const stats = fstatSync(fd);
|
|
109
|
+
const stats = fstatSync(fd, { bigint: true });
|
|
111
110
|
if (
|
|
112
111
|
!stats.isFile()
|
|
113
|
-
|| stats.nlink !== 1
|
|
114
|
-
|| stats
|
|
115
|
-
|| stats.ino !== pathStats.ino
|
|
112
|
+
|| Number(stats.nlink) !== 1
|
|
113
|
+
|| !sameFileIdentity(stats, pathStats)
|
|
116
114
|
) {
|
|
117
115
|
throw new PublicEvidenceValidationError(options.errorCode, options.errorMessage);
|
|
118
116
|
}
|
|
119
|
-
if (stats.size > options.maxBytes) throw sizeError(options);
|
|
120
|
-
if (options.requireMode600 && process.platform !== "win32" && (stats.mode & 0o777) !== 0o600) {
|
|
117
|
+
if (Number(stats.size) > options.maxBytes) throw sizeError(options);
|
|
118
|
+
if (options.requireMode600 && process.platform !== "win32" && (Number(stats.mode) & 0o777) !== 0o600) {
|
|
121
119
|
throw new PublicEvidenceValidationError(options.errorCode, options.errorMessage);
|
|
122
120
|
}
|
|
123
|
-
return consume(fd, stats.size);
|
|
121
|
+
return consume(fd, Number(stats.size));
|
|
124
122
|
} finally {
|
|
125
123
|
closeSync(fd);
|
|
126
124
|
}
|
|
@@ -1,25 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
renameSync,
|
|
7
|
-
rmSync,
|
|
8
|
-
unlinkSync,
|
|
9
|
-
writeFileSync,
|
|
10
|
-
} from "node:fs";
|
|
11
|
-
import { dirname, join } from "node:path";
|
|
12
|
-
import { ensureLabDirs, labCommunityDir } from "../paths";
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { lstatSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { sameFileIdentity, statTimeMs, type FileIdentity } from "../../lib/file-identity";
|
|
5
|
+
import { ensureLabDirs, labCommunityDir, labRoot } from "../paths";
|
|
13
6
|
import { readPrivateRegularFile } from "./file-safety";
|
|
14
7
|
import { PublicEvidenceValidationError } from "./validate";
|
|
15
8
|
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
9
|
+
const PUBLIC_EVIDENCE_MUTATION_DB_NAME = "public-evidence-mutation.sqlite";
|
|
10
|
+
const LEGACY_MUTATION_LOCK_NAME = ".mutation-lock";
|
|
11
|
+
const LEGACY_MUTATION_LOCK_OWNER = "owner.json";
|
|
12
|
+
const LEGACY_MUTATION_LOCK_INCOMPLETE_STALE_MS = 24 * 60 * 60 * 1000;
|
|
13
|
+
const LEGACY_MUTATION_LOCK_ABSOLUTE_STALE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
21
14
|
const DETACHED_MUTATION_LOCK_RE = /^\.mutation-lock-(?:stale|release)-\d+-[0-9a-f-]{36}$/;
|
|
22
|
-
const
|
|
15
|
+
const LEGACY_OWNER_FILE_OPTIONS = {
|
|
23
16
|
maxBytes: 1024,
|
|
24
17
|
errorCode: "community_cache_lock",
|
|
25
18
|
errorMessage: "community cache mutation lock metadata is unsafe",
|
|
@@ -28,33 +21,22 @@ const MUTATION_LOCK_META_FILE_OPTIONS = {
|
|
|
28
21
|
requireMode600: true,
|
|
29
22
|
} as const;
|
|
30
23
|
|
|
31
|
-
type
|
|
24
|
+
type LegacyMutationLockOwner = {
|
|
32
25
|
pid: number;
|
|
33
|
-
token: string;
|
|
34
26
|
createdAt: number;
|
|
35
27
|
};
|
|
36
28
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
token: string;
|
|
40
|
-
createdAt: number;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
type MutationLockDirectoryIdentity = {
|
|
44
|
-
dev: number;
|
|
45
|
-
ino: number;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
function mutationLockPath(configDir?: string): string {
|
|
49
|
-
return join(labCommunityDir(configDir), PUBLIC_EVIDENCE_MUTATION_LOCK_NAME);
|
|
29
|
+
function mutationLockDatabasePath(configDir?: string): string {
|
|
30
|
+
return join(labRoot(configDir), PUBLIC_EVIDENCE_MUTATION_DB_NAME);
|
|
50
31
|
}
|
|
51
32
|
|
|
52
|
-
|
|
53
|
-
|
|
33
|
+
/** Lock bookkeeping entries an older build may leave in the community cache directory. */
|
|
34
|
+
export function isMutationLockEntryName(name: string): boolean {
|
|
35
|
+
return name === LEGACY_MUTATION_LOCK_NAME || DETACHED_MUTATION_LOCK_RE.test(name);
|
|
54
36
|
}
|
|
55
37
|
|
|
56
|
-
function
|
|
57
|
-
return join(
|
|
38
|
+
function legacyMutationLockPath(configDir?: string): string {
|
|
39
|
+
return join(labCommunityDir(configDir), LEGACY_MUTATION_LOCK_NAME);
|
|
58
40
|
}
|
|
59
41
|
|
|
60
42
|
function pidDefinitelyDead(pid: number): boolean {
|
|
@@ -66,47 +48,22 @@ function pidDefinitelyDead(pid: number): boolean {
|
|
|
66
48
|
}
|
|
67
49
|
}
|
|
68
50
|
|
|
69
|
-
function
|
|
70
|
-
return nowMs - createdAt > PUBLIC_EVIDENCE_MUTATION_LOCK_ABSOLUTE_STALE_MS;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function readLockMetadata<T extends MutationLockOwner | MutationLockReclaim>(
|
|
74
|
-
path: string,
|
|
75
|
-
): T | null {
|
|
51
|
+
function readLegacyMutationLockOwner(lockPath: string): LegacyMutationLockOwner | null {
|
|
76
52
|
try {
|
|
77
|
-
const bytes = readPrivateRegularFile(
|
|
78
|
-
const raw = JSON.parse(bytes.toString("utf8")) as Partial<
|
|
53
|
+
const bytes = readPrivateRegularFile(join(lockPath, LEGACY_MUTATION_LOCK_OWNER), LEGACY_OWNER_FILE_OPTIONS);
|
|
54
|
+
const raw = JSON.parse(bytes.toString("utf8")) as Partial<LegacyMutationLockOwner>;
|
|
79
55
|
if (
|
|
80
|
-
Number.isSafeInteger(raw.pid)
|
|
81
|
-
&& Number(raw.
|
|
82
|
-
|
|
83
|
-
&& /^[0-9a-f-]{36}$/.test(raw.token)
|
|
84
|
-
&& Number.isSafeInteger(raw.createdAt)
|
|
85
|
-
&& Number(raw.createdAt) > 0
|
|
86
|
-
) {
|
|
87
|
-
return {
|
|
88
|
-
pid: Number(raw.pid),
|
|
89
|
-
token: raw.token,
|
|
90
|
-
createdAt: Number(raw.createdAt),
|
|
91
|
-
} as T;
|
|
92
|
-
}
|
|
56
|
+
Number.isSafeInteger(raw.pid) && Number(raw.pid) > 0
|
|
57
|
+
&& Number.isSafeInteger(raw.createdAt) && Number(raw.createdAt) > 0
|
|
58
|
+
) return { pid: Number(raw.pid), createdAt: Number(raw.createdAt) };
|
|
93
59
|
} catch {
|
|
94
|
-
//
|
|
95
|
-
// applicable. Normal acquisition never trusts malformed metadata.
|
|
60
|
+
// Missing or malformed metadata falls back to the directory-age rule below.
|
|
96
61
|
}
|
|
97
62
|
return null;
|
|
98
63
|
}
|
|
99
64
|
|
|
100
|
-
function readMutationLockOwner(lockPath: string): MutationLockOwner | null {
|
|
101
|
-
return readLockMetadata<MutationLockOwner>(mutationLockOwnerPath(lockPath));
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function readMutationLockReclaim(lockPath: string): MutationLockReclaim | null {
|
|
105
|
-
return readLockMetadata<MutationLockReclaim>(mutationLockReclaimPath(lockPath));
|
|
106
|
-
}
|
|
107
|
-
|
|
108
65
|
function assertMutationLockDirectory(lockPath: string) {
|
|
109
|
-
const stat = lstatSync(lockPath);
|
|
66
|
+
const stat = lstatSync(lockPath, { bigint: true });
|
|
110
67
|
if (!stat.isDirectory() || stat.isSymbolicLink()) {
|
|
111
68
|
throw new PublicEvidenceValidationError(
|
|
112
69
|
"community_cache_lock",
|
|
@@ -116,309 +73,124 @@ function assertMutationLockDirectory(lockPath: string) {
|
|
|
116
73
|
return stat;
|
|
117
74
|
}
|
|
118
75
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function mutationLockIsReclaimable(lockPath: string, nowMs: number): boolean {
|
|
132
|
-
const stat = assertMutationLockDirectory(lockPath);
|
|
133
|
-
const owner = readMutationLockOwner(lockPath);
|
|
134
|
-
if (owner) {
|
|
135
|
-
// A live PID is strong evidence only while the recorded ownership generation is
|
|
136
|
-
// reasonably recent. The absolute ceiling recovers from PID reuse after a crash.
|
|
137
|
-
return pidDefinitelyDead(owner.pid) || exceedsAbsoluteLockAge(owner.createdAt, nowMs);
|
|
138
|
-
}
|
|
139
|
-
// The only ownerless state is the tiny mkdir-to-owner publication window. Use
|
|
140
|
-
// a deliberately long fallback so a crashed acquisition can eventually heal
|
|
141
|
-
// without treating an ordinary pause as proof that the owner disappeared.
|
|
142
|
-
return nowMs - stat.mtimeMs > PUBLIC_EVIDENCE_MUTATION_LOCK_INCOMPLETE_STALE_MS;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function unlinkIfPresent(path: string): void {
|
|
76
|
+
/**
|
|
77
|
+
* Whether a directory lock left by an older OpenCodex build still has a holder.
|
|
78
|
+
*
|
|
79
|
+
* Older builds serialized through a `.mutation-lock` directory. This build never creates,
|
|
80
|
+
* renames or removes one; it only declines to run beside a holder that still looks alive, using
|
|
81
|
+
* the same evidence the older builds used: a live owner PID within the absolute age ceiling, or
|
|
82
|
+
* an ownerless directory recent enough to be an acquisition in progress.
|
|
83
|
+
*/
|
|
84
|
+
function legacyMutationLockBlocks(lockPath: string, nowMs: number): boolean {
|
|
85
|
+
let stat: ReturnType<typeof assertMutationLockDirectory>;
|
|
146
86
|
try {
|
|
147
|
-
|
|
87
|
+
stat = assertMutationLockDirectory(lockPath);
|
|
148
88
|
} catch (error) {
|
|
149
|
-
if ((error as NodeJS.ErrnoException).code
|
|
89
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
const owner = readLegacyMutationLockOwner(lockPath);
|
|
93
|
+
if (owner) {
|
|
94
|
+
return !pidDefinitelyDead(owner.pid) && nowMs - owner.createdAt <= LEGACY_MUTATION_LOCK_ABSOLUTE_STALE_MS;
|
|
150
95
|
}
|
|
96
|
+
return nowMs - statTimeMs(stat.mtimeNs) <= LEGACY_MUTATION_LOCK_INCOMPLETE_STALE_MS;
|
|
151
97
|
}
|
|
152
98
|
|
|
153
|
-
function cleanupDetachedMutationLocks(
|
|
154
|
-
const dir =
|
|
99
|
+
function cleanupDetachedMutationLocks(configDir: string | undefined, nowMs: number): void {
|
|
100
|
+
const dir = labCommunityDir(configDir);
|
|
155
101
|
for (const name of readdirSync(dir)) {
|
|
156
102
|
if (!DETACHED_MUTATION_LOCK_RE.test(name)) continue;
|
|
157
|
-
|
|
158
|
-
// lock
|
|
159
|
-
//
|
|
160
|
-
|
|
103
|
+
const detachedPath = join(dir, name);
|
|
104
|
+
// Older builds moved a released or reclaimed lock directory to one of these UUID-scoped
|
|
105
|
+
// names before deleting it. One that still records a live owner may belong to a running
|
|
106
|
+
// older-build holder, so it is left alone. Removing the rest keeps them from leaking
|
|
107
|
+
// storage or being mistaken for community cache objects.
|
|
108
|
+
const owner = readLegacyMutationLockOwner(detachedPath);
|
|
109
|
+
if (owner && !pidDefinitelyDead(owner.pid) && nowMs - owner.createdAt <= LEGACY_MUTATION_LOCK_ABSOLUTE_STALE_MS) continue;
|
|
110
|
+
rmSync(detachedPath, { recursive: true, force: true });
|
|
161
111
|
}
|
|
162
112
|
}
|
|
163
113
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
// Write through a unique temporary pathname first. If an ancient ownerless
|
|
170
|
-
// lock is reclaimed while this process was suspended, the inode check prevents
|
|
171
|
-
// this acquisition from publishing its owner metadata into a replacement lock.
|
|
172
|
-
const tempPath = join(lockPath, `.owner-${owner.token}.tmp`);
|
|
173
|
-
try {
|
|
174
|
-
writeFileSync(tempPath, JSON.stringify(owner), {
|
|
175
|
-
encoding: "utf8",
|
|
176
|
-
flag: "wx",
|
|
177
|
-
mode: 0o600,
|
|
178
|
-
});
|
|
179
|
-
if (!sameDirectoryIdentity(currentDirectoryIdentity(lockPath), expectedDirectory)) {
|
|
180
|
-
throw new PublicEvidenceValidationError(
|
|
181
|
-
"community_cache_lock",
|
|
182
|
-
"community cache mutation lock changed during owner publication",
|
|
183
|
-
);
|
|
184
|
-
}
|
|
185
|
-
renameSync(tempPath, mutationLockOwnerPath(lockPath));
|
|
186
|
-
if (!sameDirectoryIdentity(currentDirectoryIdentity(lockPath), expectedDirectory)) {
|
|
187
|
-
throw new PublicEvidenceValidationError(
|
|
188
|
-
"community_cache_lock",
|
|
189
|
-
"community cache mutation lock changed after owner publication",
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
const persisted = readMutationLockOwner(lockPath);
|
|
193
|
-
if (!persisted || persisted.pid !== owner.pid || persisted.token !== owner.token) {
|
|
194
|
-
throw new PublicEvidenceValidationError(
|
|
195
|
-
"community_cache_lock",
|
|
196
|
-
"community cache mutation lock owner publication was not durable",
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
} finally {
|
|
200
|
-
// The rename normally makes this ENOENT. If the lock pathname was replaced,
|
|
201
|
-
// the UUID-scoped temporary name can be removed without touching successor state.
|
|
202
|
-
unlinkIfPresent(tempPath);
|
|
114
|
+
/** The lock file's identity, after proving it is a single-link regular file. */
|
|
115
|
+
function mutationLockFileIdentity(path: string): FileIdentity {
|
|
116
|
+
const stat = lstatSync(path, { bigint: true });
|
|
117
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.nlink !== 1n) {
|
|
118
|
+
throw new PublicEvidenceValidationError("community_cache_lock", "community cache mutation lock is unsafe");
|
|
203
119
|
}
|
|
120
|
+
return { dev: stat.dev, ino: stat.ino };
|
|
204
121
|
}
|
|
205
122
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
123
|
+
/**
|
|
124
|
+
* Open the lock database on a file this module created as a private regular file.
|
|
125
|
+
*
|
|
126
|
+
* The Lab root is owner-only, so the processes that can replace this pathname are the user's own,
|
|
127
|
+
* the same trust boundary as the config mutation lock. Within it, the file is created exclusively
|
|
128
|
+
* with mode 0600 (never chmod-ed through the pathname), and the caller re-proves after locking
|
|
129
|
+
* that the pathname still names the file inspected before the open.
|
|
130
|
+
*/
|
|
131
|
+
function openMutationLockDatabase(path: string): { database: Database; identity: FileIdentity } {
|
|
212
132
|
try {
|
|
213
|
-
|
|
214
|
-
return nowMs - stat.mtimeMs > PUBLIC_EVIDENCE_MUTATION_LOCK_INCOMPLETE_STALE_MS;
|
|
133
|
+
writeFileSync(path, "", { flag: "wx", mode: 0o600 });
|
|
215
134
|
} catch (error) {
|
|
216
|
-
if ((error as NodeJS.ErrnoException).code
|
|
217
|
-
throw error;
|
|
135
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
|
218
136
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
function recoverStaleReclaimClaim(lockPath: string, nowMs: number): boolean {
|
|
222
|
-
if (!reclaimClaimIsRecoverable(lockPath, nowMs)) return false;
|
|
223
|
-
const claimPath = mutationLockReclaimPath(lockPath);
|
|
224
|
-
const quarantinePath = join(lockPath, `.reclaim-stale-${randomUUID()}.json`);
|
|
137
|
+
const identity = mutationLockFileIdentity(path);
|
|
225
138
|
try {
|
|
226
|
-
|
|
227
|
-
} catch (error) {
|
|
228
|
-
if ((error as NodeJS.ErrnoException).code === "ENOENT") return true;
|
|
229
|
-
throw error;
|
|
230
|
-
}
|
|
231
|
-
unlinkIfPresent(quarantinePath);
|
|
232
|
-
return true;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
function tryAcquireReclaimClaim(
|
|
236
|
-
lockPath: string,
|
|
237
|
-
nowMs: number,
|
|
238
|
-
): MutationLockReclaim | null {
|
|
239
|
-
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
240
|
-
const claim: MutationLockReclaim = {
|
|
241
|
-
pid: process.pid,
|
|
242
|
-
token: randomUUID(),
|
|
243
|
-
createdAt: nowMs,
|
|
244
|
-
};
|
|
245
|
-
try {
|
|
246
|
-
writeFileSync(mutationLockReclaimPath(lockPath), JSON.stringify(claim), {
|
|
247
|
-
encoding: "utf8",
|
|
248
|
-
flag: "wx",
|
|
249
|
-
mode: 0o600,
|
|
250
|
-
});
|
|
251
|
-
return claim;
|
|
252
|
-
} catch (error) {
|
|
253
|
-
const code = (error as NodeJS.ErrnoException).code;
|
|
254
|
-
if (code === "ENOENT") return null;
|
|
255
|
-
if (code !== "EEXIST") throw error;
|
|
256
|
-
if (!recoverStaleReclaimClaim(lockPath, nowMs)) return null;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
return null;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
function reclaimClaimStillOwned(lockPath: string, claim: MutationLockReclaim): boolean {
|
|
263
|
-
const current = readMutationLockReclaim(lockPath);
|
|
264
|
-
return current?.pid === claim.pid && current.token === claim.token;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function releaseReclaimClaim(lockPath: string, claim: MutationLockReclaim): void {
|
|
268
|
-
if (!reclaimClaimStillOwned(lockPath, claim)) return;
|
|
269
|
-
unlinkIfPresent(mutationLockReclaimPath(lockPath));
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function tryReclaimMutationLock(lockPath: string, nowMs: number): boolean {
|
|
273
|
-
// Capture the exact stale directory before the claim write changes its mtime.
|
|
274
|
-
// After claiming, revalidate the inode and owner instead of reusing an age check
|
|
275
|
-
// that our own .reclaim.json creation would make appear fresh.
|
|
276
|
-
if (!mutationLockIsReclaimable(lockPath, nowMs)) return false;
|
|
277
|
-
const staleDirectory = currentDirectoryIdentity(lockPath);
|
|
278
|
-
const claim = tryAcquireReclaimClaim(lockPath, nowMs);
|
|
279
|
-
if (!claim) return false;
|
|
280
|
-
|
|
281
|
-
let moved = false;
|
|
282
|
-
try {
|
|
283
|
-
if (!reclaimClaimStillOwned(lockPath, claim)) return false;
|
|
284
|
-
if (!sameDirectoryIdentity(currentDirectoryIdentity(lockPath), staleDirectory)) return false;
|
|
285
|
-
const currentOwner = readMutationLockOwner(lockPath);
|
|
286
|
-
if (
|
|
287
|
-
currentOwner
|
|
288
|
-
&& !pidDefinitelyDead(currentOwner.pid)
|
|
289
|
-
&& !exceedsAbsoluteLockAge(currentOwner.createdAt, nowMs)
|
|
290
|
-
) return false;
|
|
291
|
-
if (!reclaimClaimStillOwned(lockPath, claim)) return false;
|
|
292
|
-
|
|
293
|
-
const quarantinePath = join(
|
|
294
|
-
dirname(lockPath),
|
|
295
|
-
`.mutation-lock-stale-${process.pid}-${randomUUID()}`,
|
|
296
|
-
);
|
|
297
|
-
try {
|
|
298
|
-
// Rename the exact claimed directory away from the canonical pathname before
|
|
299
|
-
// deleting it. A successor can create a new lock immediately afterwards, but
|
|
300
|
-
// cleanup is confined to this unique quarantine path and cannot delete it.
|
|
301
|
-
renameSync(lockPath, quarantinePath);
|
|
302
|
-
moved = true;
|
|
303
|
-
} catch (error) {
|
|
304
|
-
if ((error as NodeJS.ErrnoException).code === "ENOENT") return true;
|
|
305
|
-
throw error;
|
|
306
|
-
}
|
|
307
|
-
rmSync(quarantinePath, { recursive: true, force: true });
|
|
308
|
-
return true;
|
|
309
|
-
} finally {
|
|
310
|
-
if (!moved) releaseReclaimClaim(lockPath, claim);
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
function discardUncommittedMutationLock(
|
|
315
|
-
lockPath: string,
|
|
316
|
-
expectedDirectory: MutationLockDirectoryIdentity,
|
|
317
|
-
): void {
|
|
318
|
-
try {
|
|
319
|
-
if (!sameDirectoryIdentity(currentDirectoryIdentity(lockPath), expectedDirectory)) return;
|
|
320
|
-
const quarantinePath = join(
|
|
321
|
-
dirname(lockPath),
|
|
322
|
-
`.mutation-lock-release-${process.pid}-${randomUUID()}`,
|
|
323
|
-
);
|
|
324
|
-
renameSync(lockPath, quarantinePath);
|
|
325
|
-
rmSync(quarantinePath, { recursive: true, force: true });
|
|
139
|
+
return { database: new Database(path, { create: false, readwrite: true }), identity };
|
|
326
140
|
} catch {
|
|
327
|
-
//
|
|
328
|
-
|
|
141
|
+
// The SQLite error text can name local paths; the stable code is what callers act on.
|
|
142
|
+
throw new PublicEvidenceValidationError("community_cache_lock", "community cache mutation lock is unavailable");
|
|
329
143
|
}
|
|
330
144
|
}
|
|
331
145
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
} catch (error) {
|
|
341
|
-
if ((error as NodeJS.ErrnoException).code === "ENOENT") return;
|
|
342
|
-
throw error;
|
|
343
|
-
}
|
|
344
|
-
if (!sameDirectoryIdentity(currentDirectory, expectedDirectory)) return;
|
|
345
|
-
const current = readMutationLockOwner(lockPath);
|
|
346
|
-
if (!current || current.pid !== owner.pid || current.token !== owner.token) return;
|
|
347
|
-
|
|
348
|
-
const quarantinePath = join(
|
|
349
|
-
dirname(lockPath),
|
|
350
|
-
`.mutation-lock-release-${process.pid}-${randomUUID()}`,
|
|
351
|
-
);
|
|
352
|
-
try {
|
|
353
|
-
renameSync(lockPath, quarantinePath);
|
|
354
|
-
} catch (error) {
|
|
355
|
-
if ((error as NodeJS.ErrnoException).code === "ENOENT") return;
|
|
356
|
-
throw error;
|
|
357
|
-
}
|
|
358
|
-
rmSync(quarantinePath, { recursive: true, force: true });
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/** Serialize public-evidence mutations across CLI/server processes with ownership-safe stale recovery. */
|
|
146
|
+
/**
|
|
147
|
+
* Serialize public-evidence mutations across CLI/server processes.
|
|
148
|
+
*
|
|
149
|
+
* The lock is an OS-held SQLite write transaction on a dedicated database, with
|
|
150
|
+
* `busy_timeout=0` so a second holder fails fast as `community_cache_busy`. Closing the handle,
|
|
151
|
+
* including at process exit or crash, releases it, so there is no stale-owner recovery and no
|
|
152
|
+
* step that renames or deletes another process's lock state. Never return a Promise from `run`.
|
|
153
|
+
*/
|
|
362
154
|
export function withPublicEvidenceMutationLock<T>(
|
|
363
155
|
configDir: string | undefined,
|
|
364
156
|
run: () => T,
|
|
365
157
|
): T {
|
|
366
158
|
ensureLabDirs(configDir);
|
|
367
|
-
const
|
|
368
|
-
|
|
369
|
-
let
|
|
370
|
-
|
|
371
|
-
while (true) {
|
|
159
|
+
const path = mutationLockDatabasePath(configDir);
|
|
160
|
+
const { database, identity } = openMutationLockDatabase(path);
|
|
161
|
+
let transactionOpen = false;
|
|
162
|
+
try {
|
|
372
163
|
try {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
if ((reclaimError as NodeJS.ErrnoException).code === "ENOENT") continue;
|
|
380
|
-
throw reclaimError;
|
|
164
|
+
database.exec("PRAGMA busy_timeout = 0; BEGIN IMMEDIATE");
|
|
165
|
+
transactionOpen = true;
|
|
166
|
+
} catch (cause) {
|
|
167
|
+
const code = cause && typeof cause === "object" && "code" in cause ? String((cause as { code?: unknown }).code) : "";
|
|
168
|
+
if (code.startsWith("SQLITE_BUSY") || code.startsWith("SQLITE_LOCKED")) {
|
|
169
|
+
throw new PublicEvidenceValidationError("community_cache_busy", "community cache is busy");
|
|
381
170
|
}
|
|
382
|
-
throw new PublicEvidenceValidationError("
|
|
171
|
+
throw new PublicEvidenceValidationError("community_cache_lock", "community cache mutation lock is unavailable");
|
|
383
172
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
const candidate: MutationLockOwner = {
|
|
387
|
-
pid: process.pid,
|
|
388
|
-
token: randomUUID(),
|
|
389
|
-
createdAt: Date.now(),
|
|
390
|
-
};
|
|
391
|
-
try {
|
|
392
|
-
publishMutationLockOwner(lockPath, candidate, directory);
|
|
393
|
-
} catch (error) {
|
|
394
|
-
discardUncommittedMutationLock(lockPath, directory);
|
|
395
|
-
throw error;
|
|
173
|
+
if (!sameFileIdentity(mutationLockFileIdentity(path), identity)) {
|
|
174
|
+
throw new PublicEvidenceValidationError("community_cache_lock", "community cache mutation lock changed while opening");
|
|
396
175
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
try {
|
|
403
|
-
cleanupDetachedMutationLocks(lockPath);
|
|
176
|
+
const nowMs = Date.now();
|
|
177
|
+
if (legacyMutationLockBlocks(legacyMutationLockPath(configDir), nowMs)) {
|
|
178
|
+
throw new PublicEvidenceValidationError("community_cache_busy", "community cache is busy");
|
|
179
|
+
}
|
|
180
|
+
cleanupDetachedMutationLocks(configDir, nowMs);
|
|
404
181
|
return run();
|
|
405
182
|
} finally {
|
|
406
|
-
if (
|
|
183
|
+
if (transactionOpen) {
|
|
184
|
+
try { database.exec("ROLLBACK"); } catch { /* closing the handle below still releases the OS lock */ }
|
|
185
|
+
}
|
|
186
|
+
try { database.close(); } catch { /* the OS lock is released with the handle */ }
|
|
407
187
|
}
|
|
408
188
|
}
|
|
409
189
|
|
|
410
|
-
/** Test-only seam for
|
|
411
|
-
export function
|
|
412
|
-
configDir: string | undefined,
|
|
413
|
-
nowMs = Date.now(),
|
|
414
|
-
): boolean {
|
|
415
|
-
return mutationLockIsReclaimable(mutationLockPath(configDir), nowMs);
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
/** Test-only seam for the exclusive stale-reclaimer claim. */
|
|
419
|
-
export function publicEvidenceTryReclaimMutationLockForTests(
|
|
190
|
+
/** Test-only seam for the older-build lock policy. This module is not barrel-exported. */
|
|
191
|
+
export function publicEvidenceLegacyMutationLockBlocksForTests(
|
|
420
192
|
configDir: string | undefined,
|
|
421
193
|
nowMs = Date.now(),
|
|
422
194
|
): boolean {
|
|
423
|
-
return
|
|
195
|
+
return legacyMutationLockBlocks(legacyMutationLockPath(configDir), nowMs);
|
|
424
196
|
}
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
} from "node:fs";
|
|
13
13
|
import type { Stats } from "node:fs";
|
|
14
14
|
import { basename, dirname, join } from "node:path";
|
|
15
|
+
import { sameFileIdentity } from "../../lib/file-identity";
|
|
15
16
|
|
|
16
17
|
export type PrivateFileCommitFault = "before_publish" | "parent_directory_sync" | null;
|
|
17
18
|
let privateFileCommitFaultForTests: PrivateFileCommitFault = null;
|
|
@@ -152,7 +153,7 @@ export function cleanupStalePrivateFileStages(finalPath: string): void {
|
|
|
152
153
|
function cleanupPublishedPrivateFileStages(finalPath: string): void {
|
|
153
154
|
let finalStats;
|
|
154
155
|
try {
|
|
155
|
-
finalStats = lstatSync(finalPath);
|
|
156
|
+
finalStats = lstatSync(finalPath, { bigint: true });
|
|
156
157
|
} catch (error) {
|
|
157
158
|
if ((error as NodeJS.ErrnoException).code === "ENOENT") return;
|
|
158
159
|
throw error;
|
|
@@ -166,9 +167,9 @@ function cleanupPublishedPrivateFileStages(finalPath: string): void {
|
|
|
166
167
|
if (!name.startsWith(prefix) || !PRIVATE_STAGE_RE.test(name)) continue;
|
|
167
168
|
const stagePath = join(dir, name);
|
|
168
169
|
try {
|
|
169
|
-
const stageStats = lstatSync(stagePath);
|
|
170
|
+
const stageStats = lstatSync(stagePath, { bigint: true });
|
|
170
171
|
if (!stageStats.isFile() || stageStats.isSymbolicLink()) continue;
|
|
171
|
-
if (stageStats
|
|
172
|
+
if (!sameFileIdentity(stageStats, finalStats)) continue;
|
|
172
173
|
unlinkSync(stagePath);
|
|
173
174
|
changed = true;
|
|
174
175
|
} catch (error) {
|
|
@@ -9,7 +9,7 @@ export const RUNTIME_PREFLIGHT_TIMEOUT_MS = 5_000;
|
|
|
9
9
|
export const RUNTIME_PREFLIGHT_GUIDANCE = "The selected Bun runtime could not create and remove a directory in the OpenCodex config directory. "
|
|
10
10
|
+ "Windows application policy may deny writes from this executable location. "
|
|
11
11
|
+ "Set OPENCODEX_BUN_PATH to a trusted Bun executable allowed by your policy before launching ocx, "
|
|
12
|
-
+ "or reinstall opencodex with npm install -g @bitkyc08/opencodex, then retry. No alternate runtime was selected.";
|
|
12
|
+
+ "or reinstall opencodex with npm install -g --allow-scripts=bun @bitkyc08/opencodex, then retry. No alternate runtime was selected.";
|
|
13
13
|
|
|
14
14
|
export class RuntimePreflightError extends Error {
|
|
15
15
|
readonly code = "OCX_RUNTIME_PREFLIGHT_FAILED";
|