Spaces:
Running
Running
Download tests/test_612_kernel.py from Vir007/continual-ai: direct link, hf CLI and curl.
- Browser
- Download file 10.8 kB
-
https://huggingface.co/spaces/Vir007/continual-ai/resolve/main/tests/test_612_kernel.py
- Command line
-
hf download hf://spaces/Vir007/continual-ai/tests/test_612_kernel.py
-
curl -L -o test_612_kernel.py https://huggingface.co/spaces/Vir007/continual-ai/resolve/main/tests/test_612_kernel.py
10.8 kB
| from __future__ import annotations | |
| import json | |
| import tempfile | |
| import threading | |
| import unittest | |
| import urllib.request | |
| from pathlib import Path | |
| from continual_ai import ContinualAgent | |
| from continual_ai.http_api import create_server | |
| from continual_ai.plasticity_adapter import suggested_target | |
| from continual_ai.schemas import ProductError, derive_descriptor, validate_descriptor | |
| A = {"workspace": "alpha"} | |
| B = {"workspace": "alpha-related"} | |
| C = {"workspace": "omega"} | |
| class RegistryDescriptorTests(unittest.TestCase): | |
| def setUp(self) -> None: | |
| self.tmp = tempfile.TemporaryDirectory() | |
| self.state = Path(self.tmp.name) / "state" | |
| def tearDown(self) -> None: | |
| self.tmp.cleanup() | |
| def test_explicit_and_derived_descriptors(self) -> None: | |
| with ContinualAgent.open(self.state, consolidate_every=0) as agent: | |
| explicit = agent.register_context(A, [0, 0, 0, 0, 0, 0]) | |
| self.assertTrue(explicit["explicit"]) | |
| self.assertEqual(explicit["descriptor"], [0, 0, 0, 0, 0, 0]) | |
| derived = agent.register_context(B) | |
| self.assertFalse(derived["explicit"]) | |
| self.assertEqual(derived["descriptor"], list(derive_descriptor(derived["canonical"]))) | |
| self.assertEqual(len(derived["descriptor"]), 6) | |
| def test_descriptor_conflict_and_validation(self) -> None: | |
| with ContinualAgent.open(self.state, consolidate_every=0) as agent: | |
| agent.register_context(A, [0, 0, 0, 0, 0, 0]) | |
| with self.assertRaises(ProductError): | |
| agent.register_context(A, [1, 1, 1, 1, 1, 1]) | |
| with self.assertRaises(ProductError): | |
| agent.register_context(C, [0, 1]) | |
| with self.assertRaises(ProductError): | |
| validate_descriptor([0, 1, 2, 0, 1, 0]) | |
| def test_registry_persists_descriptors(self) -> None: | |
| with ContinualAgent.open(self.state, consolidate_every=0) as agent: | |
| agent.register_context(A, [0, 0, 0, 0, 0, 1]) | |
| with ContinualAgent.open(self.state, consolidate_every=0) as reopened: | |
| rec = reopened.contexts.lookup(A) | |
| assert rec is not None | |
| self.assertEqual(list(rec.descriptor or []), [0, 0, 0, 0, 0, 1]) | |
| class KernelConfigureTests(unittest.TestCase): | |
| def setUp(self) -> None: | |
| self.tmp = tempfile.TemporaryDirectory() | |
| self.state = Path(self.tmp.name) / "state" | |
| def tearDown(self) -> None: | |
| self.tmp.cleanup() | |
| def test_configure_pristine_and_reopen(self) -> None: | |
| with ContinualAgent.open(self.state, consolidate_every=0, plasticity_enabled=True) as agent: | |
| agent.register_context(A, [0, 0, 0, 0, 0, 0]) | |
| result = agent.configure_descriptors(blend=1.0) | |
| self.assertTrue(result["configured"]) | |
| self.assertEqual(result["blend"], 1.0) | |
| self.assertEqual(len(result["descriptors"] or []), 8) | |
| kernel = agent.kernel_status() | |
| self.assertFalse(kernel["locked"]) | |
| with ContinualAgent.open(self.state, consolidate_every=0, plasticity_enabled=True) as reopened: | |
| kernel = reopened.kernel_status() | |
| self.assertEqual(kernel["descriptors"][0], [0, 0, 0, 0, 0, 0]) | |
| self.assertEqual(kernel["descriptor_blend"], 1.0) | |
| def test_configure_validation(self) -> None: | |
| with ContinualAgent.open(self.state, consolidate_every=0, plasticity_enabled=True) as agent: | |
| with self.assertRaises(ProductError): | |
| agent.configure_descriptors(blend=9.0) | |
| with self.assertRaises(ProductError): | |
| agent._ensure_plasticity().configure_descriptors([[0, 0, 0, 0, 0, 0]]) | |
| def test_kernel_locks_after_feedback(self) -> None: | |
| with ContinualAgent.open(self.state, consolidate_every=0, plasticity_enabled=True) as agent: | |
| agent.configure_descriptors() | |
| episode = agent.start_episode(context=A) | |
| prediction = agent.predict(episode["episode_id"], "lock-probe") | |
| agent.provide_feedback(prediction["event_id"], prediction["suggested_target"]) | |
| with self.assertRaises(ProductError): | |
| agent.configure_descriptors() | |
| def test_episodic_fast_reset(self) -> None: | |
| # Fast adaptation must not accumulate across episodes: closing an | |
| # episode discards the transient adapter while the trace persists. | |
| with ContinualAgent.open(self.state, consolidate_every=0, plasticity_enabled=True) as agent: | |
| for _ in range(30): | |
| episode = agent.start_episode(context=A) | |
| prediction = agent.predict(episode["episode_id"], "drift-input") | |
| agent.provide_feedback(prediction["event_id"], 0) | |
| agent.close_episode(episode["episode_id"]) | |
| core = agent._ensure_plasticity()._core | |
| self.assertEqual(float(abs(core.fast_context_token_bank).sum()), 0.0) | |
| self.assertGreater(core.trace_size(), 0) | |
| class KernelCalibrateTests(unittest.TestCase): | |
| def setUp(self) -> None: | |
| self.tmp = tempfile.TemporaryDirectory() | |
| self.state = Path(self.tmp.name) / "state" | |
| def tearDown(self) -> None: | |
| self.tmp.cleanup() | |
| def test_calibrate_empty_trace(self) -> None: | |
| with ContinualAgent.open(self.state, consolidate_every=0, plasticity_enabled=True) as agent: | |
| agent.configure_descriptors() | |
| with self.assertRaises(ProductError): | |
| agent.calibrate_descriptors() | |
| def test_calibrate_shadow_in_safe_mode(self) -> None: | |
| with ContinualAgent.open(self.state, consolidate_every=0, plasticity_enabled=True) as agent: | |
| agent.configure_descriptors() | |
| episode = agent.start_episode(context=A) | |
| prediction = agent.predict(episode["episode_id"], "shadow-input") | |
| agent.provide_feedback(prediction["event_id"], 1) | |
| agent.close_episode(episode["episode_id"]) | |
| before = agent.kernel_status()["desc_weights"] | |
| report = agent.calibrate_descriptors() | |
| self.assertEqual(report["status"], "shadow") | |
| self.assertFalse(report["committed"]) | |
| self.assertEqual(agent.kernel_status()["desc_weights"], before) | |
| def test_propose_budget_validation(self) -> None: | |
| with ContinualAgent.open(self.state, consolidate_every=0, plasticity_enabled=True) as agent: | |
| with self.assertRaises(ProductError): | |
| agent.propose_candidate(budget=0) | |
| with self.assertRaises(ProductError): | |
| agent.propose_candidate(budget=100000) | |
| class KernelTransferTests(unittest.TestCase): | |
| def setUp(self) -> None: | |
| self.tmp = tempfile.TemporaryDirectory() | |
| self.state = Path(self.tmp.name) / "state" | |
| def tearDown(self) -> None: | |
| self.tmp.cleanup() | |
| def test_related_transfer_end_to_end(self) -> None: | |
| signals = ["alpha-signal-one", "alpha-signal-two"] | |
| targets_a = {signal: suggested_target(signal) for signal in signals} | |
| targets_c = {signal: (target + 2) % 4 for signal, target in targets_a.items()} | |
| with ContinualAgent.open(self.state, consolidate_every=0, | |
| plasticity_enabled=True, plasticity_safe_mode=False) as agent: | |
| agent.register_context(A, [0, 0, 0, 0, 0, 0]) | |
| agent.register_context(B, [0, 0, 0, 0, 0, 1]) | |
| agent.register_context(C, [1, 1, 1, 1, 1, 1]) | |
| agent.configure_descriptors(blend=1.0) | |
| for _ in range(32): | |
| for signal in signals: | |
| for context, targets in ((A, targets_a), (C, targets_c)): | |
| episode = agent.start_episode(context=context) | |
| prediction = agent.predict(episode["episode_id"], signal) | |
| agent.provide_feedback(prediction["event_id"], targets[signal]) | |
| agent.close_episode(episode["episode_id"]) | |
| proposal = agent.propose_candidate(budget=256) | |
| self.assertEqual(proposal["status"], "candidate_accepted") | |
| agent.promote(proposal["proposal_id"]) | |
| for signal in signals: | |
| self.assertEqual(agent.probe(signal, context=A)["prediction"], targets_a[signal]) | |
| self.assertEqual(agent.probe(signal, context=C)["prediction"], targets_c[signal]) | |
| self.assertEqual(agent.probe(signal, context=B)["prediction"], targets_a[signal]) | |
| agent.checkpoint() | |
| with ContinualAgent.open(self.state, consolidate_every=0, | |
| plasticity_enabled=True, plasticity_safe_mode=False) as reopened: | |
| for signal in signals: | |
| self.assertEqual(reopened.probe(signal, context=B)["prediction"], targets_a[signal]) | |
| class KernelApiTests(unittest.TestCase): | |
| def setUp(self) -> None: | |
| self.tmp = tempfile.TemporaryDirectory() | |
| self.state = Path(self.tmp.name) / "state" | |
| def tearDown(self) -> None: | |
| self.tmp.cleanup() | |
| def _post(self, base: str, route: str, payload: dict) -> dict: | |
| data = json.dumps(payload).encode("utf-8") | |
| request = urllib.request.Request(base + route, data=data, | |
| headers={"Content-Type": "application/json"}, method="POST") | |
| with urllib.request.urlopen(request) as response: | |
| return json.loads(response.read().decode("utf-8")) | |
| def test_kernel_endpoints(self) -> None: | |
| with ContinualAgent.open(self.state, consolidate_every=0, plasticity_enabled=True) as agent: | |
| server = create_server(agent, host="127.0.0.1", port=0) | |
| thread = threading.Thread(target=server.serve_forever, daemon=True) | |
| thread.start() | |
| try: | |
| base = f"http://127.0.0.1:{server.server_port}" | |
| registered = self._post(base, "/v1/contexts/register", | |
| {"context": A, "descriptor": [0, 0, 0, 0, 0, 0]}) | |
| self.assertTrue(registered["explicit"]) | |
| with self.assertRaises(Exception): | |
| self._post(base, "/v1/descriptors/configure", {"blend": 1.0}) | |
| configured = self._post(base, "/v1/descriptors/configure", | |
| {"confirm": True, "blend": 1.0}) | |
| self.assertTrue(configured["configured"]) | |
| with urllib.request.urlopen(base + "/v1/kernel") as response: | |
| kernel = json.loads(response.read().decode("utf-8")) | |
| self.assertEqual(len(kernel["descriptors"]), 8) | |
| finally: | |
| server.shutdown() | |
| server.server_close() | |
| thread.join(timeout=5) | |
| if __name__ == "__main__": | |
| unittest.main() | |