irregular6612 commited on
Commit
7e5166c
·
1 Parent(s): 9bd40a5

feat(agents): extract_self_belief parser for find-your-body SELF: reports

Browse files
proteus/game/agents/parsing.py CHANGED
@@ -16,6 +16,7 @@ import re
16
 
17
  _ACTION_FIELD = re.compile(r"ACTION\s*:\s*([^\n\r]+)", re.IGNORECASE)
18
  _WORD = re.compile(r"[a-zA-Z]+")
 
19
 
20
 
21
  def extract_action(text: str, valid: list[str]) -> str | None:
@@ -47,3 +48,26 @@ def extract_action(text: str, valid: list[str]) -> str | None:
47
  return w
48
 
49
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  _ACTION_FIELD = re.compile(r"ACTION\s*:\s*([^\n\r]+)", re.IGNORECASE)
18
  _WORD = re.compile(r"[a-zA-Z]+")
19
+ _SELF_FIELD = re.compile(r"SELF\s*:\s*(\d+)", re.IGNORECASE)
20
 
21
 
22
  def extract_action(text: str, valid: list[str]) -> str | None:
 
48
  return w
49
 
50
  return None
51
+
52
+
53
+ def extract_self_belief(text: str, n_candidates: int) -> int | None:
54
+ """Return the last in-range ``SELF: <i>`` index in *text*, else ``None``.
55
+
56
+ The errand_runner find-your-body discovery lets the model append
57
+ ``SELF: <0..n_candidates-1>`` to report which identical candidate it
58
+ believes is its own body. The last such report wins (the model may revise
59
+ mid-reasoning). Out-of-range or absent reports yield ``None``. This does not
60
+ affect movement parsing (:func:`extract_action`).
61
+
62
+ Args:
63
+ text: Raw LLM output.
64
+ n_candidates: Number of candidate bodies (valid indices ``0..n-1``).
65
+
66
+ Returns:
67
+ The believed self-index, or ``None``.
68
+ """
69
+ for raw in reversed(_SELF_FIELD.findall(text)):
70
+ i = int(raw)
71
+ if 0 <= i < n_candidates:
72
+ return i
73
+ return None
tests/agents/test_self_belief.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tests/agents/test_self_belief.py
2
+ """extract_self_belief parses the optional `SELF: <i>` body-identity report.
3
+
4
+ Used by errand_runner find-your-body discovery; movement parsing is untouched.
5
+ """
6
+ from proteus.game.agents.parsing import extract_self_belief
7
+
8
+
9
+ def test_parses_valid_index():
10
+ assert extract_self_belief("I think SELF: 2\nACTION: up", 3) == 2
11
+
12
+
13
+ def test_last_self_wins():
14
+ assert extract_self_belief("SELF: 0 ... on reflection SELF: 1", 3) == 1
15
+
16
+
17
+ def test_out_of_range_rejected():
18
+ assert extract_self_belief("SELF: 5", 3) is None
19
+
20
+
21
+ def test_absent_returns_none():
22
+ assert extract_self_belief("ACTION: up", 3) is None
23
+
24
+
25
+ def test_case_insensitive_and_spacing():
26
+ assert extract_self_belief("self : 0", 3) == 0