> ## Documentation Index
> Fetch the complete documentation index at: https://platform.kimi.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Mount GitHub Repositories

> Mount public or private GitHub repositories when creating a session, select a branch or commit, and handle repository preparation errors.

When creating a session, you can mount one or more GitHub repositories into the session sandbox as `github_repository` resources. The agent can read code, modify files, and run tests, which is useful for code review, bug fixes, and code migration tasks.

Mounting a repository only prepares the code. To create pull requests, read issues, or perform other GitHub operations, configure [MCP](https://platform.kimi.ai/docs/hosted-agents/mcp.md) and [Vaults](https://platform.kimi.ai/docs/hosted-agents/vaults.md) separately.

## Prerequisites

Before you start, make sure you have:

* A Kimi API key with access to Hosted Agents; when using the kha CLI, you can run `kha login --base-url https://api.moonshot.ai` first instead.
* An [agent](https://platform.kimi.ai/docs/hosted-agents/agents.md) ID.
* A cloud [execution environment](https://platform.kimi.ai/docs/hosted-agents/environments.md) ID whose build is ready.
* A GitHub repository. To mount a private repository, you also need a GitHub token with read access to the repository contents.

The examples below show both cURL and kha CLI commands. Before using the CLI, [install and authenticate](https://platform.kimi.ai/docs/hosted-agents/kha.md#installation-and-authentication), then set the resource IDs shared by the examples:

```bash theme={null}
export AGENT_ID="agent_your_agent_id"
export ENVIRONMENT_ID="env_your_environment_id"
```

## Mount a public repository

The example below mounts a public repository when creating a session. If you omit `checkout`, the remote default branch is used; if you omit `mount_path`, the repository is cloned to `/workspace/<repo-name>`.

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/sessions" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "agent_id": "$AGENT_ID",
  "environment_id": "$ENVIRONMENT_ID",
  "title": "Repository research task",
  "resources": [
    {
      "type": "github_repository",
      "url": "https://github.com/<owner>/<repo>"
    }
  ]
}
JSON
```

```bash CLI theme={null}
kha sessions create \
  --agent "$AGENT_ID" \
  --environment "$ENVIRONMENT_ID" \
  --title "Repository research task" \
  --repo "https://github.com/<owner>/<repo>"
```

On success, the endpoint returns HTTP `201` and the session object. Save the session `id` from the response as `SESSION_ID`. In this example, the repository directory is `/workspace/<repo>`, where you can have the agent read code, modify files, and run tests.

> [!NOTE]
>
> A successful session creation response only means the request passed validation and was persisted; it does not mean the repository has finished cloning. Repositories are prepared asynchronously in the background, and failures are reported through `session.error` events. See [Handle repository preparation errors](#handle-repository-preparation-errors).

A `github_repository` resource accepts the following fields:

| Field | Type | Required | Description |
| - | - | - | - |
| `type` | `string` | Yes | Fixed to `github_repository`. |
| `url` | `string` | Yes | HTTPS URL of the repository in the format `https://github.com/<owner>/<repo>`. The host, owner, and repository name are normalized to lowercase. |
| `authorization_token` | `string` | Required for private repositories | GitHub token used to access the repository. Optional for public repositories. This field is write-only and is never returned in query responses. |
| `mount_path` | `string` | No | Final clone directory, defaulting to `/workspace/<repo-name>`. Must be under `/workspace` and cannot be `/workspace` itself. |
| `checkout` | `object` | No | Specifies a branch or full commit SHA; when omitted, the remote default branch is used. |

`url` does not accept SSH URLs, `.git` suffixes, trailing slashes, userinfo, query strings, fragments, extra path segments, or ports.

`mount_path` is the final directory, not its parent directory. For example, if you mount a repository at `/workspace/service`, a `README.md` file at the repository root is located at `/workspace/service/README.md`. Mount paths of different repositories cannot be identical or nested inside one another.

## Mount a private repository

Private repositories require a GitHub token with content read permission passed via `authorization_token`. The cURL example below reads the token from the `GITHUB_TOKEN` environment variable, and the CLI example passes it via standard input so the token does not appear in command-line arguments.

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/sessions" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "agent_id": "$AGENT_ID",
  "environment_id": "$ENVIRONMENT_ID",
  "title": "Private repository task",
  "resources": [
    {
      "type": "github_repository",
      "url": "https://github.com/<owner>/<private-repo>",
      "authorization_token": "$GITHUB_TOKEN",
      "mount_path": "/workspace/private-repo",
      "checkout": {"type": "branch", "name": "main"}
    }
  ]
}
JSON
```

```bash CLI theme={null}
printf '%s' "$GITHUB_TOKEN" | kha sessions create \
  --agent "$AGENT_ID" \
  --environment "$ENVIRONMENT_ID" \
  --title "Private repository task" \
  --repo "https://github.com/<owner>/<private-repo>:main" \
  --repo-token-stdin
```

> [!WARNING]
>
> Prefer `--repo-token-stdin` with the CLI. Using `--repo-token` can leave the token in your shell history or process arguments. Do not put tokens in prompts, client logs, or any other content that enters the model context.

The token is used only for authenticating repository access. It never appears in resource query responses and is not automatically used as a credential for GitHub MCP. Grant the minimum permissions required for the task: mounting a private repository requires content read permission, while operations such as creating a pull request require additional permissions determined by your GitHub workflow.

## Select a branch or commit

### Use a branch

Specify the branch name in `checkout`. With the CLI's `--repo` flag, you can append `:<branch>` directly to the repository URL.

```json cURL request body theme={null}
{
  "type": "github_repository",
  "url": "https://github.com/<owner>/<repo>",
  "checkout": {
    "type": "branch",
    "name": "release/v1"
  }
}
```

```bash CLI theme={null}
kha sessions create \
  --agent "$AGENT_ID" \
  --environment "$ENVIRONMENT_ID" \
  --repo "https://github.com/<owner>/<repo>:release/v1"
```

### Use a commit

To pin a commit, `sha` must be the full 40-character SHA. In the CLI, use the general `--resource` flag to pass the complete resource object; this flag accepts JSON and relaxed YAML and can be repeated.

```json cURL request body theme={null}
{
  "type": "github_repository",
  "url": "https://github.com/<owner>/<repo>",
  "checkout": {
    "type": "commit",
    "sha": "0123456789abcdef0123456789abcdef01234567"
  }
}
```

```bash CLI theme={null}
kha sessions create \
  --agent "$AGENT_ID" \
  --environment "$ENVIRONMENT_ID" \
  --resource '{
    type: github_repository,
    url: https://github.com/<owner>/<repo>,
    checkout: {
      type: commit,
      sha: 0123456789abcdef0123456789abcdef01234567
    }
  }'
```

## Mount multiple repositories

Add multiple repository objects to `resources`, or repeat `--repo` in the CLI:

```json cURL request body theme={null}
[
  {
    "type": "github_repository",
    "url": "https://github.com/<owner>/<frontend>",
    "mount_path": "/workspace/frontend",
    "checkout": {"type": "branch", "name": "main"}
  },
  {
    "type": "github_repository",
    "url": "https://github.com/<owner>/<backend>",
    "mount_path": "/workspace/backend",
    "checkout": {"type": "branch", "name": "main"}
  }
]
```

```bash CLI theme={null}
kha sessions create \
  --agent "$AGENT_ID" \
  --environment "$ENVIRONMENT_ID" \
  --repo "https://github.com/<owner>/<frontend>:main" \
  --repo "https://github.com/<owner>/<backend>:main"
```

The example above clones the repositories into `/workspace/frontend` and `/workspace/backend`, respectively. In the current implementation, each session can bind at most 4 GitHub repositories by default, and a session cannot bind the same normalized repository URL more than once. Repositories with the same name under different owners cause a default path conflict, so you must set distinct `mount_path` values explicitly.

When using the CLI, the token read by `--repo-token-stdin` applies to all `--repo` flags. If each repository requires a different token, submit the full request body with `--file` and set `authorization_token` on each resource separately.

Invalid URLs, `checkout` values, or `mount_path` values, mount path conflicts, duplicate repository URLs, and using a [self-hosted environment](https://platform.kimi.ai/docs/hosted-agents/self-hosted-environments.md) (which does not support automatic repository mounting) are rejected synchronously when creating a session or creating or updating a trigger configuration. Field-level errors for duplicate URLs point to `resources[i].url`, while default path conflicts and other mount path conflicts point to `resources[i].mount_path`, where `i` is the resource's index in the `resources` array.

## Query repository bindings

List a session's resources, or query a single repository by its resource binding `id`:

```bash cURL theme={null}
curl "https://api.moonshot.ai/v1/sessions/$SESSION_ID/resources" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"

curl "https://api.moonshot.ai/v1/sessions/$SESSION_ID/resources/$RESOURCE_ID" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha sessions resources list "$SESSION_ID" -o json
kha sessions resources get "$SESSION_ID" "$RESOURCE_ID" -o json
```

`RESOURCE_ID` is the binding `id` returned when creating the session or listing its resources. Query responses never return `authorization_token`.

API responses include `created_at` and `updated_at`, but do not expose repository preparation status or the commit that the platform first resolved and uses for rebuilds. When the request specifies a branch, `checkout` in the response still reflects the requested branch rather than the commit it resolved to. Repository preparation failures are reported through `session.error`; see [Handle repository preparation errors](#handle-repository-preparation-errors).

Repository bindings cannot be added, modified, or deleted after the session is created. To change the URL, branch, commit, mount path, or token, create a new session.

## Handle repository preparation errors

The platform prepares the sandbox and repositories in the background. Inspect session events to check whether a repository-related `session.error` occurred:

```bash cURL theme={null}
curl "https://api.moonshot.ai/v1/sessions/$SESSION_ID/events" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha sessions events "$SESSION_ID" --all
```

When the session sandbox is subsequently used, the platform retries any unfinished repository preparation; querying the session or its resources does not trigger this process. Handle errors based on `data.error_type`:

| `error_type` | Meaning | How to handle |
| - | - | - |
| `repository_authentication_error` | Repository authentication failed. | Check the token and read permission. Create a new session to use a different token. |
| `repository_checkout_error` | Branch or commit checkout failed. | Check the branch name or SHA, and create a new session with the corrected configuration. |
| `repository_forbidden_error` | Access to the repository was denied. | After adjusting GitHub permissions, continue using the original session's sandbox to trigger a repository preparation retry. |
| `repository_not_found_error` | The repository does not exist or is not visible. | Check the URL and access permissions; create a new session if you need to change the configuration. |
| `repository_clone_error` | Clone, fetch, or sandbox preparation failed. | Continue using the original session's sandbox to trigger a repository preparation retry. |

For event subscription, reading history, and reconnecting after a disconnect, see [Event Stream](https://platform.kimi.ai/docs/hosted-agents/event-stream.md).

## Use skills from a repository

After a repository is prepared for the first time, the platform discovers `SKILL.md` files in the repository, parses their names and descriptions, and records each skill's path along with its repository and the first successfully checked-out commit. During discovery, at most 1 MiB of each `SKILL.md` is read to parse its frontmatter; files that remain parseable after truncation are included, while incomplete or unparseable files are skipped. Reading the full file later is still subject to the limits of the file-reading tool.

A normal resume does not rescan the repository. Adding or updating skills in the remote repository does not automatically refresh the set of skills already discovered in the current session; create a new session to refresh it.

When a repo skill has the same name as a skill explicitly attached to the agent (an attached skill), both are retained; skills with the same name from different repositories can also coexist. The skill list injected into the model preserves each skill's name, description, and `SKILL.md` path. The model selects a skill based on the task and description, then reads the corresponding path. Skills are neither overwritten because of duplicate names nor deduplicated by repository preparation completion order.

For the catalog and naming rules of attached skills, see [Skills](https://platform.kimi.ai/docs/hosted-agents/skills.md).

## Workspace recovery and code versions

Repository recovery follows these rules:

* **Normal recovery**: When a valid persistent checkpoint exists, the platform does not re-clone the repository and restores the full workspace, including uncommitted changes and unpushed local commits. Restoring auxiliary metadata does not change `HEAD` or working files, and does not automatically sync the upstream branch.
* **Fresh sandbox**: When no usable checkpoint exists and the mount directory is completely absent, the repository is rebuilt from the first successfully checked-out commit. That commit is the rebuild baseline: it does not represent the workspace's current `HEAD` and does not change when the remote branch advances, the agent runs `git fetch` or `git pull`, or local commits are created.
* **Preserve existing state**: If the mount directory still exists, the platform does not automatically delete and rebuild it even when auxiliary metadata is missing or the Git working tree is unusable; it preserves the directory and reports an error.
* **Recovery boundary**: Workspace changes that were not saved to a persistent checkpoint cannot be recovered. Event history is not a filesystem backup.

To update code from upstream, have the agent run `git fetch` or `git pull` explicitly rather than relying on the recovery process to move the branch `HEAD`.

## Use GitHub workflows

Repository resources do not automatically configure GitHub MCP or create pull requests. To use those capabilities, first declare GitHub MCP on the agent and configure authentication through a vault, then have the agent create branches, commit changes, or open pull requests based on the mounted repository.

For configuration steps, see [Configure MCP Servers](https://platform.kimi.ai/docs/hosted-agents/mcp.md) and [Vaults](https://platform.kimi.ai/docs/hosted-agents/vaults.md). Repository resources and MCP are authenticated separately: the `authorization_token` used to mount a repository is not automatically provided to MCP.

## Limitations

* [Self-hosted environments](https://platform.kimi.ai/docs/hosted-agents/self-hosted-environments.md) do not support automatic repository mounting.
* Git submodules are not initialized automatically.
* Git LFS objects are not pulled automatically.
* GitHub MCP is not configured and pull requests are not created automatically.
* `git pull` is not run automatically when the remote branch advances. Have the agent run `git fetch` or `git pull` explicitly when an update is needed.
* When reusing the `github_repository` structure in [triggers](https://platform.kimi.ai/docs/hosted-agents/triggers.md), repositories are also prepared asynchronously in the background, so you must continue to handle error events for the corresponding sessions.

## Next steps

> [card] [Connect GitHub MCP](https://platform.kimi.ai/docs/hosted-agents/mcp.md)
>
> Configure external tools on your agent for GitHub workflows.

> [card] [Handle session events](https://platform.kimi.ai/docs/hosted-agents/event-stream.md)
>
> Subscribe to execution progress and handle asynchronous repository preparation errors.

> [card] [Schedule repository tasks](https://platform.kimi.ai/docs/hosted-agents/triggers.md)
>
> Run code inspection and maintenance tasks on a schedule with triggers.
