File size: 7,211 Bytes
3436bdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from __future__ import annotations

import json
import subprocess
from pathlib import Path
from typing import Any

import gradio as gr

ROOT = Path(__file__).resolve().parent


def run_command(args: list[str], timeout: int = 120) -> tuple[bool, str]:
    try:
        completed = subprocess.run(
            args,
            cwd=ROOT,
            capture_output=True,
            text=True,
            timeout=timeout,
            check=False,
        )
    except Exception as exc:  # pragma: no cover - surfaced in UI
        return False, f"{type(exc).__name__}: {exc}"

    if completed.returncode != 0:
        stderr = completed.stderr.strip()
        stdout = completed.stdout.strip()
        detail = stderr or stdout or f"command exited with {completed.returncode}"
        return False, detail
    return True, completed.stdout


def run_json_command(args: list[str], timeout: int = 120) -> tuple[dict[str, Any] | None, str | None]:
    ok, output = run_command(args, timeout=timeout)
    if not ok:
        return None, output
    try:
        return json.loads(output), None
    except json.JSONDecodeError as exc:
        return None, f"JSONDecodeError: {exc}"


def summarize_overview() -> tuple[str, dict[str, Any], dict[str, Any], dict[str, Any], dict[str, Any]]:
    context, context_err = run_json_command(["./bin/bvtctl", "context"])
    runtime, runtime_err = run_json_command(["./bin/bvtctl", "runtime"])
    memory, memory_err = run_json_command(["./bin/bvtctl", "memory-readiness"])
    pm_next, pm_err = run_json_command(["./bin/bvtctl", "pm-next"])

    if context_err or runtime_err or memory_err or pm_err:
        errors = [
            item
            for item in [
                ("context", context_err),
                ("runtime", runtime_err),
                ("memory-readiness", memory_err),
                ("pm-next", pm_err),
            ]
            if item[1]
        ]
        error_lines = "\n".join(f"- `{name}`: {message}" for name, message in errors)
        return (
            "### Operator desk unavailable\n"
            "The deterministic control surfaces did not all load.\n\n"
            f"{error_lines}",
            context or {},
            runtime or {},
            memory or {},
            pm_next or {},
        )

    current_position = context["current_position"]
    latest_runtime = context.get("latest_runtime_state", {})
    benchmark = pm_next.get("context", {}).get("benchmark", {})
    candidate = pm_next.get("action", {}).get("recommended_candidate", {})

    overview = f"""### Product one-liner
One cloud operator desk over the repo’s deterministic control surfaces.

### Why this matters
This keeps the product honest in the cloud: graph-backed reads, policy, memory readiness, and PM routing work without depending on a local Codex desktop session.

| Surface | Live read | Why in plain English |
| --- | --- | --- |
| Entry slice | `{current_position.get("slice_id", "unknown")}` | one front door stays intact |
| Runtime profile | `{current_position.get("default_profile", "unknown")}` | the cloud app shows the same control posture |
| Latest manifest | `{latest_runtime.get("latest_manifest_id", "unknown")}` | you can see what last changed the system |
| Latest lane | `{latest_runtime.get("latest_lane", "unknown")}` | tells you whether the system was thinking or acting |
| Memory readiness | {memory.get("one_liner", "unknown")} | exact lookup is visible instead of hidden |
| Next PM move | `{candidate.get("candidate_id", "unknown")}` | product delivery stays bounded |
| Benchmark latency | `{benchmark.get("avg_latency_ms", "unknown")}` ms | shows the operational cost frontier |

### Pareto read
| Option | What you get | What you give up |
| --- | --- | --- |
| This Space desk | free-tier cloud runtime, deterministic reads, bounded control visibility | no full semantic chat lane |
| Local desktop path | full repo behavior with local Codex auth | not cloud-hosted |
| Future HF model backend | real cloud chat lane | extra model/API wiring and cost |
"""
    return overview, context, runtime, memory, pm_next


def ask_turn(user_input: str) -> tuple[str, dict[str, Any]]:
    text = (user_input or "").strip()
    if not text:
        return "Enter a question first.", {}

    ok, output = run_command(["./api/run_turn.sh", text], timeout=180)
    if not ok:
        return (
            "### Turn failed\n"
            f"`api/run_turn.sh` did not complete.\n\n"
            f"Error: `{output}`",
            {},
        )

    response_path = output.strip().splitlines()[-1]
    try:
        payload = json.loads(Path(response_path).read_text(encoding="utf-8"))
    except Exception as exc:  # pragma: no cover - surfaced in UI
        return f"### Turn failed\nCould not read response packet.\n\nError: `{type(exc).__name__}: {exc}`", {}

    answer = f"""### Turn result
**Answer:** {payload.get("answer_text", "")}

| Field | Value |
| --- | --- |
| Lane | `{payload.get("lane", "unknown")}` |
| Frontier | `{payload.get("tensor_surface", {}).get("frontier_read", "unknown")}` |
| Execution gate | `{payload.get("execution_gate", {}).get("allowed", False)}` |
| Decision brief | {payload.get("decision_brief", "")} |

### Why this matters
Cloud mode is deterministic-first. Exact graph/runtime questions work best here; broad semantic questions may fall back to the graph-first holding answer because the local Codex subscription backend is not present inside Hugging Face Spaces.
"""
    return answer, payload


with gr.Blocks(title="Bit/Vector/Tensor Control Policy", fill_height=True) as demo:
    gr.Markdown(
        """
        # Bit/Vector/Tensor Control Policy
        Clean product one-liner: one cloud operator desk over graph state, control policy, and bounded runtime receipts.
        """
    )

    refresh = gr.Button("Refresh Desk", variant="primary")
    overview = gr.Markdown()

    with gr.Tabs():
        with gr.Tab("Operator Desk"):
            context_json = gr.JSON(label="Context")
            runtime_json = gr.JSON(label="Runtime")
            memory_json = gr.JSON(label="Memory Readiness")
            pm_json = gr.JSON(label="PM Next")
        with gr.Tab("Turn Runner"):
            gr.Markdown(
                """
                Ask exact or bounded questions here.

                Good examples:
                - `What is the freshest runtime artifact?`
                - `What continuity surface is active?`
                - `Is there a weekly schedule?`
                """
            )
            user_input = gr.Textbox(label="Question", lines=3, placeholder="Ask the control surface a bounded question")
            ask = gr.Button("Run Turn")
            turn_summary = gr.Markdown()
            turn_json = gr.JSON(label="Turn Packet")

    refresh.click(
        summarize_overview,
        outputs=[overview, context_json, runtime_json, memory_json, pm_json],
    )
    ask.click(
        ask_turn,
        inputs=[user_input],
        outputs=[turn_summary, turn_json],
    )

    demo.load(
        summarize_overview,
        outputs=[overview, context_json, runtime_json, memory_json, pm_json],
    )


if __name__ == "__main__":
    demo.launch()