"""Lesson 29 check: deepseek-harness, an agent harness where everything is a plugin.

Plain Python, no PyTorch and no NumPy: this chapter is about software architecture, and the toy is a program.

What is computed here:
  0. the facts the chapter quotes from the repository and the Cordis paper (paraphrased, with file paths);
  1. the toy task and a scripted fake model that returns fixed replies (deterministic, labelled as a toy);
  2. attempt 1: a while-loop harness with two hard-coded tools, its event trace, and the lines of the loop
     that name a concrete part;
  3. attempt 2: the same loop with a tool registry, what it frees and what stays fixed, and the registrations
     it leaves behind when a module is removed;
  4. a toy Cordis (Context, Fiber, plugin, inject, provide, effect, on, emit, dispose) and the harness rebuilt
     as ten plugins with the repository's names; the same task, its session-event trace, and the same answer;
  5. loading and unloading while the harness runs: boot in the wrong order, a file-system provider swapped in
     the middle of a turn, a service withdrawn and returned, and the context compared before and after;
  6. the sandbox as a provider: the same out-of-workspace request under three harnesses;
  7. the request the DeepSeek adapter builds (Messages blocks, reasoning effort, thinking sent back), beside the
     DSML text of lesson 14, which the harness never writes;
  8. context management: the compaction trigger at the real defaults, and the shared request prefix with and
     without one compaction on lesson 14's trajectory;
  9. the counts of the repository used in the chapter.

Sources read for section 0 (deepseek-ai/deepseek-harness, branch master, release dsh-v0.2.2-rc.3, read through the
GitHub API on 14 November 2026, nothing cloned and nothing run): README.md, SAFETY.md, docs/architecture.md,
docs/cordis-primer.md, docs/cordis-tutorial 01, 02, 03, 06, 07, docs/cordis-api/fiber.md, docs/agent-lifecycle.md,
docs/tool-execution-pipeline.md, docs/glossary.md, docs/subsystems (sandbox, approval, compaction, session,
storage, spill, permission-presets, subagent), docs/deepseek-llm-api-wire-extensions.md, vendor/README.md,
vendor/cordis (README, context.ts, fiber.ts, service.ts, registry.ts, reflect.ts), packages/bundle/base and
web-app cordis.patch.yml, packages/core (agent-loop, tools, tool-executor-native, session, agent),
packages/llm (llm, llm-deepseek, llm-deepseek-api-key), packages/shell (tool-bash, bash-sandbox, bash-local),
packages/sandbox (sandbox, sandbox-local, sandbox-policy), packages/compaction/compaction-basic,
packages/session (session-persistence-jsonl, session-log-deepseek), packages/api/session-controller, apps/cli.
The Cordis paper: arXiv 2608.25512 v1, abstract, sections 1 and 1.3, and algorithms 3 to 5 of section 5.1.

Chosen for this chapter, not taken from any source: the task, the scripted model and its replies, the two-file
"disk", every line of the three toy harnesses, the order of the boot and of the unloading experiments, the
200-token system prompt and the 120-token summary of section 8. Lesson 14's trajectory is made up there.
Every number in lesson-29-deepseek-harness.md is printed here.
Run:  /Users/rajat/.pyenv/shims/python3 lesson29_check.py
"""
import inspect
import json
import math


def code_lines(*objs):
    """Lines of source that are neither blank nor a comment."""
    n = 0
    for o in objs:
        for line in inspect.getsource(o).splitlines():
            s = line.strip()
            if s and not s.startswith("#") and not s.startswith('"""'):
                n += 1
    return n


def naming(obj, needles):
    """Lines of obj's source that contain one of the needles: the lines that name a concrete part."""
    return sum(any(k in line for k in needles) for line in inspect.getsource(obj).splitlines())


