okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-023-int into f-023-guifcb0c2d0e

main
3.2 KiB75 linesraw
1"""I6 (tools and images review, 2026-10-22): a picture a tool returned, with an elastic image encoder that is not running,
2used to write to a process that did not exist (AttributeError, then a 400 "malformed request" that every later turn of the
3conversation repeated).  It starts the encoder like a picture of the user does; when it cannot start, the picture becomes
4an "[image omitted: why]" note.
5
6    python -m serve.test_vision_tool_image
7"""
8import tempfile
9import unittest
10from unittest import mock
11from pathlib import Path
12
13from serve.frontend import ChatTemplate
14from serve.server import ByteTokenizer, Service, Vision
15from serve.test_vision_elastic import ElasticVision
16from serve.test_vram import VramEngine
17
18ROOT = Path(__file__).resolve().parents[1]
19
20
21def tool_turn():
22    return [{"role": "user", "content": "look at it"},
23            {"role": "tool", "tool_call_id": "t1", "content": [{"type": "image", "source": "x.png"}]}]
24
25
26class FailingStart(ElasticVision):
27    def restart(self):
28        raise RuntimeError("the vision encoder did not start: it ended without saying anything (exit code 1)")
29
30
31class ToolImage(unittest.TestCase):
32    def service(self, vision):
33        tok = ByteTokenizer()
34        engine = VramEngine(tok)
35        engine.loaded = True
36        return Service(engine, tok, ChatTemplate(ROOT / "serve/chat_template.jinja"), vision=vision)
37
38    def test_an_elastic_encoder_is_started_for_a_tool_picture(self):
39        vision = ElasticVision(tempfile.mkdtemp())
40        svc = self.service(vision)
41        messages = tool_turn()
42        svc._note_unreadable_tool_images(messages)
43        self.assertEqual(vision.starts, 1)
44        self.assertEqual(messages[1]["content"][0]["type"], "image")      # kept: it was read
45        svc._note_unreadable_tool_images(messages)                        # the running encoder is used as it is
46        self.assertEqual(vision.starts, 1)
47
48    def test_an_encoder_that_cannot_start_leaves_a_note_not_a_400(self):
49        svc = self.service(FailingStart(tempfile.mkdtemp()))
50        messages = tool_turn()
51        svc._note_unreadable_tool_images(messages)                        # no exception
52        part = messages[1]["content"][0]
53        self.assertEqual(part["type"], "text")
54        self.assertIn("[image omitted:", part["text"])
55        self.assertIn("did not start", part["text"])
56
57    def test_prepare_survives_a_tool_picture_with_an_elastic_encoder(self):
58        vision = ElasticVision(tempfile.mkdtemp())
59        svc = self.service(vision)
60        self.addCleanup(lambda: getattr(svc.embeddings, "path", None) and svc.embeddings.path.unlink(missing_ok=True))
61        svc.prepare(tool_turn(), None, {})
62        self.assertEqual(vision.starts, 1)
63
64    def test_encode_without_a_running_reader_is_a_clear_value_error(self):
65        v = Vision.__new__(Vision)
66        v.dir, v.cache, v.proc = Path(tempfile.mkdtemp()), {}, None
67        import threading
68        v.lock = threading.Lock()
69        with mock.patch.object(Vision, "normalize", return_value=b"png"), self.assertRaises(ValueError) as cm:
70            v.encode(b"bytes")
71        self.assertIn("picture reader is not running", str(cm.exception))
72
73
74if __name__ == "__main__":
75    unittest.main()