# /init -- Environment Discovery

Discover the current environment and write a session snapshot to `.session/environment.json`. This gives you situational awareness of the machine, network, available tools, running services, and current work state.

Run this command on first boot if `.session/environment.json` does not exist or is older than 1 hour.

## Behaviour

Execute all discovery steps below using shell commands, then assemble the results into a single JSON object and write it to `.session/environment.json` at the project root.

**Important:** The `.session/` directory should already exist (created by `dave.sh`). If it does not, create it with `mkdir -p .session`.

### 1. Machine identity

```bash
hostname
uname -s
uname -m

# Resolve project root: trust $DAVE_ROOT if exported, otherwise walk two parents up from cwd.
# cwd when /init runs is the PA run folder (<project-root>/agents/<paName>/).
if [ -n "$DAVE_ROOT" ]; then
  echo "$DAVE_ROOT"
else
  cd "$(pwd)/../.." && pwd
fi
```

Capture as:
```json
{
  "hostname": "<hostname output>",
  "os": "<uname -s output>",
  "arch": "<uname -m output>",
  "daveRoot": "<resolved project root; absolute path>"
}
```

### 2. Network

```bash
# Local IP (portable across macOS and Linux)
if command -v ip >/dev/null 2>&1; then
  ip route get 1.1.1.1 2>/dev/null | awk '{for(i=1;i<=NF;i++) if ($i=="src") print $(i+1)}'
else
  ipconfig getifaddr en0 2>/dev/null || echo "unknown"
fi

# Internet connectivity
curl -sf --max-time 3 https://httpbin.org/status/200 >/dev/null 2>&1 && echo "true" || echo "false"
```

Capture as:
```json
{
  "localIp": "<detected IP or 'unknown'>",
  "internet": true
}
```

### 3. Available tools

Check each tool's availability and version. Use `command -v <tool>` to test presence, then capture the version string.

| Tool | Version command |
|---|---|
| git | `git --version` |
| node | `node --version` |
| python | `python3 --version` |
| docker | `docker --version` |
| claude | `claude --version` |

Capture as:
```json
{
  "git": "2.x.x" or false,
  "node": "v22.x.x" or false,
  "python": "3.x.x" or false,
  "docker": "27.x.x" or false,
  "claude": "1.x.x" or false
}
```

Use `false` (boolean) if the tool is not installed. Use the version string if it is.

### 4. Services

Check if the workspace server is running by testing the configured port:

```bash
# Read port from preferences or default to 4200
curl -sf --max-time 2 http://127.0.0.1:${PORT:-4200}/health >/dev/null 2>&1 || \
curl -sf --max-time 2 http://127.0.0.1:${PORT:-4200}/ >/dev/null 2>&1
```

Capture as:
```json
{
  "workspace": {
    "url": "http://127.0.0.1:<port>",
    "running": true
  }
}
```

### 5. Work state

Run the HQ CLI overview and read next-session notes:

```bash
# Overview
node core/tools/desk/bin/desk overview 2>/dev/null || echo "(desk overview not available)"

# Next-session notes -- read the PA's own file
# The PA name comes from operator/config/preferences.json
cat agents/<paName>/next-session.md 2>/dev/null || echo ""
```

Read the PA name from `operator/config/preferences.json` (the `paName` field).

Capture as:
```json
{
  "overview": "<desk overview stdout>",
  "nextSession": "<contents of next-session.md or null>",
  "capturedAt": "<ISO8601>"
}
```

Use `null` (not the string "null") if next-session.md does not exist.

### 6. Assemble and write

Combine all sections into the final JSON structure:

```json
{
  "capturedAt": "<ISO8601 timestamp>",
  "machine": { "hostname": "...", "os": "...", "arch": "...", "daveRoot": "..." },
  "network": { "localIp": "...", "internet": true },
  "tools": { "git": "...", "node": "...", "python": "...", "docker": false, "claude": "..." },
  "services": { "workspace": { "url": "...", "running": true } },
  "workState": {
    "overview": "...",
    "nextSession": "..." or null,
    "capturedAt": "<ISO8601>"
  }
}
```

Write this to `.session/environment.json` at the project root. Use camelCase for all property names. Ensure the output is valid JSON (no trailing commas, proper quoting).

### 7. Report

After writing, give a brief one-line summary of what was discovered. Example:

> Environment captured: macbook-pro / macOS arm64, internet up, 4/5 tools available, workspace running on :4200.

Do not dump the full JSON back to the user.
