repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
Mirrored from https://github.com/yingqi-z20/Agent-libOS
stars
latest
clone command
git clone gitlawb://did:key:z6MkqRzA...RfoM/yingqi-z20-Agen...git clone gitlawb://did:key:z6MkqRzA.../yingqi-z20-Agen...d98dd2c9IPC1d ago| #1 | from __future__ import annotations |
| #2 | |
| #3 | import tempfile |
| #4 | import unittest |
| #5 | from pathlib import Path |
| #6 | |
| #7 | from agent_libos import AgentImage, Runtime |
| #8 | from agent_libos.models import CapabilityRight, EventType |
| #9 | from agent_libos.models.exceptions import ValidationError |
| #10 | from agent_libos.substrate import LocalResourceProviderSubstrate |
| #11 | |
| #12 | |
| #13 | class ImageRegistrationTests(unittest.TestCase): |
| #14 | def test_register_image_primitive_validates_tools_and_emits_audit(self) -> None: |
| #15 | runtime = Runtime.open("local") |
| #16 | try: |
| #17 | image = AgentImage( |
| #18 | image_id="custom-review:v0", |
| #19 | name="custom-review", |
| #20 | system_prompt="Custom review image.", |
| #21 | default_tools=["read_memory_object", "human_output"], |
| #22 | safety_profile="review", |
| #23 | ) |
| #24 | |
| #25 | runtime.register_image(image, actor="test") |
| #26 | |
| #27 | self.assertIs(runtime.get_image("custom-review:v0"), image) |
| #28 | self.assertIn("image.register", [record.action for record in runtime.audit.trace()]) |
| #29 | self.assertIn(EventType.IMAGE_REGISTERED, [event.type for event in runtime.events.list()]) |
| #30 | finally: |
| #31 | runtime.close() |
| #32 | |
| #33 | def test_register_image_rejects_unknown_default_tool(self) -> None: |
| #34 | runtime = Runtime.open("local") |
| #35 | try: |
| #36 | with self.assertRaises(ValidationError): |
| #37 | runtime.register_image( |
| #38 | { |
| #39 | "image_id": "bad-image:v0", |
| #40 | "name": "bad-image", |
| #41 | "default_tools": ["not_a_real_tool"], |
| #42 | }, |
| #43 | actor="test", |
| #44 | ) |
| #45 | finally: |
| #46 | runtime.close() |
| #47 | |
| #48 | def test_load_image_from_yaml_tool_reads_file_and_registers_image(self) -> None: |
| #49 | with tempfile.TemporaryDirectory() as temp_dir: |
| #50 | manifest = Path(temp_dir) / "images" / "yaml-agent.yaml" |
| #51 | manifest.parent.mkdir() |
| #52 | manifest.write_text( |
| #53 | """ |
| #54 | image: |
| #55 | image_id: yaml-agent:v0 |
| #56 | name: yaml-agent |
| #57 | version: v0 |
| #58 | system_prompt: | |
| #59 | YAML registered image. |
| #60 | Keep responses concise. |
| #61 | default_tools: |
| #62 | - human_output |
| #63 | - read_memory_object |
| #64 | context_policy: evidence_first |
| #65 | safety_profile: yaml-test |
| #66 | metadata: |
| #67 | role: test |
| #68 | """.lstrip(), |
| #69 | encoding="utf-8", |
| #70 | ) |
| #71 | runtime = Runtime.open("local", substrate=LocalResourceProviderSubstrate(temp_dir)) |
| #72 | try: |
| #73 | pid = runtime.process.spawn(image="review-agent:v0", goal="load image") |
| #74 | runtime.filesystem.grant_path(pid, "images/yaml-agent.yaml", [CapabilityRight.READ], issued_by="test") |
| #75 | runtime.image_registry.grant_register(pid, issued_by="test") |
| #76 | |
| #77 | result = runtime.tools.call(pid, "load_image_from_yaml", {"path": "images/yaml-agent.yaml"}) |
| #78 | |
| #79 | self.assertTrue(result.ok, result.error) |
| #80 | self.assertEqual(result.payload["image_id"], "yaml-agent:v0") |
| #81 | image = runtime.get_image("yaml-agent:v0") |
| #82 | self.assertEqual(image.system_prompt, "YAML registered image.\nKeep responses concise.\n") |
| #83 | self.assertEqual(image.default_tools, ["human_output", "read_memory_object"]) |
| #84 | self.assertEqual(image.metadata, {"role": "test"}) |
| #85 | finally: |
| #86 | runtime.close() |
| #87 | |
| #88 | def test_load_image_from_yaml_tool_requires_image_write_capability(self) -> None: |
| #89 | with tempfile.TemporaryDirectory() as temp_dir: |
| #90 | manifest = Path(temp_dir) / "yaml-agent.yaml" |
| #91 | manifest.write_text( |
| #92 | """ |
| #93 | image_id: yaml-agent:v0 |
| #94 | name: yaml-agent |
| #95 | default_tools: |
| #96 | - human_output |
| #97 | """.lstrip(), |
| #98 | encoding="utf-8", |
| #99 | ) |
| #100 | runtime = Runtime.open("local", substrate=LocalResourceProviderSubstrate(temp_dir)) |
| #101 | try: |
| #102 | pid = runtime.process.spawn(image="review-agent:v0", goal="load image without authority") |
| #103 | runtime.filesystem.grant_path(pid, "yaml-agent.yaml", [CapabilityRight.READ], issued_by="test") |
| #104 | |
| #105 | result = runtime.tools.call(pid, "load_image_from_yaml", {"path": "yaml-agent.yaml"}) |
| #106 | |
| #107 | self.assertFalse(result.ok) |
| #108 | self.assertIn("lacks write on image:yaml-agent:v0", result.error or "") |
| #109 | with self.assertRaises(KeyError): |
| #110 | runtime.get_image("yaml-agent:v0") |
| #111 | finally: |
| #112 | runtime.close() |
| #113 | |
| #114 | |
| #115 | if __name__ == "__main__": |
| #116 | unittest.main() |
| #117 |