Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(macos): show only a server error logged since the last start

AkitaOnRailsc99c98a72153
diff --git a/companions/ai-memory-macos/Sources/AIMemoryMenu/AppModel.swift b/companions/ai-memory-macos/Sources/AIMemoryMenu/AppModel.swift
index f62c3cf7..121a6c46 100644
--- a/companions/ai-memory-macos/Sources/AIMemoryMenu/AppModel.swift
+++ b/companions/ai-memory-macos/Sources/AIMemoryMenu/AppModel.swift
@@ -23,6 +23,7 @@ final class AppModel {
@ObservationIgnored private var pollTask: Task<Void, Never>?
@ObservationIgnored private var fetchFailed = false
@ObservationIgnored private var startingDeadline: Date?
+ @ObservationIgnored private var logFreshSince = Date()
init(
settings: AppSettings = .load(),
@@ -111,7 +112,8 @@ final class AppModel {
lastError = StartFailure.message(
health: (error as? HealthError).map(Self.describe) ?? error.localizedDescription,
launchd: launchd,
- logURL: logURL
+ logURL: logURL,
+ notBefore: logFreshSince
)
}
let derived = IconState.derived(
@@ -253,6 +255,7 @@ final class AppModel {
}
private func markStarting() {
+ logFreshSince = Date()
startingDeadline = Date().addingTimeInterval(45)
icon = .starting
report = nil
diff --git a/companions/ai-memory-macos/Sources/AIMemoryMenuCore/ServerLog.swift b/companions/ai-memory-macos/Sources/AIMemoryMenuCore/ServerLog.swift
index 244d36dc..af1fe02f 100644
--- a/companions/ai-memory-macos/Sources/AIMemoryMenuCore/ServerLog.swift
+++ b/companions/ai-memory-macos/Sources/AIMemoryMenuCore/ServerLog.swift
@@ -30,12 +30,26 @@ public enum ServerLog {
/// Picks the message the menu extra shows when the server is unreachable.
public enum StartFailure {
- /// Only when the agent is installed but stopped: a log line left over from
- /// an earlier attempt must not be misreported over the live health error.
- public static func message(health: String, launchd: LaunchdState, logURL: URL) -> String {
+ /// Only when the agent is installed but stopped, and only from a log
+ /// written since `notBefore` (app launch or the last start): `stderr.log`
+ /// is append-only and a manual stop also reads as `.stopped`, so an error
+ /// left over from an earlier attempt must not be misreported.
+ public static func message(
+ health: String,
+ launchd: LaunchdState,
+ logURL: URL,
+ notBefore: Date
+ ) -> String {
guard launchd == .stopped,
+ let modified = modificationDate(of: logURL),
+ modified >= notBefore,
let logged = ServerLog.recentError(at: logURL)
else { return health }
return logged
}
+
+ private static func modificationDate(of url: URL) -> Date? {
+ let attributes = try? FileManager.default.attributesOfItem(atPath: url.path)
+ return attributes?[.modificationDate] as? Date
+ }
}
diff --git a/companions/ai-memory-macos/Tests/AIMemoryMenuTests/ServerLogTests.swift b/companions/ai-memory-macos/Tests/AIMemoryMenuTests/ServerLogTests.swift
index 6c8cfce1..5a63476a 100644
--- a/companions/ai-memory-macos/Tests/AIMemoryMenuTests/ServerLogTests.swift
+++ b/companions/ai-memory-macos/Tests/AIMemoryMenuTests/ServerLogTests.swift
@@ -60,16 +60,33 @@ struct StartFailureTests {
let message = StartFailure.message(
health: "Could not read /admin/status",
launchd: .stopped,
- logURL: logURL
+ logURL: logURL,
+ notBefore: .distantPast
)
#expect(message.contains("Address already in use"))
}
+ @Test func ignoresAnErrorLoggedBeforeTheLastStart() throws {
+ let logURL = try makeLog("Error: binding 127.0.0.1:49374\n")
+ try FileManager.default.setAttributes(
+ [.modificationDate: Date().addingTimeInterval(-3600)],
+ ofItemAtPath: logURL.path
+ )
+ let message = StartFailure.message(
+ health: "Could not read /admin/status",
+ launchd: .stopped,
+ logURL: logURL,
+ notBefore: Date()
+ )
+ #expect(message == "Could not read /admin/status")
+ }
+
@Test func fallsBackToHealthWhenNotInstalled() {
let message = StartFailure.message(
health: "Connection refused",
launchd: .notInstalled,
- logURL: URL(fileURLWithPath: "/nonexistent/stderr.log")
+ logURL: URL(fileURLWithPath: "/nonexistent/stderr.log"),
+ notBefore: .distantPast
)
#expect(message == "Connection refused")
}
@@ -79,7 +96,8 @@ struct StartFailureTests {
let message = StartFailure.message(
health: "Could not read /admin/status",
launchd: .running,
- logURL: logURL
+ logURL: logURL,
+ notBefore: .distantPast
)
#expect(message == "Could not read /admin/status")
}
diff --git a/docs/macos.md b/docs/macos.md
index 1d699e79..a82c567a 100644
--- a/docs/macos.md
+++ b/docs/macos.md
@@ -409,8 +409,8 @@ wrapper's `posix` shell-script path does not. Re-run `install-hooks --agent
`~/Library/Application Support/ai-memory` by bundled `ai-memory init`.
- **`Error: binding 127.0.0.1:49374` / `Address already in use` (the server
exits immediately and the menu extra stays red):** another process already
- holds the port. **OpenCode v2's background service also defaults to 49374**,
- so the two collide when both run on defaults. Move one side — start
+ holds the port, for example OpenCode v2's background service (reported in
+ #1044 to default to 49374). Move one side — start
ai-memory elsewhere (`bind = "127.0.0.1:<free-port>"` in
`~/Library/Application Support/ai-memory/config.toml`, then update the app's
server URL in Settings to match), or move the other service