# Conflicts: # apps/desktop/e2e/archived-hidden-session-recoverable.spec.ts # apps/desktop/e2e/bot-chat-message-agent-friendly-name.spec.ts # apps/desktop/e2e/bot-mailbox-unreadable-ticket.spec.ts # apps/desktop/e2e/bot-mode-roster-localized.spec.ts # apps/desktop/e2e/bot-mode-row-click-mirrors-registry.spec.ts # apps/desktop/e2e/bot-mode-tab-shows-bot-name.spec.ts # apps/desktop/e2e/bot-roster-group-row-organisation.spec.ts # apps/desktop/e2e/bot-roster-ignores-infra-dirs.spec.ts # apps/desktop/e2e/bot-roster-timestamp-meta.spec.ts # apps/desktop/e2e/bot-roster-user-sections.spec.ts # apps/desktop/e2e/bot-routines-pane-narrow.spec.ts # apps/desktop/e2e/bot-row-open-recent-session.spec.ts # apps/desktop/e2e/bot-tile-ignores-ambient-composer-model.spec.ts # apps/desktop/e2e/group-composer-auto-grow.spec.ts # apps/desktop/e2e/group-create-gate-remote-roster.spec.ts # apps/desktop/e2e/group-prompt-renamed-primary-handle.spec.ts # apps/desktop/e2e/hosted-room-backend-continuity.spec.ts # apps/desktop/e2e/hosted-room-legacy-store-migration.spec.ts # apps/desktop/e2e/settings-scope-chips-bot-title.spec.ts # apps/desktop/e2e/worktree-branch-status.spec.ts # apps/desktop/electron/backend-probes.test.ts # apps/desktop/electron/connection-apply.test.ts # apps/desktop/electron/desktop-electron-pin.test.ts # apps/desktop/electron/desktop-uninstall.test.ts # apps/desktop/electron/gateway-file-download-transport.test.ts # apps/desktop/electron/gateway-stop-before-update.test.ts # apps/desktop/electron/github-api-auth.test.ts # apps/desktop/electron/registry-primary-profile-scope.test.ts # apps/desktop/electron/update-api-check.test.ts # apps/desktop/electron/update-handoff-marker.test.ts # apps/desktop/electron/venv-blocker-scan.test.ts # apps/desktop/scripts/after-extract.test.mjs # apps/desktop/scripts/local-pack-publish.test.mjs # apps/desktop/scripts/tasks-scroll.test.mjs # apps/desktop/src/app/settings/model-settings.test.tsx # apps/desktop/src/app/updates-overlay.blockers.test.tsx # apps/desktop/src/components/desktop-install-overlay.test.tsx # apps/desktop/src/lib/update-copy.test.ts # scripts/ci/check_os_marker_fakes.py # tests-js/desktop-mac-usage-descriptions.test.ts # tests-js/node-engine-alignment.test.ts # tests/agent/lsp/test_install_and_lint_fixes.py # tests/agent/test_command_token_source.py # tests/agent/test_compression_boundary_hook.py # tests/agent/test_create_openai_client_ssl_verify.py # tests/agent/test_custom_provider_ca_probes.py # tests/agent/test_endpoint_blackhole.py # tests/agent/test_estimator_parity.py # tests/agent/test_in_place_compaction.py # tests/agent/test_moa_loop_mode.py # tests/agent/test_model_metadata.py # tests/agent/test_skill_session_platform_gate.py # tests/agent/test_skill_utils.py # tests/agent/test_ssl_ca_guard.py # tests/computer_use/test_doctor.py # tests/cron/test_codex_execution_paths.py # tests/cron/test_cron_bot_chat_delivery.py # tests/cron/test_cron_script.py # tests/cron/test_media_delivery_parity.py # tests/cron/test_misfire_catchup.py # tests/cron/test_parallel_pool.py # tests/cron/test_recurring_eagain_redispatch.py # tests/gateway/test_choice_picker.py # tests/gateway/test_control_socket_windows_live.py # tests/gateway/test_dingtalk.py # tests/gateway/test_feishu.py # tests/gateway/test_feishu_onboard.py # tests/gateway/test_gateway_shutdown.py # tests/gateway/test_matrix.py # tests/gateway/test_model_command_custom_providers.py # tests/gateway/test_reasoning_command.py # tests/gateway/test_runtime_footer.py # tests/gateway/test_session.py # tests/gateway/test_session_hygiene.py # tests/gateway/test_status.py # tests/gateway/test_teams.py # tests/gateway/test_turn_lease.py # tests/gateway/test_whatsapp_connect.py # tests/hermes_cli/test_approvals_command.py # tests/hermes_cli/test_auth_store_lock_concurrent.py # tests/hermes_cli/test_backup.py # tests/hermes_cli/test_banner_git_state.py # tests/hermes_cli/test_certifi_repair.py # tests/hermes_cli/test_cmd_update.py # tests/hermes_cli/test_compat_manifest_targets.py # tests/hermes_cli/test_computer_use_cli.py # tests/hermes_cli/test_cpr_local_leak.py # tests/hermes_cli/test_dashboard_auth_gate.py # tests/hermes_cli/test_dashboard_procs_kill_grace.py # tests/hermes_cli/test_desktop_lifecycle_windows_live.py # tests/hermes_cli/test_doctor.py # tests/hermes_cli/test_doctor_command_install.py # tests/hermes_cli/test_fleet_config_migration_windows_live.py # tests/hermes_cli/test_gateway.py # tests/hermes_cli/test_gateway_platform_gating.py # tests/hermes_cli/test_gateway_restart_loop.py # tests/hermes_cli/test_gateway_task_probe.py # tests/hermes_cli/test_gateway_wsl.py # tests/hermes_cli/test_gui_command.py # tests/hermes_cli/test_install_cua_driver.py # tests/hermes_cli/test_kanban_db.py # tests/hermes_cli/test_lazy_command_exports.py # tests/hermes_cli/test_lazy_refresh_venv_repair.py # tests/hermes_cli/test_linux_desktop_entry.py # tests/hermes_cli/test_local_runtime.py # tests/hermes_cli/test_local_runtime_updates.py # tests/hermes_cli/test_managed_uv.py # tests/hermes_cli/test_mcp_reload_confirm_gate.py # tests/hermes_cli/test_nous_subscription.py # tests/hermes_cli/test_npm_engine.py # tests/hermes_cli/test_personality_none.py # tests/hermes_cli/test_pet_toggle.py # tests/hermes_cli/test_plan_reconciliation_windows_live.py # tests/hermes_cli/test_plugin_event_bus.py # tests/hermes_cli/test_plugin_manifest_v2.py # tests/hermes_cli/test_plugin_packs.py # tests/hermes_cli/test_plugins_cmd.py # tests/hermes_cli/test_plugins_cmd_enable_disable_nested.py # tests/hermes_cli/test_process_identity.py # tests/hermes_cli/test_profiles.py # tests/hermes_cli/test_profiles_sidebar_cache.py # tests/hermes_cli/test_pty_bridge.py # tests/hermes_cli/test_resolve_turn_limit.py # tests/hermes_cli/test_serve_runtime_inventory.py # tests/hermes_cli/test_session_vacuum_config.py # tests/hermes_cli/test_set_config_value.py # tests/hermes_cli/test_signal_handler_kanban_worker.py # tests/hermes_cli/test_slash_confirm_windows.py # tests/hermes_cli/test_stale_pid_guard.py # tests/hermes_cli/test_startup_fast_guards.py # tests/hermes_cli/test_status.py # tests/hermes_cli/test_telegram_managed_bot.py # tests/hermes_cli/test_tools_config.py # tests/hermes_cli/test_update_apply_shallow_count.py # tests/hermes_cli/test_update_autostash.py # tests/hermes_cli/test_update_concurrent_quarantine.py # tests/hermes_cli/test_update_fetch_failure_classifier.py # tests/hermes_cli/test_update_fleet_probe_resume_token.py # tests/hermes_cli/test_update_handoff_backend_reap.py # tests/hermes_cli/test_update_handoff_desktop_rebuild.py # tests/hermes_cli/test_update_head_moved_gate.py # tests/hermes_cli/test_update_host_obligation.py # tests/hermes_cli/test_update_import_guard.py # tests/hermes_cli/test_update_interrupted_recovery.py # tests/hermes_cli/test_update_inventory.py # tests/hermes_cli/test_update_launchd_unloaded_gateway.py # tests/hermes_cli/test_update_missing_configured_deps.py # tests/hermes_cli/test_update_modified_notice.py # tests/hermes_cli/test_update_multiplex_migration_hook.py # tests/hermes_cli/test_update_no_gateway_restart.py # tests/hermes_cli/test_update_orphan_backend_reap.py # tests/hermes_cli/test_update_parked_branch_guard.py # tests/hermes_cli/test_update_post_pull_syntax_guard.py # tests/hermes_cli/test_update_receipt.py # tests/hermes_cli/test_update_self_lock.py # tests/hermes_cli/test_update_shim_fail_closed.py # tests/hermes_cli/test_update_shim_self_lock.py # tests/hermes_cli/test_update_sqlite_remediation.py # tests/hermes_cli/test_update_stale_dashboard.py # tests/hermes_cli/test_update_stale_virtualenv.py # tests/hermes_cli/test_update_venv_health.py # tests/hermes_cli/test_update_venv_ownership_preflight.py # tests/hermes_cli/test_update_wedged_gateway.py # tests/hermes_cli/test_update_yes_flag.py # tests/hermes_cli/test_update_zip_two_phase.py # tests/hermes_cli/test_urllib_security.py # tests/hermes_cli/test_ux_messages_auth_config.py # tests/hermes_cli/test_ux_messages_startup.py # tests/hermes_cli/test_venv_holder_classifier.py # tests/hermes_cli/test_verify_console_scripts.py # tests/hermes_cli/test_verify_core_dependencies.py # tests/hermes_cli/test_web_server.py # tests/hermes_cli/test_web_server_console_ws.py # tests/hermes_cli/test_web_server_ws_ping.py # tests/hermes_cli/test_web_ui_build.py # tests/hermes_state/test_fts_rebuild_admission.py # tests/hermes_state/test_hermes_state.py # tests/plugins/memory/test_memory_lazy_install.py # tests/plugins/test_google_meet_plugin.py # tests/plugins/test_langfuse_plugin.py # tests/plugins/test_security_guidance_plugin.py # tests/plugins/test_transform_llm_output_hook.py # tests/scripts/desktop_update/test_desktop_update_windows_gateway_flag.py # tests/scripts/desktop_update/test_desktop_update_windows_python_handoff.py # tests/scripts/desktop_update/test_desktop_update_windows_timestamp.py # tests/scripts/install/test_install_clone_throttle_fallback.py # tests/scripts/install/test_install_lockfile_churn.py # tests/scripts/install/test_install_no_initial_commit.py # tests/scripts/install/test_install_sh_browser_install.py # tests/scripts/install/test_install_sh_node_prerelease.py # tests/scripts/install/test_install_sh_symlink_stomp.py # tests/scripts/install/test_install_sh_uv_lock_config.py # tests/scripts/install/test_install_unmerged_index.py # tests/scripts/test_contributor_map.py # tests/scripts/test_run_tests_parallel.py # tests/skills/test_competitor_news_monitor_skill.py # tests/skills/test_document_to_action_items_skill.py # tests/skills/test_google_workspace_setup.py # tests/skills/test_google_workspace_setup_deps.py # tests/skills/test_grounded_citations_skill.py # tests/skills/test_ip_as_logo_skill.py # tests/skills/test_live_dashboard_skill.py # tests/skills/test_mcp_oauth_remote_gateway_skill.py # tests/skills/test_office_document_skills.py # tests/skills/test_openclaw_migration.py # tests/skills/test_product_price_monitor_skill.py # tests/skills/test_scrollcraft_skill.py # tests/skills/test_setup_wizard_generator_skill.py # tests/skills/test_weekly_review_planning_skill.py # tests/test_engines_satisfiable.py # tests/test_fast_safe_load.py # tests/test_hermes_bootstrap.py # tests/test_hermes_constants.py # tests/test_hermes_logging.py # tests/test_managed_runtime_resolution.py # tests/test_model_tools_async_bridge.py # tests/test_packaging_build_guard.py # tests/test_packaging_metadata.py # tests/test_yaml_indent_consistency.py # tests/tools/test_approval_timeout_overflow.py # tests/tools/test_base_environment.py # tests/tools/test_bot_mode_dm.py # tests/tools/test_browser_chromium_check.py # tests/tools/test_browser_hardening.py # tests/tools/test_browser_homebrew_paths.py # tests/tools/test_browser_npx_warmup.py # tests/tools/test_browser_orphan_reaper.py # tests/tools/test_browser_real_profile.py # tests/tools/test_browser_use_cli.py # tests/tools/test_clipboard.py # tests/tools/test_code_execution.py # tests/tools/test_code_execution_modes.py # tests/tools/test_code_execution_windows_env.py # tests/tools/test_computer_use.py # tests/tools/test_delegate_liveness_timeout.py # tests/tools/test_execute_code_approval_cluster.py # tests/tools/test_execution_flag_detection.py # tests/tools/test_fal_common.py # tests/tools/test_file_operations.py # tests/tools/test_file_tools.py # tests/tools/test_file_tools_cwd_resolution.py # tests/tools/test_file_tools_live.py # tests/tools/test_lazy_deps.py # tests/tools/test_lazy_deps_durable_target.py # tests/tools/test_lazy_deps_managed.py # tests/tools/test_local_env_blocklist.py # tests/tools/test_local_tempdir.py # tests/tools/test_macos_protected_search.py # tests/tools/test_mcp_npx_cached_bin.py # tests/tools/test_oneshot_completion_linger.py # tests/tools/test_process_registry.py # tests/tools/test_read_file_schema_gating.py # tests/tools/test_skill_improvements.py # tests/tools/test_skills_sync.py # tests/tools/test_termux_api_detection.py # tests/tools/test_tirith_security.py # tests/tools/test_transcription_tools.py # tests/tools/test_tts_streaming.py # tests/tools/test_wake_word.py # tests/tui_gateway/test_compute_host_borrowed_lease.py # tests/tui_gateway/test_compute_host_turn_protocol.py # tests/tui_gateway/test_isolated_orphan_activity.py # tests/tui_gateway/test_protocol.py # tests/tui_gateway/test_slash_worker_profile_home.py # tests/tui_gateway/test_subprocess_encoding.py # tests/tui_gateway/test_tui_gateway_server.py # ui-tui/src/__tests__/terminalParity.test.ts # ui-tui/src/__tests__/termuxComposerLayout.test.ts # ui-tui/src/__tests__/textInputFastEcho.test.ts
543 lines
22 KiB
Python
543 lines
22 KiB
Python
"""Tests for the disk-cleanup plugin.
|
|
|
|
Covers the bundled plugin at ``plugins/disk-cleanup/``:
|
|
|
|
* ``disk_cleanup`` library: track / forget / dry_run / quick / status,
|
|
``is_safe_path`` and ``guess_category`` filtering.
|
|
* Plugin ``__init__``: ``post_tool_call`` hook auto-tracks files created
|
|
by ``write_file`` / ``terminal``; ``on_session_end`` hook runs quick
|
|
cleanup when anything was tracked during the turn.
|
|
* Slash command handler: status / dry-run / quick / track / forget /
|
|
unknown subcommand behaviours.
|
|
* Bundled-plugin discovery via ``PluginManager.discover_and_load``.
|
|
"""
|
|
|
|
import importlib
|
|
import json
|
|
import sys
|
|
from datetime import datetime, timedelta, timezone
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _isolate_env(tmp_path, monkeypatch):
|
|
"""Isolate HERMES_HOME for each test.
|
|
|
|
The global hermetic fixture already redirects HERMES_HOME to a tempdir,
|
|
but we want the plugin to work with a predictable subpath. We reset
|
|
HERMES_HOME here for clarity.
|
|
"""
|
|
hermes_home = tmp_path / ".hermes"
|
|
hermes_home.mkdir()
|
|
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
|
|
yield hermes_home
|
|
|
|
|
|
def _load_lib():
|
|
"""Import the plugin's library module directly from the repo path."""
|
|
repo_root = Path(__file__).resolve().parents[2]
|
|
lib_path = repo_root / "plugins" / "disk-cleanup" / "disk_cleanup.py"
|
|
spec = importlib.util.spec_from_file_location(
|
|
"disk_cleanup_under_test", lib_path
|
|
)
|
|
mod = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(mod)
|
|
return mod
|
|
|
|
|
|
def _load_plugin_init():
|
|
"""Import the plugin's __init__.py (which depends on the library)."""
|
|
repo_root = Path(__file__).resolve().parents[2]
|
|
plugin_dir = repo_root / "plugins" / "disk-cleanup"
|
|
# Use the PluginManager's module naming convention so relative imports work.
|
|
spec = importlib.util.spec_from_file_location(
|
|
"hermes_plugins.disk_cleanup",
|
|
plugin_dir / "__init__.py",
|
|
submodule_search_locations=[str(plugin_dir)],
|
|
)
|
|
# Ensure parent namespace package exists for the relative `. import disk_cleanup`
|
|
import types
|
|
if "hermes_plugins" not in sys.modules:
|
|
ns = types.ModuleType("hermes_plugins")
|
|
ns.__path__ = []
|
|
sys.modules["hermes_plugins"] = ns
|
|
mod = importlib.util.module_from_spec(spec)
|
|
mod.__package__ = "hermes_plugins.disk_cleanup"
|
|
mod.__path__ = [str(plugin_dir)]
|
|
sys.modules["hermes_plugins.disk_cleanup"] = mod
|
|
spec.loader.exec_module(mod)
|
|
return mod
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Library tests
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestIsSafePath:
|
|
def test_accepts_path_under_hermes_home(self, _isolate_env):
|
|
dg = _load_lib()
|
|
p = _isolate_env / "subdir" / "file.txt"
|
|
p.parent.mkdir()
|
|
p.write_text("x")
|
|
assert dg.is_safe_path(p) is True
|
|
|
|
def test_rejects_outside_hermes_home(self, _isolate_env):
|
|
dg = _load_lib()
|
|
assert dg.is_safe_path(Path("/etc/passwd")) is False
|
|
|
|
|
|
class TestGuessCategory:
|
|
def test_test_prefix(self, _isolate_env):
|
|
dg = _load_lib()
|
|
p = _isolate_env / "test_foo.py"
|
|
p.write_text("x")
|
|
assert dg.guess_category(p) == "test"
|
|
|
|
def test_tmp_prefix(self, _isolate_env):
|
|
dg = _load_lib()
|
|
p = _isolate_env / "tmp_foo.log"
|
|
p.write_text("x")
|
|
assert dg.guess_category(p) == "test"
|
|
|
|
def test_dot_test_suffix(self, _isolate_env):
|
|
dg = _load_lib()
|
|
p = _isolate_env / "mything.test.js"
|
|
p.write_text("x")
|
|
assert dg.guess_category(p) == "test"
|
|
|
|
def test_skips_protected_top_level(self, _isolate_env):
|
|
dg = _load_lib()
|
|
logs_dir = _isolate_env / "logs"
|
|
logs_dir.mkdir()
|
|
p = logs_dir / "test_log.txt"
|
|
p.write_text("x")
|
|
# Even though it matches test_* pattern, logs/ is excluded.
|
|
assert dg.guess_category(p) is None
|
|
|
|
def test_cron_subtree_categorised(self, _isolate_env):
|
|
dg = _load_lib()
|
|
# Only files under ``cron/output/`` are disposable run artifacts.
|
|
output_dir = _isolate_env / "cron" / "output" / "job_123"
|
|
output_dir.mkdir(parents=True)
|
|
p = output_dir / "run.md"
|
|
p.write_text("x")
|
|
assert dg.guess_category(p) == "cron-output"
|
|
|
|
|
|
def test_cronjobs_top_level_not_tracked(self, _isolate_env):
|
|
"""The legacy ``cronjobs`` alias is also control-plane at the top."""
|
|
dg = _load_lib()
|
|
cron_dir = _isolate_env / "cronjobs"
|
|
cron_dir.mkdir()
|
|
p = cron_dir / "jobs.json"
|
|
p.write_text("[]")
|
|
assert dg.guess_category(p) is None
|
|
|
|
def test_ordinary_file_returns_none(self, _isolate_env):
|
|
dg = _load_lib()
|
|
p = _isolate_env / "notes.md"
|
|
p.write_text("x")
|
|
assert dg.guess_category(p) is None
|
|
|
|
|
|
class TestProfileUserTreesNeverCleaned:
|
|
"""``workspace/`` (and the other per-profile user trees) hold project files, so
|
|
a ``test_*``/``tmp_*`` name inside them is never a disposable scratch file.
|
|
|
|
Regression for the data loss where ``workspace/<project>/tests/test_parse.py`` was
|
|
classified "test" on write and unlinked by ``quick()`` at session end.
|
|
"""
|
|
|
|
def test_session_end_hook_leaves_workspace_files_alone(self, _isolate_env):
|
|
"""End-to-end: write_file into a project tree, then session end. A scratch file at
|
|
the HERMES_HOME root is the control: it is still tracked and removed."""
|
|
pi = _load_plugin_init()
|
|
dg = _load_lib()
|
|
keep = _isolate_env / "workspace" / "proj" / "tests" / "test_parse.py"
|
|
keep.parent.mkdir(parents=True)
|
|
keep.write_text("x")
|
|
scratch = _isolate_env / "tmp_scratch.py"
|
|
scratch.write_text("x")
|
|
assert dg.guess_category(keep) is None
|
|
assert dg.guess_category(scratch) == "test"
|
|
for p in (keep, scratch):
|
|
pi._on_post_tool_call(
|
|
tool_name="write_file",
|
|
args={"path": str(p), "content": "x"},
|
|
result="OK",
|
|
task_id="t_ws", session_id="s_ws",
|
|
)
|
|
pi._on_session_end(session_id="s_ws", completed=True, interrupted=False)
|
|
assert keep.exists(), "session-end cleanup must not touch workspace project files"
|
|
assert not scratch.exists(), "root-level scratch files are still cleaned up"
|
|
|
|
def test_empty_dir_sweep_skips_workspace(self, _isolate_env):
|
|
"""Empty dirs inside a project tree are meaningful (``data/``, ``.artifacts/``)
|
|
and must survive the empty-dir sweep; unprotected empty top levels are still swept."""
|
|
dg = _load_lib()
|
|
keep = _isolate_env / "workspace" / "watch-battery" / "data"
|
|
keep.mkdir(parents=True)
|
|
sweepable = _isolate_env / "pairing"
|
|
sweepable.mkdir()
|
|
|
|
dg._sweep_empty_dirs(_isolate_env)
|
|
|
|
assert keep.exists(), "empty dir inside workspace/ must survive the sweep"
|
|
assert not sweepable.exists(), "unprotected empty dirs are still swept"
|
|
|
|
|
|
class TestProtectedDirsNeverRmtreed:
|
|
"""A tracked DIRECTORY under a protected top level (``cache/`` holds terminal snapshots)
|
|
must never be rmtree'd by the tracked-item path, only by-file aging; ``kanban/`` is never
|
|
tracked at all (its attachments/workspaces have their own lifecycle)."""
|
|
|
|
def test_stale_cache_dir_entry_is_skipped_but_its_old_files_still_age_out(self, _isolate_env):
|
|
dg = _load_lib()
|
|
cache = _isolate_env / "cache"
|
|
(cache / "terminal").mkdir(parents=True)
|
|
old_file = cache / "scratch.txt"
|
|
old_file.write_text("x")
|
|
assert dg.guess_category(cache) is None, "the cache dir itself is never tracked"
|
|
assert dg.guess_category(old_file) == "temp", "files under cache/ still age out as temp"
|
|
old_ts = (datetime.now(timezone.utc) - timedelta(days=8)).isoformat()
|
|
dg.save_tracked([
|
|
{"path": str(cache), "category": "temp", "timestamp": old_ts, "size": 0},
|
|
{"path": str(old_file), "category": "temp", "timestamp": old_ts, "size": 1},
|
|
])
|
|
|
|
summary = dg.quick()
|
|
|
|
assert (cache / "terminal").is_dir(), "protected top-level dir must survive a stale tracked entry"
|
|
assert not old_file.exists(), "old temp FILE under cache/ is still pruned (control)"
|
|
assert summary["deleted"] == 1
|
|
assert dg.load_tracked() == [], "the stale dir entry is dropped, not retried every session"
|
|
assert "SKIPPED" in (_isolate_env / "disk-cleanup" / "cleanup.log").read_text()
|
|
|
|
def test_kanban_test_files_are_never_tracked_or_deleted(self, _isolate_env):
|
|
pi = _load_plugin_init()
|
|
dg = _load_lib()
|
|
att = _isolate_env / "kanban" / "attachments" / "t1" / "test_evidence.sh"
|
|
att.parent.mkdir(parents=True)
|
|
att.write_text("x")
|
|
assert dg.guess_category(att) is None
|
|
# A stale pre-fix entry must be dropped by re-validation instead of deleted.
|
|
dg.save_tracked([{"path": str(att), "category": "test",
|
|
"timestamp": datetime.now(timezone.utc).isoformat(), "size": 1}])
|
|
pi._on_post_tool_call(tool_name="write_file", args={"path": str(att), "content": "x"},
|
|
result="OK", task_id="t1", session_id="s_kb")
|
|
scratch = _isolate_env / "test_scratch.py"
|
|
scratch.write_text("x")
|
|
pi._on_post_tool_call(tool_name="write_file", args={"path": str(scratch), "content": "x"},
|
|
result="OK", task_id="t1", session_id="s_kb")
|
|
|
|
pi._on_session_end(session_id="s_kb", completed=True, interrupted=False)
|
|
|
|
assert att.exists(), "kanban attachments are task-managed, never auto-deleted"
|
|
assert not scratch.exists(), "root-level scratch files are still cleaned up (control)"
|
|
assert dg.load_tracked() == []
|
|
|
|
|
|
class TestGitWorktreeFilesNeverCleaned:
|
|
"""Regression tests for #115295 — git-owned test_* files (committed regression tests
|
|
inside worktrees/checkouts) are never tracked or auto-deleted; scratch files outside
|
|
git trees still are."""
|
|
|
|
def test_quick_drops_stale_tracked_worktree_entry_instead_of_deleting(self, _isolate_env):
|
|
"""A test_* file inside a linked git worktree ($HERMES_HOME/worktrees/, .git is a
|
|
pointer FILE) is not classified as disposable, and a stale pre-fix tracked entry
|
|
(category "test") is dropped by quick()'s re-validation, not deleted."""
|
|
dg = _load_lib()
|
|
wt = _isolate_env / "worktrees" / "repro-wt"
|
|
wt.mkdir(parents=True)
|
|
(wt / ".git").write_text("gitdir: /elsewhere/main/.git/worktrees/repro-wt\n")
|
|
f = wt / "test_durable.py"
|
|
f.write_text("x")
|
|
assert dg.guess_category(f) is None
|
|
dg.save_tracked([{"path": str(f), "category": "test",
|
|
"timestamp": datetime.now(timezone.utc).isoformat(), "size": 1}])
|
|
result = dg.quick()
|
|
assert f.exists(), "git-owned test files must never be auto-deleted"
|
|
assert result["deleted"] == 0
|
|
assert dg.load_tracked() == [], "stale entry is dropped from tracking, not kept"
|
|
|
|
def test_scratch_outside_git_trees_still_cleaned(self, _isolate_env):
|
|
"""Control: root-level test_* scratch is still auto-deleted — even when HERMES_HOME
|
|
itself lives inside a git checkout (dotfiles repo); only .git entries strictly below
|
|
HERMES_HOME mark a file as git-owned."""
|
|
dg = _load_lib()
|
|
(_isolate_env.parent / ".git").mkdir()
|
|
scratch = _isolate_env / "test_scratch.py"
|
|
scratch.write_text("x")
|
|
assert dg.guess_category(scratch) == "test"
|
|
dg.save_tracked([{"path": str(scratch), "category": "test",
|
|
"timestamp": datetime.now(timezone.utc).isoformat(), "size": 1}])
|
|
result = dg.quick()
|
|
assert not scratch.exists()
|
|
assert result["deleted"] == 1
|
|
|
|
|
|
class TestStaleCronEntryMigration:
|
|
"""Regression tests for #37721 — stale cron-output entries in tracked.json."""
|
|
|
|
def test_quick_skips_stale_cron_output_for_jobs_json(self, _isolate_env):
|
|
"""A stale tracked.json entry with category="cron-output" for
|
|
cron/jobs.json must NOT be deleted by quick().
|
|
|
|
This is the exact scenario from #37721: an old tracked.json has
|
|
{"path": ".../cron/jobs.json", "category": "cron-output"} which
|
|
would pass the delete filter but must be skipped because
|
|
guess_category() now returns None for non-output cron paths.
|
|
"""
|
|
dg = _load_lib()
|
|
cron_dir = _isolate_env / "cron"
|
|
cron_dir.mkdir()
|
|
jobs_json = cron_dir / "jobs.json"
|
|
jobs_json.write_text('{"jobs": []}')
|
|
|
|
# Simulate a stale tracked.json entry from before #34840 by
|
|
# directly writing the tracked file (track() would reject it).
|
|
tracked_file = _isolate_env / "disk-cleanup" / "tracked.json"
|
|
tracked_file.parent.mkdir(parents=True, exist_ok=True)
|
|
tracked_file.write_text(json.dumps([{
|
|
"path": str(jobs_json),
|
|
"category": "cron-output",
|
|
"timestamp": "2025-01-01T00:00:00+00:00", # very old
|
|
"size": 123,
|
|
}]))
|
|
|
|
summary = dg.quick()
|
|
assert summary["deleted"] == 0, "cron/jobs.json must not be deleted"
|
|
assert jobs_json.exists(), "jobs.json must still exist"
|
|
# The stale entry should have been dropped from tracking.
|
|
remaining = json.loads(tracked_file.read_text())
|
|
assert len(remaining) == 0
|
|
|
|
|
|
def test_dry_run_omits_stale_cron_output(self, _isolate_env):
|
|
"""dry_run() should also skip stale cron-output entries."""
|
|
dg = _load_lib()
|
|
cron_dir = _isolate_env / "cron"
|
|
cron_dir.mkdir()
|
|
jobs_json = cron_dir / "jobs.json"
|
|
jobs_json.write_text("[]")
|
|
|
|
tracked_file = _isolate_env / "disk-cleanup" / "tracked.json"
|
|
tracked_file.parent.mkdir(parents=True, exist_ok=True)
|
|
tracked_file.write_text(json.dumps([{
|
|
"path": str(jobs_json),
|
|
"category": "cron-output",
|
|
"timestamp": "2025-01-01T00:00:00+00:00",
|
|
"size": 123,
|
|
}]))
|
|
|
|
auto, prompt = dg.dry_run()
|
|
assert len(auto) == 0, "stale cron-output for jobs.json must not appear"
|
|
assert len(prompt) == 0
|
|
|
|
def test_legitimate_cron_output_still_deleted(self, _isolate_env):
|
|
"""A valid cron-output entry under cron/output/ must still be deleted."""
|
|
dg = _load_lib()
|
|
output_dir = _isolate_env / "cron" / "output" / "job_1"
|
|
output_dir.mkdir(parents=True)
|
|
run_md = output_dir / "run.md"
|
|
run_md.write_text("x")
|
|
|
|
# Old enough to be deleted (>14 days)
|
|
from datetime import datetime, timezone, timedelta
|
|
old_ts = (datetime.now(timezone.utc) - timedelta(days=20)).isoformat()
|
|
|
|
tracked_file = _isolate_env / "disk-cleanup" / "tracked.json"
|
|
tracked_file.parent.mkdir(parents=True, exist_ok=True)
|
|
tracked_file.write_text(json.dumps([{
|
|
"path": str(run_md),
|
|
"category": "cron-output",
|
|
"timestamp": old_ts,
|
|
"size": 10,
|
|
}]))
|
|
|
|
summary = dg.quick()
|
|
assert summary["deleted"] == 1, "valid old cron-output should be deleted"
|
|
assert not run_md.exists()
|
|
|
|
|
|
class TestTrackForgetQuick:
|
|
def test_track_then_quick_deletes_test(self, _isolate_env):
|
|
dg = _load_lib()
|
|
p = _isolate_env / "test_a.py"
|
|
p.write_text("x")
|
|
assert dg.track(str(p), "test", silent=True) is True
|
|
summary = dg.quick()
|
|
assert summary["deleted"] == 1
|
|
assert not p.exists()
|
|
|
|
|
|
def test_forget_removes_entry(self, _isolate_env):
|
|
dg = _load_lib()
|
|
p = _isolate_env / "keep.tmp"
|
|
p.write_text("x")
|
|
dg.track(str(p), "temp", silent=True)
|
|
assert dg.forget(str(p)) == 1
|
|
assert p.exists() # forget does NOT delete the file
|
|
|
|
|
|
class TestStatus:
|
|
def test_empty_status(self, _isolate_env):
|
|
dg = _load_lib()
|
|
s = dg.status()
|
|
assert s["total_tracked"] == 0
|
|
assert s["top10"] == []
|
|
|
|
def test_status_with_entries(self, _isolate_env):
|
|
dg = _load_lib()
|
|
p = _isolate_env / "big.tmp"
|
|
p.write_text("y" * 100)
|
|
dg.track(str(p), "temp", silent=True)
|
|
s = dg.status()
|
|
assert s["total_tracked"] == 1
|
|
assert len(s["top10"]) == 1
|
|
rendered = dg.format_status(s)
|
|
assert "temp" in rendered
|
|
assert "big.tmp" in rendered
|
|
|
|
|
|
class TestDryRun:
|
|
def test_classifies_by_category(self, _isolate_env):
|
|
dg = _load_lib()
|
|
test_f = _isolate_env / "test_x.py"
|
|
test_f.write_text("x")
|
|
big = _isolate_env / "big.bin"
|
|
big.write_bytes(b"z" * 10)
|
|
dg.track(str(test_f), "test", silent=True)
|
|
dg.track(str(big), "other", silent=True)
|
|
auto, prompt = dg.dry_run()
|
|
# test → auto, other → neither (doesn't hit any rule)
|
|
assert any(i["path"] == str(test_f) for i in auto)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Plugin hooks tests
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestPostToolCallHook:
|
|
def test_write_file_test_pattern_tracked(self, _isolate_env):
|
|
pi = _load_plugin_init()
|
|
p = _isolate_env / "test_created.py"
|
|
p.write_text("x")
|
|
pi._on_post_tool_call(
|
|
tool_name="write_file",
|
|
args={"path": str(p), "content": "x"},
|
|
result="OK",
|
|
task_id="t1", session_id="s1",
|
|
)
|
|
tracked_file = _isolate_env / "disk-cleanup" / "tracked.json"
|
|
data = json.loads(tracked_file.read_text())
|
|
assert len(data) == 1
|
|
assert data[0]["category"] == "test"
|
|
|
|
|
|
def test_terminal_command_picks_up_paths(self, _isolate_env):
|
|
pi = _load_plugin_init()
|
|
p = _isolate_env / "tmp_created.log"
|
|
p.write_text("x")
|
|
pi._on_post_tool_call(
|
|
tool_name="terminal",
|
|
args={"command": f"touch {p}"},
|
|
result=f"created {p}\n",
|
|
task_id="t3", session_id="s3",
|
|
)
|
|
tracked_file = _isolate_env / "disk-cleanup" / "tracked.json"
|
|
data = json.loads(tracked_file.read_text())
|
|
assert any(Path(i["path"]) == p.resolve() for i in data)
|
|
|
|
def test_ignores_unrelated_tool(self, _isolate_env):
|
|
pi = _load_plugin_init()
|
|
pi._on_post_tool_call(
|
|
tool_name="read_file",
|
|
args={"path": str(_isolate_env / "test_x.py")},
|
|
result="contents",
|
|
task_id="t4", session_id="s4",
|
|
)
|
|
# read_file should never trigger tracking.
|
|
tracked_file = _isolate_env / "disk-cleanup" / "tracked.json"
|
|
assert not tracked_file.exists() or tracked_file.read_text().strip() == "[]"
|
|
|
|
|
|
class TestOnSessionEndHook:
|
|
def test_runs_quick_when_test_files_tracked(self, _isolate_env):
|
|
pi = _load_plugin_init()
|
|
p = _isolate_env / "test_cleanup.py"
|
|
p.write_text("x")
|
|
pi._on_post_tool_call(
|
|
tool_name="write_file",
|
|
args={"path": str(p), "content": "x"},
|
|
result="OK",
|
|
task_id="", session_id="s1",
|
|
)
|
|
assert p.exists()
|
|
pi._on_session_end(session_id="s1", completed=True, interrupted=False)
|
|
assert not p.exists(), "test file should be auto-deleted"
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Slash command
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Bundled-plugin discovery
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestBundledDiscovery:
|
|
def _write_enabled_config(self, hermes_home, names):
|
|
"""Write plugins.enabled allow-list to config.yaml."""
|
|
import hermes_yaml as yaml
|
|
cfg_path = hermes_home / "config.yaml"
|
|
cfg_path.write_text(yaml.safe_dump({"plugins": {"enabled": list(names)}}))
|
|
|
|
def test_disk_cleanup_discovered_but_not_loaded_by_default(self, _isolate_env):
|
|
"""Bundled plugins are discovered but NOT loaded without opt-in."""
|
|
from hermes_cli import plugins as pmod
|
|
mgr = pmod.PluginManager()
|
|
mgr.discover_and_load()
|
|
# Discovered — appears in the registry
|
|
assert "disk-cleanup" in mgr._plugins
|
|
loaded = mgr._plugins["disk-cleanup"]
|
|
assert loaded.manifest.source == "bundled"
|
|
# But NOT enabled — no hooks or commands registered
|
|
assert not loaded.enabled
|
|
assert loaded.error and "not enabled" in loaded.error
|
|
|
|
|
|
def test_disabled_beats_enabled(self, _isolate_env):
|
|
"""plugins.disabled wins even if the plugin is also in plugins.enabled."""
|
|
import hermes_yaml as yaml
|
|
cfg_path = _isolate_env / "config.yaml"
|
|
cfg_path.write_text(yaml.safe_dump({
|
|
"plugins": {
|
|
"enabled": ["disk-cleanup"],
|
|
"disabled": ["disk-cleanup"],
|
|
}
|
|
}))
|
|
from hermes_cli import plugins as pmod
|
|
mgr = pmod.PluginManager()
|
|
mgr.discover_and_load()
|
|
loaded = mgr._plugins["disk-cleanup"]
|
|
assert not loaded.enabled
|
|
assert loaded.error == "disabled via config"
|
|
|
|
def test_memory_and_context_engine_subdirs_skipped(self, _isolate_env):
|
|
"""Bundled scan must NOT pick up plugins/memory or plugins/context_engine
|
|
as top-level plugins — they have their own discovery paths."""
|
|
self._write_enabled_config(
|
|
_isolate_env, ["memory", "context_engine", "disk-cleanup"]
|
|
)
|
|
from hermes_cli import plugins as pmod
|
|
mgr = pmod.PluginManager()
|
|
mgr.discover_and_load()
|
|
assert "memory" not in mgr._plugins
|
|
assert "context_engine" not in mgr._plugins
|