workerid: one decision of who is a worker, lazy transcript recognition, opening patterns as data. done: module / next: wire callers
This commit is contained in:
1 parent
2891c95887
commit
858401d58e
3 files changed
+136
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"_about": "What an UNLISTED host tool is, judged by its name (sisi/orchestration.py `classify`). A name is split into words at punctuation and case changes; a phrase matches when its words appear next to each other. `delegation`: the tool starts a worker, so it is never counted as direct work or refused by the meter. `control`: the tool only reads status, waits, cancels, lists, schedules or messages, so it is not counted. A tool named by neither still counts as direct work. Listed tools (canon/orchestration-tools.json) always win.",
|
||||||
|
"delegation": ["delegate", "delegation", "subagent", "sub_agent", "spawn", "thread_launch"],
|
||||||
|
"control": ["status", "wait", "cancel", "list", "read_thread", "schedule", "scheduled", "notify", "send", "ask"]
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"_about": "How a session's FIRST user message says it is a delegated worker (sisi/workerid.py). Each phrase is matched case-insensitively, with white space collapsed, inside the first `window` characters of that one message only; the brief template's own first line always counts too. A phrase later in a long message, or in a later message, never counts: a session a person started is a main thread.",
|
||||||
|
"window": 400,
|
||||||
|
"openings": [
|
||||||
|
"you are a worker",
|
||||||
|
"you are a subagent",
|
||||||
|
"you are a sub-agent",
|
||||||
|
"you are a delegate",
|
||||||
|
"do this task yourself",
|
||||||
|
"do the task yourself",
|
||||||
|
"do not delegate"
|
||||||
|
],
|
||||||
|
"head_bytes": 1048576
|
||||||
|
}
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
"""Who is a worker: the one decision, used by the contract guard, the meter, the delegation guard, the fallback
|
||||||
|
logic and the gate (R-7.1-1, R-7.2-2, R-16-1). A session is a worker when the host marks it as a subagent,
|
||||||
|
`sisi delegate` started it, or its FIRST user message opens like a brief (canon/worker-openings.json). A session
|
||||||
|
a person started is a main thread. Fails open: an error is "main thread" (the safe default), never stored."""
|
||||||
|
import json, os
|
||||||
|
|
||||||
|
from . import paths, store
|
||||||
|
|
||||||
|
_DATA = {}
|
||||||
|
|
||||||
|
|
||||||
|
def _data():
|
||||||
|
if not _DATA:
|
||||||
|
try:
|
||||||
|
with open(paths.canon("worker-openings.json"), encoding="utf-8") as f:
|
||||||
|
_DATA.update(json.load(f))
|
||||||
|
except (OSError, ValueError):
|
||||||
|
return {}
|
||||||
|
return _DATA
|
||||||
|
|
||||||
|
|
||||||
|
def _squash(text):
|
||||||
|
return " ".join(str(text or "").split()).lower()
|
||||||
|
|
||||||
|
|
||||||
|
def opens_as_brief(text):
|
||||||
|
"""Does this first user message open like a brief: a listed phrase (or the template's first line) within the
|
||||||
|
first `window` characters?"""
|
||||||
|
d = _data()
|
||||||
|
try:
|
||||||
|
window = int(d.get("window") or 400)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
window = 400
|
||||||
|
head = _squash(str(text or "")[:window * 3])[:window]
|
||||||
|
phrases = [_squash(p) for p in d.get("openings") or [] if isinstance(p, str) and p.strip()]
|
||||||
|
try:
|
||||||
|
from . import brief
|
||||||
|
phrases.append(_squash(brief.template().splitlines()[0]))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return any(p and p in head for p in phrases)
|
||||||
|
|
||||||
|
|
||||||
|
def _read_head(path, nbytes):
|
||||||
|
"""(bytes, whole) from the start of the file: `whole` says the file ended inside the bound."""
|
||||||
|
with open(os.path.expanduser(path), "rb") as f:
|
||||||
|
raw = f.read(nbytes + 1)
|
||||||
|
return raw[:nbytes], len(raw) <= nbytes
|
||||||
|
|
||||||
|
|
||||||
|
def first_user_text(path):
|
||||||
|
"""(found, text): the first message the person typed, from only the head of the transcript. `found` is False
|
||||||
|
when the transcript cannot be read or holds no such message yet."""
|
||||||
|
from . import harvest
|
||||||
|
try:
|
||||||
|
nbytes = int(_data().get("head_bytes") or 1048576)
|
||||||
|
raw, whole = _read_head(path, nbytes)
|
||||||
|
except (OSError, TypeError, ValueError):
|
||||||
|
return False, ""
|
||||||
|
lines = raw.split(b"\n")
|
||||||
|
if not whole:
|
||||||
|
lines = lines[:-1] # the last line is cut by the bound
|
||||||
|
for line in lines:
|
||||||
|
if b'"user"' not in line or b"toolUseResult" in line:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
d = json.loads(line)
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
if isinstance(d, dict) and d.get("type") == "user":
|
||||||
|
text = harvest.typed_text(d)[0]
|
||||||
|
if text is not None:
|
||||||
|
return True, text
|
||||||
|
return False, ""
|
||||||
|
|
||||||
|
|
||||||
|
def is_worker_session(payload, session_record, meter_record):
|
||||||
|
"""True for a worker or subagent session. A decision made from the transcript is written into
|
||||||
|
`meter_record` (key "worker"); the caller saves it. Without a readable first message nothing is stored."""
|
||||||
|
payload = payload if isinstance(payload, dict) else {}
|
||||||
|
sess = session_record if isinstance(session_record, dict) else {}
|
||||||
|
if store.is_subagent_payload(payload) or sess.get("subagent"):
|
||||||
|
return True
|
||||||
|
if not isinstance(meter_record, dict):
|
||||||
|
return False
|
||||||
|
if "worker" in meter_record:
|
||||||
|
return bool(meter_record["worker"])
|
||||||
|
tp = payload.get("transcript_path") or sess.get("transcript_path")
|
||||||
|
if not isinstance(tp, str) or not tp:
|
||||||
|
return False
|
||||||
|
found, text = first_user_text(tp)
|
||||||
|
if not found:
|
||||||
|
return False
|
||||||
|
meter_record["worker"] = opens_as_brief(text)
|
||||||
|
meter_record["worker_from"] = "transcript"
|
||||||
|
return bool(meter_record["worker"])
|
||||||
|
|
||||||
|
|
||||||
|
def session_is_worker(payload, sid, cwd=None):
|
||||||
|
"""The same decision for a caller holding no records: reads them, and keeps a new decision in the session's
|
||||||
|
meter record so the transcript is read once."""
|
||||||
|
try:
|
||||||
|
from . import orchestration
|
||||||
|
sid = sid or "cwd-" + (cwd or os.getcwd())
|
||||||
|
sess = store.load_session(sid)
|
||||||
|
m = orchestration._peek(sid)
|
||||||
|
had = "worker" in m
|
||||||
|
found = is_worker_session(payload, sess, m)
|
||||||
|
if not had and "worker" in m:
|
||||||
|
with orchestration.Meter(sid) as real:
|
||||||
|
real.setdefault("worker", m["worker"])
|
||||||
|
real.setdefault("worker_from", m.get("worker_from"))
|
||||||
|
found = bool(real["worker"])
|
||||||
|
return found
|
||||||
|
except Exception as e:
|
||||||
|
store.log_error("workerid", e)
|
||||||
|
return False
|
||||||
Reference in new issue
Block a user