# Sequence diagrams ## Script execution ```mermaid sequenceDiagram participant U as "Browser" participant API as "Backend" participant N as "NATS / JetStream" participant A as "Agent" participant PS as "System.Management.Automation" U->>API: "POST /api/jobs" API->>N: "durable script command" N->>A: "agent command.script" A->>A: "record JobId idempotently" A-->>N: "Ack command" A->>PS: "Runspace + prelude + AddScript + AddParameter" PS-->>A: "Output / Progress / Errors" A->>N: "job output / progress" N->>API: "consume job events" API->>N: "browser session job event" N-->>U: "live WebSocket event" PS-->>A: "completion" A->>N: "job state = Succeeded/Failed" N->>API: "persist state" API->>N: "browser session update" N-->>U: "final state" ``` ## Enrollment ```mermaid sequenceDiagram participant A as "Agent" participant API as "Backend" participant N as "NATS" A->>A: "CIM stable hardware fingerprint" A->>A: "SHA-256 deterministic ClientId" A->>A: "Generate User NKey pair" A->>API: "HTTPS bootstrap: token + ClientId + public NKey" API->>API: "Validate/consume bootstrap token" API->>API: "Issue scoped NATS user JWT" API-->>A: "EnrollmentId + User JWT" A->>A: "DPAPI protect private seed + JWT" A->>A: "Clear BootstrapToken registry value" A->>N: "WSS connect using JWT + NKey proof" N-->>A: "authenticated" A->>N: "presence + inventory" ``` ## Remote shell ```mermaid sequenceDiagram participant U as "Browser / xterm" participant API as "Backend" participant N as "NATS" participant A as "Agent" participant C as "ConPTY" U->>API: "create shell for ClientId" API->>API: "authorize" API->>N: "privileged shell.open command" N->>A: "shell.open" A->>C: "create pseudoconsole + shell" A->>N: "shell state = open" API->>N: "browser session state/output bridge" N-->>U: "shell open" U->>N: "browser session shell input" N->>API: "input" API->>N: "authorized agent shell.input" N->>A: "input" A->>C: "write input" C-->>A: "terminal output" A->>N: "shell output" N->>API: "output" API->>N: "browser shell output" N-->>U: "live terminal output" ``` ## Script sequence across a restart ```mermaid sequenceDiagram participant U as "Browser" participant API as "Backend" participant W as "Sequence worker + dispatcher" participant N as "NATS / JetStream" participant A as "Agent" U->>API: "POST /api/jobs (sequenceId)" API->>API: "sequence run, a run per step (sealed snapshot), device run + step 1 job" W->>N: "durable script command (step 1, its Restart On Completion)" N->>A: "agent command.script" A->>N: "job state = Succeeded, exit 3010, Restart Requested" N->>API: "batched job states stored" W->>W: "classify exit code: Warning; restart pending: wait" A->>API: "restart request (result confirmed)" A->>A: "restart (RestartCoordinator)" A->>API: "inventory with a newer boot time" API->>API: "job Restarted" W->>W: "next step's condition; conditional advance + step 2 job" W->>N: "durable script command (step 2)" N->>A: "agent command.script" A->>N: "job state = Succeeded" W->>W: "no step left: device run Succeeded" API-->>U: "live job and sequence events" ```