59 lines
2.5 KiB
Python
59 lines
2.5 KiB
Python
"""The resume's compaction step (``claude_launch.valid_compact`` / ``_compact_args`` /
|
|
``_chain``): the reply box's *fresh context* runs ``/compact`` on a cheap model,
|
|
then the reply, as one detached run."""
|
|
|
|
import shlex
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
|
|
import launch
|
|
|
|
import claude_launch
|
|
|
|
|
|
def test_valid_compact_defaults_and_squashes():
|
|
assert claude_launch.valid_compact(None) is None
|
|
assert claude_launch.valid_compact({}) is None
|
|
c = claude_launch.valid_compact({"instructions": "keep\n every prompt"})
|
|
assert c == {"model": "haiku", "instructions": "keep every prompt"}
|
|
long = claude_launch.valid_compact({"model": "claude-haiku-5-5", "instructions": "x" * 9000})
|
|
assert long["model"] == "claude-haiku-5-5"
|
|
assert len(long["instructions"]) == claude_launch.COMPACT_INSTRUCTIONS_MAX
|
|
|
|
|
|
def test_valid_compact_rejects_a_bad_model():
|
|
with pytest.raises(HTTPException):
|
|
claude_launch.valid_compact({"model": "haiku; rm -rf /", "instructions": "x"})
|
|
|
|
|
|
def test_compact_args():
|
|
args = claude_launch._compact_args("sid-1", {"model": "haiku", "instructions": "keep it"})
|
|
assert args[1:5] == ["-p", "/compact keep it", "--resume", "sid-1"]
|
|
assert args[-2:] == ["--model", "haiku"]
|
|
assert "--settings" in args # same hooks as any run
|
|
|
|
|
|
def test_compact_args_keep_a_guest_scope():
|
|
"""A guest's fresh-context reply is scoped; its compaction must be too —
|
|
not the lab's default permission mode with no permission rules."""
|
|
scope = {"writeDirs": ["/tmp/proj"], "denyRead": ["~/.ssh/**"]}
|
|
args = claude_launch._compact_args("sid-1", {"model": "haiku", "instructions": "k"}, scope)
|
|
assert args[args.index("--permission-mode") + 1] == "dontAsk"
|
|
settings = args[args.index("--settings") + 1]
|
|
assert "Edit(//tmp/proj/**)" in settings and ".ssh" in settings
|
|
assert "--permission-prompts" in args
|
|
# Unscoped (the owner's) stays as it was.
|
|
own = claude_launch._compact_args("sid-1", {"model": "haiku", "instructions": "k"})
|
|
assert own[own.index("--permission-mode") + 1] == claude_launch.PERMISSION_MODE
|
|
|
|
|
|
def test_chain_runs_both_and_execs_the_reply():
|
|
first = ["claude", "-p", "/compact it's \"quoted\"", "--resume", "s"]
|
|
then = ["claude", "-p", "the reply; with $vars", "--resume", "s"]
|
|
sh, flag, script = claude_launch._chain(first, then)
|
|
assert (sh, flag) == ("/bin/sh", "-c")
|
|
head, tail = script.split("; exec ", 1)
|
|
assert shlex.split(head) == first
|
|
assert shlex.split(tail) == then
|