@pi-in-go/pigpen-herdr 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Michael Kinsy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # herdr reporter Package
2
+
3
+ Go extension, written against PiG's public Go extension SDK, that reports PiG's `idle`, `working` and `blocked` state to [herdr](https://github.com/herdrdev/herdr), the terminal workspace manager. Outside a herdr pane it registers nothing. It needs no Node.js: PiG builds it from source with a Go toolchain, and a Piglet Binary fuses it. It implements herdr's published [Add Herdr support to your agent](https://herdr.dev/docs/add-herdr-support/) contract: state, the resume command (herdr 0.9.2+) and release. See the [herdr Piglet](../../piglets/herdr/README.md) for what it reports and how it behaves.
4
+
5
+ **It works on its own.** Installing this Package is enough: no Piglet is needed.
6
+ The [`herdr` Piglet](../../piglets/herdr/README.md) is an optional composition that
7
+ selects this same Package. Use it directly from the Pigpen checkout root:
8
+
9
+ ```sh
10
+ pig package validate ./components/herdr
11
+ pig install ./components/herdr
12
+ ```
13
+
14
+ The local install references this checkout; keep it at its installed path and remove it with `pig remove` using the same path. The `herdr` Piglet selects this Package through `npm run stage`.
15
+
16
+ `PIG_BIN=/path/to/pig npm run test:go` runs `go test` in `extensions/herdr` against a fake `HERDR_BIN_PATH` (with `HERDR_SOCKET_PATH`) and a wire-level fake PiG host; it needs a Go toolchain and PiG's SDK (`PIG_SDK_DIR`, `PIG_SOURCE_ROOT` or `PIG_BIN` selects it). The tests are a port of the earlier TypeScript suite, case for case; the two `herdr:blocked` bus cases are named, skipped tests until the bridge below exists.
17
+
18
+ **Not ported: `herdr:blocked`.** The earlier TypeScript extension also listened to the `herdr:blocked` event on `pi.events`, the bus herdr's own Pi integration handles, so that another extension could mark a block that is not a UI prompt. This Go extension reports `blocked` only for UI prompts (`ui_prompt_start` and `ui_prompt_end`). PiG's Go SDK has the `pi.events` bridge since 0.4.0 (`Extension.Events()`), so the extension can listen for `herdr:blocked`; it does not yet, and the two tests that name it stay skipped until it does. It does not run on Pi: the TypeScript file was removed with this port, and Pi users keep herdr's own Pi integration.
19
+
20
+ [MIT](LICENSE), original work by Michael Kinsy; see `provenance.json`.
@@ -0,0 +1,55 @@
1
+ package herdr
2
+
3
+ import (
4
+ "strings"
5
+ "testing"
6
+ )
7
+
8
+ // The reporter runs on agent_start, agent_settled and prompt events. Each handler takes the lock, decides
9
+ // whether the state changed, and only a change queues a herdr call (a process exec, off the handler's path).
10
+ // These benchmarks keep the reporter released so no herdr process is started: they measure the handler logic.
11
+ //
12
+ // go test -run xxx -bench . -benchmem
13
+ func benchReporter() *reporter {
14
+ return &reporter{env: herdrEnv{bin: "/bin/true", paneID: "p1"}, rootSession: true, released: true}
15
+ }
16
+
17
+ func BenchmarkReporterStartSettled(b *testing.B) {
18
+ r := benchReporter()
19
+ path, id := "/home/u/.pig/agent/sessions/--work--/2026-01-01_abc.jsonl", "0199abcd-0000-7000-8000-000000000000"
20
+ b.ReportAllocs()
21
+ for i := 0; i < b.N; i++ {
22
+ r.agentStarted(path, id)
23
+ r.agentSettled()
24
+ }
25
+ }
26
+
27
+ // The same state again (a second agent_start in one run, a repeated prompt event) is dropped without queueing.
28
+ func BenchmarkReporterUnchangedState(b *testing.B) {
29
+ r := benchReporter()
30
+ r.agentStarted("/s.jsonl", "id")
31
+ b.ReportAllocs()
32
+ for i := 0; i < b.N; i++ {
33
+ r.agentStarted("/s.jsonl", "id")
34
+ }
35
+ }
36
+
37
+ func BenchmarkReportArgs(b *testing.B) {
38
+ r := benchReporter()
39
+ r.sessionPath, r.sessionID = "/home/u/.pig/agent/sessions/--work--/2026-01-01_abc.jsonl", "0199abcd-0000-7000-8000-000000000000"
40
+ rep := report{state: stateBlocked, message: "Allow this tool call?", seq: 7}
41
+ b.ReportAllocs()
42
+ for i := 0; i < b.N; i++ {
43
+ r.mu.Lock()
44
+ _ = r.reportArgsLocked(rep, true)
45
+ r.mu.Unlock()
46
+ }
47
+ }
48
+
49
+ func BenchmarkMessageFrom(b *testing.B) {
50
+ title := strings.Repeat("Allow bash to run a long command?\n with several lines\tand tabs ", 3)
51
+ b.ReportAllocs()
52
+ for i := 0; i < b.N; i++ {
53
+ _ = messageFrom(title)
54
+ }
55
+ }
@@ -0,0 +1,92 @@
1
+ package herdr_test
2
+
3
+ // A report herdr does not take must not stall or fail the agent, and it must not
4
+ // vanish without a trace either: a herdr that never hears about a pane is
5
+ // otherwise impossible to tell from one that was told. The reporter writes one
6
+ // line to stderr (which PiG collects as the extension's output) naming the cause.
7
+ // This came from the herdr-agent-state extension of pigpen pull request 2.
8
+
9
+ import (
10
+ "io"
11
+ "os"
12
+ "path/filepath"
13
+ "strings"
14
+ "testing"
15
+ "time"
16
+
17
+ herdr "github.com/MichaelKinsy/pigpen/components/herdr/extensions/herdr"
18
+ )
19
+
20
+ // captureStderr swaps os.Stderr for a pipe and returns what was written so far.
21
+ func captureStderr(t *testing.T) func() string {
22
+ t.Helper()
23
+ orig := os.Stderr
24
+ r, w, err := os.Pipe()
25
+ if err != nil {
26
+ t.Fatal(err)
27
+ }
28
+ os.Stderr = w
29
+ t.Cleanup(func() { os.Stderr = orig })
30
+ return func() string {
31
+ settle()
32
+ _ = w.SetDeadline(time.Now())
33
+ os.Stderr = orig
34
+ _ = w.Close()
35
+ b, _ := io.ReadAll(r)
36
+ return string(b)
37
+ }
38
+ }
39
+
40
+ func TestRefusedReportsAreExplained(t *testing.T) {
41
+ t.Run("a refused report names the cause", func(t *testing.T) {
42
+ host, h := startTUI(t)
43
+ h.failWithMessage(1, "error: pane p_9 not found")
44
+ read := captureStderr(t)
45
+ host.fire("session_start", map[string]any{"reason": "startup"})
46
+ h.waitForCalls(1)
47
+ got := read()
48
+ if !strings.Contains(got, "herdr: pane report-agent failed") || !strings.Contains(got, "pane p_9 not found") {
49
+ t.Fatalf("stderr = %q", got)
50
+ }
51
+ })
52
+
53
+ t.Run("a herdr that keeps refusing is reported once, not once per report", func(t *testing.T) {
54
+ host, h := startTUI(t)
55
+ h.failWithMessage(1, "error: failed to connect to herdr socket")
56
+ read := captureStderr(t)
57
+ host.fire("session_start", map[string]any{"reason": "startup"})
58
+ for range 3 {
59
+ host.fire("agent_start", nil)
60
+ host.fire("agent_settled", nil)
61
+ }
62
+ h.waitForCalls(2)
63
+ settle()
64
+ if n := strings.Count(read(), "failed to connect to herdr socket"); n != 1 {
65
+ t.Fatalf("the same failure was written %d times", n)
66
+ }
67
+ })
68
+
69
+ t.Run("a herdr binary that is gone is reported", func(t *testing.T) {
70
+ installFakeHerdr(t)
71
+ missing := filepath.Join(t.TempDir(), "missing")
72
+ t.Setenv("HERDR_BIN_PATH", missing)
73
+ host := startHost(t, herdr.Extension(), "tui")
74
+ read := captureStderr(t)
75
+ host.fire("session_start", map[string]any{"reason": "startup"})
76
+ if got := read(); !strings.Contains(got, "herdr: pane report-agent failed") || !strings.Contains(got, "missing") {
77
+ t.Fatalf("stderr = %q", got)
78
+ }
79
+ })
80
+
81
+ t.Run("a healthy herdr and an old herdr that refuses only the resume command stay silent", func(t *testing.T) {
82
+ host, h := startTUI(t)
83
+ h.refuseResume("old")
84
+ read := captureStderr(t)
85
+ host.fire("session_start", map[string]any{"reason": "startup"})
86
+ host.fire("agent_start", nil)
87
+ h.waitForCalls(2)
88
+ if got := read(); got != "" {
89
+ t.Fatalf("stderr = %q, want nothing", got)
90
+ }
91
+ })
92
+ }