feat(commands): multi-select ordered command pipeline with step reordering and single-pass composition (AI-05, AI-07)

This commit is contained in:
Yun Chan 2026-10-31 02:54:54 +09:00
parent f49acf0105
commit 10af20a0ad
157 changed files with 29455 additions and 913 deletions

View file

@ -0,0 +1,15 @@
# Windows typing fixture
Build without downloading dependencies:
```powershell
pwsh -File scripts/verification/typing-fixture/build.ps1
```
Open `test-results/typing-load/fixture/D3ROTypingFixture.exe` from the interactive desktop, then click **Start verification**. The default report is `test-results/typing-load/current-fixture.json`; an optional first executable argument chooses another report file.
The fixture only edits its own multiline textbox. It selects an already installed Korean input language on its own UI thread and restores that selection afterward. It never installs an IME, changes the default input method, reads other app documents, launches D3RO, or modifies D3RO configuration. Foreground/focus changes cancel injection; pressing Stop or closing the fixture cancels CPU workers. Each CPU phase stops after at most 20 seconds even if the UI is delayed.
Calibration must produce `안녕하세요 ` from the scan-code sequence `dkssudgktpdy ` and receive native `WM_IME_COMPOSITION` messages before any result is accepted. This prevents automatic Latin-to-Korean correction alone from masquerading as IME proof. The matrix uses 60/30/15 ms requested per-key spacing, both idle and optional CPU load. Read the recorded timestamps for achieved pacing. CPU load uses logical processor count minus one dedicated workers and records actual system/fixture CPU utilization. The sequence contains `안녕하세요 감사합니다 반갑습니다 ` repeated three times. The **Latin layout correction** option keeps the same keys and expected Korean text but switches this fixture's IME off after calibration; use this only when the D3RO configuration under test should automatically correct layout mistakes.
Record the application build/configuration under test in the label. Preserve each report under a distinct name before rerunning. Exact text match, native accepted-event counts, observed key counts, per-case text changes, focus validity, pacing and CPU overlap are separate evidence. Synthetic scan codes exercise OS input/IME and the global hook; they are not physical-hardware keyboard proof. This WinForms surface also does not prove Chromium/Electron or terminal behavior.

View file

