|
11 | 11 | import stat |
12 | 12 | import tempfile |
13 | 13 | import unittest |
| 14 | +from unittest import mock |
14 | 15 |
|
15 | 16 | from skillopt_sleep.staging import ( |
16 | 17 | SkillProposal, |
@@ -375,6 +376,302 @@ def test_adopt_skill_flag_promotes_only_the_named_skill(self): |
375 | 376 | self.assertEqual(_read(night.alpha_live), "# alpha v2\n") |
376 | 377 | self.assertEqual(_read(night.beta_live), "# beta v1\n") |
377 | 378 |
|
| 379 | + def test_all_skills_flag_promotes_every_staged_skill(self): |
| 380 | + with tempfile.TemporaryDirectory() as tmp: |
| 381 | + night = TwoSkillNight(tmp) |
| 382 | + claude_home = os.path.join(tmp, ".claude") |
| 383 | + os.makedirs(claude_home, exist_ok=True) |
| 384 | + rc, out = self._cli([ |
| 385 | + "adopt", "--project", tmp, "--claude-home", claude_home, |
| 386 | + "--all-skills", |
| 387 | + ]) |
| 388 | + self.assertEqual(rc, 0, out) |
| 389 | + self.assertEqual(_read(night.alpha_live), "# alpha v2\n") |
| 390 | + self.assertEqual(_read(night.beta_live), "# beta v2\n") |
| 391 | + |
| 392 | + def test_repeated_skill_flags_adopt_the_named_pair(self): |
| 393 | + with tempfile.TemporaryDirectory() as tmp: |
| 394 | + night = TwoSkillNight(tmp) |
| 395 | + claude_home = os.path.join(tmp, ".claude") |
| 396 | + os.makedirs(claude_home, exist_ok=True) |
| 397 | + rc, out = self._cli([ |
| 398 | + "adopt", "--project", tmp, "--claude-home", claude_home, |
| 399 | + "--skill", "alpha", "--skill", "beta", |
| 400 | + ]) |
| 401 | + self.assertEqual(rc, 0, out) |
| 402 | + self.assertEqual(_read(night.alpha_live), "# alpha v2\n") |
| 403 | + self.assertEqual(_read(night.beta_live), "# beta v2\n") |
| 404 | + |
| 405 | + def test_skill_and_all_skills_together_are_refused(self): |
| 406 | + with tempfile.TemporaryDirectory() as tmp: |
| 407 | + night = TwoSkillNight(tmp) |
| 408 | + claude_home = os.path.join(tmp, ".claude") |
| 409 | + os.makedirs(claude_home, exist_ok=True) |
| 410 | + rc, out = self._cli([ |
| 411 | + "adopt", "--project", tmp, "--claude-home", claude_home, |
| 412 | + "--skill", "alpha", "--all-skills", |
| 413 | + ]) |
| 414 | + self.assertEqual(rc, 2) |
| 415 | + self.assertIn("not both", out) |
| 416 | + self.assertEqual(_read(night.alpha_live), "# alpha v1\n") |
| 417 | + self.assertEqual(_read(night.beta_live), "# beta v1\n") |
| 418 | + |
| 419 | + def test_legacy_night_bare_adopt_still_copies_the_managed_pair(self): |
| 420 | + with tempfile.TemporaryDirectory() as tmp: |
| 421 | + live = os.path.join(tmp, "live", "SKILL.md") |
| 422 | + memory = os.path.join(tmp, "live", "CLAUDE.md") |
| 423 | + _write(live, "# live v1\n") |
| 424 | + _write(memory, "# mem v1\n") |
| 425 | + write_staging( |
| 426 | + tmp, report=SleepReport(night=1, project=tmp, accepted=True), |
| 427 | + proposed_skill="# live v2\n", proposed_memory="# mem v2\n", |
| 428 | + live_skill_path=live, live_memory_path=memory, |
| 429 | + report_md="# report\n", |
| 430 | + ) |
| 431 | + claude_home = os.path.join(tmp, ".claude") |
| 432 | + os.makedirs(claude_home, exist_ok=True) |
| 433 | + rc, out = self._cli([ |
| 434 | + "adopt", "--project", tmp, "--claude-home", claude_home, |
| 435 | + ]) |
| 436 | + self.assertEqual(rc, 0, out) |
| 437 | + self.assertEqual(_read(live), "# live v2\n") |
| 438 | + self.assertEqual(_read(memory), "# mem v2\n") |
| 439 | + |
| 440 | + def test_skill_flag_on_a_legacy_night_is_refused(self): |
| 441 | + with tempfile.TemporaryDirectory() as tmp: |
| 442 | + live = os.path.join(tmp, "live", "SKILL.md") |
| 443 | + _write(live, "# live v1\n") |
| 444 | + write_staging( |
| 445 | + tmp, report=SleepReport(night=1, project=tmp, accepted=True), |
| 446 | + proposed_skill="# live v2\n", proposed_memory=None, |
| 447 | + live_skill_path=live, |
| 448 | + live_memory_path=os.path.join(tmp, "live", "CLAUDE.md"), |
| 449 | + report_md="# report\n", |
| 450 | + ) |
| 451 | + claude_home = os.path.join(tmp, ".claude") |
| 452 | + os.makedirs(claude_home, exist_ok=True) |
| 453 | + rc, out = self._cli([ |
| 454 | + "adopt", "--project", tmp, "--claude-home", claude_home, |
| 455 | + "--skill", "alpha", |
| 456 | + ]) |
| 457 | + self.assertEqual(rc, 2) |
| 458 | + self.assertIn("no per-skill", out) |
| 459 | + self.assertEqual(_read(live), "# live v1\n") |
| 460 | + |
| 461 | + |
| 462 | +class TestAdoptTimeRevalidationMega(unittest.TestCase): |
| 463 | + def test_casefold_live_path_collision_is_refused_at_adopt(self): |
| 464 | + with tempfile.TemporaryDirectory() as tmp: |
| 465 | + night = TwoSkillNight(tmp) |
| 466 | + manifest_path = os.path.join(night.staging, "manifest.json") |
| 467 | + with open(manifest_path, encoding="utf-8") as f: |
| 468 | + manifest = json.load(f) |
| 469 | + live_root = os.path.dirname(os.path.dirname(night.alpha_live)) |
| 470 | + manifest["skills"][1]["live_skill_path"] = os.path.join( |
| 471 | + live_root, "ALPHA", "SKILL.md") |
| 472 | + with open(manifest_path, "w", encoding="utf-8") as f: |
| 473 | + json.dump(manifest, f) |
| 474 | + with self.assertRaises(StagingError): |
| 475 | + adopt_skills(night.staging) |
| 476 | + self.assertEqual(_read(night.alpha_live), "# alpha v1\n") |
| 477 | + self.assertEqual(_read(night.beta_live), "# beta v1\n") |
| 478 | + |
| 479 | + def test_symlink_realpath_collision_is_refused_at_adopt(self): |
| 480 | + with tempfile.TemporaryDirectory() as tmp: |
| 481 | + night = TwoSkillNight(tmp) |
| 482 | + alias = os.path.join(night.live_root, "alias", "SKILL.md") |
| 483 | + os.makedirs(os.path.dirname(alias), exist_ok=True) |
| 484 | + try: |
| 485 | + os.symlink(night.alpha_live, alias) |
| 486 | + except OSError: |
| 487 | + self.skipTest("symlinks unavailable") |
| 488 | + manifest_path = os.path.join(night.staging, "manifest.json") |
| 489 | + with open(manifest_path, encoding="utf-8") as f: |
| 490 | + manifest = json.load(f) |
| 491 | + manifest["skills"][1]["live_skill_path"] = alias |
| 492 | + with open(manifest_path, "w", encoding="utf-8") as f: |
| 493 | + json.dump(manifest, f) |
| 494 | + with self.assertRaises(StagingError): |
| 495 | + adopt_skills(night.staging) |
| 496 | + self.assertEqual(_read(night.alpha_live), "# alpha v1\n") |
| 497 | + self.assertEqual(_read(night.beta_live), "# beta v1\n") |
| 498 | + |
| 499 | + def test_receipt_write_failure_restores_a_previous_receipt(self): |
| 500 | + from skillopt_sleep import staging as staging_mod |
| 501 | + |
| 502 | + with tempfile.TemporaryDirectory() as tmp: |
| 503 | + night = TwoSkillNight(tmp) |
| 504 | + adopt_skills(night.staging, ["alpha"]) |
| 505 | + receipt_path = os.path.join(night.staging, "adopted_skills.json") |
| 506 | + previous = _read(receipt_path) |
| 507 | + real_write = staging_mod._write_atomic |
| 508 | + |
| 509 | + def boom(path, text): |
| 510 | + if os.path.basename(path) == "adopted_skills.json": |
| 511 | + raise OSError("disk full") |
| 512 | + return real_write(path, text) |
| 513 | + |
| 514 | + with mock.patch.object(staging_mod, "_write_atomic", side_effect=boom): |
| 515 | + with self.assertRaises(OSError): |
| 516 | + adopt_skills(night.staging, ["beta"]) |
| 517 | + self.assertEqual(_read(night.beta_live), "# beta v1\n") |
| 518 | + self.assertEqual(_read(night.alpha_live), "# alpha v2\n") |
| 519 | + self.assertEqual(_read(receipt_path), previous) |
| 520 | + |
| 521 | + |
| 522 | +def _accepted_group(name, body): |
| 523 | + from skillopt_sleep.consolidate import ConsolidationResult |
| 524 | + from skillopt_sleep.multi_skill import CONSOLIDATED, GroupConsolidation |
| 525 | + |
| 526 | + result = ConsolidationResult( |
| 527 | + accepted=True, gate_action="accept_new_best", |
| 528 | + baseline_score=0.1, candidate_score=0.2, |
| 529 | + new_skill=body, new_memory="", |
| 530 | + applied_edits=[], rejected_edits=[], |
| 531 | + holdout_baseline=0.1, holdout_candidate=0.2, |
| 532 | + ) |
| 533 | + return GroupConsolidation( |
| 534 | + skill_name=name, status=CONSOLIDATED, result=result, n_tasks=2, |
| 535 | + ) |
| 536 | + |
| 537 | + |
| 538 | +class TestSkillProposalsFromGroups(unittest.TestCase): |
| 539 | + def test_skips_managed_catch_all_and_unresolved_names(self): |
| 540 | + from skillopt_sleep.config import load_config |
| 541 | + from skillopt_sleep.cycle import _skill_proposals_from_groups |
| 542 | + |
| 543 | + with tempfile.TemporaryDirectory() as home: |
| 544 | + claude_home = os.path.join(home, ".claude") |
| 545 | + live = os.path.join(claude_home, "skills", "research-skill", "SKILL.md") |
| 546 | + _write(live, "# research v1\n") |
| 547 | + cfg = load_config( |
| 548 | + claude_home=claude_home, |
| 549 | + managed_skill_name="skillopt-sleep-learned", |
| 550 | + ) |
| 551 | + proposals = _skill_proposals_from_groups( |
| 552 | + cfg, |
| 553 | + { |
| 554 | + "skillopt-sleep-learned": _accepted_group( |
| 555 | + "skillopt-sleep-learned", "# managed v2\n"), |
| 556 | + "research-skill": _accepted_group( |
| 557 | + "research-skill", "# research v2\n"), |
| 558 | + "ghost-skill": _accepted_group( |
| 559 | + "ghost-skill", "# ghost v2\n"), |
| 560 | + }, |
| 561 | + "skillopt-sleep-learned", |
| 562 | + ) |
| 563 | + names = [p.skill_name for p in proposals] |
| 564 | + self.assertEqual(names, ["research-skill"]) |
| 565 | + self.assertEqual(proposals[0].live_skill_path, os.path.realpath(live)) |
| 566 | + self.assertEqual(proposals[0].proposed_skill, "# research v2\n") |
| 567 | + |
| 568 | + def test_skips_a_second_skill_that_resolves_to_the_same_live_file(self): |
| 569 | + from skillopt_sleep.config import load_config |
| 570 | + from skillopt_sleep.cycle import _skill_proposals_from_groups |
| 571 | + |
| 572 | + with tempfile.TemporaryDirectory() as home: |
| 573 | + claude_home = os.path.join(home, ".claude") |
| 574 | + skills = os.path.join(claude_home, "skills") |
| 575 | + research = os.path.join(skills, "research-skill") |
| 576 | + alias = os.path.join(skills, "alias-skill") |
| 577 | + _write(os.path.join(research, "SKILL.md"), "# research v1\n") |
| 578 | + try: |
| 579 | + os.symlink(research, alias) |
| 580 | + except OSError: |
| 581 | + self.skipTest("symlinks unavailable") |
| 582 | + cfg = load_config(claude_home=claude_home) |
| 583 | + proposals = _skill_proposals_from_groups( |
| 584 | + cfg, |
| 585 | + { |
| 586 | + "research-skill": _accepted_group( |
| 587 | + "research-skill", "# research v2\n"), |
| 588 | + "alias-skill": _accepted_group( |
| 589 | + "alias-skill", "# alias v2\n"), |
| 590 | + }, |
| 591 | + "skillopt-sleep-learned", |
| 592 | + ) |
| 593 | + self.assertEqual([p.skill_name for p in proposals], ["research-skill"]) |
| 594 | + |
| 595 | + |
| 596 | +class TestCycleStagingGaps(unittest.TestCase): |
| 597 | + def _hinted_tasks(self): |
| 598 | + from dataclasses import replace |
| 599 | + |
| 600 | + from skillopt_sleep.experiments.personas import programmer_persona, researcher_persona |
| 601 | + from skillopt_sleep.mine import assign_splits |
| 602 | + |
| 603 | + research = assign_splits(researcher_persona(), holdout_fraction=0.34, seed=42) |
| 604 | + programming = assign_splits(programmer_persona(), holdout_fraction=0.34, seed=1) |
| 605 | + tagged = [replace(t, skill_hint="research-skill") for t in research] |
| 606 | + tagged += [replace(t, id=f"prog-{t.id}", skill_hint="programming-skill") |
| 607 | + for t in programming] |
| 608 | + return tagged |
| 609 | + |
| 610 | + def test_missing_live_skill_is_skipped_not_aborted(self): |
| 611 | + from skillopt_sleep.config import load_config |
| 612 | + from skillopt_sleep.cycle import run_sleep_cycle |
| 613 | + |
| 614 | + with tempfile.TemporaryDirectory() as proj, tempfile.TemporaryDirectory() as home: |
| 615 | + claude_home = os.path.join(home, ".claude") |
| 616 | + research_live = os.path.join( |
| 617 | + claude_home, "skills", "research-skill", "SKILL.md") |
| 618 | + _write(research_live, "# research-skill v1\n") |
| 619 | + cfg = load_config( |
| 620 | + invoked_project=proj, projects="invoked", backend="mock", |
| 621 | + claude_home=claude_home, |
| 622 | + managed_skill_name="skillopt-sleep-learned", auto_adopt=False, |
| 623 | + multi_skill_report=True, |
| 624 | + ) |
| 625 | + outcome = run_sleep_cycle(cfg, seed_tasks=self._hinted_tasks()) |
| 626 | + names = [r["skill_name"] for r in staged_skills(outcome.staging_dir)] |
| 627 | + self.assertEqual(names, ["research-skill"]) |
| 628 | + self.assertFalse(os.path.isfile(os.path.join( |
| 629 | + outcome.staging_dir, "proposed_SKILL.programming-skill.md"))) |
| 630 | + |
| 631 | + def test_report_off_stages_no_per_skill_proposals(self): |
| 632 | + from skillopt_sleep.config import load_config |
| 633 | + from skillopt_sleep.cycle import run_sleep_cycle |
| 634 | + |
| 635 | + with tempfile.TemporaryDirectory() as proj, tempfile.TemporaryDirectory() as home: |
| 636 | + claude_home = os.path.join(home, ".claude") |
| 637 | + _write(os.path.join(claude_home, "skills", "research-skill", "SKILL.md"), |
| 638 | + "# research-skill v1\n") |
| 639 | + _write(os.path.join(claude_home, "skills", "programming-skill", "SKILL.md"), |
| 640 | + "# programming-skill v1\n") |
| 641 | + cfg = load_config( |
| 642 | + invoked_project=proj, projects="invoked", backend="mock", |
| 643 | + claude_home=claude_home, |
| 644 | + managed_skill_name="skillopt-sleep-learned", auto_adopt=False, |
| 645 | + multi_skill_report=False, |
| 646 | + ) |
| 647 | + outcome = run_sleep_cycle(cfg, seed_tasks=self._hinted_tasks()) |
| 648 | + self.assertEqual(staged_skills(outcome.staging_dir), []) |
| 649 | + |
| 650 | + def test_auto_adopt_does_not_promote_per_skill_live_files(self): |
| 651 | + from skillopt_sleep.config import load_config |
| 652 | + from skillopt_sleep.cycle import run_sleep_cycle |
| 653 | + |
| 654 | + with tempfile.TemporaryDirectory() as proj, tempfile.TemporaryDirectory() as home: |
| 655 | + claude_home = os.path.join(home, ".claude") |
| 656 | + research_live = os.path.join( |
| 657 | + claude_home, "skills", "research-skill", "SKILL.md") |
| 658 | + programming_live = os.path.join( |
| 659 | + claude_home, "skills", "programming-skill", "SKILL.md") |
| 660 | + _write(research_live, "# research-skill v1\n") |
| 661 | + _write(programming_live, "# programming-skill v1\n") |
| 662 | + cfg = load_config( |
| 663 | + invoked_project=proj, projects="invoked", backend="mock", |
| 664 | + claude_home=claude_home, |
| 665 | + managed_skill_name="skillopt-sleep-learned", auto_adopt=True, |
| 666 | + multi_skill_report=True, |
| 667 | + ) |
| 668 | + outcome = run_sleep_cycle(cfg, seed_tasks=self._hinted_tasks()) |
| 669 | + self.assertEqual(_read(research_live), "# research-skill v1\n") |
| 670 | + self.assertEqual(_read(programming_live), "# programming-skill v1\n") |
| 671 | + names = [r["skill_name"] for r in staged_skills(outcome.staging_dir)] |
| 672 | + self.assertIn("research-skill", names) |
| 673 | + self.assertIn("programming-skill", names) |
| 674 | + |
378 | 675 |
|
379 | 676 | if __name__ == "__main__": |
380 | 677 | unittest.main() |
0 commit comments