Automate IPA trial exploration loop
This commit is contained in:
parent
05cf7d36d6
commit
d1789d6677
12 changed files with 1289 additions and 17 deletions
15
tools/IpaTrialWorker/IpaTrialWorker.csproj
Normal file
15
tools/IpaTrialWorker/IpaTrialWorker.csproj
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\AquaSynth.Core\AquaSynth.Core.csproj" />
|
||||
<ProjectReference Include="..\..\src\AquaSynth.Faust\AquaSynth.Faust.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
492
tools/IpaTrialWorker/Program.cs
Normal file
492
tools/IpaTrialWorker/Program.cs
Normal file
|
|
@ -0,0 +1,492 @@
|
|||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
using AquaSynth.Dsl;
|
||||
using AquaSynth.Faust;
|
||||
|
||||
var command = args.FirstOrDefault();
|
||||
if (string.IsNullOrWhiteSpace(command) || command is "-h" or "--help")
|
||||
{
|
||||
PrintHelp();
|
||||
return 0;
|
||||
}
|
||||
|
||||
var options = ParseOptions(args.Skip(1).ToArray());
|
||||
try
|
||||
{
|
||||
switch (command)
|
||||
{
|
||||
case "seed":
|
||||
await SeedAsync(options);
|
||||
return 0;
|
||||
case "score":
|
||||
await ScoreAsync(options);
|
||||
return 0;
|
||||
case "dump":
|
||||
await DumpAsync(options);
|
||||
return 0;
|
||||
case "search":
|
||||
await SearchAsync(options);
|
||||
return 0;
|
||||
case "show":
|
||||
await ShowAsync(options);
|
||||
return 0;
|
||||
default:
|
||||
Console.Error.WriteLine($"Unknown command `{command}`.");
|
||||
PrintHelp();
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static async Task SeedAsync(Dictionary<string, string> options)
|
||||
{
|
||||
var artifactRoot = Required(options, "artifact-root");
|
||||
var batchId = Value(options, "batch-id", "five-seed-trials");
|
||||
var store = Value(options, "store", Path.Combine(artifactRoot, "ipa-trial-results.cc"));
|
||||
var result = await IpaTrialOrchestrator.RunAsync(
|
||||
artifactRoot,
|
||||
options: new IpaTrialOrchestrationOptions(BatchId: batchId));
|
||||
await IpaTrialResultCultCacheStore.UpsertResultsAsync(store, result.TrialResults);
|
||||
Console.WriteLine(result.ArtifactDirectory);
|
||||
Console.WriteLine(store);
|
||||
}
|
||||
|
||||
static async Task ScoreAsync(Dictionary<string, string> options)
|
||||
{
|
||||
var patchRoot = Required(options, "patch-root");
|
||||
var artifactRoot = Required(options, "artifact-root");
|
||||
var batchId = Value(options, "batch-id", $"agent-candidates-{DateTimeOffset.UtcNow:yyyyMMddTHHmmssfff}");
|
||||
var store = Value(options, "store", Path.Combine(artifactRoot, "ipa-trial-results.cc"));
|
||||
var hypothesizer = Value(options, "hypothesizer", "external-codex-hypothesis-worker");
|
||||
var candidates = CandidateScripts(patchRoot, hypothesizer);
|
||||
var result = await IpaTrialOrchestrator.RunCandidateScriptsAsync(
|
||||
artifactRoot,
|
||||
candidates,
|
||||
options: new IpaTrialOrchestrationOptions(BatchId: batchId, HypothesizerId: hypothesizer));
|
||||
await IpaTrialResultCultCacheStore.UpsertResultsAsync(store, result.TrialResults);
|
||||
Console.WriteLine(result.ArtifactDirectory);
|
||||
Console.WriteLine(store);
|
||||
}
|
||||
|
||||
static async Task DumpAsync(Dictionary<string, string> options)
|
||||
{
|
||||
var store = Required(options, "store");
|
||||
var output = Required(options, "output");
|
||||
var results = await IpaTrialResultCultCacheStore.ReadResultsAsync(store);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(output))!);
|
||||
await File.WriteAllTextAsync(output, StoreReport(store, results), Encoding.UTF8);
|
||||
Console.WriteLine(output);
|
||||
}
|
||||
|
||||
static async Task SearchAsync(Dictionary<string, string> options)
|
||||
{
|
||||
var store = Required(options, "store");
|
||||
var query = Required(options, "query");
|
||||
var output = Required(options, "output");
|
||||
var limit = int.TryParse(Value(options, "limit", "12"), NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsed)
|
||||
? Math.Clamp(parsed, 1, 100)
|
||||
: 12;
|
||||
var results = await IpaTrialResultCultCacheStore.ReadResultsAsync(store);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(output))!);
|
||||
await File.WriteAllTextAsync(output, SearchReport(store, query, Rank(results, query, limit)), Encoding.UTF8);
|
||||
Console.WriteLine(output);
|
||||
}
|
||||
|
||||
static async Task ShowAsync(Dictionary<string, string> options)
|
||||
{
|
||||
var store = Required(options, "store");
|
||||
var trialId = Required(options, "trial-id");
|
||||
var output = Required(options, "output");
|
||||
var results = await IpaTrialResultCultCacheStore.ReadResultsAsync(store);
|
||||
var result = results.FirstOrDefault(item =>
|
||||
item.TrialId.Equals(trialId, StringComparison.OrdinalIgnoreCase) ||
|
||||
item.CandidateId.Equals(trialId, StringComparison.OrdinalIgnoreCase))
|
||||
?? throw new InvalidDataException($"No trial or candidate found for `{trialId}`.");
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(output))!);
|
||||
await File.WriteAllTextAsync(output, DetailReport(store, result), Encoding.UTF8);
|
||||
Console.WriteLine(output);
|
||||
}
|
||||
|
||||
static IReadOnlyList<IpaTrialScriptCandidate> CandidateScripts(string patchRoot, string hypothesizer)
|
||||
{
|
||||
if (!Directory.Exists(patchRoot))
|
||||
{
|
||||
throw new DirectoryNotFoundException(patchRoot);
|
||||
}
|
||||
|
||||
var targetIds = IpaTrialOrchestrator.DefaultFiveSeedTrialSets
|
||||
.SelectMany(set => set.Targets)
|
||||
.Select(target => target.Target.Id)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.OrderByDescending(id => id.Length)
|
||||
.ToArray();
|
||||
return Directory.EnumerateFiles(patchRoot, "*.aqua", SearchOption.AllDirectories)
|
||||
.Order(StringComparer.OrdinalIgnoreCase)
|
||||
.Select(path =>
|
||||
{
|
||||
var stem = Path.GetFileNameWithoutExtension(path);
|
||||
var target = TargetIdFromStem(stem, targetIds);
|
||||
return new IpaTrialScriptCandidate(
|
||||
target,
|
||||
stem,
|
||||
path,
|
||||
hypothesizer,
|
||||
$"External Codex-authored IPA patch candidate `{stem}` for target `{target}`.");
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
static string TargetIdFromStem(string stem, IReadOnlyList<string> targetIds)
|
||||
{
|
||||
var explicitSplit = stem.Split(["__"], StringSplitOptions.None);
|
||||
if (explicitSplit.Length > 1 && targetIds.Contains(explicitSplit[0], StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
return explicitSplit[0];
|
||||
}
|
||||
|
||||
var matched = targetIds.FirstOrDefault(id =>
|
||||
stem.Equals(id, StringComparison.OrdinalIgnoreCase) ||
|
||||
stem.StartsWith($"{id}-", StringComparison.OrdinalIgnoreCase) ||
|
||||
stem.StartsWith($"{id}_", StringComparison.OrdinalIgnoreCase));
|
||||
if (!string.IsNullOrWhiteSpace(matched))
|
||||
{
|
||||
return matched;
|
||||
}
|
||||
|
||||
throw new InvalidDataException(
|
||||
$"Could not infer IPA target id from `{stem}`. Name files as `<targetId>__candidate-name.aqua`; known target ids: {string.Join(", ", targetIds)}.");
|
||||
}
|
||||
|
||||
static IReadOnlyList<(IpaTrialResult Result, float Score, string[] MatchedTerms)> Rank(
|
||||
IReadOnlyList<IpaTrialResult> results,
|
||||
string query,
|
||||
int limit)
|
||||
{
|
||||
var terms = ExpandTerms(Tokenize(query)).ToArray();
|
||||
return results
|
||||
.Select(result =>
|
||||
{
|
||||
var document = Tokenize(DocumentText(result)).ToArray();
|
||||
var termCounts = document.GroupBy(term => term, StringComparer.OrdinalIgnoreCase)
|
||||
.ToDictionary(group => group.Key, group => group.Count(), StringComparer.OrdinalIgnoreCase);
|
||||
var matched = terms.Where(term => termCounts.ContainsKey(term)).Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
|
||||
var lexical = matched.Sum(term => 1f + MathF.Log(1 + termCounts[term]));
|
||||
var metricBias = MetricBias(result, terms);
|
||||
var score = lexical + metricBias;
|
||||
return (result, score, matched);
|
||||
})
|
||||
.Where(item => item.score > 0)
|
||||
.OrderByDescending(item => item.score)
|
||||
.ThenByDescending(item => ScoreSort(item.result))
|
||||
.ThenBy(item => item.result.TrialId, StringComparer.Ordinal)
|
||||
.Take(limit)
|
||||
.Select(item => (item.result, item.score, item.matched))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
static IEnumerable<string> ExpandTerms(IEnumerable<string> terms)
|
||||
{
|
||||
var set = new HashSet<string>(terms, StringComparer.OrdinalIgnoreCase);
|
||||
var aliases = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
["vowel"] = ["a", "i", "u", "e", "o", "formant", "tongue", "lip", "radiation"],
|
||||
["nasal"] = ["m", "n", "ng", "ŋ", "velum", "branch", "velopharynx", "admittance"],
|
||||
["sonorant"] = ["nasal", "approximant", "l", "r", "voiced"],
|
||||
["fricative"] = ["s", "z", "f", "v", "th", "θ", "turbulence", "noise", "constriction"],
|
||||
["stop"] = ["p", "b", "t", "d", "k", "plosive", "closure", "release", "reservoir"],
|
||||
["plosive"] = ["stop", "closure", "release", "reservoir", "stored", "pressure"],
|
||||
["weak"] = ["render-failed", "weak", "low", "failed", "silent"],
|
||||
["promising"] = ["pressure", "promising", "high", "improved"],
|
||||
["articulation"] = ["gesture", "primitive", "timeline", "opening", "flow"],
|
||||
["dressing"] = ["fm", "am", "modulator", "envelope", "noise", "patch"],
|
||||
["owner"] = ["source", "lowering", "tract", "radiation", "gesture", "orchestration"]
|
||||
};
|
||||
|
||||
var queue = set.ToArray();
|
||||
foreach (var term in queue)
|
||||
{
|
||||
if (!aliases.TryGetValue(term, out var expanded))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var alias in expanded)
|
||||
{
|
||||
set.Add(alias);
|
||||
}
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
static float MetricBias(IpaTrialResult result, IReadOnlyCollection<string> terms)
|
||||
{
|
||||
var bias = 0f;
|
||||
var cosine = result.Metrics.FirstOrDefault(metric => metric.Name == "log_mel_cosine")?.Value ?? 0;
|
||||
var articulation = result.Metrics.FirstOrDefault(metric => metric.Name == "articulation_score")?.Value ?? 0;
|
||||
var gesture = result.Metrics.FirstOrDefault(metric => metric.Name == "gesture_score")?.Value ?? 0;
|
||||
if (terms.Contains("weak", StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
bias += Math.Clamp(1 - cosine, 0, 1);
|
||||
bias += Math.Clamp(1 - articulation, 0, 1);
|
||||
}
|
||||
|
||||
if (terms.Contains("promising", StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
bias += Math.Clamp(cosine, 0, 1);
|
||||
bias += Math.Clamp(articulation, 0, 1);
|
||||
}
|
||||
|
||||
if (terms.Contains("gesture", StringComparer.OrdinalIgnoreCase) ||
|
||||
terms.Contains("articulation", StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
bias += Math.Clamp(gesture, 0, 1);
|
||||
}
|
||||
|
||||
return bias;
|
||||
}
|
||||
|
||||
static string DocumentText(IpaTrialResult result)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.Append(result.TrialId).Append(' ');
|
||||
builder.Append(result.BatchId).Append(' ');
|
||||
builder.Append(result.TargetSetId).Append(' ');
|
||||
builder.AppendJoin(' ', result.Phonemes).Append(' ');
|
||||
builder.Append(result.ReferenceId).Append(' ');
|
||||
builder.Append(result.CandidateId).Append(' ');
|
||||
builder.Append(result.HypothesizerId).Append(' ');
|
||||
builder.Append(result.Hypothesis).Append(' ');
|
||||
builder.Append(result.EvaluatorId).Append(' ');
|
||||
builder.Append(result.EvaluationSummary).Append(' ');
|
||||
builder.Append(result.Verdict).Append(' ');
|
||||
builder.AppendJoin(' ', result.KnownLies).Append(' ');
|
||||
foreach (var metric in result.Metrics)
|
||||
{
|
||||
builder.Append(metric.Name).Append(' ');
|
||||
}
|
||||
|
||||
foreach (var artifact in result.Artifacts)
|
||||
{
|
||||
builder.Append(artifact.Kind).Append(' ').Append(artifact.Uri).Append(' ');
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
static string[] Tokenize(string text) =>
|
||||
text.ToLowerInvariant()
|
||||
.Split([' ', '\t', '\r', '\n', ',', '.', ':', ';', '/', '\\', '`', '"', '\'', '[', ']', '(', ')', '{', '}', '<', '>', '|', '_', '-'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Where(term => term.Length > 1 || "aeioubdklmnprstvzθŋ".Contains(term, StringComparison.Ordinal))
|
||||
.ToArray();
|
||||
|
||||
static string SearchReport(
|
||||
string store,
|
||||
string query,
|
||||
IReadOnlyList<(IpaTrialResult Result, float Score, string[] MatchedTerms)> ranked)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.AppendLine("# IPA Trial Semantic Search");
|
||||
builder.AppendLine();
|
||||
builder.AppendLine($"store: `{store}`");
|
||||
builder.AppendLine($"query: `{query}`");
|
||||
builder.AppendLine($"matches: {ranked.Count}");
|
||||
builder.AppendLine();
|
||||
foreach (var (result, score, matched) in ranked)
|
||||
{
|
||||
builder.Append("- ");
|
||||
builder.Append(result.TrialId);
|
||||
builder.Append(" / ");
|
||||
builder.Append(result.CandidateId);
|
||||
builder.Append(" / ");
|
||||
builder.Append(result.Verdict);
|
||||
builder.Append(" / score=");
|
||||
builder.Append(score.ToString("0.###", CultureInfo.InvariantCulture));
|
||||
builder.Append(" / matched=");
|
||||
builder.AppendLine(string.Join('|', matched.Take(12)));
|
||||
builder.Append(" metrics: gesture=");
|
||||
builder.Append(Metric(result, "gesture_score"));
|
||||
builder.Append(", logMelCosine=");
|
||||
builder.Append(Metric(result, "log_mel_cosine"));
|
||||
builder.Append(", articulation=");
|
||||
builder.Append(Metric(result, "articulation_score"));
|
||||
builder.Append(", rmsRatio=");
|
||||
builder.AppendLine(Metric(result, "rms_ratio"));
|
||||
builder.Append(" hypothesis: ");
|
||||
builder.AppendLine(result.Hypothesis);
|
||||
builder.Append(" evaluation: ");
|
||||
builder.AppendLine(result.EvaluationSummary);
|
||||
builder.Append(" patch: ");
|
||||
builder.AppendLine(result.CandidatePatchUri);
|
||||
builder.Append(" show: dotnet run --project tools/IpaTrialWorker/IpaTrialWorker.csproj -- show --store \"");
|
||||
builder.Append(store);
|
||||
builder.Append("\" --trial-id \"");
|
||||
builder.Append(result.TrialId);
|
||||
builder.AppendLine("\" --output <detail.md>");
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
static string DetailReport(string store, IpaTrialResult result)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.AppendLine($"# IPA Trial Detail: {result.TrialId}");
|
||||
builder.AppendLine();
|
||||
builder.AppendLine($"store: `{store}`");
|
||||
builder.AppendLine($"batch: `{result.BatchId}`");
|
||||
builder.AppendLine($"target_set: `{result.TargetSetId}`");
|
||||
builder.AppendLine($"phonemes: `{string.Join(", ", result.Phonemes)}`");
|
||||
builder.AppendLine($"reference: `{result.ReferenceId}`");
|
||||
builder.AppendLine($"candidate: `{result.CandidateId}`");
|
||||
builder.AppendLine($"verdict: `{result.Verdict}`");
|
||||
builder.AppendLine();
|
||||
builder.AppendLine("## Hypothesis");
|
||||
builder.AppendLine(result.Hypothesis);
|
||||
builder.AppendLine();
|
||||
builder.AppendLine("## Evaluation");
|
||||
builder.AppendLine(result.EvaluationSummary);
|
||||
builder.AppendLine();
|
||||
builder.AppendLine("## Metrics");
|
||||
foreach (var metric in result.Metrics.OrderBy(metric => metric.Name, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
builder.Append("- ");
|
||||
builder.Append(metric.Name);
|
||||
builder.Append(": ");
|
||||
builder.Append(metric.Value.ToString("0.######", CultureInfo.InvariantCulture));
|
||||
builder.Append(" weight=");
|
||||
builder.AppendLine(metric.Weight.ToString("0.######", CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
builder.AppendLine();
|
||||
builder.AppendLine("## Artifacts");
|
||||
foreach (var artifact in result.Artifacts)
|
||||
{
|
||||
builder.Append("- ");
|
||||
builder.Append(artifact.Kind);
|
||||
builder.Append(": ");
|
||||
builder.Append(artifact.Uri);
|
||||
if (!string.IsNullOrWhiteSpace(artifact.ContentHash))
|
||||
{
|
||||
builder.Append(" ");
|
||||
builder.Append(artifact.ContentHash);
|
||||
}
|
||||
|
||||
builder.AppendLine();
|
||||
}
|
||||
|
||||
builder.AppendLine();
|
||||
builder.AppendLine("## Known Lies");
|
||||
foreach (var lie in result.KnownLies)
|
||||
{
|
||||
builder.Append("- ");
|
||||
builder.AppendLine(lie);
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
static string StoreReport(string store, IReadOnlyList<IpaTrialResult> results)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.AppendLine($"# IPA Trial Results Store");
|
||||
builder.AppendLine();
|
||||
builder.AppendLine($"store: `{store}`");
|
||||
builder.AppendLine($"records: {results.Count}");
|
||||
builder.AppendLine();
|
||||
foreach (var group in results.GroupBy(result => result.TargetSetId).OrderBy(group => group.Key, StringComparer.Ordinal))
|
||||
{
|
||||
builder.AppendLine($"## {group.Key}");
|
||||
foreach (var result in group.OrderByDescending(ScoreSort).ThenBy(result => result.TrialId, StringComparer.Ordinal))
|
||||
{
|
||||
builder.Append("- ");
|
||||
builder.Append(result.TrialId);
|
||||
builder.Append(" / ");
|
||||
builder.Append(result.CandidateId);
|
||||
builder.Append(" / ");
|
||||
builder.Append(result.Verdict);
|
||||
builder.Append(": ");
|
||||
builder.Append("gesture=");
|
||||
builder.Append(Metric(result, "gesture_score"));
|
||||
builder.Append(", logMelCosine=");
|
||||
builder.Append(Metric(result, "log_mel_cosine"));
|
||||
builder.Append(", articulation=");
|
||||
builder.Append(Metric(result, "articulation_score"));
|
||||
builder.Append(", rmsRatio=");
|
||||
builder.Append(Metric(result, "rms_ratio"));
|
||||
builder.AppendLine();
|
||||
builder.Append(" patch: ");
|
||||
builder.AppendLine(result.CandidatePatchUri);
|
||||
builder.Append(" timeline: ");
|
||||
builder.AppendLine(result.PrimitiveTimelineUri);
|
||||
builder.Append(" evaluation: ");
|
||||
builder.AppendLine(result.EvaluationSummary);
|
||||
}
|
||||
|
||||
builder.AppendLine();
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
static float ScoreSort(IpaTrialResult result) =>
|
||||
result.Metrics.FirstOrDefault(metric => metric.Name == "log_mel_cosine")?.Value ?? float.NegativeInfinity;
|
||||
|
||||
static string Metric(IpaTrialResult result, string name) =>
|
||||
(result.Metrics.FirstOrDefault(metric => metric.Name == name)?.Value ?? 0)
|
||||
.ToString("0.######", CultureInfo.InvariantCulture);
|
||||
|
||||
static Dictionary<string, string> ParseOptions(IReadOnlyList<string> args)
|
||||
{
|
||||
var options = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
for (var i = 0; i < args.Count; i++)
|
||||
{
|
||||
var arg = args[i];
|
||||
if (!arg.StartsWith("--", StringComparison.Ordinal))
|
||||
{
|
||||
throw new ArgumentException($"Expected option, got `{arg}`.");
|
||||
}
|
||||
|
||||
var key = arg[2..];
|
||||
if (i + 1 >= args.Count || args[i + 1].StartsWith("--", StringComparison.Ordinal))
|
||||
{
|
||||
options[key] = "true";
|
||||
}
|
||||
else
|
||||
{
|
||||
options[key] = args[++i];
|
||||
}
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
static string Required(Dictionary<string, string> options, string key) =>
|
||||
options.TryGetValue(key, out var value) && !string.IsNullOrWhiteSpace(value)
|
||||
? value
|
||||
: throw new ArgumentException($"Missing required --{key} option.");
|
||||
|
||||
static string Value(Dictionary<string, string> options, string key, string fallback) =>
|
||||
options.TryGetValue(key, out var value) && !string.IsNullOrWhiteSpace(value) ? value : fallback;
|
||||
|
||||
static void PrintHelp()
|
||||
{
|
||||
Console.WriteLine("""
|
||||
IPA trial worker
|
||||
|
||||
Commands:
|
||||
seed --artifact-root <dir> [--batch-id five-seed-trials] [--store <trial-results.cc>]
|
||||
score --patch-root <dir> --artifact-root <dir> [--batch-id round-001] [--store <trial-results.cc>] [--hypothesizer id]
|
||||
dump --store <trial-results.cc> --output <report.md>
|
||||
search --store <trial-results.cc> --query <text> --output <report.md> [--limit 12]
|
||||
show --store <trial-results.cc> --trial-id <id-or-candidate> --output <detail.md>
|
||||
|
||||
Agent-authored patch files must be named <targetId>__candidate-name.aqua.
|
||||
Known seed target ids: a, i, u, e, o, m, n, ng, l, r, s, z, f, v, th, p, b, t, d, k, mix-a, mix-m, mix-s, mix-p, mix-u.
|
||||
""");
|
||||
}
|
||||
359
tools/run-ipa-trial-loop.ps1
Normal file
359
tools/run-ipa-trial-loop.ps1
Normal file
|
|
@ -0,0 +1,359 @@
|
|||
param(
|
||||
[int]$Rounds = 5,
|
||||
[string]$BatchId = "five-seed-trials",
|
||||
[string]$CodexCommand = "codex",
|
||||
[string]$CodexModel = "gpt-5.3-codex",
|
||||
[switch]$AllowCodexSourceEdits,
|
||||
[switch]$SkipLocalTrialRun
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
function Resolve-RepoRoot {
|
||||
$dir = Get-Item -LiteralPath (Get-Location)
|
||||
while ($null -ne $dir) {
|
||||
if (Test-Path -LiteralPath (Join-Path $dir.FullName "AquaSynth.sln")) {
|
||||
return $dir.FullName
|
||||
}
|
||||
|
||||
$dir = $dir.Parent
|
||||
}
|
||||
|
||||
throw "Could not find AquaSynth.sln above the current directory."
|
||||
}
|
||||
|
||||
function New-Timestamp {
|
||||
return [DateTimeOffset]::UtcNow.ToString("yyyyMMddTHHmmssfff")
|
||||
}
|
||||
|
||||
function Invoke-LoggedProcess {
|
||||
param(
|
||||
[string]$FilePath,
|
||||
[string[]]$ArgumentList,
|
||||
[string]$LogPath,
|
||||
[hashtable]$Environment = @{}
|
||||
)
|
||||
|
||||
$logDir = Split-Path -Parent $LogPath
|
||||
New-Item -ItemType Directory -Force -Path $logDir | Out-Null
|
||||
$oldValues = @{}
|
||||
foreach ($key in $Environment.Keys) {
|
||||
$oldValues[$key] = [Environment]::GetEnvironmentVariable($key, "Process")
|
||||
[Environment]::SetEnvironmentVariable($key, [string]$Environment[$key], "Process")
|
||||
}
|
||||
|
||||
try {
|
||||
$display = "$FilePath $($ArgumentList -join ' ')"
|
||||
Add-Content -LiteralPath $LogPath -Value "[$([DateTimeOffset]::UtcNow.ToString("O"))] $display"
|
||||
& $FilePath @ArgumentList *>&1 | Tee-Object -FilePath $LogPath -Append
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "Command failed with exit code $LASTEXITCODE`: $display"
|
||||
}
|
||||
}
|
||||
finally {
|
||||
foreach ($key in $Environment.Keys) {
|
||||
[Environment]::SetEnvironmentVariable($key, $oldValues[$key], "Process")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-CodexAgent {
|
||||
param(
|
||||
[string]$RepoRoot,
|
||||
[string]$Prompt,
|
||||
[string]$OutputPath,
|
||||
[string]$LogPath,
|
||||
[string]$CodexCommand,
|
||||
[string]$CodexModel,
|
||||
[bool]$AllowSourceEdits
|
||||
)
|
||||
|
||||
$promptPath = [IO.Path]::ChangeExtension($OutputPath, ".prompt.md")
|
||||
New-Item -ItemType Directory -Force -Path (Split-Path -Parent $OutputPath) | Out-Null
|
||||
Set-Content -LiteralPath $promptPath -Value $Prompt
|
||||
|
||||
$args = @(
|
||||
"exec",
|
||||
"--cd", $RepoRoot,
|
||||
"--full-auto",
|
||||
"--output-last-message", $OutputPath
|
||||
)
|
||||
if ($CodexModel.Length -gt 0) {
|
||||
$args += @("--model", $CodexModel)
|
||||
}
|
||||
if (-not $AllowSourceEdits) {
|
||||
$args += @(
|
||||
"--config", 'shell_environment_policy.inherit="all"',
|
||||
"--config", 'sandbox_permissions=["workspace-write"]'
|
||||
)
|
||||
}
|
||||
$args += "-"
|
||||
|
||||
$previousErrorActionPreference = $ErrorActionPreference
|
||||
$ErrorActionPreference = "Continue"
|
||||
try {
|
||||
Get-Content -LiteralPath $promptPath -Raw | & $CodexCommand @args 2>&1 | Tee-Object -FilePath $LogPath -Append
|
||||
$exitCode = $LASTEXITCODE
|
||||
}
|
||||
finally {
|
||||
$ErrorActionPreference = $previousErrorActionPreference
|
||||
}
|
||||
|
||||
if ($exitCode -ne 0) {
|
||||
throw "External Codex agent failed with exit code $exitCode. See $LogPath."
|
||||
}
|
||||
}
|
||||
|
||||
function Assert-CandidateBatch {
|
||||
param(
|
||||
[string]$PatchDirectory
|
||||
)
|
||||
|
||||
$targetSets = @(
|
||||
@("a", "i", "u", "e", "o"),
|
||||
@("m", "n", "ng", "l", "r"),
|
||||
@("s", "z", "f", "v", "th"),
|
||||
@("p", "b", "t", "d", "k"),
|
||||
@("mix-a", "mix-m", "mix-s", "mix-p", "mix-u")
|
||||
)
|
||||
$files = @(Get-ChildItem -LiteralPath $PatchDirectory -Filter "*.aqua" -File -Recurse)
|
||||
if ($files.Count -lt 25) {
|
||||
throw "Hypothesis worker wrote $($files.Count) .aqua candidates; expected at least 25."
|
||||
}
|
||||
|
||||
$stems = $files | ForEach-Object { [IO.Path]::GetFileNameWithoutExtension($_.Name) }
|
||||
foreach ($targetSet in $targetSets) {
|
||||
foreach ($target in $targetSet) {
|
||||
$covered = $false
|
||||
foreach ($stem in $stems) {
|
||||
if ($stem.StartsWith("$target`__", [StringComparison]::OrdinalIgnoreCase)) {
|
||||
$covered = $true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (-not $covered) {
|
||||
throw "Hypothesis worker did not write a candidate for target '$target'. Candidate files must be named <targetId>__<hypothesis-name>.aqua."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$repoRoot = Resolve-RepoRoot
|
||||
$codex = Get-Command $CodexCommand -ErrorAction Stop
|
||||
$loopId = New-Timestamp
|
||||
$loopRoot = Join-Path $repoRoot "artifacts/parity/ipa-trial-loops/$loopId"
|
||||
$logRoot = Join-Path $loopRoot "logs"
|
||||
New-Item -ItemType Directory -Force -Path $loopRoot, $logRoot | Out-Null
|
||||
$storePath = Join-Path $loopRoot "ipa-trial-results.cc"
|
||||
$workerProject = Join-Path $repoRoot "tools/IpaTrialWorker/IpaTrialWorker.csproj"
|
||||
|
||||
$indexPath = Join-Path $loopRoot "loop-index.md"
|
||||
Set-Content -LiteralPath $indexPath -Value @"
|
||||
# IPA Trial Loop $loopId
|
||||
|
||||
- repo: $repoRoot
|
||||
- codex: $($codex.Source)
|
||||
- rounds: $Rounds
|
||||
- batch_id: $BatchId
|
||||
- allow_source_edits: $AllowCodexSourceEdits
|
||||
- trial_results_store: $storePath
|
||||
- worker: $workerProject
|
||||
- trial_shape: five rounds, each asking for five five-phoneme target-set candidates
|
||||
|
||||
"@
|
||||
|
||||
if (-not $SkipLocalTrialRun) {
|
||||
$seedLog = Join-Path $logRoot "seed-trial-run.log"
|
||||
Invoke-LoggedProcess `
|
||||
-FilePath "dotnet" `
|
||||
-ArgumentList @(
|
||||
"run",
|
||||
"--project",
|
||||
$workerProject,
|
||||
"--",
|
||||
"seed",
|
||||
"--artifact-root",
|
||||
$loopRoot,
|
||||
"--batch-id",
|
||||
$BatchId,
|
||||
"--store",
|
||||
$storePath
|
||||
) `
|
||||
-LogPath $seedLog
|
||||
}
|
||||
else {
|
||||
Add-Content -LiteralPath $indexPath -Value "- seed: skipped"
|
||||
}
|
||||
|
||||
for ($round = 1; $round -le $Rounds; $round++) {
|
||||
$roundId = "round-{0:000}" -f $round
|
||||
$roundDir = Join-Path $loopRoot $roundId
|
||||
$patchDir = Join-Path $roundDir "proposed-patches"
|
||||
New-Item -ItemType Directory -Force -Path $roundDir | Out-Null
|
||||
New-Item -ItemType Directory -Force -Path $patchDir | Out-Null
|
||||
Add-Content -LiteralPath $indexPath -Value "- ${roundId}: $roundDir"
|
||||
|
||||
$preEvidence = Join-Path $roundDir "semantic-search-before.md"
|
||||
$preDumpLog = Join-Path $logRoot "$roundId-search-before.log"
|
||||
Invoke-LoggedProcess `
|
||||
-FilePath "dotnet" `
|
||||
-ArgumentList @(
|
||||
"run",
|
||||
"--project",
|
||||
$workerProject,
|
||||
"--",
|
||||
"search",
|
||||
"--store",
|
||||
$storePath,
|
||||
"--query",
|
||||
"weak promising generalization vowel nasal fricative stop articulation owner source tract radiation gesture primitive dressing",
|
||||
"--limit",
|
||||
"40",
|
||||
"--output",
|
||||
$preEvidence
|
||||
) `
|
||||
-LogPath $preDumpLog
|
||||
|
||||
$hypothesisOutput = Join-Path $roundDir "hypothesis-agent.md"
|
||||
$hypothesisLog = Join-Path $logRoot "$roundId-hypothesis-agent.log"
|
||||
$hypothesisPrompt = @"
|
||||
You are an AquaSynth IPA loss-landscape hypothesis and patch-writing worker.
|
||||
|
||||
Read this semantic search result from the CultCache trial-results database:
|
||||
$preEvidence
|
||||
|
||||
The backing store is:
|
||||
$storePath
|
||||
|
||||
Use the worker as your search organ. Do not ask for crude full dumps unless retrieval fails:
|
||||
dotnet run --project tools/IpaTrialWorker/IpaTrialWorker.csproj -- search --store "$storePath" --query "<your evidence question>" --limit 20 --output "$roundDir/search-<topic>.md"
|
||||
dotnet run --project tools/IpaTrialWorker/IpaTrialWorker.csproj -- show --store "$storePath" --trial-id "<trial id from search>" --output "$roundDir/detail-<candidate>.md"
|
||||
|
||||
Useful repo surfaces:
|
||||
- docs/ipa-vocal-tract-roadmap.md
|
||||
- src/AquaSynth.Core/IpaGestureExperiment.cs
|
||||
- src/AquaSynth.Core/FaustExport.cs
|
||||
- src/AquaSynth.Faust/IpaTrialOrchestrator.cs
|
||||
- artifacts under $loopRoot
|
||||
|
||||
Task:
|
||||
1. Read the current loss landscape across gesture, log-mel, RMS, articulation, and primitive timeline evidence.
|
||||
2. Invent the next candidate patches across five five-phoneme target sets. Cover at least one target from each set, and prefer all 25 when the evidence points to reusable changes:
|
||||
vowels: a, i, u, e, o
|
||||
nasals/approximants: m, n, ng, l, r
|
||||
fricatives: s, z, f, v, th
|
||||
stops: p, b, t, d, k
|
||||
mixed generalization: mix-a, mix-m, mix-s, mix-p, mix-u
|
||||
3. Write at least twenty-five new .aqua candidates under:
|
||||
$patchDir
|
||||
Name each file <targetId>__<hypothesis-name>.aqua so the scorer can infer the reference target.
|
||||
4. Make the candidates test transferable hypotheses, not one-off phoneme golf. Reuse the same contour idea across multiple places/manners when that is the claim being tested.
|
||||
5. If source edits are allowed, refine the DSL or lowering code only when the evidence says the graph owner is wrong rather than the patch values.
|
||||
6. Write a concise hypothesis report to:
|
||||
$roundDir/hypotheses.md
|
||||
|
||||
Authority boundary:
|
||||
- Source edits allowed: $AllowCodexSourceEdits
|
||||
- Do not rewrite existing trial artifacts or the CultCache store directly.
|
||||
- The worker process owns measurement and .cc writes; you own candidate invention and optional source refinements.
|
||||
- Keep gesture_score, clean vocal/audio evidence, and full parity dressing separate.
|
||||
- Patch candidates may use FM, AM, modulators, envelopes, and extra animated voices, but call out when dressing is compensating for failed articulation.
|
||||
- Prefer fixing primitive owners over adding audio dressing when the evidence says air is missing.
|
||||
|
||||
Return a short final summary naming the files you wrote.
|
||||
"@
|
||||
Invoke-CodexAgent `
|
||||
-RepoRoot $repoRoot `
|
||||
-Prompt $hypothesisPrompt `
|
||||
-OutputPath $hypothesisOutput `
|
||||
-LogPath $hypothesisLog `
|
||||
-CodexCommand $CodexCommand `
|
||||
-CodexModel $CodexModel `
|
||||
-AllowSourceEdits:$AllowCodexSourceEdits
|
||||
|
||||
Assert-CandidateBatch -PatchDirectory $patchDir
|
||||
|
||||
$scoreLog = Join-Path $logRoot "$roundId-score-candidates.log"
|
||||
Invoke-LoggedProcess `
|
||||
-FilePath "dotnet" `
|
||||
-ArgumentList @(
|
||||
"run",
|
||||
"--project",
|
||||
$workerProject,
|
||||
"--",
|
||||
"score",
|
||||
"--patch-root",
|
||||
$patchDir,
|
||||
"--artifact-root",
|
||||
$roundDir,
|
||||
"--batch-id",
|
||||
$roundId,
|
||||
"--store",
|
||||
$storePath,
|
||||
"--hypothesizer",
|
||||
"external-codex-$roundId"
|
||||
) `
|
||||
-LogPath $scoreLog
|
||||
|
||||
$postEvidence = Join-Path $roundDir "semantic-search-after.md"
|
||||
$postDumpLog = Join-Path $logRoot "$roundId-search-after.log"
|
||||
Invoke-LoggedProcess `
|
||||
-FilePath "dotnet" `
|
||||
-ArgumentList @(
|
||||
"run",
|
||||
"--project",
|
||||
$workerProject,
|
||||
"--",
|
||||
"search",
|
||||
"--store",
|
||||
$storePath,
|
||||
"--query",
|
||||
"latest round weak promising transfer generalization vowel nasal fricative stop articulation owner source tract radiation gesture primitive dressing",
|
||||
"--limit",
|
||||
"60",
|
||||
"--output",
|
||||
$postEvidence
|
||||
) `
|
||||
-LogPath $postDumpLog
|
||||
|
||||
$evaluatorOutput = Join-Path $roundDir "evaluator-agent.md"
|
||||
$evaluatorLog = Join-Path $logRoot "$roundId-evaluator-agent.log"
|
||||
$evaluatorPrompt = @"
|
||||
You are an AquaSynth IPA trial science evaluator.
|
||||
|
||||
Read the updated semantic retrieval from the trial-results database:
|
||||
$postEvidence
|
||||
|
||||
Also read the hypothesis worker output:
|
||||
$hypothesisOutput
|
||||
|
||||
Round artifacts are under:
|
||||
$roundDir
|
||||
|
||||
Task:
|
||||
1. Evaluate whether the worker's proposed hypotheses follow from the measured evidence.
|
||||
2. Call out any hypothesis that is likely full-patch dressing impersonating articulation.
|
||||
3. Recommend exactly one next implementation/refinement target, with file/module owner and why.
|
||||
4. Write your evaluator report to:
|
||||
$roundDir/evaluation.md
|
||||
|
||||
Authority boundary:
|
||||
- Do not edit source files.
|
||||
- Do not rewrite trial artifacts.
|
||||
- Be explicit about which layer owns the failure: gesture DSL, primitive source, tract/radiation lowering, audio scoring, or orchestration.
|
||||
- Treat the CultCache .cc store as the shared trial memory that future hypothesis workers will query through the worker search/show commands.
|
||||
|
||||
Return a short final summary naming the report you wrote.
|
||||
"@
|
||||
Invoke-CodexAgent `
|
||||
-RepoRoot $repoRoot `
|
||||
-Prompt $evaluatorPrompt `
|
||||
-OutputPath $evaluatorOutput `
|
||||
-LogPath $evaluatorLog `
|
||||
-CodexCommand $CodexCommand `
|
||||
-CodexModel $CodexModel `
|
||||
-AllowSourceEdits:$false
|
||||
}
|
||||
|
||||
Write-Host "IPA trial loop complete: $loopRoot"
|
||||
Loading…
Reference in a new issue