Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ 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)
- 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
Expand Down
5 changes: 4 additions & 1 deletion docs/harbor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 35 additions & 5 deletions internal/cli/notify_logon_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package cli

import (
"errors"
"os"
"path/filepath"
"strings"

"golang.org/x/sys/windows/registry"
)
Expand All @@ -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
Expand All @@ -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)
}
31 changes: 31 additions & 0 deletions internal/cli/notify_logon_windows_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
6 changes: 4 additions & 2 deletions internal/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}

Expand Down
18 changes: 18 additions & 0 deletions internal/gateway/keepalive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package gateway

import (
"bufio"
"encoding/json"
"io"
"log/slog"
"net/http"
Expand Down Expand Up @@ -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
Expand Down
Loading