get_workspace sections + teaching truncation note + assets-role editors
On a large project (43 scenes with full action paragraphs) get_workspace serializes past the 4000-char tool-result cap, so the tail-chop landed mid-beats and characters/locations/items were ALWAYS below the waterline — observed live: an assets sub-agent re-called get_workspace in a loop, blind to the sections it needed, with no editor tools in its allowlist anyway. - get_workspace gains sections=[beats|characters|locations|items|scenes]; project + stats always included; omitted sections listed; invalid sections error names the valid set; no-arg call unchanged (back-compat). - All three truncation sites share TRUNCATE_NOTE, which names the way out (sections=[...], scoped tools) instead of a bare '[truncated]'. - The assets role gains update_character / update_location / update_item (deletes deliberately excluded). (cherry picked from commit 9460b6c2e398bad5403ff73e4e8e7d2a96478821)
This commit is contained in:
@@ -37,6 +37,16 @@ TASK_START = "task/start"
|
||||
TASK_END = "task/end"
|
||||
|
||||
TOOL_RESULT_TRUNCATE = 4000
|
||||
# Appended wherever a tool result is cut for the LLM. Must TEACH the way out —
|
||||
# a bare "[truncated]" sends agents into retry loops hoping for a different
|
||||
# cut (observed live 2026-08-24: an assets sub-agent re-called get_workspace
|
||||
# repeatedly, blind to characters/locations below the cut).
|
||||
TRUNCATE_NOTE = (
|
||||
"…[truncated — the full result is too large for one reply. Fetch a "
|
||||
"smaller slice instead of retrying: get_workspace accepts "
|
||||
"sections=[\"characters\",\"locations\",\"items\",\"beats\",\"scenes\"], "
|
||||
"and scoped tools (get_story, list_scenes) return less.]"
|
||||
)
|
||||
|
||||
# Cap for ANY single event payload at append time: keeps the append-only log
|
||||
# faithful in shape but bounded in size (a 500 KB tool result would otherwise
|
||||
@@ -311,7 +321,7 @@ def derive_llm_history(
|
||||
def _truncate_result(result: Any) -> str:
|
||||
text = json.dumps(result, ensure_ascii=False, default=str)
|
||||
if len(text) > TOOL_RESULT_TRUNCATE:
|
||||
return text[:TOOL_RESULT_TRUNCATE] + "…[truncated]"
|
||||
return text[:TOOL_RESULT_TRUNCATE] + TRUNCATE_NOTE
|
||||
return text
|
||||
|
||||
|
||||
|
||||
@@ -242,7 +242,7 @@ async def run_turn(
|
||||
)
|
||||
result_text = json.dumps(result, ensure_ascii=False, default=str)
|
||||
if len(result_text) > session_log.TOOL_RESULT_TRUNCATE:
|
||||
result_text = result_text[: session_log.TOOL_RESULT_TRUNCATE] + "…[truncated]"
|
||||
result_text = result_text[: session_log.TOOL_RESULT_TRUNCATE] + session_log.TRUNCATE_NOTE
|
||||
messages.append(
|
||||
{
|
||||
"role": "tool",
|
||||
|
||||
@@ -44,6 +44,9 @@ ROLE_TOOLS: dict[str, list[str]] = {
|
||||
],
|
||||
"assets": [
|
||||
"get_workspace",
|
||||
"update_character",
|
||||
"update_location",
|
||||
"update_item",
|
||||
"list_workflows",
|
||||
"comfy_server_info",
|
||||
"run_workflow",
|
||||
@@ -471,7 +474,7 @@ async def _run_sub_agent(
|
||||
)
|
||||
result_text = json.dumps(result, ensure_ascii=False, default=str)
|
||||
if len(result_text) > session_log.TOOL_RESULT_TRUNCATE:
|
||||
result_text = result_text[: session_log.TOOL_RESULT_TRUNCATE] + "…[truncated]"
|
||||
result_text = result_text[: session_log.TOOL_RESULT_TRUNCATE] + session_log.TRUNCATE_NOTE
|
||||
messages.append(
|
||||
{"role": "tool", "tool_call_id": call_id, "content": result_text}
|
||||
)
|
||||
|
||||
@@ -21,11 +21,35 @@ def register(registry: ToolRegistry) -> None:
|
||||
name="get_workspace",
|
||||
description=(
|
||||
"Get the current workspace: session linkage and, when linked, "
|
||||
"the project with story beats, characters, locations, scenes, "
|
||||
"and stats. ALWAYS call this before editing anything or when "
|
||||
"you need current ids/counts."
|
||||
"the project with story beats, characters, locations, items, "
|
||||
"scenes, and stats. ALWAYS call this before editing anything or "
|
||||
"when you need current ids/counts. On a LARGE project the full "
|
||||
"result gets truncated — pass sections (e.g. "
|
||||
"[\"characters\",\"locations\",\"items\"]) to fetch only what "
|
||||
"you need; project + stats are always included."
|
||||
),
|
||||
parameters={"type": "object", "properties": {}},
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sections": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"beats",
|
||||
"characters",
|
||||
"locations",
|
||||
"items",
|
||||
"scenes",
|
||||
],
|
||||
},
|
||||
"description": (
|
||||
"Which sections to include. Omit for all — but on "
|
||||
"large projects prefer only the sections you need."
|
||||
),
|
||||
}
|
||||
},
|
||||
},
|
||||
executor=t_get_workspace,
|
||||
requires_project=False,
|
||||
category="workspace",
|
||||
@@ -143,6 +167,9 @@ def register(registry: ToolRegistry) -> None:
|
||||
)
|
||||
|
||||
|
||||
_WS_SECTIONS = ("beats", "characters", "locations", "items", "scenes")
|
||||
|
||||
|
||||
# ── executors ───────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -170,6 +197,16 @@ async def t_get_workspace(ctx: ToolContext, args: dict[str, Any]) -> dict[str, A
|
||||
out["mode"] = "sandbox"
|
||||
out["hint"] = "Linked project no longer exists."
|
||||
return out
|
||||
requested = args.get("sections")
|
||||
if requested:
|
||||
wanted = {sec for sec in requested if sec in _WS_SECTIONS}
|
||||
if not wanted:
|
||||
return {
|
||||
"ok": False,
|
||||
"error": f"No valid sections in {requested!r}; valid: {sorted(_WS_SECTIONS)}",
|
||||
}
|
||||
else:
|
||||
wanted = set(_WS_SECTIONS)
|
||||
beats = [
|
||||
row_to_dict(r)
|
||||
for r in conn.execute(
|
||||
@@ -177,7 +214,7 @@ async def t_get_workspace(ctx: ToolContext, args: dict[str, Any]) -> dict[str, A
|
||||
"WHERE project_id = ? ORDER BY order_index",
|
||||
(ctx.project_id,),
|
||||
).fetchall()
|
||||
]
|
||||
] if "beats" in wanted else None
|
||||
characters = [
|
||||
row_to_dict(r)
|
||||
for r in conn.execute(
|
||||
@@ -185,7 +222,7 @@ async def t_get_workspace(ctx: ToolContext, args: dict[str, Any]) -> dict[str, A
|
||||
"sheet_path, consistency_prompt FROM characters WHERE project_id = ?",
|
||||
(ctx.project_id,),
|
||||
).fetchall()
|
||||
]
|
||||
] if "characters" in wanted else None
|
||||
locations = [
|
||||
row_to_dict(r)
|
||||
for r in conn.execute(
|
||||
@@ -193,7 +230,7 @@ async def t_get_workspace(ctx: ToolContext, args: dict[str, Any]) -> dict[str, A
|
||||
"FROM locations WHERE project_id = ?",
|
||||
(ctx.project_id,),
|
||||
).fetchall()
|
||||
]
|
||||
] if "locations" in wanted else None
|
||||
items = [
|
||||
row_to_dict(r)
|
||||
for r in conn.execute(
|
||||
@@ -201,7 +238,7 @@ async def t_get_workspace(ctx: ToolContext, args: dict[str, Any]) -> dict[str, A
|
||||
"FROM items WHERE project_id = ?",
|
||||
(ctx.project_id,),
|
||||
).fetchall()
|
||||
]
|
||||
] if "items" in wanted else None
|
||||
scenes = [
|
||||
row_to_dict(r)
|
||||
for r in conn.execute(
|
||||
@@ -209,15 +246,21 @@ async def t_get_workspace(ctx: ToolContext, args: dict[str, Any]) -> dict[str, A
|
||||
"location_id, video_path FROM scenes WHERE project_id = ? ORDER BY order_index",
|
||||
(ctx.project_id,),
|
||||
).fetchall()
|
||||
]
|
||||
] if "scenes" in wanted else None
|
||||
out["mode"] = "linked"
|
||||
out["project"] = project
|
||||
out["stats"] = _project_stats(ctx.project_id, conn)
|
||||
out["beats"] = beats
|
||||
out["characters"] = characters
|
||||
out["locations"] = locations
|
||||
out["items"] = items
|
||||
out["scenes"] = scenes
|
||||
for key, value in (
|
||||
("beats", beats),
|
||||
("characters", characters),
|
||||
("locations", locations),
|
||||
("items", items),
|
||||
("scenes", scenes),
|
||||
):
|
||||
if value is not None:
|
||||
out[key] = value
|
||||
if len(wanted) < len(_WS_SECTIONS):
|
||||
out["sections_omitted"] = sorted(set(_WS_SECTIONS) - wanted)
|
||||
return out
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
@@ -1791,3 +1791,72 @@ def test_unlink_project_requires_linked_session(client):
|
||||
out = asyncio.run(execute_tool(ctx, "unlink_project", {}))
|
||||
assert out["ok"] is False
|
||||
assert "requires a linked project" in out["error"]
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────
|
||||
# get_workspace sections + teaching truncation + assets-role editors
|
||||
# (Observed live 2026-08-25: on a 43-scene project the assets sub-agent's
|
||||
# get_workspace result truncated mid-beats, hiding characters/locations/items
|
||||
# entirely; it retried in a loop and had no editor tools anyway.)
|
||||
# ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_get_workspace_sections_scoped_fetch(client):
|
||||
from calliope.agent.harness.registry import ToolContext
|
||||
from calliope.agent.harness.tools import execute_tool
|
||||
|
||||
pid = _mk_project(client, "Sectioned")
|
||||
conn = get_db(settings.db_path)
|
||||
try:
|
||||
conn.execute(
|
||||
"INSERT INTO characters (project_id, name, role) VALUES (?, ?, ?)",
|
||||
(pid, "Mia", "lead"),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO story_beats (project_id, order_index, title, description) "
|
||||
"VALUES (?, 1, 'Open', 'a')",
|
||||
(pid,),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
sid = _mk_session(project_id=pid)
|
||||
ctx = ToolContext(session_id=sid, project_id=pid)
|
||||
|
||||
out = asyncio.run(execute_tool(ctx, "get_workspace", {"sections": ["characters"]}))
|
||||
assert out["mode"] == "linked"
|
||||
assert [c["name"] for c in out["characters"]] == ["Mia"]
|
||||
assert "beats" not in out and "scenes" not in out
|
||||
assert "beats" in out["sections_omitted"]
|
||||
assert "stats" in out and "project" in out # always included
|
||||
|
||||
# No sections -> everything (back-compat)
|
||||
out = asyncio.run(execute_tool(ctx, "get_workspace", {}))
|
||||
for key in ("beats", "characters", "locations", "items", "scenes"):
|
||||
assert key in out
|
||||
assert "sections_omitted" not in out
|
||||
|
||||
# Garbage sections -> explicit error, not a silent full fetch
|
||||
out = asyncio.run(execute_tool(ctx, "get_workspace", {"sections": ["bogus"]}))
|
||||
assert out["ok"] is False
|
||||
assert "valid" in out["error"]
|
||||
|
||||
|
||||
def test_truncation_note_teaches_scoped_fetch():
|
||||
from calliope.agent.harness import log as session_log
|
||||
|
||||
big = {"blob": "x" * (session_log.TOOL_RESULT_TRUNCATE + 100)}
|
||||
text = session_log._truncate_result(big)
|
||||
assert "sections=" in text
|
||||
assert len(text) < session_log.TOOL_RESULT_TRUNCATE + len(session_log.TRUNCATE_NOTE) + 1
|
||||
|
||||
|
||||
def test_assets_role_has_editors_and_they_resolve():
|
||||
from calliope.agent.harness.orchestrator import ROLE_TOOLS, _scoped_payload
|
||||
from calliope.agent.harness.registry import ToolContext
|
||||
|
||||
for tool in ("update_character", "update_location", "update_item"):
|
||||
assert tool in ROLE_TOOLS["assets"]
|
||||
ctx = ToolContext(session_id=9_999_003, project_id=99)
|
||||
names = {t["function"]["name"] for t in _scoped_payload(ctx, ROLE_TOOLS["assets"])}
|
||||
assert {"update_character", "update_location", "update_item", "get_workspace"} <= names
|
||||
|
||||
Reference in New Issue
Block a user