IMPORTANT: This prompt may run in a non-interactive environment.

Non-interactive rules:
- Do not use AskUserQuestion or any interactive prompt/confirmation tool.
- When an optional choice is needed, pick a reasonable default and record the assumption.
- Skip optional confirmations and proceed directly.

Task: perform a structured code research investigation inside the selected CodeRef vault.

Execution context:
- Working directory: `{{projectPath}}`
{{#if repositoryPath}}- Resolved target path: `{{repositoryPath}}`{{/if}}
{{#if targetType}}- Target type: `{{targetType}}`{{/if}}
{{#if vaultId}}- Vault ID: `{{vaultId}}`{{/if}}
{{#if vaultName}}- Vault name: `{{vaultName}}`{{/if}}
{{#if vaultType}}- Vault type: `{{vaultType}}`{{/if}}
{{#if reposDirectory}}- Repositories workspace: `{{reposDirectory}}`{{/if}}
{{#if docsDirectory}}- Documentation output area: `{{docsDirectory}}`{{/if}}
- Research direction: `{{{researchDirection}}}`

{{#if repositoryUrls}}
Ordered repository inputs:
{{#each repositoryUrls}}
- `{{this}}`
{{/each}}
{{else}}
No external repository URLs were provided. Analyze the existing contents already present in the selected CodeRef vault.
{{/if}}

Requirements:
1. Work inside `{{projectPath}}`, which is the selected CodeRef vault root.
2. Keep repository workspace material under `repos/` and research deliverables under `docs/`.
3. Do not write final deliverables outside the selected CodeRef vault root.
4. Base every conclusion on inspected local evidence. Do not invent repository facts.
5. Prefer updating or creating concise markdown research deliverables in `docs/` rather than scattering notes across the vault.

Required runtime workflow:
1. Validate the CodeRef vault layout.
   - Confirm the working directory exists and is accessible.
   - Confirm `repos/` and `docs/` exist; create them if they are missing.
   - Record any assumptions if the existing vault layout is incomplete.
2. Acquire repository material when URLs are provided.
{{#if repositoryUrls}}
   - For each repository URL above, ensure a corresponding checkout exists under `repos/`.
   - If the repository directory does not exist yet, clone it into `repos/`.
   - If the repository directory already exists, inspect its remotes and update it safely.
   - Do not delete unrelated repositories already present in `repos/`.
{{else}}
   - Skip clone/update work and inspect the repositories or files already available in the selected CodeRef vault.
{{/if}}
3. Inspect the available code and documentation.
   - Read the most relevant manifests, README files, key source directories, and architecture notes.
   - Trace the modules, dependencies, flows, and risk areas needed to answer the research direction.
   - If multiple repositories are involved, identify how they relate to each other.
4. Produce research deliverables under `docs/`.
   - Create or update at least one markdown deliverable in `docs/`.
   - Summarize the investigated scope, findings, evidence, risks, and recommended follow-up actions.
   - Use filenames that make the topic discoverable from the vault.
5. Return a short execution summary in the session output.
   - Include what was inspected, which repositories were cloned or updated, what documents were written, and any limitations.

Safety constraints:
- Do not rely on interactive authentication flows.
- If a repository cannot be cloned or updated, continue with the remaining available evidence and report the exact failure.
- Do not rewrite large existing docs unless the research task truly requires it; prefer additive or targeted updates.
- Keep all required output inside the selected CodeRef vault root.
