Skip to main content

zig_core/
self_cmd.rs

1//! Commands that act on the currently running zig/zag session.
2//!
3//! The session's process id is resolved from the `ZAG_PROCESS_ID`
4//! environment variable injected by `zag-agent` when the process was
5//! spawned. These commands are designed to be invoked by agents from
6//! inside their own interactive session (e.g. an interactive `consult`
7//! step calling `zig self terminate` to hand control back to the
8//! workflow engine).
9
10use crate::error::ZigError;
11
12/// Instruction appended to interactive agents' system prompts so they
13/// know how to exit the session when they finish. Exposed for tests in
14/// `run_tests.rs` and used from `run::build_agent_config`.
15pub const INTERACTIVE_SELF_TERMINATE_INSTRUCTION: &str = "\n\n\
16    When you are finished with this session, exit cleanly by running \
17    `zig self terminate` in your shell. Do not wait for the user to close \
18    the session — self-terminate so the workflow can proceed to the next step.";
19
20/// Terminate the current running session (SIGTERM) by delegating to
21/// `zag-orch`'s process registry. Resolves `"self"` via
22/// `ZAG_PROCESS_ID`; errors if that env var is not set.
23pub fn terminate() -> Result<(), ZigError> {
24    zag_orch::ps::request_kill("self")
25        .map_err(|e| ZigError::Zag(format!("self terminate failed: {e}")))
26}