packages feed

baikai-openai 0.7.3.0 → 0.7.3.1

raw patch · 4 files changed

+219/−12 lines, 4 filesdep ~baikaiPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: baikai

API changes (from Hackage documentation)

Files

CHANGELOG.md view
@@ -7,6 +7,51 @@  ## [Unreleased] +## [baikai-openai 0.7.3.1] - 2026-11-14++### Fixed++- Require `baikai ^>=0.10.1.1` in library and tests so new adapter consumers+  receive the Codex final-message correction.++## [baikai 0.10.1.1] - 2026-11-14++### Fixed++- `baikai`: the Codex batch parser returns the last completed agent message+  instead of concatenating complete messages, preserving structured JSON after+  intermediate commentary. Started and updated items cannot replace the final+  answer; usage and thread observations still cover the whole run ([BUG-2](docs/bug-reports/codex-concatenates-complete-agent-messages.md)).++## [baikai 0.10.1.0] - 2026-11-03++### Added++- `Baikai.Trace.withTraceFold` and `withTraceFoldWith` own tracing across the+  entire consumer fold; `withTraceList` and `withTraceListWith` collect events.+  A healthy sink receives abort evidence before cancellation propagates,+  including cancellation between stream steps.++### Fixed++- `withTrace`, `withTraceWith`, and the traced call in `runRequestWith` and+  `runRequestWithRegistry` now finalize synchronously on cancellation. Cleanup+  resumes an interrupted terminal drain within the same one-second budget,+  preserves the original exception, and never duplicates terminal evidence.+  General-stream abandonment keeps its garbage-collection-eventual contract.++## [baikai-effectful 0.5.0.0] - 2026-11-03++### Added++- `streamCollectTraced` delegates traced event collection to core with bounded+  cleanup on cancellation. Re-export `TraceSink` for operation signatures.++### Changed (breaking)++- `Baikai` gains `StreamCollectTraced`. Third-party interpreters must handle+  this constructor. The binding requires `baikai ^>=0.10.1`.+ ## [baikai 0.10.0.0] - 2026-10-25  ### Added@@ -108,6 +153,8 @@  - Layered job configuration accepts `thinking "none"` through the shared   canonical parser, keeping explicit off distinct from an absent preference.+- The UTF-8 stdin test runs in a child process under an ASCII locale, avoiding+  shared-stdin descriptor races with parallel subprocess tests in the tarball.  ### Changed 
baikai-openai.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.4 name: baikai-openai-version: 0.7.3.0+version: 0.7.3.1 synopsis: OpenAI providers for the baikai abstraction description:   OpenAI-compatible backends for baikai: Chat Completions over SSE against OpenAI and@@ -78,7 +78,7 @@   autogen-modules: Paths_baikai_openai   build-depends:     aeson ^>=2.2,-    baikai ^>=0.10.0,+    baikai ^>=0.10.1.1,     base >=4.20 && <5,     base64-bytestring ^>=1.2,     bytestring ^>=0.12,@@ -133,7 +133,7 @@    build-depends:     aeson,-    baikai ^>=0.10.0,+    baikai ^>=0.10.1.1,     baikai-openai,     base >=4.20 && <5,     bytestring,
test/CliEvidenceSpec.hs view
@@ -172,7 +172,12 @@     -- digest of an empty envelope would be a real-looking value     -- standing for a response that never arrived.     field "response_commitment" ev @?= Just (String "unobserved")-    field "usage" ev @?= Just (String "unobserved")+    case observedObject "usage" ev of+      Just u -> do+        KeyMap.lookup "input_tokens" u @?= Just (Number 8)+        KeyMap.lookup "cache_read_tokens" u @?= Just (Number 2)+        KeyMap.lookup "output_tokens" u @?= Just (Number 3)+      Nothing -> assertFailure "the failed run lost its reported usage"  optOutTest :: TestTree optOutTest =@@ -268,12 +273,15 @@ baseOptions :: Options baseOptions = emptyOptions & #evidence .~ Just (evidenceRequest "run-55") --- | The event stream @codex-cli 0.146.0@ emits, with the thread--- identifier kept exactly as recorded.+-- | A recorded @codex-cli 0.146.0@ run, augmented with commentary,+-- earlier usage, and a later thread event to protect independent observations. recordedEvents :: ([Text], Int) recordedEvents =   ( [ "{\"type\":\"thread.started\",\"thread_id\":\"019fd471-4a48-7c83-be67-6b7c49646e43\"}",       "{\"type\":\"turn.started\"}",+      "{\"type\":\"item.completed\",\"item\":{\"type\":\"agent_message\",\"text\":\"commentary\"}}",+      "{\"type\":\"turn.completed\",\"usage\":{\"input_tokens\":1,\"output_tokens\":1}}",+      "{\"type\":\"thread.started\",\"thread_id\":\"later-thread-must-not-win\"}",       "{\"type\":\"item.completed\",\"item\":{\"id\":\"item_0\",\"type\":\"agent_message\",\"text\":\"ok\"}}",       "{\"type\":\"turn.completed\",\"usage\":{\"input_tokens\":16071,\"cached_input_tokens\":6912,\       \\"cache_write_input_tokens\":0,\"output_tokens\":5,\"reasoning_output_tokens\":0}}"@@ -288,10 +296,13 @@     0   ) --- | A run that named its thread and then failed.+-- | A run that named its thread, answered, and counted tokens, then failed. failingEvents :: ([Text], Int) failingEvents =-  ( ["{\"type\":\"thread.started\",\"thread_id\":\"019fd471-dead-7c83-be67-6b7c49646e43\"}"],+  ( [ "{\"type\":\"thread.started\",\"thread_id\":\"019fd471-dead-7c83-be67-6b7c49646e43\"}",+      "{\"type\":\"item.completed\",\"item\":{\"type\":\"agent_message\",\"text\":\"apparently valid answer\"}}",+      "{\"type\":\"turn.completed\",\"usage\":{\"input_tokens\":10,\"cached_input_tokens\":2,\"output_tokens\":3}}"+    ],     4   ) 
test/StructuredCliSpec.hs view
@@ -12,6 +12,7 @@ import Control.Lens ((&), (.~), (^.)) import Data.Aeson (Value (..), (.=)) import Data.Aeson qualified as Aeson+import Data.Aeson.Key qualified as Key import Data.Aeson.KeyMap qualified as KeyMap import Data.ByteString qualified as BS import Data.ByteString.Lazy qualified as LBS@@ -22,6 +23,7 @@ import Data.Text.Encoding qualified as Text import Data.Text.IO qualified as TextIO import Data.Vector qualified as Vector+import Streamly.Data.Stream qualified as Stream import System.Directory (doesFileExist, getPermissions, setOwnerExecutable, setPermissions) import System.FilePath ((</>)) import System.IO.Temp (withSystemTempDirectory)@@ -34,6 +36,8 @@     "StructuredCliSpec: codex exec --output-schema passthrough"     [ renderingTests,       processTests,+      finalAnswerTests,+      finalAnswerIntegrationTests,       testCase "the provider declares NativeJsonSchema without spawning anything" $         CodexCli.codexCliProvider CodexCli.defaultCodexCliConfig ^. #structuredOutput           @?= NativeJsonSchema@@ -75,8 +79,21 @@ processTests =   testGroup     "fake codex process"-    [ testCase "the schema file carries the schema, the reply is returned unchanged, the file is removed" $+    [ testCase "two complete schema-conforming objects return only the final object" $         withFakeCodex $ \run -> do+          (resp, captured) <- run briefingOptions (Fake (map agentMessage [briefingText "Intermediate", briefingText "Final"]) "" 0)+          responseError resp @?= Nothing+          let body = flattenAssistantText (resp ^. #message . #content)+          body @?= briefingText "Final"+          Aeson.eitherDecodeStrict' (Text.encodeUtf8 body) @?= Right (briefing "Final")+          resp ^. #evidence @?= Nothing+          case captured of+            Nothing -> assertFailure "the tool received no --output-schema"+            Just (path, bytes) -> do+              Aeson.decodeStrict bytes @?= Just briefingSchema+              doesFileExist (Text.unpack path) >>= (@?= False),+      testCase "the schema file carries the schema, the reply is returned unchanged, the file is removed" $+        withFakeCodex $ \run -> do           (resp, captured) <- run schemaOptions (Fake conformingEvents "" 0)           responseError resp @?= Nothing           flattenAssistantText (resp ^. #message . #content) @?= conformingText@@ -123,6 +140,86 @@           assertBool "the schema is part of the commitment" (c1 /= encodedCommitment plain)     ] +finalAnswerTests :: TestTree+finalAnswerTests =+  testGroup+    "final answer selection"+    [ testCase name $+        withFakeCodex $ \run -> do+          (resp, _) <- run opts (Fake (map agentMessage bodies) "" 0)+          responseError resp @?= Nothing+          flattenAssistantText (resp ^. #message . #content) @?= expected+    | (name, opts, bodies, expected) <-+        [ ("single briefing is unchanged", briefingOptions, [briefingText "Final"], briefingText "Final"),+          ("three objects select the last", briefingOptions, map briefingText ["First", "Second", "Final"], briefingText "Final"),+          ("prose commentary precedes a JSON answer", briefingOptions, ["Working on it.", briefingText "Final"], briefingText "Final"),+          ("plain-text calls select the final message and trim outer whitespace", emptyOptions, ["commentary", "  final answer\n "], "final answer"),+          ("an empty final answer replaces commentary", briefingOptions, [briefingText "Earlier", ""], ""),+          ("missing schema fields do not recover an earlier object", briefingOptions, [briefingText "Earlier", "{}"], "{}")+        ]+    ]++finalAnswerIntegrationTests :: TestTree+finalAnswerIntegrationTests =+  testGroup+    "final answer integration"+    [ testCase "malformed final JSON is returned faithfully and still fails decoding" $+        withFakeCodex $ \run -> do+          (resp, _) <- run briefingOptions (Fake (map agentMessage [briefingText "Earlier", " \n{\"why\":  "]) "" 0)+          responseError resp @?= Nothing+          let body = flattenAssistantText (resp ^. #message . #content)+          body @?= "{\"why\":"+          assertBool "inner JSON remains malformed" (case Aeson.eitherDecodeStrict' @Value (Text.encodeUtf8 body) of Left _ -> True; Right _ -> False),+      testCase "complete and synthetic stream expose one identical final text block" $+        withFakeCodexAndStream $ \run -> do+          let fake = Fake (briefingEvents "commentary" (briefingText "Final")) "" 0+              opts = briefingOptions & #evidence .~ Just (evidenceRequest "stream-103")+          (direct, _, _) <- run False opts fake+          (collected, events, _) <- run True opts fake+          responseError direct @?= Nothing+          responseError collected @?= Nothing+          let directBody = flattenAssistantText (direct ^. #message . #content)+          flattenAssistantText (collected ^. #message . #content) @?= directBody+          Aeson.eitherDecodeStrict' (Text.encodeUtf8 directBody) @?= Right (briefing "Final")+          Vector.toList (direct ^. #message . #content) @?= [AssistantText (TextContent (briefingText "Final"))]+          [body | TextEnd BlockEndPayload {content = body} <- events] @?= [briefingText "Final"]+          length [() | EventDone _ <- events] @?= 1+          length [() | EventError _ <- events] @?= 0+          case reverse events of+            EventDone TerminalPayload {reason = reason} : _ -> reason @?= Stop+            _ -> assertFailure ("expected final EventDone: " <> show events)+          direct ^. #responseId @?= Just "briefing-thread"+          collected ^. #responseId @?= direct ^. #responseId+          direct ^. #message . #usage . #inputTokens @?= 8+          direct ^. #message . #usage . #cacheReadTokens @?= 2+          direct ^. #message . #usage . #outputTokens @?= 3+          collected ^. #message . #usage @?= direct ^. #message . #usage+          evidenceField "response_commitment" collected @?= evidenceField "response_commitment" direct+          evidenceField "usage" collected @?= evidenceField "usage" direct,+      testCase "response evidence commits to the final answer independently of commentary" $+        withFakeCodex $ \run -> do+          let opts = briefingOptions & #evidence .~ Just (evidenceRequest "run-103")+              fake intermediate final = Fake (briefingEvents intermediate final) "" 0+          (first, _) <- run opts (fake "first commentary" (briefingText "Final"))+          (second, _) <- run opts (fake "different commentary" (briefingText "Final"))+          (changed, _) <- run opts (fake "first commentary" (briefingText "Changed"))+          let commitment = evidenceField "response_commitment" first+          assertBool "a successful response is observed" (case commitment of Just (Object o) -> KeyMap.member "observed" o; _ -> False)+          commitment @?= evidenceField "response_commitment" second+          assertBool "changing the final answer changes the commitment" (commitment /= evidenceField "response_commitment" changed)+          evidenceField "response_id" first @?= Just (Aeson.object ["observed" .= ("briefing-thread" :: Text)])+          evidenceField "observed_model" first @?= Just (String "unobserved"),+      testCase "a valid-looking answer cannot turn a nonzero exit into success" $+        withFakeCodex $ \run -> do+          (resp, captured) <- run briefingOptions (Fake (briefingEvents "commentary" (briefingText "Final")) "failed after answering\n" 3)+          fmap (^. #category) (responseError resp) @?= Just ProcessFailure+          fmap (^. #exitCode) (responseError resp) @?= Just (Just 3)+          resp ^. #responseId @?= Just "briefing-thread"+          case captured of+            Just (path, _) -> doesFileExist (Text.unpack path) >>= (@?= False)+            Nothing -> assertFailure "the tool received no schema"+    ]+ -- | What the fake prints and how it exits. data Fake = Fake   { eventLines :: [Text],@@ -139,6 +236,12 @@ -- executable's path. withFakeCodex :: ((Options -> Fake -> IO (Response, Maybe (Text, BS.ByteString))) -> IO a) -> IO a withFakeCodex k =+  withFakeCodexAndStream $ \run -> k $ \opts fake -> do+    (resp, _, captured) <- run False opts fake+    pure (resp, captured)++withFakeCodexAndStream :: ((Bool -> Options -> Fake -> IO (Response, [AssistantMessageEvent], Maybe (Text, BS.ByteString))) -> IO a) -> IO a+withFakeCodexAndStream k =   withSystemTempDirectory "baikai-codex-structured" $ \dir -> do     let outPath = dir </> "stdout"         errPath = dir </> "stderr"@@ -167,11 +270,19 @@     registerApiProviderWith       reg       (CodexCli.codexCliProvider CodexCli.defaultCodexCliConfig {CodexCli.executable = exe})-    k $ \opts fake -> do+    k $ \streaming opts fake -> do       TextIO.writeFile outPath (Text.unlines (eventLines fake))       TextIO.writeFile errPath (stderrBody fake)       writeFile codePath (show (code fake))-      resp <- completeRequestWith reg testModel testContext opts+      (resp, events) <-+        if streaming+          then do+            events <- Stream.toList (streamRequestWith reg testModel testContext opts)+            resp <- Stream.fold (reassembleResponse testModel) (Stream.fromList events)+            pure (resp, events)+          else do+            resp <- completeRequestWith reg testModel testContext opts+            pure (resp, [])       sent <- doesFileExist pathPath       captured <-         if sent@@ -180,7 +291,7 @@             bytes <- BS.readFile copyPath             pure (Just (path, bytes))           else pure Nothing-      pure (resp, captured)+      pure (resp, events, captured)  writeFakeExecutable :: FilePath -> String -> String -> IO FilePath writeFakeExecutable dir name body = do@@ -196,6 +307,11 @@   Just (Object o) -> KeyMap.lookup "request_commitment" o   _ -> Nothing +evidenceField :: Text -> Response -> Maybe Value+evidenceField key resp = case Aeson.toJSON <$> (resp ^. #evidence) of+  Just (Object o) -> KeyMap.lookup (Key.fromText key) o+  _ -> Nothing+ -- ============================================================ -- Fixtures -- ============================================================@@ -249,6 +365,39 @@ conformingText :: Text conformingText =   "{\"items\":[{\"label\":\"Strawberry\",\"level\":\"low\"},{\"label\":\"Mango\",\"level\":\"high\"}]}"++briefing :: Text -> Value+briefing why = Aeson.object ["why" .= why, "what" .= ("X" :: Text), "blocking" .= ("B" :: Text), "status" .= ("S" :: Text)]++briefingText :: Text -> Text+briefingText why = "{\"why\":\"" <> why <> "\",\"what\":\"X\",\"blocking\":\"B\",\"status\":\"S\"}"++briefingSchema :: Value+briefingSchema =+  Aeson.object+    [ "type" .= ("object" :: Text),+      "properties" .= Aeson.object ["why" .= string, "what" .= string, "blocking" .= string, "status" .= string],+      "required" .= (["why", "what", "blocking", "status"] :: [Text]),+      "additionalProperties" .= False+    ]+  where+    string = Aeson.object ["type" .= ("string" :: Text)]++briefingOptions :: Options+briefingOptions = emptyOptions & #responseFormat .~ Just (JsonSchema (jsonSchemaFormat "briefing" briefingSchema))++agentMessage :: Text -> Text+agentMessage body = Text.decodeUtf8 . LBS.toStrict . Aeson.encode $ Aeson.object ["type" .= ("item.completed" :: Text), "item" .= Aeson.object ["type" .= ("agent_message" :: Text), "text" .= body]]++briefingEvents :: Text -> Text -> [Text]+briefingEvents intermediate final =+  [ "{\"type\":\"thread.started\",\"thread_id\":\"briefing-thread\"}",+    agentMessage intermediate,+    "{\"type\":\"turn.completed\",\"usage\":{\"input_tokens\":1,\"output_tokens\":1}}",+    "{\"type\":\"thread.started\",\"thread_id\":\"later-thread\"}",+    agentMessage final,+    "{\"type\":\"turn.completed\",\"usage\":{\"input_tokens\":10,\"cached_input_tokens\":2,\"output_tokens\":3}}"+  ]  -- | The event stream @codex-cli 0.159.0@ emits for an @--output-schema@ -- run: the structured reply is the text of the ordinary final