# ---------------------------------------------------------------- 0. published facts
FACTS = [
    "THE REPOSITORY (README.md, package.json, GitHub metadata)",
    "Description: 'DeepSeek Harness: Everything is a Plugin.' MIT licence. Topics: ai-agents, cordis, dsh, dsh-plugin.",
    "'DeepSeek Harness is in developer preview and iterating rapidly. THERE WILL BE COMPATIBILITY-BREAKING CHANGES.'",
    "Version 0.2.2-rc.3 (root package.json; GitHub release dsh-v0.2.2-rc.3, a pre-release, 13 Nov 2026). Node >= 24.9.",
    "Start: 'npx @deepseek-ai/dsh web' serves the Web UI at http://127.0.0.1:3080 by default.",
    "      (packages/bundle/web-app/cordis.patch.yml: host ctx.webStartup.host ?? '127.0.0.1', port ?? 3080.)",
    "Built on Cordis (github.com/cordiverse/cordis), vendored under vendor/cordis as @deepseek-ai/cordis at upstream",
    "      version 4.0.0-rc.12 (vendor/README.md). Plugins are found through the GitHub topic dsh-plugin.",
    "",
    "CORDIS (arXiv 2608.25512 v1, Shi, Zhang, Cui; vendor/cordis/src; docs/cordis-primer.md; docs/cordis-tutorial)",
    "Temporal composability: 'the ability to completely revert a component's side effects upon removal'.",
    "Spatial composability: 'the ability to declare and reactively manage inter-component dependencies'.",
    "Revertible effects: every change to the context carries an inverse that the runtime holds.",
    "Reactive coeffects: a component declares what it requires; each context change activates or deactivates it.",
    "Paper section 1.2.2 names 'self-evolving agent harnesses' as a motivating case; without temporal",
    "      composability each change 'forces a full restart that discards all process-local accumulated state'.",
    "Code names: Context (ctx), plugin (a function with optional inject, an object with apply, or a Service",
    "      subclass), ctx.plugin() returns a Fiber, Service registers itself as ctx.<name>, static inject / inject",
    "      lists required services, ctx.effect() returns a disposer, ctx.on / ctx.emit for events, fiber.dispose().",
    "FiberState (vendor/cordis/src/fiber.ts): PENDING, LOADING, ACTIVE, FAILED, DISPOSED, UNLOADING.",
    "Tutorial 3: 'If a required service disappears while the app runs ... every dependent plugin is unloaded too,",
    "      and loads again when the service returns.' Tutorial 2: disposers start in reverse registration order.",
    "Paper algorithm 5: refresh recomputes the fiber's target from its declared keys; unload 'reverts all tracked",
    "      effects in LIFO order'. A binding counts as available only while the fiber that installed it is ACTIVE.",
    "",
    "THE HARNESS (docs/architecture.md and the source files named)",
    "'Every part of the product is a plugin, including the model adapter, the tool registry, the session log, and",
    "      the agent loop itself, so each is replaceable from configuration.' 'There is no privileged core to patch.'",
    "A step is 'one model request plus the tools it calls'; a turn is 'zero or more steps'.",
    "A seam is a swappable capability with three roles: Service Definition, Service Provider, Consumer.",
    "AgentLoop (packages/core/agent-loop/src/index.ts): class AgentLoop extends Service, registered as 'agentLoop',",
    "      static inject = ['agents', 'sessions', 'llm', 'tools', 'systemPrompt', 'sessionProjections'].",
    "The driver (packages/core/agent-loop/src/agent.ts, class ReactLoopAgent): followup() queues a message for the",
    "      next turn, steer() for the next step; turn() appends turn/start, loops over step(); step() streams one",
    "      model request, appends assistant/message, returns 'completed' when the message has no tool-call block,",
    "      otherwise calls ctx.tools.execute({calls, ...}) and the turn loop runs another step.",
    "README of agent-loop, limitations: 'No built-in turn budget'. maxParallelToolCalls defaults to 10.",
    "DeepSeek adapter (packages/llm/llm-deepseek/src/serialize.ts and adapter.ts): POST <baseURL>/v1/messages with",
    "      baseURL https://api.deepseek.com/anthropic; reasoning blocks become 'thinking' blocks (with a stored",
    "      signature), tool calls 'tool_use', tool results 'tool_result' inside a user-role message; reasoning",
    "      effort off | low | high | max, default high, sent as output_config.effort; off sends thinking disabled.",
    "      Default models deepseek-flash and deepseek-v4-pro, context window 1,048,576, output cap 256,000.",
    "A code search of the repository for 'DSML' and for 'deepseek-recipe' returns no result (14 Nov 2026).",
    "Tools (packages/core/tools/src/pipeline.ts, docs/tool-execution-pipeline.md): tool/call is logged, then the",
    "      tools/pre-execute waterfall (hooks, permission, sandbox), ctx.approval for an 'ask', registered guards,",
    "      the tools/execute waterfall around the tool body, tools/post-execute, then tool/result.",
    "bash (packages/shell/tool-bash, bash-sandbox, packages/sandbox/sandbox-local): ctx.shell.execute ->",
    "      ctx.sandbox.confine(['bash', '-c', command], policy) -> Linux bwrap then Landlock, macOS Seatbelt,",
    "      Windows ACL restricted token -> ctx.subprocess.",
    "",
    "SAFETY (SAFETY.md; docs/subsystems/sandbox.md, approval.md; packages/bundle/base/cordis.patch.yml)",
    "'It has not undergone a security audit and must not be treated as secure or production-ready.'",
    "Sandboxing, approval prompts and permission controls 'do not guarantee isolation or prevent damage'.",
    "'Prefer a disposable virtual machine, container, or dedicated environment.'",
    "SandboxMode = read-only | workspace-write | danger-full-access; it 'governs filesystem effects only';",
    "      'Network and process visibility are outside this vocabulary.' Enforcement is reported as full or partial.",
    "Base bundle defaults: sandbox mode DSH_PERMISSION_MODE ?? 'workspace-write'; approval policy 'ask', or",
    "      'never' under danger-full-access. Approval outcomes: allowed-once, rejected, cancelled, unavailable;",
    "      callers 'fail closed unless it is allowed-once'.",
    "Session-log upload (packages/session/session-log-deepseek): on the official API route the request carries a",
    "      dsh_session_log field, enabled by default, which 'may expose the Session working directory,",
    "      system-prompt snapshots, user and Assistant content, ... tool arguments and results'. A switch turns it off.",
    "",
    "CONTEXT (packages/compaction/compaction-basic/README.md; base bundle)",
    "Trigger floor(min(W x 0.8, W - O - B)) with headroom B = 65,536; the newest 16% of W - O stays verbatim; the",
    "      oldest balanced span is replaced by one summary message. Tool results above 8,192 characters are pruned",
    "      to a head of 4,096 and a tail of 1,024; a result above 12,500 tokens is spilled to a file.",
    "'Summarization reuses the provider's warm prefix.' deepseek-flash declares systemPromptUpdate: in-history, so",
    "      a changed prompt is appended 'after the cached history instead of rewriting the leading system message'.",
    "",
    "NOT FOUND in the repository: the words DSML or deepseek-recipe; a turn or step budget; a security audit; any",
    "      benchmark of agent quality in the files read; a component named an asynchronous start/stop loop (the",
    "      nearest is the interrupt_agent tool, which the built-in configurations switch off with interrupt: false).",
]

# ---------------------------------------------------------------- 1. the task and the scripted model
TASK = "Read forecast.txt and save its last word to note.txt"
ROGUE = "Read forecast.txt and save its last word to ../outside.txt"
FORECAST = "The next day is bright and warm"


def new_disk():
    """The machine's files. They belong to no harness and outlive every plugin."""
    return {"forecast.txt": FORECAST}


