JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
import asyncio
import pytest
from jiuwenswarm.symphony.fingerprint.batching import gather_limited
from jiuwenswarm.symphony.fingerprint.models import (
ExtractedSkillSchema,
ParameterSpec,
RawSkillManifest,
SkillFolder,
)
from jiuwenswarm.symphony.fingerprint.normalize import (
IONameResolution,
SkillFingerprintNormalizer,
)
from jiuwenswarm.symphony.fingerprint.pipeline import FingerprintExtractor
class _VocabularyObservingResolver:
batch_size = 1
def __init__(self):
self.vocabulary_snapshots = []
async def resolve_async(self, candidates_by_skill, vocabulary):
self.vocabulary_snapshots.append(vocabulary.term_names())
return {
candidate.token: IONameResolution(
action="create_new",
normalized_value=candidate.token,
confidence=0.9,
)
for candidates in candidates_by_skill
for candidate in candidates
}
@pytest.mark.asyncio
async def test_extraction_batches_preserve_order_and_worker_limit():
active = 0
max_active = 0
async def run_batch(batch):
nonlocal active, max_active
active += 1
max_active = max(max_active, active)
try:
await asyncio.sleep(0.01 * (4 - batch[0]))
return batch[0]
finally:
active -= 1
results = await gather_limited(
[[1], [2], [3]],
max_workers=2,
run_batch=run_batch,
)
assert results == [1, 2, 3]
assert max_active == 2
@pytest.mark.asyncio
async def test_extraction_batch_failure_is_propagated():
async def run_batch(batch):
if batch == [2]:
raise RuntimeError("extract failed")
await asyncio.sleep(0)
return batch[0]
with pytest.raises(RuntimeError, match="extract failed"):
await gather_limited(
[[1], [2], [3]],
max_workers=2,
run_batch=run_batch,
)
@pytest.mark.asyncio
async def test_normalization_batches_commit_vocabulary_in_order(tmp_path):
resolver = _VocabularyObservingResolver()
normalizer = SkillFingerprintNormalizer(io_name_resolver=resolver)
extractor = FingerprintExtractor(
schema_extractor=object(),
normalizer=normalizer,
normalization_workers=4,
normalization_batch_size=1,
)
manifests = {}
schemas = {}
indexed_folders = []
for index, input_name in enumerate(("first_value", "second_value")):
folder = SkillFolder(
id_hint=f"skill-{index}",
path=tmp_path / f"skill-{index}",
entry=tmp_path / f"skill-{index}" / "SKILL.md",
relative_path=f"skill-{index}",
)
manifests[index] = RawSkillManifest(
folder=folder,
frontmatter={"name": f"skill-{index}"},
body="Test.",
body_sha256=f"sha-{index}",
)
schemas[index] = ExtractedSkillSchema(
description="Test schema",
inputs=[ParameterSpec(name=input_name, type="text")],
outputs=[],
)
indexed_folders.append((index, folder))
results = await extractor._normalize_schemas(
indexed_folders,
len(indexed_folders),
manifests,
schemas,
)
assert list(results) == [0, 1]
assert resolver.vocabulary_snapshots == [[], ["first_value"]]