Observe native Claude automations and preserve pending timers
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -63,6 +64,65 @@ func (c *Conn) Buffered() bool { return c.reader.Buffered() > 0 }
|
||||
|
||||
func (c *Conn) Close() error { return c.conn.Close() }
|
||||
|
||||
// Status reads a snapshot without attaching or replacing the active delivery
|
||||
// client. It is bounded by ctx and closes the short-lived connection.
|
||||
func ReadStatus(ctx context.Context, socket string) (*Status, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
if d, ok := ctx.Deadline(); ok && d.Before(deadline) {
|
||||
deadline = d
|
||||
}
|
||||
opCtx, cancel := context.WithDeadline(ctx, deadline)
|
||||
defer cancel()
|
||||
dialer := net.Dialer{}
|
||||
conn, err := dialer.DialContext(opCtx, "unix", socket)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
stopCancel := make(chan struct{})
|
||||
go func() {
|
||||
select {
|
||||
case <-opCtx.Done():
|
||||
_ = conn.Close()
|
||||
case <-stopCancel:
|
||||
}
|
||||
}()
|
||||
defer close(stopCancel)
|
||||
if err := conn.SetDeadline(deadline); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := json.Marshal(Frame{Op: OpStatus})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data = append(data, '\n')
|
||||
if _, err := conn.Write(data); err != nil {
|
||||
if opCtx.Err() != nil {
|
||||
return nil, opCtx.Err()
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
const maxStatusBytes = 4 << 20
|
||||
line, err := bufio.NewReader(io.LimitReader(conn, maxStatusBytes+1)).ReadBytes('\n')
|
||||
if err != nil {
|
||||
if opCtx.Err() != nil {
|
||||
return nil, opCtx.Err()
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if len(line) > maxStatusBytes {
|
||||
return nil, fmt.Errorf("host status exceeds read limit")
|
||||
}
|
||||
var frame Frame
|
||||
if err := json.Unmarshal(line, &frame); err != nil || frame.Op != OpStatus || frame.Status == nil {
|
||||
return nil, fmt.Errorf("unexpected host status response")
|
||||
}
|
||||
return frame.Status, nil
|
||||
}
|
||||
|
||||
// Attach dials socket and starts delivery from offset. It returns the status
|
||||
// the host replied with; output frames follow on the connection.
|
||||
func Attach(socket string, offset int64, timeout time.Duration) (*Conn, *Status, error) {
|
||||
|
||||
Reference in New Issue
Block a user