---
name: init-session
description: >
  (What) Bootstrap session — ensure 7 mandatory files exist.
  (When) Every session start. Must be first skill triggered.
  (Why) Infrastructure gate. (How) Check files → create missing → /check-session.
metadata:
  author: David Seo of GEM².AI
  version: 16.0.0
allowed-tools:
  - Read
  - Write
  - Bash(mkdir *)
  - Bash(date *)
  - Bash(git *)
---

(* TPMN SKILL — init-session *)

(* === Input === *)
A ≜ [
  project_dir: Path,
  7_Mandatory_Files: [
    ".claude/skills/{slug}/SKILL.md"       — project skill,
    ".claude/TPMN-SKILL-STANDARD.md"       — skill authoring standard,
    ".gem-squared/alarm.md"                — mutable state,
    "CLAUDE.md"                            — behavioral rules,
    ".mcp.json"                            — MCP server config,
    ".gitignore"                           — git hygiene,
    ".gem-squared/work-plan/"              — directory
  ]
]

(* === Output === *)
B ≜ [
  project_slug: 𝕊,                   (* sole project identity — derived from SKILL.md name or dir basename *)
  session_started_at: 𝕊,              (* ISO8601 timestamp *)
  mandatory_files: 𝕊                  (* e.g., "All 7 present" or "Created: alarm.md, .gitignore" *)
]

(* === Precondition === *)
P ≜ project_dir ≠ ⊥

(* === Transform === *)
F ≜ <<
  1. Retrieve project_slug:
       Read .claude/skills/*/SKILL.md → extract `name:` field from frontmatter.
       IF no SKILL.md exists → derive from directory name (basename of project_dir).
       IF project_slug not found by any method → derive from basename — always succeeds.
  2. Ensure all 3 canonical directory trees exist (mkdir -p equivalent):
       Tree 1 — .gem-squared/ (20 dirs):
         work-plan, verify-work-logs, truth-logs, archive, evidences, external-skills,
         gem2-core-skills/_preface,
         gem2-core-skills/{archive-work,check-session,end-session,extract-skill,
                          init-session,plan-work,proceed-work,search-kg,
                          search-skill,skill-to-kg,update-work-plan,verify-work}
       Tree 2 — .claude/skills/ (5 dirs):
         agents, {slug}/references, {slug}/assets, {slug}/eval-viewer, {slug}/scripts
       Tree 3 — ~/.claude/skills/ (12 dirs):
         archive-work, check-session, end-session, extract-skill,
         init-session, plan-work, proceed-work, search-kg,
         search-skill, skill-to-kg, update-work-plan, verify-work
       Allowed tool: Bash(mkdir -p {path}) for each missing directory.
  3. FOR each file in 7_Mandatory_Files: check exists.
       IF missing → create with defaults:
         SKILL.md: minimal project identity skeleton (bundled template).
         TPMN-SKILL-STANDARD.md: bundled v3.0 fallback.
         alarm.md: empty counters (PENDING:0|IN_PROGRESS:0|COMPLETED:0|DECOMPOSED:0|ABORTED:0).
         CLAUDE.md: bundled template.
         .mcp.json: minimal MCP config.
         .gitignore: standard gem2 ignores.
         work-plan/: mkdir (already covered by step 2).
       IF exists AND NOT updatable (alarm.md, .mcp.json, .gitignore):
         NEVER overwrite — these contain mutable state or user customizations.
  4. Record session_started_at timestamp in alarm.md ("Last checked" line).
  5. Invoke /skill-to-kg archive (skill hygiene):
       This moves all non-core skills from .claude/skills/ to .gem-squared/external-skills/.
       BEFORE archiving, MUST print a clear notification to the user:
         "TPMN Skill Hygiene: /skill-to-kg will move {N} non-core skills from
          .claude/skills/ to .gem-squared/external-skills/.
          Skills moved: {list of skill names}.
          These skills are NOT deleted — they remain searchable via /search-kg
          and can be restored anytime with: /skill-to-kg restore <skill-name>"
       IF no non-core skills exist → skip silently.
       IF non-core skills exist → print notification, then archive.
       This ensures .claude/skills/ contains only the 12 lifecycle skills
       + the project identity skill — reducing trigger collision.
>>

(* === Constraint === *)
CONSTRAINT ≜ [
  ⊢ project_slug must be resolved before step 2,
  ⊢ NEVER start work — infrastructure readiness only,
  ⊢ NEVER analyze session state — that is /check-session mandate,
  ⊢ NEVER modify alarm.md beyond initial creation + timestamp,
  ⊢ NEVER modify CLAUDE.md beyond initial creation,
  ⊢ NEVER skip file checks — all 7 checked every time,
  ⊢ ALWAYS notify user before /skill-to-kg archive — list skill names and explain they are not deleted
]

(* === Invariant === *)
INV ≜ [
  ⊢ All 3 canonical directory trees exist after this skill completes (28 dirs total),
  ⊢ All 7 mandatory files exist after this skill completes,
  ⊢ project_slug is the sole project identity — derived from SKILL.md name or dir basename,
  ⊢ Non-updatable files (alarm.md, .mcp.json, .gitignore) are NEVER overwritten if they exist,
  ⊢ alarm.md records session_started_at timestamp
]

(* === Post-Execution Routing === *)
Routing ≜ [
  project_slug = ⊥   → STOP, report: "Could not derive project slug",
  files_created ≠ ∅  → report created files, then /skill-to-kg archive (with notification), then /check-session,
  all_present = ⊤    → /skill-to-kg archive (with notification), then /check-session
]