def scripted_model(request):
    """A fake model: fixed replies chosen by how many tool results follow the last user message. A toy."""
    msgs = request["messages"]
    last_user = max(i for i, m in enumerate(msgs) if m["role"] == "user")
    target = msgs[last_user]["content"].split()[-1]
    results = [m for m in msgs[last_user + 1:] if m["role"] == "tool"]
    if results and results[-1].get("isError"):
        return [{"type": "text", "text": "I could not finish: " + results[-1]["content"]}]
    if len(results) == 0:
        return [{"type": "reasoning", "text": "The sentence is in the file. Read it first."},
                {"type": "tool-call", "id": "call-1", "name": "read", "arguments": {"path": "forecast.txt"}}]
    word = results[0]["content"].split()[-1]
    if len(results) == 1:
        return [{"type": "reasoning", "text": f"The last word is {word}. Save it."},
                {"type": "tool-call", "id": "call-2", "name": "write", "arguments": {"path": target, "text": word}}]
    return [{"type": "text", "text": f"The next day is bright and {word}. I saved the word to {target}."}]


# ---------------------------------------------------------------- 2. attempt 1: one loop, two hard-coded tools
def harness_v1(task, files):
    messages = [{"role": "user", "content": task}]
    trace = ["user/message"]
    while True:
        reply = scripted_model({"messages": messages})
        messages.append({"role": "assistant", "content": reply})
        trace.append("assistant/message")
        calls = [b for b in reply if b["type"] == "tool-call"]
        if not calls:
            return reply[-1]["text"], trace
        for call in calls:
            args = call["arguments"]
            if call["name"] == "read":
                result = files[args["path"]]
            elif call["name"] == "write":
                files[args["path"]] = args["text"]
                result = "ok"
            messages.append({"role": "tool", "toolCallId": call["id"], "content": result})
            trace.append(f"tool/call {call['name']}")
            trace.append("tool/result")


PARTS = {  # what a line must mention to count as naming that concrete part
    "the model": ["scripted_model"],
    "the tools": ['== "read"', '== "write"'],
    "the files": ["files[", "disk["],
    "the conversation": ["messages = [", "messages.append"],
    "the interface": ["trace"],
}

# ---------------------------------------------------------------- 3. attempt 2: a tool registry
REGISTRY = {}


def register(name, fn):
    REGISTRY[name] = fn


def fs_tools_module(files):
    """A module that contributes two tools. Nothing records that these two entries belong together."""
    register("read", lambda a: files[a["path"]])
    register("write", lambda a: files.__setitem__(a["path"], a["text"]) or "ok")


def harness_v2(task):
    messages = [{"role": "user", "content": task}]
    trace = ["user/message"]
    while True:
        reply = scripted_model({"messages": messages})
        messages.append({"role": "assistant", "content": reply})
        trace.append("assistant/message")
        calls = [b for b in reply if b["type"] == "tool-call"]
        if not calls:
            return reply[-1]["text"], trace
        for call in calls:
            result = REGISTRY[call["name"]](call["arguments"])
            messages.append({"role": "tool", "toolCallId": call["id"], "content": result})
            trace.append(f"tool/call {call['name']}")
            trace.append("tool/result")


# ---------------------------------------------------------------- 4. a toy Cordis
PENDING, LOADING, ACTIVE, UNLOADING, DISPOSED = "PENDING", "LOADING", "ACTIVE", "UNLOADING", "DISPOSED"


class Root:
    """What every context shares: the service table, the listeners, the fibers, and a clock for the figures."""

    def __init__(self):
        self.services, self.listeners, self.fibers = {}, [], []
        self.uid, self.tick, self.timeline, self.label = 0, 0, [], "boot"
        self.mounts = []                                       # (tick, plugin name), for the lifetime figure

    def resolve(self, key):                                    # a service counts only while its provider is ACTIVE
        impl = self.services.get(key)
        return impl if impl and impl["fiber"].state == ACTIVE else None

    def notify(self, key):                                     # paper, algorithm 3
        for fiber in list(self.fibers):
            if key in fiber.inject:
                fiber.refresh()


class Fiber:
    """One loaded plugin instance: its state, what it needs, what it provides, and how to undo it."""

    def __init__(self, root, plugin):
        self.root, self.plugin = root, plugin
        self.name, self.inject = plugin.name, plugin.inject
        self.state, self.target, self.disposables, self.provides = PENDING, None, [], []
        root.mounts.append((root.tick, self.name))

    def set(self, state):
        self.root.tick += 1
        self.root.timeline.append((self.root.tick, self.root.label, self.name, self.state, state))
        self.state = state

    def refresh(self):                                         #A
        if self.state not in (PENDING, ACTIVE):
            return
        impls = [self.root.resolve(key) for key in self.inject]
        target = None if None in impls else tuple(i["uid"] for i in impls)   #B
        if target == self.target:
            return
        if self.state == ACTIVE:
            self.unload()                                      #C
        self.target = target
        if target is not None:
            self.set(LOADING)
            self.plugin(Context(self.root, self))              #D
            self.set(ACTIVE)
            for key in self.provides:
                self.root.notify(key)                          #E

    def unload(self, final=False):
        self.set(UNLOADING)
        for key in self.provides:
            self.root.notify(key)                              #F
        while self.disposables:
            self.disposables.pop()()                           #G
        self.provides, self.target = [], None
        self.set(DISPOSED if final else PENDING)

    def dispose(self):
        if self.state == ACTIVE:
            self.unload(final=True)
        elif self.state != DISPOSED:
            self.set(DISPOSED)
        if self in self.root.fibers:
            self.root.fibers.remove(self)


