Ghostlight/tools/run_qwen_ink_sequential_generation.py
2026-05-02 03:05:47 +01:00

538 lines
24 KiB
Python

"""Run a sequential Qwen Ink generation prototype.
Pass 1: generate Maer choices from Maer-local awareness only.
Pass 2: generate participant appraisal/consolidation for the selected
observable event.
Pass 3: generate the next actor's action from updated local state.
"""
from __future__ import annotations
import argparse
import json
import re
import urllib.request
from pathlib import Path
from typing import Any
ROOT = Path(__file__).resolve().parents[1]
DEFAULT_ENDPOINT = "http://192.168.1.84:11434/api/generate"
DEFAULT_FIXTURE = ROOT / "examples" / "agent-state.cold-wake-story-lab.json"
DEFAULT_ANNOTATION = ROOT / "examples" / "ink" / "cold-wake-sanctuary-intake.training.json"
DEFAULT_OUT_DIR = ROOT / "experiments" / "ink"
VALID_ACTION_TYPES = {
"speak",
"silence",
"move",
"gesture",
"touch_object",
"block_object",
"use_object",
"show_object",
"withhold_object",
"transfer_object",
"spend_resource",
"attack",
"wait",
"mixed",
}
def load_json(path: Path) -> Any:
return json.loads(path.read_text(encoding="utf-8"))
def write_text(path: Path, text: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, encoding="utf-8")
def write_json(path: Path, payload: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(payload, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
def find_agent(fixture: dict[str, Any], agent_id: str) -> dict[str, Any]:
return next(agent for agent in fixture["agents"] if agent["agent_id"] == agent_id)
def find_scene(fixture: dict[str, Any], scene_id: str) -> dict[str, Any]:
return next(scene for scene in fixture["scenes"] if scene["scene_id"] == scene_id)
def find_pack(scene: dict[str, Any], speaker_id: str) -> dict[str, Any]:
return next(pack for pack in scene["dialogue_context_packs"] if pack["speaker_agent_id"] == speaker_id)
def find_relationship(fixture: dict[str, Any], source_id: str, target_id: str) -> dict[str, Any]:
return next(rel for rel in fixture["relationships"] if rel["source_id"] == source_id and rel["target_id"] == target_id)
def memory_summaries(agent: dict[str, Any], memory_ids: list[str]) -> list[str]:
summaries: list[str] = []
for group in agent["memories"].values():
for memory in group:
if memory["memory_id"] in memory_ids:
summaries.append(f"{memory['memory_id']}: {memory['summary']}")
return summaries
def goal_summaries(agent: dict[str, Any], goal_ids: list[str]) -> list[str]:
return [
f"{goal['goal_id']}: {goal['description']} Priority {goal['priority']}. Stake: {goal['emotional_stake']}"
for goal in agent["goals"]
if goal["goal_id"] in goal_ids
]
def selected_variables(agent: dict[str, Any]) -> dict[str, dict[str, float]]:
groups = agent["canonical_state"]
names_by_group = {
"behavioral_dimensions": ["interpersonal_warmth", "control_pressure", "suspicion", "rigidity", "volatility"],
"presentation_strategy": ["detachment", "competence_theater", "strategic_opacity", "ironic_distance"],
"voice_style": ["formality", "lexical_precision", "ritualized_address", "verbal_warmth", "pointedness"],
"situational_state": ["exhaustion", "panic", "grievance_activation", "overstimulation"],
}
selected: dict[str, dict[str, float]] = {}
for group_name, variable_names in names_by_group.items():
selected[group_name] = {
name: groups[group_name][name]["current_activation"]
for name in variable_names
if name in groups[group_name]
}
return selected
def post_json(endpoint: str, payload: dict[str, Any]) -> dict[str, Any]:
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
request = urllib.request.Request(
endpoint,
data=data,
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(request, timeout=180) as response:
return json.loads(response.read().decode("utf-8"))
def extract_json_object(text: str) -> Any | None:
stripped = text.strip()
if stripped.startswith("{"):
return json.loads(stripped)
fenced = re.search(r"```(?:json)?\s*(\{.*?\})\s*```", stripped, re.DOTALL)
if fenced:
return json.loads(fenced.group(1))
start = stripped.find("{")
end = stripped.rfind("}")
if start != -1 and end != -1 and end > start:
return json.loads(stripped[start : end + 1])
return None
def request_qwen(endpoint: str, model: str, prompt: str, temperature: float, top_p: float, num_ctx: int) -> tuple[dict[str, Any], str, Any, list[str]]:
request_payload = {
"model": model,
"prompt": prompt,
"stream": False,
"think": False,
"keep_alive": "10m",
"options": {"num_ctx": num_ctx, "temperature": temperature, "top_p": top_p},
}
raw = post_json(endpoint, request_payload)
response_text = raw.get("response", "").strip()
parsed = None
notes: list[str] = []
try:
parsed = extract_json_object(response_text)
except (json.JSONDecodeError, TypeError) as exc:
notes.append(f"parse error: {exc}")
if parsed is None:
notes.append("could not parse JSON")
return raw, response_text, parsed, notes
def observable_action_payload(actor_id: str, selected_choice: dict[str, Any]) -> dict[str, Any]:
return {
"actor_id": actor_id,
"action_type": selected_choice["action_type"],
"observable_action": selected_choice["observable_action"],
"spoken_text": selected_choice.get("spoken_text", ""),
}
def build_maer_prompt(fixture: dict[str, Any], annotation: dict[str, Any]) -> str:
scene_id = annotation["scene_id"]
actor_id = annotation["protagonist_agent_id"]
responder_id = annotation["npc_agent_ids"][0]
scene = find_scene(fixture, scene_id)
actor = find_agent(fixture, actor_id)
pack = find_pack(scene, actor_id)
rel_actor_to_responder = find_relationship(fixture, actor_id, responder_id)
payload = {
"task": "Generate candidate player choices for Maer only. Do not write Sella's response.",
"output_contract": {
"format": "Return only JSON. No markdown. No commentary.",
"root_keys": ["choices"],
"choice_count": 4,
"valid_action_types": sorted(VALID_ACTION_TYPES),
"choice_fields": [
"branch_id",
"ink_path",
"choice_text",
"action_type",
"actor_intent_private",
"observable_action",
"spoken_text",
"state_basis",
"expected_consequence_surfaces",
"training_hooks",
],
},
"hard_rules": [
"Use only Maer's local awareness and scene-observable facts.",
"Do not include Sella's private state, private motives, or future response.",
"Do not resolve whether the corrupted packet contains a person.",
"Include at least two non-speech choices.",
f"Use action_type values only from this exact enum: {', '.join(sorted(VALID_ACTION_TYPES))}.",
"The selected branch must be reducible to observable action for Sella.",
"Use semantic training hooks, not branch ids.",
],
"scene_observable_context": {
"scene_id": scene_id,
"location": scene["location"],
"public_stakes": scene["public_stakes"],
},
"maer_local_awareness": {
"identity": actor["identity"],
"local_truth": pack["speaker_local_truth"],
"beliefs": pack["speaker_beliefs"],
"goals": goal_summaries(actor, pack["active_goals"]),
"memories": memory_summaries(actor, pack["active_memories"]),
"relationship_read": rel_actor_to_responder["summary"],
"presentation_constraints": pack["presentation_constraints"],
"selected_current_activation": selected_variables(actor),
},
"projection_controls_visible_to_maer_choice_generation": annotation["projection_controls"],
}
return (
"You are Ghostlight's Maer-local choice generator. Generate only Maer's player choices.\n"
"Do not write Sella's response or private interpretation. Return valid JSON only.\n\n"
+ json.dumps(payload, indent=2, ensure_ascii=False)
)
def sella_local_payload(fixture: dict[str, Any], annotation: dict[str, Any]) -> dict[str, Any]:
scene_id = annotation["scene_id"]
responder_id = annotation["npc_agent_ids"][0]
actor_id = annotation["protagonist_agent_id"]
scene = find_scene(fixture, scene_id)
responder = find_agent(fixture, responder_id)
pack = find_pack(scene, responder_id)
rel_responder_to_actor = find_relationship(fixture, responder_id, actor_id)
return {
"identity": responder["identity"],
"local_truth": pack["speaker_local_truth"],
"beliefs": pack["speaker_beliefs"],
"goals": goal_summaries(responder, pack["active_goals"]),
"memories": memory_summaries(responder, pack["active_memories"]),
"relationship_read": rel_responder_to_actor["summary"],
"presentation_constraints": pack["presentation_constraints"],
"selected_current_activation": selected_variables(responder),
}
def build_sella_appraisal_prompt(fixture: dict[str, Any], annotation: dict[str, Any], selected_choice: dict[str, Any]) -> str:
scene_id = annotation["scene_id"]
responder_id = annotation["npc_agent_ids"][0]
actor_id = annotation["protagonist_agent_id"]
scene = find_scene(fixture, scene_id)
payload = {
"task": "Generate Sella's participant appraisal/consolidation for one observable Maer event.",
"output_contract": {
"format": "Return only JSON. No markdown. No commentary.",
"root_keys": ["appraisal"],
"appraisal_fields": [
"responder_agent_id",
"private_interpretation",
"attributed_motive",
"misread_risk",
"immediate_state_deltas",
"relationship_deltas",
"belief_updates",
"response_constraints",
],
},
"hard_rules": [
"Use only Sella's local awareness plus the observable Maer action.",
"Do not read Maer's private intent. Treat actor intent as unavailable.",
"Sella may correctly read, partially read, or misread Maer's action.",
"Do not resolve whether the corrupted packet contains a person.",
"This pass proposes reviewed state changes for Sella caused by the event. Do not write Sella's next action.",
"Keep deltas small and bounded between -0.20 and 0.20.",
],
"scene_observable_context": {
"scene_id": scene_id,
"location": scene["location"],
"public_stakes": scene["public_stakes"],
},
"observable_maer_action": observable_action_payload(actor_id, selected_choice),
"sella_local_awareness": sella_local_payload(fixture, annotation),
"projection_controls_visible_to_sella_appraisal": annotation["projection_controls"],
}
return (
"You are Ghostlight's Sella-local appraisal generator. Generate only Sella's immediate internal appraisal.\n"
"Do not use Maer's private intent. Do not write her response. Return valid JSON only.\n\n"
+ json.dumps(payload, indent=2, ensure_ascii=False)
)
def build_sella_prompt(
fixture: dict[str, Any],
annotation: dict[str, Any],
selected_choice: dict[str, Any],
appraisal: dict[str, Any] | None,
) -> str:
scene_id = annotation["scene_id"]
responder_id = annotation["npc_agent_ids"][0]
actor_id = annotation["protagonist_agent_id"]
scene = find_scene(fixture, scene_id)
payload = {
"task": "Generate Sella's next action from Sella-local awareness after applying reviewed event appraisal.",
"output_contract": {
"format": "Return only JSON. No markdown. No commentary.",
"root_keys": ["response"],
"valid_action_types": sorted(VALID_ACTION_TYPES),
"response_fields": [
"responder_agent_id",
"action_type",
"observable_response",
"spoken_text",
"sella_private_interpretation",
"sella_attributed_motive",
"misread_risk",
"state_basis",
"reviewed_consequences",
"training_hooks",
],
},
"hard_rules": [
"Use only Sella's local awareness, the observable Maer event, and reviewed Sella state changes from that event.",
"Do not read Maer's private intent. Treat actor intent as unavailable.",
"Sella may correctly read, partially read, or misread Maer's action.",
"Do not resolve whether the corrupted packet contains a person.",
"Do not accept without cost, condition, authority defense, or resentment risk.",
f"Use action_type values only from this exact enum: {', '.join(sorted(VALID_ACTION_TYPES))}.",
"Use semantic training hooks, not branch ids.",
],
"scene_observable_context": {
"scene_id": scene_id,
"location": scene["location"],
"public_stakes": scene["public_stakes"],
},
"observable_maer_action": observable_action_payload(actor_id, selected_choice),
"sella_immediate_appraisal": appraisal,
"sella_local_awareness": sella_local_payload(fixture, annotation),
"projection_controls_visible_to_sella_response_generation": annotation["projection_controls"],
}
return (
"You are Ghostlight's Sella-local next-action generator. Generate only Sella's next action.\n"
"Use reviewed event appraisal as the changed state she is acting from. Return valid JSON only.\n\n"
+ json.dumps(payload, indent=2, ensure_ascii=False)
)
def validate_choice_payload(parsed: Any) -> list[str]:
notes: list[str] = []
if not isinstance(parsed, dict):
return ["choice response was not an object"]
choices = parsed.get("choices")
if not isinstance(choices, list):
return ["choice response missing choices array"]
if len(choices) != 4:
notes.append(f"expected 4 choices, got {len(choices)}")
required = ["branch_id", "ink_path", "choice_text", "action_type", "actor_intent_private", "observable_action", "state_basis", "expected_consequence_surfaces", "training_hooks"]
non_speech = 0
for index, choice in enumerate(choices):
if not isinstance(choice, dict):
notes.append(f"choices[{index}] was not an object")
continue
missing = [key for key in required if key not in choice]
if missing:
notes.append(f"choices[{index}] missing keys: {', '.join(missing)}")
if choice.get("action_type") not in VALID_ACTION_TYPES:
notes.append(f"choices[{index}] invalid action_type")
for list_key in ["expected_consequence_surfaces", "training_hooks"]:
if not isinstance(choice.get(list_key), list):
notes.append(f"choices[{index}] {list_key} must be an array")
if choice.get("action_type") != "speak":
non_speech += 1
if non_speech < 2:
notes.append("choices had fewer than two non-speech actions")
return notes
def validate_appraisal_payload(parsed: Any) -> list[str]:
if not isinstance(parsed, dict):
return ["appraisal was not an object"]
appraisal = parsed.get("appraisal")
if not isinstance(appraisal, dict):
return ["appraisal missing appraisal object"]
required = [
"responder_agent_id",
"private_interpretation",
"attributed_motive",
"misread_risk",
"immediate_state_deltas",
"relationship_deltas",
"response_constraints",
]
missing = [key for key in required if key not in appraisal]
notes = [f"appraisal missing keys: {', '.join(missing)}"] if missing else []
for list_key in ["immediate_state_deltas", "relationship_deltas", "response_constraints"]:
if list_key in appraisal and not isinstance(appraisal[list_key], list):
notes.append(f"appraisal.{list_key} must be an array")
return notes
def validate_response_payload(parsed: Any) -> list[str]:
if not isinstance(parsed, dict):
return ["response was not an object"]
response = parsed.get("response")
if not isinstance(response, dict):
return ["response missing response object"]
required = ["responder_agent_id", "action_type", "observable_response", "sella_private_interpretation", "misread_risk", "state_basis", "reviewed_consequences", "training_hooks"]
missing = [key for key in required if key not in response]
notes = [f"response missing keys: {', '.join(missing)}"] if missing else []
if response.get("action_type") not in VALID_ACTION_TYPES:
notes.append("response invalid action_type")
if "training_hooks" in response and not isinstance(response["training_hooks"], list):
notes.append("response.training_hooks must be an array")
return notes
def choose_selected_choice(parsed: Any, preferred_action: str) -> dict[str, Any]:
choices = parsed.get("choices", []) if isinstance(parsed, dict) else []
for choice in choices:
if choice.get("action_type") == preferred_action:
return choice
if choices:
return choices[0]
raise SystemExit("No choices available to select")
def main() -> int:
parser = argparse.ArgumentParser(description="Run a sequential local-awareness Qwen Ink prototype.")
parser.add_argument("--fixture", type=Path, default=DEFAULT_FIXTURE)
parser.add_argument("--annotation", type=Path, default=DEFAULT_ANNOTATION)
parser.add_argument("--out-dir", type=Path, default=DEFAULT_OUT_DIR)
parser.add_argument("--endpoint", default=DEFAULT_ENDPOINT)
parser.add_argument("--model", default="qwen3.5:9b")
parser.add_argument("--temperature", type=float, default=0.55)
parser.add_argument("--top-p", type=float, default=0.9)
parser.add_argument("--num-ctx", type=int, default=8192)
parser.add_argument("--capture-id", default="cold-wake-sanctuary-intake.qwen-sequential.v1")
parser.add_argument("--preferred-action", default="show_object")
args = parser.parse_args()
fixture = load_json(args.fixture)
annotation = load_json(args.annotation)
maer_prompt = build_maer_prompt(fixture, annotation)
maer_raw, maer_response_text, maer_parsed, maer_notes = request_qwen(
args.endpoint, args.model, maer_prompt, args.temperature, args.top_p, args.num_ctx
)
maer_notes.extend(validate_choice_payload(maer_parsed))
selected_choice = choose_selected_choice(maer_parsed, args.preferred_action)
sella_appraisal_prompt = build_sella_appraisal_prompt(fixture, annotation, selected_choice)
sella_appraisal_raw, sella_appraisal_response_text, sella_appraisal_parsed, sella_appraisal_notes = request_qwen(
args.endpoint, args.model, sella_appraisal_prompt, args.temperature, args.top_p, args.num_ctx
)
sella_appraisal_notes.extend(validate_appraisal_payload(sella_appraisal_parsed))
appraisal = sella_appraisal_parsed.get("appraisal") if isinstance(sella_appraisal_parsed, dict) else None
sella_prompt = build_sella_prompt(fixture, annotation, selected_choice, appraisal)
sella_raw, sella_response_text, sella_parsed, sella_notes = request_qwen(
args.endpoint, args.model, sella_prompt, args.temperature, args.top_p, args.num_ctx
)
sella_notes.extend(validate_response_payload(sella_parsed))
all_notes = maer_notes + sella_appraisal_notes + sella_notes
capture = {
"schema_version": "ghostlight.qwen_ink_sequential_capture.v0",
"capture_id": args.capture_id,
"source_fixture_ref": str(args.fixture.relative_to(ROOT)).replace("\\", "/"),
"source_annotation_ref": str(args.annotation.relative_to(ROOT)).replace("\\", "/"),
"model": maer_raw.get("model", args.model),
"endpoint": args.endpoint,
"request_options": {
"think": False,
"num_ctx": args.num_ctx,
"temperature": args.temperature,
"top_p": args.top_p,
},
"passes": {
"maer_choice_generation": {
"local_agent_id": "maer_tidecall",
"prompt_text": maer_prompt,
"response_text": maer_response_text,
"parsed_response": maer_parsed,
"validation_notes": maer_notes,
},
"selected_observable_action": selected_choice,
"sella_immediate_appraisal": {
"local_agent_id": "sella_ren",
"prompt_text": sella_appraisal_prompt,
"response_text": sella_appraisal_response_text,
"parsed_response": sella_appraisal_parsed,
"validation_notes": sella_appraisal_notes,
},
"sella_response_generation": {
"local_agent_id": "sella_ren",
"prompt_text": sella_prompt,
"response_text": sella_response_text,
"parsed_response": sella_parsed,
"validation_notes": sella_notes,
},
},
"review": {
"status": "accepted_as_draft" if not all_notes else "useful_needs_revision",
"strengths": [
"Separated Maer choice generation from Sella next-action generation.",
"Inserted participant appraisal/consolidation before next-actor action generation.",
"Sella prompt received observable Maer event and reviewed Sella state changes, not Maer's private intent.",
],
"failure_notes": all_notes,
"accepted_into_ink_ref": None,
"pipeline_lessons": [
"Sequential generation needs event appraisal/consolidation before next actor selection.",
"Action type selection should be tool-shaped or schema-constrained, not left to prose compliance.",
"Next pass should materialize the sequential capture into Ink and mutation training data.",
],
},
}
args.out_dir.mkdir(parents=True, exist_ok=True)
base = args.capture_id.replace(".", "-")
capture_path = args.out_dir / f"{base}.capture.json"
maer_prompt_path = args.out_dir / f"{base}.maer.prompt.md"
maer_response_path = args.out_dir / f"{base}.maer.response.md"
sella_appraisal_prompt_path = args.out_dir / f"{base}.sella-appraisal.prompt.md"
sella_appraisal_response_path = args.out_dir / f"{base}.sella-appraisal.response.md"
sella_prompt_path = args.out_dir / f"{base}.sella.prompt.md"
sella_response_path = args.out_dir / f"{base}.sella.response.md"
write_json(capture_path, capture)
write_text(maer_prompt_path, maer_prompt + "\n")
write_text(maer_response_path, maer_response_text + "\n")
write_text(sella_appraisal_prompt_path, sella_appraisal_prompt + "\n")
write_text(sella_appraisal_response_path, sella_appraisal_response_text + "\n")
write_text(sella_prompt_path, sella_prompt + "\n")
write_text(sella_response_path, sella_response_text + "\n")
print(f"wrote {capture_path.relative_to(ROOT)}")
print(json.dumps({"selected_observable_action": selected_choice, "sella_appraisal": sella_appraisal_parsed, "sella_response": sella_parsed}, indent=2, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())