This protocol is a behavior contract for AI agents generating or refactoring PowerShell + WinForms desktop apps. Its purpose is to produce operational desktop UIs that are responsive, readable, maintainable, and safe to use under real work. This is not end-user documentation — it is a machine-readable operating contract that governs what the AI must do, must not do, and must explicitly state when it deviates. Every section is assigned a priority tier; when context or output length is constrained, lower tiers yield to higher ones — never the reverse.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/xxyoudeadpunkxx/ai-protocol-kit/llms.txt
Use this file to discover all available pages before exploring further.
Priority Tiers
If two rules from different tiers conflict, the lower tier yields and the conflict must be stated in the output.
| Tier | Applies to | Description |
|---|---|---|
| Tier A — Non-negotiable | Sections 0, 2, 3, 5, 6, 7 | Violation causes a crash, a frozen UI, or a wrong-environment launch. No exceptions. |
| Safety / Integrity — Never bypass silently | Secrets, destructive actions, privilege elevation, unsupported environments, test truthfulness | Binding regardless of tier. If they cannot be satisfied, stop or ask for user confirmation. |
| Tier B — Structural | Sections 1, 4, 8, 9, 10, 11, 12, 13 core DPI/readability/accessibility baseline, 14, 15, 18 | Deviation requires an explicit stated reason in the output. |
| Tier C — Polish | Extended accessibility polish in Section 13 | Best effort. If skipped, say so explicitly. |
| Process — Always active | Sections 16, 17, 19, 20 | Govern verification, acceptance, rejection, and final output even when lower-tier implementation details are skipped. |
Target Runtime Matrix
Before generating any code, the AI must declare the exact target environment:- Session type: Windows desktop interactive session only
- Shell version: Windows PowerShell 5.1, PowerShell 7+, or both
- Backing runtime: Expected .NET runtime — Framework or modern .NET
- Architecture: x64/x86 requirements (required when COM, native binaries, registry views, ODBC/OLEDB, or shell integration are involved)
- Modules: Required modules and minimum versions
- Launch command: Supported launch command
- Unsupported contexts: Linux, macOS, WSL, SSH remoting, WinRM, noninteractive scheduled task, headless CI — unless explicitly handled as static/test-only mode
Hard Constraints
Default Stack
Use native PowerShell + WinForms by default. Do not introduce third-party UI libraries unless the user explicitly asks for them and accepts the dependency cost. Minimum baseline — always required:powershell.exe -STA, pwsh.exe -STA, or an equivalent single-threaded apartment host. Fail early or show a clear warning if the current host is not STA.
Layout Rules
Use layout containers — never fixed-coordinate-only forms:| Container | Use for |
|---|---|
TableLayoutPanel | Main shell and major regions |
FlowLayoutPanel | Command bars, filters, status badges, button groups |
SplitContainer | Adjustable main/detail or content/log regions |
Dock and Anchor deliberately. Set a fixed minimum window size.
Avoid: deeply nested layout trees; rebuilding layout during resize; expensive refresh during resize; oversized command areas that force scrolling.
Operational dashboard shape:
Responsiveness and Dispatcher
An acceptable UI must remain movable, clickable where safe, repaintable, and visibly alive during work.Validate immediately
Validate input before starting any work. Do not begin an operation with unvalidated state.
Show a running state
Display a visible loading or running state. The user must know work is in progress.
Disable conflicting controls
Disable only the controls that would conflict with the running operation. Keep the rest accessible.
Run work off the UI thread
Run slow work outside the UI thread, or keep any synchronous section very short. Use the background execution strategies below.
Invoke / BeginInvoke calls across unrelated business logic.
The dispatcher helper must:
- Ignore updates after shutdown begins
- Ignore disposed or disposing controls
- Avoid forcing handle creation from a worker thread
- Marshal through
BeginInvoke/Invokeonly after the handle exists - Centralize exception handling for UI callbacks
- Be the only allowed path for worker-to-UI updates
Background Execution Strategies
| Scenario | Strategy |
|---|---|
| UI-only fast action | Run on UI thread only if it is visibly instantaneous |
| I/O wait or external command | Background worker, runspace, or helper process with progress callbacks |
| CPU-heavy work | Runspace pool, process isolation, or a bounded thread/job strategy |
| Risky or unstable external calls | Helper process isolation — prefer over in-process thread jobs |
Start-ThreadJob exists unless the dependency is declared.
PowerShell 7+ only: Thread jobs or ForEach-Object -Parallel may be used only with throttling and UI-safe marshaling.
Hard rule: Never pass WinForms controls into jobs, runspaces, or thread jobs. Every background operation must have cancellation/stop behavior, stale-result protection, and final UI restore.
Verification and Acceptance
Smoke testing is mandatory when the environment can support it. Minimum smoke test for a local Windows desktop UI:- Launch the app from its documented command
- Verify the window opens quickly and is visible
- Resize the window smaller than fullscreen
- Click refresh or the main safe action
- Trigger one slow or failing action if available
- Confirm the UI remains movable and repaintable during work
- Confirm logs/status update
- Close the app cleanly
/mnt extraction, CI, headless sandbox), run the strongest available checks: syntax/parser checks, static scans, dry runs that do not require a desktop. State exactly which checks were run and which were not possible.
Before claiming completion, scan for:
Start-Sleepin UI handlers- Long business logic directly in click/tick handlers
Application.DoEvents()in loops- Direct control updates from jobs/runspaces/thread callbacks
catchblocks that hide errors-ErrorAction SilentlyContinuewithout explicit justification- Unbounded
AppendText/Controls.Addloops - Timers not stopped/disposed
- Workers started before handle creation
- Missing form-closing cleanup
Protocol Text
The full source protocol text for reference and direct loading into an assistant session:2. Host And Message Pump
- Use a single UI owner thread.
- Start UI entrypoints with
powershell.exe -STA,pwsh.exe -STA, or an equivalent single-threaded apartment host. Fail early or show a clear warning if the current host is not STA. - Create controls only on the UI thread.
- Do not start background workers from the form constructor.
- Start workers only after the form handle exists, preferably in
Shown/Loador after the message pump has started. - Use
Application.Run($form)or anApplicationContext-equivalent owner pattern. - Do not use
ShowDialog()as the normal main-loop strategy unless the tool is explicitly modal and short-lived. - On shutdown, set a closing flag before disposing controls so late callbacks are ignored.
- If the visible UI process owns the form, do not launch that process hidden. Hidden launch is acceptable only for a helper process that is separate from the visible UI.
3. Hard Constraints (Canonical List)
This is the single source of truth for hard constraints. Sections 16 and 19 reference this list instead of restating it; do not treat a constraint as weaker because it only appears here.- Treat WinForms as event-driven desktop software, not as a quick form script.
- Keep the UI thread responsive.
- Do not perform slow work directly inside click handlers or timer handlers.
- Do not update controls from worker threads without marshaling back to the UI thread.
- Do not build the main UI with fixed coordinates.
- Do not hide operation status, progress, or errors.
Application.DoEvents()may be used only for tiny UI-state flushes. Never use it to mask long synchronous work or as the normal responsiveness strategy.- Do not use
System.Windows.Forms.Timer, or any timer type, as a background worker for slow work. - Do not append unbounded text/log output directly to a control; cap, batch, or page it.
- Do not start workers before the form handle exists.
- Do not leave timers running or disposed-of controls referenced after form closing.
- Do not state that a check, test, or scan was performed if it was not actually executed in this session. If a check was skipped, asserted from reading the code, or not possible in the current environment, label it explicitly as such — never as “verified.”
- Prefer stable, boring, operational UI over decorative UI.
4. Layout Rules
Use layout containers:TableLayoutPanelfor the main shell and major regions.FlowLayoutPanelfor command bars, filters, status badges, and button groups.SplitContainerwhen the user needs adjustable main/detail or content/log regions.DockandAnchordeliberately.- Fixed minimum window size.
- deeply nested layout trees;
- rebuilding layout during resize;
- expensive refresh during resize;
- fixed-coordinate-only forms;
- oversized command areas that force scrolling.
5. Responsiveness And Dispatcher
An acceptable UI must remain:- movable;
- clickable where safe;
- repaintable;
- visibly alive during work.
- validate immediately;
- show a running/loading state;
- disable only controls that would conflict with the operation;
- run the work outside the UI thread or keep the synchronous section very short;
- report progress or current step where practical;
- write to an app console/log;
- restore controls and cursor state in
finally.
Invoke / BeginInvoke calls across unrelated business logic.
The helper must:
- ignore updates after shutdown begins;
- ignore disposed or disposing controls;
- avoid forcing handle creation from a worker thread;
- marshal through
BeginInvoke/Invokeonly after the handle exists; - centralize exception handling for UI callbacks;
- be the only allowed path for worker-to-UI updates.
Control.InvokeAsync only when the declared target runtime supports it. Do not recommend it generically for Windows PowerShell 5.1 / .NET Framework targets.
6. Background Execution Strategy
Declare the chosen strategy per app or per action:- UI-only fast action: run on UI thread only if it is visibly instantaneous.
- I/O wait or external command: prefer background worker, runspace, or helper process with progress callbacks.
- CPU-heavy work: use runspace pool, process isolation, or a bounded thread/job strategy.
- Risky or unstable external calls: prefer helper process isolation over in-process thread jobs.
- Windows PowerShell 5.1 compatibility: do not assume
Start-ThreadJobexists unless the dependency is declared. - PowerShell 7+ only: thread jobs or
ForEach-Object -Parallelmay be used only with throttling and UI-safe marshaling. - Never pass WinForms controls into jobs, runspaces, or thread jobs.
- Every background operation must have cancellation/stop behavior, stale-result protection, and final UI restore.
7. Timer Policy
- Use
System.Windows.Forms.Timeronly for UI polling, debouncing, status refresh, and scheduling lightweight checks on the UI thread. - Do not use
System.Windows.Forms.Timerfor slow work. - If using
System.Threading.TimerorSystem.Timers.Timer, treat callbacks as worker-thread callbacks and marshal all UI changes through the dispatcher. - Prevent overlapping ticks with an
isRunningflag, lock, cancellation token, or operation sequence id. - Stop and dispose all timers on form closing.
8. Application State
Use one explicit application state object. Minimum state:IsClosing;- current operation id or sequence id;
- refresh/action in-progress flags;
- cancellation/stop flags;
- current config;
- last refresh time;
- log path;
- selected item or current context.
- the form is not closing;
- the operation id is still current;
- target controls still exist;
- the result is not stale.
9. Actions, Errors, And Destructive Operations
A button click handler should not contain business logic. Preferred shape:- visible start state;
- visible completion or failure state;
- error path;
- log entry.
- action wrappers must use terminating-error discipline;
- for cmdlets that may emit non-terminating errors, use
-ErrorAction Stopor scoped$ErrorActionPreference = 'Stop'; - do not rely on
$?,Write-Error, or console output as the only failure signal; - catch blocks must log both user-readable message and technical
ErrorRecord/ exception details; finallymust restore cursor, controls, running flags, and cancellation state.
- do not request elevation unless the specific action requires it;
- detect missing privileges and explain the required permission;
- never silently self-elevate;
- destructive actions must expose target, scope, consequence, and confirmation;
- prefer dry-run / preview for delete, overwrite, stop service, kill process, registry, ACL, network, or bulk file actions;
- logs must record destructive action intent and result without leaking secrets.
10. Logging And Status
Every operational app needs a visible app console or status log. Minimum log behavior:- timestamped entries;
- action started;
- action completed;
- action failed;
- short user-readable message;
- technical details in a file log when useful.
- idle/running/error state;
- last refresh time;
- connected/disconnected state where relevant;
- current action.
11. Loading And Refresh
Do not autoload heavy data before the form is shown. Safe startup:- off by default unless the user asked otherwise;
- toggleable;
- skipped while another refresh is running;
- bounded by a reasonable interval;
- logged.
12. Data Volume Controls
- Use
DataGridViewonly with bounded row counts, paging, lazy loading, orVirtualModefor large datasets. - Avoid expensive autosizing modes on large grids.
- Do not repopulate entire grids on every timer tick or resize.
- Preserve selection and scroll position where practical.
- For logs, cap visible lines and write full detail to file.
- Batch UI updates with
BeginUpdate/EndUpdatewhere supported. - Large lists, logs, and trees must be capped, paged, lazy-loaded, virtualized, or searchable.
13. DPI And Accessibility
Core DPI, readability, and accessibility baseline is Tier B. Environment-dependent validation and extended accessibility polish are Tier C only when they cannot be implemented or verified in the current runtime. Skipped items must be stated explicitly. Use:- Segoe UI or system UI font;
- consistent spacing;
- restrained colors;
- clear disabled, running, success, warning, and error states;
- readable contrast;
- compact command bars;
- no decorative elements that hide operational state.
AutoScaleMode, layout containers, minimum sizes, and DPI smoke tests.
Never satisfy DPI requirements only by testing fullscreen. Test at 125% and 150% scaling when possible.
Minimum accessibility baseline:
- predictable tab order;
- keyboard-reachable primary commands;
- tooltips for unclear actions;
- no color-only status communication;
- readable disabled and focused states;
AccessibleName/AccessibleDescriptionfor non-textual controls;- mnemonic
&markers for primary menu/buttons where appropriate; - default and cancel buttons when the form has a clear primary/cancel action;
- focus management after errors, refresh, and modal prompts;
- high-contrast usability.
14. Launch And Packaging
Every app must include a documented launch command. Minimum:- executable:
powershell.exeorpwsh.exe; -NoProfile;-STAon Windows;-Filewith an absolute or script-root-safe path;- working directory behavior;
- module dependency check;
- log directory behavior;
- whether
ExecutionPolicyoverride is used and why.
15. Code Organization
Avoid one giant script when the app is expected to grow. For small single-file tools, separate code into regions/functions:- bootstrap;
- config/state;
- UI helpers;
- layout construction;
- actions;
- background work;
- logging;
- event binding.
- stop timers;
- cancel pending workers when possible;
- ignore late worker callbacks after shutdown begins;
- flush file logs;
- dispose disposable UI, process, filesystem, and timer resources;
- avoid leaving helper processes running unless the user explicitly asked for that.
16. Verification, Static Scans, And Smoke Tests
Smoke testing is mandatory when the environment can support it. Minimum smoke test for a local Windows desktop UI:- launch the app from its documented command;
- verify the window opens quickly and is visible;
- resize the window smaller than fullscreen;
- click refresh or the main safe action;
- trigger one slow or failing action if available;
- confirm the UI remains movable and repaintable during work;
- confirm logs/status update;
- close the app cleanly.
/mnt extraction, CI, or a headless sandbox, it must still run the strongest available checks:
- syntax/parser checks;
- static scans;
- dry runs that do not require a desktop;
- headless tests when the stack supports them;
- launch checks only when a GUI is actually available.
Start-Sleepin UI handlers;- long business logic directly in click/tick handlers;
Application.DoEvents()in loops;- direct control updates from jobs/runspaces/thread callbacks;
catchblocks that hide errors;-ErrorAction SilentlyContinuewithout explicit justification;- unbounded
AppendText/Controls.Addloops; - timers not stopped/disposed;
- workers started before handle creation;
- missing form-closing cleanup.
17. Acceptance Checklist
A generated or refactored app is acceptable only if:- target runtime matrix is declared;
- the window opens quickly;
- the UI can be moved while work runs;
- controls do not require fullscreen to be usable;
- resizing does not visibly freeze the app;
- command areas do not force horizontal scrolling;
- logs/status explain what the app is doing;
- long work has visible progress or running state;
- errors are visible and recoverable;
- no unsafe cross-thread control updates are used;
- large lists/logs are capped, paged, lazy, or batched;
- app state, cancellation, and stale callback handling are explicit;
- the app remains usable at 125% and 150% DPI;
- required smoke tests were run, or unavailable checks were explicitly listed.
18. Complexity Escalation
PowerShell + WinForms is acceptable for operational tools, dashboards, admin consoles, and internal utilities. If the app requires multi-window navigation, complex state, heavy data binding, plugin architecture, installer/updater, enterprise accessibility, or long-term product maintenance, the agent must flag the complexity and propose alternatives:- keep PowerShell core with a compiled C# WinForms shell;
- WPF / WinUI for richer Windows UI;
- web/local dashboard if cross-platform or remote access matters.
19. Anti-Patterns
Reject or rewrite:- fixed-coordinate-only forms;
- giant top areas full of oversized buttons;
- blocking click handlers;
- hidden errors;
- silent failures;
- autorefresh that cannot be disabled;
- updating controls from worker threads;
- refreshing heavy data on resize;
- huge unbounded text/log updates;
- worker callbacks that can update disposed controls;
InvokeRequiredchecks before handle creation without a safe dispatcher;- timers used as background workers;
- introducing UI libraries before native WinForms has been proven insufficient;
- claiming a UI was tested when only syntax/static checks were performed.
20. AI Output Requirements
When proposing or generating a PowerShell + WinForms app, include:- chosen architecture;
- target runtime matrix;
- file layout if more than one file is used;
- changed files when modifying an existing app;
- changed behavior;
- entrypoint and run command;
- background/async strategy;
- UI dispatcher strategy;
- logging path and redaction behavior;
- known limitations;
- exact checks run;
- exact checks skipped and why;
- smoke test results, including any skipped checks and why they were skipped;
- residual risks;
- manual smoke checklist still required.