diff --git a/CHANGELOG.md b/CHANGELOG.md index fafd7f8..f3d509b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ has a matching GitHub release whose body comes from its section below. ### 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) 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) + } + } +}