Repository navigation
Expand file tree
/
Copy pathSessionCompanionConfig.cs
More file actions
332 lines (312 loc) · 15 KB
/
Copy pathSessionCompanionConfig.cs
File metadata and controls
332 lines (312 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System.Security.Cryptography;
using System.Text.Json;
using System.Text.Json.Serialization;
using OpenClaw.SessionProtocol;
namespace OpenClaw.SessionHost;
internal sealed record CompanionConfigPatch(
CompanionGatewayPatch Gateway, JsonElement ExpectedGateway, string ExpectedHash, string ExpectedPath);
internal sealed record CompanionGatewayPatch(string Mode, int Port, string Bind, CompanionAuthPatch Auth);
internal sealed record CompanionAuthPatch(string Mode, string Token);
internal sealed record CompanionConfigSnapshot(JsonElement Gateway, string Path, string Hash, bool Exists);
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(CompanionConfigPatch))]
[JsonSerializable(typeof(CompanionConfigSnapshot))]
internal sealed partial class CompanionConfigPatchContext : JsonSerializerContext;
internal static class SessionCompanionConfig
{
private const string ConfigurationOperation = """
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { isDeepStrictEqual } from "node:util";
const [applicationDirectory, expectedPath, patchPath] = process.argv.slice(1);
const sdkUrl = (name) => pathToFileURL(
resolve(applicationDirectory, "dist", "plugin-sdk", `${name}.js`),
).href;
const assertEnvironment = () => {
for (const name of [
"OPENCLAW_CONFIG_PATH", "OPENCLAW_STATE_DIR", "OPENCLAW_HOME", "OPENCLAW_PROFILE",
"OPENCLAW_GATEWAY_URL", "OPENCLAW_GATEWAY_PORT", "OPENCLAW_GATEWAY_TOKEN",
"OPENCLAW_GATEWAY_PASSWORD",
]) {
if (process.env[name]?.trim()) {
throw new Error(`The agent account has a ${name} override. Remove it before preparing Companion.`);
}
}
};
const assertSnapshot = (snapshot) => {
assertEnvironment();
if (!snapshot.valid ||
resolve(snapshot.path).toLowerCase() !== resolve(expectedPath).toLowerCase()) {
throw new Error("The agent Gateway configuration is invalid or resolves to another profile.");
}
};
assertEnvironment();
if (patchPath) {
const patch = JSON.parse(await readFile(patchPath, "utf8"));
const { mutateConfigFile } = await import(sdkUrl("config-mutation"));
await mutateConfigFile({
base: "source",
baseHash: patch.expectedHash,
writeOptions: { expectedConfigPath: patch.expectedPath },
afterWrite: { mode: "none", reason: "The package owns the Gateway lifecycle." },
mutate(draft, { snapshot }) {
assertSnapshot(snapshot);
// The root hash alone cannot detect a changed included Gateway.
if (!isDeepStrictEqual(snapshot.config.gateway ?? {}, patch.expectedGateway)) {
throw new Error("Gateway configuration changed while Companion prepared it. Retry setup.");
}
draft.gateway ??= {};
const auth = { ...draft.gateway.auth, ...patch.gateway.auth };
Object.assign(draft.gateway, patch.gateway, { auth });
},
});
} else {
const { readConfigFileSnapshot } = await import(sdkUrl("health"));
const snapshot = await readConfigFileSnapshot({
observe: false,
recoverSuspicious: false,
pluginValidation: "core-only",
});
assertSnapshot(snapshot);
process.stdout.write(JSON.stringify({
gateway: snapshot.config.gateway ?? {},
path: snapshot.path,
hash: snapshot.hash,
exists: snapshot.exists,
}));
}
""";
public static int Run(
string requestPath,
Func<string, string> readFile,
Action<string, string> writeFile,
string? profileRoot = null,
Func<SessionCompanionConfigRequest, string, int>? applyPatch = null,
Func<SessionCompanionConfigRequest, (int ExitCode, string Output)>? readEffectiveConfiguration = null,
Func<IDisposable>? enterState = null)
{
string resultPath = SessionLaunchProtocol.ResultPathFor(requestPath);
string? requestId = null;
try
{
SessionCompanionConfigRequest request =
SessionCompanionConfigProtocol.ReadRequest(readFile(requestPath));
requestId = request.RequestId;
enterState ??= SessionStateAccess.ForAgent().EnterReader;
using IDisposable state = enterState();
string configPath = Path.Combine(profileRoot ?? AgentProfile.GetPath(), ".openclaw", "openclaw.json");
SessionCompanionConfigResult result = Configure(
request, configPath, writeFile,
applyPatch ?? ((r, patch) => ApplyPatch(r, configPath, patch)),
readEffectiveConfiguration ?? (r => ReadEffectiveConfiguration(r, configPath)));
writeFile(resultPath, SessionCompanionConfigProtocol.SerializeResult(result));
return 0;
}
catch (Exception exception) when (exception is SessionLaunchException or IOException or
UnauthorizedAccessException or ArgumentException or JsonException or
InvalidOperationException or System.ComponentModel.Win32Exception)
{
writeFile(resultPath, SessionCompanionConfigProtocol.SerializeResult(
new SessionCompanionConfigResult { RequestId = requestId, Error = exception.Message }));
return SessionLaunchProtocol.HelperFailureExitCode;
}
}
internal static SessionCompanionConfigResult Configure(
SessionCompanionConfigRequest request,
string configPath,
Action<string, string> writeFile,
Func<SessionCompanionConfigRequest, string, int> applyPatch,
Func<SessionCompanionConfigRequest, (int ExitCode, string Output)> readEffectiveConfiguration)
{
CompanionConfigSnapshot snapshot = ReadSnapshot(request, configPath, readEffectiveConfiguration);
(int? existingPort, string? existingToken) = ReadGatewayConfiguration(snapshot.Gateway);
if (request.CheckOnly)
{
if (!snapshot.Exists)
{
throw new SessionLaunchException(
"The agent's Gateway configuration is missing. Run `clawctl companion prepare` first.");
}
if (existingPort is null || string.IsNullOrWhiteSpace(existingToken))
{
throw new SessionLaunchException(
"The agent's Gateway configuration has no usable port or token. Run `clawctl companion prepare` first.");
}
return new SessionCompanionConfigResult
{
RequestId = request.RequestId,
Port = existingPort.Value,
Token = existingToken
};
}
if (existingPort is not null && !string.IsNullOrWhiteSpace(existingToken))
{
return new SessionCompanionConfigResult
{
RequestId = request.RequestId,
Port = existingPort.Value,
Token = existingToken
};
}
int port = existingPort ?? request.Port;
string token = existingToken ?? request.Token ??
Convert.ToHexString(RandomNumberGenerator.GetBytes(32));
string patchPath = Path.Combine(
Path.GetDirectoryName(configPath)!,
".companion-config-" + Guid.NewGuid().ToString("N") + ".json");
Directory.CreateDirectory(Path.GetDirectoryName(configPath)!);
try
{
writeFile(patchPath, JsonSerializer.Serialize(
new CompanionConfigPatch(new CompanionGatewayPatch(
"local", port, "loopback", new CompanionAuthPatch("token", token)),
snapshot.Gateway, snapshot.Hash, snapshot.Path),
CompanionConfigPatchContext.Default.CompanionConfigPatch));
int exitCode = applyPatch(request, patchPath);
if (exitCode != 0)
{
throw new SessionLaunchException(
$"OpenClaw rejected the Companion Gateway configuration (exit code {exitCode}). " +
"It may have changed during preparation. Retry setup, or inspect it with " +
"`clawctl pwsh` and `openclaw config validate`.");
}
}
finally
{
File.Delete(patchPath);
}
CompanionConfigSnapshot configured = ReadSnapshot(request, configPath, readEffectiveConfiguration);
if (!configured.Exists)
{
throw new SessionLaunchException("OpenClaw did not create the agent's Gateway configuration.");
}
(int? configuredPort, string? configuredToken) =
ReadGatewayConfiguration(configured.Gateway);
if (configuredPort != port || configuredToken != token)
{
throw new SessionLaunchException(
"The agent's Gateway configuration changed while Companion prepared it. Retry setup.");
}
return new SessionCompanionConfigResult { RequestId = request.RequestId, Port = port, Token = token };
}
private static CompanionConfigSnapshot ReadSnapshot(
SessionCompanionConfigRequest request,
string configPath,
Func<SessionCompanionConfigRequest, (int ExitCode, string Output)> readEffectiveConfiguration)
{
(int exitCode, string output) = readEffectiveConfiguration(request);
if (exitCode != 0)
{
throw new SessionLaunchException(
$"OpenClaw could not read the effective Gateway configuration (exit code {exitCode}). " +
"Check agent profile and Gateway environment overrides with `clawctl pwsh`, " +
"then run `openclaw config validate`.");
}
CompanionConfigSnapshot? snapshot = JsonSerializer.Deserialize(
output, CompanionConfigPatchContext.Default.CompanionConfigSnapshot);
if (snapshot is null || string.IsNullOrWhiteSpace(snapshot.Hash) ||
!string.Equals(snapshot.Path, configPath, StringComparison.OrdinalIgnoreCase))
{
throw new SessionLaunchException(
"OpenClaw did not return a snapshot of the expected agent Gateway configuration.");
}
return snapshot;
}
private static (int? Port, string? Token) ReadGatewayConfiguration(JsonElement gateway)
{
if (gateway.ValueKind != JsonValueKind.Object)
{
throw new SessionLaunchException("The agent's effective Gateway configuration is not an object.");
}
if (!gateway.EnumerateObject().Any())
{
return (null, null);
}
if (!gateway.TryGetProperty("mode", out JsonElement mode) ||
mode.ValueKind != JsonValueKind.String || mode.GetString() != "local")
{
throw new SessionLaunchException(
"The agent's existing Gateway mode is not local. Reconfigure it explicitly.");
}
if (gateway.TryGetProperty("bind", out JsonElement bind) &&
(bind.ValueKind != JsonValueKind.String || bind.GetString() != "loopback"))
{
throw new SessionLaunchException(
"The agent's existing Gateway bind is not loopback. Reconfigure it explicitly.");
}
int? port = null;
if (gateway.TryGetProperty("port", out JsonElement configuredPort))
{
if (!configuredPort.TryGetInt32(out int value) || value is < 1 or > 65535)
{
throw new SessionLaunchException("The agent's existing Gateway port is invalid.");
}
port = value;
}
bool hasAuth = gateway.TryGetProperty("auth", out JsonElement auth);
if (hasAuth && auth.ValueKind != JsonValueKind.Object)
{
throw new SessionLaunchException("The agent's Gateway authentication is not an object.");
}
JsonElement authMode = default;
if (hasAuth && auth.TryGetProperty("mode", out authMode) &&
(authMode.ValueKind != JsonValueKind.String || authMode.GetString() != "token"))
{
throw new SessionLaunchException("The agent's Gateway uses a different authentication mode.");
}
if (authMode.ValueKind == JsonValueKind.Undefined &&
(HasPassword(auth) ||
(gateway.TryGetProperty("remote", out JsonElement remote) && HasPassword(remote))))
{
throw new SessionLaunchException("The agent's Gateway implicitly uses password authentication.");
}
if (!hasAuth || !auth.TryGetProperty("token", out JsonElement authToken))
{
return (port, null);
}
if (authToken.ValueKind != JsonValueKind.String || string.IsNullOrWhiteSpace(authToken.GetString()) ||
authToken.GetString()!.Contains("${", StringComparison.Ordinal) ||
authToken.GetString() == "__OPENCLAW_REDACTED__")
{
throw new SessionLaunchException(
"OpenClaw did not expose the existing Gateway token to Companion. " +
"The existing configuration was not changed.");
}
return (port, authToken.GetString());
}
private static bool HasPassword(JsonElement settings)
=> settings.ValueKind == JsonValueKind.Object &&
settings.TryGetProperty("password", out JsonElement password) &&
password.ValueKind != JsonValueKind.Null &&
(password.ValueKind != JsonValueKind.String || !string.IsNullOrWhiteSpace(password.GetString()));
private static int ApplyPatch(SessionCompanionConfigRequest request, string configPath, string patchPath)
=> RunNode(request,
["--input-type=module", "--eval", ConfigurationOperation,
request.ApplicationDirectory!, configPath, patchPath]).ExitCode;
private static (int ExitCode, string Output) ReadEffectiveConfiguration(
SessionCompanionConfigRequest request, string configPath)
=> RunNode(
request,
["--input-type=module", "--eval", ConfigurationOperation, request.ApplicationDirectory!, configPath]);
private static (int ExitCode, string Output) RunNode(
SessionCompanionConfigRequest request,
IReadOnlyList<string> arguments)
{
List<string> nodeArguments = [];
if (request.NativeRootPath is not null)
{
nodeArguments.AddRange(["--import", new Uri(request.PreloadPath!).AbsoluteUri]);
}
nodeArguments.AddRange(arguments);
SessionProcessOutput output = SessionProcessLauncher.Capture(new SessionLaunchRequest
{
Executable = request.NodePath,
Arguments = nodeArguments,
WorkingDirectory = AppContext.BaseDirectory,
Environment = request.Environment,
NativeRootPath = request.NativeRootPath
}, TimeSpan.FromMinutes(2));
return (output.ExitCode, output.StandardOutput);
}
}