class Context:
    """The object a plugin receives. Everything a plugin does to the application goes through it."""

    def __init__(self, root, fiber):
        self.root, self.fiber = root, fiber

    def __getattr__(self, key):                                # ctx.tools, ctx.llm, ctx.sessions
        impl = self.root.resolve(key)
        if impl is None:
            raise AttributeError(f"service {key} is not available")
        return impl["value"]

    def get(self, key):                                        # an optional dependency: None when absent
        impl = self.root.resolve(key)
        return impl["value"] if impl else None

    def effect(self, execute):                                 #H
        dispose = execute()
        self.fiber.disposables.append(dispose)
        return dispose

    def provide(self, key, value):                             #I
        def install():
            self.root.uid += 1
            self.root.services[key] = {"uid": self.root.uid, "value": value, "fiber": self.fiber}
            return lambda: self.root.services.pop(key)
        self.effect(install)
        self.fiber.provides.append(key)

    def on(self, event, listener):
        def install():
            self.root.listeners.append((event, listener))
            return lambda: self.root.listeners.remove((event, listener))
        self.effect(install)

    def emit(self, event, *args):
        for name, listener in list(self.root.listeners):
            if name == event:
                listener(*args)

    def plugin(self, plugin):                                  # a child plugin is itself an effect of its parent
        fiber = Fiber(self.root, plugin)
        self.root.fibers.append(fiber)
        self.effect(lambda: fiber.dispose)
        fiber.refresh()
        return fiber


def plugin(name, inject=()):
    """Cordis accepts a function with optional name and inject fields. This decorator sets the two fields."""
    def wrap(apply):
        apply.name, apply.inject = name, tuple(inject)
        return apply
    return wrap


@plugin("root")
def _root_plugin(ctx):
    pass


def new_app():
    root = Root()
    fiber = Fiber(root, _root_plugin)
    fiber.state = ACTIVE
    return Context(root, fiber)


# ---------------------------------------------------------------- 4b. the harness as plugins
def make_plugins(disk):
    """Ten plugins with the repository's package names. Real Cordis traces which plugin calls a service; the
    toy passes the caller's ctx by hand, so that a registration is undone with the plugin that made it."""

    @plugin("session")                                         # packages/core/session -> ctx.sessions
    def session(ctx):
        class SessionStore:
            def __init__(self):
                self.events = []

            def append(self, kind, data=None):
                event = (len(self.events) + 1, kind, data)
                self.events.append(event)
                ctx.emit("session/event", event)
                return event

            def derive_messages(self):                         # the model's history is derived from the log
                return [data for _, kind, data in self.events
                        if kind in ("user/message", "assistant/message", "tool/result")]
        ctx.provide("sessions", SessionStore())

    @plugin("llm")                                             # packages/llm/llm -> ctx.llm
    def llm(ctx):
        class LlmRuntime:
            adapters = {}

            def register_adapter(self, caller, provider, adapter):
                def install():
                    self.adapters[provider] = adapter
                    return lambda: self.adapters.pop(provider)
                caller.effect(install)

            def stream(self, request):
                if request["provider"] not in self.adapters:
                    raise LookupError("NO_ADAPTER")
                return self.adapters[request["provider"]](request)
        ctx.provide("llm", LlmRuntime())

    @plugin("llm-scripted", inject=["llm"])                    # stands where packages/llm/llm-deepseek-api-key stands
    def llm_scripted(ctx):
        ctx.llm.register_adapter(ctx, "scripted", scripted_model)

    @plugin("tools")                                           # packages/core/tools -> ctx.tools
    def tools(ctx):
        class ToolsService:
            def __init__(self):
                self.definitions, self.executor = {}, None

            def register(self, caller, name, execute):
                def install():
                    self.definitions[name] = execute
                    return lambda: self.definitions.pop(name)
                caller.effect(install)

            def set_executor(self, caller, executor):
                def install():
                    self.executor = executor
                    return lambda: setattr(self, "executor", None)
                caller.effect(install)

            def execute(self, calls, log):
                if self.executor is None:
                    raise LookupError("TOOL_EXECUTOR_UNAVAILABLE")
                return self.executor(calls, log)
        ctx.provide("tools", ToolsService())

    @plugin("tool-executor-native", inject=["tools"])          # packages/core/tool-executor-native
    def tool_executor_native(ctx):
        def execute(calls, log):
            for call in calls:
                log.append("tool/call", call)
                tool = ctx.tools.definitions.get(call["name"])
                try:
                    if tool is None:
                        raise LookupError(f"no tool named {call['name']}")
                    result = {"role": "tool", "toolCallId": call["id"], "content": tool(call["arguments"])}
                except Exception as error:                     # a failure becomes a result the model can read
                    result = {"role": "tool", "toolCallId": call["id"], "content": str(error), "isError": True}
                log.append("tool/result", result)
        ctx.tools.set_executor(ctx, execute)

    @plugin("fs-local")                                        # packages/fs/fs-local -> ctx.fs, no boundary
    def fs_local(ctx):
        class LocalFs:
            label = "fs-local"

            def read(self, path):
                return disk[path]

            def write(self, path, text):
                disk[path] = text
                return "ok"
        ctx.provide("fs", LocalFs())

    @plugin("fs-sandbox")                                      # packages/fs/fs-sandbox -> ctx.fs, workspace-write
    def fs_sandbox(ctx):
        class SandboxFs:
            label = "fs-sandbox"

            def read(self, path):
                return disk[path]

            def write(self, path, text):
                if path.startswith("/") or ".." in path:
                    raise PermissionError(f"{path} is outside the workspace")
                disk[path] = text
                return "ok"
        ctx.provide("fs", SandboxFs())

    @plugin("tool-fs", inject=["tools", "fs"])                 # packages/fs/tool-fs: the tools read and write
    def tool_fs(ctx):
        ctx.tools.register(ctx, "read", lambda a: ctx.fs.read(a["path"]))
        ctx.tools.register(ctx, "write", lambda a: ctx.fs.write(a["path"], a["text"]))

    @plugin("agent-loop", inject=["sessions", "llm", "tools"])  # packages/core/agent-loop -> ctx.agentLoop
    def agent_loop(ctx):
        def followup(text, provider):
            log = ctx.sessions
            log.append("turn/start")
            log.append("user/message", {"role": "user", "content": text})
            step, reason = 0, "error"
            try:
                while True:
                    step += 1
                    log.append("step/start", step)
                    try:
                        reply = ctx.llm.stream({"provider": provider, "messages": log.derive_messages()})
                        log.append("assistant/message", {"role": "assistant", "content": reply})
                        calls = [b for b in reply if b["type"] == "tool-call"]
                        if calls:
                            ctx.tools.execute(calls, log)
                    finally:
                        log.append("step/end", step)
                    if not calls:
                        reason = "completed"
                        return reply[-1]["text"]
            finally:
                log.append("turn/end", reason)
        ctx.provide("agentLoop", followup)

    @plugin("headless")                                        # packages/bundle/headless: an interface is a listener
    def headless(ctx):
        seen = []
        ctx.on("session/event", lambda event: seen.append(event))
        ctx.provide("headlessView", seen)

    return dict(session=session, llm=llm, llm_scripted=llm_scripted, tools=tools,
                tool_executor_native=tool_executor_native, fs_local=fs_local, fs_sandbox=fs_sandbox,
                tool_fs=tool_fs, agent_loop=agent_loop, headless=headless)


