feat(menubar): add CodeburnCLI.scriptEnvironment for shell-script embedding

Exposes the validated argv and augmented PATH so the LaunchAgent runner
script can be generated without re-resolving the binary or PATH.
This commit is contained in:
iamtoruk 2026-05-30 00:10:29 -07:00
parent bdbd7b19f6
commit be7b8278c5
3 changed files with 25 additions and 6 deletions

View file

@ -161,12 +161,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
}
private func prepareRefreshPipelineForSleep() {
// Cancel only in-flight work so a fetch started before sleep cannot
// apply a stale result after wake. The DispatchSource timer is paused
// by the kernel during sleep and fires normally on wake, so we leave
// it running — tearing it down here was the stranding bug: a missed
// wake notification left refreshTimer nil forever. The 5s rate limiter
// and single-flight forceRefreshTask guard absorb the wake burst.
// Leave the timer running: the kernel pauses it during sleep, and tearing
// it down stranded the loop whenever a wake notification was missed.
forceRefreshTask?.cancel()
forceRefreshTask = nil
forceRefreshStartedAt = nil

View file

@ -99,6 +99,13 @@ enum CodeburnCLI {
return process
}
// argv tokens are `isSafe`-validated and PATH entries are constants, so the
// result is safe to interpolate directly into the LaunchAgent shell script.
static func scriptEnvironment() -> (argv: [String], path: String) {
let basePath = ProcessInfo.processInfo.environment["PATH"] ?? ""
return (baseArgv(), augmentedPath(basePath))
}
static func isSafe(_ s: String) -> Bool {
let range = NSRange(s.startIndex..<s.endIndex, in: s)
return safeArgPattern.firstMatch(in: s, range: range) != nil

View file

@ -0,0 +1,16 @@
import Foundation
import Testing
@testable import CodeBurnMenubar
@Suite("Menubar refresh script")
struct MenubarRefreshScriptTests {
@Test("scriptEnvironment exposes a safe argv and augmented PATH")
func scriptEnvironmentIsSafe() {
let env = CodeburnCLI.scriptEnvironment()
#expect(!env.argv.isEmpty)
#expect(env.argv.allSatisfy { CodeburnCLI.isSafe($0) })
// Homebrew + /usr/local are always appended for GUI-launched apps.
#expect(env.path.contains("/opt/homebrew/bin"))
#expect(env.path.contains("/usr/local/bin"))
}
}