Skip to content

Commit 64901f9

Browse files
committed
fix: keep MiniMax diagnose output quiet
1 parent 0ce9227 commit 64901f9

5 files changed

Lines changed: 50 additions & 3 deletions

File tree

Sources/CodexBarCLI/CLIEntry.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ enum CodexBarCLI {
3030

3131
do {
3232
let invocation = try program.resolve(argv: argv)
33-
Self.bootstrapLogging(values: invocation.parsedValues)
33+
Self.bootstrapLogging(path: invocation.path, values: invocation.parsedValues)
3434
switch invocation.path {
3535
case ["usage"]:
3636
await self.runUsage(invocation.parsedValues)
@@ -155,12 +155,17 @@ enum CodexBarCLI {
155155

156156
// MARK: - Helpers
157157

158-
private static func bootstrapLogging(values: ParsedValues) {
158+
private static func bootstrapLogging(path: [String], values: ParsedValues) {
159+
CodexBarLog.bootstrapIfNeeded(self.loggingConfiguration(path: path, values: values))
160+
}
161+
162+
static func loggingConfiguration(path: [String], values: ParsedValues) -> CodexBarLog.Configuration {
159163
let isJSON = values.flags.contains("jsonOutput") || values.flags.contains("jsonOnly")
160164
let verbose = values.flags.contains("verbose")
161165
let rawLevel = values.options["logLevel"]?.last
162166
let level = Self.resolvedLogLevel(verbose: verbose, rawLevel: rawLevel)
163-
CodexBarLog.bootstrapIfNeeded(.init(destination: .stderr, level: level, json: isJSON))
167+
let destination: CodexBarLog.Destination = path == ["diagnose"] ? .discard : .stderr
168+
return .init(destination: destination, level: level, json: isJSON)
164169
}
165170

166171
static func resolvedLogLevel(verbose: Bool, rawLevel: String?) -> CodexBarLog.Level {

Sources/CodexBarCLI/CLIHelpers.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ extension CodexBarCLI {
366366
CommandSignature.describe(CacheOptions())
367367
}
368368

369+
static func _diagnoseSignatureForTesting() -> CommandSignature {
370+
CommandSignature.describe(DiagnoseOptions())
371+
}
372+
369373
static func _configSetAPIKeySignatureForTesting() -> CommandSignature {
370374
CommandSignature.describe(ConfigSetAPIKeyOptions())
371375
}

Sources/CodexBarCLI/DiagnoseOptions.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ struct DiagnoseOptions: CommanderParsable {
66
@Flag(names: [.short("v"), .long("verbose")], help: "Enable verbose logging")
77
var verbose: Bool = false
88

9+
@Flag(name: .long("json-output"), help: "Emit machine-readable logs")
10+
var jsonOutput: Bool = false
11+
912
@Option(name: .long("log-level"), help: "Set log level (trace|verbose|debug|info|warning|error|critical)")
1013
var logLevel: String?
1114

Sources/CodexBarCore/Logging/CodexBarLog.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Logging
44
public enum CodexBarLog {
55
public enum Destination: Sendable {
66
case stderr
7+
case discard
78
case oslog(subsystem: String)
89
}
910

@@ -85,6 +86,8 @@ public enum CodexBarLog {
8586
case .stderr:
8687
if config.json { return JSONStderrLogHandler(label: label) }
8788
return StreamLogHandler.standardError(label: label)
89+
case .discard:
90+
return DiscardLogHandler()
8891
case let .oslog(subsystem):
8992
#if canImport(os)
9093
return OSLogLogHandler(label: label, subsystem: subsystem)
@@ -154,6 +157,18 @@ public enum CodexBarLog {
154157
}
155158
}
156159

160+
private struct DiscardLogHandler: LogHandler {
161+
var metadata: Logger.Metadata = [:]
162+
var logLevel: Logger.Level = .critical
163+
164+
subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
165+
get { self.metadata[metadataKey] }
166+
set { self.metadata[metadataKey] = newValue }
167+
}
168+
169+
func log(event _: LogEvent) {}
170+
}
171+
157172
public struct CodexBarLogger: Sendable {
158173
private let logFn: @Sendable (CodexBarLog.Level, String, [String: String]?) -> Void
159174

Tests/CodexBarTests/CLIArgumentParsingTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,24 @@ struct CLIArgumentParsingTests {
6464
#expect(!parsed.flags.contains("jsonOutput"))
6565
#expect(CodexBarCLI._decodeFormatForTesting(from: parsed) == .json)
6666
}
67+
68+
@Test
69+
func `diagnose accepts json output flag but discards provider logs`() throws {
70+
let signature = CodexBarCLI._diagnoseSignatureForTesting()
71+
let parser = CommandParser(signature: signature)
72+
let parsed = try parser.parse(arguments: [
73+
"--provider", "minimax",
74+
"--format", "json",
75+
"--json-output",
76+
])
77+
78+
#expect(parsed.flags.contains("jsonOutput"))
79+
let config = CodexBarCLI.loggingConfiguration(path: ["diagnose"], values: parsed)
80+
switch config.destination {
81+
case .discard:
82+
break
83+
case .stderr, .oslog:
84+
Issue.record("diagnose should not emit provider logs beside the safe JSON export")
85+
}
86+
}
6787
}

0 commit comments

Comments
 (0)