def snapshot(app):
    """Everything a plugin can have changed in the shared context."""
    root = app.root
    tools = root.services["tools"]["value"] if "tools" in root.services else None
    llm = root.services["llm"]["value"] if "llm" in root.services else None
    return {"services": sorted(root.services), "listeners": len(root.listeners), "fibers": len(root.fibers),
            "tools": sorted(tools.definitions) if tools else [], "adapters": sorted(llm.adapters) if llm else []}


def states(app):
    return {f.name: f.state for f in app.root.fibers}


def label_of(event):
    seq, kind, data = event
    if kind == "tool/call":
        return f"tool/call {data['name']}"
    if kind == "tool/result" and data.get("isError"):
        return "tool/result (error)"
    if kind == "turn/end":
        return f"turn/end {data}"
    return kind


# ---- run everything once at import, so that the figure script can read the results ----
# attempt 1
DISK1 = new_disk()
ANSWER1, TRACE1 = harness_v1(TASK, DISK1)
V1_LINES = code_lines(harness_v1)
V1_NAMES = {part: naming(harness_v1, needles) for part, needles in PARTS.items()}
# attempt 2
DISK2 = new_disk()
fs_tools_module(DISK2)
ANSWER2, TRACE2 = harness_v2(TASK)
V2_LINES = code_lines(harness_v2)
V2_NAMES = {part: naming(harness_v2, needles) for part, needles in PARTS.items()}
V2_LEFT_BEHIND = sorted(REGISTRY)            # "remove" the module: nothing knows which entries were its own

# attempt 3: boot in a deliberately wrong order, consumers before providers
DISK3 = new_disk()
P = make_plugins(DISK3)
APP = new_app()
EMPTY = snapshot(APP)
BOOT_ORDER = ["agent_loop", "tool_fs", "tool_executor_native", "llm_scripted", "headless",
              "session", "llm", "tools", "fs_local"]
FIBERS, BOOT_STATES = {}, []
MARKS = [(0, "boot, consumers first")]            # (tick, phase) for the lifetime figure
for key in BOOT_ORDER:
    APP.root.label = f"mount {P[key].name}"
    FIBERS[key] = APP.plugin(P[key])
    BOOT_STATES.append((P[key].name, dict(states(APP))))
BOOTED = snapshot(APP)

# turn 1: the same task
APP.root.label = "turn 1"
MARKS.append((APP.root.tick, "turn 1"))
ANSWER3 = APP.agentLoop(TASK, "scripted")
EVENTS3 = list(APP.sessions.events)
TRACE3 = [label_of(e) for e in EVENTS3]
V3_LINES = code_lines(P["agent_loop"])
V3_NAMES = {part: naming(P["agent_loop"], needles) for part, needles in PARTS.items()}
FRAMEWORK_LINES = code_lines(Root, Fiber, Context, plugin)
PLUGIN_LINES = sum(code_lines(p) for p in P.values())
PLUGIN_LINES_EACH = {p.name: code_lines(p) for p in P.values()}
DISK3_AFTER_TURN1 = dict(DISK3)
VIEW_EVENTS = len(APP.headlessView)

# turn 2: swap the file-system provider in the middle of the turn, after the first tool result
del DISK3["note.txt"]
SWAP = {"done": False, "used": []}


@plugin("swap-fs-mid-turn")
def swapper(ctx):
    def on_event(event):
        if event[1] == "tool/result":
            SWAP["used"].append(ctx.get("fs").label if ctx.get("fs") else None)
        if event[1] == "tool/result" and not SWAP["done"]:
            SWAP["done"] = True
            APP.root.label = "swap fs"
            FIBERS["fs_local"].dispose()                       # tool-fs loses 'fs' and unloads; its two tools go
            SWAP["tools_between"] = sorted(APP.tools.definitions)
            SWAP["state_between"] = FIBERS["tool_fs"].state
            FIBERS["fs_sandbox"] = APP.plugin(P["fs_sandbox"])  # tool-fs comes back by itself
            APP.root.label = "turn 2"
    ctx.on("session/event", on_event)


APP.root.label = "turn 2"
MARKS.append((APP.root.tick, "turn 2: fs swapped"))
FIBERS["swapper"] = APP.plugin(swapper)
N_BEFORE = len(APP.sessions.events)
ANSWER_SWAP = APP.agentLoop(TASK, "scripted")
TRACE_SWAP = [label_of(e) for e in APP.sessions.events[N_BEFORE:]]
SWAP_TIMELINE = [t for t in APP.root.timeline if t[1] in ("swap fs",) or t[2] == "fs-sandbox"]
FIBERS["swapper"].dispose()
SWAP["fs_after"] = APP.fs.label
SWAP["note"] = DISK3.get("note.txt")

