Skip to content

Commit 72e3c44

Browse files
bkudiessCopilot
andcommitted
Fix device identity load failures
Co-authored-by: Copilot App <[email protected]>
1 parent fe43b15 commit 72e3c44

28 files changed

Lines changed: 2136 additions & 190 deletions

‎docs/CONNECTION_ARCHITECTURE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ The invariant is that a paired device token always wins. Do not downgrade a pair
183183

184184
**`CredentialResolver`** implements the precedence for WebSocket connections (operator and node roles). It also returns a detailed `GatewayCredentialResolution` so the active snapshot and diagnostics can distinguish `Resolved`, `Missing`, `Unreadable`, `Corrupt`, `FallbackUsed`, and `BootstrapRequired`. Shared-token-only gateways are a clean resolved state when no paired device token exists. If a stored per-gateway device token is unreadable or corrupt and the resolver falls back to a shared/bootstrap token, `GatewayConnectionSnapshot` preserves that fallback status instead of reporting only the token source.
185185

186-
Unreadable/corrupt identity fallback is an explicit same-gateway recovery path, not a silent cross-gateway downgrade. A readable stored device token still always wins. When the per-gateway identity file cannot be read or parsed, fallback may use only credentials already stored on the same active `GatewayRecord`; the snapshot and diagnostics report `FallbackUsed` with `PrimaryStatus=Unreadable` or `Corrupt` so UI/diagnostics can prompt repair or re-pair. Credential reads never fall back to another gateway's identity directory.
186+
Unreadable/corrupt identity fallback is a credential-resolution diagnostic, not permission to replace an existing keypair. A readable stored device token still always wins. When the per-gateway identity file cannot be read or parsed, resolution may identify a same-gateway shared/bootstrap credential, but gateway client construction fails closed until the persisted identity is readable or the user explicitly resets pairing. OpenClaw never regenerates, overwrites, or otherwise changes the identity path on a load failure. The snapshot and diagnostics report the persisted-identity error so UI and diagnostics can prompt repair or explicit re-pair. Credential reads never fall back to another gateway's identity directory.
187187

188188
Node credential precedence follows the same invariant with a distinct stored token:
189189

‎docs/WINDOWS_NODE_TESTING.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ Local MCP clients also see MCP-only `app.*` commands such as `app.navigate`, `ap
136136
### Node doesn't connect
137137
- Check the active gateway in Connection settings. Gateway records live in `%APPDATA%\OpenClawTray\gateways.json`; post-pairing device tokens live under `%APPDATA%\OpenClawTray\gateways\<gateway-id>\device-key-ed25519.json`.
138138
- Check logs for connection errors
139+
- If logs report that the saved device identity could not be loaded, fix access to the existing identity file or use an explicit reset/re-pair action. The tray preserves an unreadable or corrupt identity instead of replacing it automatically.
139140
- Verify gateway is running and accessible
140141
- If only a bootstrap token exists, finish pairing or approve the device; paired device tokens take precedence on future connects.
141142

‎openclaw-windows-node.slnx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
</Folder>
3131
<Folder Name="/tests/">
3232
<Project Path="tests/OpenClaw.TestSupport/OpenClaw.TestSupport.csproj" />
33+
<Project Path="tests/OpenClaw.Shared.TestHost/OpenClaw.Shared.TestHost.csproj" />
3334
<Project Path="tests/OpenClaw.Shared.Tests/OpenClaw.Shared.Tests.csproj" />
3435
<Project Path="tests/OpenClaw.Connection.Tests/OpenClaw.Connection.Tests.csproj" />
3536
<Project Path="tests/OpenClaw.WinNode.Cli.Tests/OpenClaw.WinNode.Cli.Tests.csproj" />

‎src/OpenClaw.Cli/Program.cs‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ internal sealed class CliOptions
1919
public bool Verbose { get; set; }
2020
}
2121

22+
internal sealed class DeferredLogger(IOpenClawLogger inner) : IOpenClawLogger
23+
{
24+
private bool _enabled;
25+
26+
public void Enable() => _enabled = true;
27+
public void Info(string message) { if (_enabled) inner.Info(message); }
28+
public void Debug(string message) { if (_enabled) inner.Debug(message); }
29+
public void Warn(string message) { if (_enabled) inner.Warn(message); }
30+
public void Error(string message, Exception? ex = null) { if (_enabled) inner.Error(message, ex); }
31+
public void Trace(string message) { if (_enabled) inner.Trace(message); }
32+
}
33+
2234
internal static class Program
2335
{
2436
private static async Task<int> Main(string[] args)
@@ -55,6 +67,30 @@ private static async Task<int> Main(string[] args)
5567
return 2;
5668
}
5769

70+
var deferredLogger = options.Verbose
71+
? new DeferredLogger(new ConsoleLogger())
72+
: null;
73+
IOpenClawLogger logger = deferredLogger is null
74+
? NullLogger.Instance
75+
: deferredLogger;
76+
OpenClawGatewayClient client;
77+
try
78+
{
79+
client = new OpenClawGatewayClient(
80+
gatewayUrl,
81+
token,
82+
logger,
83+
identityPath: options.IdentityDataPath);
84+
}
85+
catch (DeviceIdentityLoadException)
86+
{
87+
Console.Error.WriteLine(DeviceIdentityLoadException.RecoveryMessage);
88+
return 1;
89+
}
90+
91+
using var clientLifetime = client;
92+
deferredLogger?.Enable();
93+
5894
Console.WriteLine($"Settings file: {options.SettingsPath}");
5995
Console.WriteLine($"Gateway URL: {GatewayUrlHelper.SanitizeForDisplay(gatewayUrl)}");
6096
Console.WriteLine($"Token source: {(options.TokenOverride is null ? "settings" : "--token override")}");
@@ -64,13 +100,6 @@ private static async Task<int> Main(string[] args)
64100
Console.WriteLine($"SSH tunnel in settings: {loaded.UseSshTunnel} (local port {loaded.SshTunnelLocalPort})");
65101
}
66102

67-
IOpenClawLogger logger = options.Verbose ? new ConsoleLogger() : NullLogger.Instance;
68-
using var client = new OpenClawGatewayClient(
69-
gatewayUrl,
70-
token,
71-
logger,
72-
identityPath: options.IdentityDataPath);
73-
74103
var lastStatus = ConnectionStatus.Disconnected;
75104
var connectedTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
76105
var errorTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

‎src/OpenClaw.Connection/DeviceIdentityStore.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public void StoreToken(string identityPath, string token, string[]? scopes, stri
2424
identity.StoreDeviceTokenForRole(role, token, scopes);
2525
_logger.Info($"[IdentityStore] Stored {role} device token at {identityPath}");
2626
}
27+
catch (DeviceIdentityLoadException)
28+
{
29+
throw;
30+
}
2731
catch (Exception ex)
2832
{
2933
_logger.Error($"[IdentityStore] Failed to store {role} device token: {ex.Message}");

0 commit comments

Comments
 (0)