<language_directive>
IMPORTANT LANGUAGE INSTRUCTION: Please use English (US) for all output fields.
</language_directive>

<task>
Return one JSON object for bundled proposal optimization.
The response MUST follow UTF-8 without BOM text semantics. Do not emit a BOM, U+FEFF, zero-width no-break space, or any hidden prefix/control characters before the opening `{`.
</task>

<authority>
- Current input is the authoritative source of repository boundaries, permissions, and editable scope.
- Do not invent repositories, permissions, project constraints, or undocumented hero settings.
- Word-cloud memory is advisory context only.
</authority>

<input>
{{#if hasCustomTitle}}
<title>{{ title }}</title>
{{else}}
<title>(default title placeholder - you may suggest optimizedTitle)</title>
{{/if}}
<description>
{{{ description }}}
</description>
</input>

{{#if wordCloudContext}}
<word_cloud_summary>
<project_key>{{ wordCloudContext.ProjectKey }}</project_key>
{{#if wordCloudContext.HighFrequencyTerms}}
<high_frequency_terms>
{{#each wordCloudContext.HighFrequencyTerms}}
- term: {{ Term }} | frequency: {{ Frequency }} | focusState: {{ FocusState }}{{#if ClassicPhrasing}} | classicPhrasing: {{ ClassicPhrasing }}{{/if}}{{#if CorrectionHint}} | correctionHint: {{ CorrectionHint }}{{/if}}{{#if ConceptHint}} | conceptHint: {{ ConceptHint }}{{/if}}
{{/each}}
</high_frequency_terms>
{{/if}}
{{#if wordCloudContext.CanonicalTerms}}
<canonical_terms>{{#each wordCloudContext.CanonicalTerms}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}</canonical_terms>
{{/if}}
{{#if wordCloudContext.RepositoryHints}}
<repository_hints>{{#each wordCloudContext.RepositoryHints}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}</repository_hints>
{{/if}}
{{#if wordCloudContext.NegativePatternHints}}
<negative_pattern_hints>{{#each wordCloudContext.NegativePatternHints}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}</negative_pattern_hints>
{{/if}}
{{#if wordCloudContext.Diagnostics}}
<memory_diagnostics>
{{#each wordCloudContext.Diagnostics}}
- [{{ Severity }}] {{ Code }}: {{ Message }}
{{/each}}
</memory_diagnostics>
{{/if}}
</word_cloud_summary>
{{/if}}

{{#if dungeonSettings}}
<dungeon_settings>
<script_key>{{ dungeonSettings.ScriptKey }}</script_key>
<selected_hero_id>{{ dungeonSettings.SelectedHeroId }}</selected_hero_id>
<captain_hero_id>{{ dungeonSettings.CaptainHeroId }}</captain_hero_id>
<roster_id>{{ dungeonSettings.RosterId }}</roster_id>
{{#if dungeonSettings.ExtensionFields}}
<extension_fields>
{{#each dungeonSettings.ExtensionFields}}
- {{@key}}: {{this}}
{{/each}}
</extension_fields>
{{/if}}
</dungeon_settings>
{{/if}}

{{#if imageContexts}}
<local_image_context>
{{#each imageContexts}}
- File: {{ FileName }}
  - Physical path: {{ StoragePath }}
{{/each}}
</local_image_context>
{{/if}}

{{#if optimizationDirection}}
<optimization_direction>{{ optimizationDirection }}</optimization_direction>
{{/if}}

<keyword_semantics>
Keywords must capture historical focus states, classic phrasing, and concept-correction hints that improve description accuracy.
Each keyword should reflect why the term matters, not just the raw noun.
</keyword_semantics>

<optimized_description_requirements>
`optimizedDescription` is a first-class bundled output and MUST satisfy all of the following requirements:

- Preserve all Repo / Project / Vault scope information mentioned in the original description, including names, paths, physical paths, editable markers, and intended purpose semantics.
- If the user explicitly provides repository, project-reference, or vault references, editable scope, or physical paths, reuse them exactly; do not rewrite, validate, supplement, or invent any repository path, project path, vault physical path, permission, or purpose.
- Frontend-composed Repo / Project / Vault scope blocks such as `### Target Repositories`, `### 目标仓库`, `### Target Project References`, `### 目标项目引用`, `### Target Vaults`, or `### 目标 Vault` should be normalized into standardized Markdown tables inside `optimizedDescription` instead of keeping the original bullet-list layout verbatim.
- If the original description includes user-authored reference sections, HagIndex references, or Markdown tables outside those system-composed Repo / Project / Vault scope blocks, preserve them verbatim inside `optimizedDescription` without reformatting or deleting them.
- Preserve original-language semantics; Chinese input stays Chinese, English input stays English.
- When `hasCustomTitle=true`, title semantics MUST shape `optimizedDescription`; when `hasCustomTitle=false`, derive the description from `description` only.
- `optimizedDescription` MUST be structured Markdown and MUST begin immediately with the first level-two heading, with no preamble text.
- The section structure inside `optimizedDescription` MUST remain aligned with the legacy prompt:
  - `## Involved Repositories`
  - `## Context`
  - `## Problem`
  - `## Solution`
  - `## Impact`
  - `## Additional References` (only when the original description includes those references)
- `## Involved Repositories` MUST remain the single top-level scope section; when scope exists, it should contain the following sub-blocks as needed:
  - `### Repos`
  - `### Projects`
  - `### Vaults`
- Repo information MUST use a standardized Markdown table with exact headers:
  `| Repository Name | Path | Edit Permission | Purpose |`
- Project-reference information MUST use a separate standardized Markdown table with exact headers:
  `| Project Name | Path | Edit Permission | Purpose |`
- Vault information MUST use a separate standardized Markdown table with exact headers:
  `| Vault Name | Path | Edit Permission | Purpose |`
- When repos, project references, and vaults coexist, render separate `### Repos`, `### Projects`, and `### Vaults` tables inside the same `## Involved Repositories` section; do not merge rows into one table and do not create another top-level scope heading.
- Editable repositories and editable vaults MUST use `[editable]` in the Edit Permission column; reference-only entries MUST use `-`.
- Editable project references MUST use `[editable]` in the Edit Permission column; reference-only entries MUST use `-`.
- If the user did not specify any repositories, project references, or vaults, explicitly state `User has not specified any repositories, project references, or vaults` and do not fabricate a repo table, project table, or vault table.
- Keep every Markdown table multi-line: the header row, separator row, and each data row MUST remain on separate lines and MUST NOT be flattened into one line.
- Do not wrap `optimizedDescription` in Markdown code fences.
- `optimizedDescription` MUST remain UTF-8-without-BOM-ready Markdown text. Do not prepend a BOM, U+FEFF, or hidden prefix/control characters.
- Keep the same clarity, professional tone, hierarchy, and actionability across the full bundled response.
</optimized_description_requirements>

<output_contract>
Return JSON only. No markdown fence. No commentary.
The JSON response itself MUST follow UTF-8 without BOM semantics, and the first emitted character MUST be `{`.
Any file-ready string field, especially `optimizedDescription`, `optimizedProposalMarkdown`, and `optimizedTitle` when provided, MUST omit BOM / U+FEFF / hidden prefix characters and stay ready to write as UTF-8 without BOM text.

Required fields:
- optimizedProposalName: kebab-case, lowercase, <= 100 chars
- optimizedDescription: rich markdown-ready text which MUST satisfy `optimized_description_legacy_requirements`
- keywords: non-empty array

Optional fields:
- optimizedTitle: include when a better human-readable title helps, especially for default-title sessions
- optimizedProposalMarkdown: optional draft content for later proposal generation
- dungeonSettings: echo or lightly normalize the provided settings block when helpful

JSON shape:
{
  "optimizedTitle": "optional string",
  "optimizedProposalMarkdown": "optional string",
  "optimizedProposalName": "required-kebab-case",
  "optimizedDescription": "required string",
  "keywords": [
    {
      "term": "required string",
      "canonicalTerm": "optional string",
      "focusState": "primary|secondary|watch|deprecated|neutral",
      "classicPhrasing": "optional string",
      "correctionHint": "optional string",
      "conceptHint": "optional string"
    }
  ],
  "dungeonSettings": {
    "scriptKey": "optional string",
    "selectedHeroId": "optional string",
    "captainHeroId": "optional string",
    "rosterId": "optional string",
    "extensionFields": {
      "futureKey": "futureValue"
    }
  }
}
</output_contract>
