"""Validate task order in an independent local workflow and read its role settings."""
from concurrent.futures import ThreadPoolExecutor
from hashlib import sha256
import json
from pathlib import Path
import sys
from threading import Barrier
import tomllib
CONFIG = '[agents]\nenabled = true\nmax_concurrent_threads_per_session = 2\n'
ROLE = 'name = "catalog_reviewer"\ndescription = "Review local catalog fields."\ndeveloper_instructions = "Verify identifiers and report evidence."\n'
def main():
cases = []
def check(name, data, operation, expected, actual):
assert actual == expected, (name, expected, actual)
cases.append(dict(name="subagents_" + name, input=data, operation=operation, expected=expected, actual=actual))
config = tomllib.loads(CONFIG)["agents"]
role = tomllib.loads(ROLE)
check("config", [CONFIG, ROLE], "Parse the current sample keys and role metadata using Python",
[True, 2, "catalog_reviewer", "Verify identifiers and report evidence."],
[config["enabled"], config["max_concurrent_threads_per_session"], role["name"], role["developer_instructions"]])
records = {"catalog": ["A", "B"], "orders": ["C", "C"]}
rendezvous = Barrier(2, timeout=5)
def inspect_area(name):
rendezvous.wait()
values = records[name]
return name, len(values) - len(set(values))
with ThreadPoolExecutor(max_workers=config["max_concurrent_threads_per_session"]) as workers:
tasks = [workers.submit(inspect_area, label) for label in records]
parallel = dict(task.result(timeout=5) for task in tasks)
check("parallel", records, "Run both independent checks before collecting their results",
{"catalog":0, "orders":1}, parallel)
stages = []
schema = {"code": str, "quantity": int}
stages.append("schema")
row = {"code":"B4","quantity":2}
row_is_valid = all(type(row[key]) is expected_type for key,expected_type in schema.items())
accepted = dict(row) if row_is_valid else {}
stages.append("endpoint")
assert accepted == row
stages.append("tests")
check("sequence", row, "Build a schema, validate the endpoint payload, then check it",
["schema","endpoint","tests"], stages)
print(json.dumps(dict(version={"python": sys.version.split()[0],"kind":"local Python threads and TOML"},
python_sha256=sha256(Path(sys.executable).read_bytes()).hexdigest(),cases=cases)))
if __name__=="__main__":
main()
本例需在 Windows、Python 3.12.14。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows、Python 3.12.14。
入口文件:projects/codex-subagents/subagents_lab.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
subagents_lab.py,或下载右侧课程工程包。 - 确认本地已准备 Windows、Python 3.12.14。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a90.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a121.zip