-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
238 lines (224 loc) · 11.4 KB
/
Copy pathProgram.cs
File metadata and controls
238 lines (224 loc) · 11.4 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
using System.Text.Json;
using GsdOrchestrator.Loop;
using GsdOrchestrator.Scheduling;
using GsdOrchestrator.Verification;
using Microsoft.Extensions.Logging.Abstractions;
var options = Arguments.Parse(args);
var serializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web) { WriteIndented = true };
Directory.CreateDirectory(options.Output);
var feature = await RunCoordinatorScenario("feature", [Passed()], options);
var repair = await RunCoordinatorScenario("repair", [Failed(), Passed()], options);
var restart = await RunRestartScenario(options);
var policy = RunPolicyScenario();
await Write("feature", feature);
await Write("repair", repair);
await Write("restart", restart);
await Write("policy", policy);
Console.WriteLine($"Generated executable CAS loop pilot evidence at {Path.GetFullPath(options.Output)}");
return;
async Task Write(string name, PilotEvidence evidence)
{
var path = Path.Combine(options.Output, $"{name}.json");
await File.WriteAllTextAsync(path, JsonSerializer.Serialize(evidence, serializerOptions));
}
async Task<PilotEvidence> RunCoordinatorScenario(string scenario, IReadOnlyList<VerificationRunResult> results, Arguments arguments)
{
var database = Path.Combine(Path.GetTempPath(), $"cas-pilot-{scenario}-{Guid.NewGuid():N}.db");
try
{
var store = new SqliteGoalStore(database, NullLogger<SqliteGoalStore>.Instance);
await store.InitializeAsync();
await store.SaveAsync(Seed($"goal-{scenario}"));
var learning = new CapturingLearning();
var worker = new MafProcessLoopWorker(arguments.Python, arguments.MafRoot);
var coordinator = new LoopCoordinator(store, worker, new ScriptedVerifier(results), learning, NullLogger<LoopCoordinator>.Instance);
var run = await coordinator.RunAsync($"goal-{scenario}", new(true, 0, 2, 0, 2, 0, 10));
var peak = run.PeakConcurrency;
var isolatedImplementation = scenario != "feature" || VerifyIsolatedWorktree();
var events = new List<PilotEvent>();
if (scenario == "feature")
{
events.Add(new(0, "analysis.fanout", "four-read-only-specialists"));
events.Add(new(1, "analysis.fanin", $"peak-concurrency-{peak}"));
}
var offset = events.Count;
events.AddRange(run.Aggregate.Events.Select((item, index) =>
new PilotEvent(index + offset, item.Type, Outcome(item.PayloadJson))));
var assertions = scenario == "feature"
? new Dictionary<string, object>
{
["parallelAnalysis"] = true,
["peakConcurrency"] = peak,
["isolatedImplementation"] = isolatedImplementation,
["mandatoryVerification"] = "passed",
["goalStatus"] = run.Aggregate.Goal.Status.ToString().ToLowerInvariant(),
["terminalLearningPublications"] = learning.Outcomes.Count,
}
: new Dictionary<string, object>
{
["initialVerification"] = "failed",
["repairCreated"] = run.RepairCreated,
["repairAttempt"] = 2,
["repairLimit"] = 2,
["subsequentVerification"] = "passed",
["terminalLearningPublications"] = learning.Outcomes.Count,
};
return new("1.0.0", scenario, "passed", true,
[Git(Directory.GetCurrentDirectory(), "rev-parse", "HEAD").Trim(), "54fd535", "9057fa0"], events, assertions,
run.Aggregate.Evidence.Select(item => item.Uri).Distinct().ToArray(),
["dotnet run --project tools/LoopPilotRunner"]);
}
finally
{
if (File.Exists(database)) File.Delete(database);
}
}
async Task<PilotEvidence> RunRestartScenario(Arguments arguments)
{
var database = Path.Combine(Path.GetTempPath(), $"cas-pilot-restart-{Guid.NewGuid():N}.db");
try
{
var store = new SqliteGoalStore(database, NullLogger<SqliteGoalStore>.Instance);
await store.InitializeAsync();
var seed = Seed("goal-restart") with
{
Goal = Seed("goal-restart").Goal with { Status = GoalStatus.Running },
WorkItems = [Seed("goal-restart").WorkItems[0] with { Status = WorkItemStatus.Running }],
Leases = [new("expired", "goal-restart", "work-1", "old-worker", DateTimeOffset.UtcNow.AddMinutes(-10), DateTimeOffset.UtcNow.AddMinutes(-5))],
};
await store.SaveAsync(seed);
var recovered = await store.RecoverExpiredLeasesAsync(DateTimeOffset.UtcNow);
var first = new Dictionary<string, bool>();
var duplicate = new Dictionary<string, bool>();
foreach (var effect in new[] { "commit", "comment", "pull-request" })
{
first[effect] = await store.TryReserveIdempotencyKeyAsync("goal-restart", "work-1", $"restart-{effect}", effect);
duplicate[effect] = await store.TryReserveIdempotencyKeyAsync("goal-restart", "work-1", $"restart-{effect}", effect);
}
return new("1.0.0", "restart", "passed", true, ["ddef0bf"],
[new(0, "lease.expired", "worker-interrupted"), new(1, "lease.reclaimed", recovered == 1 ? "new-owner" : "failed"), new(2, "side-effects.reconciled", duplicate.Values.Any(value => value) ? "duplicate" : "idempotent")],
new()
{
["leaseReclaimed"] = recovered == 1,
["duplicateCommit"] = duplicate["commit"] ? 1 : 0,
["duplicateComment"] = duplicate["comment"] ? 1 : 0,
["duplicatePullRequest"] = duplicate["pull-request"] ? 1 : 0,
["idempotencyPreserved"] = first.Values.All(value => value) && duplicate.Values.All(value => !value),
},
["cas://evidence/pilots/restart/sqlite-lease", "cas://evidence/pilots/restart/idempotency"],
["dotnet run --project tools/LoopPilotRunner"]);
}
finally
{
if (File.Exists(database)) File.Delete(database);
}
}
PilotEvidence RunPolicyScenario()
{
var denied = false;
try { LoopPolicyGuard.RequireReadablePath("repo/.env"); }
catch (UnauthorizedAccessException) { denied = true; }
var push = LoopPolicyGuard.EvaluateExternalAction("push", false);
var deploy = LoopPolicyGuard.EvaluateExternalAction("deploy", false);
return new("1.0.0", "policy", "passed", true, ["e1e3232"],
[new(0, "sandbox.read", denied ? "env-denied" : "allowed"), new(1, "push.requested", Decision(push)), new(2, "deploy.requested", Decision(deploy))],
new()
{
["envAccess"] = denied ? "denied" : "allowed",
["pushBeforeApproval"] = push == ExternalActionDecision.Authorized,
["deployBeforeApproval"] = deploy == ExternalActionDecision.Authorized,
["approvalRequired"] = true,
},
["cas://evidence/pilots/policy/guard"],
["dotnet run --project tools/LoopPilotRunner"]);
}
bool VerifyIsolatedWorktree()
{
var root = Path.Combine(Path.GetTempPath(), $"cas-pilot-repo-{Guid.NewGuid():N}");
var worktree = Path.Combine(Path.GetTempPath(), $"cas-pilot-worktree-{Guid.NewGuid():N}");
Directory.CreateDirectory(root);
try
{
Git(root, "init", "-b", "main");
Git(root, "config", "user.email", "pilot@localhost");
Git(root, "config", "user.name", "CAS Pilot");
File.WriteAllText(Path.Combine(root, "README.md"), "baseline");
Git(root, "add", "README.md");
Git(root, "commit", "-m", "baseline");
var before = Git(root, "rev-parse", "HEAD").Trim();
Git(root, "worktree", "add", "-b", "codex/pilot-isolation", worktree, before);
File.WriteAllText(Path.Combine(worktree, "pilot.txt"), "isolated mutation");
var sourceAfter = Git(root, "rev-parse", "HEAD").Trim();
var changed = Git(worktree, "status", "--porcelain").Contains("pilot.txt", StringComparison.Ordinal);
Git(root, "worktree", "remove", "--force", worktree);
return before == sourceAfter && changed && !Path.GetFullPath(worktree).StartsWith(Path.GetFullPath(root), StringComparison.OrdinalIgnoreCase);
}
finally
{
if (Directory.Exists(worktree)) Directory.Delete(worktree, true);
if (Directory.Exists(root)) Directory.Delete(root, true);
}
}
string Git(string workingDirectory, params string[] arguments)
{
using var process = new System.Diagnostics.Process
{
StartInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "git",
WorkingDirectory = workingDirectory,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
foreach (var argument in arguments) process.StartInfo.ArgumentList.Add(argument);
process.Start();
var output = process.StandardOutput.ReadToEnd();
var error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0) throw new InvalidOperationException($"git {string.Join(' ', arguments)} failed: {error}");
return output;
}
static string Decision(ExternalActionDecision decision) => decision == ExternalActionDecision.WaitingApproval ? "waiting-approval" : "authorized";
static string Outcome(string payload) => JsonDocument.Parse(payload).RootElement.GetProperty("outcome").GetString() ?? "unknown";
static VerificationRunResult Passed() => Verification(VerificationOutcome.Passed);
static VerificationRunResult Failed() => Verification(VerificationOutcome.Failed);
static VerificationRunResult Verification(VerificationOutcome outcome) => new(outcome,
[new("test", VerificationCategory.Test, true, outcome, $"cas://evidence/verifier/{outcome.ToString().ToLowerInvariant()}", outcome == VerificationOutcome.Passed ? 0 : 1, 1)]);
static GoalAggregate Seed(string goalId) => new(
new(goalId, $"corr-{goalId}", GoalStatus.Planned, new(3, 3, 2, 600, 10, 2)),
[new("work-1", goalId, "repo", "provider", WorkItemStatus.Ready, 2, $"idem-{goalId}")],
[], [], [], [], [], [], [], []);
sealed record PilotEvent(int Sequence, string Type, string Outcome);
sealed record PilotEvidence(string SchemaVersion, string Scenario, string Status, bool Bounded, string[] SourceCommits, IReadOnlyList<PilotEvent> Events, Dictionary<string, object> Assertions, string[] Artifacts, string[] Reproduce);
sealed class ScriptedVerifier(IReadOnlyList<VerificationRunResult> results) : ILoopVerifier
{
private int _index;
public Task<VerificationRunResult> VerifyAsync(string goalId, LoopWorkResult work, CancellationToken cancellationToken) => Task.FromResult(results[_index++]);
}
sealed class CapturingLearning : ITerminalOutcomePublisher
{
public List<TerminalLoopOutcome> Outcomes { get; } = [];
public Task PublishAsync(TerminalLoopOutcome outcome, CancellationToken cancellationToken)
{
Outcomes.Add(outcome);
return Task.CompletedTask;
}
}
sealed record Arguments(string Output, string Python, string MafRoot)
{
public static Arguments Parse(string[] args)
{
string? Value(string name)
{
var index = Array.IndexOf(args, name);
return index >= 0 && index + 1 < args.Length ? args[index + 1] : null;
}
return new(
Value("--output") ?? Path.Combine(Directory.GetCurrentDirectory(), "evidence", "loop-pilots"),
Value("--python") ?? "python",
Value("--maf-root") ?? Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..", "autogen")));
}
}