fix(cli): non-zero exit when an agent command emits an error envelope

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-22 00:13:29 +01:00
parent e1e5f245e1
commit dd181ef63c
2 changed files with 35 additions and 7 deletions
+14 -1
View File
@@ -1,6 +1,7 @@
package cli
import (
"errors"
"io"
"time"
@@ -9,6 +10,10 @@ import (
"git.dcglab.co.uk/steve/emcli/internal/store"
)
// errCommandFailed signals that a command emitted an error envelope, so the
// process should exit non-zero. The JSON envelope is the authoritative detail.
var errCommandFailed = errors.New("command reported error")
// Mailer is the subset of the IMAP client the agent commands use.
type Mailer interface {
SelectFolder(folder string) (uint32, uint32, error)
@@ -26,7 +31,15 @@ type Deps struct {
Out io.Writer
}
func (d Deps) emit(e Envelope) error { return e.Write(d.Out) }
func (d Deps) emit(e Envelope) error {
if err := e.Write(d.Out); err != nil {
return err // write failure (rare) — propagate as-is
}
if e.Error {
return errCommandFailed
}
return nil
}
func (d Deps) audit(account, action, target, result, reason string) {
_ = d.Store.Audit(d.Now(), store.AuditEntry{