@ -0,0 +1,365 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;
// Test fixture only: sends scan codes solely while its own textbox has focus.
// It never changes the system/default IME, launches D3RO, or inspects user documents.
internal static class Program
{
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string output = args.Length > 0 ? Path.GetFullPath(args[0]) : Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "current-fixture.json"));
Application.Run(new Fixture(output));
}
}
internal sealed class Fixture : Form
{
private readonly ObservedTextBox input = new ObservedTextBox();
private readonly Label status = new Label();
private readonly Button start = new Button();
private readonly Button stop = new Button();
private readonly CheckBox stress = new CheckBox();
private readonly ComboBox mode = new ComboBox();
private readonly TextBox runLabel = new TextBox();
private readonly string output;
private InputLanguage originalLanguage;
private CancellationTokenSource cancellation;
private RunReport report;
private CaseReport current;
private Stopwatch clock = new Stopwatch();
private readonly System.Windows.Forms.Timer sampler = new System.Windows.Forms.Timer();
private ulong lastIdle, lastKernel, lastUser;
private long lastCpuTicks, lastWallTicks;
private int ownPid;
public Fixture(string reportPath)
{
output = reportPath;
ownPid = Process.GetCurrentProcess().Id;
Text = "D3RO Typing Verification — isolated input fixture";
Width = 940;
Height = 540;
StartPosition = FormStartPosition.CenterScreen;
var panel = new FlowLayoutPanel { Dock = DockStyle.Top, Height = 90, Padding = new Padding(8) };
start.Text = "Start verification";
start.Width = 145;
stop.Text = "Stop";
stop.Enabled = false;
stop.Width = 70;
stress.Text = "Include CPU load";
stress.Checked = true;
stress.Width = 145;
mode.DropDownStyle = ComboBoxStyle.DropDownList;
mode.Items.AddRange(new object[] { "Hangul IME", "Latin layout correction" });
mode.SelectedIndex = 0;
mode.Width = 165;
runLabel.Text = "label-current-app-state";
runLabel.Width = 210;
panel.Controls.Add(start);
panel.Controls.Add(stop);
panel.Controls.Add(stress);
panel.Controls.Add(mode);
panel.Controls.Add(runLabel);
panel.Controls.Add(new Label { Text = "The test types only into the box below. Changing focus aborts. No clipboard or Unicode text injection.", AutoSize = true });
input.Multiline = true;
input.AcceptsReturn = true;
input.ScrollBars = ScrollBars.Vertical;
input.Dock = DockStyle.Fill;
input.Font = new Font("Malgun Gothic", 20);
input.AccessibleName = "Verification input only";
input.ImeMode = ImeMode.Hangul;
status.Dock = DockStyle.Bottom;
status.Height = 75;
status.Text = "Ready. Select the app-state label, then Start verification.\r\nReport: " + output;
Controls.Add(input);
Controls.Add(panel);
Controls.Add(status);
start.Click += async delegate { await Run(); };
stop.Click += delegate { if (cancellation != null) cancellation.Cancel(); };
input.KeyDown += delegate { if (current != null) current.ObservedKeyDowns++; };
input.KeyUp += delegate { if (current != null) current.ObservedKeyUps++; };
input.TextChanged += delegate {
if (current != null) current.Changes.Add(new TextObservation { ElapsedMs = clock.ElapsedMilliseconds, Text = input.Text });
};
input.ImeComposition += delegate(bool committed) {
if (current != null) {
current.ImeCompositionMessages++;
if (committed) current.ImeResultMessages++;
}
};
sampler.Interval = 500;
sampler.Tick += delegate { SampleCpu(); };
FormClosing += delegate { if (cancellation != null) cancellation.Cancel(); RestoreLanguage(); };
}
private async Task Run()
{
if (cancellation != null) return;
cancellation = new CancellationTokenSource();
var token = cancellation.Token;
start.Enabled = false;
stop.Enabled = true;
mode.Enabled = false;
stress.Enabled = false;
runLabel.Enabled = false;
bool layout = mode.SelectedIndex == 1;
report = new RunReport {
StartedAtUtc = DateTime.UtcNow.ToString("o"), Label = runLabel.Text,
ProcessId = ownPid, WindowHandle = Handle.ToInt64(), LogicalProcessors = Environment.ProcessorCount,
Mode = layout ? "latin-layout-correction" : "hangul-ime",
ReportPath = output, Status = "running", Injection = "SendInput KEYEVENTF_SCANCODE key-down/key-up pairs",
Limitations = "Synthetic scan codes exercise Windows IME but do not prove hardware keyboard delivery. CPU workers are bounded; compare observed CPU samples. D3RO version/config is not inferred by this fixture."
};
clock.Restart();
InitializeCpu();
sampler.Start();
Save();
try {
originalLanguage = InputLanguage.CurrentInputLanguage;
InputLanguage korean = InputLanguage.InstalledInputLanguages.Cast<InputLanguage>().FirstOrDefault(l => l.Culture.TwoLetterISOLanguageName == "ko");
if (korean == null) throw new InvalidOperationException("No installed Korean input language; the fixture will not install or change system settings.");
InputLanguage.CurrentInputLanguage = korean;
input.ImeMode = ImeMode.Hangul;
input.Focus();
await Task.Delay(500, token);
report.KeyboardLayout = Native.GetKeyboardLayout(0).ToInt64().ToString("X");
// Calibration always proves IME composition independently of the layout-correction mode.
CaseReport calibration = await TypeCase("calibration", "dkssudgktpdy ", "안녕하세요 ", 60, false, token);
report.CalibrationPassed = calibration.ExactMatch && calibration.ImeCompositionMessages > 0;
if (!report.CalibrationPassed) throw new InvalidOperationException("Hangul calibration needs exact text and native IME composition messages. Result is not accepted as IME typing proof.");
if (layout) input.ImeMode = ImeMode.Off;
foreach (bool cpu in stress.Checked ? new bool[] { false, true } : new bool[] { false }) {
foreach (int pace in new int[] { 60, 30, 15 }) {
token.ThrowIfCancellationRequested();
string keys = Repeat("dkssudgktpdy rkatkgkqslek qksrkqtmqslek ", 3);
string expected = Repeat("안녕하세요 감사합니다 반갑습니다 ", 3);
await TypeCase((cpu ? "cpu" : "idle") + "-" + pace, keys, expected, pace, cpu, token);
}
}
report.Status = report.Cases.All(c => c.ExactMatch) ? "passed" : "failed";
} catch (OperationCanceledException) {
report.Status = "aborted";
report.Error = "Cancelled or foreground/focus changed; input was stopped.";
} catch (Exception error) {
report.Status = "failed";
report.Error = error.GetType().Name + ": " + error.Message;
} finally {
sampler.Stop();
current = null;
report.FinishedAtUtc = DateTime.UtcNow.ToString("o");
report.ElapsedMs = clock.ElapsedMilliseconds;
Save();
RestoreLanguage();
cancellation.Dispose();
cancellation = null;
start.Enabled = true;
stop.Enabled = false;
mode.Enabled = true;
stress.Enabled = true;
runLabel.Enabled = true;
status.Text = report.Status.ToUpperInvariant() + ": " + report.Cases.Count(c => c.ExactMatch) + "/" + report.Cases.Count + " exact text matches.\r\n" + (report.Error ?? "") + "\r\n" + output;
}
}
private async Task<CaseReport> TypeCase(string name, string keys, string expected, int pace, bool load, CancellationToken token)
{
AssertOwnFocus(token);
current = null;
input.Clear(); // Only the fixture's own test field; never another app.
await Task.Delay(200, token);
current = new CaseReport { Name = name, PaceMs = pace, Expected = expected, Keys = keys, StartedAtUtc = DateTime.UtcNow.ToString("o"), StartedElapsedMs = clock.ElapsedMilliseconds };
report.Cases.Add(current);
var item = current;
using (var workers = new CpuWorkers(load ? Math.Max(1, Environment.ProcessorCount - 1) : 0, 20000)) {
item.CpuWorkers = workers.Count;
if (load) await Task.Delay(900, token);
status.Text = "Running " + name + ". Keep focus inside the test input.\r\n" + output;
try {
foreach (char key in keys) {
AssertOwnFocus(token);
long requested = clock.ElapsedMilliseconds;
uint sent = Native.SendScanPair(key);
item.SentEvents += (int)sent;
item.RequestedEvents += 2;
item.KeyTimesMs.Add(requested);
if (sent != 2) throw new InvalidOperationException("SendInput inserted " + sent + "/2 events; no retry.");
await Task.Delay(pace, token);
}
await Task.Delay(1500, token);
AssertOwnFocus(token);
item.Actual = input.Text;
item.ExactMatch = string.Equals(item.Actual, item.Expected, StringComparison.Ordinal);
item.FirstMismatchIndex = FirstMismatch(item.Expected, item.Actual);
} finally {
item.Actual = input.Text;
item.ElapsedMs = clock.ElapsedMilliseconds - item.StartedElapsedMs;
item.FinishedAtUtc = DateTime.UtcNow.ToString("o");
Save();
current = null;
}
}
return item;
}
private void AssertOwnFocus(CancellationToken token)
{
token.ThrowIfCancellationRequested();
if (Native.GetForegroundWindow() != Handle || Native.GetFocus() != input.Handle) {
cancellation.Cancel();
throw new OperationCanceledException("Fixture textbox is not the active focused receiver.");
}
}
private void InitializeCpu()
{
Native.ReadSystemTimes(out lastIdle, out lastKernel, out lastUser);
lastCpuTicks = Process.GetCurrentProcess().TotalProcessorTime.Ticks;
lastWallTicks = Stopwatch.GetTimestamp();
}
private void SampleCpu()
{
ulong idle, kernel, user;
Native.ReadSystemTimes(out idle, out kernel, out user);
ulong total = kernel - lastKernel + user - lastUser;
double system = total > 0 ? 100.0 * (total - Math.Min(total, idle - lastIdle)) / total : 0;
long cpu = Process.GetCurrentProcess().TotalProcessorTime.Ticks;
long now = Stopwatch.GetTimestamp();
double seconds = (double)(now - lastWallTicks) / Stopwatch.Frequency;
double process = seconds > 0 ? 100.0 * (cpu - lastCpuTicks) / TimeSpan.TicksPerSecond / seconds / Environment.ProcessorCount : 0;
report.CpuSamples.Add(new CpuSample { ElapsedMs = clock.ElapsedMilliseconds, Case = current == null ? "between" : current.Name, SystemPercent = system, FixturePercent = process });
lastIdle = idle; lastKernel = kernel; lastUser = user; lastCpuTicks = cpu; lastWallTicks = now;
}
private void Save()
{
Directory.CreateDirectory(Path.GetDirectoryName(output));
var json = new JavaScriptSerializer { MaxJsonLength = 16 * 1024 * 1024 }.Serialize(report);
File.WriteAllText(output, json, new UTF8Encoding(false));
}
private void RestoreLanguage()
{
if (originalLanguage == null) return;
try { InputLanguage.CurrentInputLanguage = originalLanguage; } catch { }
originalLanguage = null;
}
private static string Repeat(string value, int count) { return string.Concat(Enumerable.Repeat(value, count)); }
private static int FirstMismatch(string expected, string actual)
{
for (int i = 0; i < Math.Min(expected.Length, actual.Length); i++) if (expected[i] != actual[i]) return i;
return expected.Length == actual.Length ? -1 : Math.Min(expected.Length, actual.Length);
}
}
internal sealed class CpuWorkers : IDisposable
{
private readonly CancellationTokenSource stop = new CancellationTokenSource();
private readonly List<Thread> threads = new List<Thread>();
public int Count { get { return threads.Count; } }
public CpuWorkers(int count, int maximumMs)
{
for (int i = 0; i < count; i++) {
var thread = new Thread(delegate() {
var timer = Stopwatch.StartNew();
while (!stop.IsCancellationRequested && timer.ElapsedMilliseconds < maximumMs) Thread.SpinWait(10000);
});
thread.IsBackground = true;
thread.Name = "D3RO fixture bounded CPU worker";
threads.Add(thread);
thread.Start();
}
}
public void Dispose()
{
stop.Cancel();
foreach (Thread thread in threads) thread.Join(1000);
stop.Dispose();
}
}
internal sealed class ObservedTextBox : TextBox
{
internal event Action<bool> ImeComposition;
protected override void WndProc(ref Message message)
{
if (message.Msg == 0x10f && ImeComposition != null) {
ImeComposition((message.LParam.ToInt64() & 0x800) != 0);
}
base.WndProc(ref message);
}
}
internal static class Native
{
[StructLayout(LayoutKind.Sequential)] private struct KeyboardInput { public ushort Vk, Scan; public uint Flags, Time; public IntPtr Extra; }
[StructLayout(LayoutKind.Explicit, Size = 40)] private struct Input { [FieldOffset(0)] public uint Type; [FieldOffset(8)] public KeyboardInput Keyboard; }
[StructLayout(LayoutKind.Sequential)] private struct FileTime { public uint Low, High; public ulong Value { get { return ((ulong)High << 32) | Low; } } }
[DllImport("user32.dll")] internal static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")] internal static extern IntPtr GetFocus();
[DllImport("user32.dll")] internal static extern IntPtr GetKeyboardLayout(uint thread);
[DllImport("user32.dll", SetLastError = true)] private static extern uint SendInput(uint count, Input[] inputs, int size);
[DllImport("kernel32.dll")] private static extern bool GetSystemTimes(out FileTime idle, out FileTime kernel, out FileTime user);
internal static void ReadSystemTimes(out ulong idle, out ulong kernel, out ulong user)
{
FileTime i, k, u;
if (!GetSystemTimes(out i, out k, out u)) throw new InvalidOperationException("GetSystemTimes failed");
idle = i.Value; kernel = k.Value; user = u.Value;
}
internal static uint SendScanPair(char key)
{
ushort scan;
if (key == ' ') scan = 0x39;
else {
string[] rows = { "qwertyuiop", "asdfghjkl", "zxcvbnm" };
int[] starts = { 0x10, 0x1e, 0x2c };
scan = 0;
for (int row = 0; row < rows.Length; row++) {
int index = rows[row].IndexOf(key);
if (index >= 0) { scan = (ushort)(starts[row] + index); break; }
}
if (scan == 0) throw new InvalidOperationException("Unsupported synthetic corpus key");
}
var pair = new Input[2];
pair[0].Type = pair[1].Type = 1;
pair[0].Keyboard.Scan = pair[1].Keyboard.Scan = scan;
pair[0].Keyboard.Flags = 8;
pair[1].Keyboard.Flags = 8 | 2;
return SendInput(2, pair, Marshal.SizeOf(typeof(Input)));
}
}
internal sealed class RunReport
{
public string StartedAtUtc, FinishedAtUtc, Label, Mode, ReportPath, Status, Error, Injection, Limitations, KeyboardLayout;
public int ProcessId, LogicalProcessors;
public long WindowHandle, ElapsedMs;
public bool CalibrationPassed;
public List<CaseReport> Cases = new List<CaseReport>();
public List<CpuSample> CpuSamples = new List<CpuSample>();
}
internal sealed class CaseReport
{
public string Name, Expected, Actual, Keys, StartedAtUtc, FinishedAtUtc;
public int PaceMs, CpuWorkers, SentEvents, RequestedEvents, ObservedKeyDowns, ObservedKeyUps, FirstMismatchIndex, ImeCompositionMessages, ImeResultMessages;
public long StartedElapsedMs, ElapsedMs;
public bool ExactMatch;
public List<long> KeyTimesMs = new List<long>();
public List<TextObservation> Changes = new List<TextObservation>();
}
internal sealed class TextObservation { public long ElapsedMs; public string Text; }
internal sealed class CpuSample { public long ElapsedMs; public string Case; public double SystemPercent, FixturePercent; }

View file

@ -0,0 +1,12 @@
param([string]$OutputDirectory = '')
$ErrorActionPreference = 'Stop'
$repoRoot = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '../../..'))
if (-not $OutputDirectory) { $OutputDirectory = Join-Path $repoRoot 'test-results/typing-load/fixture' }
$OutputDirectory = [IO.Path]::GetFullPath($OutputDirectory)
$compiler = Join-Path $env:WINDIR 'Microsoft.NET/Framework64/v4.0.30319/csc.exe'
if (-not (Test-Path -LiteralPath $compiler)) { throw 'The installed .NET Framework x64 C# compiler is required.' }
New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null
$target = Join-Path $OutputDirectory 'D3ROTypingFixture.exe'
& $compiler /nologo /target:winexe /platform:x64 /optimize+ /r:System.Windows.Forms.dll /r:System.Drawing.dll /r:System.Web.Extensions.dll "/out:$target" (Join-Path $PSScriptRoot 'TypingFixture.cs')
if ($LASTEXITCODE -ne 0) { throw "Fixture compilation failed: $LASTEXITCODE" }
Write-Output $target