From 478dc155e89302777af820dbdaae00b95c0da010 Mon Sep 17 00:00:00 2001 From: wxianfeng Date: Thu, 4 Jun 2026 10:41:50 +0800 Subject: [PATCH] fix consume fail --- internal/app/event_command.go | 17 ++++++++++++++--- internal/event/types.go | 11 ++++++----- internal/event/types_test.go | 4 ++-- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/internal/app/event_command.go b/internal/app/event_command.go index 28941a37..a3bceac6 100644 --- a/internal/app/event_command.go +++ b/internal/app/event_command.go @@ -269,10 +269,21 @@ func newEventBusCommand() *cobra.Command { ctx, cancel := signal.NotifyContext(c.Context(), os.Interrupt, syscall.SIGTERM) defer cancel() + // Acquire ReadyPipe early so pre-bus.Run failures can signal + // 'E' to the parent process instead of silently dying. + readyPipe := busctl.ReadyFDFromEnv() + failEarly := func(err error) error { + if readyPipe != nil { + _, _ = readyPipe.Write([]byte{'E'}) + _ = readyPipe.Close() + } + return err + } + configDir := defaultConfigDir() resolvedID, secret, _, _, err := authpkg.ResolveAppCredentialsStrict(configDir) if err != nil { - return fmt.Errorf("event _bus: %w", err) + return failEarly(fmt.Errorf("event _bus: %w", err)) } clientID := resolvedID if clientIDOverride != "" { @@ -288,7 +299,7 @@ func newEventBusCommand() *cobra.Command { ClientSecret: secret, }) if err != nil { - return err + return failEarly(err) } // busLogger writes to bus.log inside WorkDir so the daemon's @@ -310,7 +321,7 @@ func newEventBusCommand() *cobra.Command { Edition: editionName, Source: src, IdleTimeout: idleTimeout, - ReadyPipe: busctl.ReadyFDFromEnv(), + ReadyPipe: readyPipe, Logger: slog.Default(), } // env-var tuning (only fills in fields left at zero; explicit diff --git a/internal/event/types.go b/internal/event/types.go index d6d6debf..497c52bc 100644 --- a/internal/event/types.go +++ b/internal/event/types.go @@ -82,16 +82,17 @@ func itoa(n int64) string { } // ClientIDHash returns the path-safe identifier derived from the (possibly -// unsafe) ClientID string. We use sha256(clientID)[:16] hex-encoded; this is -// the value placed in filesystem paths and Named Pipe names. The original -// ClientID is stored in bus.meta and shown in status output. See plan -// "路径安全" decision in §1. +// unsafe) ClientID string. We use sha256(clientID)[:8] hex-encoded (16 chars); +// this is the value placed in filesystem paths and Named Pipe names. 8 bytes +// (64 bits) is sufficient to distinguish a handful of ClientIDs on one machine +// while keeping the Unix socket path under macOS's 104-byte sun_path limit. +// The original ClientID is stored in bus.meta and shown in status output. func ClientIDHash(clientID string) string { if clientID == "" { return "" } h := sha256.Sum256([]byte(clientID)) - return hex.EncodeToString(h[:16]) + return hex.EncodeToString(h[:8]) } // RedactSecret returns a redacted form of a secret string for logging and diff --git a/internal/event/types_test.go b/internal/event/types_test.go index 71ab1664..6f2a315a 100644 --- a/internal/event/types_test.go +++ b/internal/event/types_test.go @@ -60,7 +60,7 @@ func TestRawEvent_DedupKey_NilSafe(t *testing.T) { } func TestClientIDHash_StableAndPathSafe(t *testing.T) { - pathSafe := regexp.MustCompile(`^[0-9a-f]{32}$`) + pathSafe := regexp.MustCompile(`^[0-9a-f]{16}$`) cases := []string{ "ding_abc123", "ding_with_special!@#", @@ -72,7 +72,7 @@ func TestClientIDHash_StableAndPathSafe(t *testing.T) { for _, in := range cases { got := ClientIDHash(in) if !pathSafe.MatchString(got) { - t.Fatalf("ClientIDHash(%q) = %q, not path-safe hex16(32 chars)", in, got) + t.Fatalf("ClientIDHash(%q) = %q, not path-safe hex8(16 chars)", in, got) } // Stable if got2 := ClientIDHash(in); got2 != got {