---
name: draft-load-team
description: >
  Pulls the latest team context from the shared GitHub repo directly into the
  local context/ directory. Reads CHANGES.jsonl, filters to entries since last
  load, writes updated context files. Personal layer is never touched.
---

# /draft:load-team — Load Team Context

Pull the latest shared context from the team repo into your local workspace.

After loading, your `context/` IS the shared brain — ready for the next session.
Your `personal/` layer is never touched by this command.

---

## Step 1: Read config and verify auth

Read `$DRAFT_WORKSPACE/config/collaboration.md`.
Read `$DRAFT_WORKSPACE/config/local.md`.

If `config/collaboration.md` is missing or `mode` is not `github`:
```
Error: Collaboration not configured. Run /draft:setup-collab first.
```
Hard stop.

Verify gh auth:
```bash
gh auth status
```

If fails:
```
GitHub auth expired. Run `gh auth login --web` then re-run /load-team.
```
Hard stop.

---

## Step 2: Clone and read CHANGES

```bash
TMPDIR=$(mktemp -d /tmp/draft-load-XXXX)
git clone [team_repo_url] "$TMPDIR"
```

If git clone fails:
```bash
rm -rf "$TMPDIR"
```
```
Couldn't reach [team_repo_url]. Check your network and repo access.
```
Exit 1. DO NOT update `config/local.md`.

Set the subdir prefix based on `team_repo_subdir`:
```bash
# team_repo_subdir = root → SUBDIR_PATH="$TMPDIR"
# team_repo_subdir = .draft → SUBDIR_PATH="$TMPDIR/.draft"
# team_repo_subdir = [custom] → SUBDIR_PATH="$TMPDIR/[custom]"
```

Check for CHANGES.jsonl:
```bash
CHANGES_FILE="$SUBDIR_PATH/CHANGES.jsonl"
```

If `CHANGES_FILE` does not exist:
```bash
rm -rf "$TMPDIR"
```
```
No change history in the team repo yet. Ask your curator to run /publish-team first.
```
Exit 0.

---

## Step 3: Merge shared config

Read `$SUBDIR_PATH/config/collaboration.md`.

Repo version wins: overwrite all shared fields (`mode`, `team_repo_url`, `team_repo_subdir`, `repo_is_private`, `teammates`) in local `$DRAFT_WORKSPACE/config/collaboration.md` with repo values.

**Never touch `config/local.md`** — machine state is always local-only.

This is how new teammates added by the curator propagate automatically to all existing teammates.

---

## Step 4: Filter and deduplicate CHANGES

Read all entries from `CHANGES.jsonl` (one JSON object per line).

Filter: keep entries where `ts` > `config/local.md:last_loaded`
- If `last_loaded` is `null`: keep ALL entries (initial load).

Deduplicate: build a set of `id` values already seen; skip any duplicate IDs.

If no entries remain after filtering:
```bash
rm -rf "$TMPDIR"
```
```
Team context is up to date (last loaded: [last_loaded]).
```
Exit 0.

---

## Step 5: Write context files

For each unique `file` path in the filtered entries:

```bash
SOURCE="$SUBDIR_PATH/[file]"           # e.g. $SUBDIR_PATH/context/product/index.md
DEST="$DRAFT_WORKSPACE/[file]"         # e.g. ~/.draft/workspace/context/product/index.md
```

- If `SOURCE` exists: copy to `DEST` (overwrites local version of that dimension).
- If `SOURCE` doesn't exist: skip with warning: `"[file] listed in CHANGES but not found in repo — skipping."`

Only `context/` paths appear in CHANGES entries. `personal/` is never touched.

Create destination directory if needed:
```bash
mkdir -p "$(dirname "$DEST")"
```

Cleanup:
```bash
rm -rf "$TMPDIR"
```

---

## Step 6: Surface summary and update local state

Print:
```
Team context loaded: [N] changes since [last_loaded or "initial load"]

[For each filtered entry]:
  • [dimension]: [summary]
```

Update `$DRAFT_WORKSPACE/config/local.md`:
- Set `last_loaded` to current ISO 8601 timestamp.
