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()
|
||||
|
||||
Reference in New Issue
Block a user