# withdraw a service that the loop itself needs
APP.root.label = "unload tools"
MARKS.append((APP.root.tick, "tools disposed"))
FIBERS["tools"].dispose()
AFTER_TOOLS_GONE = dict(states(APP))
LOOP_WHILE_GONE = APP.get("agentLoop")
LOG_LEN_WHILE_GONE = len(APP.sessions.events)
APP.root.label = "mount tools"
MARKS.append((APP.root.tick, "tools mounted"))
FIBERS["tools"] = APP.plugin(P["tools"])
AFTER_TOOLS_BACK = dict(states(APP))
TOOLS_BACK = sorted(APP.tools.definitions)

# turn 3: remove only the model adapter; the loop keeps running and the turn ends with an error
APP.root.label = "unload adapter"
MARKS.append((APP.root.tick, "adapter disposed"))
FIBERS["llm_scripted"].dispose()
N_BEFORE = len(APP.sessions.events)
try:
    APP.agentLoop(TASK, "scripted")
    NO_ADAPTER = "no error"
except LookupError as error:
    NO_ADAPTER = str(error)
TRACE_NO_ADAPTER = [label_of(e) for e in APP.sessions.events[N_BEFORE:]]
LOOP_STATE_NO_ADAPTER = FIBERS["agent_loop"].state
APP.root.label = "mount adapter"
MARKS.append((APP.root.tick, "adapter mounted"))
FIBERS["llm_scripted"] = APP.plugin(P["llm_scripted"])

# the sandbox as a provider: the rogue request under each harness
ROGUE_V1_DISK = new_disk()
ROGUE_V1, _ = harness_v1(ROGUE, ROGUE_V1_DISK)
APP.root.label = "turn 4"
N_BEFORE = len(APP.sessions.events)
ROGUE_V3 = APP.agentLoop(ROGUE, "scripted")                    # ctx.fs is fs-sandbox since the swap
TRACE_ROGUE = [label_of(e) for e in APP.sessions.events[N_BEFORE:]]
ROGUE_V3_WROTE = "../outside.txt" in DISK3
TOTAL_EVENTS = len(APP.sessions.events)
ROGUE_LOCAL_DISK = new_disk()                                  # a second application, composed with fs-local
_P2, _APP2 = make_plugins(ROGUE_LOCAL_DISK), new_app()
for _key in ["session", "llm", "llm_scripted", "tools", "tool_executor_native", "fs_local", "tool_fs", "agent_loop"]:
    _APP2.plugin(_P2[_key])
ROGUE_LOCAL = _APP2.agentLoop(ROGUE, "scripted")
ROGUE_LOCAL_WROTE = "../outside.txt" in ROGUE_LOCAL_DISK

# temporal composability: dispose the root and compare the context with the one before the first plugin
APP.root.label = "dispose root"
MARKS.append((APP.root.tick, "root disposed"))
LIVE_BEFORE_DISPOSE = snapshot(APP)
APP.fiber.unload(final=True)
AFTER_DISPOSE = snapshot(APP)
TIMELINE = list(APP.root.timeline)
MOUNTS = list(APP.root.mounts)


# ---------------------------------------------------------------- 7. the request the adapter builds
def serialize_messages(messages, system, tools, effort="high", model="deepseek-flash", max_tokens=256_000):
    """A reduced copy of packages/llm/llm-deepseek/src/serialize.ts: same block names, no images, no signatures."""
    wire = []
    for m in messages:
        if m["role"] == "assistant":
            role, content = "assistant", []
            for b in m["content"]:
                if b["type"] == "reasoning":
                    content.append({"type": "thinking", "thinking": b["text"]})
                elif b["type"] == "tool-call":
                    content.append({"type": "tool_use", "id": b["id"], "name": b["name"], "input": b["arguments"]})
                else:
                    content.append({"type": "text", "text": b["text"]})
        elif m["role"] == "tool":
            role, content = "user", [{"type": "tool_result", "tool_use_id": m["toolCallId"], "content": m["content"]}]
        else:
            role, content = "user", [{"type": "text", "text": m["content"]}]
        if wire and wire[-1]["role"] == role:
            wire[-1]["content"] += content
        else:
            wire.append({"role": role, "content": content})
    body = {"model": model, "stream": True, "messages": wire, "max_tokens": max_tokens,
            "thinking": {"type": "disabled" if effort == "off" else "enabled"}}
    if effort != "off":
        body["output_config"] = {"effort": effort}
    body["system"] = system
    body["tools"] = [{"name": n, "description": d, "input_schema": s} for n, d, s in tools]
    return body


TOOL_SCHEMAS = [("read", "Read a file.", {"type": "object", "properties": {"path": {"type": "string"}}}),
                ("write", "Write a file.", {"type": "object", "properties": {"path": {"type": "string"}, "text": {"type": "string"}}})]
HISTORY3 = [d for _, k, d in EVENTS3 if k in ("user/message", "assistant/message", "tool/result")][:-1]   # what call 3 saw
BODY3 = serialize_messages(HISTORY3, "You are a careful assistant.", TOOL_SCHEMAS)
BODY3_OFF = serialize_messages(HISTORY3, "You are a careful assistant.", TOOL_SCHEMAS, effort="off")
WIRE_SHAPE = [(m["role"], [b["type"] for b in m["content"]]) for m in BODY3["messages"]]
THINKING_SENT_BACK = sum(b["type"] == "thinking" for m in BODY3["messages"] for b in m["content"])
WRITE_CALL = HISTORY3[3]["content"][1]


