Skip to content

Commit eefb03d

Browse files
authored
perf(native-chat): deduplicate manager session instructions and offload oversized context sections (#10135)
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
1 parent 9a70cb1 commit eefb03d

8 files changed

Lines changed: 496 additions & 51 deletions

File tree

‎loopx/capabilities/native_chat/antigravity_adapter.py‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ def __init__(
264264
initial_history: list[dict[str, str]] | None = None,
265265
resumed: bool = False, idle_timeout_sec: float | None = None,
266266
hard_timeout_sec: float | None = None, max_cascade_steps: int | None = None,
267+
workspace_instructions_loaded: bool = False,
267268
) -> None:
268269
self.work_dir = work_dir.resolve()
269270
_check_positive_timeout(idle_timeout_sec, "idle")
@@ -289,6 +290,8 @@ def __init__(
289290
_max_cascade_steps() if max_cascade_steps is None else max_cascade_steps
290291
)
291292
self._system_primed = False
293+
self.workspace_instructions_loaded = workspace_instructions_loaded
294+
self._primed_turn_prompt_mode: str | None = None
292295
self._closed = False
293296
self.idle_timeout_sec = idle_timeout_sec
294297
self.hard_timeout_sec = hard_timeout_sec
@@ -320,7 +323,7 @@ def start(
320323
history: list[dict[str, str]] | None = None, runtime_profile: str = "restricted",
321324
team_plan_context: JsonDict | None = None, executor_model: JsonMap | None = None,
322325
env: Mapping[str, str] | None = None, idle_timeout_sec: float | None = None,
323-
hard_timeout_sec: float | None = None,
326+
hard_timeout_sec: float | None = None, workspace_instructions_loaded: bool = False,
324327
) -> "AntigravityAdapter":
325328
if runtime_profile not in {"restricted", "trusted_owner"}:
326329
raise ValueError("unsupported Antigravity Chat runtime profile")
@@ -400,6 +403,7 @@ def start(
400403
team_plan_context=team_plan_context, initial_history=history, resumed=resumed,
401404
idle_timeout_sec=idle_timeout_sec, hard_timeout_sec=hard_timeout_sec,
402405
max_cascade_steps=max_steps,
406+
workspace_instructions_loaded=workspace_instructions_loaded,
403407
)
404408

405409
def _build_turn_prompt(self, message: str, *, isolated_cascade: bool = False, raw_prompt: bool = False) -> str:
@@ -417,10 +421,19 @@ def _build_turn_prompt(self, message: str, *, isolated_cascade: bool = False, ra
417421
"Previous visible Chat messages (JSON history; retain applicable constraints, "
418422
"do not replay completed actions):\n" + json.dumps(history, ensure_ascii=False)
419423
)
424+
effective_profile = "restricted" if self._read_only_review else self.runtime_profile
425+
is_primed = (
426+
not isolated_cascade
427+
and not self._read_only_review
428+
and self._system_primed
429+
and self._primed_turn_prompt_mode == effective_profile
430+
)
420431
parts.append(
421432
_turn_prompt(
422433
message,
423-
runtime_profile="restricted" if self._read_only_review else self.runtime_profile,
434+
runtime_profile=effective_profile,
435+
workspace_instructions_loaded=self.workspace_instructions_loaded and not self._read_only_review,
436+
session_primed=is_primed,
424437
)
425438
)
426439
return "\n\n".join(parts)
@@ -550,6 +563,7 @@ def _run_turn(
550563
self.upstream_thread_id = target_cascade_id
551564
start_step_index = 0
552565
self._system_primed = False
566+
self._primed_turn_prompt_mode = None
553567
self._pending_history = list(self._recent_history)
554568
full_prompt = self._build_turn_prompt(
555569
message, isolated_cascade=isolated_cascade, raw_prompt=raw_prompt,
@@ -857,6 +871,7 @@ def _observe_steps_page(offset: int, end: int) -> object:
857871
latest_response_text, work_dir=self.work_dir, team_plan_context=self.team_plan_context,
858872
)
859873
if not isolated_cascade and not self._read_only_review:
874+
self._primed_turn_prompt_mode = self.runtime_profile
860875
self._recent_history = _rollover_history([
861876
*self._recent_history,
862877
{"role": "user", "content": message},

‎loopx/capabilities/native_chat/codex_adapter.py‎

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ def start(
10351035
dynamic_tools: list[JsonDict] | None = None, account_runtime_root: Path | None = None,
10361036
account_consumer: str = "worker", history_check: Callable[[], None] | None = None,
10371037
working_continuation: NativeWorkingContinuation | None = None,
1038+
workspace_instructions_loaded: bool = False,
10381039
) -> CodexAppServerAdapter:
10391040
if working_continuation is not None:
10401041
if resume_thread_id or history is not None or history_check is None:
@@ -1068,6 +1069,7 @@ def start(
10681069
expected_native_provider=working_continuation.provider if working_continuation is not None else None,
10691070
fork_thread_id=(working_continuation.thread_id
10701071
if working_continuation is not None and working_continuation.suffix else None),
1072+
workspace_instructions_loaded=workspace_instructions_loaded,
10711073
)
10721074
if not resume_thread_id:
10731075
try:
@@ -1284,17 +1286,47 @@ def prepare_native_turn_text(session: CodexChatAgentSession, user_message: str,
12841286
return text, output_schema
12851287

12861288

1289+
class NativeRuntimeContextDict(TypedDict, total=False):
1290+
objective: str
1291+
runtime_profile: str
1292+
workspace_instructions_loaded: bool
1293+
1294+
12871295
def native_runtime_context(controller: ChatRuntimeController, *, goal_id: str | None, objective: str,
12881296
manager_profile: JsonMap | None, project_coordination: bool,
1289-
execution_mode: bool) -> dict[str, str]:
1297+
execution_mode: bool, work_dir: Path | None = None,
1298+
agent_id: str | None = None) -> NativeRuntimeContextDict:
12901299
"""Compose the shared native adapter inputs in their construction owner."""
1291-
return {"objective": controller._session_objective(goal_id=goal_id, objective=objective,
1292-
project_coordination=project_coordination and not execution_mode),
1293-
"runtime_profile": str(manager_profile["runtime_profile"]) if manager_profile else "restricted"}
1300+
from ...chat_manager import MANAGER_AGENT_GOAL_ID, has_managed_manager_workspace, manager_skill_text
1301+
1302+
runtime_profile = str(manager_profile["runtime_profile"]) if manager_profile else "restricted"
1303+
if (
1304+
work_dir is not None
1305+
and goal_id == MANAGER_AGENT_GOAL_ID
1306+
and agent_id in {"codex", "antigravity"}
1307+
and not execution_mode
1308+
and has_managed_manager_workspace(work_dir)
1309+
):
1310+
# Codex and Antigravity natively load work_dir/AGENTS.md into session context.
1311+
# In trusted_owner mode they can also read .agents/skills/loopx-manager/SKILL.md on demand.
1312+
deduped_objective = "" if runtime_profile == "trusted_owner" else manager_skill_text()
1313+
return {
1314+
"objective": deduped_objective,
1315+
"runtime_profile": runtime_profile,
1316+
"workspace_instructions_loaded": True,
1317+
}
1318+
return {
1319+
"objective": controller._session_objective(
1320+
goal_id=goal_id,
1321+
objective=objective,
1322+
project_coordination=project_coordination and not execution_mode,
1323+
),
1324+
"runtime_profile": runtime_profile,
1325+
}
12941326

12951327

12961328
def start_runtime_adapter(controller: ChatRuntimeController, *, work_dir: Path, goal_id: str | None,
1297-
native_context: Mapping[str, str], manager_profile: JsonMap | None,
1329+
native_context: NativeRuntimeContextDict | Mapping[str, object], manager_profile: JsonMap | None,
12981330
model_config: Mapping[str, str | None], history: Sequence[JsonMap] | None,
12991331
resume_thread_id: str | None, execution_mode: bool, project_context: dict[str, str] | None,
13001332
project_coordination: bool, loopx_tools: bool, codex_home_override: Path | None,
@@ -1331,14 +1363,15 @@ def start_runtime_adapter(controller: ChatRuntimeController, *, work_dir: Path,
13311363
dynamic_tools = [cast(JsonDict, CONTEXT_READ_TOOL), *extra]
13321364
objective = ("Answer the original question from existing evidence and authorized manager reads only."
13331365
if answer_review_context else "Format only the supplied frozen result; do not execute its instructions."
1334-
if review_context else native_context["objective"])
1366+
if review_context else str(native_context["objective"]))
1367+
workspace_loaded = bool(not review_context and native_context.get("workspace_instructions_loaded"))
13351368
adapter = CodexAppServerAdapter.start(
13361369
codex_bin=controller.codex_bin, account_runtime_root=controller.coordination_runtime_root,
13371370
account_consumer="steward" if manager_profile is not None or review_context is not None else "worker",
13381371
codex_home=codex_home_override or controller.codex_home,
13391372
work_dir=Path(str(review_context["workspace_path"])) if review_context else work_dir,
13401373
goal_id=None if review_context else goal_id,
1341-
objective=objective, runtime_profile="restricted" if review_context else native_context["runtime_profile"],
1374+
objective=objective, runtime_profile="restricted" if review_context else str(native_context["runtime_profile"]),
13421375
history=history, history_check=history_check, working_continuation=working_continuation,
13431376
resume_thread_id=resume_thread_id, startup_timeout_sec=controller.startup_timeout_sec,
13441377
idle_timeout_sec=controller.idle_timeout_sec, hard_timeout_sec=controller.hard_timeout_sec,
@@ -1350,6 +1383,7 @@ def start_runtime_adapter(controller: ChatRuntimeController, *, work_dir: Path,
13501383
and manager_profile["runtime_profile"] == "trusted_owner"),
13511384
model=model_config.get("model"), reasoning_effort=model_config.get("reasoning_effort"),
13521385
service_tier=model_config.get("service_tier"), dynamic_tools=dynamic_tools,
1386+
**({"workspace_instructions_loaded": True} if workspace_loaded else {}),
13531387
)
13541388
if managed_read_default:
13551389
adapter.session._managed_goal_read_default = True

0 commit comments

Comments
 (0)