okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
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 6python -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 (): 22return [{"role" :"user" ,"content" :"look at it" }, 23 {"role" :"tool" ,"tool_call_id" :"t1" ,"content" : [{"type" :"image" ,"source" :"x.png" }]}] 24 25 26class FailingStart (ElasticVision ): 27def restart (self ): 28raise RuntimeError ("the vision encoder did not start: it ended without saying anything (exit code 1)" ) 29 30 31class ToolImage (unittest .TestCase ): 32def service (self ,vision ): 33tok = ByteTokenizer () 34engine = VramEngine (tok ) 35engine .loaded = True 36return Service (engine ,tok ,ChatTemplate (ROOT / "serve/chat_template.jinja" ),vision = vision ) 37 38def test_an_elastic_encoder_is_started_for_a_tool_picture (self ): 39vision = ElasticVision (tempfile .mkdtemp ()) 40svc = self .service (vision ) 41messages = tool_turn () 42svc ._note_unreadable_tool_images (messages ) 43self .assertEqual (vision .starts ,1 ) 44self .assertEqual (messages [1 ]["content" ][0 ]["type" ],"image" )# kept: it was read 45svc ._note_unreadable_tool_images (messages )# the running encoder is used as it is 46self .assertEqual (vision .starts ,1 ) 47 48def test_an_encoder_that_cannot_start_leaves_a_note_not_a_400 (self ): 49svc = self .service (FailingStart (tempfile .mkdtemp ())) 50messages = tool_turn () 51svc ._note_unreadable_tool_images (messages )# no exception 52part = messages [1 ]["content" ][0 ] 53self .assertEqual (part ["type" ],"text" ) 54self .assertIn ("[image omitted:" ,part ["text" ]) 55self .assertIn ("did not start" ,part ["text" ]) 56 57def test_prepare_survives_a_tool_picture_with_an_elastic_encoder (self ): 58vision = ElasticVision (tempfile .mkdtemp ()) 59svc = self .service (vision ) 60self .addCleanup (lambda :getattr (svc .embeddings ,"path" ,None )and svc .embeddings .path .unlink (missing_ok = True )) 61svc .prepare (tool_turn (),None , {}) 62self .assertEqual (vision .starts ,1 ) 63 64def test_encode_without_a_running_reader_is_a_clear_value_error (self ): 65v = Vision .__new__ (Vision ) 66v .dir ,v .cache ,v .proc = Path (tempfile .mkdtemp ()), {},None 67import threading 68v .lock = threading .Lock () 69with mock .patch .object (Vision ,"normalize" ,return_value = b"png" ),self .assertRaises (ValueError )as cm : 70v .encode (b"bytes" ) 71self .assertIn ("picture reader is not running" ,str (cm .exception )) 72 73 74if __name__ == "__main__" : 75unittest .main ()