Adds Pattern A (sync heartbeat) and Pattern B (async tool family) to the
4 long-running optimization tools, mirroring the existing Multistart pattern.
`zemax_optimize`, `zemax_global_search`, `zemax_hammer`, `zemax_constrained_optimize`
are sync blocking calls that can run for minutes. MCP clients (Claude Desktop
30s heartbeat, Claude Code 600s stream watchdog) would kill the connection
during long optimizations. This release lets these tools survive any client
watchdog and offers a true non-blocking variant.
The 4 existing sync tools now accept (auto-bound by C# MCP SDK):
- IProgress<ProgressNotificationValue> progress — emits notifications/progress
every 5s during the optimization (clients pass _meta.progressToken to receive)
- CancellationToken cancellationToken — client cancel propagates to
Zemax optimizer Cancel() + WaitForCompletion()
Backward compatible: clients not passing progressToken see zero behavior change.
Each long sync tool gets a non-blocking triplet:
- zemax_optimize_async / _status / _stop
- zemax_global_search_async / _status / _stop
- zemax_hammer_async / _status / _stop
- zemax_constrained_optimize_async / _status / _stop
Submit returns within ~100ms. Status polls are non-blocking. Stop signals
cancellation via the per-tool State's CancellationTokenSource. Per-tool
Singleton State (HammerState/GlobalSearchState/OptimizeState/
ConstrainedOptimizeState) shares progress between submit task and
status/stop tools.
zemax_multistart_optimize is unchanged (was already async with this pattern).
Per-tool State classes mirror the existing MultistartState design:
- volatile bool _isRunning + double fields under a single _lock
- CancellationTokenSource lifecycle managed by Reset / CreateCancellationToken / RequestCancellation
- Lifecycle methods: SetRunning / UpdateMerit (or UpdateProgress) / SetCompleted
Sync tools' Run+Wait blocking calls were replaced with Run() + 1s polling
loop that emits progress, honors ct.IsCancellationRequested, then issues
Zemax Cancel() + WaitForCompletion() on exit.
LMOptimizer.Optimize signature extended (additive, defaults preserve old
callers): + Action<int, double, double, double>? onProgress + CancellationToken.
Iteration boundary calls onProgress?.Invoke(...) and ThrowIfCancellationRequested().
- 23 xUnit tests covering State lifecycle (HammerState 12, GlobalSearchState 5,
OptimizeState 3, ConstrainedOptimizeState 3)
- 8 end-to-end smoke tests on Cooke 40-degree-field sample (sync hammer baseline,
hammer async lifecycle, hammer cancel, global_search async, optimize async with
cycles, constrained_optimize async, multistart regression)
Build: 0 errors. Unit tests pass. Live smoke tests pass on Zemax R23 standalone.
- src/ZemaxMCP.Core/Services/ConstrainedOptimization/{Hammer,GlobalSearch,Optimize,ConstrainedOptimize}State.cs
- src/ZemaxMCP.Server/Tools/Optimization/{Hammer,GlobalSearch,Optimize,ConstrainedOptimize}{Async,Status,Stop}Tool.cs (12 files)
- tests/ZemaxMCP.Core.Tests/{HammerState,GlobalSearchState,OptimizeState,ConstrainedOptimizeState}Tests.cs + .csproj
- docs/async-optimization.md (user-facing guide)
- docs/superpowers/specs/2026-04-29-zemax-mcp-long-task-async-design.md (design)
- docs/superpowers/plans/2026-04-29-zemax-mcp-long-task-async.md (impl plan)
- src/ZemaxMCP.Server/Program.cs (DI: 4 State Singletons + 12 tool registrations)
- src/ZemaxMCP.Server/Tools/Optimization/{HammerOptimization,GlobalSearch,Optimize,ConstrainedOptimize}Tool.cs (Pattern A)
- src/ZemaxMCP.Core/Services/ConstrainedOptimization/LMOptimizer.cs (onProgress + ct, additive)
- README.md (tools table)
- Directory.Packages.props (xUnit + test SDK)
- OpticStudioMCPServer.sln (Tests project)
1. Single Zemax instance: while an async optimization runs, all other zemax_*
calls block on ZemaxSession's SemaphoreSlim until the async task completes
or is cancelled. Status / stop tools are exempt (don't go through session).
Pre-existing limitation, now also documented in docs/async-optimization.md.
2. Cancellation latency:
- Hammer/GlobalSearch/Optimize: 1 polling tick + Zemax Cancel()+WaitForCompletion(),
usually 1–3 seconds.
- ConstrainedOptimize: up to one full LM iteration, which includes a Jacobian
recompute (N finite-difference evaluations for N variables) — can be tens of
seconds for systems with many variables.
3. Same-family async concurrency: each *_async tool rejects a second submit
while one is running ("Already running"). Cross-family concurrency is also
blocked by the SemaphoreSlim above.
4. No persistence: state is in-memory; server restart loses async job state.
5. Hammer's `automatic` parameter is reserved (currently a no-op).
See per-phase code-quality reviews in the design history. Items not blocking
this release but tracked for next iteration:
- TOCTOU window on submit (inherited from Multistart pattern)
- SetRunning ordering on Task.Run lambda entry
- IsRunning try/catch silent swallow (should log)
- 5000ms heartbeat magic number hoist
- ComputeJacobian internal cancellation granularity
- pollCycles field naming/semantic clarification
- Spec: docs/superpowers/specs/2026-04-29-zemax-mcp-long-task-async-design.md
- Plan: docs/superpowers/plans/2026-04-29-zemax-mcp-long-task-async.md
- Pattern source: existing MultistartState design under
src/ZemaxMCP.Core/Services/ConstrainedOptimization/MultistartState.cs
- Sister project (matlab-mcp-core-server): same problem family solved with a
generic JobManager design — see that project's docs/superpowers for
comparison rationale on the design choice ("per-tool 富状态 vs 通用 JobManager").