Repository navigation
Expand file tree
/
Copy pathSetupStateStore.cs
More file actions
217 lines (180 loc) · 6.56 KB
/
Copy pathSetupStateStore.cs
File metadata and controls
217 lines (180 loc) · 6.56 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
using System.Text.Json;
using System.Text.Json.Serialization;
namespace OpenClaw.Launcher.Session;
[JsonConverter(typeof(JsonStringEnumConverter<SetupPhase>))]
internal enum SetupPhase
{
Ready,
Preparing,
TearingDown
}
/// <summary>
/// The durable marker that this installation completed explicit setup.
/// </summary>
/// <remarks>
/// The marker is separate from the session record because older management
/// commands can create a session without completing the new setup workflow.
/// A valid session therefore does not, by itself, authorize <c>openclaw</c>.
/// </remarks>
internal sealed record SetupRecord
{
[JsonPropertyName("schemaVersion")]
public int SchemaVersion { get; init; }
[JsonPropertyName("applicationId")]
public string ApplicationId { get; init; } = string.Empty;
[JsonPropertyName("completedUtc")]
public DateTimeOffset CompletedUtc { get; init; }
[JsonPropertyName("phase")]
public SetupPhase Phase { get; init; } = SetupPhase.Ready;
[JsonPropertyName("sandboxId")]
public string? SandboxId { get; init; }
[JsonPropertyName("startupEnabled")]
public bool StartupEnabled { get; init; } = true;
[JsonPropertyName("agentNodePath")]
public string? AgentNodePath { get; init; }
[JsonPropertyName("agentNodeVersion")]
public string? AgentNodeVersion { get; init; }
[JsonPropertyName("agentNodeArchive")]
public string? AgentNodeArchive { get; init; }
/// <summary>
/// The agent-owned root holding the mirrored native dependency packages.
/// </summary>
[JsonPropertyName("agentNativeRoot")]
public string? AgentNativeRoot { get; init; }
}
/// <summary>Why the explicit setup marker could not be used.</summary>
internal enum SetupStateFault
{
/// <summary>Setup has not completed for this installation.</summary>
Missing,
/// <summary>The marker exists but cannot be read as a record.</summary>
Unreadable,
/// <summary>The marker was written by an incompatible newer version.</summary>
UnsupportedSchema,
/// <summary>The marker is missing values it cannot be used without.</summary>
Incomplete,
/// <summary>The marker belongs to a different package identity.</summary>
ForeignIdentity,
}
internal sealed record SetupStateResult(
SetupRecord? Record,
SetupStateFault? Fault,
string? Detail)
{
public static SetupStateResult Found(SetupRecord record) =>
new(record, null, null);
public static SetupStateResult Failed(SetupStateFault fault, string detail) =>
new(null, fault, detail);
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(SetupRecord))]
internal sealed partial class SetupStateJsonContext : JsonSerializerContext;
/// <summary>
/// Reads and atomically writes the explicit setup marker.
/// </summary>
internal sealed class SetupStateStore
{
public const int CurrentSchemaVersion = 2;
private readonly string _filePath;
public SetupStateStore(string filePath)
{
ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
_filePath = Path.GetFullPath(filePath);
}
public string FilePath => _filePath;
public SetupStateResult Read(string expectedApplicationId)
{
ArgumentException.ThrowIfNullOrWhiteSpace(expectedApplicationId);
string text;
try
{
text = File.ReadAllText(_filePath);
}
catch (Exception exception) when (
exception is FileNotFoundException or DirectoryNotFoundException)
{
return SetupStateResult.Failed(
SetupStateFault.Missing,
"Explicit setup has not completed.");
}
catch (Exception exception) when (
exception is IOException or UnauthorizedAccessException)
{
return SetupStateResult.Failed(
SetupStateFault.Unreadable,
$"The setup record could not be read: {exception.Message}");
}
SetupRecord? record;
try
{
record = JsonSerializer.Deserialize(
text,
SetupStateJsonContext.Default.SetupRecord);
}
catch (JsonException exception)
{
return SetupStateResult.Failed(
SetupStateFault.Unreadable,
$"The setup record is not valid JSON: {exception.Message}");
}
if (record is null)
{
return SetupStateResult.Failed(
SetupStateFault.Unreadable,
"The setup record is empty.");
}
if (record.SchemaVersion > CurrentSchemaVersion)
{
return SetupStateResult.Failed(
SetupStateFault.UnsupportedSchema,
$"The setup record uses schema version {record.SchemaVersion}, " +
$"but this build understands version {CurrentSchemaVersion}. A " +
"newer OpenClaw installation may have written it.");
}
if (record.SchemaVersion < 1 ||
string.IsNullOrWhiteSpace(record.ApplicationId) ||
!Enum.IsDefined(record.Phase))
{
return SetupStateResult.Failed(
SetupStateFault.Incomplete,
"The setup record is missing its schema version or application identity.");
}
if (!string.Equals(
record.ApplicationId,
expectedApplicationId,
StringComparison.OrdinalIgnoreCase))
{
return SetupStateResult.Failed(
SetupStateFault.ForeignIdentity,
$"The setup record belongs to '{record.ApplicationId}', but this " +
$"installation is '{expectedApplicationId}'.");
}
return SetupStateResult.Found(record);
}
public void Write(SetupRecord record)
{
ArgumentNullException.ThrowIfNull(record);
ArgumentException.ThrowIfNullOrWhiteSpace(record.ApplicationId);
string? directory = Path.GetDirectoryName(_filePath);
if (!string.IsNullOrEmpty(directory))
{
Directory.CreateDirectory(directory);
}
string text = JsonSerializer.Serialize(
record with { SchemaVersion = CurrentSchemaVersion },
SetupStateJsonContext.Default.SetupRecord);
string temporaryPath = _filePath + ".tmp";
File.WriteAllText(temporaryPath, text);
File.Move(temporaryPath, _filePath, overwrite: true);
}
public void Clear()
{
try
{
File.Delete(_filePath);
}
catch (DirectoryNotFoundException)
{
}
}
}