@onecraft/wallet 0.0.0-stage → 0.1.145
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/.claude-plugin/marketplace.json +20 -0
- package/.openclaw/AGENTS.md +100 -0
- package/.openclaw/extensions/agent-wallet/README.md +195 -0
- package/.openclaw/extensions/agent-wallet/dist/index.js +2198 -0
- package/.openclaw/extensions/agent-wallet/index.ts +2198 -0
- package/.openclaw/extensions/agent-wallet/openclaw.plugin.json +251 -0
- package/.openclaw/extensions/agent-wallet/package.json +49 -0
- package/.openclaw/extensions/agent-wallet/skills/cards/SKILL.md +211 -0
- package/.openclaw/extensions/agent-wallet/skills/wallet-operator/SKILL.md +30 -0
- package/CHANGELOG.md +1232 -0
- package/LICENSE +104 -0
- package/README.md +193 -2
- package/VERSION +1 -0
- package/agent-wallet/.env.example +63 -0
- package/agent-wallet/AGENTS.md +130 -0
- package/agent-wallet/CARD_VAULT.md +640 -0
- package/agent-wallet/README.md +615 -0
- package/agent-wallet/UPGRADE_COMPATIBILITY.md +66 -0
- package/agent-wallet/agent_wallet/__init__.py +16 -0
- package/agent-wallet/agent_wallet/approval.py +165 -0
- package/agent-wallet/agent_wallet/autonomous_permissions.py +281 -0
- package/agent-wallet/agent_wallet/autonomous_policy.py +305 -0
- package/agent-wallet/agent_wallet/autonomous_session.py +236 -0
- package/agent-wallet/agent_wallet/boot_key_migration.py +155 -0
- package/agent-wallet/agent_wallet/boot_key_recovery.py +57 -0
- package/agent-wallet/agent_wallet/bootstrap.py +194 -0
- package/agent-wallet/agent_wallet/card_catalog.py +243 -0
- package/agent-wallet/agent_wallet/card_runtime_secrets.py +108 -0
- package/agent-wallet/agent_wallet/card_vault.py +331 -0
- package/agent-wallet/agent_wallet/card_vault_cli.py +174 -0
- package/agent-wallet/agent_wallet/card_vault_import.py +65 -0
- package/agent-wallet/agent_wallet/card_vault_keychain.py +239 -0
- package/agent-wallet/agent_wallet/config.py +719 -0
- package/agent-wallet/agent_wallet/connector_cli.py +198 -0
- package/agent-wallet/agent_wallet/connectors/__init__.py +28 -0
- package/agent-wallet/agent_wallet/connectors/catalog.py +68 -0
- package/agent-wallet/agent_wallet/connectors/client.py +308 -0
- package/agent-wallet/agent_wallet/connectors/intent_policy.py +243 -0
- package/agent-wallet/agent_wallet/connectors/manifest.py +192 -0
- package/agent-wallet/agent_wallet/connectors/registry.py +341 -0
- package/agent-wallet/agent_wallet/encrypted_storage.py +314 -0
- package/agent-wallet/agent_wallet/evm_user_wallets.py +910 -0
- package/agent-wallet/agent_wallet/exceptions.py +10 -0
- package/agent-wallet/agent_wallet/file_ops.py +34 -0
- package/agent-wallet/agent_wallet/http_client.py +25 -0
- package/agent-wallet/agent_wallet/kernel_browser.py +479 -0
- package/agent-wallet/agent_wallet/kernel_cli.py +57 -0
- package/agent-wallet/agent_wallet/kernel_gateway.py +95 -0
- package/agent-wallet/agent_wallet/keystore.py +503 -0
- package/agent-wallet/agent_wallet/laso_card_vault.py +246 -0
- package/agent-wallet/agent_wallet/models.py +66 -0
- package/agent-wallet/agent_wallet/morpho_monitor.py +298 -0
- package/agent-wallet/agent_wallet/nonce_registry.py +59 -0
- package/agent-wallet/agent_wallet/openclaw_adapter.py +9129 -0
- package/agent-wallet/agent_wallet/openclaw_cli.py +658 -0
- package/agent-wallet/agent_wallet/openclaw_runtime.py +194 -0
- package/agent-wallet/agent_wallet/plugin_bundle.py +44 -0
- package/agent-wallet/agent_wallet/providers/__init__.py +1 -0
- package/agent-wallet/agent_wallet/providers/bags.py +202 -0
- package/agent-wallet/agent_wallet/providers/dexscreener.py +147 -0
- package/agent-wallet/agent_wallet/providers/evm_activity.py +257 -0
- package/agent-wallet/agent_wallet/providers/evm_portfolio.py +459 -0
- package/agent-wallet/agent_wallet/providers/imd.py +720 -0
- package/agent-wallet/agent_wallet/providers/jupiter.py +519 -0
- package/agent-wallet/agent_wallet/providers/kamino.py +404 -0
- package/agent-wallet/agent_wallet/providers/lifi.py +302 -0
- package/agent-wallet/agent_wallet/providers/solana_rpc.py +447 -0
- package/agent-wallet/agent_wallet/providers/tokenized_stocks.py +189 -0
- package/agent-wallet/agent_wallet/providers/wdk_evm_local.py +346 -0
- package/agent-wallet/agent_wallet/providers/x402.py +1972 -0
- package/agent-wallet/agent_wallet/sealed_keys.py +118 -0
- package/agent-wallet/agent_wallet/solana_stake.py +103 -0
- package/agent-wallet/agent_wallet/solana_tx.py +93 -0
- package/agent-wallet/agent_wallet/spending_limits.py +119 -0
- package/agent-wallet/agent_wallet/telemetry.py +481 -0
- package/agent-wallet/agent_wallet/transaction_policy.py +573 -0
- package/agent-wallet/agent_wallet/update_check.py +190 -0
- package/agent-wallet/agent_wallet/user_wallets.py +573 -0
- package/agent-wallet/agent_wallet/validation.py +31 -0
- package/agent-wallet/agent_wallet/vault_executor.py +456 -0
- package/agent-wallet/agent_wallet/wallet_layer/__init__.py +1 -0
- package/agent-wallet/agent_wallet/wallet_layer/base.py +1204 -0
- package/agent-wallet/agent_wallet/wallet_layer/base58.py +44 -0
- package/agent-wallet/agent_wallet/wallet_layer/factory.py +117 -0
- package/agent-wallet/agent_wallet/wallet_layer/solana.py +5246 -0
- package/agent-wallet/agent_wallet/wallet_layer/wdk_evm.py +2705 -0
- package/agent-wallet/examples/bootstrap_wallet.py +21 -0
- package/agent-wallet/examples/openclaw_runtime_onboarding.py +28 -0
- package/agent-wallet/examples/openclaw_user_wallet_example.py +31 -0
- package/agent-wallet/examples/openclaw_wallet_adapter_example.py +33 -0
- package/agent-wallet/openclaw.plugin.json +118 -0
- package/agent-wallet/pyproject.toml +36 -0
- package/agent-wallet/scripts/bootstrap_openclaw_evm.py +312 -0
- package/agent-wallet/scripts/build_release_bundle.py +210 -0
- package/agent-wallet/scripts/finalize_openclaw_local_wallet_config.py +121 -0
- package/agent-wallet/scripts/install_agent_wallet.py +1357 -0
- package/agent-wallet/scripts/install_openclaw_local_config.py +421 -0
- package/agent-wallet/scripts/install_openclaw_sealed_keys.py +117 -0
- package/agent-wallet/scripts/manage_openclaw_evm_wallet.py +333 -0
- package/agent-wallet/scripts/security_utils.py +37 -0
- package/agent-wallet/scripts/setup_evm_wallet.sh +140 -0
- package/agent-wallet/scripts/switch_openclaw_wallet_network.py +109 -0
- package/agent-wallet/skills/wallet-operator/SKILL.md +157 -0
- package/bin/lib/boot-key.mjs +184 -0
- package/bin/lib/evm-daemon.mjs +389 -0
- package/bin/lib/host-detection.mjs +259 -0
- package/bin/lib/integrations.mjs +578 -0
- package/bin/lib/release-python.mjs +9 -0
- package/bin/lib/update-transaction.mjs +235 -0
- package/bin/openclaw-agent-wallet.mjs +3354 -0
- package/claude-code/plugins/agent-wallet/.claude-plugin/plugin.json +19 -0
- package/claude-code/plugins/agent-wallet/.mcp.json +14 -0
- package/claude-code/plugins/agent-wallet/AGENTLAYER_AGENT_GUIDE.md +282 -0
- package/claude-code/plugins/agent-wallet/README.md +134 -0
- package/claude-code/plugins/agent-wallet/commands/agentlayer-autonomous-approve.md +39 -0
- package/claude-code/plugins/agent-wallet/commands/agentlayer-autonomous-revoke.md +17 -0
- package/claude-code/plugins/agent-wallet/commands/cards.md +129 -0
- package/claude-code/plugins/agent-wallet/commands/guide.md +40 -0
- package/claude-code/plugins/agent-wallet/commands/imd.md +125 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-base.md +60 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-ethereum.md +37 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-evm.md +39 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-setup.md +25 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-sol.md +51 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-statusline.md +34 -0
- package/claude-code/plugins/agent-wallet/commands/x402.md +121 -0
- package/claude-code/plugins/agent-wallet/commands/yield.md +62 -0
- package/claude-code/plugins/agent-wallet/hooks/hooks.json +16 -0
- package/claude-code/plugins/agent-wallet/scripts/bootstrap_backend.sh +136 -0
- package/claude-code/plugins/agent-wallet/scripts/enable_statusline.py +52 -0
- package/claude-code/plugins/agent-wallet/scripts/resolve_wallet_addresses.py +69 -0
- package/claude-code/plugins/agent-wallet/scripts/run_mcp.sh +66 -0
- package/claude-code/plugins/agent-wallet/scripts/wallet_statusline.py +130 -0
- package/claude-code/plugins/agent-wallet/scripts/wallet_statusline_setup.sh +40 -0
- package/claude-code/plugins/agent-wallet/skills/wallet-operator/SKILL.md +21 -0
- package/codex/plugins/agent-wallet/.codex-plugin/plugin.json +37 -0
- package/codex/plugins/agent-wallet/.mcp.json +15 -0
- package/codex/plugins/agent-wallet/README.md +158 -0
- package/codex/plugins/agent-wallet/scripts/run_mcp.sh +47 -0
- package/codex/plugins/agent-wallet/server.py +1797 -0
- package/codex/plugins/agent-wallet/skills/cards/SKILL.md +225 -0
- package/codex/plugins/agent-wallet/skills/imd/SKILL.md +145 -0
- package/codex/plugins/agent-wallet/skills/wallet-base/SKILL.md +56 -0
- package/codex/plugins/agent-wallet/skills/wallet-operator/SKILL.md +85 -0
- package/codex/plugins/agent-wallet/skills/wallet-sol/SKILL.md +29 -0
- package/codex/plugins/agent-wallet/skills/x402/SKILL.md +159 -0
- package/codex/plugins/agent-wallet/skills/yield/SKILL.md +65 -0
- package/connectors/AGENTS.md +37 -0
- package/connectors/DEVELOPER_GUIDE.md +131 -0
- package/connectors/README.md +139 -0
- package/connectors/READ_ONLY_BETA.md +54 -0
- package/connectors/RELEASING.md +48 -0
- package/connectors/conformance/README.md +31 -0
- package/connectors/conformance/package.json +54 -0
- package/connectors/conformance/scripts/sync-license.mjs +3 -0
- package/connectors/conformance/scripts/sync-spec.mjs +16 -0
- package/connectors/conformance/src/cli.ts +43 -0
- package/connectors/conformance/src/index.ts +7 -0
- package/connectors/conformance/src/runner.ts +314 -0
- package/connectors/conformance/src/types.ts +27 -0
- package/connectors/conformance/test/fixtures/invalid-response-identity.json +14 -0
- package/connectors/conformance/test/fixtures/missing-tool-fixture.json +4 -0
- package/connectors/conformance/test/runner.test.mjs +151 -0
- package/connectors/conformance/tsconfig.json +17 -0
- package/connectors/examples/reference-crypto-data/Dockerfile +21 -0
- package/connectors/examples/reference-crypto-data/README.md +51 -0
- package/connectors/examples/reference-crypto-data/conformance.json +11 -0
- package/connectors/examples/reference-crypto-data/connector.json +95 -0
- package/connectors/examples/reference-crypto-data/package.json +21 -0
- package/connectors/examples/reference-crypto-data/railway.toml +9 -0
- package/connectors/examples/reference-crypto-data/src/connector.ts +137 -0
- package/connectors/examples/reference-crypto-data/src/server.ts +13 -0
- package/connectors/examples/reference-crypto-data/test/connector.test.mjs +97 -0
- package/connectors/examples/reference-crypto-data/tsconfig.json +17 -0
- package/connectors/package-lock.json +561 -0
- package/connectors/package.json +19 -0
- package/connectors/scripts/smoke-packed-packages.mjs +121 -0
- package/connectors/sdk-typescript/README.md +47 -0
- package/connectors/sdk-typescript/package.json +49 -0
- package/connectors/sdk-typescript/scripts/sync-license.mjs +3 -0
- package/connectors/sdk-typescript/src/connector.ts +204 -0
- package/connectors/sdk-typescript/src/errors.ts +11 -0
- package/connectors/sdk-typescript/src/http.ts +116 -0
- package/connectors/sdk-typescript/src/index.ts +26 -0
- package/connectors/sdk-typescript/src/types.ts +101 -0
- package/connectors/sdk-typescript/test/sdk.test.mjs +181 -0
- package/connectors/sdk-typescript/tsconfig.json +21 -0
- package/connectors/spec/connector-manifest.schema.json +340 -0
- package/connectors/spec/connector-protocol.md +159 -0
- package/connectors/spec/transaction-intent.schema.json +281 -0
- package/connectors/templates/read-only/.env.example +1 -0
- package/connectors/templates/read-only/Dockerfile +11 -0
- package/connectors/templates/read-only/README.md +33 -0
- package/connectors/templates/read-only/conformance.json +11 -0
- package/connectors/templates/read-only/connector.json +64 -0
- package/connectors/templates/read-only/package.json +21 -0
- package/connectors/templates/read-only/railway.toml +9 -0
- package/connectors/templates/read-only/src/connector.ts +61 -0
- package/connectors/templates/read-only/src/server.ts +14 -0
- package/connectors/templates/read-only/test/connector.test.mjs +62 -0
- package/connectors/templates/read-only/tsconfig.json +17 -0
- package/cursor/plugins/agent-wallet/.cursor-plugin/plugin.json +14 -0
- package/cursor/plugins/agent-wallet/README.md +62 -0
- package/cursor/plugins/agent-wallet/mcp.json +16 -0
- package/cursor/plugins/agent-wallet/scripts/run_mcp.sh +33 -0
- package/cursor/plugins/agent-wallet/server.py +33 -0
- package/cursor/plugins/agent-wallet/skills/cards/SKILL.md +210 -0
- package/cursor/plugins/agent-wallet/skills/wallet-base/SKILL.md +56 -0
- package/cursor/plugins/agent-wallet/skills/wallet-operator/SKILL.md +23 -0
- package/cursor/plugins/agent-wallet/skills/wallet-sol/SKILL.md +29 -0
- package/cursor/plugins/agent-wallet/skills/x402/SKILL.md +130 -0
- package/hermes/plugins/agent_wallet/README.md +54 -0
- package/hermes/plugins/agent_wallet/__init__.py +55 -0
- package/hermes/plugins/agent_wallet/plugin.yaml +9 -0
- package/hermes/plugins/agent_wallet/schemas.py +211 -0
- package/hermes/plugins/agent_wallet/tools.py +622 -0
- package/install-from-github.sh +139 -0
- package/integrations/hedera/treasury-guard/README.md +92 -0
- package/integrations/hedera/treasury-guard/examples/treasury-policy.example.json +9 -0
- package/integrations/hedera/treasury-guard/package-lock.json +880 -0
- package/integrations/hedera/treasury-guard/package.json +8 -0
- package/integrations/hedera/treasury-guard/skills/onecraft-hedera-treasury/SKILL.md +22 -0
- package/integrations/hedera/treasury-guard/src/hedera-mirror-client.mjs +106 -0
- package/integrations/hedera/treasury-guard/src/hedera-signer.mjs +120 -0
- package/integrations/hedera/treasury-guard/src/treasury-cli.mjs +177 -0
- package/integrations/hedera/treasury-guard/src/treasury-policy.mjs +155 -0
- package/mcp/README.md +39 -0
- package/mcp/scripts/run_mcp.sh +37 -0
- package/mcp/server.py +33 -0
- package/package.json +106 -4
- package/scripts/check_release_version.mjs +72 -0
- package/scripts/version_targets.mjs +60 -0
- package/setup.sh +87 -0
- package/wdk-evm-wallet/.env.example +38 -0
- package/wdk-evm-wallet/README.md +254 -0
- package/wdk-evm-wallet/bootstrap.sh +8 -0
- package/wdk-evm-wallet/package-lock.json +2359 -0
- package/wdk-evm-wallet/package.json +43 -0
- package/wdk-evm-wallet/run-local.sh +12 -0
- package/wdk-evm-wallet/src/config.js +461 -0
- package/wdk-evm-wallet/src/imd.js +231 -0
- package/wdk-evm-wallet/src/json.js +35 -0
- package/wdk-evm-wallet/src/local_vault.js +407 -0
- package/wdk-evm-wallet/src/network_state.js +96 -0
- package/wdk-evm-wallet/src/pending_transactions.js +148 -0
- package/wdk-evm-wallet/src/server.js +849 -0
- package/wdk-evm-wallet/src/shutdown.js +63 -0
- package/wdk-evm-wallet/src/wdk_evm_wallet.js +10036 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
function readJson(pathname) {
|
|
7
|
+
try {
|
|
8
|
+
return JSON.parse(fs.readFileSync(pathname, "utf8"));
|
|
9
|
+
} catch (error) {
|
|
10
|
+
if (error?.code === "ENOENT") return null;
|
|
11
|
+
throw error;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function writeJsonAtomic(pathname, value) {
|
|
16
|
+
fs.mkdirSync(path.dirname(pathname), { recursive: true });
|
|
17
|
+
const temporary = `${pathname}.tmp-${process.pid}-${Date.now()}`;
|
|
18
|
+
try {
|
|
19
|
+
fs.writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });
|
|
20
|
+
fs.renameSync(temporary, pathname);
|
|
21
|
+
fs.chmodSync(pathname, 0o600);
|
|
22
|
+
} finally {
|
|
23
|
+
fs.rmSync(temporary, { force: true });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function uniquePath(target) {
|
|
28
|
+
if (!fs.existsSync(target)) return target;
|
|
29
|
+
let counter = 2;
|
|
30
|
+
while (fs.existsSync(`${target}-${counter}`)) counter += 1;
|
|
31
|
+
return `${target}-${counter}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function readLink(target) {
|
|
35
|
+
try {
|
|
36
|
+
return fs.lstatSync(target).isSymbolicLink() ? fs.readlinkSync(target) : null;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (error?.code === "ENOENT") return null;
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function processIsRunning(pid) {
|
|
44
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
45
|
+
try {
|
|
46
|
+
process.kill(pid, 0);
|
|
47
|
+
return true;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
return error?.code !== "ESRCH";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function createUpdateTransactionManager({ runtimeBase, packageVersion, env = process.env }) {
|
|
54
|
+
const base = path.resolve(runtimeBase);
|
|
55
|
+
const journalPath = path.join(base, "update-journal.json");
|
|
56
|
+
const lockPath = path.join(base, "update.lock");
|
|
57
|
+
|
|
58
|
+
function readJournal() {
|
|
59
|
+
try {
|
|
60
|
+
return readJson(journalPath);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
return { schema_version: 1, state: "corrupt", error: error.message };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function writeJournal(state, details = {}) {
|
|
67
|
+
writeJsonAtomic(journalPath, {
|
|
68
|
+
...details,
|
|
69
|
+
schema_version: 2,
|
|
70
|
+
state,
|
|
71
|
+
version: details.version || packageVersion,
|
|
72
|
+
updated_at: new Date().toISOString(),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function runtimeOwned(candidate) {
|
|
77
|
+
if (!candidate) return false;
|
|
78
|
+
const relative = path.relative(base, path.resolve(candidate));
|
|
79
|
+
return relative !== "" && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function interruptedPath(version) {
|
|
83
|
+
return uniquePath(
|
|
84
|
+
path.join(base, "releases", `.interrupted-${version || "unknown"}-${Date.now()}`),
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function acquireLock(allowStaleRetry = true) {
|
|
89
|
+
const ownerPath = path.join(lockPath, "owner.json");
|
|
90
|
+
const token = crypto.randomUUID();
|
|
91
|
+
fs.mkdirSync(base, { recursive: true });
|
|
92
|
+
try {
|
|
93
|
+
fs.mkdirSync(lockPath);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error?.code !== "EEXIST") throw error;
|
|
96
|
+
let owner = null;
|
|
97
|
+
try {
|
|
98
|
+
owner = readJson(ownerPath);
|
|
99
|
+
} catch {
|
|
100
|
+
// An unreadable lock is not safe to steal automatically.
|
|
101
|
+
}
|
|
102
|
+
const stale = owner?.hostname === os.hostname() && !processIsRunning(Number(owner.pid));
|
|
103
|
+
if (stale && allowStaleRetry) {
|
|
104
|
+
fs.rmSync(lockPath, { recursive: true, force: true });
|
|
105
|
+
return acquireLock(false);
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
ok: false,
|
|
109
|
+
path: lockPath,
|
|
110
|
+
owner: owner ? { pid: owner.pid, hostname: owner.hostname, started_at: owner.started_at } : null,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
writeJsonAtomic(ownerPath, {
|
|
115
|
+
schema_version: 1,
|
|
116
|
+
pid: process.pid,
|
|
117
|
+
hostname: os.hostname(),
|
|
118
|
+
token,
|
|
119
|
+
started_at: new Date().toISOString(),
|
|
120
|
+
});
|
|
121
|
+
} catch (error) {
|
|
122
|
+
fs.rmSync(lockPath, { recursive: true, force: true });
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
return { ok: true, path: lockPath, owner_path: ownerPath, token };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function releaseLock(lock) {
|
|
129
|
+
if (!lock?.ok) return;
|
|
130
|
+
try {
|
|
131
|
+
const owner = readJson(lock.owner_path);
|
|
132
|
+
if (owner?.token === lock.token) fs.rmSync(lock.path, { recursive: true, force: true });
|
|
133
|
+
} catch {
|
|
134
|
+
// Never remove a lock whose ownership can no longer be verified.
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function recover() {
|
|
139
|
+
const journal = readJournal();
|
|
140
|
+
if (!journal || ["committed", "failed", "recovered"].includes(journal.state)) {
|
|
141
|
+
return { attempted: false, ok: true, reason: "no interrupted update" };
|
|
142
|
+
}
|
|
143
|
+
if (journal.state === "corrupt") {
|
|
144
|
+
return { attempted: false, ok: false, reason: "update journal is corrupt", error: journal.error };
|
|
145
|
+
}
|
|
146
|
+
const paths = [journal.staging_root, journal.release_root, journal.replaced_root].filter(Boolean);
|
|
147
|
+
if (paths.some((candidate) => !runtimeOwned(candidate))) {
|
|
148
|
+
return { attempted: false, ok: false, reason: "update journal contains an unsafe path" };
|
|
149
|
+
}
|
|
150
|
+
if (journal.state === "committing") {
|
|
151
|
+
const releaseExists = Boolean(journal.release_root && fs.existsSync(journal.release_root));
|
|
152
|
+
const replacedExists = Boolean(journal.replaced_root && fs.existsSync(journal.replaced_root));
|
|
153
|
+
if (!releaseExists && replacedExists) {
|
|
154
|
+
fs.renameSync(journal.replaced_root, journal.release_root);
|
|
155
|
+
writeJournal("recovered", { ...journal, action: "restored_replaced_release" });
|
|
156
|
+
return { attempted: true, ok: true, action: "restored_replaced_release" };
|
|
157
|
+
}
|
|
158
|
+
if (releaseExists) {
|
|
159
|
+
writeJournal("recovered", { ...journal, action: "release_already_present" });
|
|
160
|
+
return { attempted: true, ok: true, action: "release_already_present" };
|
|
161
|
+
}
|
|
162
|
+
return { attempted: true, ok: false, reason: "interrupted commit has no recoverable release" };
|
|
163
|
+
}
|
|
164
|
+
if (["preparing", "verified"].includes(journal.state)) {
|
|
165
|
+
let quarantined = null;
|
|
166
|
+
if (journal.staging_root && fs.existsSync(journal.staging_root)) {
|
|
167
|
+
quarantined = interruptedPath(journal.version);
|
|
168
|
+
fs.renameSync(journal.staging_root, quarantined);
|
|
169
|
+
}
|
|
170
|
+
writeJournal("recovered", {
|
|
171
|
+
...journal,
|
|
172
|
+
action: "quarantined_incomplete_staging",
|
|
173
|
+
quarantined_runtime: quarantined,
|
|
174
|
+
});
|
|
175
|
+
return {
|
|
176
|
+
attempted: true,
|
|
177
|
+
ok: true,
|
|
178
|
+
action: "quarantined_incomplete_staging",
|
|
179
|
+
quarantined_runtime: quarantined,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
return { attempted: false, ok: false, reason: `unsupported update journal state: ${journal.state}` };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function status() {
|
|
186
|
+
const journal = readJournal();
|
|
187
|
+
const state = journal?.state || null;
|
|
188
|
+
const currentPath = path.join(base, "current");
|
|
189
|
+
const currentTarget = readLink(currentPath);
|
|
190
|
+
const currentResolves = Boolean(
|
|
191
|
+
currentTarget && fs.existsSync(path.resolve(path.dirname(currentPath), currentTarget)),
|
|
192
|
+
);
|
|
193
|
+
let lockOwner = null;
|
|
194
|
+
try {
|
|
195
|
+
const owner = readJson(path.join(lockPath, "owner.json"));
|
|
196
|
+
if (owner) {
|
|
197
|
+
lockOwner = {
|
|
198
|
+
pid: owner.pid,
|
|
199
|
+
hostname: owner.hostname,
|
|
200
|
+
started_at: owner.started_at,
|
|
201
|
+
active: owner.hostname === os.hostname() ? processIsRunning(Number(owner.pid)) : null,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
} catch {
|
|
205
|
+
if (fs.existsSync(lockPath)) lockOwner = { unreadable: true };
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
journal_schema_version: journal?.schema_version || null,
|
|
209
|
+
state,
|
|
210
|
+
journal_ok: state !== "corrupt",
|
|
211
|
+
needs_recovery: Boolean(state && !["committed", "failed", "recovered"].includes(state)),
|
|
212
|
+
current_resolves: currentTarget ? currentResolves : null,
|
|
213
|
+
lock: lockOwner,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function holdLockForTest() {
|
|
218
|
+
const milliseconds = Number.parseInt(String(env.AGENT_WALLET_TEST_HOLD_LOCK_MS || ""), 10);
|
|
219
|
+
if (Number.isFinite(milliseconds) && milliseconds > 0) {
|
|
220
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return {
|
|
225
|
+
journalPath,
|
|
226
|
+
lockPath,
|
|
227
|
+
readJournal,
|
|
228
|
+
writeJournal,
|
|
229
|
+
acquireLock,
|
|
230
|
+
releaseLock,
|
|
231
|
+
recover,
|
|
232
|
+
status,
|
|
233
|
+
holdLockForTest,
|
|
234
|
+
};
|
|
235
|
+
}
|