Full port of the Store (17 tables, FTS5, per-profile slices, sessions, turn state, bots, settings, MCP registry), DebugLog, SETTINGS_CATALOG and boot_settings, ShellSession (as ShellContext), Conversation persistence, Repl/Boot, and the PlexerController family with FlowValidator/PlexerFlow. Verified by replaying 643 operations through tai.py and the C++ code (results, tables, FTS, schema, files identical) and by restarting the REPL under per-pane shells and checking the vault. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
275 lines
18 KiB
Python
275 lines
18 KiB
Python
"""Input tables for tests/foundation_parity.py: pure-function cases, flow specs
|
||
and a fake /proc tree. Each case is [op, positional args]; both
|
||
implementations run the same list and the driver diffs the outputs."""
|
||
import os
|
||
|
||
|
||
def pure_cases():
|
||
cases = []
|
||
add = cases.append
|
||
for name in ("DEFAULT_SYSTEM", "MERGE_SYSTEM", "COMPACT_SYSTEM", "AUTONOMOUS_NOTE", "HELP_TEXT", "LEGACY_PASSWORD_NOTE", "VAULT_MEMORY_NOTE"):
|
||
add(["prompt", [name]])
|
||
add(["capability_block", []])
|
||
add(["capability_block", [True, False]])
|
||
add(["capability_block", [False, True]])
|
||
add(["orientation_block", ["/tmp"]])
|
||
add(["orientation_block", ["/tmp", True]])
|
||
add(["orientation_block", [""]])
|
||
add(["settings_catalog", []])
|
||
nested = {"b": 1, "a": [1, 2.5, "ü😀", None, True, {"x": "\n\t\"\\"}], "z": {"y": {"q": []}}}
|
||
for value in (nested, 1e16, 1e-5, 0.1, 1.0, 123456789012345678, -0.0, "\x7f\x1f\x00", [], {}, 1e22, 12345678.9, [1e15, 1e-4]):
|
||
add(["json_dumps", [value]])
|
||
for value in (0.1, 1e16, 1e-5, 123456.789, 1.5e300, 5e-324, 2.5, 100.0, 1e15, 1.2345678901234567e16, 0.0001, 0.00001234, 3.0, 1e100, 123456789.123456789):
|
||
add(["float_repr", [value]])
|
||
for text in ('{"a":1}', '[1,2,3]', ' {"k" : "\\u00e9\\ud83d\\ude00"} ', '{"a":', '[1,]', 'NaN', '{"a":1} trailing', '"\\x"', '01', '1.', '-',
|
||
'{"a":1,"a":2}', '[1e2, -0, 2E-2, 1.5e+3]', '"\\ud83d"', 'tru', '{"a" 1}', '[[[[1]]]]'):
|
||
add(["json_roundtrip", [text]])
|
||
for raw in ("Hello World!", " --x-- ", "ÅÄÖ test", "a" * 40, "İstanbul", "Kelvin K", "", "x" * 31 + "-yz", "a--b__c", "Ünï-cödé", "123 456", "---"):
|
||
add(["normalize_tag", [raw]])
|
||
add(["normalize_tags", [["A b", "a-b", "", "C", "c", "d"] + ["t%d" % n for n in range(30)]]])
|
||
for word in ("news", "cats", "boxes", "ladies", "class", "bus", "analysis", "photos", "is", "ab", "classes", "churches", "dishes", "buzzes", "series", "dogs", "ss", "gas", "physics", "mice"):
|
||
add(["singular_noun", [word]])
|
||
add(["tag_variants", [word]])
|
||
for raw in ("mem:0123456789abcdef", "mem:0123456789abcdeg", "mem:0123456789ABCDEF", "secret:api_key", "secret:bad key", "secret:", "sched:12", "sched:1a",
|
||
" mem:0123456789abcdef \n", "other:1", "secret:a\n", "secret:a.b-c_d", "sched:", "mem:abc", ""):
|
||
add(["parse_node_id", [raw]])
|
||
for q in ("Hello World", "", "!!!", "x", "ÄÖ ünïcode 123", "a-b c_d", "KELVIN K", "İ"):
|
||
add(["fts_match_variants", [q]])
|
||
add(["fts_rank", ["fts_docs"]])
|
||
for ident in ("a", "work-1", "-bad", "a" * 32, "a" * 33, "ok\n", "", "ü", "A_b-c", "_a", "ok\n\n"):
|
||
add(["profile_re", [ident]])
|
||
add(["bot_re", [ident]])
|
||
add(["secret_re", [ident]])
|
||
add(["mcp_server_re", [ident]])
|
||
add(["split_models", ["a, b\nc,,d "]])
|
||
add(["split_models", [""]])
|
||
add(["split_models", [" , "]])
|
||
add(["split_models", ["x;y, z"]])
|
||
add(["parse_backends", ["lab|http://x/|key|m|opencode;l2|b|$HOME|;bad|x;\nl3|b|k|m|zzz;|b|k;l4||k; l5 | http://y// | k | m | OpenCode "]])
|
||
add(["parse_backends", [""]])
|
||
nsv = [("model", "x"), ("model", ""), ("model", None), ("model", 5), ("backends", None), ("backends", "a|b|c"), ("retry_seconds", 2.5), ("retry_seconds", "3"),
|
||
("retry_seconds", "1_0"), ("retry_seconds", " 4.5 "), ("retry_seconds", "inf"), ("retry_seconds", "nan"), ("retry_seconds", "x"), ("retry_seconds", True),
|
||
("retry_seconds", -0.5), ("retry_seconds", 0), ("retry_seconds", [1]), ("retry_seconds", 1e20), ("retry_seconds", ".5"), ("retry_seconds", "5."),
|
||
("zen_enabled", True), ("zen_enabled", False), ("zen_enabled", 1), ("zen_enabled", 0), ("zen_enabled", 1.0), ("zen_enabled", 2), ("zen_enabled", "ON"),
|
||
("zen_enabled", " no "), ("zen_enabled", "maybe"), ("zen_enabled", None), ("zen_enabled", []), ("theme", "dark"), ("theme", "neon"), ("theme", 3),
|
||
("gemini_plan", "plan"), ("muse_bin", None), ("muse_bin", "/x"), ("muse_bin", ""), ("voice", ""), ("devplace_base", "http://x/")]
|
||
for key, value in nsv:
|
||
add(["normalize_setting_value", [key, value]])
|
||
pse = [("model", "x"), ("theme", "nord"), ("theme", "bad"), ("zen_enabled", "On"), ("zen_enabled", "junk"), ("zen_enabled", " 1 "), ("gemini_plan", "plan"),
|
||
("gemini_plan", "Plan"), ("gemini_plan", "1"), ("codex_sandbox", "read-only"), ("codex_sandbox", "no"), ("box_caps", "0"), ("box_caps", "false"),
|
||
("box_caps", "1"), ("box_caps", "junk"), ("retry_seconds", "2.5"), ("retry_seconds", "abc"), ("retry_seconds", "12"), ("retry_seconds", "1e3"),
|
||
("retry_seconds", "5."), ("retry_seconds", "007"), ("openrouter_enabled", "off"), ("opencode_enabled", "")]
|
||
for key, raw in pse:
|
||
add(["parse_setting_env", [key, raw]])
|
||
for key, raw in (("retry_seconds", "2.5"), ("zen_enabled", "1"), ("zen_enabled", "0"), ("model", "m"), ("retry_seconds", "0.0")):
|
||
add(["parse_setting_stored", [key, raw]])
|
||
lines = "".join("line %d é\n" % n for n in range(130))
|
||
for text, extra in ((lines, []), (lines, [10, 2, 100]), ("", []), ("one", []), ("a\r\nb\rc\x0cd\x0be\x1cf
g\u0085h\n\n\ni", []),
|
||
("x" * 5000 + "\n" + "y" * 10, [60, 3, 4000]), (" \n lead\n\n", [2, 1, 50]), ("a\nb\nc\nd\ne\nf\n", [2, 1, 4000])):
|
||
add(["chunk_lines", [text] + extra])
|
||
for text, extra in (("short", []), ("x" * 2100, []), ("é" * 2100, []), ("y" * 2051, []), ("z" * 2050, []), ("abcdefghij" * 10, [10, 10])):
|
||
add(["truncate", [text] + extra])
|
||
add(["sanitize_sid", ["a b/c!d.e-f_g" + "x" * 70]])
|
||
add(["sanitize_sid", [""]])
|
||
return cases
|
||
|
||
|
||
def flow_cases():
|
||
cases = []
|
||
add = cases.append
|
||
base = {"title": "T", "screens": [
|
||
{"id": "s1", "title": "S1", "body": "hello", "fields": [
|
||
{"id": "c", "type": "choice", "label": "Pick", "options": ["a", {"label": "B", "value": 2}], "default": 1},
|
||
{"id": "t", "type": "text", "label": "Name", "initial": "x", "show_if": {"c": 2}},
|
||
{"id": "i", "type": "info", "text": "note"}],
|
||
"actions": [{"id": "ok", "label": "OK"}, {"id": "no", "label": "No"}], "next": {"ok": "s2", "no": "$cancel"}},
|
||
{"id": "s2", "body": "bye"}]}
|
||
bad = [
|
||
None, [], {}, {"v": 2, "screens": []}, {"screens": []}, {"screens": [{}]}, {"screens": [{"id": "a b", "body": "x"}]}, {"screens": [{"id": "a"}]},
|
||
{"screens": [{"id": "a", "body": "x"}, {"id": "a", "body": "y"}]}, {"timeout": 1, "screens": [{"id": "a", "body": "x"}]},
|
||
{"timeout": "9", "screens": [{"id": "a", "body": "x"}]}, {"timeout": "z", "screens": [{"id": "a", "body": "x"}]},
|
||
{"screens": [{"id": "a", "body": "x", "fields": [{"id": "f", "type": "bogus"}]}]},
|
||
{"screens": [{"id": "a", "body": "x", "fields": [{"id": "f", "type": "choice", "options": []}]}]},
|
||
{"screens": [{"id": "a", "body": "x", "fields": [{"id": "f", "type": "choice", "options": ["a"], "default": 3}]}]},
|
||
{"screens": [{"id": "a", "body": "x", "fields": [{"id": "f", "type": "choice", "options": [{"label": " "}]}]}]},
|
||
{"screens": [{"id": "a", "body": "x", "fields": [{"id": "f", "type": "choice", "options": [{"label": "l", "value": [1]}]}]}]},
|
||
{"screens": [{"id": "a", "body": "x", "show_if": {"nope": 1}}]},
|
||
{"screens": [{"id": "a", "body": "x", "next": "zzz"}]},
|
||
{"screens": [{"id": "a", "body": "x", "next": {"k": "a"}}]},
|
||
{"screens": [{"id": "a", "body": "x", "actions": [{"id": "k", "label": "K"}], "next": {"z": "a"}}]},
|
||
{"screens": [{"id": "a", "body": "x", "actions": [{"id": "k", "label": " "}]}]},
|
||
{"screens": [{"id": "a", "body": "x", "actions": [{"id": "k", "label": "K"}, {"id": "k", "label": "K"}]}]},
|
||
{"screens": [{"id": "a", "body": "x", "next": 5}]},
|
||
{"screens": [{"id": "a", "body": "x"}], "start": "q"},
|
||
{"title": 5, "screens": [{"id": "a", "body": "x"}]},
|
||
{"screens": [{"id": "a", "title": 5, "body": "x"}]},
|
||
{"screens": [{"id": "a", "body": "x" * 2001}]},
|
||
{"screens": [{"id": "a", "body": "x", "fields": "z"}]},
|
||
{"screens": [{"id": "a", "body": "x", "actions": 7}]},
|
||
{"screens": [{"id": "a", "body": "x", "fields": [{"id": "f", "type": "text", "show_if": {"f": 1}}, {"id": "g", "type": "text", "show_if": {}}]}]},
|
||
{"screens": ["str"]},
|
||
{"screens": [{"id": "a", "body": "x", "fields": [5]}]},
|
||
{"screens": [{"id": "a", "body": "x", "actions": [5]}]},
|
||
{"screens": [{"id": 5, "body": "x"}]},
|
||
{"screens": [{"id": "a" * 70, "body": "x"}]},
|
||
]
|
||
add(["validate_flow_spec", [base]])
|
||
for spec in bad:
|
||
add(["validate_flow_spec", [spec]])
|
||
ok = {"status": "ok"}
|
||
add(["run_flow", [base, [dict(ok, value=1), {"status": "ok", "value": "Zed"}, dict(ok, value=0), ok]]])
|
||
add(["run_flow", [base, [dict(ok, value=0), dict(ok, value=1)]]])
|
||
add(["run_flow", [base, [dict(ok, value=0), dict(ok, value=1), ok]]])
|
||
add(["run_flow", [base, [{"status": "cancelled"}]]])
|
||
add(["run_flow", [base, [dict(ok, value=0), {"status": "timeout"}]]])
|
||
add(["run_flow", [{"screens": [{"id": "a", "body": "x", "next": "a"}]}, [ok] * 80]])
|
||
add(["run_flow", [{"screens": [{"id": "a", "body": "x"}, {"id": "b", "body": "y", "show_if": {}}]}, [ok, ok]]])
|
||
return cases
|
||
|
||
|
||
def proc_cases(proc):
|
||
"""ShellSession.current against the fake /proc: nested shells, a shell-less chain, bad pins."""
|
||
cases = []
|
||
for env, pid in (({"TAI_SESSION": ""}, 200), ({"TAI_SESSION": "pinned"}, 200), ({"TAI_SESSION": "20260101-000000-p100-fa6db223"}, 200),
|
||
({"TAI_SESSION": "20260101-000000-p999-fa6db223"}, 200), ({"TAI_SESSION": "bad id!/x"}, 200), ({}, 100), ({}, 300),
|
||
({"TAI_SESSION": "x-p9-"}, 300), ({}, 400), ({}, 999999)):
|
||
cases.append(["shell_current", [env, pid, proc, 1761400000.5, 100]])
|
||
for variant in (proc + "_noboot", proc + "_nobtime", proc + "_bare"):
|
||
for pid in (200, 100, 300):
|
||
cases.append(["shell_current", [{}, pid, variant, 1761400000.5, 100]])
|
||
cases.append(["shell_current", [{}, 200, proc, 1761400000.5, 250]])
|
||
cases.append(["shell_current", [{}, 200, proc, 1761400000.25, 0 + 100]])
|
||
return cases
|
||
|
||
|
||
def build_fake_proc(root, boot_id=True, btime=True):
|
||
"""A /proc with init(1) > zsh(50) > bash(100) > tai(200), plus a lone tai(300) under init and an oddly named process."""
|
||
os.makedirs(os.path.join(root, "sys", "kernel", "random"))
|
||
if btime:
|
||
with open(os.path.join(root, "stat"), "w") as handle:
|
||
handle.write("cpu 1 2 3\nbtime 1700000000\nprocesses 5\n")
|
||
if boot_id:
|
||
with open(os.path.join(root, "sys", "kernel", "random", "boot_id"), "w") as handle:
|
||
handle.write("FA6DB223-1111-2222-3333-444444444444\n")
|
||
|
||
def make(pid, comm, ppid, ticks, cmdline):
|
||
os.makedirs(os.path.join(root, str(pid)))
|
||
tail = "S %d 1 1 0 -1 0 0 0 0 0 0 0 0 0 20 0 1 0 %d 1000 100" % (ppid, ticks)
|
||
with open(os.path.join(root, str(pid), "stat"), "w") as handle:
|
||
handle.write("%d (%s) %s\n" % (pid, comm, tail))
|
||
with open(os.path.join(root, str(pid), "cmdline"), "wb") as handle:
|
||
handle.write(cmdline.replace(" ", "\0").encode() + b"\0")
|
||
make(1, "init", 0, 10, "/sbin/init")
|
||
make(50, "zsh", 1, 5000, "-zsh")
|
||
make(100, "bash", 50, 123456, "/usr/bin/bash --login")
|
||
make(200, "tai", 100, 123999, "/usr/bin/python3 tai.py")
|
||
make(300, "tai", 1, 700, "/opt/tai")
|
||
make(400, "my (odd) name", 100, 124000, "weird")
|
||
|
||
|
||
def debug_cases():
|
||
"""DebugLog: sentence and json sinks, redaction, flattening, quoting, meta caps."""
|
||
long_text = "word " * 2000
|
||
events = [
|
||
["INFO", "app", "boot", "tai booted", {"profile": "default", "meta": {"version": "1", "home": "/x y", "mode": "repl"}}],
|
||
["DEBUG", "vault", "secret_use", "token is topsecret-9999 and more", {"profile": "default", "bot": "main", "chat": "c 1", "meta": {"name": "dbg_secret", "val": "topsecret-9999"}}],
|
||
["WARNING", "net", "slow", "multi\nline\t message with spaces", {"meta": {"n": 3, "f": 1.5, "b": True, "none": None, "list": ["a", "b c", 1], "nested": {"k": {"k2": {"k3": {"k4": {"k5": "deep"}}}}}}}],
|
||
["ERROR", "app", "big", "long meta", {"profile": "p", "meta": {"text": long_text, "other": "ü" * 5000, "q": 'say "hi" \\ there', "empty": ""}}],
|
||
["INFO", "app", "huge", "x" * 3000, {"meta": {"a%d" % n: "y" * 400 for n in range(60)}}],
|
||
["INFO", "app", "plain", "no meta", {}],
|
||
]
|
||
return [
|
||
["save_secret", ["dbg_secret", "topsecret-9999"]],
|
||
["debug_log", ["file", "sentence", events]],
|
||
["debug_log", ["file", "json", events]],
|
||
["log_query", [{}]],
|
||
["log_query", [{"level": "ERROR"}]],
|
||
["log_query", [{"component": "vault", "event": "secret_use"}]],
|
||
["log_query", [{"limit": 2}]],
|
||
]
|
||
|
||
|
||
def prune_cases():
|
||
"""More scoped session and turn files than FILES_CAP: the oldest are removed on save, the legacy file never."""
|
||
cases = [["save_session", ["prune", [{"role": "user", "content": "legacy"}]]], ["save_turn_state", ["prune", "main", {"open": False}]]]
|
||
for n in range(24):
|
||
cases.append(["save_session", ["prune", [{"role": "user", "content": "m%d" % n}], "main", "ctx%02d" % n]])
|
||
cases.append(["save_turn_state", ["prune", "main", {"open": False, "n": n}, "ctx%02d" % n]])
|
||
cases.append(["save_session", ["prune", [{"role": "user", "content": "b%d" % n}], "bot1", "ctx%02d" % n]])
|
||
cases.append(["sleep", [0.01]])
|
||
return cases
|
||
|
||
|
||
def readback_cases(session):
|
||
"""Read-only operations replayed by BOTH implementations on BOTH vaults (a vault is portable between them)."""
|
||
return [
|
||
["get_all_settings", []], ["get_all_settings", [], {"scope": "work"}], ["list_bots", ["default"]], ["load_session", ["default", "main", session]],
|
||
["load_session", ["default", "helper", session]], ["load_system", ["default"]], ["load_system", ["work"]], ["load_bot_system", ["default", "helper"]],
|
||
["load_turn_state", ["default", "main", session]], ["search_records", ["fox"]], ["search_records", ["fox", None, [], 8, "work"]], ["list_schedules", []],
|
||
["history_load", ["default"]], ["list_profiles", []], ["mcp_list_servers", []], ["audit_history", []], ["list_secret_infos", []], ["known_tags", []],
|
||
["tag_counts", []], ["log_query", [{}]], ["fts_search", ["fox"]], ["search_events", ["default", "fox"]], ["load_dir_defaults", ["/tmp/dd"]],
|
||
["resolve_bot", ["default", "h"]], ["load_bots", ["default"]], ["file_record_id", ["/tmp/x.py"]],
|
||
]
|
||
|
||
|
||
def style_cases():
|
||
"""Glow banner pieces: status bar layouts at several widths, ANSI helpers."""
|
||
cases = [["glow_prompt", []], ["glow_title", ["1.2.3", "work"]], ["glow_title", ["v", "ünï"]]]
|
||
for width in (200, 80, 40, 30, 20, 12, 5, 1):
|
||
for yolo, agents in ((False, 0), (True, 0), (False, 3), (True, 2)):
|
||
cases.append(["statusbar", ["1.44.2", "default", "/tmp/some/long/directory/name/here", yolo, agents, width]])
|
||
cases.append(["statusbar", ["1", "p", "@@HOME@@", False, 0, 80]])
|
||
cases.append(["statusbar", ["1", "p", "@@HOME@@/sub/dir", False, 0, 80]])
|
||
sample = "\033[1m\033[38;5;111mhello\033[0m \033[2mwörld\033[0m tail"
|
||
for text in (sample, "plain", "", "\033[31m\033[0m", "a\033[1mb\033[0mc\033[2m"):
|
||
cases.append(["strip_ansi", [text]])
|
||
for width in (0, 1, 3, 5, 8, 100):
|
||
cases.append(["truncate_ansi", [text, width]])
|
||
return cases
|
||
|
||
|
||
def plexer_cases():
|
||
"""NativePlexerController against the scripted backend ($SOCK) and TmuxPlexerController against a private tmux server."""
|
||
choice = {"title": "t", "text": "pick", "kind": "choice", "options": ["a", "b"], "default": 0}
|
||
cases = [
|
||
["native_ctl", ["$SOCK", "7", "status", []]],
|
||
["native_ctl", ["$SOCK", "", "status", []]],
|
||
["native_ctl", ["$SOCK", "7", "send_keys", ["ls -l", True]]],
|
||
["native_ctl", ["$SOCK", "7", "send_keys", ["éé", False]]],
|
||
["native_ctl", ["$SOCK", "7", "read_pane", [50]]],
|
||
["native_ctl", ["$SOCK", "7", "type_text", ["héy", True]]],
|
||
["native_ctl", ["$SOCK", "7", "ask", [choice, 5]]],
|
||
["native_ctl", ["$SOCK", "7", "ask", [{"title": "t2", "text": "name?", "kind": "text", "initial": ""}, 5]]],
|
||
["native_ctl", ["$SOCK", "7", "ask", [dict(choice, title="cancel"), 5]]],
|
||
["native_ctl", ["$SOCK", "7", "ask", [dict(choice, title="timeout"), 5]]],
|
||
["native_ctl", ["$SOCK", "7", "ask", [dict(choice, title="weird"), 5]]],
|
||
["native_ctl", ["$SOCK", "7", "ask", [dict(choice, title="never"), 1]]],
|
||
["native_ctl", ["/nonexistent/sock", "7", "status", []]],
|
||
["native_ctl", ["$SOCK", "7", "call", [{"cmd": "refuse"}]]],
|
||
["native_ctl", ["$SOCK", "7", "call", [{"cmd": "boom"}]]],
|
||
["native_ctl", ["$SOCK", "7", "call", [{"cmd": "garbage"}]]],
|
||
["native_ctl", ["$SOCK", "7", "call", [{"cmd": "nope"}]]],
|
||
["tmux_ctl", ["%0", "status", []]],
|
||
["tmux_ctl", ["%0", "send_keys", ["hello world", True]]],
|
||
["sleep", [0.5]],
|
||
["tmux_ctl", ["%0", "read_pane", [5]]],
|
||
["tmux_ctl", ["%0", "type_text", ["abc", True]]],
|
||
["sleep", [0.5]],
|
||
["tmux_ctl", ["%0", "read_pane", [8]]],
|
||
["tmux_ctl", ["%99", "status", []]],
|
||
["tmux_ctl", ["%99", "send_keys", ["x", False]]],
|
||
["tmux_ctl", ["%99", "read_pane", [5]]],
|
||
["tmux_ctl", ["%0", "ask", [choice, 2]]],
|
||
["tmux_ctl", ["%0", "ask", [{"title": "p", "text": "q", "kind": "text", "initial": ""}, 2]]],
|
||
["tmux_ctl", ["%0", "ask", [{"title": "p", "text": "q", "kind": "message"}, 2]]],
|
||
["tmux_ctl", ["%0", "ask", [{"title": "p", "kind": "bogus"}, 2]]],
|
||
]
|
||
return cases
|
||
|
||
|
||
def repl_cases():
|
||
"""REPL slash commands that the session layer owns, run on a shared store."""
|
||
script = ["/profiles", "/bots", "/bot", "/bot nobody", "/bot helper", "/bots", "/bot H", "/bot main", "/profile", "/profile bad name", "/profile -x",
|
||
"/profile work", "/profiles", "/profile default", "/clear", "/ctx", "/help", "/zzz", "/", "/PROFILES", "/bots extra args", "/quit"]
|
||
return [["repl_script", [script]]]
|