2026-11-11 08:20:16 +00:00
"""Pin scripts/install.sh: the installed skill directories keep their inode, so a bind mount of them stays current."""
from __future__ import annotations
2026-11-14 21:17:28 +00:00
import json
2026-11-11 08:20:16 +00:00
import os
import shutil
import subprocess
import tempfile
import unittest
from pathlib import Path
2026-11-14 21:25:12 +00:00
REPO_ROOT = Path ( __file__ ) . resolve () . parents [ 1 ]
INSTALL = REPO_ROOT / "scripts" / "install.sh"
2026-11-14 21:56:06 +00:00
ORGANIZE = REPO_ROOT / "skills" / "kvb-org-organize"
2026-11-14 21:25:12 +00:00
UNIT = REPO_ROOT / "skills" / "kvb-agent-optimize" / "systemd" / "kvb-code-graph.service"
2026-11-11 08:20:16 +00:00
NPX = "#!/bin/sh \n mkdir -p .agents/skills/locked \n echo locked > .agents/skills/locked/SKILL.md \n "
2026-11-13 11:18:31 +00:00
BYTECODE = ( "__pycache__/radio.cpython-312.pyc" , "json.pyc" , "legacy.pyo" )
2026-11-14 21:17:28 +00:00
LIVE_MEMORY = {
"warn" : { "available" : 15 , "swap_free" : 40 , "psi_full" : 5 , "container_swap_gb" : 4 },
"stop" : { "available" : 8 , "swap_free" : 10 , "psi_full" : 10 , "container_swap_gb" : 16 },
}
SHIPPED = ORGANIZE / "config.default.json"
DEFAULT = json . loads ( SHIPPED . read_text ( encoding = "utf-8" ))
ROW_MEMORY = DEFAULT [ "memory_limits" ]
2026-11-15 07:07:52 +00:00
HOME_TOOLS = ( "bash" , "cp" , "dirname" , "find" , "install" , "ln" , "mkdir" , "python3" , "rm" )
2026-11-17 18:48:25 +00:00
COUNCIL_PARTS = (
"scripts/orchestrator.sh" , "scripts/tokeneconomizer.sh" , "plugins-lock.json" ,
"skills/kvb-org-organize/scripts/councilroot.py" , "skills/kvb-org-organize/scripts/nofollow.py" ,
2026-11-26 08:44:15 +00:00
"skills/kvb-org-organize/scripts/schedules.py" ,
2026-11-17 18:48:25 +00:00
)
2026-11-27 10:44:15 +00:00
RUNNING_JOB = "0123456789abcdef"
2026-11-15 07:07:52 +00:00
AGENT_ROOTS = { "sandbox" : { "filesystem" : { "allowWrite" : [ "~/.local/state/agents-shared" , "~/.local/state/agents-release/**" ]}}}
2026-11-14 21:25:12 +00:00
CGROUP_KILL = """
case "$*" in *try-restart*kvb-code-graph*)
unit=$(while IFS= read -r line; do echo "$line"; done < "$HOME/.config/systemd/user/kvb-code-graph.service")
case "$unit" in *KillMode=process*) ;; *) echo "cgroup killed the daemon" >> " {daemon} " ;; esac
esac"""
HOME_UNITS = (
"kvb-dream/systemd/kvb-dream-notify.service" ,
"kvb-dream/systemd/kvb-dream-notify.timer" ,
"kvb-org-queue-cleanup/systemd/kvb-org-queue-cleanup.service" ,
"kvb-org-queue-cleanup/systemd/kvb-org-queue-cleanup.timer" ,
"kvb-org-queue-cleanup/systemd/kvb-org-queue-cleanup.path" ,
2026-11-26 08:44:15 +00:00
"kvb-org-organize/systemd/kvb-schedules.service" ,
"kvb-org-organize/systemd/kvb-schedules.timer" ,
"kvb-org-organize/systemd/kvb-schedules.path" ,
2026-11-14 21:25:12 +00:00
)
def read_lines ( path : Path ) -> list [ str ]:
return path . read_text ( encoding = "utf-8" ) . splitlines () if path . is_file () else []
2026-11-11 08:20:16 +00:00
class TestInstall ( unittest . TestCase ):
2026-11-14 21:17:28 +00:00
def install ( self , root : Path , target : Path , npx : str = NPX , planted : tuple [ str , ... ] = (), config : Path | None = None ,
code : int = 0 , stubs_extra : tuple [ tuple [ str , str ], ... ] = ()) -> subprocess . CompletedProcess :
2026-11-11 20:33:54 +00:00
repo , stubs = root / "repo" , root / "bin"
( repo / "scripts" ) . mkdir ( parents = True )
shutil . copy ( INSTALL , repo / "scripts" / "install.sh" )
2026-11-14 21:17:28 +00:00
if config is not None :
shutil . copytree ( ORGANIZE , repo / "skills" / ORGANIZE . name , ignore = shutil . ignore_patterns ( "__pycache__" ))
2026-11-11 20:33:54 +00:00
( repo / "scripts" / "enable-plugins.js" ) . write_text ( "" , encoding = "utf-8" )
( repo / "skills" / "own" ) . mkdir ( parents = True )
( repo / "skills" / "own" / "SKILL.md" ) . write_text ( "own" , encoding = "utf-8" )
2026-11-13 11:18:31 +00:00
for name in planted :
( repo / name ) . parent . mkdir ( parents = True , exist_ok = True )
( repo / name ) . write_bytes ( b "planted" )
2026-11-11 20:33:54 +00:00
stubs . mkdir ()
2026-11-14 21:17:28 +00:00
for name , text in (( "npx" , npx ), * stubs_extra ):
( stubs / name ) . write_text ( text , encoding = "utf-8" )
( stubs / name ) . chmod ( 0o755 )
2026-11-11 20:33:54 +00:00
env = { ** os . environ , "PATH" : f " { stubs } :/usr/bin:/bin" , "TARGET" : str ( target ), "HOME" : str ( root / "home" )}
2026-11-14 21:17:28 +00:00
if config is not None :
env |= { "AGENTS_COUNCIL_CONFIG" : str ( config ), "AGENTS_COUNCIL_ROOT" : str ( root / "council" )}
2026-11-11 20:33:54 +00:00
done = subprocess . run ([ "bash" , str ( repo / "scripts" / "install.sh" )], env = env , capture_output = True , text = True , check = False )
2026-11-14 21:17:28 +00:00
self . assertEqual ( done . returncode , code , done . stderr )
return done
def test_the_install_stops_before_any_copy_while_the_live_config_lacks_a_memory_bound_the_new_code_needs ( self ):
for limits , code in (( LIVE_MEMORY , 2 ), ( ROW_MEMORY , 0 )):
with self . subTest ( code = code ), tempfile . TemporaryDirectory () as tmp :
root = Path ( tmp )
target = root / "target"
target . mkdir ()
config = root / "config.json"
config . write_text ( json . dumps ({ ** DEFAULT , "memory_limits" : limits }), encoding = "utf-8" )
before = config . stat ()
done = self . install ( root , target , config = config , code = code )
after = config . stat ()
self . assertEqual (( after . st_ino , after . st_mtime_ns , after . st_mode ), ( before . st_ino , before . st_mtime_ns , before . st_mode ))
installed = sorted ( path . name for path in target . iterdir ())
if code :
self . assertIn (
"lacks memory_limits.warn.psi_some, memory_limits.warn.swap_out_mb_min, memory_limits.stop.swap_out_mb_min;" ,
done . stderr ,
)
self . assertEqual ( installed , [])
self . assertFalse (( root / "repo" / ".agents" ) . exists ())
else :
self . assertEqual ( installed , [ ".agents" , ".claude" ])
self . assertEqual ( json . loads ( config . read_text ( encoding = "utf-8" )), { ** DEFAULT , "memory_limits" : limits })
def test_an_install_without_a_config_writes_the_shipped_default_as_the_initial_one ( self ):
with tempfile . TemporaryDirectory () as tmp :
root = Path ( tmp )
target = root / "target"
target . mkdir ()
config = root / "council-config" / "config.json"
self . install ( root , target , config = config )
self . assertEqual ( config . read_bytes (), SHIPPED . read_bytes ())
self . assertEqual ( config . stat () . st_mode & 0o777 , 0o600 )
self . assertEqual ( config . parent . stat () . st_mode & 0o777 , 0o700 )
def test_a_config_written_while_the_install_creates_its_folder_survives ( self ):
with tempfile . TemporaryDirectory () as tmp :
root = Path ( tmp )
target = root / "target"
target . mkdir ()
config = root / "council-config" / "config.json"
racer = f "#!/bin/sh \n mkdir -p { config . parent } \n echo ' {{\" mine \" : true }} ' > { config } \n exec /usr/bin/install \" $@ \"\n "
done = self . install ( root , target , config = config , stubs_extra = (( "install" , racer ),))
self . assertEqual ( json . loads ( config . read_text ( encoding = "utf-8" )), { "mine" : True })
self . assertIn ( str ( config ), done . stderr )
2026-11-11 20:33:54 +00:00
2026-11-27 10:44:15 +00:00
def home_install ( self , root : Path , binary : bool , config : bool = True , fail_now : bool = False , schedules : dict | None = None ,
runner_job : dict | None = None ) -> tuple [ subprocess . CompletedProcess , list [ str ], list [ str ]]:
2026-11-14 21:25:12 +00:00
repo , stubs , home = root / "repo" , root / "bin" , root / "home"
( repo / "scripts" ) . mkdir ( parents = True )
shutil . copy ( INSTALL , repo / "scripts" / "install.sh" )
2026-11-15 07:07:52 +00:00
for part in COUNCIL_PARTS :
( repo / part ) . parent . mkdir ( parents = True , exist_ok = True )
shutil . copy ( REPO_ROOT / part , repo / part )
2026-11-14 21:25:12 +00:00
for unit in HOME_UNITS :
( repo / "skills" / unit ) . parent . mkdir ( parents = True , exist_ok = True )
( repo / "skills" / unit ) . write_text ( "[Unit] \n " , encoding = "utf-8" )
( repo / "skills" / UNIT . relative_to ( REPO_ROOT / "skills" )) . parent . mkdir ( parents = True )
shutil . copy ( UNIT , repo / "skills" / UNIT . relative_to ( REPO_ROOT / "skills" ))
stubs . mkdir ()
for tool in HOME_TOOLS :
( stubs / tool ) . symlink_to ( shutil . which ( tool ))
log , cbm = root / "systemctl.log" , root / "cbm.log"
now = ' \n case "$*" in *--now*kvb-code-graph*) exit 1 ;; esac' if fail_now else ""
for name , body in (
( "npx" , NPX ),
( "systemctl" , f '#!/bin/sh \n echo "$*" >> " { log } " { now }{ CGROUP_KILL . format ( daemon = root / "daemon.log" ) } \n exit 0 \n ' ),
( "curl" , "#!/bin/sh \n exit 22 \n " ),
( "codebase-memory-mcp" , f '#!/bin/sh \n echo "$*" >> " { cbm } " \n exit 0 \n ' ),
):
( stubs / name ) . write_text ( body , encoding = "utf-8" )
( stubs / name ) . chmod ( 0o755 )
home . mkdir ()
2026-11-15 07:07:52 +00:00
( home / ".claude" ) . mkdir ()
( home / ".claude" / "settings.json" ) . write_text ( json . dumps ( AGENT_ROOTS ), encoding = "utf-8" )
2026-11-14 21:25:12 +00:00
if binary :
( home / ".local" / "bin" ) . mkdir ( parents = True )
shutil . copy ( stubs / "codebase-memory-mcp" , home / ".local" / "bin" / "codebase-memory-mcp" )
if config :
( home / ".config" / "kvb-code-graph" ) . mkdir ( parents = True )
( home / ".config" / "kvb-code-graph" / "config.json" ) . write_text (
'{"queue_file_threshold": 2000, "queue_task": "work/code-graph-index"} \n ' , encoding = "utf-8"
)
2026-11-26 08:44:15 +00:00
if schedules is not None :
council = home / ".local" / "state" / "agents-shared"
council . mkdir ( parents = True , mode = 0o700 )
( council / "schedules.json" ) . write_text ( json . dumps ( schedules ), encoding = "utf-8" )
2026-11-27 10:44:15 +00:00
if runner_job is not None :
hub_state = home / ".local" / "state" / "kvb-orchestrator"
hub_state . mkdir ( parents = True )
( hub_state / "runner-job.json" ) . write_text ( json . dumps ( runner_job ), encoding = "utf-8" )
2026-11-15 07:07:52 +00:00
env = { "PATH" : str ( stubs ), "TARGET" : str ( home ), "HOME" : str ( home ), "AGENTS_COUNCIL_LEGACY" : str ( root / "legacy" )}
2026-11-14 21:25:12 +00:00
done = subprocess . run ([ "/bin/bash" , str ( repo / "scripts" / "install.sh" )], env = env , capture_output = True , text = True , check = False )
return done , read_lines ( log ), read_lines ( cbm )
def test_the_home_install_enables_the_code_graph_service ( self ):
with tempfile . TemporaryDirectory () as tmp :
root = Path ( tmp )
done , calls , _ = self . home_install ( root , binary = True )
self . assertEqual ( done . returncode , 0 , done . stderr )
self . assertIn ( "--user enable --now kvb-code-graph.service" , calls )
installed = root / "home" / ".config" / "systemd" / "user" / "kvb-code-graph.service"
self . assertEqual ( installed . read_text ( encoding = "utf-8" ), UNIT . read_text ( encoding = "utf-8" ))
self . assertNotIn ( "kvb-code-graph: no config" , done . stderr )
2026-11-26 08:44:15 +00:00
def test_the_home_install_starts_the_schedules_reconciler_and_leaves_the_dream_timer_to_it ( self ):
with tempfile . TemporaryDirectory () as tmp :
root = Path ( tmp )
done , calls , _ = self . home_install ( root , binary = True )
self . assertEqual ( done . returncode , 0 , done . stderr )
self . assertIn ( "--user enable --now kvb-org-queue-cleanup.timer kvb-org-queue-cleanup.path kvb-schedules.path kvb-schedules.timer" , calls )
self . assertIn ( "--user start kvb-schedules.service" , calls )
self . assertFalse ([ call for call in calls if "enable" in call and "kvb-dream-notify" in call ])
units = root / "home" / ".config" / "systemd" / "user"
for name in ( "kvb-schedules.service" , "kvb-schedules.timer" , "kvb-schedules.path" , "kvb-dream-notify.timer" ):
self . assertTrue (( units / name ) . is_file (), name )
def test_a_code_graph_row_switched_off_keeps_the_service_off_on_a_reinstall ( self ):
with tempfile . TemporaryDirectory () as tmp :
done , calls , _ = self . home_install ( Path ( tmp ), binary = True , schedules = { "code-graph" : { "on" : False }})
self . assertEqual ( done . returncode , 0 , done . stderr )
self . assertFalse ([ call for call in calls if "kvb-code-graph" in call ])
self . assertIn ( "kvb-code-graph.service stays off" , done . stdout + done . stderr )
2026-11-14 21:25:12 +00:00
def test_the_home_install_turns_the_daemon_auto_index_off ( self ):
with tempfile . TemporaryDirectory () as tmp :
done , _ , cbm = self . home_install ( Path ( tmp ), binary = True )
self . assertEqual ( done . returncode , 0 , done . stderr )
self . assertIn ( "config set auto_index false" , cbm )
self . assertNotIn ( "config set auto_index true" , cbm )
def test_the_home_install_without_config_says_so_once ( self ):
with tempfile . TemporaryDirectory () as tmp :
done , _ , _ = self . home_install ( Path ( tmp ), binary = True , config = False )
self . assertEqual ( done . returncode , 0 , done . stderr )
notes = [ line for line in done . stderr . splitlines () if line . startswith ( "skills: kvb-code-graph: no config" )]
self . assertEqual ( len ( notes ), 1 , done . stderr )
self . assertIn ( "queue_file_threshold" , notes [ 0 ])
self . assertIn ( "queue_task" , notes [ 0 ])
def test_a_reinstall_restarts_a_running_supervisor_on_the_new_unit ( self ):
with tempfile . TemporaryDirectory () as tmp :
done , calls , _ = self . home_install ( Path ( tmp ), binary = True )
self . assertEqual ( done . returncode , 0 , done . stderr )
reload = max ( number for number , call in enumerate ( calls ) if call == "--user daemon-reload" )
self . assertIn ( "--user try-restart kvb-code-graph.service" , calls [ reload :])
def test_a_restart_of_the_supervisor_leaves_its_daemon_running ( self ):
with tempfile . TemporaryDirectory () as tmp :
done , _ , _ = self . home_install ( Path ( tmp ), binary = True )
self . assertEqual ( done . returncode , 0 , done . stderr )
self . assertEqual ( read_lines ( Path ( tmp ) / "daemon.log" ), [])
self . assertIn ( "KillMode=process" , UNIT . read_text ( encoding = "utf-8" ) . splitlines ())
def test_a_unit_that_cannot_start_now_takes_effect_at_next_login ( self ):
with tempfile . TemporaryDirectory () as tmp :
done , calls , _ = self . home_install ( Path ( tmp ), binary = True , fail_now = True )
self . assertEqual ( done . returncode , 0 , done . stderr )
self . assertIn ( "--user enable kvb-code-graph.service" , calls )
self . assertIn ( "takes effect at next login" , done . stdout + done . stderr )
def test_the_home_install_skips_the_service_without_the_binary ( self ):
with tempfile . TemporaryDirectory () as tmp :
done , calls , _ = self . home_install ( Path ( tmp ), binary = False )
self . assertEqual ( done . returncode , 0 , done . stderr )
self . assertFalse ([ call for call in calls if "kvb-code-graph" in call ])
self . assertIn ( "graphs keep their watch only while a session runs" , done . stderr )
2026-11-17 18:48:25 +00:00
def test_the_home_install_runs_the_tokeneconomizer_step_and_a_host_without_git_skips_it ( self ):
with tempfile . TemporaryDirectory () as tmp :
done , _ , _ = self . home_install ( Path ( tmp ), binary = True )
self . assertEqual ( done . returncode , 0 , done . stderr )
self . assertIn ( "skills: git or python3 not found; skipped TokenEconomizer." , done . stderr )
self . assertFalse (( Path ( tmp ) / "home" / "Repositories" ) . exists ())
2026-11-27 10:44:15 +00:00
def test_a_hub_row_the_runner_runs_stops_the_home_install_before_it_changes_anything ( self ):
with tempfile . TemporaryDirectory () as tmp :
root = Path ( tmp )
done , calls , _ = self . home_install ( root , binary = True , runner_job = { "pid" : os . getpid (), "job" : RUNNING_JOB , "task" : "operator/push" , "started" : 0 })
self . assertEqual ( done . returncode , 1 , done . stderr )
self . assertIn ( f "a hub row runs (operator/push, job { RUNNING_JOB } ): the install would stop the runner and kill it; run the install in a terminal once no row runs" , done . stderr )
self . assertEqual ( calls , [])
self . assertFalse (( root / "repo" / ".agents" ) . exists () or ( root / "home" / ".agents" ) . exists () or ( root / "home" / ".claude" / "skills" ) . exists ())
def test_a_marker_whose_runner_is_gone_lets_the_home_install_pass ( self ):
gone = subprocess . Popen ([ "true" ])
gone . wait ()
with tempfile . TemporaryDirectory () as tmp :
root = Path ( tmp )
done , _ , _ = self . home_install ( root , binary = True , runner_job = { "pid" : gone . pid , "job" : RUNNING_JOB , "task" : "operator/push" , "started" : 0 })
self . assertEqual ( done . returncode , 0 , done . stderr )
self . assertNotIn ( "a hub row runs" , done . stderr )
self . assertTrue (( root / "home" / ".claude" / "skills" / "locked" / "SKILL.md" ) . is_file ())
2026-11-11 08:20:16 +00:00
def test_reinstall_empties_the_skill_directories_in_place ( self ):
with tempfile . TemporaryDirectory () as tmp :
root = Path ( tmp )
2026-11-11 20:33:54 +00:00
target = root / "target"
2026-11-11 08:20:16 +00:00
installed = [ target / ".agents" / "skills" , target / ".claude" / "skills" ]
for folder in installed :
( folder / "stale" ) . mkdir ( parents = True )
before = [ folder . stat () . st_ino for folder in installed ]
2026-11-11 20:33:54 +00:00
self . install ( root , target )
2026-11-11 08:20:16 +00:00
self . assertEqual ([ folder . stat () . st_ino for folder in installed ], before )
for folder in installed :
self . assertEqual ( sorted ( path . name for path in folder . iterdir ()), [ "locked" , "own" ])
2026-11-11 20:33:54 +00:00
def test_a_project_install_links_every_skill_the_global_install_holds ( self ):
with tempfile . TemporaryDirectory () as tmp :
root = Path ( tmp )
target = root / "target"
target . mkdir ()
linked = ( ".agents/skills/locked" , ".claude/skills/own" )
for folder in linked :
( root / "home" / folder ) . mkdir ( parents = True )
self . install ( root , target )
for folder in ( ".agents/skills" , ".claude/skills" ):
installed = { path . name : path . readlink () if path . is_symlink () else None for path in ( target / folder ) . iterdir ()}
expected = { name : root / "home" / folder / name if f " { folder } / { name } " in linked else None for name in ( "locked" , "own" )}
self . assertEqual ( installed , expected )
2026-11-13 11:18:31 +00:00
def test_no_python_bytecode_reaches_the_installed_skills ( self ):
locked = "" . join ( f "echo planted > .agents/skills/locked/scripts/ { name } \n " for name in BYTECODE )
npx = f " { NPX } mkdir -p .agents/skills/locked/scripts/__pycache__ \n { locked } "
for linked in ( False , True ):
with self . subTest ( linked = linked ), tempfile . TemporaryDirectory () as tmp :
root = Path ( tmp )
target = root / "target"
( target / ".agents" / "skills" ) . mkdir ( parents = True )
if linked :
( target / ".claude" ) . mkdir ()
( target / ".claude" / "skills" ) . symlink_to ( Path ( ".." ) / ".agents" / "skills" )
self . install ( root , target , npx , ( * ( f "skills/own/scripts/ { name } " for name in BYTECODE ), "skills/own/scripts/radio.py" ))
for folder in ( ".agents/skills" , ".claude/skills" ):
installed = target / folder
with self . subTest ( folder = folder ):
bytecode = [ str ( path . relative_to ( installed )) for path in sorted ( installed . rglob ( "*" )) if path . name == "__pycache__" or path . suffix in ( ".pyc" , ".pyo" )]
self . assertEqual ( bytecode , [])
self . assertTrue (( installed / "own" / "scripts" / "radio.py" ) . is_file ())
self . assertTrue (( installed / "locked" / "SKILL.md" ) . is_file ())
2026-11-11 08:20:16 +00:00
2026-11-26 08:44:15 +00:00
class TestCoachLock ( unittest . TestCase ):
COACH = ( "kvb-coach" , "kvb-coach-meditation" , "kvb-coach-off" , "kvb-coach-on" )
def test_the_lock_pins_the_four_coach_skills_at_one_commit_of_the_coach_repository ( self ):
lock = json . loads (( REPO_ROOT / "skills-lock.json" ) . read_text ( encoding = "utf-8" ))[ "skills" ]
coach = { name : entry for name , entry in lock . items () if entry [ "source" ] == "kevinveenbirkenbach/coach" }
self . assertEqual ( sorted ( coach ), list ( self . COACH ))
self . assertEqual ( len ({ entry [ "commit" ] for entry in coach . values ()}), 1 )
self . assertEqual ({ name : ( entry [ "sourceType" ], entry [ "skillPath" ]) for name , entry in coach . items ()}, { name : ( "github" , f "skills/ { name } /SKILL.md" ) for name in self . COACH })
self . assertFalse ([ name for name in self . COACH if ( REPO_ROOT / "skills" / name ) . exists ()])
def test_the_installer_registers_the_coach_reminder_it_does_not_ship ( self ):
text = ( REPO_ROOT / "scripts" / "enable-plugins.js" ) . read_text ( encoding = "utf-8" )
self . assertIn ( 'const REMINDERS = ["kvb-dream", "kvb-coach"];' , text )
2026-11-11 08:20:16 +00:00
if __name__ == "__main__" :
unittest . main ()