> For the complete documentation index, see [llms.txt](https://ai-os-and-trend-finder.gitbook.io/ai-os-and-trend-finder-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai-os-and-trend-finder.gitbook.io/ai-os-and-trend-finder-docs/.spec_system/archive/phases/phase_36/session_06_adaptive_music_engine_expansion.md).

# Session 06: Adaptive Music Engine Expansion

**Session ID**: `phase36-session06-adaptive-music-engine-expansion` **Status**: Complete **Completed**: 2026-06-28 **Estimated Tasks**: \~12-25 **Estimated Duration**: 2-4 hours

***

## Objective

Add the minimal runtime audio-engine support needed for adaptive stingers without destabilizing existing music and SFX behavior.

***

## Scope

### In Scope (MVP)

* Add a second music or ambience bus, transient gain automation, or ducking rules only as needed for adaptive stingers.
* Define how combat start, low HP, boss reveal, objective unlock, final defense, victory transition, and defeat transition events request stingers.
* Add tests for bus interaction, fallback, mute, volume, and event dispatch behavior.
* Document any ducking or gain automation behavior added.

### Out of Scope

* Bulk stinger asset generation.
* Replacing existing music crossfades, one-shot behavior, or heartbeat loop without evidence.

***

## Prerequisites

* [x] Session 01 balance findings support adding adaptive stingers.
* [x] Existing music, SFX, and heartbeat loop behavior are covered by tests or manual evidence.

***

## Deliverables

1. Documented adaptive-audio runtime contract.
2. Minimal engine support for stingers or tension layers.
3. Focused tests covering the new contract.

***

## Success Criteria

* [x] Existing music crossfades, heartbeat loop, one-shots, mute, volume preferences, autoplay unlock, and silent fallback still work.
* [x] New adaptive behavior is covered by focused tests.
* [x] Docs explain ducking or gain automation behavior if added.
* [x] The product outcome is proven end to end: adaptive requests execute through real runtime paths, failures recover through existing audio fallback behavior, and browser review confirms the contract does not clutter gameplay.

## Folded Source Detail Routed To This Session

### Source Findings Owned

* A5 says adaptive music is possible but requires engine work.
* Pre-session audio docs explicitly said there was no sidechain or event-based music ducking.
* `audio.ts` supports one active music track and heartbeat loop.
* Batch 5 says music moments should be added only after the runtime contract supports them cleanly.

### Engine Contract Candidates

* Second music or ambience bus.
* Transient music gain automation.
* Ducking rules.
* Stinger request and cooldown behavior.
* Fallback behavior when decode, AudioContext, autoplay unlock, or asset load fails.

### State Changes To Support

* Combat start.
* Low HP.
* Boss reveal.
* Objective unlock.
* Final defense.
* Victory transition.
* Defeat transition.

### Constraints

* This is engine work, not bulk asset replacement.
* Do not replace existing music crossfades, heartbeat loop, one-shots, mute, volume preferences, autoplay unlock, or silent fallback without evidence.
* Add tests and manual browser checks because audio output cannot be fully validated by headless tests.
* Skip or reduce this scope if Session 01 playtesting shows adaptive stingers are unnecessary or too intrusive.

## Implementation Contract Notes

Session 06 implements an adaptive request contract in the existing Web Audio engine rather than a parallel mixer. The request IDs are:

* `combat_start`
* `low_hp`
* `boss_reveal`
* `objective_unlock`
* `final_defense`
* `victory_transition`
* `defeat_transition`

The engine applies per-request cooldowns and transiently ducks only the primary music and theme ambience lanes. Master mute, music volume, SFX volume, lazy decode, autoplay unlock, heartbeat, theme ambience, terminal music, and silent-engine fallback remain on their existing paths.

Renderer routing derives adaptive requests from existing simulation events and previous/current snapshot transitions. Objective, final-defense, victory, and defeat paths request ducking without replaying provisional one-shots when the simulation event already emitted the same committed cue.

Session 06 deliberately adds no new audio assets, dependencies, settings, schema fields, save migrations, simulation RNG changes, remote content paths, or public demo bridge calls.

## Validation Evidence To Date

* `bun run typecheck` passed after the engine API, cooldown, ducking, adapter, renderer, browser proof, and documentation edits.
* `bunx vitest run src/extensions/ai-rogue/runtime/__tests__/audio.test.ts src/extensions/ai-rogue/runtime/__tests__/renderer-audio-adapter.test.ts` passed with 2 files and 34 tests after focused adaptive coverage was added.
* `bunx vitest run src/extensions/ai-rogue/runtime/__tests__/combat.test.ts src/extensions/ai-rogue/runtime/__tests__/protocols.test.ts src/extensions/ai-rogue/runtime/__tests__/compile.test.ts src/extensions/ai-rogue/runtime/__tests__/objective-lock.test.ts` passed with 4 files and 26 tests.
* `bunx playwright test tests/e2e/ai-rogue-runtime.spec.ts -g "AI Rogue browser audio pack fetches enemy, boss, and theme audio"` passed on desktop and mobile Chromium proof viewports.
* `bun run lint`, `bun run typecheck:scripts`, and `bash scripts/check-asset-sizes.sh` passed.
* Targeted ASCII/LF scan passed for touched runtime, test, docs, phase stub, and session files.

## Session 07 Handoff

Dedicated adaptive stinger generation remains Session 07 scope. Session 07 can replace the provisional cue mapping behind the same request IDs without changing renderer derivation or simulation contracts.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ai-os-and-trend-finder.gitbook.io/ai-os-and-trend-finder-docs/.spec_system/archive/phases/phase_36/session_06_adaptive_music_engine_expansion.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
