Add graceful SIGTERM handling to the agent binary for Kubernetes compatibility.
When running in Kubernetes, pods receive SIGTERM before being killed. The agent needs to: 1. Catch SIGTERM 2. Finish the current iteration cleanly (don't interrupt mid-tool-call) 3. Emit a shutdown event to the trace 4. Exit with code 0
Without this, k8s will SIGKILL after the grace period, potentially corrupting workspace state.
The agent binary is at Omni/Agent.hs (or Omni/Agent/Engine.hs for the core loop).
There's no signal handling currently.
In the agent's main entry point, install a SIGTERM handler:
import System.Posix.Signals (installHandler, sigTERM, Handler(Catch))
import Control.Concurrent.MVar
-- Create shutdown flag
shutdownFlag <- newMVar False
-- Install handler
_ <- installHandler sigTERM (Catch $ putMVar shutdownFlag True) Nothing
In the main agent loop (Engine.hs), check the shutdown flag between iterations:
-- In the iteration loop
shouldShutdown <- readMVar shutdownFlag
when shouldShutdown $ do
emitEvent EventShutdown
exitSuccess
In Omni/Agent/Events.hs or Omni/Agent/Trace.hs, add:
data Event = ...
| EventShutdown { evTimestamp :: UTCTime }
With JSON encoding:
{"type": "shutdown", "timestamp": "...", "reason": "SIGTERM"}
The signal handler should NOT interrupt execution - just set a flag. The agent loop checks the flag at safe points (between iterations, after tool results).
1. Run agent with a long task
2. Send SIGTERM: kill -TERM <pid>
3. Verify:
For k8s testing:
kubectl delete pod <agent-pod> --grace-period=30
# Watch logs to see shutdown event
Omni/Agent.hs or Omni/Agent/Cli.hs — signal handler setupOmni/Agent/Engine.hs — check shutdown flag in loopOmni/Agent/Events.hs or Omni/Agent/Trace.hs — add EventShutdown typeOmni/Agent/Watch.hs — handle shutdown event in status updatesunix package (already a dep via Alpha)
Installed SIGTERM handler in agent CLI, added shutdown checks to sequential interpreter (emits shutdown event), and updated watch status/docs.