ilang-protocol 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ilang-ai
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.4
2
+ Name: ilang-protocol
3
+ Version: 1.0.0
4
+ Summary: The iLang loader: fetches the official iLang runtime, verifies it, caches it and puts it in front of any model's context. Don't learn iLang. Your AI should.
5
+ Author: Long Quan Zhu
6
+ License: MIT
7
+ Project-URL: Homepage, https://ilang.ai
8
+ Project-URL: Source, https://github.com/ilang-ai/ilang
9
+ Project-URL: Specification, https://github.com/ilang-ai/ilang-spec
10
+ Keywords: ilang,iLang,ai,llm,prompt,protocol,agent,openai,anthropic,gemini,deepseek
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
14
+ Requires-Python: >=3.9
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Dynamic: license-file
18
+
19
+ # ilang-protocol
20
+
21
+ **Don't learn iLang. Your AI should.**
22
+
23
+ The Python loader for [iLang](https://ilang.ai). It fetches the official iLang runtime from the canon repository, checks its sha256, keeps a verified copy on disk and adds it to any model's messages.
24
+
25
+ ```bash
26
+ pip install ilang-protocol
27
+ ```
28
+
29
+ ```python
30
+ import ilang
31
+
32
+ messages = ilang.wrap([{"role": "user", "content": "帮我分析这个问题"}]) # OpenAI-style messages
33
+ system = ilang.system() # Anthropic, Gemini
34
+ print(ilang.status())
35
+ ```
36
+
37
+ Updates are checked at most once an hour; without a network the last verified copy is used, and with no copy at all the messages go out unchanged unless `strict=True`. Pin an exact runtime with `version=` for reproducible runs; add `extensions=["media"]` for image, video and audio work. The core runtime is about 27,000 tokens, so enable your provider's prompt caching.
38
+
39
+ Standard library only, Python 3.9+. Source, JavaScript version and documentation: [github.com/ilang-ai/ilang](https://github.com/ilang-ai/ilang). MIT.
@@ -0,0 +1,21 @@
1
+ # ilang-protocol
2
+
3
+ **Don't learn iLang. Your AI should.**
4
+
5
+ The Python loader for [iLang](https://ilang.ai). It fetches the official iLang runtime from the canon repository, checks its sha256, keeps a verified copy on disk and adds it to any model's messages.
6
+
7
+ ```bash
8
+ pip install ilang-protocol
9
+ ```
10
+
11
+ ```python
12
+ import ilang
13
+
14
+ messages = ilang.wrap([{"role": "user", "content": "帮我分析这个问题"}]) # OpenAI-style messages
15
+ system = ilang.system() # Anthropic, Gemini
16
+ print(ilang.status())
17
+ ```
18
+
19
+ Updates are checked at most once an hour; without a network the last verified copy is used, and with no copy at all the messages go out unchanged unless `strict=True`. Pin an exact runtime with `version=` for reproducible runs; add `extensions=["media"]` for image, video and audio work. The core runtime is about 27,000 tokens, so enable your provider's prompt caching.
20
+
21
+ Standard library only, Python 3.9+. Source, JavaScript version and documentation: [github.com/ilang-ai/ilang](https://github.com/ilang-ai/ilang). MIT.
@@ -0,0 +1,57 @@
1
+ """iLang loader. Don't learn iLang. Your AI should.
2
+
3
+ import ilang
4
+ messages = ilang.wrap(messages) # OpenAI-style message lists
5
+ system = ilang.system() # providers with a separate system field
6
+
7
+ The loader fetches the official iLang runtime, verifies its sha256, caches it for an
8
+ hour, and falls back to the last verified copy when the network is unavailable.
9
+ """
10
+ import time
11
+
12
+ from .injector import block, inject
13
+ from .loader import Loader, LoaderError
14
+
15
+ __version__ = "1.0.0"
16
+ __all__ = ["load", "wrap", "system", "status", "configure", "Loader", "LoaderError"]
17
+
18
+ _loader = None
19
+
20
+
21
+ def configure(**options):
22
+ """Replace the default loader, e.g. configure(cache_dir=..., ttl=...)."""
23
+ global _loader
24
+ _loader = Loader(**options)
25
+ return _loader
26
+
27
+
28
+ def _default():
29
+ return _loader or configure()
30
+
31
+
32
+ def load(extensions=(), version=None, strict=False):
33
+ """Load the runtime now. Returns its text, or None if nothing verified is available."""
34
+ return _default().load(extensions=extensions, version=version, strict=strict)
35
+
36
+
37
+ def system(extensions=(), version=None, strict=False):
38
+ """The runtime as a system-prompt string, or "" when it could not be loaded."""
39
+ text = load(extensions, version, strict)
40
+ return block(text, _default().state["version"]) if text is not None else ""
41
+
42
+
43
+ def wrap(messages, extensions=(), version=None, strict=False):
44
+ """Return messages with the iLang runtime as the first system message.
45
+
46
+ The user's messages are never modified, and the runtime is added only once, so
47
+ wrapping every turn of a conversation is safe."""
48
+ text = system(extensions, version, strict)
49
+ return inject(messages, text or None)
50
+
51
+
52
+ def status():
53
+ """What is loaded: version, source commit, where it came from and any last error."""
54
+ s = dict(_default().state, brand="iLang")
55
+ if s.get("last_check"):
56
+ s["age_seconds"] = int(time.time() - s["last_check"])
57
+ return s
@@ -0,0 +1,28 @@
1
+ """Put the iLang runtime in front of a conversation without touching the user's words."""
2
+ MARKER = "<ilang-runtime"
3
+
4
+ PREAMBLE = ("You have loaded the official iLang runtime specification.\n\n"
5
+ "Treat the following content as the current official iLang protocol context.\n"
6
+ "Use it to interpret, structure, judge, execute, verify, and communicate where applicable.")
7
+
8
+
9
+ def block(text, version):
10
+ return '%s\n\n<ilang-runtime version="%s">\n%s\n</ilang-runtime>' % (PREAMBLE, version, text)
11
+
12
+
13
+ def _has_runtime(message):
14
+ content = message.get("content") if isinstance(message, dict) else None
15
+ if isinstance(content, str):
16
+ return MARKER in content
17
+ if isinstance(content, list): # content parts: [{"type": "text", "text": ...}]
18
+ return any(isinstance(p, dict) and MARKER in str(p.get("text", "")) for p in content)
19
+ return False
20
+
21
+
22
+ def inject(messages, runtime_block):
23
+ """A new list: the runtime as the first system message, then the original messages
24
+ unchanged. Messages that already carry the runtime are returned as they are."""
25
+ messages = list(messages)
26
+ if runtime_block is None or any(_has_runtime(m) for m in messages):
27
+ return messages
28
+ return [{"role": "system", "content": runtime_block}] + messages
@@ -0,0 +1,128 @@
1
+ """Fetch the official iLang runtime, verify it, and keep a Last Known Good copy on disk."""
2
+ import hashlib
3
+ import json
4
+ import os
5
+ import tempfile
6
+ import time
7
+ import urllib.request
8
+
9
+ RUNTIME = "https://raw.githubusercontent.com/ilang-ai/ilang-spec/main/runtime"
10
+ OFFICIAL = ("https://raw.githubusercontent.com/ilang-ai/", "https://github.com/ilang-ai/",
11
+ "https://ilang.ai/")
12
+ TTL = 3600
13
+ TIMEOUT = 10
14
+ AGENT = "ilang-loader-python/1.0.0"
15
+
16
+
17
+ class LoaderError(RuntimeError):
18
+ pass
19
+
20
+
21
+ def _sha(data):
22
+ return hashlib.sha256(data).hexdigest()
23
+
24
+
25
+ class Loader:
26
+ def __init__(self, runtime=RUNTIME, cache_dir=None, ttl=TTL, allow_custom_source=False):
27
+ if not runtime.startswith(OFFICIAL) and not allow_custom_source:
28
+ raise ValueError("iLang loads only from official sources unless allow_custom_source=True")
29
+ self.runtime = runtime.rstrip("/")
30
+ self.cache = cache_dir or os.environ.get("ILANG_CACHE_DIR") or os.path.join(
31
+ os.environ.get("XDG_CACHE_HOME") or os.path.join(os.path.expanduser("~"), ".cache"), "ilang")
32
+ self.ttl = ttl
33
+ self.state = {"loaded": False, "source": "none", "version": None, "source_commit": None,
34
+ "last_check": None, "error": None}
35
+ self._memo = {}
36
+
37
+ def _read(self, *parts):
38
+ try:
39
+ with open(os.path.join(self.cache, *parts), "rb") as f:
40
+ return f.read()
41
+ except OSError:
42
+ return None
43
+
44
+ def _write(self, data, *parts):
45
+ path = os.path.join(self.cache, *parts)
46
+ os.makedirs(os.path.dirname(path), exist_ok=True)
47
+ fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path))
48
+ with os.fdopen(fd, "wb") as f:
49
+ f.write(data)
50
+ os.replace(tmp, path) # atomic: a reader never sees half a file
51
+
52
+ def _fetch(self, url):
53
+ if not url.startswith(OFFICIAL) and not url.startswith(self.runtime):
54
+ raise LoaderError("refusing to fetch from %s" % url)
55
+ req = urllib.request.Request(url, headers={"User-Agent": AGENT})
56
+ with urllib.request.urlopen(req, timeout=TIMEOUT) as r:
57
+ return r.read()
58
+
59
+ def _last_check(self):
60
+ raw = self._read("state.json")
61
+ return json.loads(raw).get("last_check", 0) if raw else 0
62
+
63
+ def _get(self, manifest, name, where, remote):
64
+ """One bundle, verified against the manifest; (bytes, came_from_network)."""
65
+ meta = manifest["bundles"].get(name)
66
+ if meta is None:
67
+ raise LoaderError("the runtime has no bundle named %r" % name)
68
+ data = self._read(*where, name + ".md")
69
+ if data is not None and _sha(data) == meta["sha256"]:
70
+ return data, False
71
+ if not remote:
72
+ raise LoaderError("no verified %s bundle in the cache" % name)
73
+ data = self._fetch(meta["url"])
74
+ if len(data) != meta["bytes"] or _sha(data) != meta["sha256"]:
75
+ raise LoaderError("the %s bundle failed its sha256 check" % name)
76
+ return data, True
77
+
78
+ def _load(self, names, version):
79
+ where = ("versions", version) if version else ("latest",)
80
+ cached = self._read(*where, "manifest.json")
81
+ manifest = json.loads(cached) if cached else None
82
+ checked = False
83
+ if manifest is None or (not version and time.time() - self._last_check() >= self.ttl):
84
+ url = "%s/versions/%s/manifest.json" % (self.runtime, version) if version else \
85
+ self.runtime + "/manifest.json"
86
+ manifest, checked = json.loads(self._fetch(url)), True
87
+ got = [self._get(manifest, n, where, True) for n in names]
88
+ for n, (data, new) in zip(names, got): # write only once everything has verified
89
+ if new:
90
+ self._write(data, *where, n + ".md")
91
+ if checked:
92
+ self._write(json.dumps(manifest).encode(), *where, "manifest.json")
93
+ if not version:
94
+ self._write(json.dumps({"last_check": time.time()}).encode(), "state.json")
95
+ source = "remote" if checked or any(new for _, new in got) else "cache"
96
+ return manifest, [data.decode("utf-8") for data, _ in got], source
97
+
98
+ def load(self, extensions=(), version=None, strict=False):
99
+ """The runtime text, or None when nothing verified is available and strict is off."""
100
+ names = ["core"] + [e for e in extensions if e != "core"]
101
+ key = (tuple(names), version)
102
+ hit = self._memo.get(key)
103
+ if hit and (version or time.time() - hit[1] < self.ttl):
104
+ self.state.update(hit[2])
105
+ return hit[0]
106
+ try:
107
+ manifest, texts, source = self._load(names, version)
108
+ self.state["error"] = None
109
+ except Exception as err: # network, hash or parse failure
110
+ self.state["error"] = "%s: %s" % (type(err).__name__, err)
111
+ try: # Last Known Good, never the network
112
+ where = ("versions", version) if version else ("latest",)
113
+ manifest = json.loads(self._read(*where, "manifest.json") or b"null")
114
+ texts = [self._get(manifest, n, where, False)[0].decode("utf-8") for n in names]
115
+ source = "cache"
116
+ except Exception:
117
+ if strict:
118
+ raise LoaderError(self.state["error"])
119
+ self.state.update(loaded=False, source="none")
120
+ return None
121
+ self.state.update(loaded=True, source=source, version=manifest["version"],
122
+ source_commit=manifest["source_commit"],
123
+ last_check=self._last_check() or None)
124
+ text = "\n\n".join(texts)
125
+ if self.state["error"] is None: # a fallback copy is re-checked next call
126
+ self._memo[key] = (text, time.time(), {k: self.state[k] for k in
127
+ ("loaded", "source", "version", "source_commit")})
128
+ return text
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.4
2
+ Name: ilang-protocol
3
+ Version: 1.0.0
4
+ Summary: The iLang loader: fetches the official iLang runtime, verifies it, caches it and puts it in front of any model's context. Don't learn iLang. Your AI should.
5
+ Author: Long Quan Zhu
6
+ License: MIT
7
+ Project-URL: Homepage, https://ilang.ai
8
+ Project-URL: Source, https://github.com/ilang-ai/ilang
9
+ Project-URL: Specification, https://github.com/ilang-ai/ilang-spec
10
+ Keywords: ilang,iLang,ai,llm,prompt,protocol,agent,openai,anthropic,gemini,deepseek
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
14
+ Requires-Python: >=3.9
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Dynamic: license-file
18
+
19
+ # ilang-protocol
20
+
21
+ **Don't learn iLang. Your AI should.**
22
+
23
+ The Python loader for [iLang](https://ilang.ai). It fetches the official iLang runtime from the canon repository, checks its sha256, keeps a verified copy on disk and adds it to any model's messages.
24
+
25
+ ```bash
26
+ pip install ilang-protocol
27
+ ```
28
+
29
+ ```python
30
+ import ilang
31
+
32
+ messages = ilang.wrap([{"role": "user", "content": "帮我分析这个问题"}]) # OpenAI-style messages
33
+ system = ilang.system() # Anthropic, Gemini
34
+ print(ilang.status())
35
+ ```
36
+
37
+ Updates are checked at most once an hour; without a network the last verified copy is used, and with no copy at all the messages go out unchanged unless `strict=True`. Pin an exact runtime with `version=` for reproducible runs; add `extensions=["media"]` for image, video and audio work. The core runtime is about 27,000 tokens, so enable your provider's prompt caching.
38
+
39
+ Standard library only, Python 3.9+. Source, JavaScript version and documentation: [github.com/ilang-ai/ilang](https://github.com/ilang-ai/ilang). MIT.
@@ -0,0 +1,11 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ ilang/__init__.py
5
+ ilang/injector.py
6
+ ilang/loader.py
7
+ ilang_protocol.egg-info/PKG-INFO
8
+ ilang_protocol.egg-info/SOURCES.txt
9
+ ilang_protocol.egg-info/dependency_links.txt
10
+ ilang_protocol.egg-info/top_level.txt
11
+ tests/test_ilang.py
@@ -0,0 +1,26 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "ilang-protocol"
7
+ version = "1.0.0"
8
+ description = "The iLang loader: fetches the official iLang runtime, verifies it, caches it and puts it in front of any model's context. Don't learn iLang. Your AI should."
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ license = { text = "MIT" }
12
+ authors = [{ name = "Long Quan Zhu" }]
13
+ keywords = ["ilang", "iLang", "ai", "llm", "prompt", "protocol", "agent", "openai", "anthropic", "gemini", "deepseek"]
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "Operating System :: OS Independent",
17
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
18
+ ]
19
+
20
+ [project.urls]
21
+ Homepage = "https://ilang.ai"
22
+ Source = "https://github.com/ilang-ai/ilang"
23
+ Specification = "https://github.com/ilang-ai/ilang-spec"
24
+
25
+ [tool.setuptools]
26
+ packages = ["ilang"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,195 @@
1
+ """Offline tests: a local HTTP server plays the official runtime. Standard library only.
2
+
3
+ python -m unittest discover -s tests (from the python/ directory)
4
+ ILANG_NETWORK_TESTS=1 ... also checks the real official runtime
5
+ """
6
+ import hashlib
7
+ import http.server
8
+ import json
9
+ import os
10
+ import shutil
11
+ import sys
12
+ import tempfile
13
+ import threading
14
+ import unittest
15
+
16
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
17
+ import ilang # noqa: E402
18
+ from ilang.injector import inject # noqa: E402
19
+ from ilang.loader import Loader, LoaderError # noqa: E402
20
+
21
+
22
+ class FakeRuntime:
23
+ """Serves runtime/manifest.json, bundles and versions/<v>/ from a temp directory."""
24
+
25
+ def __init__(self):
26
+ self.root = tempfile.mkdtemp()
27
+ handler = lambda *a, **k: http.server.SimpleHTTPRequestHandler(*a, directory=self.root, **k)
28
+ http.server.SimpleHTTPRequestHandler.log_message = lambda *a: None
29
+ self.server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
30
+ threading.Thread(target=self.server.serve_forever, daemon=True).start()
31
+ self.base = "http://127.0.0.1:%d/runtime" % self.server.server_address[1]
32
+
33
+ def publish(self, version, texts, corrupt=None):
34
+ bundles = {}
35
+ for name, text in texts.items():
36
+ data = text.encode("utf-8")
37
+ self._put(data, "runtime", name + ".md")
38
+ self._put(data, "runtime", "versions", version, name + ".md")
39
+ served = b"tampered" if corrupt == name else data
40
+ if corrupt == name:
41
+ self._put(served, "runtime", name + ".md")
42
+ bundles[name] = {"url": "%s/%s.md" % (self.base, name), "bytes": len(data),
43
+ "sha256": hashlib.sha256(data).hexdigest()}
44
+ manifest = {"brand": "iLang", "runtime_schema": 1, "version": version,
45
+ "source_commit": "c0ffee" + version, "bundles": bundles}
46
+ self._put(json.dumps(manifest).encode(), "runtime", "manifest.json")
47
+ pinned = dict(manifest, bundles={n: dict(b, url="%s/versions/%s/%s.md" % (self.base, version, n))
48
+ for n, b in bundles.items()})
49
+ self._put(json.dumps(pinned).encode(), "runtime", "versions", version, "manifest.json")
50
+
51
+ def _put(self, data, *parts):
52
+ path = os.path.join(self.root, *parts)
53
+ os.makedirs(os.path.dirname(path), exist_ok=True)
54
+ with open(path, "wb") as f:
55
+ f.write(data)
56
+
57
+ def stop(self):
58
+ self.server.shutdown()
59
+ self.server.server_close()
60
+ shutil.rmtree(self.root, ignore_errors=True)
61
+
62
+
63
+ class LoaderTests(unittest.TestCase):
64
+ def setUp(self):
65
+ self.remote = FakeRuntime()
66
+ self.remote.publish("v1", {"core": "CORE ONE", "media": "MEDIA ONE"})
67
+ self.cache = tempfile.mkdtemp()
68
+
69
+ def tearDown(self):
70
+ self.remote.stop()
71
+ shutil.rmtree(self.cache, ignore_errors=True)
72
+
73
+ def loader(self, ttl=3600):
74
+ return Loader(runtime=self.remote.base, cache_dir=self.cache, ttl=ttl, allow_custom_source=True)
75
+
76
+ def test_loads_verifies_and_caches(self):
77
+ ld = self.loader()
78
+ self.assertEqual(ld.load(), "CORE ONE")
79
+ self.assertEqual(ld.state["source"], "remote")
80
+ self.assertEqual(ld.state["version"], "v1")
81
+ self.assertTrue(os.path.exists(os.path.join(self.cache, "latest", "core.md")))
82
+
83
+ def test_within_ttl_the_network_is_not_used(self):
84
+ self.loader().load()
85
+ self.remote.stop()
86
+ ld = self.loader()
87
+ self.assertEqual(ld.load(), "CORE ONE")
88
+ self.assertEqual(ld.state["source"], "cache")
89
+
90
+ def test_remote_down_after_ttl_uses_last_known_good(self):
91
+ self.loader(ttl=0).load()
92
+ self.remote.stop()
93
+ ld = self.loader(ttl=0)
94
+ self.assertEqual(ld.load(), "CORE ONE")
95
+ self.assertEqual(ld.state["source"], "cache")
96
+ self.assertIsNotNone(ld.state["error"])
97
+
98
+ def test_no_cache_and_no_network_fails_open_or_raises_when_strict(self):
99
+ self.remote.stop()
100
+ ld = self.loader()
101
+ self.assertIsNone(ld.load())
102
+ self.assertFalse(ld.state["loaded"])
103
+ with self.assertRaises(LoaderError):
104
+ self.loader().load(strict=True)
105
+
106
+ def test_a_bundle_failing_its_hash_never_replaces_last_known_good(self):
107
+ self.loader(ttl=0).load()
108
+ self.remote.publish("v2", {"core": "CORE TWO", "media": "MEDIA TWO"}, corrupt="core")
109
+ ld = self.loader(ttl=0)
110
+ self.assertEqual(ld.load(), "CORE ONE")
111
+ self.assertIn("sha256", ld.state["error"])
112
+ with open(os.path.join(self.cache, "latest", "core.md"), encoding="utf-8") as f:
113
+ self.assertEqual(f.read(), "CORE ONE")
114
+
115
+ def test_a_new_version_replaces_the_cache(self):
116
+ self.loader(ttl=0).load()
117
+ self.remote.publish("v2", {"core": "CORE TWO", "media": "MEDIA TWO"})
118
+ ld = self.loader(ttl=0)
119
+ self.assertEqual(ld.load(), "CORE TWO")
120
+ self.assertEqual(ld.state["version"], "v2")
121
+
122
+ def test_pinned_version_is_immutable_and_works_offline(self):
123
+ self.remote.publish("v2", {"core": "CORE TWO", "media": "MEDIA TWO"})
124
+ self.assertEqual(self.loader().load(version="v1"), "CORE ONE")
125
+ self.remote.stop()
126
+ self.assertEqual(self.loader().load(version="v1"), "CORE ONE")
127
+
128
+ def test_extensions_are_loaded_on_request(self):
129
+ self.assertEqual(self.loader().load(extensions=["media"]), "CORE ONE\n\nMEDIA ONE")
130
+
131
+ def test_only_official_sources_by_default(self):
132
+ with self.assertRaises(ValueError):
133
+ Loader(runtime="https://example.com/runtime")
134
+
135
+
136
+ class InjectorTests(unittest.TestCase):
137
+ def setUp(self):
138
+ self.remote = FakeRuntime()
139
+ self.remote.publish("v1", {"core": "CORE ONE"})
140
+ self.cache = tempfile.mkdtemp()
141
+ ilang.configure(runtime=self.remote.base, cache_dir=self.cache, allow_custom_source=True)
142
+
143
+ def tearDown(self):
144
+ self.remote.stop()
145
+ shutil.rmtree(self.cache, ignore_errors=True)
146
+
147
+ def test_runtime_goes_first_and_user_messages_are_untouched(self):
148
+ msgs = [{"role": "system", "content": "app rules"}, {"role": "user", "content": "帮我完成这个任务"}]
149
+ original = json.dumps(msgs)
150
+ out = ilang.wrap(msgs)
151
+ self.assertEqual(json.dumps(msgs), original) # input not mutated
152
+ self.assertEqual(out[0]["role"], "system")
153
+ self.assertIn('<ilang-runtime version="v1">', out[0]["content"])
154
+ self.assertIn("CORE ONE", out[0]["content"])
155
+ self.assertEqual(out[1:], msgs)
156
+
157
+ def test_runtime_is_added_only_once(self):
158
+ once = ilang.wrap([{"role": "user", "content": "hi"}])
159
+ again = ilang.wrap(once + [{"role": "assistant", "content": "ok"}, {"role": "user", "content": "next"}])
160
+ self.assertEqual(sum("<ilang-runtime" in m["content"] for m in again), 1)
161
+
162
+ def test_content_parts_are_recognised(self):
163
+ msgs = [{"role": "system", "content": [{"type": "text", "text": "<ilang-runtime version=x>"}]}]
164
+ self.assertEqual(inject(msgs, "block"), msgs)
165
+
166
+ def test_fail_open_returns_the_messages_unchanged(self):
167
+ self.remote.stop()
168
+ ilang.configure(runtime=self.remote.base, cache_dir=tempfile.mkdtemp(), allow_custom_source=True)
169
+ msgs = [{"role": "user", "content": "hi"}]
170
+ self.assertEqual(ilang.wrap(msgs), msgs)
171
+ self.assertEqual(ilang.system(), "")
172
+ self.assertFalse(ilang.status()["loaded"])
173
+
174
+ def test_status_reports_version_and_commit(self):
175
+ ilang.wrap([{"role": "user", "content": "hi"}])
176
+ s = ilang.status()
177
+ self.assertEqual((s["brand"], s["version"], s["source_commit"]), ("iLang", "v1", "c0ffeev1"))
178
+
179
+
180
+ @unittest.skipUnless(os.environ.get("ILANG_NETWORK_TESTS"), "set ILANG_NETWORK_TESTS=1")
181
+ class OfficialRuntimeTests(unittest.TestCase):
182
+ def test_the_official_runtime_loads_and_verifies(self):
183
+ cache = tempfile.mkdtemp()
184
+ try:
185
+ ld = Loader(cache_dir=cache)
186
+ text = ld.load(extensions=["media"], strict=True)
187
+ self.assertIn("iLang runtime bundle (core)", text)
188
+ self.assertIn("iLang runtime bundle (media)", text)
189
+ self.assertEqual(ld.state["source"], "remote")
190
+ finally:
191
+ shutil.rmtree(cache, ignore_errors=True)
192
+
193
+
194
+ if __name__ == "__main__":
195
+ unittest.main()