mirror of
https://github.com/usestrix/strix.git
synced 2026-08-23 19:32:37 +02:00
feat(migration): phase 2.1-2.3 — sandbox dispatch + thin slice tool wrappers
Phase 2.1 — sandbox dispatch helper:
- strix/tools/_sandbox_dispatch.py: post_to_sandbox() centralizes the
host->container HTTP wire format. Connect=10s, read=150s timeouts mirror
legacy executor.py. 50 MB response cap (C18) prevents OOM from a runaway
tool. All errors surface as {"error": str} so the model can recover
instead of the run dying.
Phase 2.2 — C6 lock-protected JSONL writes:
- strix/tools/notes/notes_actions.py: notes.jsonl appends are now wrapped
in _notes_lock so concurrent agents can't interleave half-written lines.
Regression test in test_notes_jsonl_concurrency.py verifies 1000 parallel
writes produce exactly 1000 valid JSON lines.
Phase 2.3 — thin-slice SDK wrappers (think + todo + notes):
- strix/tools/_legacy_adapter.py: LegacyAgentStateAdapter shim — exposes
just enough surface (.agent_id) for legacy tools that close over
agent_state, sourced from ctx.context['agent_id'].
- strix/tools/thinking/thinking_sdk_tools.py: 1 tool (think).
- strix/tools/todo/todo_sdk_tools.py: 6 tools (create/list/update/done/
pending/delete) with bulk-form preserved.
- strix/tools/notes/notes_sdk_tools.py: 5 tools (create/list/get/update/
delete) with asyncio.to_thread around the lock-protected file I/O.
Tests: 22 new tests pass (10 sandbox dispatch + 2 concurrency + 10 SDK
local). Full suite still green.
Per-file ruff ignores added for SDK wrapper files: TC002 (RunContextWrapper
must be runtime-importable because the SDK calls get_type_hints() to
derive the JSON schema) and PLR0911 (sandbox dispatch's 10 short-circuit
returns are intentional, each a distinct documented failure mode).
Refs: PLAYBOOK.md §3.4, AUDIT_R3.md C6/C18.
This commit is contained in:
@@ -38,6 +38,12 @@ def _get_notes_jsonl_path() -> Path | None:
|
||||
|
||||
|
||||
def _append_note_event(op: str, note_id: str, note: dict[str, Any] | None = None) -> None:
|
||||
"""Append one note operation to the run's ``notes/notes.jsonl``.
|
||||
|
||||
C6 (AUDIT_R2.md §1.1): hold ``_notes_lock`` across the file open + write
|
||||
so two concurrent agents (or two parallel SDK tool calls in Phase 6)
|
||||
cannot interleave bytes mid-line and corrupt the JSONL.
|
||||
"""
|
||||
notes_path = _get_notes_jsonl_path()
|
||||
if not notes_path:
|
||||
return
|
||||
@@ -50,7 +56,7 @@ def _append_note_event(op: str, note_id: str, note: dict[str, Any] | None = None
|
||||
if note is not None:
|
||||
event["note"] = note
|
||||
|
||||
with notes_path.open("a", encoding="utf-8") as f:
|
||||
with _notes_lock, notes_path.open("a", encoding="utf-8") as f:
|
||||
f.write(f"{json.dumps(event, ensure_ascii=True)}\n")
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
"""SDK function-tool wrappers for the legacy notes tools.
|
||||
|
||||
Five tools, all module-global (no per-agent silo). The legacy
|
||||
``notes_actions.py`` module already implements JSONL persistence and
|
||||
wiki Markdown rendering; these wrappers are pure delegation.
|
||||
|
||||
The C6 fix (lock-protected JSONL writes) was applied directly to the
|
||||
legacy module, so both code paths benefit.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from agents import RunContextWrapper
|
||||
|
||||
from strix.tools._decorator import strix_tool
|
||||
from strix.tools.notes import notes_actions as _legacy
|
||||
|
||||
|
||||
def _dump(result: dict[str, Any]) -> str:
|
||||
return json.dumps(result, ensure_ascii=False, default=str)
|
||||
|
||||
|
||||
@strix_tool(timeout=30)
|
||||
async def create_note(
|
||||
ctx: RunContextWrapper,
|
||||
title: str,
|
||||
content: str,
|
||||
category: str = "general",
|
||||
tags: list[str] | None = None,
|
||||
) -> str:
|
||||
"""Create a note in the current run's notes store.
|
||||
|
||||
Notes are persisted to ``run_dir/notes/notes.jsonl`` and (for the
|
||||
``wiki`` category) rendered as Markdown to ``run_dir/wiki/<slug>.md``.
|
||||
|
||||
Args:
|
||||
title: Required, non-empty title.
|
||||
content: Note body. Markdown is preserved.
|
||||
category: One of ``"general" | "findings" | "methodology" |
|
||||
"questions" | "plan" | "wiki"``.
|
||||
tags: Optional list of free-form tags.
|
||||
"""
|
||||
# The legacy function does file I/O under a threading.RLock.
|
||||
# Wrap in to_thread so we don't block the event loop while waiting
|
||||
# on the lock or fsync.
|
||||
result = await asyncio.to_thread(
|
||||
_legacy.create_note,
|
||||
title=title,
|
||||
content=content,
|
||||
category=category,
|
||||
tags=tags,
|
||||
)
|
||||
return _dump(result)
|
||||
|
||||
|
||||
@strix_tool(timeout=30)
|
||||
async def list_notes(
|
||||
ctx: RunContextWrapper,
|
||||
category: str | None = None,
|
||||
tags: list[str] | None = None,
|
||||
search: str | None = None,
|
||||
include_content: bool = False,
|
||||
) -> str:
|
||||
"""List notes, optionally filtered.
|
||||
|
||||
Args:
|
||||
category: Filter by category.
|
||||
tags: Filter to notes that have any of these tags.
|
||||
search: Substring match against title and content.
|
||||
include_content: When False (default), entries get a ``content_preview``;
|
||||
when True, full content is included.
|
||||
"""
|
||||
result = await asyncio.to_thread(
|
||||
_legacy.list_notes,
|
||||
category=category,
|
||||
tags=tags,
|
||||
search=search,
|
||||
include_content=include_content,
|
||||
)
|
||||
return _dump(result)
|
||||
|
||||
|
||||
@strix_tool(timeout=30)
|
||||
async def get_note(ctx: RunContextWrapper, note_id: str) -> str:
|
||||
"""Fetch one note by its 5-char ID. Returns full content."""
|
||||
result = await asyncio.to_thread(_legacy.get_note, note_id=note_id)
|
||||
return _dump(result)
|
||||
|
||||
|
||||
@strix_tool(timeout=30)
|
||||
async def update_note(
|
||||
ctx: RunContextWrapper,
|
||||
note_id: str,
|
||||
title: str | None = None,
|
||||
content: str | None = None,
|
||||
tags: list[str] | None = None,
|
||||
) -> str:
|
||||
"""Update a note's title, content, or tags. Pass ``None`` to leave a field unchanged."""
|
||||
result = await asyncio.to_thread(
|
||||
_legacy.update_note,
|
||||
note_id=note_id,
|
||||
title=title,
|
||||
content=content,
|
||||
tags=tags,
|
||||
)
|
||||
return _dump(result)
|
||||
|
||||
|
||||
@strix_tool(timeout=30)
|
||||
async def delete_note(ctx: RunContextWrapper, note_id: str) -> str:
|
||||
"""Delete a note. For wiki notes, also removes the rendered Markdown file."""
|
||||
result = await asyncio.to_thread(_legacy.delete_note, note_id=note_id)
|
||||
return _dump(result)
|
||||
Reference in New Issue
Block a user