def dsml(call):
    """Lesson 14's format (V4 report, table 4). The harness does not contain this; the serving side writes it."""
    rows = ["<|DSML|tool_calls>", f'<|DSML|invoke name="{call["name"]}">']
    for k, v in call["arguments"].items():
        is_str = isinstance(v, str)
        rows.append(f'<|DSML|parameter name="{k}" string="{"true" if is_str else "false"}">{v if is_str else json.dumps(v)}</|DSML|parameter>')
    return "\n".join(rows + ["</|DSML|invoke>", "</|DSML|tool_calls>"])


# ---------------------------------------------------------------- 8. context management
W, O, B = 1_048_576, 256_000, 65_536           # context window, output cap, headroom: the adapter and compaction defaults
THRESHOLD_RATIO, RETAIN_RATIO = 0.8, 0.16
TRIGGER = math.floor(min(W * THRESHOLD_RATIO, W - O - B))
RETAIN = math.floor(RETAIN_RATIO * (W - O))
PRUNE = dict(threshold=8192, head=4096, tail=1024)
SPILL_TOKENS = 12_500
SYSTEM_TOKENS, SUMMARY_TOKENS = 200, 120       # chosen
L14 = [("user 1", 40), ("thinking", 300), ("tool call", 30), ("tool result", 500), ("thinking", 400),
       ("tool call", 30), ("tool result", 600), ("answer", 100), ("user 2", 20)]          # lesson 14, made up there
CUTS = [1, 4, 7, 9]                            # messages of L14 present at model calls 1 to 4
REQ = [SYSTEM_TOKENS + sum(t for _, t in L14[:c]) for c in CUTS]                           # append-only requests
OUTPUT = [330, 430, 100]                       # what the model generated at calls 1 to 3 (thinking + call, or answer)
SEEN = [0] + [r + o for r, o in zip(REQ[:-1], OUTPUT)]   # prefix the engine has already processed, at best
NEW = [r - s_ for r, s_ in zip(REQ, SEEN)]     # tokens left to prefill when the whole seen prefix is a cache hit
COMPACTED_SPAN = sum(t for _, t in L14[:4])    # user 1 .. tool result 1 are replaced by one summary
REQ4_COMPACTED = SYSTEM_TOKENS + SUMMARY_TOKENS + sum(t for _, t in L14[4:9])
SEEN4_COMPACTED = SYSTEM_TOKENS                # only the system prompt still stands at its old positions
NEW4_COMPACTED = REQ4_COMPACTED - SEEN4_COMPACTED

# ---------------------------------------------------------------- 9. counts of the repository
REPO = dict(paths=24_638, packages=382, client=74, experimental=65, base_rows=99, base_disabled=3,
            profiles=["web", "headless", "sdk", "sdk-minimal", "acp"], version="0.2.2-rc.3",
            agent_ts_lines=751, native_executor_bytes=1423, cordis_core_lines=1914)


