diff --git a/.github/workflows/conformance-probes.yml b/.github/workflows/conformance-probes.yml index ed1bfde194..a536d5aeb0 100644 --- a/.github/workflows/conformance-probes.yml +++ b/.github/workflows/conformance-probes.yml @@ -138,12 +138,18 @@ jobs: POLICY_IDENTITY=$(.venv/bin/python -c 'from autoskillit.execution.backends._probe_cache import PROBE_POLICY_IDENTITY; print(PROBE_POLICY_IDENTITY)') echo "policy_identity=${POLICY_IDENTITY}" >> "$GITHUB_OUTPUT" + - name: Resolve probe cache key + id: resolve-cache-key + env: + PROBE_CACHE_KEY: probe-codex-${{ steps.resolve-version.outputs.cli_version }}-${{ steps.resolve-policy.outputs.policy_identity }}-${{ runner.os }}-${{ hashFiles('src/autoskillit/**', 'tests/**', '.autoskillit/recipes/**', 'pyproject.toml', 'uv.lock', '.github/workflows/conformance-probes.yml', '!**/__pycache__/**') }} + run: echo "key=$PROBE_CACHE_KEY" >> "$GITHUB_OUTPUT" + - name: Restore probe cache id: restore-cache uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 with: path: .autoskillit/temp/probe-cache/codex - key: probe-codex-${{ steps.resolve-version.outputs.cli_version }}-${{ steps.resolve-policy.outputs.policy_identity }} + key: ${{ steps.resolve-cache-key.outputs.key }} - name: Run Codex conformance probes if: steps.restore-cache.outputs.cache-hit != 'true' @@ -172,7 +178,7 @@ jobs: uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 with: path: .autoskillit/temp/probe-cache/codex - key: probe-codex-${{ steps.resolve-version.outputs.cli_version }}-${{ steps.resolve-policy.outputs.policy_identity }} + key: ${{ steps.resolve-cache-key.outputs.key }} - name: Post probe failure if: failure() @@ -269,12 +275,18 @@ jobs: POLICY_IDENTITY=$(.venv/bin/python -c 'from autoskillit.execution.backends._probe_cache import PROBE_POLICY_IDENTITY; print(PROBE_POLICY_IDENTITY)') echo "policy_identity=${POLICY_IDENTITY}" >> "$GITHUB_OUTPUT" + - name: Resolve probe cache key + id: resolve-cache-key + env: + PROBE_CACHE_KEY: probe-claude-${{ steps.resolve-version.outputs.cli_version }}-${{ steps.resolve-policy.outputs.policy_identity }}-${{ runner.os }}-${{ hashFiles('src/autoskillit/**', 'tests/**', '.autoskillit/recipes/**', 'pyproject.toml', 'uv.lock', '.github/workflows/conformance-probes.yml', '!**/__pycache__/**') }} + run: echo "key=$PROBE_CACHE_KEY" >> "$GITHUB_OUTPUT" + - name: Restore probe cache id: restore-cache uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 with: path: .autoskillit/temp/probe-cache/claude - key: probe-claude-${{ steps.resolve-version.outputs.cli_version }}-${{ steps.resolve-policy.outputs.policy_identity }} + key: ${{ steps.resolve-cache-key.outputs.key }} - name: Run Claude Code conformance probes if: steps.restore-cache.outputs.cache-hit != 'true' @@ -300,7 +312,7 @@ jobs: uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 with: path: .autoskillit/temp/probe-cache/claude - key: probe-claude-${{ steps.resolve-version.outputs.cli_version }}-${{ steps.resolve-policy.outputs.policy_identity }} + key: ${{ steps.resolve-cache-key.outputs.key }} - name: Upload bounded startup-readiness traces if: always() diff --git a/src/autoskillit/cli/app.py b/src/autoskillit/cli/app.py index 6986f56283..6d55bc6d47 100644 --- a/src/autoskillit/cli/app.py +++ b/src/autoskillit/cli/app.py @@ -35,6 +35,7 @@ AuditAdmissionStoreAuthority, RecipeSource, atomic_write, + claude_code_project_dir, get_logger, ) @@ -344,6 +345,12 @@ def update() -> None: run_update_command() +@app.command +def claude_project_log_dir(cwd: str) -> None: + """Write the Claude project log directory for an explicit working directory.""" + sys.stdout.write(str(claude_code_project_dir(str(Path(cwd).resolve()))) + "\n") + + @app.command def doctor(*, output_json: bool = False, repair: bool = False) -> None: """Check project setup (``--repair`` opts into safe repairs).""" diff --git a/src/autoskillit/core/__init__.pyi b/src/autoskillit/core/__init__.pyi index db7ce56df5..21eb50dffd 100644 --- a/src/autoskillit/core/__init__.pyi +++ b/src/autoskillit/core/__init__.pyi @@ -148,6 +148,7 @@ from .io import fast_dumps as fast_dumps from .io import fast_loads as fast_loads from .io import find_latest_session_id as find_latest_session_id from .io import github_review_ledger_path as github_review_ledger_path +from .io import is_claude_session_log as is_claude_session_log from .io import is_generated_path as is_generated_path from .io import is_git_main_checkout as is_git_main_checkout from .io import is_git_worktree as is_git_worktree diff --git a/src/autoskillit/core/io/__init__.py b/src/autoskillit/core/io/__init__.py index ce4c3c9f64..c7b3a13891 100644 --- a/src/autoskillit/core/io/__init__.py +++ b/src/autoskillit/core/io/__init__.py @@ -80,6 +80,7 @@ destination_location, find_latest_session_id, github_review_ledger_path, + is_claude_session_log, is_generated_path, is_git_main_checkout, is_git_worktree, @@ -142,6 +143,7 @@ "fast_loads", "find_latest_session_id", "github_review_ledger_path", + "is_claude_session_log", "is_generated_path", "is_git_main_checkout", "is_git_worktree", diff --git a/src/autoskillit/core/io/paths.py b/src/autoskillit/core/io/paths.py index 8d1cbe8fb8..f980a5e6fa 100644 --- a/src/autoskillit/core/io/paths.py +++ b/src/autoskillit/core/io/paths.py @@ -126,10 +126,13 @@ def resolve_project_dir() -> Path: def claude_code_project_dir(cwd: str) -> Path: """Derive the Claude Code project log directory from a working directory path. - Encodes the cwd by replacing '/' and '_' with '-', matching Claude Code's - internal convention for ~/.claude/projects//. + Replace every UTF-16 unit outside ASCII letters and digits with a hyphen + under ~/.claude/projects/, matching the native JavaScript slug rule. """ - project_hash = cwd.replace("/", "-").replace("_", "-") + project_hash = "".join( + char if char.isascii() and char.isalnum() else "-" * (2 if ord(char) > 0xFFFF else 1) + for char in cwd + ) return Path.home() / ".claude" / "projects" / project_hash @@ -145,6 +148,11 @@ def claude_code_log_path(cwd: str, session_id: str) -> Path | None: return claude_code_project_dir(cwd) / f"{session_id}.jsonl" +def is_claude_session_log(path: Path) -> bool: + """Return whether a path names a session transcript rather than an orphaned copy.""" + return path.suffix == ".jsonl" and ".orphaned-" not in path.name + + def find_latest_session_id(cwd: str | None = None) -> str | None: """Return the session_id of the most recent Claude Code session for cwd. @@ -163,7 +171,7 @@ def find_latest_session_id(cwd: str | None = None) -> str | None: return None jsonl_files = sorted( - (f for f in project_dir.glob("*.jsonl")), + (f for f in project_dir.glob("*.jsonl") if is_claude_session_log(f)), key=lambda f: safe_mtime(f) or 0.0, reverse=True, ) diff --git a/src/autoskillit/execution/process/__init__.py b/src/autoskillit/execution/process/__init__.py index 33d15e4925..34e61ac96c 100644 --- a/src/autoskillit/execution/process/__init__.py +++ b/src/autoskillit/execution/process/__init__.py @@ -82,6 +82,7 @@ kill_process_tree, ) from autoskillit.execution.process._process_monitor import ( + SessionMonitorResult, _active_liveness_signals, _has_active_child_processes, _heartbeat, @@ -162,6 +163,7 @@ "OwnerScope", "OwnerScopeSealedError", "OwnerScopeSettlement", + "SessionMonitorResult", "TetherRecord", "TetherSpec", "TetherSweepOutcome", diff --git a/src/autoskillit/execution/process/_process_monitor.py b/src/autoskillit/execution/process/_process_monitor.py index fe4a820eac..cd0392b479 100644 --- a/src/autoskillit/execution/process/_process_monitor.py +++ b/src/autoskillit/execution/process/_process_monitor.py @@ -14,6 +14,7 @@ VANISHED_ERRORS, ChannelBStatus, get_logger, + is_claude_session_log, read_active_operation_leases, scan_observed, ) @@ -271,7 +272,7 @@ async def _discover_session_log( candidates = [ entry for entry in scan_observed(session_log_dir) - if entry.path.suffix == ".jsonl" and entry.status.st_ctime > spawn_time + if is_claude_session_log(entry.path) and entry.status.st_ctime > spawn_time ] session_file, session_entry = _select_session_log_file( session_log_dir, candidates, expected_session_id, resume_cursor diff --git a/src/autoskillit/skills_extended/audit-bugs/SKILL.md b/src/autoskillit/skills_extended/audit-bugs/SKILL.md index ce99a3cec0..47c5a7e10b 100644 --- a/src/autoskillit/skills_extended/audit-bugs/SKILL.md +++ b/src/autoskillit/skills_extended/audit-bugs/SKILL.md @@ -84,22 +84,18 @@ The user may provide a "since" date (e.g., `2/7`, `2026-02-07`, `last week`). If ### Step 1: Locate Project Logs -Claude Code stores conversation logs at `~/.claude/projects/` in a folder derived from the project's absolute path with `/` replaced by `-`. +Claude Code stores conversation logs under `~/.claude/projects/`. Resolve the directory for the current working directory through the installed CLI: -Derive the log directory: ```bash -# Convert current working directory to Claude's folder naming scheme -PROJECT_PATH=$(pwd) -LOG_DIR="$HOME/.claude/projects/-${PROJECT_PATH//\//-}" -# Remove leading double dash if present -LOG_DIR="${LOG_DIR//--/-}" +actual_absolute_cwd=$(pwd -P) +LOG_DIR=$(autoskillit claude-project-log-dir "$actual_absolute_cwd") ``` Verify the directory exists and contains `.jsonl` files. ### Step 2: Filter by Date and Investigate Skill -1. Use `find` with `-newermt` to filter `.jsonl` files modified since the target date +1. Use `find "$LOG_DIR"` with `-maxdepth 1`, `-name '*.jsonl'`, `! -name '*.orphaned-*.jsonl'`, and `-newermt` to filter session files modified since the target date, excluding orphaned copies 2. From those, `grep -l '"skill".*"investigate"'` to find files where the investigate skill was invoked (tool invocation pattern) 3. Also `grep -l '/autoskillit:investigate'` to catch user-typed invocations 4. Combine and deduplicate. Only use top-level files (not subagent logs under `*/subagents/`) diff --git a/src/autoskillit/skills_extended/audit-friction/SKILL.md b/src/autoskillit/skills_extended/audit-friction/SKILL.md index 1160271584..315d1b3c4c 100644 --- a/src/autoskillit/skills_extended/audit-friction/SKILL.md +++ b/src/autoskillit/skills_extended/audit-friction/SKILL.md @@ -139,12 +139,11 @@ For each match batch, use the bounded `-A 10 -B 10` command above to confirm it ### Step 1: Locate Project Logs -Derive the log directory from the current working directory: +Resolve the JSONL directory for the current working directory under `~/.claude/projects/` through the installed CLI: ```bash -PROJECT_PATH=$(pwd) -LOG_DIR="$HOME/.claude/projects/-${PROJECT_PATH//\//-}" -LOG_DIR="${LOG_DIR//--/-}" +actual_absolute_cwd=$(pwd -P) +LOG_DIR=$(autoskillit claude-project-log-dir "$actual_absolute_cwd") ``` Verify the directory exists and contains `.jsonl` files. If the directory is missing or empty, report this to the user before stopping. diff --git a/src/autoskillit/skills_extended/investigate/SKILL.md b/src/autoskillit/skills_extended/investigate/SKILL.md index 2695074276..c64745f88d 100644 --- a/src/autoskillit/skills_extended/investigate/SKILL.md +++ b/src/autoskillit/skills_extended/investigate/SKILL.md @@ -293,12 +293,11 @@ Before writing the report, check whether the root cause identified in Step 3 has #### Part A: Mine Past Investigation Logs -Derive the Claude project log directory from the current working directory: +Resolve the current project's JSONL directory under `~/.claude/projects/` through the installed CLI: ```bash -PROJECT_PATH=$(pwd) -LOG_DIR="$HOME/.claude/projects/-${PROJECT_PATH//\//-}" -LOG_DIR="${LOG_DIR//--/-}" +actual_absolute_cwd=$(pwd -P) +LOG_DIR=$(autoskillit claude-project-log-dir "$actual_absolute_cwd") ``` Search for `.jsonl` files containing prior AutoSkillit investigate-skill invocations, **excluding subagent log subdirectories** (`*/subagents/*`) so prior subagent conversations are not double-counted: diff --git a/src/autoskillit/skills_extended/setup-project/SKILL.md b/src/autoskillit/skills_extended/setup-project/SKILL.md index ce1c87c4a5..e5695b84ab 100644 --- a/src/autoskillit/skills_extended/setup-project/SKILL.md +++ b/src/autoskillit/skills_extended/setup-project/SKILL.md @@ -150,10 +150,15 @@ Launch parallel child delegations against `project_dir`. If the user opted into **Subagent F+ — Conversation History Mining (only if user opted in):** -Claude Code stores conversation history at `~/.claude/projects//` as JSONL files (one per session). The path encoding replaces `/` and `_` with `-` (e.g., `/home/user/my_project` -> `-home-user-my-project`). +Claude Code stores conversation history under `~/.claude/projects/` as JSONL files (one per session). Run this subagent with its tool cwd set to `project_dir`, then resolve that physical directory through the installed CLI: -1. Compute the encoded directory name from `project_dir` -2. List all `.jsonl` files in `~/.claude/projects//`, including subagent files in `/subagents/agent-*.jsonl` +```bash +actual_absolute_cwd=$(pwd -P) +LOG_DIR=$(autoskillit claude-project-log-dir "$actual_absolute_cwd") +``` + +1. Verify `LOG_DIR` exists and contains `.jsonl` files; report missing or empty history before stopping +2. List session files with `find "$LOG_DIR" -name '*.jsonl' ! -name '*.orphaned-*.jsonl'`, including subagent files in `{session-uuid}/subagents/agent-*.jsonl` and excluding orphaned copies 3. Sort by modification time (newest first) 4. Divide into batches of ~20-30 files per subagent — launch multiple parallel subagents if needed diff --git a/src/autoskillit/workspace/skill_capabilities/_scanner.py b/src/autoskillit/workspace/skill_capabilities/_scanner.py index 13c14b30b7..a975b98d49 100644 --- a/src/autoskillit/workspace/skill_capabilities/_scanner.py +++ b/src/autoskillit/workspace/skill_capabilities/_scanner.py @@ -21,7 +21,10 @@ from autoskillit.core import SKILL_CAPABILITY_REGISTRY _STATIC_PATTERNS: dict[str, tuple[re.Pattern[str], ...]] = { - "claude_dir": (re.compile(r"\.claude/"),), + "claude_dir": ( + re.compile(r"\.claude/"), + re.compile(r"\bautoskillit\s+claude-project-log-dir\b"), + ), "commit_files": (re.compile(r"\bcommit_files\s*\("),), "write_audit_semantic_result": (re.compile(r"\bwrite_audit_semantic_result\s*\("),), "write_standalone_audit_evidence": (re.compile(r"\bwrite_standalone_audit_evidence\s*\("),), diff --git a/tests/cli/test_claude_project_log_dir.py b/tests/cli/test_claude_project_log_dir.py new file mode 100644 index 0000000000..de0c8309b4 --- /dev/null +++ b/tests/cli/test_claude_project_log_dir.py @@ -0,0 +1,41 @@ +"""Tests for the side-effect-free Claude project-log directory command.""" + +from __future__ import annotations + +from pathlib import Path + +import pytest + +pytestmark = [pytest.mark.layer("cli"), pytest.mark.small] + + +@pytest.mark.parametrize( + ("cwd", "encoded"), + [ + ( + "/workspace/repro/.autoskillit/temp/sandbox", + "-workspace-repro--autoskillit-temp-sandbox", + ), + ("/home/user/projects/my_project", "-home-user-projects-my-project"), + ("/home/user/projects/my_project/", "-home-user-projects-my-project"), + ("/home/user/projects/my_project/.", "-home-user-projects-my-project"), + ("workspace/repro/.", "-workspace-repro"), + ], +) +def test_claude_project_log_dir_prints_one_exact_path_without_mcp_activation( + cwd: str, + encoded: str, + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + home = tmp_path / "home" + monkeypatch.setattr(Path, "home", lambda: home) + monkeypatch.chdir("/") + from autoskillit.cli.app import app + + with pytest.raises(SystemExit) as exc_info: + app(["claude-project-log-dir", cwd]) + assert exc_info.value.code == 0 + + assert capsys.readouterr().out == f"{home / '.claude' / 'projects' / encoded}\n" diff --git a/tests/contracts/test_skill_semantic_authenticity.py b/tests/contracts/test_skill_semantic_authenticity.py index fb0e26bc78..abd45909a8 100644 --- a/tests/contracts/test_skill_semantic_authenticity.py +++ b/tests/contracts/test_skill_semantic_authenticity.py @@ -9,6 +9,22 @@ pytestmark = [pytest.mark.layer("contracts"), pytest.mark.small] +@pytest.mark.parametrize( + ("heading", "expected"), + [("## Step 1", frozenset({"claude_dir"})), ("## Examples", frozenset())], +) +def test_project_log_cli_requires_claude_dir_only_in_executable_instructions( + heading: str, expected: frozenset[str] +) -> None: + from autoskillit.workspace.skill_capabilities import detect_skill_capabilities + + body = ( + f"{heading}\n```bash\n" + 'LOG_DIR=$(autoskillit claude-project-log-dir "$actual_absolute_cwd")\n```\n' + ) + assert detect_skill_capabilities(body, "log-mining") == expected + + def _parsed_body(path: Path) -> str: from autoskillit.workspace import read_skill_frontmatter diff --git a/tests/core/test_paths.py b/tests/core/test_paths.py index 355397c5f3..6807469d82 100644 --- a/tests/core/test_paths.py +++ b/tests/core/test_paths.py @@ -45,13 +45,23 @@ def test_not_in_git_repo_returns_false(self, tmp_path: Path) -> None: class TestClaudeCodeLogPath: - def test_claude_code_log_path_with_real_session_id(self) -> None: + def test_claude_code_log_path_with_real_session_id( + self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path + ) -> None: + monkeypatch.setattr(Path, "home", lambda: tmp_path) from autoskillit.core.paths import claude_code_log_path - result = claude_code_log_path(cwd="/home/user/my_project", session_id="abc-123") + result = claude_code_log_path( + cwd="/workspace/repro/.autoskillit/temp/sandbox", + session_id="native-session", + ) assert ( result - == Path.home() / ".claude" / "projects" / "-home-user-my-project" / "abc-123.jsonl" + == tmp_path + / ".claude" + / "projects" + / "-workspace-repro--autoskillit-temp-sandbox" + / "native-session.jsonl" ) def test_claude_code_log_path_with_fallback_session_id(self) -> None: @@ -76,11 +86,31 @@ def test_claude_code_log_path_with_empty_session_id(self) -> None: result = claude_code_log_path(cwd="/home/user/project", session_id="") assert result is None - def test_claude_code_project_dir_encoding(self) -> None: + @pytest.mark.parametrize( + ("cwd", "encoded"), + [ + ("/home/user/projects/my_project", "-home-user-projects-my-project"), + ( + "/workspace/repro/.autoskillit/temp/sandbox", + "-workspace-repro--autoskillit-temp-sandbox", + ), + ("/workspace/repro/project.with.dot", "-workspace-repro-project-with-dot"), + ("/workspace/A b+C@dé", "-workspace-A-b-C-d-"), + ("/workspace/a😀b", "-workspace-a--b"), + ], + ) + def test_claude_code_project_dir_encoding( + self, + monkeypatch: pytest.MonkeyPatch, + tmp_path: Path, + cwd: str, + encoded: str, + ) -> None: + monkeypatch.setattr(Path, "home", lambda: tmp_path) from autoskillit.core.paths import claude_code_project_dir - result = claude_code_project_dir("/home/user/my_project") - assert result == Path.home() / ".claude" / "projects" / "-home-user-my-project" + result = claude_code_project_dir(cwd) + assert result == tmp_path / ".claude" / "projects" / encoded class TestFindLatestSessionId: @@ -141,6 +171,43 @@ def test_uses_cwd_when_none(self, tmp_path: Path) -> None: result = find_latest_session_id() # cwd=None → uses Path.cwd() assert result == "ccc333" + def test_ignores_newer_orphaned_transcript( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + import os + + monkeypatch.setattr(Path, "home", lambda: tmp_path) + from autoskillit.core.paths import claude_code_project_dir, find_latest_session_id + + cwd = "/workspace/repro/.autoskillit/temp/sandbox" + project_dir = claude_code_project_dir(cwd) + project_dir.mkdir(parents=True) + native = project_dir / "native-session.jsonl" + orphaned = project_dir / "native-session.orphaned-2000-deadbeef.jsonl" + native.write_text("{}") + orphaned.write_text("{}") + os.utime(native, (1_000.0, 1_000.0)) + os.utime(orphaned, (2_000.0, 2_000.0)) + + assert find_latest_session_id(cwd) == "native-session" + + def test_returns_none_when_only_orphaned_transcripts_exist( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + import os + + monkeypatch.setattr(Path, "home", lambda: tmp_path) + from autoskillit.core.paths import claude_code_project_dir, find_latest_session_id + + cwd = "/workspace/project.with.dot" + project_dir = claude_code_project_dir(cwd) + project_dir.mkdir(parents=True) + orphaned = project_dir / "session.orphaned-3000-deadbeef.jsonl" + orphaned.write_text("{}") + os.utime(orphaned, (3_000.0, 3_000.0)) + + assert find_latest_session_id(cwd) is None + class TestPkgRoot: def test_pkg_root_matches_importlib_resources(self) -> None: diff --git a/tests/execution/backends/test_cli_conformance_probes.py b/tests/execution/backends/test_cli_conformance_probes.py index 5d3211080f..b288eb9716 100644 --- a/tests/execution/backends/test_cli_conformance_probes.py +++ b/tests/execution/backends/test_cli_conformance_probes.py @@ -22,8 +22,9 @@ import threading import time import tomllib +import uuid from collections.abc import Callable -from contextlib import asynccontextmanager +from contextlib import asynccontextmanager, suppress from dataclasses import replace from datetime import UTC, datetime from pathlib import Path @@ -45,12 +46,14 @@ MANAGED_JOIN_PARENT_ID_ENV_VAR, OUTPUT_DISCIPLINE_DIGEST, RESPONSE_BACKSTOP_EXEMPTION_REGISTRY, + ChannelBStatus, DefaultManagedWorkerCapacity, OutputFormat, SemanticAdaptationContext, SkillContractError, SkillSemanticAdaptationResult, SkillSemanticPlan, + SubprocessResult, WriteBehaviorSpec, agent_definition_digest, load_agent_definitions, @@ -100,7 +103,11 @@ from autoskillit.execution.backends.claude import ClaudeCodeBackend, ClaudeStreamParser from autoskillit.execution.backends.codex import CodexBackend from autoskillit.execution.process import ( + EventCursor, + SessionMonitorResult, TetherSpec, + _jsonl_contains_marker, + _process_race, kill_process_tree, run_managed_async, spawn_owned_process, @@ -155,6 +162,7 @@ ) from autoskillit.workspace import AgentSkillDocument, DefaultSkillResolver from tests._codex_feature_policy import RETIRED_CODEX_FEATURES +from tests._realistic_project import PINNED_CLAUDE_SHIM_VERSION_OUTPUT from tests.execution._process_group_helpers import _cleanup_owned_process_group from tests.execution.backends._codex_fixtures import installed_catalog from tests.execution.backends._conformance_assertions import ( @@ -3631,6 +3639,232 @@ def test_claude_startup_readiness_addressability_trace( ) +def _provision_claude_transcript_home(source_home: Path, isolated_home: Path, cwd: Path) -> Path: + claude_home = isolated_home / ".claude" + claude_home.mkdir(parents=True) + + source_state = source_home / ".claude.json" + claude_state: dict[str, object] = {} + if source_state.is_file(): + claude_state = json.loads(source_state.read_text(encoding="utf-8")) + claude_state.update( + { + "autoUpdates": False, + "hasCompletedOnboarding": True, + "lastOnboardingVersion": CLAUDE_CODE_CAPABILITIES.min_version, + "theme": "dark", + } + ) + projects = claude_state.setdefault("projects", {}) + assert isinstance(projects, dict) + projects[str(cwd)] = {"hasTrustDialogAccepted": True} + (isolated_home / ".claude.json").write_text( + json.dumps(claude_state, sort_keys=True), encoding="utf-8" + ) + (claude_home / "settings.json").write_text( + json.dumps( + { + "env": {"DISABLE_AUTOUPDATER": "1"}, + "skipDangerousModePermissionPrompt": True, + "theme": "dark", + }, + sort_keys=True, + ), + encoding="utf-8", + ) + source_credentials = source_home / ".claude" / ".credentials.json" + if source_credentials.is_file(): + shutil.copy2(source_credentials, claude_home / ".credentials.json") + + return claude_home + + +@_skip_unless_claude_startup_smoke +@pytest.mark.timeout(180) +@pytest.mark.parametrize( + "cwd_suffix", + [ + pytest.param(Path("project/.autoskillit/temp/probe"), id="dotted-autoskillit"), + pytest.param(Path("project/control"), id="dot-free-control"), + ], +) +def test_claude_transcript_path_matches_native_cli( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + cwd_suffix: Path, +) -> None: + import anyio + + source_home = Path.home() + configured_executable = os.environ.get("CLAUDE_CODE_EXECPATH") + executable = Path(configured_executable or shutil.which("claude") or "").resolve(strict=True) + isolated_home = tmp_path / "home" + cwd = (tmp_path / cwd_suffix).resolve() + cwd.mkdir(parents=True) + + claude_home = _provision_claude_transcript_home(source_home, isolated_home, cwd) + + monkeypatch.setenv("HOME", str(isolated_home)) + monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(claude_home)) + monkeypatch.setenv("CLAUDE_CODE_EXECPATH", str(executable)) + monkeypatch.setenv("XDG_CONFIG_HOME", str(isolated_home / ".config")) + monkeypatch.setenv("XDG_DATA_HOME", str(isolated_home / ".local" / "share")) + + backend = ClaudeCodeBackend() + locator = backend.session_locator() + session_log_dir = locator.project_log_dir(str(cwd)) + assert session_log_dir.parent == claude_home / "projects" + session_log_dir.mkdir(parents=True) + synchronization_dir = cwd / ".autoskillit" / "temp" + synchronization_dir.mkdir(parents=True, exist_ok=True) + ready_path = synchronization_dir / "selection-ready" + acknowledgement_path = synchronization_dir / "completion-acknowledged" + marker = f"AUTOSKILLIT_CLAUDE_TRANSCRIPT_{uuid.uuid4().hex}" + wait_for_ready = ( + f"for attempt in $(seq 1 400); do test -f {shlex.quote(str(ready_path))} " + "&& exit 0; sleep 0.05; done; exit 1" + ) + wait_for_acknowledgement = ( + "for attempt in $(seq 1 400); do test -f " + f"{shlex.quote(str(acknowledgement_path))} && exit 0; " + "sleep 0.05; done; exit 1" + ) + prompt = ( + "Call Bash exactly once with this command and wait for it to finish:\n" + f"{wait_for_ready}\n" + "After it succeeds, emit an assistant text block containing exactly this standalone " + f"line:\n{marker}\n" + "Then immediately call Bash exactly once with this command and wait for it to finish:\n" + f"{wait_for_acknowledgement}\n" + "After it succeeds, return a short success result without repeating the marker." + ) + + original_monitor = _process_race._session_log_monitor + selected_paths: list[Path] = [] + completed_monitors: list[SessionMonitorResult] = [] + evidence = { + "schema_version": 1, + "binary": str(executable), + "cli_version": "unverified", + "cwd": str(cwd), + "native_session_id": None, + "native_path": None, + "selected_path": None, + "completion_captured": False, + "status": "failed", + } + + async def monitored_session_log(*args, **kwargs) -> SessionMonitorResult: + on_session_file_selected = kwargs.get("on_session_file_selected") + on_poll = kwargs.get("_on_poll") + + def selected(path: Path, cursor: EventCursor) -> None: + if on_session_file_selected is not None: + on_session_file_selected(path, cursor) + selected_paths.append(path) + evidence["selected_path"] = str(path) + + def after_poll() -> None: + if on_poll is not None: + on_poll() + if selected_paths and not ready_path.exists(): + ready_path.touch() + + kwargs["_on_poll"] = after_poll + kwargs["on_session_file_selected"] = selected + monitor_result = await original_monitor(*args, **kwargs) + if monitor_result.status is ChannelBStatus.COMPLETION: + completed_monitors.append(monitor_result) + evidence["completion_captured"] = True + acknowledgement_path.touch() + return monitor_result + + monkeypatch.setattr(_process_race, "_session_log_monitor", monitored_session_log) + + evidence_dir = ( + Path(__file__).resolve().parents[3] / ".autoskillit" / "temp" / "claude-startup-readiness" + ) + evidence_path = evidence_dir / f"transcript-path-{time.time_ns()}-{uuid.uuid4().hex}.json" + try: + version_result = subprocess.run( + [str(executable), "--version"], + check=True, + capture_output=True, + text=True, + timeout=10, + env=dict(os.environ), + ) + cli_version = version_result.stdout.strip() + evidence["cli_version"] = cli_version + assert cli_version == PINNED_CLAUDE_SHIM_VERSION_OUTPUT + + spec = backend.build_skill_session_cmd( + prompt, + cwd=str(cwd), + completion_marker=marker, + output_format=OutputFormat.STREAM_JSON, + ) + spec = replace(spec, cmd=(str(executable), *spec.cmd[1:])) + assert spec.env["CLAUDE_CODE_EXECPATH"] == str(executable) + assert spec.env["CLAUDE_CONFIG_DIR"] == str(claude_home) + assert spec.env["HOME"] == str(isolated_home) + + async def run_probe() -> SubprocessResult: + return await run_managed_async( + list(spec.cmd), + cwd=cwd, + timeout=150, + env=spec.env, + pass_fds=spec.inherited_fds, + completion_marker=marker, + completion_record_types=backend.capabilities.completion_record_types, + session_log_dir=session_log_dir, + session_record_types=backend.capabilities.session_record_types, + stream_parser=backend.stream_parser(marker), + lifecycle_observation_enabled=True, + _phase1_poll=0.05, + _phase2_poll=0.05, + _heartbeat_poll=0.05, + ) + + result = anyio.run(run_probe) + evidence["native_session_id"] = result.session_id + evidence["channel_confirmation"] = str(result.channel_confirmation) + assert result.returncode == 0, result.stderr[-2_000:] + assert len(completed_monitors) == 1 + assert completed_monitors[0].status is ChannelBStatus.COMPLETION + assert completed_monitors[0].session_id == result.channel_b_session_id + assert result.channel_b_session_id == result.session_id + uuid.UUID(result.session_id) + native_files = sorted((claude_home / "projects").glob(f"*/{result.session_id}.jsonl")) + assert len(native_files) == 1 + native_path = native_files[0].resolve() + evidence["native_path"] = str(native_path) + native_session_id = native_path.stem + assert len(selected_paths) == 1 + selected_path = selected_paths[0].resolve() + assert selected_path == native_path + assert result.session_id == native_session_id + assert locator.session_log_path(str(cwd), native_session_id) == native_path + assert locator.locate_session(native_session_id) == native_path + + transcript_records = _read_ndjson(native_path) + assert any(record.get("cwd") == str(cwd) for record in transcript_records) + transcript = native_path.read_text(encoding="utf-8") + assert _jsonl_contains_marker(transcript, marker, frozenset({"assistant"})) + assert ready_path.is_file() + assert acknowledgement_path.is_file() + evidence["status"] = "passed" + finally: + suppressed_errors = (OSError,) if sys.exc_info()[0] is not None else () + with suppress(*suppressed_errors): + evidence_dir.mkdir(parents=True, exist_ok=True) + evidence_path.write_text( + json.dumps(evidence, indent=2, sort_keys=True) + "\n", + encoding="utf-8", + ) + + @_skip_unless_claude_startup_smoke @pytest.mark.timeout(240) def test_installed_claude_delivery_measurement(tmp_path: Path) -> None: diff --git a/tests/execution/test_process_session_log_monitor.py b/tests/execution/test_process_session_log_monitor.py index 20cb725285..ddc40ee5fc 100644 --- a/tests/execution/test_process_session_log_monitor.py +++ b/tests/execution/test_process_session_log_monitor.py @@ -782,6 +782,125 @@ async def run_second_monitor() -> None: assert selected == [expected_file, competing_file] +class TestClaudeProjectDirectoryMonitor: + @pytest.mark.anyio + async def test_real_locator_monitors_dotted_cwd_transcript_after_initial_eof( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + home = tmp_path / "home" + monkeypatch.setattr(Path, "home", lambda: home) + from autoskillit.execution.backends._claude.session_locator import ( + ClaudeSessionLocator, + ) + + cwd_prefix = tmp_path / "workspace" / "repro" + cwd = cwd_prefix / ".autoskillit" / "temp" / "sandbox" + cwd.mkdir(parents=True) + locator = ClaudeSessionLocator() + located_dir = locator.project_log_dir(str(cwd)) + located_dir.mkdir(parents=True) + + assert "." not in str(cwd_prefix) + producer_prefix = str(cwd_prefix).replace("/", "-").replace("_", "-") + producer_dir = ( + home / ".claude" / "projects" / (f"{producer_prefix}--autoskillit-temp-sandbox") + ) + producer_dir.mkdir(parents=True, exist_ok=True) + native_session_id = "6427e502-e026-4c71-a420-eeb03e0dc3cd" + transcript = producer_dir / f"{native_session_id}.jsonl" + spawn_time = time.time() - 1 + transcript.write_text( + json.dumps( + { + "type": "user", + "cwd": str(cwd), + "sessionId": native_session_id, + "message": {"content": "initial"}, + } + ) + + "\n" + ) + selected: list[Path] = [] + marker_written = False + + def append_marker_after_initial_eof() -> None: + nonlocal marker_written + if marker_written: + return + marker_written = True + with transcript.open("a") as stream: + stream.write( + json.dumps( + { + "type": "assistant", + "message": {"content": "done\n\nMARKER"}, + } + ) + + "\n" + ) + + with anyio.fail_after(1.0): + result = await _session_log_monitor( + located_dir, + "MARKER", + stale_threshold=0.25, + spawn_time=spawn_time, + expected_session_id=native_session_id, + _phase1_poll=0.005, + _phase2_poll=0.005, + _phase1_timeout=0.25, + _on_poll=append_marker_after_initial_eof, + on_session_file_selected=lambda path, _cursor: selected.append(path), + ) + + assert result.status is ChannelBStatus.COMPLETION + assert result.session_id == native_session_id + assert selected == [transcript] + assert json.loads(selected[0].read_text().splitlines()[0])["cwd"] == str(cwd) + assert located_dir == producer_dir + assert locator.session_log_path(str(cwd), native_session_id) == transcript + from autoskillit.core import claude_code_log_path + + assert claude_code_log_path(str(cwd), native_session_id) == transcript + + @pytest.mark.anyio + async def test_existing_directory_with_only_orphaned_transcript_stays_unselected( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ) -> None: + home = tmp_path / "home" + monkeypatch.setattr(Path, "home", lambda: home) + from autoskillit.execution.backends._claude.session_locator import ( + ClaudeSessionLocator, + ) + + cwd = tmp_path / "workspace" / "project.with.dot" + cwd.mkdir(parents=True) + session_log_dir = ClaudeSessionLocator().project_log_dir(str(cwd)) + session_log_dir.mkdir(parents=True) + spawn_time = time.time() - 1 + orphaned = session_log_dir / "native.orphaned-2000-deadbeef.jsonl" + orphaned.write_text( + json.dumps({"type": "assistant", "message": {"content": "MARKER"}}) + "\n" + ) + selected: list[Path] = [] + + with anyio.fail_after(1.0): + result = await _session_log_monitor( + session_log_dir, + "MARKER", + stale_threshold=0.1, + spawn_time=spawn_time, + _phase1_poll=0.005, + _phase2_poll=0.005, + _phase1_timeout=0.05, + on_session_file_selected=lambda path, _cursor: selected.append(path), + ) + + assert result.status is ChannelBStatus.STALE + assert result.session_id == "" + assert selected == [] + + class TestSessionLogMonitorDirMissing: """DIR_MISSING is reserved for a missing session-log directory.""" diff --git a/tests/execution/test_process_submodules.py b/tests/execution/test_process_submodules.py index 95cafe6211..900538ae91 100644 --- a/tests/execution/test_process_submodules.py +++ b/tests/execution/test_process_submodules.py @@ -36,6 +36,7 @@ "OwnerScope", "OwnerScopeSealedError", "OwnerScopeSettlement", + "SessionMonitorResult", "TetherRecord", "TetherSpec", "TetherSweepOutcome", diff --git a/tests/infra/test_conformance_probes_workflow.py b/tests/infra/test_conformance_probes_workflow.py index 0d80b236b7..497ecc0913 100644 --- a/tests/infra/test_conformance_probes_workflow.py +++ b/tests/infra/test_conformance_probes_workflow.py @@ -3,17 +3,28 @@ from __future__ import annotations import re +from fnmatch import fnmatchcase from pathlib import Path import pytest from autoskillit.core.io import load_yaml +from tests._git_inventory import git_ls_files -pytestmark = [pytest.mark.layer("infra"), pytest.mark.small] +pytestmark = [pytest.mark.layer("infra"), pytest.mark.medium] _WORKFLOW_PATH = ( Path(__file__).resolve().parents[2] / ".github" / "workflows" / "conformance-probes.yml" ) +_REPO_ROOT = _WORKFLOW_PATH.parents[2] + + +def _probe_cache_inputs(expression: str) -> tuple[str, ...]: + match = re.search(r"hashFiles\(([^)]+)\)", expression) + assert match is not None + inputs = tuple(re.findall(r"'([^']+)'", match.group(1))) + assert inputs + return inputs def _on(workflow: dict) -> dict: @@ -285,52 +296,84 @@ def test_exports_output_discipline_policy_identity( assert "policy_identity=${POLICY_IDENTITY}" in run assert "GITHUB_OUTPUT" in run - @pytest.mark.parametrize("job_name", ["codex-probe", "claude-probe"]) - def test_cache_restore_step(self, workflow: dict, job_name: str) -> None: - steps = workflow["jobs"][job_name]["steps"] - restore_steps = [s for s in steps if "cache/restore" in (s.get("uses") or "")] - assert len(restore_steps) >= 1, f"No cache restore step in {job_name}" - - @pytest.mark.parametrize("job_name", ["codex-probe", "claude-probe"]) - def test_cache_save_step(self, workflow: dict, job_name: str) -> None: - steps = workflow["jobs"][job_name]["steps"] - save_steps = [s for s in steps if "cache/save" in (s.get("uses") or "")] - assert len(save_steps) >= 1, f"No cache save step in {job_name}" - - @pytest.mark.parametrize("job_name", ["codex-probe", "claude-probe"]) - def test_probe_gated_on_cache_miss(self, workflow: dict, job_name: str) -> None: - steps = workflow["jobs"][job_name]["steps"] - gated_steps = [s for s in steps if "cache-hit" in (s.get("if") or "")] - assert len(gated_steps) >= 1, f"No cache-gated probe step in {job_name}" - @pytest.mark.parametrize( - ("job_name", "expected_backend"), - [("codex-probe", "codex"), ("claude-probe", "claude")], + ("job_name", "backend", "probe_step_name"), + [ + ("codex-probe", "codex", "Run Codex conformance probes"), + ("claude-probe", "claude", "Run Claude Code conformance probes"), + ], ) - def test_cache_key_contains_backend( - self, workflow: dict, job_name: str, expected_backend: str + def test_cache_is_exact_and_invalidated_by_all_probe_inputs( + self, workflow: dict, job_name: str, backend: str, probe_step_name: str ) -> None: steps = workflow["jobs"][job_name]["steps"] - restore_steps = [s for s in steps if "cache/restore" in (s.get("uses") or "")] - assert restore_steps, f"No cache/restore step found in {job_name}" - for step in restore_steps: - key = step.get("with", {}).get("key", "") - assert f"probe-{expected_backend}" in key + checkout_index = next( + index + for index, step in enumerate(steps) + if (step.get("uses") or "").startswith("actions/checkout@") + ) + restore_steps = [ + (index, step) + for index, step in enumerate(steps) + if "cache/restore" in (step.get("uses") or "") + ] + save_steps = [step for step in steps if "cache/save" in (step.get("uses") or "")] + assert len(restore_steps) == 1 + assert len(save_steps) == 1 + restore_index, restore = restore_steps[0] + save = save_steps[0] + probe = next(step for step in steps if step.get("name") == probe_step_name) + + key_prefix = ( + f"probe-{backend}-${{{{ steps.resolve-version.outputs.cli_version }}}}-" + "${{ steps.resolve-policy.outputs.policy_identity }}-${{ runner.os }}-" + ) + key_index, key_step = next( + (index, step) + for index, step in enumerate(steps) + if step.get("id") == "resolve-cache-key" + ) + key = key_step["env"]["PROBE_CACHE_KEY"] + cache_inputs = _probe_cache_inputs(key) + expected_cache_inputs = { + "path": f".autoskillit/temp/probe-cache/{backend}", + "key": "${{ steps.resolve-cache-key.outputs.key }}", + } + + assert checkout_index < key_index < restore_index + assert key.startswith(key_prefix) + assert key_step["run"] == 'echo "key=$PROBE_CACHE_KEY" >> "$GITHUB_OUTPUT"' + assert restore["with"] == expected_cache_inputs + assert save["with"] == expected_cache_inputs + targets = re.findall(r"\btests/[\w/.-]+\.py\b", probe["run"]) + assert targets + assert all( + any(fnmatchcase(target, pattern) for pattern in cache_inputs) for target in targets + ) + assert { + "src/autoskillit/**", + "tests/**", + ".autoskillit/recipes/**", + "pyproject.toml", + "uv.lock", + ".github/workflows/conformance-probes.yml", + "!**/__pycache__/**", + } <= set(cache_inputs) + assert probe["if"] == "steps.restore-cache.outputs.cache-hit != 'true'" + assert save["if"] == "success() && steps.restore-cache.outputs.cache-hit != 'true'" @pytest.mark.parametrize("job_name", ["codex-probe", "claude-probe"]) - def test_restore_and_save_keys_include_policy_identity( + def test_cache_hash_inputs_match_tracked_repository_files( self, workflow: dict, job_name: str ) -> None: - steps = workflow["jobs"][job_name]["steps"] - cache_steps = [ - s - for s in steps - if "cache/restore" in (s.get("uses") or "") or "cache/save" in (s.get("uses") or "") - ] - assert len(cache_steps) == 2 - for step in cache_steps: - key = step.get("with", {}).get("key", "") - assert "steps.resolve-policy.outputs.policy_identity" in key + key_step = next( + step + for step in workflow["jobs"][job_name]["steps"] + if step.get("id") == "resolve-cache-key" + ) + for pathspec in _probe_cache_inputs(key_step["env"]["PROBE_CACHE_KEY"]): + if not pathspec.startswith("!"): + assert git_ls_files(_REPO_ROOT, pathspec), pathspec class TestOutputBudgetE2E: diff --git a/tests/server/test_session_log_tools.py b/tests/server/test_session_log_tools.py index bf6e70b39b..405134964b 100644 --- a/tests/server/test_session_log_tools.py +++ b/tests/server/test_session_log_tools.py @@ -48,6 +48,8 @@ async def test_inspection_does_not_require_an_open_kitchen(tool_ctx, retained_lo @pytest.fixture def retained_logs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> dict[str, Path]: + home = tmp_path / "home" + monkeypatch.setattr(Path, "home", lambda: home) log_root = tmp_path / "logs" session_dir = log_root / "sessions" / "session-one" session_dir.mkdir(parents=True) @@ -55,8 +57,19 @@ def retained_logs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> dict[str, anomalies = session_dir / "anomalies.jsonl" audit = session_dir / "audit_log.json" turn_usage = session_dir / "turn_usage.jsonl" - transcript = tmp_path / "transcripts" / "session-one.jsonl" - transcript.parent.mkdir() + cwd_prefix = tmp_path / "workspace" / "repro" + cwd = cwd_prefix / ".autoskillit" / "temp" / "sandbox" + cwd.mkdir(parents=True) + assert "." not in str(cwd_prefix) + encoded_prefix = str(cwd_prefix).replace("/", "-").replace("_", "-") + transcript = ( + home + / ".claude" + / "projects" + / f"{encoded_prefix}--autoskillit-temp-sandbox" + / "session-one.jsonl" + ) + transcript.parent.mkdir(parents=True) summary.write_text('{"needs_retry":true,"retry_reason":"early_stop"}\n') anomalies.write_text('{"kind":"retry"}\n{"kind":"error"}\n') audit.write_text('{"subtype":"missing_completion_marker"}\n') @@ -70,7 +83,7 @@ def retained_logs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> dict[str, row = { "session_id": "session-one", "dir_name": "session-one", - "cwd": str(tmp_path / "project"), + "cwd": str(cwd), "backend": "claude-code", "claude_code_log": str(transcript), "codex_log": None, @@ -99,6 +112,8 @@ def retained_logs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> dict[str, "audit": audit, "turn_usage": turn_usage, "transcript": transcript, + "cwd": cwd, + "home": home, } @@ -198,6 +213,46 @@ async def test_codex_transcript_resolves_through_backend_locator( assert result["content"] == '{"event":"turn.failed"}\n' +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("stored_path_kind", "expected_status"), + [("canonical", "answered"), ("pre-repair", "transcript_identity_mismatch")], +) +async def test_claude_transcript_stored_path_must_match_real_dotted_cwd_helper( + retained_logs, + monkeypatch: pytest.MonkeyPatch, + stored_path_kind: str, + expected_status: str, +) -> None: + from autoskillit.core.paths import claude_code_log_path + + monkeypatch.setattr(session_logs, "claude_code_log_path", claude_code_log_path) + canonical = claude_code_log_path(str(retained_logs["cwd"]), "session-one") + assert canonical == retained_logs["transcript"] + if stored_path_kind == "canonical": + stored_path = canonical + else: + pre_repair_name = str(retained_logs["cwd"]).replace("/", "-").replace("_", "-") + stored_path = ( + retained_logs["home"] / ".claude" / "projects" / pre_repair_name / "session-one.jsonl" + ) + _update_index(retained_logs, claude_code_log=str(stored_path)) + + result = json.loads( + await session_logs.inspect_session_logs( + operation="read", + session_id="session-one", + artifact="transcript", + ) + ) + + if expected_status == "answered": + assert result["status"] == "answered" + assert result["content"] == '{"event":"turn.failed"}\n' + else: + assert result["reason"] == expected_status + + @pytest.mark.asyncio @pytest.mark.parametrize("backend", ["claude-code", "codex"]) async def test_transcript_locator_mismatch_is_rejected( diff --git a/tests/server/test_tools_execution_command.py b/tests/server/test_tools_execution_command.py index 48367ffae1..1c0e9c922d 100644 --- a/tests/server/test_tools_execution_command.py +++ b/tests/server/test_tools_execution_command.py @@ -452,19 +452,39 @@ async def _capture_marker(_marker_dir, session_id, _label): @pytest.mark.anyio async def test_run_skill_attestation_stays_live_without_l1_marker_key( - self, tool_ctx_kitchen_open, monkeypatch + self, tool_ctx_kitchen_open, monkeypatch, tmp_path: Path ): from autoskillit.core import LAUNCH_ID_ENV_VAR from autoskillit.core.pipeline._execution_marker import execution_marker from autoskillit.hooks.guards.fabricated_completion_guard import ( _has_fresh_matching_marker, ) + from autoskillit.pipeline.exploration_context import OwnerBoundExplorationContextStore + from autoskillit.server._exploration_service import DefaultExplorationService from tests.fakes import MockSubprocessRunner + home = tmp_path / "home" + caller_prefix = tmp_path / "caller" + caller_cwd = caller_prefix / "project.with.dot" + child_cwd = Path("/tmp") + caller_cwd.mkdir(parents=True) + monkeypatch.setattr(Path, "home", lambda: home) + monkeypatch.setattr(tool_ctx_kitchen_open, "project_dir", caller_cwd) monkeypatch.delenv(LAUNCH_ID_ENV_VAR, raising=False) monkeypatch.setattr( tools_execution, "find_caller_session_id", lambda **_kwargs: "session-a" ) + assert "." not in str(caller_prefix) + encoded_prefix = str(caller_prefix).replace("/", "-").replace("_", "-") + actual_transcript = ( + home + / ".claude" + / "projects" + / f"{encoded_prefix}-project-with-dot" + / "session-a.jsonl" + ) + actual_transcript.parent.mkdir(parents=True) + actual_transcript.write_text('{"type":"assistant","message":{"content":"caller"}}\n') marker_state: dict[str, object] = {} @contextlib.asynccontextmanager @@ -488,22 +508,41 @@ async def check_marker_during_launch(cmd, **kwargs): marker_payload = json.loads(marker_path.read_text(encoding="utf-8")) assert marker_payload["label"] == "run-skill" assert marker_payload["session_id"] == session_id - assert _has_fresh_matching_marker(marker_path, session_id) + assert marker_path.parent == actual_transcript.parent + assert _has_fresh_matching_marker(actual_transcript, session_id) return await runner(cmd, **kwargs) monkeypatch.setattr(tool_ctx_kitchen_open, "runner", check_marker_during_launch) runner.push(_make_result(returncode=1)) # clone guard snapshot runner.push(_make_result(returncode=0, stdout=_SUCCESS_JSON)) - payload = json.loads(await run_skill("/investigate marker attestation", cwd="/tmp")) - _ack_direct_run_skill_result(tool_ctx_kitchen_open, payload) + with OwnerBoundExplorationContextStore( + trusted_root=caller_cwd, service=DefaultExplorationService() + ) as exploration_store: + monkeypatch.setattr( + tool_ctx_kitchen_open, "exploration_context_store", exploration_store + ) + payload = json.loads( + await run_skill("/investigate marker attestation", cwd=str(child_cwd)) + ) + _ack_direct_run_skill_result(tool_ctx_kitchen_open, payload) claude_calls = [ call for call in runner.call_args_list if Path(call[0][0]).name == "claude" ] - assert len(claude_calls) == 1 + if len(claude_calls) != 1: + pytest.fail( + "run_skill response before Claude launch:\n" + json.dumps(payload, indent=2), + pytrace=False, + ) + assert claude_calls[0][1] == child_cwd.resolve() assert "marker_dir" not in claude_calls[0][3] assert "session_id" not in claude_calls[0][3] + marker_path = marker_state["path"] + assert isinstance(marker_path, Path) + assert marker_path.parent == actual_transcript.parent + assert not marker_path.exists() + assert not _has_fresh_matching_marker(actual_transcript, "session-a") @pytest.mark.anyio async def test_marker_dir_routes_through_session_locator( diff --git a/tests/skills/test_claude_project_log_resolution_contracts.py b/tests/skills/test_claude_project_log_resolution_contracts.py new file mode 100644 index 0000000000..2ddf010b61 --- /dev/null +++ b/tests/skills/test_claude_project_log_resolution_contracts.py @@ -0,0 +1,50 @@ +"""Contracts for skills that discover Claude project JSONL logs.""" + +from __future__ import annotations + +import pytest + +from autoskillit.workspace.skills import DefaultSkillResolver + +pytestmark = [pytest.mark.layer("skills"), pytest.mark.medium] + +_LOG_DIR_COMMAND = 'LOG_DIR=$(autoskillit claude-project-log-dir "$actual_absolute_cwd")' +_SKILL_SECTIONS = ( + ("investigate", "#### Part A: Mine Past Investigation Logs", "#### Part B:"), + ("audit-friction", "### Step 1: Locate Project Logs", "### Step 3:"), + ("audit-bugs", "### Step 1: Locate Project Logs", "### Step 3:"), + ("setup-project", "**Subagent F+ — Conversation History Mining", "### Step 2:"), +) + + +def _section(skill_name: str, start_marker: str, end_marker: str) -> str: + skill = DefaultSkillResolver().resolve(skill_name) + assert skill is not None + text = skill.path.read_text(encoding="utf-8") + start = text.index(start_marker) + end = text.index(end_marker, start) + return text[start:end] + + +@pytest.mark.parametrize(("skill_name", "start_marker", "end_marker"), _SKILL_SECTIONS) +def test_claude_log_mining_uses_native_project_log_resolution( + skill_name: str, start_marker: str, end_marker: str +) -> None: + section = _section(skill_name, start_marker, end_marker) + cwd_index = section.find("actual_absolute_cwd=$(pwd -P)") + command_index = section.find(_LOG_DIR_COMMAND) + discovery_index = section.find('"$LOG_DIR"', command_index) + + assert 0 <= cwd_index < command_index < discovery_index + assert "//\\//-" not in section + assert 'LOG_DIR="${LOG_DIR//--/-}"' not in section + + +@pytest.mark.parametrize( + ("skill_name", "start_marker", "end_marker"), + [entry for entry in _SKILL_SECTIONS if entry[0] in ("audit-bugs", "setup-project")], +) +def test_session_discovery_excludes_orphaned_transcripts( + skill_name: str, start_marker: str, end_marker: str +) -> None: + assert "! -name '*.orphaned-*.jsonl'" in _section(skill_name, start_marker, end_marker) diff --git a/tests/skills/test_investigate_contracts.py b/tests/skills/test_investigate_contracts.py index aaa8221516..16ee11ff86 100644 --- a/tests/skills/test_investigate_contracts.py +++ b/tests/skills/test_investigate_contracts.py @@ -55,6 +55,9 @@ def test_investigate_historical_step_mines_jsonl_logs(step_35_section: str) -> N assert ".jsonl" in step_35_section or "JSONL" in step_35_section, ( "Step 3.5 must reference '.jsonl' or 'JSONL' as the log file format to scan" ) + assert 'LOG_DIR=$(autoskillit claude-project-log-dir "$actual_absolute_cwd")' in ( + step_35_section + ), "Step 3.5 must resolve the Claude project log directory through the native CLI" def test_investigate_historical_step_searches_prior_invocations(step_35_section: str) -> None: