Nine findings from a review of the phase-14 stack. The one about a retry withdrawing a memory is not a bug — a memory anchored to a node describes that node, and it goes when the node goes. The root is the exception, and it is the only one: migration 62 parked every memory written before memories had coordinates on depth 0, so withdrawing the opening node would retire a whole bank nobody attached there. A memory with no source range covers no story and now stays; a summary that genuinely ends there is still withdrawn. The rest are repairs. * The adventure list quoted whichever attempt was written last rather than the one the story tells, so switching back left the index disagreeing with the page. * A v1 import gave a typed memory no depth, rebuilding the NULL the migration exists to remove — invisible until the imported adventure forked. * The action cap counted a v1 file's turns, and a turn expands into a row per saved attempt, so a file inside the cap could write a multiple of it. * Forking a live node on a borrowed ancestor promoted a sibling on a branch the caller never named. It is a branch switch, and now says so. * Switching attempts left the state, status and memory panels reading the previous take: the story does not change length, so nothing keyed on its length noticed. Same class as the branch-switch bug this phase already fixed. * A retry after switching back numbered the new attempt into the middle of the group instead of the end. * The cursor backfill numbered every action in the table once per adventure; correlated to the adventure being updated, it is an index lookup instead. * Renaming a branch answered own_actions=0. And one behaviour change recorded rather than repaired: script-visible history and actionCount no longer count blank-text rows. That is the right shape and there is no reading compatible with both, so plan/14 says so. 409 tests. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015H5qiyiR7gtFQaoDphHZ3g
112 lines
4.6 KiB
Python
112 lines
4.6 KiB
Python
"""Runs an adventure's enabled scripts through a turn's hook points, applying
|
|
state and story-card mutations back to the database after each hook."""
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from .. import models
|
|
from ..context import history as context_history
|
|
from .engine import run_hook
|
|
|
|
MAX_STORY_CARDS = 5000 # AI Dungeon's per-adventure sanity cap
|
|
|
|
|
|
class ScriptPipeline:
|
|
def __init__(self, adventure: models.Adventure, db: Session):
|
|
self.adventure = adventure
|
|
self.db = db
|
|
self.logs: list[str] = []
|
|
self.errors: list[str] = []
|
|
|
|
@property
|
|
def message(self) -> str | None:
|
|
state = self.adventure.script_state
|
|
msg = state.get("message") if isinstance(state, dict) else None
|
|
return msg if isinstance(msg, str) and msg.strip() else None
|
|
|
|
def _history(self) -> list[dict]:
|
|
# The path, not `adventure.actions` — that collection is every branch's
|
|
# actions, and this is the documented history API a user script reads.
|
|
# Handing a script the siblings of the turn it is running on would be
|
|
# the same bug as building a prompt from them, only user-visible.
|
|
#
|
|
# `story_actions` also drops blank-text rows, which `adventure.actions`
|
|
# kept, so this array is shorter than it used to be for an adventure
|
|
# that has any — and `info.actionCount` counts the same way. That is
|
|
# deliberate: a row with no text is this app's bookkeeping, it has no
|
|
# counterpart in the AI Dungeon history a ported script was written
|
|
# against, and the prompt has never included one. A script keyed on
|
|
# "every N actions" will land on different turns than it did before
|
|
# phase 14; there is no reading of this that is compatible with both.
|
|
return [
|
|
{"text": a.text, "rawText": a.text, "type": a.type}
|
|
for a in context_history.story_actions(self.adventure)
|
|
]
|
|
|
|
def _cards(self) -> list[dict]:
|
|
return [
|
|
{"id": c.id, "keys": c.keys, "entry": c.entry, "type": c.type}
|
|
for c in self.adventure.story_cards
|
|
]
|
|
|
|
def _info(self) -> dict:
|
|
return {
|
|
"actionCount": context_history.count(self.adventure),
|
|
"characterNames": [],
|
|
"memoryLength": len(self.adventure.memory),
|
|
"maxChars": 0,
|
|
}
|
|
|
|
def _apply_cards(self, returned: list) -> None:
|
|
existing = {c.id: c for c in self.adventure.story_cards}
|
|
seen_ids = set()
|
|
added = 0
|
|
for item in returned:
|
|
if not isinstance(item, dict):
|
|
continue
|
|
card_id = item.get("id")
|
|
keys = str(item.get("keys") or "")
|
|
entry = str(item.get("entry") or "")
|
|
card_type = str(item.get("type") or "")
|
|
if card_id in existing:
|
|
seen_ids.add(card_id)
|
|
card = existing[card_id]
|
|
card.keys, card.entry, card.type = keys, entry, card_type
|
|
elif len(existing) + added < MAX_STORY_CARDS:
|
|
self.db.add(
|
|
models.StoryCard(
|
|
adventure_id=self.adventure.id,
|
|
keys=keys, entry=entry, type=card_type,
|
|
)
|
|
)
|
|
added += 1
|
|
for card_id, card in existing.items():
|
|
if card_id not in seen_ids:
|
|
self.db.delete(card)
|
|
|
|
def run(self, hook: str, text: str) -> tuple[str, bool]:
|
|
"""Chain `hook` across all enabled scripts. Returns (text, stop)."""
|
|
state = self.adventure.script_state if isinstance(self.adventure.script_state, dict) else {}
|
|
for script in self.adventure.scripts:
|
|
hook_js = getattr(script, f"{hook}_js")
|
|
if not script.enabled or not hook_js.strip():
|
|
continue
|
|
result = run_hook(
|
|
script.library_js, hook_js, text, state,
|
|
self._history(), self._cards(), self._info(),
|
|
)
|
|
if result.error:
|
|
self.errors.append(f"{script.name} ({hook}): {result.error}")
|
|
continue # a broken script never breaks the turn
|
|
self.logs.extend(f"[{script.name}/{hook}] {line}" for line in result.logs)
|
|
self._apply_cards(result.story_cards)
|
|
state = result.state
|
|
self.adventure.script_state = state
|
|
self.db.commit()
|
|
text = result.text
|
|
if result.stop:
|
|
return text, True
|
|
return text, False
|
|
|
|
def report(self) -> dict:
|
|
return {"logs": self.logs, "errors": self.errors, "message": self.message}
|