def main():
    print("=" * 100)
    print("0. FACTS READ FROM THE REPOSITORY AND THE PAPER")
    print("=" * 100)
    for line in FACTS:
        print(line)

    print("\n" + "=" * 100)
    print("1. THE TASK AND THE SCRIPTED MODEL (a toy)")
    print("=" * 100)
    print("task:", TASK)
    print("forecast.txt:", FORECAST)
    for i, msgs in enumerate(([{"role": "user", "content": TASK}],), 1):
        print("reply to the bare task:", scripted_model({"messages": msgs}))

    print("\n" + "=" * 100)
    print("2. ATTEMPT 1: ONE LOOP, TWO HARD-CODED TOOLS")
    print("=" * 100)
    print("lines of code in harness_v1:", V1_LINES)
    print("answer:", ANSWER1)
    print("disk after:", DISK1)
    print("trace:", " | ".join(TRACE1))
    print("model calls:", TRACE1.count("assistant/message"), " tool calls:", sum(t.startswith("tool/call") for t in TRACE1))
    print("lines of the loop that name a concrete part:", V1_NAMES)

    print("\n" + "=" * 100)
    print("3. ATTEMPT 2: A TOOL REGISTRY")
    print("=" * 100)
    print("lines of code in harness_v2:", V2_LINES)
    print("answer equal to attempt 1:", ANSWER2 == ANSWER1, "| trace equal:", TRACE2 == TRACE1, "| disk equal:", DISK2 == DISK1)
    print("lines of the loop that name a concrete part:", V2_NAMES)
    print("after the module that registered the file tools is 'removed', the registry still holds:", V2_LEFT_BEHIND)

    print("\n" + "=" * 100)
    print("4. ATTEMPT 3: EVERY PART IS A PLUGIN")
    print("=" * 100)
    print("toy Cordis (Root, Fiber, Context, plugin):", FRAMEWORK_LINES, "lines of code")
    print("ten plugins:", PLUGIN_LINES, "lines of code; the agent-loop plugin alone:", V3_LINES)
    print("  each:", PLUGIN_LINES_EACH)
    print("all three:", V1_LINES, "->", V2_LINES + code_lines(register, fs_tools_module), "->", FRAMEWORK_LINES + PLUGIN_LINES, "lines (loop, loop + registry + tool module, toy Cordis + plugins)")
    print("boot order (consumers first):", [P[k].name for k in BOOT_ORDER])
    for name, st in BOOT_STATES:
        print(f"  after mounting {name:22s}", {k: v for k, v in st.items()})
    print("context before any plugin:", EMPTY)
    print("context after boot:       ", BOOTED)
    print("answer:", ANSWER3)
    print("answer equal to attempt 1:", ANSWER3 == ANSWER1, "| disk equal:", DISK3_AFTER_TURN1 == DISK1)
    print("session events of turn 1:", len(EVENTS3))
    for seq, kind, data in EVENTS3:
        print(f"  {seq:2d}  {label_of((seq, kind, data))}")
    print("events seen by the headless plugin:", VIEW_EVENTS)
    print("lines of the agent-loop plugin that name a concrete part:", V3_NAMES)

    print("\n" + "=" * 100)
    print("5. LOADING AND UNLOADING WHILE THE HARNESS RUNS")
    print("=" * 100)
    print("turn 2, fs-local disposed and fs-sandbox mounted after the first tool result")
    print("  tools registered between the two:", SWAP["tools_between"], "| tool-fs state:", SWAP["state_between"])
    print("  provider seen at each tool result:", SWAP["used"])
    print("  trace:", " | ".join(TRACE_SWAP))
    print("  answer equal to turn 1:", ANSWER_SWAP == ANSWER3, "| note.txt:", SWAP["note"], "| ctx.fs after:", SWAP["fs_after"])
    for t in [t for t in TIMELINE if t[1] == "swap fs"]:
        print(f"  tick {t[0]:3d}  {t[2]:22s} {t[3]:9s} -> {t[4]}")
    print("the tools service disposed:")
    print("  states:", AFTER_TOOLS_GONE)
    print("  ctx.get('agentLoop'):", LOOP_WHILE_GONE, "| events still in the session log:", LOG_LEN_WHILE_GONE)
    for t in [t for t in TIMELINE if t[1] == "unload tools"]:
        print(f"  tick {t[0]:3d}  {t[2]:22s} {t[3]:9s} -> {t[4]}")
    print("the tools service mounted again:")
    print("  states:", AFTER_TOOLS_BACK)
    print("  tools registered again:", TOOLS_BACK)
    print("the adapter plugin disposed, then a message sent:")
    print("  error:", NO_ADAPTER, "| agent-loop state:", LOOP_STATE_NO_ADAPTER)
    print("  trace:", " | ".join(TRACE_NO_ADAPTER))
    print("root disposed:")
    print("  context just before:", LIVE_BEFORE_DISPOSE)
    print("  context after:      ", AFTER_DISPOSE)
    print("  equal to the context before the first plugin:", AFTER_DISPOSE == EMPTY)
    print("  state changes recorded in all:", len(TIMELINE))
    print("  phases (tick at which each begins):", MARKS)

    print("\n" + "=" * 100)
    print("6. THE SANDBOX AS A PROVIDER")
    print("=" * 100)
    print("request:", ROGUE)
    print("attempt 1 :", ROGUE_V1, "| wrote ../outside.txt:", "../outside.txt" in ROGUE_V1_DISK)
    print("plugins, fs-local  :", ROGUE_LOCAL, "| wrote ../outside.txt:", ROGUE_LOCAL_WROTE)
    print("plugins, fs-sandbox:", ROGUE_V3, "| wrote ../outside.txt:", ROGUE_V3_WROTE)
    print("  trace:", " | ".join(TRACE_ROGUE))
    print("session events over the four turns and the failed one:", TOTAL_EVENTS)

    print("\n" + "=" * 100)
    print("7. THE REQUEST THE ADAPTER BUILDS FOR MODEL CALL 3")
    print("=" * 100)
    for role, kinds in WIRE_SHAPE:
        print(f"  {role:10s} {kinds}")
    print("top-level fields:", [k for k in BODY3 if k != "messages"])
    print("thinking:", BODY3["thinking"], "| output_config:", BODY3.get("output_config"), "| max_tokens:", BODY3["max_tokens"])
    print("effort off -> thinking:", BODY3_OFF["thinking"], "| output_config present:", "output_config" in BODY3_OFF)
    print("thinking blocks sent back:", THINKING_SENT_BACK, "| wire messages:", len(BODY3["messages"]))
    print("the write call as the harness sends it:", json.dumps({"type": "tool_use", "id": WRITE_CALL["id"], "name": WRITE_CALL["name"], "input": WRITE_CALL["arguments"]}))
    print("the same call in lesson 14's DSML (not in the harness):")
    print(dsml(WRITE_CALL))

    print("\n" + "=" * 100)
    print("8. CONTEXT MANAGEMENT")
    print("=" * 100)
    print(f"W = {W:,}  O = {O:,}  B = {B:,}")
    print(f"W x 0.8 = {W * THRESHOLD_RATIO:,.1f}   W - O - B = {W - O - B:,}   trigger = {TRIGGER:,}  ({TRIGGER / W:.4f} of W)")
    print(f"kept verbatim: 0.16 x (W - O) = 0.16 x {W - O:,} = {RETAIN:,}")
    print("tool-result pruner:", PRUNE, "-> a result of 20,000 characters keeps", PRUNE["head"] + PRUNE["tail"])
    print("spill above", f"{SPILL_TOKENS:,}", "tokens")
    print("lesson 14's trajectory behind a system prompt of", SYSTEM_TOKENS, "tokens")
    for i, (r, s, n) in enumerate(zip(REQ, SEEN, NEW), 1):
        print(f"  call {i}: request {r:,} tokens, prefix already processed {s:,}, new {n:,}")
    print(f"  call 4 after one compaction: span of {COMPACTED_SPAN} tokens -> summary of {SUMMARY_TOKENS}; request {REQ4_COMPACTED:,}, unchanged prefix {SEEN4_COMPACTED}, new {NEW4_COMPACTED:,}")
    print(f"  new tokens at call 4, compacted against append-only: {NEW4_COMPACTED:,} / {NEW[3]} = {NEW4_COMPACTED / NEW[3]:.1f}")

    print("\n" + "=" * 100)
    print("9. COUNTS OF THE REPOSITORY (git tree of master, 14 Nov 2026)")
    print("=" * 100)
    for k, v in REPO.items():
        print(f"  {k}: {v}")
    print("  packages outside client and experimental:", REPO["packages"] - REPO["client"] - REPO["experimental"])


if __name__ == "__main__":
    main()
