AI turns documents or topics into real, native PowerPoint decks—with native shapes, transitions and animations, data-backed charts and tables on demand, audio narration from speaker notes, and support for your own .pptx templates. · by Hugo He
refactor(confirm-ui): enforce a one-run two-stage lifecycle
hugohe3406a40b24589
diff --git a/skills/ppt-master/scripts/confirm_ui/server.py b/skills/ppt-master/scripts/confirm_ui/server.pyindex 8330a0fe..031efbe2 100644--- a/skills/ppt-master/scripts/confirm_ui/server.py+++ b/skills/ppt-master/scripts/confirm_ui/server.py@@ -2,9 +2,9 @@"""PPT Master - Template and Strategist confirmation UI Server (Steps 3-4)-Lightweight Flask backend for the independent Step-3 template selection and-interactive Step-4 Strategist confirmation page. The agent writes template-input to ``<project>/confirm_ui/template_options.json``; template selection is+Lightweight Flask backend for conditional Step-3 template selection and the+interactive Step-4 Strategist confirmation page. When triggered, the agent+writes ``<project>/confirm_ui/template_options.json``; template selection ispersisted separately from the Strategist contract. Strategist writes each stage to``<project>/confirm_ui/recommendations.stageN.json``; this server selects thecurrent stage from ``result.json`` and renders it as a clickable page (color@@ -47,7 +47,6 @@ import threadingimport timeimport urllib.errorimport urllib.request-import uuidimport webbrowserfrom pathlib import Pathfrom typing import Optional@@ -88,11 +87,9 @@ LOCK_FILE_NAME = '.confirm_ui.lock'# Round-trip/session files, all under <project_path>/confirm_ui/.CONFIRM_DIR_NAME = 'confirm_ui'-LEGACY_RECOMMENDATIONS_NAME = 'recommendations.json'RECOMMENDATION_STAGE_NAMES = {1: 'recommendations.stage1.json',2: 'recommendations.stage2.json',- 3: 'recommendations.stage3.json',}RESULT_NAME = 'result.json'SESSION_NAME = 'session.json'@@ -585,6 +582,12 @@ def _read_template_selection(selection_file: Path) -> dict:"""Read a selection and revalidate it against current indexed options."""data = _read_json_object(selection_file)_validate_template_selection(data)+ options_file = selection_file.parent / TEMPLATE_OPTIONS_NAME+ if not _is_newer(selection_file, options_file):+ raise ValueError(+ f'{TEMPLATE_SELECTION_NAME} must be confirmed after the current '+ f'{TEMPLATE_OPTIONS_NAME}'+ )options, candidates = _build_template_options(selection_file.parent)if data['options_sha256'] != options['options_sha256']:raise ValueError(@@ -639,7 +642,13 @@ def _read_template_handoff(project_path: Path, handoff_file: Path) -> dict:"""Read a handoff and bind it to the current selection and installed state."""data = _read_json_object(handoff_file)_validate_template_handoff(data)- selection = _read_template_selection(handoff_file.parent / TEMPLATE_SELECTION_NAME)+ selection_file = handoff_file.parent / TEMPLATE_SELECTION_NAME+ selection = _read_template_selection(selection_file)+ if not _is_newer(handoff_file, selection_file):+ raise ValueError(+ f'{TEMPLATE_HANDOFF_NAME} must be completed after the current '+ f'{TEMPLATE_SELECTION_NAME}'+ )if data['mode'] != selection['mode']:raise ValueError(f'{TEMPLATE_HANDOFF_NAME} mode does not match selection')if data['selection_sha256'] != selection['selection_sha256']:@@ -705,9 +714,13 @@ def _complete_template_phase(project_path: Path) -> int:def _reset_template_phase(confirm_dir: Path) -> int:- """Remove only the two mutable Step-3 receipts so the user may reselect."""+ """Remove the one-run Step-3 artifacts before a fresh UI lifecycle."""removed = []- for filename in (TEMPLATE_SELECTION_NAME, TEMPLATE_HANDOFF_NAME):+ for filename in (+ TEMPLATE_HANDOFF_NAME,+ TEMPLATE_SELECTION_NAME,+ TEMPLATE_OPTIONS_NAME,+ ):path = confirm_dir / filenametry:path.unlink()@@ -718,9 +731,9 @@ def _reset_template_phase(confirm_dir: Path) -> int:logger.error('cannot remove %s: %s', path, exc)return 1if removed:- logger.info('reset template phase receipts: %s', ', '.join(removed))+ logger.info('reset template phase artifacts: %s', ', '.join(removed))else:- logger.info('template phase receipts already absent')+ logger.info('template phase artifacts already absent')return 0@@ -749,8 +762,8 @@ def _template_stage1_ready_error(project_path: Path, confirm_dir: Path) -> Optiostage1_data = _read_json_object(stage1_file)if _recommendation_stage(stage1_data) != 1:return f'{stage1_file.name} does not declare stage1'- if stage1_file.stat().st_mtime_ns < handoff_file.stat().st_mtime_ns:- return f'{stage1_file.name} is older than {TEMPLATE_HANDOFF_NAME}'+ if not _is_newer(stage1_file, handoff_file):+ return f'{stage1_file.name} must be newer than {TEMPLATE_HANDOFF_NAME}'except (OSError, json.JSONDecodeError, ValueError) as exc:return f'cannot validate fresh Stage 1 recommendations: {exc}'return None@@ -821,6 +834,40 @@ def _confirmation_launch_error() -> Optional[str]:"""Return why neither the template phase nor current Strategist stage can launch."""options_file = confirm_dir / TEMPLATE_OPTIONS_NAME+ result_file = confirm_dir / RESULT_NAME+ result_stage = _result_stage(result_file)+ fresh_stage1 = _fresh_stage1_restart(confirm_dir)+ fresh_template_options = _fresh_template_restart(confirm_dir)+ if (+ result_file.exists()+ and fresh_stage1+ and options_file.exists()+ and not fresh_template_options+ ):+ return (+ f'{TEMPLATE_OPTIONS_NAME} belongs to an earlier Confirm UI run — '+ 'run --reset-template-phase before launching fresh Stage 1'+ )+ if (+ result_file.exists()+ and result_stage is None+ and not fresh_stage1+ and not fresh_template_options+ ):+ return (+ f'{RESULT_NAME} is not a current stage1/final receipt — write fresh '+ f'template options or {RECOMMENDATION_STAGE_NAMES[1]} before '+ 'starting a new run'+ )+ if (+ result_stage == 'final'+ and not fresh_stage1+ and not fresh_template_options+ ):+ return (+ 'the previous Confirm UI run is complete — write fresh template '+ f'options or {RECOMMENDATION_STAGE_NAMES[1]} before starting a new one'+ )if options_file.exists():try:_build_template_options(confirm_dir)@@ -991,11 +1038,6 @@ def _wait_for_result(except OSError:pass- skip_error = _stage_skip_error(result_file.parent)- if skip_error:- logger.error('%s', skip_error)- return 2-returncode = proc.poll()if returncode is not None:logger.error('confirm UI exited before a fresh result was written')@@ -1048,43 +1090,43 @@ def _wait_for_template_selection(def _result_stage(result_file: Path) -> Optional[str]:- """Return the canonical ``stage`` field of result.json, or None."""+ """Return the current result stage (Stage 1 or final), or None."""if not result_file.is_file():return Nonetry:data = _read_json_object(result_file)except (OSError, json.JSONDecodeError, ValueError):return None- return _stage_key(data.get('stage'))+ stage = _stage_key(data.get('stage'))+ status = data.get('status')+ if stage == 'stage1' and status == 'stage1-confirmed':+ return stage+ if stage == 'final' and status == 'confirmed':+ return stage+ return Nonedef _stage_key(value: object) -> Optional[str]:- """Normalize current stage names while accepting legacy tier values."""+ """Normalize the two current recommendation/result stage names."""if value is None:return Noneraw = str(value).strip().lower()- if raw in {'1', 'stage1', 'tier1'}:+ if raw == 'stage1':return 'stage1'- if raw in {'2', 'stage2', 'tier2'}:+ if raw == 'stage2':return 'stage2'- if raw in {'3', 'stage3', 'tier3'}:- return 'stage3'if raw == 'final':return 'final'return Nonedef _recommendation_stage(data: dict) -> int:- """Return a recommendation payload's stage, with legacy tier fallback."""+ """Return a recommendation payload's declared stage."""stage = _stage_key(data.get('stage'))- if not stage and 'tier' in data:- stage = _stage_key(data.get('tier'))if stage == 'stage1':return 1if stage == 'stage2':return 2- if stage == 'stage3':- return 3return 0@@ -1094,28 +1136,22 @@ def _stage_name(number: Optional[int]) -> Optional[str]:return 'stage1'if number == 2:return 'stage2'- if number == 3:- return 'stage3'return Nonedef _result_stage_number(stage: Optional[str]) -> int:- """Return result progression: stage1=1, stage2=2, final=4."""+ """Return result progression: stage1=1, final=2."""if stage == 'stage1':return 1- if stage == 'stage2':- return 2if stage == 'final':- return 4+ return 2return 0def _expected_recommendation_stage(result_stage: Optional[str]) -> int:"""Return the recommendation stage that follows the current result."""- if result_stage == 'stage1':+ if result_stage in {'stage1', 'final'}:return 2- if result_stage in {'stage2', 'final'}:- return 3return 1@@ -1124,31 +1160,36 @@ def _stage_recommendations_path(confirm_dir: Path, stage_number: int) -> Path:return confirm_dir / RECOMMENDATION_STAGE_NAMES[stage_number]-def _active_recommendations_path(confirm_dir: Path) -> Path:- """Resolve the recommendation file for the current confirmation stage.+def _is_newer(path: Path, baseline: Path) -> bool:+ """Return whether ``path`` was authored after ``baseline``."""+ try:+ return path.stat().st_mtime_ns > baseline.stat().st_mtime_ns+ except OSError:+ return False- Once any stage-specific file exists, the legacy single-file input is ignored.- If the expected stage is missing but a later stage exists, return that later- file so the existing stage-skip guard can report the ordering error.- """++def _fresh_stage1_restart(confirm_dir: Path) -> bool:+ """Return whether a newly authored Stage 1 starts a fresh UI session."""+ stage1_file = _stage_recommendations_path(confirm_dir, 1)+ result_file = confirm_dir / RESULT_NAME+ return _is_newer(stage1_file, result_file)+++def _fresh_template_restart(confirm_dir: Path) -> bool:+ """Return whether fresh template options start a new UI session."""+ return _is_newer(+ confirm_dir / TEMPLATE_OPTIONS_NAME,+ confirm_dir / RESULT_NAME,+ )+++def _active_recommendations_path(confirm_dir: Path) -> Path:+ """Resolve the recommendation file for the current two-stage session."""+ if _fresh_stage1_restart(confirm_dir) or _fresh_template_restart(confirm_dir):+ return _stage_recommendations_path(confirm_dir, 1)result_stage = _result_stage(confirm_dir / RESULT_NAME)expected_stage = _expected_recommendation_stage(result_stage)- expected_file = _stage_recommendations_path(confirm_dir, expected_stage)- staged_files = {- stage_number: _stage_recommendations_path(confirm_dir, stage_number)- for stage_number in RECOMMENDATION_STAGE_NAMES- }- if any(path.exists() for path in staged_files.values()):- if expected_file.exists():- return expected_file- for stage_number in range(expected_stage + 1, 4):- candidate = staged_files[stage_number]- if candidate.exists():- return candidate- return expected_file-- legacy_file = confirm_dir / LEGACY_RECOMMENDATIONS_NAME- return legacy_file if legacy_file.exists() else expected_file+ return _stage_recommendations_path(confirm_dir, expected_stage)def _read_active_recommendations(@@ -1168,52 +1209,18 @@ def _read_active_recommendations(f'{filename} must declare stage={_stage_name(stage_number)}, 'f'found {_stage_name(actual_stage) or "absent"}')+ if stage_number == 2:+ result_file = confirm_dir / RESULT_NAME+ if _result_stage(result_file) == 'stage1':+ if not _is_newer(rec_file, result_file):+ raise ValueError(+ f'{filename} must be authored after the current '+ 'Stage-1 confirmation'+ )breakreturn rec_file, data-def _stage_skip(rec_stage_number: int, result_stage: Optional[str]) -> bool:- """Detect a staged recommendation running ahead of the confirmed progression.-- Stages confirm strictly in order (stage1 → stage2 → stage3): a- recommendation may only run one stage past the last confirmed result, so- e.g. a ``stage3`` file while only stage 1 is confirmed is a skip — the- page must not render it (an active template never exempts stage 2).- Legacy single-pass recommendations (no ``stage``) are not staged and are- exempt, as is any state after the final confirmation.- """- if rec_stage_number <= 1 or result_stage == 'final':- return False- return rec_stage_number > _result_stage_number(result_stage) + 1---def _stage_skip_error(confirm_dir: Path) -> Optional[str]:- """Return a directive error when the active recommendation skips a stage."""- try:- rec_file, rec_data = _read_active_recommendations(confirm_dir)- except (OSError, json.JSONDecodeError, ValueError):- return None- rec_stage_number = _recommendation_stage(rec_data)- result_stage = _result_stage(confirm_dir / RESULT_NAME)- if not _stage_skip(rec_stage_number, result_stage):- return None- expected = _stage_name(_result_stage_number(result_stage) + 1)- reattach = (- '--wait-only --wait-stage stage1'- if expected == 'stage1'- else '--wait-only --wait-stage stage2'- )- expected_file = RECOMMENDATION_STAGE_NAMES[- _expected_recommendation_stage(result_stage)- ]- return (- f'stage skip detected: {rec_file.name} is {_stage_name(rec_stage_number)} but the last '- f'confirmed result is {result_stage or "absent"} — the page will not render a skipped stage. '- f'Stages confirm in order and an active template does not exempt stage2 (generate-pptx Step 4). '- f'Write the missing {expected_file} recommendations, then re-run with {reattach}.'- )--def _template_confirmation_required(project_path: Path) -> bool:"""Return whether the confirmed project state has an active template."""confirm_dir = project_path / CONFIRM_DIR_NAME@@ -1577,14 +1584,7 @@ def _submission_stage_error(rec_stage_number = _recommendation_stage(recommendations)if rec_stage_number == 0:- if template_required:- return (- 'an installed template requires the Stage 1 → Stage 2 → Stage 3 '- 'flow; legacy single-pass confirmation is not allowed'- )- if submitted_stage not in {None, 'stage3', 'final'}:- return 'legacy single-pass recommendations accept only a final submission'- return None+ return 'recommendations must declare stage1 or stage2'if rec_stage_number == 1:language_error = _primary_language_error(recommendations)@@ -1626,25 +1626,27 @@ def _submission_stage_error(allowed_submissions = {1: {'stage1'},- 2: {'stage2'},- 3: {'stage3', 'final'},+ 2: {'final'},}if submitted_stage not in allowed_submissions[rec_stage_number]:- expected = 'final' if rec_stage_number == 3 else _stage_name(rec_stage_number)+ expected = 'final' if rec_stage_number == 2 else 'stage1'return (f'confirmation stage mismatch: {rec_file.name} is 'f'{_stage_name(rec_stage_number)}, so the submitted stage must be 'f'{expected}')- previous_stage = _result_stage(confirm_dir / RESULT_NAME)+ previous_stage = (+ None+ if _fresh_stage1_restart(confirm_dir)+ else _result_stage(confirm_dir / RESULT_NAME)+ )allowed_predecessors = {- 1: {None, 'stage1', 'stage2', 'final'},- 2: {'stage1', 'stage2'},- 3: {'stage2', 'final'},+ 1: {None},+ 2: {'stage1'},}if previous_stage not in allowed_predecessors[rec_stage_number]:- expected_previous = 'stage1' if rec_stage_number == 2 else 'stage2'+ expected_previous = 'stage1' if rec_stage_number == 2 else 'no prior result'return (f'confirmation predecessor mismatch: {_stage_name(rec_stage_number)} 'f'requires a confirmed {expected_previous} result, found '@@ -1735,8 +1737,7 @@ def _expected_result_stage(confirm_dir: Path) -> str:return 'final'return {1: 'stage1',- 2: 'stage2',- 3: 'final',+ 2: 'final',}.get(_recommendation_stage(recommendations), 'final')@@ -1781,37 +1782,32 @@ def _build_session_state(except (OSError, json.JSONDecodeError, ValueError) as exc:rec_error = str(exc)- result_stage = _result_stage(result_file)+ fresh_restart = (+ _fresh_stage1_restart(confirm_dir)+ or _fresh_template_restart(confirm_dir)+ )+ result_stage = None if fresh_restart else _result_stage(result_file)result_stage_number = _result_stage_number(result_stage)- stage_skip = _stage_skip(rec_stage_number, result_stage)- # A skipped stage is never presented: the page keeps its "deriving…" state- # (waiting_agent) until the AI creates the missing stage file.if result_stage == 'final':expected_stage_number = Nonestatus = 'done'current_stage = 'final'- elif result_stage == 'stage2':- expected_stage_number = 3- status = 'ready_user' if rec_stage_number >= 3 else 'waiting_agent'- current_stage = _stage_name(rec_stage_number) if rec_stage_number >= 3 else 'stage2'elif result_stage == 'stage1':expected_stage_number = 2- ready = rec_stage_number >= 2 and not stage_skip+ ready = rec_stage_number == 2status = 'ready_user' if ready else 'waiting_agent'- current_stage = _stage_name(rec_stage_number) if ready else 'stage1'+ current_stage = 'stage2' if ready else 'stage1'else:- expected_stage_number = 1 if stage_skip else (rec_stage_number or 1)- ready = bool(rec_stage_number) and not stage_skip+ expected_stage_number = 1+ ready = rec_stage_number == 1status = 'ready_user' if ready else 'waiting_agent'- current_stage = rec_stage if ready else 'stage1'+ current_stage = 'stage1'session = {- 'session_id': previous.get('session_id') or uuid.uuid4().hex,'phase': 'strategist','status': status,'current_stage': current_stage,- 'stage_skip': stage_skip,'expected_stage': _stage_name(expected_stage_number),'expected_stage_number': expected_stage_number,'recommendation_stage': rec_stage,@@ -1929,9 +1925,9 @@ def _sync_session_state(# Earlier-stage choices are not rendered on later pages, so their values live-# only in browser STATE and would be lost on refresh. Fold them from result.json-# into the served recommendations so a refresh / reopen resumes from the user's-# actual communication contract and complete deck-solution choices.+# only in browser STATE and would be lost on an in-run refresh. Fold them from+# result.json into Stage-2 recommendations so the same live run resumes from the+# user's actual communication contract and complete deck-solution choices._CONTRACT_RECOMMEND_KEYS = ('canvas',)@@ -1944,18 +1940,6 @@ _CONTRACT_VALUE_KEYS = ('artifact_afterlife','content_divergence',)-_DECK_DIRECTION_RECOMMEND_KEYS = (- 'delivery_purpose',- 'mode',- 'visual_style',- 'icons',- 'image_usage',-)-_PRODUCTION_RECOMMEND_KEYS = (- 'formula_policy',- 'image_ai_path',- 'generation_mode',-)_PROACTIVE_EXECUTION_DEFAULTS = {'proactive_speaker_notes': True,'proactive_custom_animations': False,@@ -2005,6 +1989,8 @@ def _merge_confirmed_choices(data: dict, result_file: Path) -> None:res = _read_json_object(result_file)except (OSError, json.JSONDecodeError, ValueError):return+ if _result_stage(result_file) != 'stage1':+ returnrecommend = data.setdefault('recommend', {})if not isinstance(recommend, dict):recommend = data['recommend'] = {}@@ -2022,78 +2008,25 @@ def _merge_confirmed_choices(data: dict, result_file: Path) -> None:for key in _CONTRACT_VALUE_KEYS:if key in res:data[key] = {'value': res.get(key) or ''}- if _recommendation_stage(data) < 3:- return- for key in _DECK_DIRECTION_RECOMMEND_KEYS:- if res.get(key) not in (None, ''):- recommend[key] = res[key]- if 'page_count' in res:- data['page_count'] = {'value': res.get('page_count') or ''}- if 'image_notes' in res:- data['image_notes'] = {'value': res.get('image_notes') or ''}- if 'template_application' in res:- data['template_application'] = {- 'value': res.get('template_application') or '',- }- if isinstance(res.get('color'), dict):- data['color'] = {'selected': 0, 'candidates': [res['color']]}- if isinstance(res.get('typography'), dict):- data['typography'] = {'selected': 0, 'candidates': [res['typography']]}- if isinstance(res.get('image_strategy'), dict):- data['image_strategy'] = {- 'selected': 0,- 'candidates': [res['image_strategy']],- }- custom_candidates = data.get('custom_candidates')- if not isinstance(custom_candidates, dict):- custom_candidates = {}- data['custom_candidates'] = custom_candidates- for field, behavior_field in (- ('mode', 'mode_behavior'),- ('visual_style', 'visual_style_behavior'),- ):- behavior = res.get(behavior_field)- if res.get(field) != 'custom' or not str(behavior or '').strip():- continue- candidate = custom_candidates.get(field)- if not isinstance(candidate, dict):- candidate = {}- candidate['behavior'] = behavior- custom_candidates[field] = candidate- image_strategy = res.get('image_strategy')- if isinstance(image_strategy, dict) and image_strategy.get('rendering') == 'custom':- custom_candidates['image_strategy'] = image_strategy- # Stage 3 must retain its own production recommendations until final- # confirmation. A final-result reopen reflects those confirmed mechanics.- # Legacy single-pass results have no stage but do carry status=confirmed.- result_stage = _stage_key(res.get('stage'))- is_final = result_stage == 'final' or (- result_stage is None and res.get('status') == 'confirmed'- )- if not is_final:- return- for key in _PRODUCTION_RECOMMEND_KEYS:- if res.get(key) not in (None, ''):- recommend[key] = res[key]- if 'refine_spec' in res:- data['refine_spec'] = {'value': bool(res.get('refine_spec'))}- for key, default in _PROACTIVE_EXECUTION_DEFAULTS.items():- data[key] = {'value': res.get(key, default)}def _apply_locked_recommendations(result: dict,recommendations_file: Path,previous_result_file: Path,+ *,+ carry_previous: bool,) -> dict:"""Restore profile-locked fields and return locks for staged carry-over."""# This marker is server-owned; never accept a client-supplied carry-over map.result.pop(_LOCKED_RECOMMENDATIONS_KEY, None)locked_values = {}- try:- previous = _read_json_object(previous_result_file)- except (OSError, json.JSONDecodeError, ValueError):- previous = {}+ previous = {}+ if carry_previous:+ try:+ previous = _read_json_object(previous_result_file)+ except (OSError, json.JSONDecodeError, ValueError):+ previous = {}previous_locks = previous.get(_LOCKED_RECOMMENDATIONS_KEY)if isinstance(previous_locks, dict):locked_values.update(previous_locks)@@ -2109,10 +2042,7 @@ def _apply_locked_recommendations(# Stage 1 starts a new contract and therefore replaces any stale locks left# by an earlier run. Later stages inherit those locks across server restarts.- if (- recommendations_loaded- and _recommendation_stage(recommendations) in {0, 1}- ):+ if recommendations_loaded and _recommendation_stage(recommendations) == 1:locked_values = {}for key, field in recommendations.items():if key in _PROACTIVE_EXECUTION_DEFAULTS:@@ -2146,11 +2076,6 @@ def _wait_only_for_result(if result_status is not None:return result_status- skip_error = _stage_skip_error(result_file.parent)- if skip_error:- logger.error('%s', skip_error)- return 2-lock = _read_lock(lock_file)pid = _lock_pid(lock)if not pid or not _process_alive(pid):@@ -2172,6 +2097,11 @@ def _wait_result_status(target_stage: str,) -> Optional[int]:"""Return a terminal wait status when the persisted result resolves the target."""+ if (+ _fresh_stage1_restart(result_file.parent)+ or _fresh_template_restart(result_file.parent)+ ):+ return Nonecurrent_stage = _result_stage(result_file)if current_stage == target_stage:logger.info('confirmation stage=%s received: %s', target_stage, result_file)@@ -2569,6 +2499,18 @@ def create_app(@app.route('/api/recommendations')def get_recommendations():"""Serve the Strategist-authored recommendations for this project."""+ result_file = confirm_dir / RESULT_NAME+ if (+ _result_stage(result_file) == 'final'+ and not _fresh_stage1_restart(confirm_dir)+ and not _fresh_template_restart(confirm_dir)+ ):+ return jsonify({+ 'error': (+ 'the current Confirm UI run is complete; write a fresh '+ f'{RECOMMENDATION_STAGE_NAMES[1]} before starting another'+ ),+ }), 409rec_file = _active_recommendations_path(confirm_dir)if not rec_file.exists():return jsonify({'error': f'{rec_file.name} not found'}), 404@@ -2591,33 +2533,17 @@ def create_app(f'{template_error}'),}), 409- # Report whether a result already exists (re-open after confirm).- result_file = confirm_dir / RESULT_NAME- if _stage_skip(- _recommendation_stage(data),- _result_stage(result_file),- ):- return jsonify({- 'error': _stage_skip_error(confirm_dir),- }), 409- data['_already_confirmed'] = result_file.exists()# Later stages render only downstream sections, so fold earlier confirmed- # choices from result.json back in. A refresh / reopen then re-inits from+ # choices from result.json back in. An in-run refresh then re-inits from# the user's choices instead of catalog defaults.if rec_stage_number >= 2 and result_file.exists():_merge_confirmed_choices(data, result_file)- if rec_stage_number > 0:- language_error = _canonicalize_primary_language(- data,- required=True,- )- if language_error:- return jsonify({'error': language_error}), 409- else:- # Legacy single-pass files remain permissive. Canonicalize only- # aliases/tags the shared helper already understands; an old prose- # value must never prevent the compatibility UI from opening.- _canonicalize_primary_language(data, required=False)+ language_error = _canonicalize_primary_language(+ data,+ required=True,+ )+ if language_error:+ return jsonify({'error': language_error}), 409try:template_required = _template_confirmation_required(project_path)except (OSError, json.JSONDecodeError, ValueError) as exc:@@ -2639,7 +2565,7 @@ def create_app(recommendation_error = _stage2_design_directions_error(data)if recommendation_error:return jsonify({'error': recommendation_error}), 409- if rec_stage_number in {0, 3}:+ if rec_stage_number == 2:proactive_values, proactive_error = (_resolve_proactive_execution_values(data))@@ -2734,14 +2660,14 @@ def create_app(result['primary_language'] = main_languageelse:result.pop('primary_language', None)- if stage == 'stage2' or rec_stage_number == 3:+ if rec_stage_number == 2:solution_error = _stage2_solution_error(result,main_language=main_language,)if solution_error:return jsonify({'error': solution_error}), 400- if stage not in {'stage1', 'stage2'}:+ if rec_stage_number == 2:proactive_defaults, proactive_recommendation_error = (_resolve_proactive_execution_values(current_recommendations))@@ -2760,25 +2686,16 @@ def create_app(result,rec_file,result_file,+ carry_previous=rec_stage_number > 1,)if not template_required:result.pop('template_application', None)locked_values.pop('template_application', None)- if stage not in {'stage1', 'stage2'}:- proactive_result_error = _normalize_proactive_execution_result(- result,- proactive_defaults,- )- if proactive_result_error:- return jsonify({'error': proactive_result_error}), 400result.pop('template_reuse_scope', None)result.pop('template_adherence', None)- # Staged flow: Stage 1 / Stage 2 submits record intermediate choices but do- # NOT close the page. Only a final submit is a full confirmation. A- # payload with no stage is a legacy free-design single-pass confirmation.- if stage in {'stage1', 'stage2'}:- result['stage'] = stage- result['status'] = f'{stage}-confirmed'+ if stage == 'stage1':+ result['stage'] = 'stage1'+ result['status'] = 'stage1-confirmed'if locked_values:result[_LOCKED_RECOMMENDATIONS_KEY] = locked_valueselse:@@ -2825,11 +2742,10 @@ def build_parser() -> argparse.ArgumentParser:'dead server on the recorded/default port so browser polling can resume.',)parser.add_argument(- '--wait-stage', default='final', metavar='{template,stage1,stage2,final}',+ '--wait-stage', default='final', metavar='{template,stage1,final}',help='Wait for the Step-3 template selection or this result.json stage ''(default: final). Use template after the initial template-phase '- 'launch, stage1 after its Strategist handoff, and stage2 for the '- 'direction handoff.',+ 'launch and stage1 after the communication-contract handoff.',)parser.add_argument('--wait-timeout', type=int, default=WAIT_TIMEOUT_DEFAULT,@@ -2853,8 +2769,9 @@ def build_parser() -> argparse.ArgumentParser:)parser.add_argument('--reset-template-phase', action='store_true',- help='Agent-only: remove exactly template_selection.json and '- 'template_handoff.json so the user may select again.',+ help='Agent-only: remove exactly template_options.json, '+ 'template_selection.json, and template_handoff.json before a '+ 'fresh one-run UI lifecycle.',)return parser@@ -2882,8 +2799,8 @@ def main(argv: Optional[list[str]] = None) -> int:return 1raw_wait_stage = str(args.wait_stage).strip().lower()wait_stage = 'template' if raw_wait_stage == 'template' else _stage_key(raw_wait_stage)- if wait_stage not in {'template', 'stage1', 'stage2', 'final'}:- logger.error('--wait-stage must be template, stage1, stage2, or final')+ if wait_stage not in {'template', 'stage1', 'final'}:+ logger.error('--wait-stage must be template, stage1, or final')return 2template_control = args.complete_template_phase or args.reset_template_phasediff --git a/skills/ppt-master/scripts/confirm_ui/static/app.js b/skills/ppt-master/scripts/confirm_ui/static/app.jsindex 004b21de..35aec5e2 100644--- a/skills/ppt-master/scripts/confirm_ui/static/app.js+++ b/skills/ppt-master/scripts/confirm_ui/static/app.js@@ -1,7 +1,7 @@-/* PPT Master - template selection and Strategist confirmation UI- * Generate Step 3 independently selects free design or exact template- * candidates before Stage 1 captures the communication contract. Stage 2 then- * confirms a coherent deck solution, and Stage 3 resolves production mechanics.+/* PPT Master - conditional template selection and Strategist confirmation UI+ * Explicit template requests use Generate Step 3 before Stage 1; ordinary free+ * design starts at Stage 1. Stage 2 combines the coherent deck solution and+ * production mechanics, then writes the final result.* Finite fields use /static/catalogs.json; coordinated design directions seed* color, typography, icons, generated-image rendering, and conditional* template-application prose. Final confirm saves result.json.@@ -17,14 +17,13 @@template_stage_title: "Choose a template",template_stage_hint: "Choose free design, or combine one registered template per type plus one specified path. This selection is separate from Stage 1.",stage_anchors: "Stage 1 · Communication contract",- stage_design: "Stage 2 · Deck direction & visual system",- stage_images: "Stage 3 · Resources & production",+ stage_final_plan: "Stage 2 · Final plan & production",loading: "Loading…",load_error: "Could not load the current recommendation stage. The AI must write it before launch.",btn_confirm: "Confirm",btn_confirm_template: "Confirm template & continue →",btn_confirm_contract: "Confirm contract & continue →",- btn_confirm_solution: "Confirm solution & continue →",+ btn_confirm_final_plan: "Confirm final plan →",deriving: "Generating the downstream options from your choices…",template_applying: "Applying the template selection and preparing Stage 1…",template_load_error: "Could not load the available templates.",@@ -33,7 +32,6 @@template_free_selected: "Free design selected",template_count_selected: "{count} template(s) selected",connection_lost: "Connection to the confirm server was interrupted; retrying. If this keeps failing, return to the chat for confirmation.",- already_confirmed: "Already confirmed once. Re-submitting overwrites the previous choices.",confirmed_title: "✓ Confirmed",confirmed_hint: "Your choices are saved. You can close this page and return to the chat.",lang_toggle_title: "Switch language",@@ -122,8 +120,8 @@image_strategy_custom_placeholder: "Describe the exact generated-image direction, subjects, composition, style cues, or things to avoid.",image_strategy_reference_hint: "Reference images show rendering only. Final AI images inherit the deck color scheme selected above.",image_strategy_no_reference: "No reference image for this custom choice.",- image_source_summary: "Confirmed image sources",- image_production_hint: "Image sources and rendering were confirmed in Stage 2. This stage only resolves the production path.",+ image_source_summary: "Selected image sources",+ image_production_hint: "Image sources and rendering are selected above. Resolve only the production path here.",image_usage_notes: "Additional image requirements",image_usage_notes_placeholder: "e.g. realistic handwashing scenes; avoid cartoon germs; keep product photos untouched.",image_usage_required: "Select at least one image usage option.",@@ -159,7 +157,6 @@size_role_subtitle: "subtitle",size_role_annotation: "annotation",custom_typography: "Custom typography",- custom_typography_repair: "Complete the concrete font names required for this project to continue.",custom_color: "Custom color",custom_color_placeholder: "Describe your colors in words, e.g. deep navy primary, warm orange accent, white background — or paste HEX values…",role_background: "bg",@@ -203,14 +200,13 @@template_stage_title: "テンプレートを選択",template_stage_hint: "自由デザイン、または種類ごとに登録済み1件と指定パス1件までを組み合わせます。この選択はステージ1とは独立しています。",stage_anchors: "ステージ 1 · コミュニケーション契約",- stage_design: "ステージ 2 · 全体方針とビジュアルシステム",- stage_images: "ステージ 3 · リソースと制作",+ stage_final_plan: "ステージ 2 · 最終プランと制作",loading: "読み込み中…",load_error: "現在の推奨ステージを読み込めませんでした。起動前にAIが書き込む必要があります。",btn_confirm: "確定",btn_confirm_template: "テンプレートを確定して次へ →",btn_confirm_contract: "契約内容を確定して次へ →",- btn_confirm_solution: "全体方針を確定して次へ →",+ btn_confirm_final_plan: "最終プランを確定 →",deriving: "選択内容をもとに後続の選択肢を生成しています…",template_applying: "テンプレートの選択を適用し、ステージ1を準備しています…",template_load_error: "利用可能なテンプレートを読み込めませんでした。",@@ -219,7 +215,6 @@template_free_selected: "自由デザインを選択中",template_count_selected: "{count} 件のテンプレートを選択中",connection_lost: "確認ページのサーバー接続が中断されました。再試行しています。失敗が続く場合はチャットで確認してください。",- already_confirmed: "すでに一度確定済みです。再送信すると前回の選択を上書きします。",confirmed_title: "✓ 確定しました",confirmed_hint: "選択内容を保存しました。このページを閉じてチャットに戻ってください。",lang_toggle_title: "言語を切り替え",@@ -308,8 +303,8 @@image_strategy_custom_placeholder: "生成画像の方向性、被写体、構図、スタイル要素、避けたい要素を具体的に入力してください。",image_strategy_reference_hint: "参照画像はレンダリングのみを示します。最終AI画像の色は上で選んだデッキ配色を継承します。",image_strategy_no_reference: "このカスタム選択には参照画像がありません。",- image_source_summary: "確定済みの画像ソース",- image_production_hint: "画像ソースとレンダリングはステージ2で確定済みです。ここでは制作経路だけを決めます。",+ image_source_summary: "選択中の画像ソース",+ image_production_hint: "画像ソースとレンダリングは上で選択済みです。ここでは制作経路だけを決めます。",image_usage_notes: "画像に関する補足要件",image_usage_notes_placeholder: "例:リアルな手洗いシーンを優先、漫画調の菌のイラストは避ける、製品写真はそのまま使う。",image_usage_required: "画像の使用方法を少なくとも1つ選択してください。",@@ -345,7 +340,6 @@size_role_subtitle: "サブタイトル",size_role_annotation: "注釈",custom_typography: "カスタムタイポグラフィ",- custom_typography_repair: "続行するには、このプロジェクトに必要な具体的な書体名を入力してください。",custom_color: "カスタム配色",custom_color_placeholder: "配色を言葉で説明 — 例:濃紺をメインに暖色オレンジのアクセント、背景は白 — またはHEX値を貼り付け…",role_background: "背景",@@ -389,14 +383,13 @@template_stage_title: "选择模板",template_stage_hint: "选择自由设计,或组合每类一个已注册模板与一个指定地址。这个选择独立于 Stage 1。",stage_anchors: "第一阶段 · 沟通契约",- stage_design: "第二阶段 · 完整方案与视觉系统",- stage_images: "第三阶段 · 资源与生产执行",+ stage_final_plan: "第二阶段 · 最终方案与制作",loading: "加载中…",load_error: "无法加载推荐文件,需在启动前写入。",btn_confirm: "确认",btn_confirm_template: "确认模板并继续 →",btn_confirm_contract: "确认沟通契约并继续 →",- btn_confirm_solution: "确认完整方案并继续 →",+ btn_confirm_final_plan: "确认最终方案 →",deriving: "正在根据你的选择生成下游选项…",template_applying: "正在应用模板选择并准备 Stage 1…",template_load_error: "无法加载可用模板。",@@ -405,7 +398,6 @@template_free_selected: "已选择自由设计",template_count_selected: "已选择 {count} 个模板",connection_lost: "确认页服务连接中断,正在重试;如果持续失败,请回到聊天窗口走聊天确认。",- already_confirmed: "已确认过一次,重新提交会覆盖之前的选择。",confirmed_title: "✓ 已确认",confirmed_hint: "选择已保存,可关闭此页并回到聊天窗口。",lang_toggle_title: "切换语言",@@ -494,8 +486,8 @@image_strategy_custom_placeholder: "描述生成图的具体方向、主体、构图、风格关键词或需要避免的内容。",image_strategy_reference_hint: "参考图只展示渲染风格;最终 AI 图片直接继承上方已选的整套 PPT 配色。",image_strategy_no_reference: "自定义选择没有参考图。",- image_source_summary: "已确认的图片来源",- image_production_hint: "图片来源和渲染方向已在第二阶段确认;这里仅决定实际生产路径。",+ image_source_summary: "已选图片来源",+ image_production_hint: "图片来源和渲染方向已在上方选择;这里仅决定实际生产路径。",image_usage_notes: "图片补充要求",image_usage_notes_placeholder: "例如:优先真实洗手场景;不要卡通病菌;产品照片保持原样。",image_usage_required: "请至少选择一种图片使用方式。",@@ -531,7 +523,6 @@size_role_subtitle: "副标题",size_role_annotation: "注释",custom_typography: "自定义字体方案",- custom_typography_repair: "请补全当前项目所需的具体字体名称后继续。",custom_color: "自定义配色",custom_color_placeholder: "用文字描述配色,如:深蓝主色、暖橙强调、白色背景——或直接粘贴 HEX 值…",role_background: "背景",@@ -2075,6 +2066,9 @@var refreshStylePreview = function () {};// Replaced when the selected generated-image preview mounts.var refreshImageStrategyPreview = function () {};+ // Replaced when the final plan's image-production section mounts; image-use+ // edits call it so the conditional AI path stays synchronized on the page.+ var refreshImageProduction = function () {};// Replaced when the typography section mounts; the canvas section calls it so// the body-size hint tracks the chosen canvas height.var refreshBodySizeHint = function () {};@@ -2384,11 +2378,6 @@});}- function typographyNeedsConcreteRepair() {- return !!(STATE.typography && STATE.typography.name === "custom" &&- !typographyFamiliesComplete(STATE.typography));- }-function sampleText(role, field) {// Keep comparison copy stable: choices change visual treatment, not content.var useEnglish = field === "english" || isEnglishProject();@@ -2455,9 +2444,6 @@function renderTypography(host) {var cands = typographyRecommendationCandidates().filter(typographyFamiliesComplete);var sec = section(7, "sec_type");- if (stageNumber(REC) >= 3 && typographyNeedsConcreteRepair()) {- setSectionNote(sec, t("custom_typography_repair"));- }var grid = el("div", "font-grid");var customFields = el("div", "custom-typography-fields font-picker-fields");var customInputs = {};@@ -3133,6 +3119,7 @@}STATE.image_usage = current;refreshUsageChips();+ refreshImageProduction();}(CAT.image_usage || []).forEach(function (option) {var label = optionLabel(option);@@ -3166,23 +3153,29 @@function renderImageProduction(host) {var sec = section("P", "sec_image_production", t("image_production_hint"));- var summary = el("div", "subfield");- summary.appendChild(el("div", "subfield-label", t("image_source_summary")));- var chips = el("div", "chips locked-summary-chips");- (STATE.image_usage || []).forEach(function (id) {- var option = findCatalogOption(CAT.image_usage, id);- chips.appendChild(el("div", "chip selected locked-summary-chip",- option ? optionLabel(option) : humanizeId(id)));- });- summary.appendChild(chips);- sec.appendChild(summary);- if (needsGeneratedImagesForUsage(STATE.image_usage)) {- var pathField = el("div", "subfield");- pathField.appendChild(el("div", "subfield-label", t("image_ai_path")));- enumField(pathField, CAT.image_ai_path, recOrFirst("image_ai_path", CAT.image_ai_path),- function () { return STATE.image_ai_path; }, function (value) { STATE.image_ai_path = value; });- sec.appendChild(pathField);- }+ var body = el("div", "image-production-body");+ sec.appendChild(body);+ refreshImageProduction = function () {+ body.innerHTML = "";+ var summary = el("div", "subfield");+ summary.appendChild(el("div", "subfield-label", t("image_source_summary")));+ var chips = el("div", "chips locked-summary-chips");+ (STATE.image_usage || []).forEach(function (id) {+ var option = findCatalogOption(CAT.image_usage, id);+ chips.appendChild(el("div", "chip selected locked-summary-chip",+ option ? optionLabel(option) : humanizeId(id)));+ });+ summary.appendChild(chips);+ body.appendChild(summary);+ if (needsGeneratedImagesForUsage(STATE.image_usage)) {+ var pathField = el("div", "subfield");+ pathField.appendChild(el("div", "subfield-label", t("image_ai_path")));+ enumField(pathField, CAT.image_ai_path, recOrFirst("image_ai_path", CAT.image_ai_path),+ function () { return STATE.image_ai_path; }, function (value) { STATE.image_ai_path = value; });+ body.appendChild(pathField);+ }+ };+ refreshImageProduction();host.appendChild(sec);}@@ -3248,25 +3241,20 @@host.appendChild(sec);}- // Stage of the staged confirm flow:- // 1 = communication contract, 2 = complete deck direction,- // 3 = resources + production execution,- // "all" = legacy single-pass (the recommendation payload carried no stage).+ // Two-stage confirmation: communication contract, then complete final plan.var STAGE = 1;function stageNumber(data) {- var raw = data && data.stage != null ? data.stage : (data && data.tier);+ var raw = data && data.stage;raw = String(raw == null ? "" : raw).toLowerCase();- if (raw === "1" || raw === "stage1" || raw === "tier1") return 1;- if (raw === "2" || raw === "stage2" || raw === "tier2") return 2;- if (raw === "3" || raw === "stage3" || raw === "tier3") return 3;- return "all";+ if (raw === "stage1") return 1;+ if (raw === "stage2") return 2;+ return 0;}function stageTitle(stage) {if (stage === 1) return t("stage_anchors");- if (stage === 2) return t("stage_design");- if (stage === 3) return t("stage_images");+ if (stage === 2) return t("stage_final_plan");return t("page_title");}@@ -3283,6 +3271,7 @@// write to now-detached nodes until renderStylePreview remounts them.refreshStylePreview = function () {};refreshImageStrategyPreview = function () {};+ refreshImageProduction = function () {};refreshBodySizeHint = function () {};refreshSizeInputs = function () {};var previewHost = document.getElementById("topbar-preview");@@ -3310,40 +3299,6 @@renderTypography(styleGroup);host.appendChild(styleGroup);renderImageDirection(host);- } else if (stage === 3) {- if (previewHost) renderStylePreview(previewHost);- if (previewHost) renderImageStrategyPreview(previewHost);- // Stage 3 contains production mechanics only. It summarizes the- // confirmed image source and reopens typography only to repair a- // legacy prose-only Custom result that cannot be final as-is.- if (typographyNeedsConcreteRepair()) {- var repairStyleGroup = el("div", "style-group");- renderTypography(repairStyleGroup);- host.appendChild(repairStyleGroup);- }- renderImageProduction(host);- renderFormulaPolicy(host);- renderProactiveExecution(host);- renderMode(host);- renderRefine(host);- } else {- // Legacy single-pass: show every section on one page.- if (previewHost) renderStylePreview(previewHost);- renderCommunication(host);- renderDelivery(host);- renderCanvas(host);- renderTemplateApplication(host);- renderDesignDirections(host);- renderNarrativeDirection(host);- renderVisualDirection(host);- renderReadingMode(host);- renderPages(host);- var legacyStyleGroup = el("div", "style-group");- renderColor(legacyStyleGroup);- renderIcons(legacyStyleGroup);- renderTypography(legacyStyleGroup);- host.appendChild(legacyStyleGroup);- renderImageDirection(host);renderImageProduction(host);renderFormulaPolicy(host);renderProactiveExecution(host);@@ -3358,10 +3313,8 @@function updateActionBar(stage) {var btn = document.getElementById("btn-confirm");btn.disabled = false;- // Stage 1/2 both confirm the current values before advancing. Stage 3- // and legacy single-pass submit the final result.if (stage === 1) btn.textContent = t("btn_confirm_contract");- else if (stage === 2) btn.textContent = t("btn_confirm_solution");+ else if (stage === 2) btn.textContent = t("btn_confirm_final_plan");else btn.textContent = t("btn_confirm");}@@ -3466,8 +3419,7 @@STATE.typography.body_size = defaultBodySizeForCanvas(STATE.canvas, STATE.delivery_purpose);}// A freshly authored Stage 2 starts from one deterministic reading-mode- // baseline. Stage 3 carries the confirmed Stage-2 values and must not- // normalize them again.+ // baseline.if (stageNumber(REC) === 2) syncUnpinnedTypographySizes(true);var rawImageUsage = recValue("image_usage") || directionField("image_usage");STATE.image_usage = selectedImageUsageIds(rawImageUsage);@@ -3492,7 +3444,7 @@}}- function initStage3State() {+ function initProductionState() {STATE.formula_policy = pick("formula_policy", CAT.formula_policy);STATE.image_ai_path = pick("image_ai_path", CAT.image_ai_path);STATE.proactive_speaker_notes = booleanRecommendation("proactive_speaker_notes", true);@@ -3505,8 +3457,6 @@function initState() {initStage1State();- initStage2State();- initStage3State();}// ---- confirm + close -------------------------------------------------@@ -3688,31 +3638,6 @@return !!valid;}- function stage2Payload() {- var payload = communicationPayload();- payload.stage = "stage2";- if (Object.prototype.hasOwnProperty.call(STATE, "template_application")) {- payload.template_application = STATE.template_application;- }- payload.mode = STATE.mode;- payload.mode_behavior = STATE.mode_behavior;- payload.visual_style = STATE.visual_style;- payload.visual_style_behavior = STATE.visual_style_behavior;- payload.page_count = STATE.page_count;- // Reading mode keeps the legacy delivery_purpose key for compatibility.- if (isPptCanvas(STATE.canvas)) payload.delivery_purpose = STATE.delivery_purpose;- payload.color = JSON.parse(JSON.stringify(STATE.color || {}));- payload.icons = STATE.icons;- payload.typography = JSON.parse(JSON.stringify(STATE.typography || {}));- normalizeTypographyForSubmit(payload);- payload.image_usage = selectedImageUsageIds(STATE.image_usage);- if (String(STATE.image_notes || "").trim()) payload.image_notes = STATE.image_notes;- if (needsGeneratedImagesForUsage(payload.image_usage)) {- payload.image_strategy = normalizedImageStrategy(STATE.image_strategy);- }- return normalizeCreativePayload(payload);- }-function submitStage(payload, nextStage) {var btn = document.getElementById("btn-confirm");btn.disabled = true;@@ -3748,12 +3673,7 @@}function submitStage2() {- var payload = stage2Payload();- if (!imageUsageValid(payload.image_usage)) return;- if (!imageStrategyValid(payload)) return;- if (!designSystemValid(payload)) return;- if (!customSelectionsValid(payload)) return;- submitStage(payload, 3);+ confirm();}function showDeriving() {@@ -3792,8 +3712,10 @@function enterStage(data, stage) {REC = data;- if (stage >= 2) initStage2State();- if (stage >= 3) initStage3State();+ if (stage === 2) {+ initStage2State();+ initProductionState();+ }STAGE = stage;document.getElementById("loading").style.display = "none";document.getElementById("sections").style.display = "block";@@ -3933,9 +3855,6 @@TEMPLATE_PHASE = false;TEMPLATE_SUBMITTING = false;enterStage(REC, forceStage || stageNumber(REC));- if (REC._already_confirmed) {- document.getElementById("confirm-status").textContent = t("already_confirmed");- }return REC;});}@@ -4045,12 +3964,17 @@else confirm();});- // Session is the first network read. New Generate runs expose an- // independent Step-3 template phase; old sessions omit it (or report- // `strategist`) and retain the existing Stage-1-first behavior.+ // Session is the first network read. Explicit template requests expose+ // Step 3; ordinary free design reports `strategist` and starts with+ // Stage 1.fetchJson("/api/session", "session").catch(function () {return { phase: "strategist" };}).then(function (session) {+ if (session && session.status === "done") {+ document.getElementById("loading").style.display = "none";+ showConfirmedOverlay();+ return null;+ }if (session && session.phase === "template") {return loadTemplateUi(session).catch(function (err) {showError(t("template_load_error") + " " +