From 1c31459c21b179b247b02e1df3f2345917f453e4 Mon Sep 17 00:00:00 2001 From: T-Crypt <132922589+T-Crypt@users.noreply.github.com> Date: Mon, 16 Nov 2026 07:28:34 -0700 Subject: [PATCH 1/2] fix(gateway): send a streamed start failure's error and DONE as two events sseFailure wrote "data: {error}\ndata: [DONE]\n\n", one SSE event with two data lines. A client joins those lines with a newline, so it read the error object followed by "[DONE]", failed to parse it and reported "malformed server-sent event JSON" with no reason. A blank line now ends the error event before DONE. TestKeepAliveFailureEmitsErrorEvent reads the stream the way an SSE client does and requires each event's data to be JSON or [DONE]. Closes #346 --- CHANGELOG.md | 6 ++++++ internal/gateway/gateway.go | 6 ++++-- internal/gateway/keepalive_test.go | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb819b9..fafd7f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ has a matching GitHub release whose body comes from its section below. ## [Unreleased] +### Fixed + +- A streamed request whose engine never got the card now ends with an error a client can read. The gateway sent + the error object and `[DONE]` as one server-sent event, so a client read the two lines together, failed to parse + them and reported "malformed server-sent event JSON" instead of the reason. They are now two events. (#346) + ## [v0.6.1] - 2026-11-15 ### Fixed diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 8f46b91..fb518a4 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -742,7 +742,9 @@ func ssePreamble(w http.ResponseWriter) { } // sseFailure answers a streaming request whose engine would not come up: -// the same OpenAI-shaped error the 503 path carries, as one SSE event. +// the same OpenAI-shaped error the 503 path carries, as one SSE event, then +// DONE as its own event. A client joins the data lines of one event, so the +// two on one event read as JSON followed by "[DONE]", which does not parse. func (g *Gateway) sseFailure(w http.ResponseWriter, engine, what string, err error) { code := "" var se *startError @@ -751,7 +753,7 @@ func (g *Gateway) sseFailure(w http.ResponseWriter, engine, what string, err err } g.log().Warn("gateway start failed", "engine", engine, "request", what, "code", code, "err", err) b, _ := json.Marshal(berthError(code, err.Error())) - fmt.Fprintf(w, "data: %s\ndata: [DONE]\n\n", b) + fmt.Fprintf(w, "data: %s\n\ndata: [DONE]\n\n", b) _ = http.NewResponseController(w).Flush() } diff --git a/internal/gateway/keepalive_test.go b/internal/gateway/keepalive_test.go index bdcec98..5a119f6 100644 --- a/internal/gateway/keepalive_test.go +++ b/internal/gateway/keepalive_test.go @@ -7,6 +7,7 @@ package gateway import ( "bufio" + "encoding/json" "io" "log/slog" "net/http" @@ -101,6 +102,23 @@ func TestKeepAliveFailureEmitsErrorEvent(t *testing.T) { if strings.Count(s, "data: [DONE]") != 1 { t.Fatalf("more than one DONE: %s", s) } + // An SSE client joins the data lines of one event with "\n" (the + // blank line ends the event), so the error object and DONE must be + // two events, or the client reads one event that is not JSON. + for ev := range strings.SplitSeq(strings.TrimSpace(s), "\n\n") { + var data []string + for line := range strings.SplitSeq(ev, "\n") { + if d, ok := strings.CutPrefix(line, "data: "); ok { + data = append(data, d) + } + } + if len(data) == 0 { + continue + } + if d := strings.Join(data, "\n"); d != "[DONE]" && !json.Valid([]byte(d)) { + t.Errorf("event data is neither JSON nor [DONE]: %q", d) + } + } } // /v1/berth/wait blocks until the engine answers, 404s a model nobody From ec934fa437bb232456b825d3a5988c9cf831479a Mon Sep 17 00:00:00 2001 From: T-Crypt <132922589+T-Crypt@users.noreply.github.com> Date: Mon, 16 Nov 2026 07:30:09 -0700 Subject: [PATCH 2/2] fix(notify): start the Windows tray at logon without a console service install --notify wrote the bare berth.exe to the user's Run key. berth.exe is a console program, so a console opened at every logon, and closing it sent CTRL_CLOSE_EVENT and ended the tray. The Run value is now wscript on a berth-notify.vbs stub, the launcher the other units use: no console, berth notify runs hidden, and its output goes to logs\berth-notify.log. Uninstall removes the stub with the Run value. Closes #344 --- CHANGELOG.md | 7 ++++ docs/harbor.md | 5 ++- internal/cli/notify_logon_windows.go | 40 ++++++++++++++++++++--- internal/cli/notify_logon_windows_test.go | 31 ++++++++++++++++++ 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 internal/cli/notify_logon_windows_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index eb819b9..f1c67b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ has a matching GitHub release whose body comes from its section below. ## [Unreleased] +### Fixed + +- On Windows, `berth service install --notify` no longer opens a console window at logon. The Run key started + `berth.exe notify` itself, a console program, and closing that window ended the tray. It now starts the tray + through the hidden launcher the other services use, and its output goes to `logs\berth-notify.log`. Run the + install again to replace an older Run value. (#344) + ## [v0.6.1] - 2026-11-15 ### Fixed diff --git a/docs/harbor.md b/docs/harbor.md index dd05659..7171a2b 100644 --- a/docs/harbor.md +++ b/docs/harbor.md @@ -133,7 +133,10 @@ for a phone. `berth service install --notify` runs it as a user unit. On Windows it is a tray icon in the taskbar: its notifications are Windows' own toasts (a click opens the harbor), its tooltip is the latest event, and its menu opens the harbor, turns the toasts off and on, - lists the last 10 events and quits. `berth service install --notify` starts it at logon. + lists the last 10 events and quits. `berth service install --notify` starts it at logon + with no console window, through the same hidden launcher as the other services, and its + output goes to `logs\berth-notify.log` in the state directory. Run the install again to + move an older install off the console. ## The harbor's views Everything the node collects shows in one of five tabs, and every panel answers a diff --git a/internal/cli/notify_logon_windows.go b/internal/cli/notify_logon_windows.go index 3dff828..41dcc5f 100644 --- a/internal/cli/notify_logon_windows.go +++ b/internal/cli/notify_logon_windows.go @@ -5,6 +5,9 @@ package cli import ( "errors" + "os" + "path/filepath" + "strings" "golang.org/x/sys/windows/registry" ) @@ -17,14 +20,30 @@ const ( notifyValue = "BerthNotify" ) +// notifyLauncher is the Run value that starts berth notify at logon, and +// the stub it runs. berth.exe is a console program, so the bare exe in the +// Run key opened a console at every logon, and closing that window ended +// the tray (issue 344). The stub is the one the other units start through: +// wscript opens no console, runs berth notify hidden and appends its +// output to the unit's log. +func notifyLauncher(exe string) (run, stub string, err error) { + u := notifyUnit(exe) + stub, err = unitStub(u) + return strings.Join(taskAction(u), " "), stub, err +} + // notifyAtLogon starts berth notify at the user's logon, or stops starting // it when on is false. It returns the change in words for the report. func notifyAtLogon(exe string, on, dryRun bool) (string, error) { + stubPath := unitStubPath(notifyUnit("").Name) if !on { line := `reg delete HKCU\` + logonRunKey + ` /v ` + notifyValue + ` /f` if dryRun { return line, nil } + if err := os.Remove(stubPath); err != nil && !errors.Is(err, os.ErrNotExist) { + return line, err + } k, err := registry.OpenKey(registry.CURRENT_USER, logonRunKey, registry.SET_VALUE) if err != nil { return line, nil // no Run key: nothing starts it @@ -35,15 +54,26 @@ func notifyAtLogon(exe string, on, dryRun bool) (string, error) { } return line, nil } - cmd := `"` + exe + `" notify` - line := `reg add HKCU\` + logonRunKey + ` /v ` + notifyValue + ` /d "` + cmd + `"` - if dryRun { - return line, nil + run, stub, err := notifyLauncher(exe) + line := `reg add HKCU\` + logonRunKey + ` /v ` + notifyValue + ` /d "` + run + `"` + if err != nil || dryRun { + return line, err + } + // UTF-16 with a BOM is a script wscript reads, whatever the paths hold; + // the log's directory exists before the first start, since the + // redirection creates the file but not the path to it. + for _, dir := range []string{filepath.Dir(stubPath), filepath.Dir(unitLogPath(notifyUnit("").Name))} { + if err := os.MkdirAll(dir, 0o755); err != nil { + return line, err + } + } + if err := os.WriteFile(stubPath, utf16le(stub), 0o644); err != nil { + return line, err } k, _, err := registry.CreateKey(registry.CURRENT_USER, logonRunKey, registry.SET_VALUE) if err != nil { return line, err } defer k.Close() - return line, k.SetStringValue(notifyValue, cmd) + return line, k.SetStringValue(notifyValue, run) } diff --git a/internal/cli/notify_logon_windows_test.go b/internal/cli/notify_logon_windows_test.go new file mode 100644 index 0000000..cc697ab --- /dev/null +++ b/internal/cli/notify_logon_windows_test.go @@ -0,0 +1,31 @@ +// Copyright 2026 Trevin Tindall (T-Crypt) +// SPDX-License-Identifier: Apache-2.0 + +package cli + +import ( + "strings" + "testing" +) + +// The tray starts at logon through the same hidden launcher as the other +// units: the Run value is wscript on a stub, never the console exe, so no +// window opens and closing one cannot end the tray (issue 344). +func TestNotifyAtLogonStartsTheTrayHidden(t *testing.T) { + exe := `C:\Program Files\berth\berth.exe` + run, stub, err := notifyLauncher(exe) + if err != nil { + t.Fatal(err) + } + if want := `wscript.exe //B //Nologo "` + unitStubPath("berth-notify") + `"`; run != want { + t.Errorf("Run value = %s, want %s", run, want) + } + if strings.Contains(run, "berth.exe") { + t.Errorf("the Run value starts the console exe: %s", run) + } + for _, want := range []string{`""` + exe + `"" notify`, unitLogPath("berth-notify"), ", 0, True)"} { + if !strings.Contains(stub, want) { + t.Errorf("stub lacks %q:\n%s", want, stub) + } + } +}