diff --git a/.claude/agents/environment-agent.md b/.claude/agents/environment-agent.md new file mode 100644 index 0000000..2f1463e --- /dev/null +++ b/.claude/agents/environment-agent.md @@ -0,0 +1,26 @@ +--- +name: environment-agent +description: Analyze the vacuum robot grid and valid actions +tools: Read, Grep, Glob +model: sonnet +skills: + - grid-validation +--- + +Analyze the following 4x4 vacuum robot grid: + +S 0 0 0 +0 X 0 0 +0 0 0 0 +0 -1 0 10 + +Tasks: +1. Identify all valid states. +2. Identify possible robot actions. +3. Determine obstacle and boundary constraints. +4. Propose a state representation. +5. Report any missing requirements. + +Work independently. +Do not implement Q-learning. +Do not modify repository files. diff --git a/.claude/agents/reward-agent.md b/.claude/agents/reward-agent.md new file mode 100644 index 0000000..7adfc37 --- /dev/null +++ b/.claude/agents/reward-agent.md @@ -0,0 +1,26 @@ +--- +name: reward-agent +description: Investigate unknown reward behavior +tools: Read, Grep, Glob +model: sonnet +--- + +Investigate the vacuum robot reward function. + +Grid: +S 0 0 0 +0 X 0 0 +0 0 0 0 +0 -1 0 10 + +Tasks: +1. Identify known reward values. +2. Determine what reward information is missing. +3. Explain how rewards could be observed + through interaction with the environment. +4. Identify possible terminal conditions. +5. Report assumptions separately from facts. + +Work independently. +Do not implement Q-learning. +Do not modify repository files. \ No newline at end of file diff --git a/.claude/agents/transition-agent.md b/.claude/agents/transition-agent.md new file mode 100644 index 0000000..b898e0f --- /dev/null +++ b/.claude/agents/transition-agent.md @@ -0,0 +1,25 @@ +--- +name: transition-agent +description: Investigates transition and reward behavior +tools: Read, Grep, Glob, Bash +model: sonnet +skills: + - transition-testing + - reward-design +--- + +You are investigating a reinforcement +learning vacuum robot environment. + +Read context/vacuum-environment.md. + +Your responsibilities: +1. Identify known transition rules. +2. Identify known reward values. +3. Examine relevant repository code. +4. Report missing T and R definitions. +5. Suggest questions for the instructor. + +Do not edit files. +Do not implement Q-learning. +Do not invent missing requirements. diff --git a/.claude/agents/validation-agent.md b/.claude/agents/validation-agent.md new file mode 100644 index 0000000..fd7fc7e --- /dev/null +++ b/.claude/agents/validation-agent.md @@ -0,0 +1,41 @@ +--- +name: validation-agent +description: Validate the vacuum robot environment implementation against the known requirements +tools: Read, Grep, Glob, Bash +model: sonnet +--- + +You are validating a reinforcement +learning vacuum robot environment. + +Read context/vacuum-environment.md. + +Your responsibilities: +1. Check the implementation against the + known facts: + - Grid size: 4x4 + - Start: (0,0) + - Obstacle: (1,1) + - Penalty location: (3,1) + - Goal location: (3,3) +2. Verify the robot cannot enter the + obstacle or leave the grid. +3. Run any existing tests or scripts + and report the results faithfully. +4. Identify behavior that relies on + assumptions about the unknown items + (transition model T, complete reward + function R, movement probabilities, + invalid action behavior, terminal-state + rules). +5. Report confirmed facts separately + from assumptions. + +Use Bash only to run existing code or +tests. Do not use it to create, edit, +or delete files. + +Work independently. +Do not edit files. +Do not implement Q-learning. +Do not invent missing requirements. diff --git a/.claude/settings.json b/.claude/settings.json new file mode 100644 index 0000000..ce6ce29 --- /dev/null +++ b/.claude/settings.json @@ -0,0 +1,15 @@ +{ + "hooks": { + "UserPromptSubmit": [ + { + "hooks": [ + { + "type": "command", + "command": "bash \"$CLAUDE_PROJECT_DIR/scripts/context-switcher.sh\"", + "timeout": 10 + } + ] + } + ] + } +} \ No newline at end of file diff --git a/.claude/skills/grid-validation/SKILL.md b/.claude/skills/grid-validation/SKILL.md new file mode 100644 index 0000000..2c8a8d4 --- /dev/null +++ b/.claude/skills/grid-validation/SKILL.md @@ -0,0 +1,11 @@ +--- +name: grid-validation +description: Validate the vacuum robot grid definition. Use when creating or editing the environment, grid, or terminal states. +--- + +# Grid Validation Procedure +1. Confirm the grid is 4x4 with start at (0,0). +2. Confirm exactly one wall at (1,1), one -1 terminal at (3,1), one +10 terminal at (3,3). +3. Confirm terminal states have no outgoing transitions. +4. Confirm a path exists from start to the goal. +5. Report pass/fail for each check. diff --git a/.claude/skills/reward-design/SKILL.md b/.claude/skills/reward-design/SKILL.md new file mode 100644 index 0000000..ea9b9d5 --- /dev/null +++ b/.claude/skills/reward-design/SKILL.md @@ -0,0 +1,24 @@ +--- +name: reward-design +description: Choose, check, and justify the reward and hyperparameter values (living reward, discount factor, learning rate, epsilon) for the vacuum robot. Use when setting or changing reward values or Q-learning hyperparameters. +--- + +# Reward and Hyperparameter Design Procedure + +## Current values (source of truth: context/vacuum-environment.md) +- Living reward: -0.04 per step +- Discount (gamma): 0.9 +- Learning rate (alpha): 0.1 +- Epsilon: 0.1 + +## Steps +1. Read context/vacuum-environment.md and confirm the values above match it. If they differ, report the mismatch and do not silently pick one. +2. Check each value against these rules: + - Living reward must be small and negative compared to the terminal rewards (+10 goal, -1 pit), so the robot prefers short paths but still reaches the goal. + - Gamma must be strictly between 0 and 1. Too low and the +10 will not propagate back to the start. + - Alpha must be between 0 and 1. Smaller is more stable, larger learns faster but is noisier. + - Epsilon must be between 0 and 1. Note any plan to decay it. +3. Write a 1-2 sentence justification for each value. +4. Remember the robot does NOT know R or T. The reward function belongs to the environment. The agent only sees rewards returned by step(). Never let agent code read the reward table directly. +5. If a value is changed, record the old value, new value, and reason in the Decision Log in context/vacuum-environment.md. +6. Report a short summary table: parameter, value, justification, pass/fail. diff --git a/.claude/skills/transition-testing/SKILL.md b/.claude/skills/transition-testing/SKILL.md new file mode 100644 index 0000000..f6a0342 --- /dev/null +++ b/.claude/skills/transition-testing/SKILL.md @@ -0,0 +1,20 @@ +--- +name: transition-testing +description: Test the vacuum robot environment's movement and transition behavior. Use when writing, running, or reviewing tests for step(), actions, wall bumps, or terminal states. +--- + +# Transition Testing Procedure + +Tests go in tests/. Run them with: python -m pytest tests/ (or python -m unittest if pytest is not installed). + +## Cases to cover +1. **Normal moves:** from (0,0), moving right goes to (0,1) and moving down goes to (1,0). +2. **Edge bumps:** from (0,0), moving up or left leaves the state at (0,0). +3. **Wall bump:** the wall at (1,1) cannot be entered. From (1,0), moving right stays at (1,0). From (0,1), moving down stays at (0,1). +4. **Terminal states:** entering (3,3) returns reward +10 and done=True. Entering (3,1) returns reward -1 and done=True. +5. **Living reward:** a normal non-terminal move returns the living reward (-0.04). +6. **Interface:** step(state, action) returns (next_state, reward, done) and nothing else leaks out, such as the full reward table. +7. **Determinism:** unless the assignment specifies noisy movement, the same state and action must always give the same result. + +## Report +List each case with pass/fail. For any failure, show the state, action, expected result, and actual result. diff --git a/.gitignore b/.gitignore index 3d22bf9..c7ef76b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ __pycache__/ *.py[cod] *.pyo *.pyd +*.pyc +*pytest_cache # Virtual environments .venv/ diff --git a/Catrina-Project-2.mp4 b/Catrina-Project-2.mp4 new file mode 100644 index 0000000..3019be9 Binary files /dev/null and b/Catrina-Project-2.mp4 differ diff --git a/README.md b/README.md index 78c1767..d2c42cc 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,35 @@ +# Project 2: Agent Skills Component for mini-coding-agent +Group 11: Catrina Coons, Yulisa Medrano + + +---- +# Handoff to Yulisa for 2nd Part of the Project: + +I've finished the first half of the SDLC: intent, spec, plan, plus a README draft, and my video. They're in intent/agent-skills/ and README.md is drafted. Everything from here on is yours: implementation into mini-coding-agent.py, skills from the Lab 5 and testing it, finish the README, and your half of the video. Treat spec.md as the contract and plan.md as your guide, it will walk you through the implementation. The requirement numbers (R1–R21) are meant to show up in your test names and commit messages. You can then import the skills from Lab 5, just be sure to change the file path and add the keywords. Tune the agent with a few test prompts to see if it triggers the skills from keywords correctly, then have it build the vacuum robot environment just like we made Claude Code do for Lab 5. + +If you disagree with a design decision, go ahead and change it. Just update spec.md/plan.md in the same commit. Lmk if you have any questions! + +---- + +mini-coding-agent already has five of the six coding-agent components from class. This +fork adds the missing one: agent skills. The agent finds skill folders such as skills/ +reward-design/SKILL.md, checks each request against them using keyword similarity, loads +the best match into its prompt, and can also load a skill on its own during a task. Every +prompt, similarity check and executed skill is logged. We demonstrate it by having the +agent build and train a Q-learning vacuum robot (the Lab). + +## SDLC documents + +intent.md - The problem, the desired outcome, constraints, open questions +spec.md - Numbered requirements R1–R21, the design, areas of concern +plan.md - Files that change, order of work (citing R#), risks, proof + +## How the skills componenet works + +[TODO] + +### Requirements are unchanged from original Mini-Coding_Agent directions +   # Mini-Coding-Agent diff --git a/YulisaVideoProject2.mp4 b/YulisaVideoProject2.mp4 new file mode 100644 index 0000000..62eac40 Binary files /dev/null and b/YulisaVideoProject2.mp4 differ diff --git a/intent/agent-skills/intent.md b/intent/agent-skills/intent.md new file mode 100644 index 0000000..18beee1 --- /dev/null +++ b/intent/agent-skills/intent.md @@ -0,0 +1,61 @@ +# Intent: mini-coding-agent can find and follow agent skills + +Author: Catrina Coons. Status: draft (2026-11-16). + +## Problem + +mini-coding-agent has five of the six components covered in class (repo context, prompt shape, tools and permissions, context reduction, memory, delegation), and now it has the skills component. When we solved the vacuum-robot Lab, we wrote skills (grid-validation, reward-design, transition-testing skills that are in .claude/skills/ folder) that capture the rules the work must follow. mini-coding-agent can use them. It does know a .claude/skills/ folder exists. It can tell when a request needs one of these skills based on the user prompt, and it can load a skill's instructions into what the model sees. So every prompt has to restate those rules by hand, or they get ignored. + +We can record whether a skill was considered on used. So we can show, debug, or why the agent did or did not follow a skill. + +## Proposed outcome + +The agent finds skill folders such as .claude/skills/grid-validation/SKILL.md, .claude/skills/reward-design/SKILL.md, .claude/skills/transition-testing/SKILL.md in the workspace. + +When a user prompt matches a skill's description closely enough, the agent puts that skill's instructions into the prompt and follows them. Nobody has to name the skill. + +If the agent realizes partway through a task that it needs a skill, it can load that skill on its own. + +Every decision is logged: the prompt, the similarity check (each skill's score), and which skill ran. + +To prove it works, the agent uses our Lab skills to build the vacuum-robot environment and train a Q-learning agent on it. + +### Affected users and systems + +Anyone running mini-coding-agent who wants repeatable, rule-following behavior on a recurring kind of task. + +MiniAgent.ask() / prompt() (where the prompt is assembled), build_tools() (a new tool), and .mini-coding-agent/ (where the new log goes). + +The default local model (qwen3.5:4b) is small. Whatever we build has to work with a model that has a short context and weak reasoning. + +## Constraints + +Python standard library only. No new pip dependencies, and no embedding model. + +The similarity check uses keyword matching, the approach used in class. + +"ask" stays the default approval policy. Skills never bypass approval. + +Existing behavior and existing tests keep working when there is no skills/ folder. + +Skills use the format from class: YAML frontmatter (name, description), then numbered, checkable rules, ending with a check that leaves evidence. + +### Out of scope + +Embedding or LLM-based semantic matching. + +Hooks or CI checks that enforce skill rules. Skills stay advisory. + +Downloading or sharing skills between repositories. + +## Open questions + +What exactly counts as "similar enough"? Which words are compared, how is the score computed, and what is the threshold? + +Should more than one skill be able to load for the same prompt? + +Where in the prompt does the skill text go so it doesn't break prompt-prefix reuse? + +When the model loads a skill mid-task, does that skill stay active for the rest of the request? + +Do delegated child agents get skills too? diff --git a/intent/agent-skills/plan.md b/intent/agent-skills/plan.md new file mode 100644 index 0000000..8469c09 --- /dev/null +++ b/intent/agent-skills/plan.md @@ -0,0 +1,52 @@ +# Plan: mini-coding-agent can find and follow agent skills + +Author: Catrina Coons. Status: draft (2026-11-16). Source: intent/agent-skills/intent.md, intent/agent-skills/spec.md + +## Files that change + +mini_coding_agent.py: new SkillLibrary class; changes to MiniAgent.__init__, build_tools() (load_skill), build_prefix() (skill list), prompt() (active skills section), ask() (prompt-time check, reset active set) and tool_delegate() (child shares the library); build_agent() / build_arg_parser() (new flags); main() (/skills command); HELP_DETAILS. + +tests/test_skills.py (new): unit tests that use FakeModelClient and tmp_path. + +.claude/skills/grid-validation/SKILL.md, .claude/skills/reward-design/SKILL.md, .claude/skills/transition-testing/SKILL.md (ported, paths updated), .claude/skills/q-learning-training/SKILL.md (new). + +lab/vacuum-robot/: context/vacuum-environment.md, environment.py, q_learning.py, tests/. The agent writes these during the demo run, not by hand. +README.md: a Project 2 section at the top. + +logs/ (or a copy of .mini-coding-agent/skill_log.jsonl): the evidence from the Lab run. + +## Order of work + +Failing tests first (tests/test_skills.py) for: discovery and skipping bad frontmatter (R1); no skills/ gives an unchanged prompt (R2); keyword extraction and the scoring math on a hand-computed example (R5, R6); threshold, top-k and tie order (R7, R9); the skill body appears in prompt() between prefix and memory (R8); load_skill works and rejects unknown names (R10, R11); log lines have every required field (R13, R14). + +SkillLibrary: discovery and frontmatter parser, terms(text), score(prompt) that returns every skill's score and matched keywords, get(name), and log(event), which appends to .mini-coding-agent/skill_log.jsonl (R1, R5, R6, R9, R13, R14). + +Wire up prompt time: in ask(), reset the active set, run the check, log it, activate the matches, print the console line, and record a history event (R4, R7, R15, R16). Add the active-skills section to prompt() (R8). Add the skill list to build_prefix() only when skills exist (R2, R3). + +Wire up mid-task: add the load_skill tool (not risky) and its validation in validate_tool(), plus an example in tool_example(). Handle "already active" without tripping repeated_tool_call() (R10, R11, R14). + +Delegation: pass the same SkillLibrary to the child agent in tool_delegate(). Assert that the child stays read_only with approval "never" (R12, R17). + +CLI: add --skill-threshold, --max-skills and /skills, and update the help text (Design). + +Skills: port the three Lab skills (fix paths to lab/vacuum-robot/, add keywords:), and write q-learning-training (R18). + +Calibrate: run 8–10 realistic Lab prompts, read the scores in the log, and adjust the threshold or skill keywords until each prompt picks the right skill and off-topic prompts pick none. Record the final values in the README (R6, area of concern 1–2). + +Run the Lab with the agent, a sequence of prompts at --approval ask, so the agent builds the environment, validates it, writes and runs the tests, records the hyperparameters, and trains Q-learning (R19, R20). Keep the log (R21). + +README and video. + +## Risks + +The small model may not follow skill rules, or may write broken code. If so, break the Lab into smaller prompts, raise --max-steps and --max-new-tokens, or try a bigger Ollama model. Document whichever was used. + +Adding a tool and a prefix section changes the prompt for every user. Mitigated by R2: with no skills, the prompt is byte-for-byte unchanged, and a test checks this. + +Threshold tuning is guesswork. Mitigated by logging every skill's score, not just the winner, so tuning is based on data. + +## Proof + +python -m pytest tests/ passes, both the original tests and test_skills.py (R1–R17). +.mini-coding-agent/skill_log.jsonl from the Lab run contains similarity_check lines with scores for all four skills, and skill_executed lines for both triggers (R13, R14, R21). +python -m pytest lab/vacuum-robot/tests passes, and python lab/vacuum-robot/q_learning.py prints a greedy policy that goes from (0,0) to (3,3) around the wall and avoids (3,1) (R19, R20). diff --git a/intent/agent-skills/spec.md b/intent/agent-skills/spec.md new file mode 100644 index 0000000..202b9a0 --- /dev/null +++ b/intent/agent-skills/spec.md @@ -0,0 +1,126 @@ +# Spec: mini-coding-agent can find and follow agent skills + +Author: Catrina Coons. Status: draft (2026-11-16). Source: intent/agent-skills/intent.md + +Constraints carried in: standard library only; keyword-based similarity; "ask" stays the default and skills never bypass approval; no behavior change when .claude/skills/ is absent; class skill format. + +## Definitions + +Skill: a folder .claude/skills// (relative to the workspace repo root) that contains SKILL.md. + +SKILL.md: a frontmatter block between --- lines with name and description (required) and keywords (optional, comma-separated), followed by a markdown body. + +Skill terms: the keyword set built from a skill's name, description and keywords. + +Prompt terms: the keyword set built from the user's request. + +## Requirements + +### Discovery + +R1. At startup, the agent loads every .claude/skills/*/SKILL.md under the workspace repo root. A skill with missing or broken frontmatter is skipped, and a warning is logged. The agent does not crash. + +R2. If there is no .claude/skills/ folder, or it is empty, the agent behaves exactly as it did before: same prompt and same tools. All existing tests still pass. + +R3. The stable prompt prefix includes a short "Available skills" list (name plus description only). This lets the model know which skills exist without loading their bodies. + +### Similarity check (trigger 1: prompt time) + +R4. At the start of every ask(), the user's request is checked against every skill before the first model call. + +R5. Keywords are extracted the same way for the prompt and for each skill: lowercase the text, split on anything that isn't a letter or digit (so q-learning becomes q, learning), drop stopwords and tokens shorter than 2 characters, and strip a trailing plural s from tokens longer than 3 characters. + +R6. Score = |prompt terms ∩ skill terms| / |prompt terms|. This is the share of the request's keywords that the skill covers, from 0.0 to 1.0. A skill matches when score ≥ threshold and it shares at least 2 keywords with the prompt. The threshold defaults to 0.25 and can be set with --skill-threshold. + +R7. At most --max-skills matching skills are loaded (default 1), highest score first. Ties are broken by name, so the result is deterministic. + +R8. Each matched skill's full body is added to the prompt in an "Active skills" section. It goes after the stable prefix and before memory and the transcript, and stays there for every model call in that ask(). Each body is limited to 3,000 characters. + +R9. Matching is deterministic: the same request and the same skills always give the same scores and the same selection. + +### Trigger 2: during execution + +R10. A new tool, load_skill(name: str), is available and is not risky (it only reads a file). It returns the skill body, or an error that lists the valid names. + +R11. A skill loaded with load_skill is added to the active set for the rest of the current ask(). Loading an already-active skill returns a short "already active" message and does not count as a repeated call. + +R12. Delegated child agents run the same prompt-time check on their delegated task and can use load_skill. They stay read-only, and skills do not change that. + +### Logging + +R13. Every similarity check appends one JSON line to .mini-coding-agent/skill_log.jsonl with event: "similarity_check", timestamp, session_id, depth, trigger ("prompt"), prompt, threshold, scores (every skill: name, score rounded to 3 decimals, matched keywords) and selected (names, possibly empty). + +R14. Every time a skill becomes active, whether from the check or from load_skill, one JSON line is appended with event: "skill_executed", timestamp, session_id, depth, trigger ("prompt" or "load_skill"), skill, path and score (null for load_skill). + +R15. Skill activation is also printed as one console line, e.g. [skill] reward-design (score 0.43, prompt match). Logs are added to and never overwritten. + +R16. The skill events are also recorded in the session history, so --resume and /memory show which skill was used. + +### Safety + +R17. Skills are instructions only. They never change the approval policy, tool risk levels, or path checks. Any risky action a skill asks for still goes through approve(). + +### Demonstration (the Lab) + +R18. The repo contains four Lab skills: grid-validation, reward-design, transition-testing (from the earlier Lab, with paths updated to lab/vacuum-robot/) and a new q-learning-training. + +R19. Using those skills, the agent builds the Lab in lab/vacuum-robot/: + +An environment for the 4×4 grid: start (0,0), wall (1,1), −1 terminal (3,1), +10 terminal (3,3). It is deterministic. step(state, action) returns (next_state, reward, done) and does not expose R or T. + +A Q-learning agent that learns only from step(). + +Tests. + +A run that prints the learned Q-values and the greedy policy. + +R20. Hyperparameters, with their reasons recorded in lab/vacuum-robot/context/vacuum-environment.md: + +Parameter Value Justification + +Living reward −0.04 Small cost per step makes the robot prefer short paths without outweighing +10 or −1. + +Discount γ 0.9 Long enough horizon for +10 to reach the start about 6 steps away, and still converges quickly. + +Learning rate α 0.1 Stable updates. The environment is deterministic, so a modest rate converges reliably. + +Epsilon ε 0.1 10% exploration. Can decay toward 0.01 over episodes so the final policy is greedy. + +R21. The log from at least one real Lab run shows a prompt-time match and a load_skill activation, and that log is committed (or captured) as evidence. + +Design + +Where the code lives: a small SkillLibrary class in mini_coding_agent.py (the project keeps everything in one file). It handles discovery, parsing, keyword extraction, scoring and log writing. MiniAgent holds one instance. + +When it triggers: (1) in ask(), right after the user message is recorded and before the loop starts; (2) when the model calls load_skill during the loop. This answers the class question "what is the exact moment the skills component is triggered?" + +Prompt placement: prompt() becomes prefix → active skills → memory → transcript → request. The prefix only gets the short skill list (R3), so it stays stable and can still be reused from cache. Bodies change per request, so they sit outside the prefix. + +Active set: stored for the current ask(). It is cleared at the start of the next ask() and then rebuilt by the new similarity check. + +Frontmatter parsing: a minimal standard-library parser that reads key: value lines between the first two --- lines. Full YAML is not supported. + +CLI: two new flags, --skill-threshold (float, default 0.25) and --max-skills (int, default 1). A new .claude/skills REPL command lists the loaded skills. + +Answers to the intent's open questions + +"Similar enough" is defined by R5 and R6: share of prompt keywords covered, at least 2 shared keywords, threshold 0.25. + +Several skills can load at once, but the default is 1 (R7). This keeps the prompt small for a 4B model. + +The skill text goes after the prefix and before memory (R8). + +Yes. A skill loaded mid-task stays active for the rest of that request (R11). + +Yes. Delegated agents get skills and stay read-only (R12). + +## Areas of concern + +Keyword matching misses synonyms ("train the robot" vs "Q-learning"). The fix is the optional keywords: field in the skill frontmatter, plus load_skill as a fallback. Owner: Yulisa Medrano (skill author). + +False positives on generic words ("test", "file"). The fix is the stopword list, the 2-shared-keyword minimum, and tuning the threshold against logged scores. Owner: Yulisa Medrano. + +A small model ignores or partly follows a skill. Skills are advisory (R17). The fix is short, numbered rules that end in a check that leaves evidence, and the log shows whether the skill was loaded. Owner: Yulisa Medrano. + +Prompt size. The default --max-new-tokens 512 and --max-steps 6 may be too small for the Lab, so the README documents the flags to raise for the Lab run. Owner: Yulisa Medrano. +Prompt injection through skill files. A skill can tell the model to run commands. Approval stays in force (R17), and the README says to review skills before using --approval auto. Owner: Catrina Coons (spec), Yulisa Medrano (README). diff --git a/lab/q_learning.py b/lab/q_learning.py new file mode 100644 index 0000000..5d8eafb --- /dev/null +++ b/lab/q_learning.py @@ -0,0 +1,158 @@ +"""Tabular Q-learning for the 4x4 vacuum robot. + +The agent never reads the environment's reward table or transition model. +It only sees rewards and next states returned by env.step(). + +Run: py q_learning.py [--episodes N] [--p-intended P] [--seed S] +""" + +import argparse +import random +from collections import defaultdict + +from vacuum_env import DOWN, EXIT, LEFT, MOVE_ACTIONS, RIGHT, UP, VacuumEnv + +# Hyperparameters from intent/agent-skills/spec.md +GAMMA = 0.9 +ALPHA = 0.1 +EPSILON = 0.1 + +# Training setup confirmed by the user +EPISODES = 5000 +MAX_STEPS = 100 +SEED = 0 + + +class QLearningAgent: + def __init__(self, alpha=ALPHA, gamma=GAMMA, epsilon=EPSILON, seed=None): + self.alpha = alpha + self.gamma = gamma + self.epsilon = epsilon + self.q = defaultdict(float) # (state, action) -> value, starts at 0 + self._rng = random.Random(seed) + + def value(self, state, legal): + return max(self.q[(state, a)] for a in legal) + + def greedy_action(self, state, legal): + """Best action; ties broken by the order of `legal` (deterministic).""" + return max(legal, key=lambda a: self.q[(state, a)]) + + def choose_action(self, state, legal): + """Epsilon-greedy; greedy ties broken at random.""" + if self._rng.random() < self.epsilon: + return self._rng.choice(legal) + best = self.value(state, legal) + return self._rng.choice([a for a in legal if self.q[(state, a)] == best]) + + def update(self, state, action, reward, next_state, done, next_legal): + if done: + target = reward + else: + target = reward + self.gamma * self.value(next_state, next_legal) + key = (state, action) + self.q[key] += self.alpha * (target - self.q[key]) + + +def train(env, agent, episodes=EPISODES, max_steps=MAX_STEPS): + """Run Q-learning. Returns the undiscounted return of each episode.""" + returns = [] + for _ in range(episodes): + state = env.reset() + total = 0.0 + for _ in range(max_steps): + action = agent.choose_action(state, env.legal_actions(state)) + next_state, reward, done = env.step(state, action) + next_legal = () if done else env.legal_actions(next_state) + agent.update(state, action, reward, next_state, done, next_legal) + total += reward + if done: + break + state = next_state + returns.append(total) + return returns + + +# ---- text output ------------------------------------------------------- + +_ARROWS = {UP: "^", DOWN: "v", LEFT: "<", RIGHT: ">"} + + +def format_policy(env, agent): + rows = [] + for r in range(env.ROWS): + cells = [] + for c in range(env.COLS): + s = (r, c) + if s in env.WALLS: + cells.append("#") + elif env.is_terminal(s): + cells.append("EXIT") + else: + cells.append(_ARROWS[agent.greedy_action(s, MOVE_ACTIONS)]) + rows.append(" ".join(f"{x:>5}" for x in cells)) + return "\n".join(rows) + + +def format_values(env, agent): + rows = [] + for r in range(env.ROWS): + cells = [] + for c in range(env.COLS): + s = (r, c) + if s in env.WALLS: + cells.append(f"{'#':>7}") + else: + cells.append(f"{agent.value(s, env.legal_actions(s)):7.3f}") + rows.append(" ".join(cells)) + return "\n".join(rows) + + +def format_q_table(env, agent): + header = f"{'state':<8}" + "".join(f"{a:>9}" for a in MOVE_ACTIONS + (EXIT,)) + lines = [header] + for s in env.states(): + legal = env.legal_actions(s) + cells = [ + f"{agent.q[(s, a)]:9.3f}" if a in legal else f"{'-':>9}" + for a in MOVE_ACTIONS + (EXIT,) + ] + lines.append(f"{str(s):<8}" + "".join(cells)) + return "\n".join(lines) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument("--episodes", type=int, default=EPISODES) + parser.add_argument("--max-steps", type=int, default=MAX_STEPS) + parser.add_argument( + "--p-intended", + type=float, + default=0.8, + help="probability the move goes in the chosen direction", + ) + parser.add_argument("--seed", type=int, default=SEED) + args = parser.parse_args() + + env = VacuumEnv(p_intended=args.p_intended, seed=args.seed) + agent = QLearningAgent(seed=args.seed + 1) + returns = train(env, agent, args.episodes, args.max_steps) + + print( + f"Q-learning: {args.episodes} episodes, gamma={GAMMA}, alpha={ALPHA}, " + f"epsilon={EPSILON}, p_intended={args.p_intended}, seed={args.seed}" + ) + last = returns[-100:] + print( + f"Average return over last {len(last)} episodes: {sum(last) / len(last):.3f}\n" + ) + print("Q-table:") + print(format_q_table(env, agent)) + print("\nGreedy policy:") + print(format_policy(env, agent)) + print("\nState values V(s) = max_a Q(s, a):") + print(format_values(env, agent)) + + +if __name__ == "__main__": + main() diff --git a/lab/vacuum_env.py b/lab/vacuum_env.py new file mode 100644 index 0000000..d80d3f3 --- /dev/null +++ b/lab/vacuum_env.py @@ -0,0 +1,110 @@ +"""4x4 vacuum robot gridworld environment. + +Grid (row, col), start at top-left: + + S 0 0 0 + 0 X 0 0 + 0 0 0 0 + 0 -1 0 10 + +Confirmed facts (context/vacuum-environment.md): + - 4x4 grid, start (0,0), wall (1,1) not enterable. + - (3,1) is a -1 terminal (pit), (3,3) is a +10 terminal (goal). + - Actions up/down/left/right; bumping a wall or edge = stay in place. + - Living reward -0.04. + +Design decisions confirmed by the user (not in the assignment text): + - Movement is stochastic and configurable: with probability p_intended the + robot moves in the chosen direction, otherwise it slips to one of the two + perpendicular directions with equal probability. Default p_intended = 0.8 + (0.8 / 0.1 / 0.1). p_intended = 1.0 gives deterministic movement. + - Terminal rewards use a separate EXIT action: entering a terminal square is + an ordinary move (living reward, done=False). In a terminal square the only + legal action is EXIT, which pays +10 / -1 and ends the episode (done=True). + - A bump (wall or edge) is an ordinary move: it pays the living reward. + +The reward table and transition model are private to the environment. A +learning agent should only use reset(), legal_actions() and step(). +""" + +import random + +UP, DOWN, LEFT, RIGHT, EXIT = "up", "down", "left", "right", "exit" +MOVE_ACTIONS = (UP, DOWN, LEFT, RIGHT) + +_DELTAS = {UP: (-1, 0), DOWN: (1, 0), LEFT: (0, -1), RIGHT: (0, 1)} +_PERPENDICULAR = { + UP: (LEFT, RIGHT), + DOWN: (LEFT, RIGHT), + LEFT: (UP, DOWN), + RIGHT: (UP, DOWN), +} + + +class VacuumEnv: + ROWS = 4 + COLS = 4 + START = (0, 0) + WALLS = frozenset({(1, 1)}) + + def __init__(self, p_intended=0.8, living_reward=-0.04, seed=None): + if not 0.0 <= p_intended <= 1.0: + raise ValueError("p_intended must be between 0 and 1") + self.p_intended = p_intended + self.living_reward = living_reward + self._terminal_rewards = {(3, 1): -1.0, (3, 3): 10.0} + self._rng = random.Random(seed) + + # ---- public interface ------------------------------------------------- + + def reset(self): + """Start a new episode; returns the start state.""" + return self.START + + def states(self): + """All enterable cells (the wall is not a state).""" + return [ + (r, c) + for r in range(self.ROWS) + for c in range(self.COLS) + if (r, c) not in self.WALLS + ] + + def is_terminal(self, state): + return state in self._terminal_rewards + + def legal_actions(self, state): + return (EXIT,) if self.is_terminal(state) else MOVE_ACTIONS + + def step(self, state, action): + """Apply action in state. Returns (next_state, reward, done). + + After EXIT the episode is over and next_state is None. + """ + if state not in self.states(): + raise ValueError(f"invalid state {state!r}") + if action not in self.legal_actions(state): + raise ValueError(f"action {action!r} not legal in state {state!r}") + + if action == EXIT: + return None, self._terminal_rewards[state], True + + direction = self._sample_direction(action) + return self._move(state, direction), self.living_reward, False + + # ---- private helpers ------------------------------------------------- + + def _move(self, state, direction): + dr, dc = _DELTAS[direction] + r, c = state[0] + dr, state[1] + dc + if not (0 <= r < self.ROWS and 0 <= c < self.COLS) or (r, c) in self.WALLS: + return state # bump: stay in place + return (r, c) + + def _sample_direction(self, action): + u = self._rng.random() + if u < self.p_intended: + return action + left_slip, right_slip = _PERPENDICULAR[action] + slip = (1.0 - self.p_intended) / 2.0 + return left_slip if u < self.p_intended + slip else right_slip diff --git a/mini_coding_agent.py b/mini_coding_agent.py index 1bf2403..1ea9cb5 100644 --- a/mini_coding_agent.py +++ b/mini_coding_agent.py @@ -12,7 +12,7 @@ DOC_NAMES = ("AGENTS.md", "README.md", "pyproject.toml", "package.json") -HELP_TEXT = "/help, /memory, /session, /reset, /exit" +HELP_TEXT = "/help, /skills, /memory, /session, /reset, /exit" WELCOME_ART = ( "/\\ /\\\\", "{ `---' }", @@ -25,6 +25,7 @@ [ "Commands:", "/help Show this help message.", + "/skills Show discovered agent skills.", "/memory Show the agent's distilled working memory.", "/session Show the path to the saved session file.", "/reset Clear the current session history and memory.", @@ -222,6 +223,60 @@ def complete(self, prompt, max_new_tokens): return data.get("response", "") +class SkillLibrary: + """Discover and score workspace-local SKILL.md files (stdlib only).""" + + STOP_WORDS = {"the", "and", "for", "with", "from", "that", "this", "when", "into", + "use", "using", "are", "can", "will", "you", "your", "one", "each", + "skill", "skills", "robot", "vacuum", "lab"} + + def __init__(self, root): + self.root = Path(root).resolve() + self.skills = {} + self.warnings = [] + directory = self.root / ".claude" / "skills" + if not directory.is_dir(): + return + for path in sorted(directory.glob("*/SKILL.md")): + try: + if not path.resolve().is_relative_to(self.root): + raise ValueError("skill path escapes workspace") + raw = path.read_text(encoding="utf-8") + match = re.match(r"\A---\s*\n(.*?)\n---\s*\n(.*)\Z", raw, re.S) + if not match: + raise ValueError("missing YAML frontmatter") + fields = {} + for line in match.group(1).splitlines(): + if ":" in line: + key, value = line.split(":", 1) + fields[key.strip()] = value.strip().strip('"\'') + name, description = fields.get("name", ""), fields.get("description", "") + if not name or not description or name != path.parent.name: + raise ValueError("invalid name or description") + keywords = fields.get("keywords", "").strip("[]") + terms = self.terms(" ".join((name, description, keywords.replace(",", " ")))) + self.skills[name] = {"name": name, "description": description, + "path": str(path.relative_to(self.root)), + "body": match.group(2).strip(), "terms": terms} + except (OSError, ValueError, UnicodeError) as exc: + self.warnings.append(f"{path}: {exc}") + + @classmethod + def terms(cls, value): + return {term for term in re.findall(r"[a-z0-9]+", value.lower()) + if len(term) >= 3 and term not in cls.STOP_WORDS} + + def rank(self, prompt): + words = self.terms(prompt) + scores = [] + for skill in self.skills.values(): + matched = sorted(words & skill["terms"]) + score = len(matched) / len(skill["terms"]) if skill["terms"] else 0.0 + scores.append({"name": skill["name"], "score": round(score, 3), + "matched_keywords": matched}) + return sorted(scores, key=lambda row: (-row["score"], row["name"])) + + class MiniAgent: def __init__( self, @@ -235,6 +290,8 @@ def __init__( depth=0, max_depth=1, read_only=False, + skill_threshold=0.25, + max_skills=1, ): self.model_client = model_client self.workspace = workspace @@ -246,6 +303,12 @@ def __init__( self.depth = depth self.max_depth = max_depth self.read_only = read_only + self.skill_threshold = skill_threshold + self.max_skills = max_skills + self.skill_library = SkillLibrary(self.root) + self.active_skills = {} + for warning in self.skill_library.warnings: + print(f"[skill] warning: {warning}", file=sys.stderr) self.session = session or { "id": datetime.now().strftime("%Y%m%d-%H%M%S") + "-" + uuid.uuid4().hex[:6], "created_at": now(), @@ -318,6 +381,13 @@ def build_tools(self): "run": self.tool_patch_file, }, } + if self.skill_library.skills: + tools["load_skill"] = { + "schema": {"name": "str"}, + "risky": False, + "description": "Activate a named skill and read its instructions.", + "run": self.tool_load_skill, + } if self.depth < self.max_depth: tools["delegate"] = { "schema": {"task": "str", "max_steps": "int=3"}, @@ -362,15 +432,35 @@ def build_prefix(self): "- Before writing tests for existing code, read the implementation first.", "- When writing tests, match the current implementation unless the user explicitly asked you to change the code.", "- New files should be complete and runnable, including obvious imports.", + "- Agent skills are stored in .claude/skills/, not intent/agent-skills/skills/.", + "- Always use list_files to verify a directory before reading an unknown file path.", + "- If a tool reports that a path does not exist, do not repeat the same failed path.", + "- Tool calls must contain valid JSON with no extra characters.", + "- After successfully reading a requested file, summarize its contents and return a final answer.", "- Do not repeat the same tool call with the same arguments if it did not help. Choose a different tool or return a final answer.", + "- Never claim a file was modified unless a write or edit tool succeeded.", + "- Never claim tests passed unless their execution results confirm it.", + "- If a file was already read successfully, use its content rather than repeating the same read.", + "- When asked to apply a skill, inspect the relevant project files and report evidence for each required check.", + "- If a tool call fails, correct the arguments or report the failure rather than repeating it.", + "- Agent skills are stored ONLY in .claude/skills//SKILL.md.", + "- The intent/agent-skills/ directory contains SDLC documentation, NOT executable skills.", + "- Automatically activated skill instructions are already included in the prompt.", + "- Do not use read_file or list_files to rediscover an already active skill.", + "- When a skill is active, apply its instructions to the relevant project files.", + "- If the required project files are missing, report that limitation instead of repeatedly searching.", "- Required tool arguments must not be empty. Do not call read_file, write_file, patch_file, run_shell, or delegate with args={}.", ]) + skill_listing = ("Available skills:\n" + "\n".join( + f"- {skill['name']}: {skill['description']}" + for skill in self.skill_library.skills.values())) if self.skill_library.skills else "" return "\n\n".join([ "You are Mini-Coding-Agent, a small local coding agent running through Ollama.", "Rules:\n" + rules, "Tools:\n" + tool_text, "Valid response examples:\n" + examples, self.workspace.text(), + *([skill_listing] if skill_listing else []), ]) def memory_text(self): @@ -407,7 +497,7 @@ def history_text(self): seen_reads.add(path) if item["role"] == "tool": - limit = 900 if recent else 180 + limit = 3000 if recent else 500 lines.append(f"[tool:{item['name']}] {json.dumps(item['args'], sort_keys=True)}") lines.append(clip(item["content"], limit)) else: @@ -420,12 +510,24 @@ def history_text(self): #### 2) Prompt Shape And Cache Reuse (Continued) ####### ######################################################## def prompt(self, user_message): + active = "\n\n".join( + f"Active skill: {name}\n{skill['body']}" + for name, skill in self.active_skills.items() + ) return "\n\n".join([ - self.prefix, - self.memory_text(), - "Transcript:\n" + self.history_text(), - "Current user request:\n" + user_message, - ]) + self.prefix, + *([active] if active else []), + self.memory_text(), + "Current user request:\n" + user_message, + "Transcript:\n" + self.history_text(), + ( + "Continue from the transcript above. " + "If the requested file has already been read successfully, " + "do not read it again. Use the available content to answer. " + "If the task is complete, respond with " + "your answer." + ), + ]) ############################################### #### 5) Session Memory (Continued) ########### @@ -442,7 +544,45 @@ def note_tool(self, name, args, result): note = f"{name}: {clip(str(result).replace(chr(10), ' '), 220)}" self.remember(memory["notes"], note, 5) + def skill_event(self, event): + event = {"timestamp": now(), "session_id": self.session["id"], + "depth": self.depth, **event} + destination = self.root / ".mini-coding-agent" / "skill_log.jsonl" + destination.parent.mkdir(parents=True, exist_ok=True) + with destination.open("a", encoding="utf-8") as stream: + stream.write(json.dumps(event, ensure_ascii=False) + "\n") + self.record({"role": "skill", "content": json.dumps(event), "created_at": event["timestamp"]}) + + def activate_skill(self, name, trigger, score=None): + skill = self.skill_library.skills.get(name) + if skill is None: + return "error: unknown skill; valid names: " + ", ".join(self.skill_library.skills) + if name in self.active_skills: + return f"skill '{name}' already active" + self.active_skills[name] = skill + self.skill_event({"event": "skill_executed", "trigger": trigger, + "skill": name, "path": skill["path"], "score": score}) + print(f"[skill] {name} ({'score ' + str(score) if score is not None else 'load_skill'}, {trigger})") + return f"activated {name}\n{skill['body']}" + + def tool_load_skill(self, args): + return self.activate_skill(str(args.get("name", "")), "load_skill") + + def check_skills(self, user_message): + if not self.skill_library.skills: + return + scores = self.skill_library.rank(user_message) + selected = [row for row in scores if row["score"] >= self.skill_threshold + and len(row["matched_keywords"]) >= 2][:self.max_skills] + self.skill_event({"event": "similarity_check", "trigger": "prompt", + "prompt": user_message, "threshold": self.skill_threshold, + "scores": scores, "selected": [row["name"] for row in selected]}) + for row in selected: + self.activate_skill(row["name"], "prompt", row["score"]) + def ask(self, user_message): + self.active_skills = {} + self.check_skills(user_message) memory = self.session["memory"] if not memory["task"]: memory["task"] = clip(user_message.strip(), 300) @@ -456,12 +596,28 @@ def ask(self, user_message): attempts += 1 raw = self.model_client.complete(self.prompt(user_message), self.max_new_tokens) kind, payload = self.parse(raw) + #print(f"\n[DEBUG] Step: {attempts}") + #print(f"[DEBUG] Model output: {raw[:1000]}") if kind == "tool": tool_steps += 1 name = payload.get("name", "") args = payload.get("args", {}) + if name != "load_skill" and self.repeated_tool_call(name, args): + final = ( + f"Stopped: repeated tool call '{name} detected." + " The agent could not complete the task." + ) + self.record({ + "role": "assistant", + "content": final, + "created_at": now(), + }) + return final result = self.run_tool(name, args) + #print(f"[DEBUG] Tool: {name}") + #print(f"[DEBUG] Arguments: {args}") + #print(f"[DEBUG] Result: {result[:1500]}") self.record( { "role": "tool", @@ -505,7 +661,7 @@ def run_tool(self, name, args): if example: message += f"\nexample: {example}" return message - if self.repeated_tool_call(name, args): + if name != "load_skill" and self.repeated_tool_call(name, args): return f"error: repeated identical tool call for {name}; choose a different tool or return a final answer" if tool["risky"] and not self.approve(name, args): return f"error: approval denied for {name}" @@ -530,6 +686,7 @@ def tool_example(self, name): "write_file": 'def binary_search(nums, target):\n return -1\n', "patch_file": 'return -1return mid', "delegate": '{"name":"delegate","args":{"task":"inspect README.md","max_steps":3}}', + "load_skill": '{"name":"load_skill","args":{"name":"grid-validation"}}', } return examples.get(name, "") @@ -591,6 +748,11 @@ def validate_tool(self, name, args): raise ValueError(f"old_text must occur exactly once, found {count}") return + if name == "load_skill": + if not str(args.get("name", "")).strip(): + raise ValueError("name must not be empty") + return + if name == "delegate": if self.depth >= self.max_depth: raise ValueError("delegate depth exceeded") @@ -716,6 +878,7 @@ def extract_raw(text, tag): def reset(self): self.session["history"] = [] + self.active_skills = {} self.session["memory"] = {"task": "", "files": [], "notes": []} self.session_store.save(self.session) @@ -763,6 +926,12 @@ def tool_read_file(self, args): raise ValueError("invalid line range") lines = path.read_text(encoding="utf-8", errors="replace").splitlines() body = "\n".join(f"{number:>4}: {line}" for number, line in enumerate(lines[start - 1:end], start=start)) + if start > len(lines): + return ( + f"Error: Requested line {start}, but this file " + f"only contains {len(lines)}. " + "Do not request this range again." + ) return f"# {path.relative_to(self.root)}\n{body}" def tool_search(self, args): @@ -860,6 +1029,8 @@ def tool_delegate(self, args): depth=self.depth + 1, max_depth=self.max_depth, read_only=True, + skill_threshold=self.skill_threshold, + max_skills=self.max_skills, ) child.session["memory"]["task"] = task child.session["memory"]["notes"] = [clip(self.history_text(), 300)] @@ -931,6 +1102,8 @@ def build_agent(args): approval_policy=args.approval, max_steps=args.max_steps, max_new_tokens=args.max_new_tokens, + skill_threshold=args.skill_threshold, + max_skills=args.max_skills, ) return MiniAgent( model_client=model, @@ -939,6 +1112,8 @@ def build_agent(args): approval_policy=args.approval, max_steps=args.max_steps, max_new_tokens=args.max_new_tokens, + skill_threshold=args.skill_threshold, + max_skills=args.max_skills, ) @@ -962,6 +1137,8 @@ def build_arg_parser(): parser.add_argument("--max-steps", type=int, default=6, help="Maximum tool/model iterations per request.") parser.add_argument("--max-new-tokens", type=int, default=512, help="Maximum model output tokens per step.") parser.add_argument("--temperature", type=float, default=0.2, help="Sampling temperature sent to Ollama.") + parser.add_argument("--skill-threshold", type=float, default=0.25, help="Minimum skill match score.") + parser.add_argument("--max-skills", type=int, default=1, help="Maximum automatically activated skills.") parser.add_argument("--top-p", type=float, default=0.9, help="Top-p sampling value sent to Ollama.") return parser @@ -997,6 +1174,9 @@ def main(argv=None): if user_input == "/help": print(HELP_DETAILS) continue + if user_input == "/skills": + print("\n".join(f"{x['name']}: {x['description']}" for x in agent.skill_library.skills.values()) or "No skills found.") + continue if user_input == "/memory": print(agent.memory_text()) continue diff --git a/tests/test_skills.py b/tests/test_skills.py new file mode 100644 index 0000000..7eb352c --- /dev/null +++ b/tests/test_skills.py @@ -0,0 +1,60 @@ +import json +from mini_coding_agent import FakeModelClient, MiniAgent, SessionStore, SkillLibrary, WorkspaceContext, build_arg_parser + + +def skill(root, name="grid-validation", valid=True): + path = root / ".claude" / "skills" / name / "SKILL.md" + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text((f"---\nname: {name}\ndescription: Validate grid dimensions and terminal states\nkeywords: grid, dimensions, terminal, states\n---\nCheck the 4x4 grid.\n" if valid else "bad frontmatter"), encoding="utf-8") + return path + + +def agent(root, outputs, **kwargs): + workspace = WorkspaceContext.build(root) + return MiniAgent(FakeModelClient(outputs), workspace, SessionStore(root / ".mini-coding-agent" / "sessions"), **kwargs) + + +def events(root): + return [json.loads(x) for x in (root / ".mini-coding-agent" / "skill_log.jsonl").read_text().splitlines()] + + +def test_discovery_and_invalid_skills(tmp_path): + skill(tmp_path) + skill(tmp_path, "broken", False) + library = SkillLibrary(tmp_path) + assert list(library.skills) == ["grid-validation"] + assert len(library.warnings) == 1 + + +def test_automatic_activation_prompt_and_logging(tmp_path): + skill(tmp_path) + a = agent(tmp_path, ["Checked."]) + assert a.ask("Validate grid dimensions and terminal states") == "Checked." + assert "Check the 4x4 grid." in a.model_client.prompts[0] + assert list(a.active_skills) == ["grid-validation"] + assert [e["event"] for e in events(tmp_path)] == ["similarity_check", "skill_executed"] + assert events(tmp_path)[0]["prompt"] == "Validate grid dimensions and terminal states" + assert any(e["role"] == "skill" for e in a.session["history"]) + + +def test_load_skill_and_idempotency(tmp_path): + skill(tmp_path) + a = agent(tmp_path, []) + assert "activated grid-validation" in a.run_tool("load_skill", {"name": "grid-validation"}) + assert "already active" in a.run_tool("load_skill", {"name": "grid-validation"}) + assert len(events(tmp_path)) == 1 + assert "valid names" in a.run_tool("load_skill", {"name": "unknown"}) + + +def test_no_skills_keeps_existing_behavior(tmp_path): + a = agent(tmp_path, ["ok"]) + assert "load_skill" not in a.tools + assert "Available skills" not in a.prefix + assert a.ask("Validate grid dimensions") == "ok" + assert not (tmp_path / ".mini-coding-agent" / "skill_log.jsonl").exists() + + +def test_cli_options(): + args = build_arg_parser().parse_args(["--skill-threshold", "0.4", "--max-skills", "2"]) + assert args.skill_threshold == 0.4 + assert args.max_skills == 2