Compare commits

..

2 commits

Author SHA1 Message Date
380ebd9124 Add research_tracking.py to diagnostics (Phase 1 consolidation)
Argus's research lifecycle tracking module. Was in root diagnostics/
only — missing from both repos. Completes Phase 1 file inventory.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 10:15:27 +02:00
c0cc4ef090 Add vitality modules + upgrade alerting with SQL injection protection
- vitality.py (25K): 10-dimension vitality scoring (Ship + Argus, Leo-approved)
- vitality_routes.py (10K): Dashboard routes for vitality endpoints
- alerting.py: Updated with _ALLOWED_DIM_EXPRS SQL injection protection, stricter dim_expr validation
- alerting_routes.py: Added proper try/finally/conn.close, ValueError catch on hours param
- Diff log documenting multi-copy resolution decisions

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 10:12:53 +02:00
612 changed files with 4586 additions and 142831 deletions

View file

@ -1,40 +0,0 @@
---
name: crabbox
description: Use for remote Linux verification, isolated CI proof, Crabbox lease cleanup, and PR evidence without production deploys or secret forwarding.
---
# Crabbox
Use Crabbox for remote Linux verification and PR proof only.
Allowed jobs:
- `crabbox job run unit`
- `crabbox job run lint-phase1b`
- `crabbox job run ci-contract`
- `crabbox job run phase1b-local-proof`
- `crabbox job run sync-smoke`
Default workflow:
1. Run `crabbox job run --dry-run ci-contract`.
2. Run `crabbox job run --dry-run phase1b-local-proof`.
3. Inspect the planned commands and confirm no production secrets or production deploy commands appear.
4. Run `crabbox job run ci-contract`.
5. Run `crabbox job run phase1b-local-proof`.
6. Save the run id, lease id, stdout, downloaded proof JSON, and JUnit output.
7. Stop the lease unless the CLI has already stopped it.
Boundaries:
- Do not run production deploy commands from Crabbox.
- Do not forward production GitHub, Forgejo, OpenRouter, SSH, Bitwarden, or VPS secrets.
- Do not target the production `decision-engine` repo for sandbox proof.
- Do not mutate the production VPS.
- Do not call Crabbox proof equivalent to production proof unless the lease recreates `/opt/teleo-eval`, systemd services, runtime users, DB paths, timers, and deploy scripts.
Failure handling:
- If sync sanity fails, stop the lease and retry on a fresh lease.
- If a proof script fails, save the full run output and do not summarize it as a pass.
- If a remote box has unknown state, stop it instead of debugging against reused state.

View file

@ -1,41 +0,0 @@
---
name: decision-engine-refinement
description: Use when improving Living IP decision-engine quality, LLM model selection, evaluator prompts, rubrics, replay evals, Rio or Theseus reviewer behavior, or model bakeoffs.
---
# Decision Engine Refinement
Use this skill for quality work, not infrastructure work. Pentagon.run or Crabbox can run remote jobs; this repo owns model judgment, rubric design, prompt/tool refinement, and proof artifacts.
## Workflow
1. Read `docs/llm-refinement-decision-engine.md`.
2. Identify the lane: Rio economics, Theseus model integrity, Leo cross-domain, domain factuality, retrieval quality, or prompt/tool self-upgrade.
3. Build or reuse a replayable fixture before changing prompts or model assignments.
4. Compare baseline vs candidate with the same input, same rubric, and structured verdict format.
5. Record false approves, false rejects, useful disagreements, cost, and latency.
6. Change runtime prompts/models only after the candidate shows a measured improvement with no critical regression.
## Hard Rules
- Do not change live model assignments because one answer sounds better.
- Do not use production DB writes to tune prompts.
- Do not collapse Rio and Theseus into generic "reviewers".
- Do not treat payment, popularity, or engagement as quality approval.
- Do not claim production decision-engine improvement without replay evidence and live/staging readback.
## Agent Responsibilities
- Rio: incentive design, contribution weights, paid-query effects, market/mechanism reasoning, OPSEC, correlated-prior warnings.
- Theseus: model diversity, adversarial evals, disagreement queues, self-upgrade criteria, prompt/tool safety, verifier drift.
- Leo: cross-domain synthesis, fallback review, final arbitration where the route or rubric is ambiguous.
## Expected Artifacts
- fixture file or DB query used for sampling;
- baseline verdict output;
- candidate verdict output;
- summary JSON with quality, cost, latency, and disagreement metrics;
- patch scoped to prompts, model config, rubric docs, or eval harness.
Run `python3 scripts/check_llm_refinement_contract.py` after editing this surface.

View file

@ -1,83 +0,0 @@
---
name: leo-telegram-canary-ops
description: Use for live Leo Telegram tests through authenticated Chrome/Computer Use, including memory, KB audit, staged DB writes, screenshots, and clear separation from external outreach.
---
# Leo Telegram Canary Ops
## Job
Run Telegram-visible Leo canaries when testing Leo itself, with retained screenshots and DB readbacks.
## Trigger Phrases
- "test Leo in Telegram"
- "send the Leo group message"
- "Telegram-visible proof"
- "Chrome Telegram canary"
- "Leo remembers conversation"
- "staged write through Telegram"
## Authorization Boundary
Live Telegram bot messages to the Leo group are authorized when they are test canaries for Leo behavior.
This is different from external outreach. Do not send external outreach, partner messages, public announcements, or non-test communications without exact authorization.
## Required Tooling
Use Computer Use through `node_repl` and the Chrome/Computer Use skill path. Do not use:
- AppleScript,
- `osascript`,
- JXA,
- System Events,
- focus hijacking,
- foregrounding hacks.
## Canary Types
Name the canary before sending:
- bot readiness,
- memory,
- KB audit truth,
- proposed-vs-applied truth,
- open-ended m3taversal-style triage,
- staged write,
- no-canonical-mutation proof,
- screenshot/readback proof.
## Message Discipline
Use unique markers such as `WL-LIVE-TG-CORY-YYYYMMDD-Tn`.
Ask Leo for machine-checkable reply markers like:
- `LIVE_TG_TURN1_ACK`
- `LIVE_TG_TURN3_STAGED`
- `LIVE_TG_TURN4_READBACK`
Do not rely only on exact-ID prompts. At least one representative canary should be a vague operator-style prompt such as: m3taversal says the agents are not working, the KB is in the same state as last night, and Leo should be able to manipulate the KB; ask Leo what that means, what it would inspect first, what fixed means, and how it separates proposed, approved, and applied. Keep this canary read-only unless the task explicitly authorizes a staging or apply step.
## Required Proof
After each live Telegram canary:
1. Capture screenshot.
2. Record exact message sent.
3. Record Leo reply marker and substance.
4. Query DB if the canary involves DB state.
5. Record canonical public table counts when proving no mutation.
6. Save a local markdown/json artifact.
7. Sync artifacts to VPS report dir when relevant.
## Current Known Proof
- Live memory and KB audit passed for marker `WL-LIVE-TG-CORY-20260709`.
- Live staged write passed for `WL-LIVE-TG-CORY-20260709-T3`.
- Readback passed for `WL-LIVE-TG-CORY-20260709-T4`.
- Open-ended m3taversal-style read-only triage passed for the legacy marker
`WL-LIVE-TG-CORY-OPEN-20260709`.
Use `docs/reports/leo-working-state-20260709/current-truth-index.md` for artifact paths.

View file

@ -1,93 +0,0 @@
---
name: living-ip-kb-interop
description: Use when giving Hermes, OpenClaw, Claude-style, Pentagon, or other external agents safe read/write access patterns for the Living IP knowledge base.
---
# Living IP KB Interop
Use this skill when an outside agent needs to read from the Living IP knowledge base or propose a write back into it. The default is propose-first, proof-backed, and no-secret.
## Goal
Any Hermes, OpenClaw, Claude-style, or Pentagon agent should be able to:
1. search the knowledge base;
2. read a cited file or record;
3. propose a source, claim, entity, or correction;
4. route the proposal to the right evaluator agents;
5. leave a proof artifact that shows inputs, tools, and no denied actions.
## Read Path
Prefer deterministic local surfaces before asking an LLM:
- repository files under the knowledge base checkout;
- generated claim indexes from `lib/claim_index.py`;
- search helpers in `lib/search.py`;
- copied SQLite state through `teleo-db-operator`;
- retained proof JSON in `.crabbox-results/` or `proof/`.
Read outputs must include file paths, source paths, claim/entity IDs when available, and the exact query used.
## Write Path
All writes are proposals until the normal review/evaluation pipeline accepts them.
Allowed proposal targets:
- source file proposal;
- claim file proposal;
- entity file proposal;
- correction proposal;
- route/evaluator proof artifact.
Required fields:
- source or rationale;
- target domain;
- proposed author/agent;
- route evidence;
- confidence or uncertainty tag;
- citations to existing KB context;
- proof output path.
Do not write directly to main. Do not mutate production `pipeline.db`. Use `teleo-db-operator` for any SQLite write, and only after explicit authorization, backup, transaction, and readback.
## Minimal Tool Contract
Adapters should expose this shape even if their runtime uses different names:
- `kb.search(query, domain?, limit?)`
- `kb.get(path_or_id)`
- `kb.propose_source(markdown, metadata)`
- `kb.propose_claim(markdown, metadata)`
- `kb.propose_entity(markdown, metadata)`
- `kb.route(diff_or_metadata)`
- `kb.proof(path, payload)`
If a runtime cannot implement one of these, record the missing tool as a blocker instead of silently skipping it.
## Denied Actions
- raw Bitwarden export;
- card, token, or password reads;
- production DB writes;
- direct pushes to main;
- public comments or messages;
- hidden Slack, Linear, Telegram, or GitHub sends;
- uncited knowledge writes;
- model-driven edits without route evidence.
## Expected Artifact
Write `.crabbox-results/kb-interop-proof.json` or a caller-specified proof path containing:
- runtime name;
- model/provider if known;
- tools invoked;
- denied tools not invoked;
- query or input fixture;
- cited reads;
- proposed writes;
- route evidence;
- verifier result.

View file

@ -1,70 +0,0 @@
---
name: nousresearch-hermes-agent
description: Use when packaging Living IP agents, skills, prompts, memory, model routing, or decision-engine workflows for NousResearch Hermes Agent.
---
# NousResearch Hermes Agent
Use this skill to adapt Living IP decision-engine behavior to Hermes Agent. Keep the package fixture-first and no-secret by default.
## Current External Surface
As of 2026-06-01, the upstream Hermes Agent README describes:
- model switching via `hermes model`;
- tools via `hermes tools`;
- a messaging gateway for Telegram, Discord, Slack, WhatsApp, Signal, and CLI;
- built-in skill creation and self-improvement;
- cron scheduling;
- terminal backends including local, Docker, SSH, Modal, and Daytona;
- OpenClaw migration commands.
Verify upstream docs before depending on a command in code.
## Living IP Package Shape
Create a package that includes:
- agent identity file for Rio or Theseus;
- skill instructions copied from repo-owned `.agents/skills/*`;
- `living-ip-kb-interop` for read/propose/writeback behavior;
- no-secret tool allowlist;
- fixture replay command;
- model selection notes;
- proof output path.
Do not package production DBs, tokens, API keys, SSH keys, or Bitwarden exports.
## Rio Package
Rio Hermes package should focus on:
- internet finance and mechanism reasoning;
- contribution weights and paid-query effects;
- OPSEC finance filters;
- source-diversity warnings;
- fixture tests for false economic reasoning.
## Theseus Package
Theseus Hermes package should focus on:
- model-diversity evals;
- disagreement queues;
- self-upgrade criteria;
- prompt/tool safety;
- fixture tests for overconfident or poorly grounded model judgments.
## Handoff Contract
Every Hermes handoff must include:
1. install/config snippet;
2. model/provider selection left configurable;
3. tool allowlist;
4. fixture-first demo;
5. no-live-write default;
6. proof artifact path;
7. known blockers.
Do not claim Hermes production integration until a Hermes runtime actually executes the fixture and writes proof.

View file

@ -1,70 +0,0 @@
---
name: openclaw-agent
description: Use when adapting Living IP decision-engine agents, skills, tools, prompt files, or no-secret workflows to OpenClaw agent workspaces.
---
# OpenClaw Agent
Use this skill to package Living IP decision-engine behavior for OpenClaw workspaces. Treat OpenClaw as a distribution/runtime surface, not a new source of truth.
## Current External Surface
As of 2026-06-01, the upstream OpenClaw README describes:
- Node 24 or Node 22.19+ runtime;
- `openclaw onboard --install-daemon`;
- Gateway daemon usage;
- agent prompt files `AGENTS.md`, `SOUL.md`, and `TOOLS.md`;
- workspace skills at `~/.openclaw/workspace/skills/<skill>/SKILL.md`;
- model configuration in OpenClaw config;
- security guidance for DM pairing, allowlists, and sandboxing.
Verify upstream docs before depending on a command in code.
## Living IP Workspace Shape
Create or update:
- `AGENTS.md`: scope, repo boundaries, proof requirements;
- `SOUL.md`: Rio or Theseus identity;
- `TOOLS.md`: bounded tools only;
- `skills/decision-engine-refinement/SKILL.md`;
- `skills/living-ip-kb-interop/SKILL.md`;
- `skills/teleo-db-operator/SKILL.md` only for read-only local copies unless explicitly authorized.
## Tool Policy
Default allow:
- read files;
- run local fixture tests;
- write proof artifacts;
- inspect git diffs;
- query copied SQLite DBs read-only.
Default deny:
- production DB writes;
- token reads;
- Bitwarden vault export;
- live GitHub PR comments;
- public messaging sends;
- broad shell automation against host services.
## Rio And Theseus
- Rio OpenClaw package: economic reasoning, contribution incentives, paid-query guardrails, OPSEC.
- Theseus OpenClaw package: eval integrity, adversarial prompts, model bakeoffs, self-upgrade review.
## Proof Contract
An OpenClaw adapter is useful only if it can run a fixture and produce:
- prompt files used;
- tool allowlist;
- model selected;
- fixture input;
- structured verdict output;
- proof that no denied tools were invoked.
Do not claim OpenClaw production readiness until the package runs in an OpenClaw workspace and writes proof.

View file

@ -1,63 +0,0 @@
---
name: private-password-storage
description: Store or update a private password or credential in the device-local macOS Keychain through a native secure popup, and check presence without retrieval. Use when a user asks to save, store, update, or verify a private credential securely on a Mac.
---
# Private Password Storage
Use the bundled helper so the credential enters only through a native
`NSSecureTextField` and goes directly to Security.framework.
## Choose The Right Authentication Route
Prefer the provider's supported credential manager or login flow when one
exists. Keychain storage does not authenticate an application and cannot
satisfy OAuth, OTP, passkeys, browser sessions, or `gcloud` login by itself.
Never ask the user to paste a credential into chat.
## Store Or Update
Run `--store` only after the user asks to open the secure popup:
```bash
.agents/skills/private-password-storage/scripts/private-password-storage \
--service "com.example.application" \
--account "operator@example.invalid" \
--label "Example application password" \
--store
```
The popup accepts normal typing, Cmd+V, and right-click Paste. Empty input
stays in the popup and is rejected. A successful write prints only `stored`.
The item is stored as `kSecAttrAccessibleWhenUnlockedThisDeviceOnly` with
synchronization disabled.
## Check Presence
```bash
.agents/skills/private-password-storage/scripts/private-password-storage \
--service "com.example.application" \
--account "operator@example.invalid" \
--status
```
Status prints only `present` or `absent`. It never returns the credential.
## Safety Contract
- Keep the credential out of process arguments, shell variables, files,
logs, screenshots, terminal output, clipboard writes, and proof artifacts.
- Do not add retrieval, reveal, echo, export, or fingerprint modes.
- Do not use AppleScript, `osascript`, System Events, `pbcopy`, or the
`security add-generic-password` CLI.
- Treat service, account, and label as non-secret metadata; do not print them.
- Report only the helper status and whether a native popup was used.
## Sanitized Canary
Use only a generated fake value and a service beginning with
`dev.codex.private-password-storage.canary.` plus an account beginning with
`canary-`. The test-only `--delete-test-item` action is prefix-guarded and
deletes only that exact service/account pair. Verify `present`, delete the
exact item, then verify `absent`. Do not retain the fake value anywhere.

View file

@ -1,4 +0,0 @@
interface:
display_name: "Private Password Storage"
short_description: "Store device-local credentials securely"
default_prompt: "Use $private-password-storage to store or update a private credential securely in the macOS Keychain."

View file

@ -1,5 +0,0 @@
#!/bin/sh
set -eu
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
exec python3 "$SCRIPT_DIR/private_password_storage.py" "$@"

View file

@ -1,191 +0,0 @@
#!/usr/bin/env python3
"""Launch the native macOS Keychain helper without accepting secret arguments."""
from __future__ import annotations
import argparse
import os
import platform
import plistlib
import subprocess
import sys
import tempfile
from collections.abc import Iterator
from contextlib import contextmanager
from pathlib import Path
class SafeArgumentParser(argparse.ArgumentParser):
def error(self, message: str) -> None:
raise ValueError(message)
def parse_args() -> argparse.Namespace:
parser = SafeArgumentParser(allow_abbrev=False)
parser.add_argument("--service", required=True)
parser.add_argument("--account", required=True)
parser.add_argument("--label", default="Private credential")
actions = parser.add_mutually_exclusive_group(required=True)
actions.add_argument("--store", action="store_true")
actions.add_argument("--status", action="store_true")
actions.add_argument(
"--delete-test-item", action="store_true", help=argparse.SUPPRESS
)
return parser.parse_args()
def validate_metadata(args: argparse.Namespace) -> bool:
values = (args.service, args.account, args.label)
return all(value and "\x00" not in value for value in values)
@contextmanager
def native_command() -> Iterator[tuple[list[str], Path | None] | None]:
test_helper = os.environ.get("PRIVATE_PASSWORD_STORAGE_TEST_HELPER")
if test_helper:
yield ([test_helper], None)
return
if sys.platform != "darwin":
yield None
return
discovery = subprocess.run(
["/usr/bin/xcrun", "--find", "swiftc"],
check=False,
capture_output=True,
text=True,
)
if discovery.returncode != 0:
yield None
return
if not discovery.stdout.strip():
yield None
return
target = f"{platform.machine()}-apple-macosx12.0"
with tempfile.TemporaryDirectory(prefix="private-password-storage-") as temp:
app = Path(temp) / "Private Password Storage.app"
macos = app / "Contents" / "MacOS"
macos.mkdir(parents=True)
executable = macos / "private-password-storage-native"
info = {
"CFBundleDisplayName": "Private Password Storage",
"CFBundleExecutable": executable.name,
"CFBundleIdentifier": "dev.codex.private-password-storage",
"CFBundleName": "Private Password Storage",
"CFBundlePackageType": "APPL",
"CFBundleVersion": "1",
"CFBundleShortVersionString": "1.0",
"LSMinimumSystemVersion": "12.0",
"NSHighResolutionCapable": True,
}
with (app / "Contents" / "Info.plist").open("wb") as handle:
plistlib.dump(info, handle)
compilation = subprocess.run(
[
"/usr/bin/xcrun",
"swiftc",
"-target",
target,
str(Path(__file__).with_suffix(".swift")),
"-o",
str(executable),
],
check=False,
capture_output=True,
text=True,
)
if compilation.returncode != 0:
yield None
return
yield ([str(executable)], app)
def action_args(args: argparse.Namespace) -> list[str]:
if args.store:
action = "--store"
elif args.status:
action = "--status"
else:
action = "--delete-test-item"
return [
"--service",
args.service,
"--account",
args.account,
"--label",
args.label,
action,
]
def main() -> int:
try:
args = parse_args()
except (ValueError, SystemExit):
sys.stdout.write("error\n")
return 64
if not validate_metadata(args):
sys.stdout.write("error\n")
return 64
with native_command() as native:
if native is None:
sys.stdout.write("error\n")
return 69
command, app = native
try:
if args.store and app is not None:
result_file = app.parent / "store-status"
completed = subprocess.run(
[
"/usr/bin/open",
"-W",
"-n",
str(app),
"--args",
*action_args(args),
"--result-file",
str(result_file),
],
check=False,
capture_output=True,
text=True,
)
output = (
result_file.read_text(encoding="utf-8").strip()
if result_file.is_file()
else ""
)
else:
completed = subprocess.run(
[*command, *action_args(args)],
check=False,
capture_output=True,
text=True,
)
output = completed.stdout.strip()
except KeyboardInterrupt:
sys.stdout.write("cancelled\n")
return 1
allowed = {
"stored": {"stored", "cancelled"},
"status": {"present", "absent"},
"delete": {"absent"},
}
expected = "stored" if args.store else "status" if args.status else "delete"
if completed.returncode not in (0, 1) or output not in allowed[expected]:
sys.stdout.write("error\n")
return 1
sys.stdout.write(f"{output}\n")
if expected == "stored" and output != "stored":
return 1
return completed.returncode
if __name__ == "__main__":
raise SystemExit(main())

View file

@ -1,346 +0,0 @@
import AppKit
import Foundation
import Security
private enum Action {
case store
case status
case deleteTestItem
}
private struct Options {
let service: String
let account: String
let label: String
let action: Action
let resultFile: String?
}
private enum StoreResult {
case stored
case cancelled
case failed
}
private enum PresenceResult {
case present
case absent
case failed
}
private func parseOptions() -> Options? {
var service: String?
var account: String?
var label = "Private credential"
var action: Action?
var resultFile: String?
var index = 1
let arguments = CommandLine.arguments
while index < arguments.count {
let argument = arguments[index]
switch argument {
case "--service", "--account", "--label", "--result-file":
guard index + 1 < arguments.count else { return nil }
let value = arguments[index + 1]
guard !value.isEmpty, !value.contains("\0") else { return nil }
if argument == "--service" {
service = value
} else if argument == "--account" {
account = value
} else if argument == "--label" {
label = value
} else {
resultFile = value
}
index += 2
case "--store", "--status", "--delete-test-item":
guard action == nil else { return nil }
action = argument == "--store" ? .store
: argument == "--status" ? .status : .deleteTestItem
index += 1
default:
return nil
}
}
guard let service, let account, let action, !label.isEmpty else { return nil }
return Options(
service: service,
account: account,
label: label,
action: action,
resultFile: resultFile
)
}
private func deviceLocalQuery(service: String, account: String) -> [CFString: Any] {
[
kSecClass: kSecClassGenericPassword,
kSecAttrService: service,
kSecAttrAccount: account,
kSecAttrSynchronizable: kCFBooleanFalse as Any,
]
}
private func storeInKeychain(
service: String,
account: String,
label: String,
secretData: Data
) -> Bool {
let query = deviceLocalQuery(service: service, account: account)
var item = query
item[kSecAttrLabel] = label
item[kSecAttrAccessible] = kSecAttrAccessibleWhenUnlockedThisDeviceOnly
item[kSecValueData] = secretData
let addStatus = SecItemAdd(item as CFDictionary, nil)
if addStatus == errSecSuccess {
return true
}
guard addStatus == errSecDuplicateItem else {
return false
}
let updates: [CFString: Any] = [
kSecAttrLabel: label,
kSecAttrAccessible: kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
kSecValueData: secretData,
]
return SecItemUpdate(query as CFDictionary, updates as CFDictionary) == errSecSuccess
}
private func keychainPresence(service: String, account: String) -> PresenceResult {
var query = deviceLocalQuery(service: service, account: account)
query[kSecMatchLimit] = kSecMatchLimitOne
query[kSecReturnData] = kCFBooleanFalse
let status = SecItemCopyMatching(query as CFDictionary, nil)
if status == errSecSuccess {
return .present
}
if status == errSecItemNotFound {
return .absent
}
return .failed
}
private func deleteExactCanaryItem(service: String, account: String) -> Bool {
guard service.hasPrefix("dev.codex.private-password-storage.canary."),
account.hasPrefix("canary-") else {
return false
}
let query = deviceLocalQuery(service: service, account: account)
let status = SecItemDelete(query as CFDictionary)
return status == errSecSuccess || status == errSecItemNotFound
}
@MainActor
private final class SecurePromptController: NSObject, NSWindowDelegate {
private let service: String
private let account: String
private let itemLabel: String
private let window: NSWindow
private let secureField = NSSecureTextField()
private let validationLabel = NSTextField(labelWithString: "")
private var result: StoreResult?
init(service: String, account: String, label: String) {
self.service = service
self.account = account
self.itemLabel = label
self.window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 440, height: 190),
styleMask: [.titled, .closable],
backing: .buffered,
defer: false
)
super.init()
configureWindow()
}
private func configureWindow() {
window.title = "Store Private Credential"
window.isReleasedWhenClosed = false
window.delegate = self
window.center()
let content = NSView()
window.contentView = content
let title = NSTextField(labelWithString: itemLabel)
title.font = NSFont.systemFont(ofSize: 15, weight: .semibold)
title.lineBreakMode = .byTruncatingTail
secureField.placeholderString = "Password"
secureField.font = NSFont.systemFont(ofSize: 14)
validationLabel.stringValue = "Password cannot be empty."
validationLabel.textColor = .systemRed
validationLabel.isHidden = true
let cancelButton = NSButton(
title: "Cancel", target: self, action: #selector(cancelPressed)
)
cancelButton.keyEquivalent = "\u{1b}"
let storeButton = NSButton(
title: "Store", target: self, action: #selector(storePressed)
)
storeButton.keyEquivalent = "\r"
let pasteMenu = NSMenu()
let pasteItem = NSMenuItem(
title: "Paste", action: #selector(NSText.paste(_:)), keyEquivalent: ""
)
pasteItem.target = nil
pasteMenu.addItem(pasteItem)
secureField.menu = pasteMenu
[title, secureField, validationLabel, cancelButton, storeButton].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
content.addSubview($0)
}
NSLayoutConstraint.activate([
title.topAnchor.constraint(equalTo: content.topAnchor, constant: 24),
title.leadingAnchor.constraint(equalTo: content.leadingAnchor, constant: 24),
title.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -24),
secureField.topAnchor.constraint(equalTo: title.bottomAnchor, constant: 16),
secureField.leadingAnchor.constraint(equalTo: title.leadingAnchor),
secureField.trailingAnchor.constraint(equalTo: title.trailingAnchor),
secureField.heightAnchor.constraint(equalToConstant: 28),
validationLabel.topAnchor.constraint(equalTo: secureField.bottomAnchor, constant: 6),
validationLabel.leadingAnchor.constraint(equalTo: secureField.leadingAnchor),
storeButton.trailingAnchor.constraint(equalTo: secureField.trailingAnchor),
storeButton.bottomAnchor.constraint(equalTo: content.bottomAnchor, constant: -20),
cancelButton.trailingAnchor.constraint(equalTo: storeButton.leadingAnchor, constant: -10),
cancelButton.centerYAnchor.constraint(equalTo: storeButton.centerYAnchor),
])
}
private func installPasteMenu() {
let mainMenu = NSMenu()
let editMenuItem = NSMenuItem()
let editMenu = NSMenu(title: "Edit")
let pasteItem = NSMenuItem(
title: "Paste", action: #selector(NSText.paste(_:)), keyEquivalent: "v"
)
pasteItem.keyEquivalentModifierMask = [.command]
pasteItem.target = nil
editMenu.addItem(pasteItem)
editMenuItem.submenu = editMenu
mainMenu.addItem(editMenuItem)
NSApp.mainMenu = mainMenu
}
func run() -> StoreResult {
installPasteMenu()
NSApp.setActivationPolicy(.regular)
NSApp.activate(ignoringOtherApps: true)
window.makeKeyAndOrderFront(nil)
window.makeFirstResponder(secureField)
NSApp.run()
return result ?? .cancelled
}
@objc private func storePressed() {
guard !secureField.stringValue.isEmpty else {
validationLabel.stringValue = "Password cannot be empty."
validationLabel.isHidden = false
NSSound.beep()
window.makeFirstResponder(secureField)
return
}
validationLabel.isHidden = true
var secretData = Data(secureField.stringValue.utf8)
let stored = storeInKeychain(
service: service,
account: account,
label: itemLabel,
secretData: secretData
)
secureField.stringValue = ""
secretData.resetBytes(in: 0..<secretData.count)
guard stored else {
validationLabel.stringValue = "The credential could not be stored."
validationLabel.isHidden = false
return
}
result = .stored
window.close()
}
@objc private func cancelPressed() {
secureField.stringValue = ""
result = .cancelled
window.close()
}
func windowWillClose(_ notification: Notification) {
secureField.stringValue = ""
if result == nil {
result = .cancelled
}
NSApp.stop(nil)
}
}
@MainActor
private func runSecurePrompt(options: Options) -> StoreResult {
let application = NSApplication.shared
let controller = SecurePromptController(
service: options.service,
account: options.account,
label: options.label
)
_ = application
return controller.run()
}
private func emit(_ value: String, code: Int32, resultFile: String? = nil) -> Never {
let data = Data((value + "\n").utf8)
if let resultFile {
do {
try data.write(to: URL(fileURLWithPath: resultFile), options: .atomic)
} catch {
exit(74)
}
} else {
FileHandle.standardOutput.write(data)
}
exit(code)
}
guard let options = parseOptions() else {
emit("error", code: 64)
}
switch options.action {
case .status:
switch keychainPresence(service: options.service, account: options.account) {
case .present:
emit("present", code: 0, resultFile: options.resultFile)
case .absent:
emit("absent", code: 1, resultFile: options.resultFile)
case .failed:
emit("error", code: 1, resultFile: options.resultFile)
}
case .store:
let storeResult = MainActor.assumeIsolated {
runSecurePrompt(options: options)
}
switch storeResult {
case .stored:
emit("stored", code: 0, resultFile: options.resultFile)
case .cancelled:
emit("cancelled", code: 1, resultFile: options.resultFile)
case .failed:
emit("error", code: 1, resultFile: options.resultFile)
}
case .deleteTestItem:
guard deleteExactCanaryItem(service: options.service, account: options.account) else {
emit("error", code: 1, resultFile: options.resultFile)
}
emit("absent", code: 0, resultFile: options.resultFile)
}

View file

@ -1,76 +0,0 @@
---
name: teleo-db-operator
description: Use when reading, auditing, backing up, querying, or safely writing the Teleo pipeline SQLite database, including review_records, audit_log, costs, prs, sources, and contributor feedback loops.
---
# Teleo DB Operator
Default to read-only. The database is evidence for decision-engine refinement, not a scratchpad.
## Discover
1. Read `lib/config.py` for `DB_PATH` and related paths.
2. Prefer local or copied DBs over production DBs.
3. If using production, record whether access is read-only or write-authorized.
4. Never print secret values found near DB paths or shell history.
## Read Path
Use `sqlite3` or Python `sqlite3`.
Recommended read targets:
- `review_records`: evaluator, model, outcome, rejection reason.
- `audit_log`: route decisions, approve/reject events, failure details.
- `costs`: model cost by date/stage.
- `prs`: status, tier, route compatibility fields, verdicts.
- `sources`: priority, feedback, extraction model.
For refinement work, export aggregated JSON or CSV into `.crabbox-results/` or `proof/`, not raw private DB snapshots.
## Write Path
Writes require explicit authorization and a backup.
Required sequence:
1. Create a backup or operate on a copy.
2. Write the exact SQL in a retained artifact.
3. Use `BEGIN IMMEDIATE;`.
4. Apply the minimal mutation.
5. Read back the changed rows.
6. Commit the transaction only after readback is correct.
7. Write a blocker artifact instead of guessing if any precondition is missing.
Never write production prompt/model state as part of an experiment. Experiments should replay fixtures and produce proof first.
## Safety Boundaries
- Do not attach, copy, or commit `pipeline.db`.
- Do not run broad `UPDATE` or `DELETE` without a `WHERE` clause and a prior row count.
- Do not mutate `prs`, `sources`, or contributor state from a model response alone.
- Do not treat local copied DB proof as production proof.
## Useful Queries
```sql
SELECT reviewer, reviewer_model, outcome, rejection_reason, count(*) AS n
FROM review_records
GROUP BY reviewer, reviewer_model, outcome, rejection_reason
ORDER BY n DESC;
```
```sql
SELECT event, count(*) AS n
FROM audit_log
WHERE stage = 'evaluate'
GROUP BY event
ORDER BY n DESC;
```
```sql
SELECT model, stage, calls, input_tokens, output_tokens, cost_usd
FROM costs
ORDER BY date DESC, cost_usd DESC
LIMIT 50;
```

View file

@ -1,155 +0,0 @@
---
name: teleo-gcp-parity-ops
description: Use for passwordless Teleo GCP VM access, private Cloud SQL canonical parity, GCP Leo runtime readback, m3taversal no-send replay, rollback, and cleanup without collapsing VPS proof into GCP proof.
---
# Teleo GCP Parity Ops
## Working Target
Restore a copy of the VPS canonical Leo database to GCP, prove exact schema,
row, role, extension, performance, and private-connectivity parity, then run the
real GCP Leo read/reasoning path without Telegram sends or DB mutation.
## Operator Paths
The direct alias is passwordless but firewall-source-dependent:
```bash
ssh teleo-gcp-staging
```
It uses `/Users/user/.ssh/google_compute_engine` for
`billy_livingip_xyz@34.65.143.148`, disables password and keyboard-interactive
authentication, and does not store a Google password. It works only while the
Mac's public egress matches `teleo-prod-allow-ssh-current-ip`. On 2026-07-12 the
Mac returned to `99.35.221.133` while the rule still allowed
`176.108.138.1/32`, so TCP/22 timed out before authentication.
The intended durable path is `.github/workflows/gcp-iap-operator.yml`, using
short-lived GitHub OIDC, IAP, OS Login, and fixed reviewed operations. It is
merged but not bootstrapped: workflow run `29208215340` failed at auth with
`invalid_target` because provider `teleo-iap-operator` is absent, disabled, or
deleted. Do not call this path working until a live `status` run passes.
Verified target:
- project `teleo-501523`;
- VM `teleo-prod-1` in `europe-west6-a`;
- Cloud SQL `teleo-pgvector-standby`, PostgreSQL 16.14;
- private endpoint `10.61.0.3:5432`, public IP disabled, TLS required;
- service `leoclean-gcp-prod-parallel.service`.
Never print the Cloud SQL or Google password. On the VM, use the attached service account
and Secret Manager through reviewed wrappers or a short-lived environment
variable, then unset it.
## Two Different Databases
- Canonical collective knowledge is Cloud SQL database `teleo_canonical`, with
`public.*` and `kb_stage.*` tables.
- Hermes conversation continuity is `state.db` plus session JSONL files. The
`leoclean-cloudsql-memory-sync.service` snapshot path copies this runtime
memory; it does not populate or prove canonical claims, sources, evidence,
edges, or proposals.
Do not describe a passing Hermes memory sync as canonical KB parity.
## Current Verified State - 2026-07-12
- GCP `teleo_canonical` matches the captured VPS canonical database across
`39/39` tables and `52,164/52,164` rows.
- Exact high-signal counts are claims `1837`, sources `4145`, claim evidence
`4670`, claim edges `4916`, and proposals `26`.
- Rowsets, schemas, columns, constraints, indexes, functions, types, triggers,
views, policies, required roles, extensions, and performance checks have zero
mismatches. `pgcrypto` and `kb_apply` are present.
- The GCP gateway remained active/running at PID `148735`, `NRestarts=0`, with
start time `2026-07-09 07:00:04 UTC` throughout restore, swap, and read tests.
- The reviewed GCP `teleo-kb` wrapper, Cloud SQL tool, and bridge skill were
synchronized without a service restart. All six `DC-01` through `DC-06`
database-read routes pass.
- Generated candidate databases and temporary client/operation directories were
removed. `teleo_canonical_pre_20260712t1905z` is retained disabled, with zero
connections, as the verified pre-swap rollback point.
- A real no-send model replay returned six replies and nominally scored `6/6`,
but it is rejected as m3taversal-standard proof because `DC-03` and `DC-05`
printed zero canonical counts. The real database has
`1837/4145/4916/4670/26`.
- The harness now enables clone-bound, default-read-only `teleo-kb status`, runs
under the Hermes virtualenv, and rejects any printed count that differs from
the canonical status receipt. The hardened rerun and clone cleanup wait on
the exact operator-access gate below.
Primary retained proof:
- `docs/reports/leo-working-state-20260709/gcp-canonical-parity-live-20260712.json`
- `docs/reports/leo-working-state-20260709/gcp-cory-model-replay-current.json`
- `docs/reports/leo-working-state-20260709/gcp-operator-access-blocker-current.json`
- root task output `outputs/gcp-staging-canonical-parity-20260712T1905Z/`
- root task output `outputs/gcp-cory-model-replay-20260712T1940Z/`
## Required Parity Rows
Track these independently:
1. control-plane project, VM, Cloud SQL, private-IP, and TLS identity;
2. canonical database schema, counts, row hashes, constraints, indexes,
functions, extensions, roles, and performance;
3. GCP service PID/restarts plus live profile and tool hashes;
4. `DC-01` through `DC-06` DB-read readiness;
5. real no-send model replies, strict score, and exact count consistency;
6. no-mutation fingerprints, child/profile cleanup, clone deletion, and rollback
disposition.
## Canonical Restore And Replay
Use:
- `ops/capture_vps_canonical_postgres_snapshot.py` for a single source dump and
manifest;
- `ops/postgres_parity_manifest.sql` for row/catalog/role/performance readback;
- `ops/verify_postgres_parity_manifest.py --scope gcp_staging` for exact parity;
- `scripts/run_gcp_generated_db_direct_claim_suite.py` for the adapter-free,
read-only, no-send six-response replay against a generated `teleo_clone_*`.
The replay must use the Hermes virtualenv, not system Python:
```bash
/home/teleo/.hermes/hermes-agent/venv/bin/python \
scripts/run_gcp_generated_db_direct_claim_suite.py ...
```
The replay is not complete until the generated clone, temporary profile,
children, upload/run directories, and any temporary client are absent.
## Safety And Claim Ceiling
- Do not send Telegram messages from the GCP parity lane.
- Do not apply, approve, or stage KB changes during read/reasoning replay.
- Do not restart the live GCP gateway for tool-file synchronization unless a
separate restart window is explicitly requested.
- Do not call control-plane inventory, memory sync, route readiness, or a
nominal scorer pass full m3taversal parity.
The strongest accepted claim requires exact DB parity plus real no-send model
replies with truthful counts and cleanup. It still does not prove Telegram
delivery, GCP canonical mutation, ongoing replication, or production cutover.
## Exact Current Gate
All autonomous alternatives have been exhausted: privileged local `gcloud`
requires Google password reauthentication; direct SSH misses the firewall
`/32`; the dedicated WIF provider is not bootstrapped; the existing
`living-ip-github` provider can authenticate only an artifact service account
that lacks Compute/IAM/Secret Manager/Cloud SQL permissions; no Mullvad or
Tailscale route exposes the allowed office ISP address.
CTA: in Chrome, open the existing tab titled `Google Cloud Platform` for
`billy@livingip.xyz`, enter the current Google password, click `Next`, then tell
Codex exactly `GCP reauthenticated`. Do not send the password.
After that, rotate only the SSH `/32`, bootstrap and live-test the IAP operator,
run the hardened replay, delete `teleo_clone_cory_20260712t1940z`, remove its
run directory, and verify the disabled rollback database still has zero
connections.

View file

@ -1,70 +0,0 @@
---
name: teleo-infra-provenance
description: Use when tracing Teleo repo/deploy/runtime provenance, preventing path confusion between GitHub, local checkouts, VPS mirrors, live service cwd, and generated report artifacts.
---
# Teleo Infrastructure Provenance
## Job
Prevent incorrect assumptions about where Leo/Teleo code, runtime state, DB state, and evidence live.
## Trigger Phrases
- "where is Leo running from"
- "VPS provenance"
- "teleo-infrastructure repo"
- "deploy source"
- "why /opt/teleo-eval"
- "what is canonical"
## Surfaces To Separate
- GitHub/canonical repo truth.
- Local Codex workspace checkout.
- VPS live deploy/source mirror.
- Hermes/leoclean runtime profile.
- Postgres canonical DB.
- `kb_stage` proposal ledger.
- Retained proof artifacts under local `outputs/`.
- Synced report artifacts under the VPS profile report directory.
## Current Known Path Map
- Repo-local evidence pack: `docs/reports/leo-working-state-20260709/`
- VPS deploy/source area: `/opt/teleo-eval/workspaces/deploy-infra`
- VPS deploy stamp: `/opt/teleo-eval/.last-deploy-sha`
- Auto-sync: `teleo-auto-deploy.timer` checks `main` every two minutes.
- VPS profile reports: `/home/teleo/.hermes/profiles/leoclean/kb_stage/reports/`
- Live service: `leoclean-gateway.service`
## Required Readbacks
Before claiming provenance:
1. `git -C <path> status --short --branch`
2. `git -C <path> rev-parse HEAD`
3. `/opt/teleo-eval/.last-deploy-sha`
4. latest `teleo-auto-deploy.service` journal decision
5. service PID, start timestamp, restart count, `WorkingDirectory`, and `User`
6. commit delta and file checksums for the runtime paths being compared
7. latest retained report artifact timestamp
## Common Failure
Do not say the repo/deploy split is the direct cause of a Leo outage unless
evidence ties it to the active failure. The split can be a
reproducibility/parity risk without being the immediate runtime cause. A commit
present in the deploy checkout or stamp does not prove a gateway restart,
runtime-profile change, permission migration, worker enablement, or DB apply.
## Output
Return:
- current repo HEADs,
- dirty state,
- live service cwd/user,
- which path owns code changes,
- which path owns runtime reports,
- exact stale/provenance risk if any.

View file

@ -1,143 +0,0 @@
---
name: teleo-kb-db-change-workflow
description: Use for Teleo document-to-claim composition, proposal normalization, separate review/apply authority, isolated apply canaries, row-level proof, rollback, and production-apply gating.
---
# Teleo KB DB Change Workflow
## Working Target
Turn a source document, post, or operator correction into source-bound evidence,
atomic claims, graph links, and a reviewable proposal; then move an approved
change into exact canonical rows only through a guarded apply transaction.
Composition and application are separate capabilities. A synthetic apply test
does not prove source ingestion, and a staged source extraction does not prove
canonical application.
## State Model
- `pending_review`: staged, not approved, canonical rows unchanged.
- `approved`: reviewed intent exists, but `applied_at` may still be null.
- `applied`: guarded transaction and canonical postflight completed.
- `packet_ready_not_executed`: rehearsed artifacts exist; production is
unchanged.
Never infer `applied` from chat text, a packet, a clone, or `status=approved`.
## Composition Contract
A useful composed change must retain:
1. immutable source locator, captured bytes or excerpt, and SHA-256 binding;
2. atomic claim body plus type, confidence, tags, and provenance metadata;
3. exact evidence excerpt linked to both claim and source;
4. graph edges, conflict/update/supersession candidates, and uncertainty;
5. duplicate and existing-row search before staging;
6. a proposal payload that accounts for every intended canonical row.
Do not flatten strategy, governance, concept maps, identity, and reasoning tools
into generic claim rows merely because the input document discusses them.
Run the deterministic source-to-proposal canary:
```bash
.venv/bin/python scripts/run_leo_local_ingestion_proposal_canary.py \
--fixture fixtures/working-leo/document-ingestion-v1.json \
--output outputs/leo-local-ingestion-proposal-canary-current.json
```
Require the source, claim, evidence, and proposal UUIDs to link exactly; claim
and evidence text must be exact substrings of the source; status must remain
`pending_review`; the container must use network `none` and leave no volume or
container behind.
## Canonical Apply Contract
The strict `approve_claim` v2 payload may create:
- `public.claims`
- `public.sources`
- `public.claim_evidence`
- `public.claim_edges`
- `public.reasoning_tools`
The lifecycle is split across:
- `scripts/kb_proposal_normalize.py`: fail-closed rich intent to strict payload;
- `scripts/approve_proposal.py`: `kb_review` approves the exact type/payload;
- `scripts/apply_proposal.py`: operator-only `kb_apply` writes and verifies;
- `scripts/kb_apply_prereqs.sql`: roles, immutable approval row, gate functions,
ownership, indexes, and ACLs;
- `scripts/run_approve_claim_isolated_container_canary.sh`: disposable
lifecycle plus optional live-readonly source/service checks.
## Authority Invariants
1. `kb_review` and `kb_apply` are separate `NOINHERIT` login roles.
2. `kb_gate_owner` is `NOLOGIN` and owns immutable approval/gate functions.
3. Review binds DB role, reviewer, type, full payload, timestamp, and note.
4. Apply locks and compares that snapshot before canonical writes.
5. Payload-controlled fields and exact table deltas are verified.
6. Existing evidence/edge semantic mismatches roll back the transaction.
7. Replay, stale payload, source-hash drift, and direct privilege probes fail.
8. `kb_apply` remains a trusted operator-only canonical writer, not autonomous
chat write authority.
## Operator Path
1. Read the proposal, source bytes/hash, and canonical target rows.
2. Normalize rich intent; reject lossy, duplicate, or unsupported mappings.
3. Run focused tests:
```bash
.venv/bin/python -m pytest -q \
tests/test_kb_proposal_normalize.py \
tests/test_approve_proposal.py \
tests/test_apply_proposal.py \
tests/test_kb_apply_prereqs.py
```
4. Run the generic and real-packet disposable canaries.
5. Require exact before/after projections, deltas, source hashes, approval row,
applied timestamp, service state, rollback, and cleanup.
6. Treat source checkout deployment, permission migration, worker enablement,
and production proposal application as separate windows.
## Current Proof - 2026-07-12
- Deterministic source composition passes all `13` checks and links one source,
one extracted claim, one evidence row, and one `pending_review` proposal. It
proves the retained fixture path, not arbitrary-source model extraction.
Arbitrary production document/tweet ingestion is not proven.
- The isolated approved-change lifecycle creates exact deltas of `2` claims,
`2` sources, `2` evidence rows, `1` edge, and `1` reasoning tool, stamps the
proposal applied, rolls back, and leaves no container or volume.
- The earlier generic and Helmer v3 source-bound receipts remain `37/37`; the
newer lifecycle receipt adds stricter service/source isolation and cleanup.
- A VPS live-readonly T3 clone run passed with unchanged canonical counts and
unchanged gateway PID. Production was not applied.
- The broader live-VPS full-data source-composition checkpoint passes `34/34`:
new hash-bound document/post, conflicting atomic claims, exact source/evidence
links, strict proposal, separated approval/apply, restarted discovery, graph
reasoning, and cleanup. This is isolated clone proof, not Telegram delivery
or production mutation.
- PR #86 merged the harness/operator fixes. VPS auto-deploy synchronized source
without restarting Leo; the gateway remained PID `2403328`, `NRestarts=0`.
- Current VPS canonical counts remain claims `1837`, sources `4145`, evidence
`4670`, edges `4916`, proposals `26`.
Read:
- `docs/reports/leo-working-state-20260709/approve-claim-clone-canary-current.json`
- `docs/reports/leo-working-state-20260709/leo-source-composition-clone-checkpoint-current.json`
- `outputs/leo-local-ingestion-proposal-canary-current.json`
## Claim Ceiling
Leo has strong isolated proof for source-bound composition, guarded canonical
application, restarted discovery, and graph reasoning. Production still has
three approved-but-unapplied legacy packets and fourteen pending-review
proposals. No broad autonomous production apply authority exists, and no
production rich packet should be applied without exact retained authorization,
operator identity, postflight, regression, rollback, and cleanup evidence.

View file

@ -1,98 +0,0 @@
---
name: teleo-leo-onboarding
description: Use when a worker needs fast context on Teleo/Living IP, Leo, the product architecture, current VPS/GCP split, and the July 9 working-state evidence before doing Leo, Teleo, KB, Telegram, VPS, or GCP work.
---
# Teleo / Leo Onboarding
## Job
Orient the worker before action. Build a current, proof-linked understanding of the company/product, Leo's role, the infrastructure surfaces, and the exact claim ceiling.
## Trigger Phrases
- "onboard to Leo"
- "what is Teleo / Living IP"
- "load Leo VPS context"
- "Fable handoff for Leo"
- "before working on Teleo infra"
- "explain the architecture"
## Core Model
- Teleo is the knowledge/agent infrastructure layer behind Leo.
- Leo is the operator-facing agent expected to answer in Telegram, remember operator context, reason from canonical KB state, stage concrete KB changes, and support approved changes becoming canonical DB rows with proof.
- The immediate July 9 issue is not generic bot liveness. It is m3taversal's expectation that approved KB changes move beyond proposal state when appropriate.
- The VPS is the currently proven Telegram-visible Leo surface.
- GCP is a separate lane. The live gateway, private Cloud SQL canonical rows,
and exact VPS-to-GCP database parity are proven. Direct passwordless SSH was
proven but is not currently on-demand because its firewall `/32` no longer
matches this Mac's egress, and the dedicated OIDC/IAP path is not bootstrapped.
Telegram delivery, GCP canonical mutation, ongoing replication, and cutover
remain separate proof rows.
## Architecture Boundaries
- VPS canonical KB: Docker Postgres `teleo-pg`, database `teleo`.
- GCP canonical KB: private Cloud SQL database `teleo_canonical`.
- Canonical rows: `public.claims`, `public.sources`,
`public.claim_evidence`, `public.claim_edges`, and related identity/strategy
tables; staging/review is `kb_stage.*`.
- Hermes `state.db` and session JSONL files are conversation continuity, not
canonical collective knowledge. GCP memory-sync success does not prove the
canonical KB was restored or updated.
- `SOUL.md` is a rendered/runtime artifact. Direct file edits are not canonical
identity changes without DB row and render/sync proof.
## Read First
From the repo root, read:
1. `docs/reports/leo-working-state-20260709/working-leo-current-proof-20260712.md`
2. `docs/reports/leo-working-state-20260709/current-truth-index.md`
3. `docs/reports/leo-working-state-20260709/operator-surface-map.md`
4. `docs/reports/leo-working-state-20260709/working-leo-definition-20260709.md`
5. `docs/reports/leo-working-state-20260709/cory-expected-working-leo-outcomes-20260709.md`
6. `docs/reports/leo-working-state-20260709/approve-claim-clone-canary-current.md`
7. `docs/reports/leo-working-state-20260709/gcp-cloud-sql-t3-live-readonly-current.md`
8. `docs/reports/leo-working-state-20260709/gcp-canonical-parity-live-20260712.json`
9. `docs/reports/leo-working-state-20260709/gcp-cory-model-replay-current.json`
10. `docs/reports/leo-working-state-20260709/gcp-operator-access-blocker-current.json`
## Required Status Split
Every status answer must split:
- `VPS runtime`
- `Telegram-visible Leo`
- `KB proposal/staging`
- `Canonical DB apply`
- `GCP parity`
- `Runtime/code provenance`
Do not collapse GCP demo readiness into Telegram completion. Do not collapse proposal approval into canonical DB application.
## Hard Boundaries
- Do not do paid route work or introduce paid-route naming.
- Do not change live VPS Leo runtime behavior unless explicitly authorized.
- Do not production-apply DB packets unless explicitly authorized.
- Do not expose secret contents.
- Do not treat an old summary as current if a fresh readback is cheap.
- Try `ssh teleo-gcp-staging` for passwordless GCP VM access, but verify the
current public egress matches the firewall `/32`. If it does not, follow the
exact gate in `gcp-operator-access-blocker-current.json`; do not pretend the
merged OIDC/IAP workflow is bootstrapped. Do not print the Cloud SQL or Google
password or create IAM/users inside a read-only lane.
- A benchmark score is not sufficient when an answer contradicts the DB receipt.
Compare every printed count to `teleo-kb status` or the canonical manifest.
## Output Format
For onboarding handoffs, return:
1. Product/architecture summary in 8 bullets or fewer.
2. Current proof split by VPS, Telegram, DB, GCP.
3. Evidence files actually read.
4. Exact claim ceiling.
5. Next runnable non-production action.

View file

@ -1,54 +0,0 @@
---
name: teleo-proof-handoff
description: Use to package Leo/Teleo evidence and prompts for Fable, Codex, Opus, or future workers so they can continue without rereading the whole thread.
---
# Teleo Proof Handoff
## Job
Create a compact, evidence-indexed handoff that preserves current truth, claim ceilings, exact blockers, and next executable actions.
## Trigger Phrases
- "handoff to Fable"
- "worker onboarding"
- "pack the evidence"
- "resume Leo work"
- "create continuation prompt"
- "what should the next agent know"
## Required Sections
1. `current_canary`: the next runnable action and expected result.
2. `claim_ceiling`: what cannot be claimed yet.
3. `proven`: exact proof bullets with paths.
4. `not_proven`: exact remaining gaps.
5. `do_not_do`: scope boundaries.
6. `files_to_read_first`: no more than 10 files.
7. `commands_to_rerun`: freshness checks.
8. `next_action`: one non-production action to start now.
## Blocker Rule
If a worker claims blocked, require:
- `current_canary`
- `attempted_routes`
- `exact_gate`
- `clear_CTA`
- `next_non_user_action`
Do not accept vague blockers such as "login blocked", "human-origin proof needed", or "no access" without the exact app/account/route and next CTA.
## Fable Prompt
Use `docs/reports/leo-working-state-20260709/fable-leo-teleo-onboarding.md` as the default first prompt.
## Evidence Index
Use `docs/reports/leo-working-state-20260709/current-truth-index.md`.
## Handoff Claim Ceiling
A handoff is not completion. It is complete only if a future worker can start from retained files and run the named canary without asking for context.

View file

@ -1,226 +0,0 @@
---
name: teleo-vps-runtime-ops
description: Use for Leo VPS navigation, service health, Docker/Postgres readbacks, clone DB rehearsals, report sync, and runtime stability verification without changing live Leo behavior.
---
# Teleo VPS Runtime Ops
## Job
Navigate and verify the VPS safely, with exact readbacks and no surprise live-runtime changes.
## Trigger Phrases
- "Leo on VPS"
- "check VPS stability"
- "navigate Teleo VPS"
- "leoclean service"
- "run DB rehearsal on VPS"
- "sync reports to VPS"
## Known Surfaces
- Host: `77.42.65.182`
- SSH user: `root`
- Local key path is available; never print private key contents.
- Service: `leoclean-gateway.service`
- DB container: `teleo-pg`
- DB: `teleo`
- Last retained count endpoints: claims `1837`, sources `4145`, evidence `4670`,
edges `4916`, reasoning tools `17`, proposals `26`. Treat these as stale until
refreshed.
- Profile reports: `/home/teleo/.hermes/profiles/leoclean/kb_stage/reports/`
- Deploy/source area: `/opt/teleo-eval/workspaces/deploy-infra`
## Required Fresh Readbacks
Before claiming runtime state, read:
```bash
systemctl show leoclean-gateway.service -p ActiveState -p SubState -p MainPID -p NRestarts -p ExecMainStartTimestamp -p User -p WorkingDirectory
```
Before claiming DB state, use `docker exec teleo-pg psql -U postgres -d teleo` and query exact tables/rows.
Wrap verification queries in `begin transaction read only; ... rollback;`.
Before claiming cleanup, query disposable DBs:
```sql
select datname from pg_database where datname like 'teleo%rehearsal%20260709' or datname like 'teleo%packet%20260709';
```
## Auto-Deploy Semantics
`teleo-auto-deploy.timer` checks `main` every two minutes. A changed checkout
HEAD or `.last-deploy-sha` does not by itself prove that Leo restarted or that
canonical data changed. For each synchronized commit, also read:
1. `journalctl -u teleo-auto-deploy.service` for the exact deploy decision;
2. the commit delta under `hermes-agent/leoclean-bin/` and
`hermes-agent/leoclean-skills/vps/`;
3. gateway PID, start timestamp, and restart count;
4. `teleo-kb-apply-worker.service` enablement and active state;
5. canonical count endpoints in a read-only transaction.
Treat source checkout sync, runtime profile sync, service restart, permission
migration, worker enablement, and canonical DB apply as separate state changes.
## Mutating Boundaries
Allowed when needed:
- read-only service/DB inspection,
- disposable clone DB creation and drop for rehearsal,
- SQL rollback transactions,
- syncing report artifacts to the report directory,
- no-secret file checks.
Not allowed from this skill alone:
- restarting or changing the live Leo service,
- changing live runtime config,
- production DB commit/apply,
- exposing secret contents,
- deleting non-disposable data.
## Rehearsal Rules
For DB rehearsals:
1. Capture production preflight counts.
2. Create a disposable DB from live state.
3. Run commit SQL only in the disposable DB.
4. Run postflight counts.
5. Run delete rollback if applicable.
6. Drop the disposable DB.
7. Verify production counts stayed unchanged.
8. Verify the disposable DB no longer exists.
For the guarded claim-bundle lifecycle, prefer the retained isolated wrapper:
```bash
scripts/run_approve_claim_isolated_container_canary.sh \
--output docs/reports/leo-working-state-20260709/approve-claim-clone-canary-current.json
```
It must use a disposable unexposed PostgreSQL container, bind its receipt to
current source hashes, compare exact payload projections and table deltas, read
live count/service endpoints without writing them, and independently prove the
container/workdir are absent afterward. Do not install the candidate code into
the live Leo deploy merely to run this canary.
To prove that Leo itself can inspect one lifecycle state through the real
`GatewayRunner` while remaining bound to a disposable full-data clone, run the
checkpoint on the VPS as `teleo` while that clone exists:
```bash
sudo -u teleo install -d -m 700 /home/teleo/leo-checkpoint-reports
sudo -u teleo HOME=/home/teleo \
/home/teleo/.hermes/hermes-agent/venv/bin/python \
scripts/run_leo_clone_bound_handler_checkpoint.py \
--container <disposable-container> \
--db teleo \
--prompt-id <stable-id> \
--prompt "<m3taversal-style KB question>" \
--expected-state approved \
--copy-model-auth \
--output /home/teleo/leo-checkpoint-reports/leo-clone-bound-checkpoint.json
```
The supplied clone must have Docker label
`com.livingip.leo.checkpoint=disposable`, network mode `none`, no published
ports, no mount source shared with production, and a PostgreSQL system
identifier distinct from production. The harness resolves the name once, pins
all operations to the full container ID, and fails if the name is rebound.
`--copy-model-auth` copies only `auth.json` into a private UUID-scoped profile
and binds only the configured provider's env-backed credential in memory. The
receipt records provider/variable names, never credential values or hashes. The
gateway runs in a dedicated process group with no delivery adapters and only
`skills_list`, `skill_view`, and a terminal handler restricted to the temporary
clone-bound wrapper's read-only verbs. The terminal subprocess must not inherit
the model credential. A timeout must terminate the child process group before
the profile is removed.
The checkpoint must observe at least one successful `teleo-kb` call bound to
the supplied container/database, remove the profile, and prove production row
counts plus row-content fingerprints, gateway state, and live bridge hashes are
unchanged. Every model tool call must be in the allowlist and every KB call must
complete successfully for a clean pass. A correct answer after a failed call
and retry is recovered behavior, not a clean reliability pass. The checkpoint
does not review, approve, apply, restart, post to Telegram, or mutate
production.
After that read-only checkpoint is clean, prove the lower-level
pending-to-canonical lifecycle in the same kind of disposable clone:
```bash
sudo -u teleo HOME=/home/teleo \
/home/teleo/.hermes/hermes-agent/venv/bin/python \
scripts/run_leo_clone_lifecycle_checkpoint.py \
--container <disposable-container> \
--db teleo \
--copy-model-auth \
--operator-review \
--guarded-apply \
--output /home/teleo/leo-checkpoint-reports/leo-clone-lifecycle-checkpoint.json
```
This command gives Leo only one deterministic staging verb. The harness, not
Leo, owns review and guarded apply. A pass requires exact structured receipts
for conversation memory, pending, approved-but-unapplied, and applied state;
exact linked claim/source/evidence rows; an isolated handler reopen using the
same persisted session; zero rejected or nonzero terminal calls; and unchanged
production DB fingerprints, service state, and live bridge hashes. It is a
mutation primitive, not proof that Leo can extract knowledge from arbitrary
documents or tweets.
For the real source-composition checkpoint, first create a fresh full-data
clone with the same disposable label/network/mount rules, then install the gate
and separated ephemeral clone credentials into a private run directory:
```bash
sudo -u teleo HOME=/home/teleo \
/home/teleo/.hermes/hermes-agent/venv/bin/python \
scripts/bootstrap_clone_kb_gate.py \
--container <fresh-disposable-container> \
--db teleo \
--output-dir <private-run-directory>
```
The bootstrap must verify both `kb_review` and `kb_apply` logins and emit only
credential file paths, never secret values or hashes. Then run:
```bash
sudo -u teleo HOME=/home/teleo \
/home/teleo/.hermes/hermes-agent/venv/bin/python \
scripts/run_leo_clone_composition_checkpoint.py \
--container <fresh-disposable-container> \
--db teleo \
--copy-model-auth \
--operator-review \
--guarded-apply \
--review-secrets-file <private-run-directory>/kb-review.env \
--apply-secrets-file <private-run-directory>/kb-apply.env \
--output <private-run-directory>/reports/composition-current.json
```
A pass requires model-driven dedupe search and extraction from supplied source
bytes, exact source hashes and excerpts, linked claims/evidence/conflict edges,
no row change before staging, deterministic normalization, separated review and
apply, exact clone deltas, a new handler process reasoning over exact canonical
rows without supplied IDs, session-marker recall, nonce-bound tool receipts,
unchanged production DB/service/bridge state, and complete clone/profile/secret
cleanup. The retained current receipt is
`docs/reports/leo-working-state-20260709/leo-source-composition-clone-checkpoint-current.json`.
It is not Telegram-visible proof and does not authorize production apply.
## Blocker Format
Do not say "blocked" without:
- `current_canary`
- `attempted_routes`
- `exact_gate`
- `clear_CTA`
- `next_non_user_action`

View file

@ -1,210 +0,0 @@
---
name: working-leo-m3taversal-outcomes
description: "Use when defining, testing, or repairing Leo against m3taversal's expected outcomes: Telegram memory, critical reasoning, canonical KB truth, proposed-vs-approved-vs-applied state, and guarded DB manipulation."
---
# Working Leo / m3taversal Outcomes
## Job
Keep Leo work anchored to what m3taversal appears to mean by "working": not just answers, but remembered context, truthful KB state, and approved concrete changes becoming canonical rows through a guarded proof path.
## Participant Identity Rule
- Address `@m3taversal` only as `m3taversal`, exactly.
- Never infer, invent, shorten, or substitute a personal name from a session
header, memory, identity document, another chat, or another participant.
- Resolve the speaker from the current Telegram update. Do not transfer identity
across users when someone replies, tags an account, or joins the thread.
- The legacy database value `m3ta` may be reported only as a quoted stored
reviewer handle. It is never a form of address.
- Standard response labels are neutral. Use `Next proof-changing follow-up:`.
## Trigger Phrases
- "working Leo"
- "m3taversal expected outcomes"
- "m3taversal says Leo is broken"
- "able to manipulate the knowledge base"
- "same state as last night"
- "critical reasoning behavior"
## Definition Of Working
A working Leo is a Telegram-facing agent that:
1. Remembers the current operator conversation.
2. Grounds KB answers in canonical Teleo Postgres rows when claiming KB truth.
3. Distinguishes `proposed`, `pending_review`, `approved`, `applied`, and `not applied`.
4. Does not say an approval changed canonical DB state when only `kb_stage` changed.
5. Can answer vague, high-level m3taversal-style incident prompts without being spoon-fed exact proposal IDs.
6. Can compose the KB from a previously unindexed document, URL, or tweet-like
source: retain a byte/hash-bound source locator, extract atomic claims and
exact evidence excerpts, preserve useful metadata, and link every claim to
its evidence and source.
7. Detects duplicates, conflicts, updates, and insufficient evidence before
staging; uncertainty must remain visible instead of being normalized into a
stronger claim.
8. Can stage those concrete KB changes from Telegram with enough structure for
human review, without making staged content canonical.
9. Can move approved concrete changes through a guarded apply path when authorized.
10. Retains m3taversal's caveats and review notes in source/evidence/proposal rows.
11. Reasons over claims, evidence, sources, edges, and open conflicts as a graph,
and can explain which rows support or weaken an answer without being given IDs.
12. Rebuilds any compiled identity/workspace artifact deterministically from
canonical DB rows, reports the source rows and freshness boundary, and does
not treat edits to a generated artifact as canonical knowledge.
13. Produces before/after table-level proof and service stability readback.
14. Survives an intentional `leoclean-gateway.service` restart: active before, active after, canonical KB counts unchanged, and a no-post handler smoke still answers.
15. For a reviewed graph bundle, produces exact payload-controlled row
projections, exact table deltas, source-byte binding, and cleanup proof in a
disposable runtime before any production permission or apply window.
## Current Benchmark Cases
- Marker memory: `WL-LIVE-TG-CORY-20260709`.
- Truth correction: `approved != applied`.
- Applied canary: proposal `00957f6c-9883-4015-95a4-6b09367efb0e` and edge `c167933e-d513-4f43-9335-d5d8aeb259f2`.
- Staged write canary: proposal `8dfedb3f-3aa4-4200-970f-4c0016f6869f`, status `pending_review`, with no new public canonical rows after the test timestamp.
- Open-ended triage canary: `WL-LIVE-TG-CORY-OPEN-20260709`, where Leo inferred the likely failure mode from "agents not working / same state as last night" and explained the proposed, approved, and applied split without exact IDs.
- Old rich proposal packets: `14fa5ecc...`, `ac036c9d...`, and `a64df080...` are not to be silently production-applied.
- Guarded apply proof: both generic and real Helmer v3 receipts pass `37/37` in
disposable PostgreSQL; production Helmer remains unapplied.
- Clone-bound handler proof: the real VPS `GatewayRunner` can inspect the full
current-data disposable clone, discover the Helmer proposal, distinguish
`approved` from `applied`, and produce bound bridge-call evidence without a
Telegram post or production change. The proof surface must expose no delivery
adapters or send tool, restrict terminal execution to clone-bound read-only KB
verbs, kill the handler process group on timeout, and compare production
row-content fingerprints before and after. Treat open-ended latency and
retries as a separate reliability dimension; a correct answer after a failed
tool call is recovered behavior, not a clean pass, and one correct answer does
not establish a stable pass rate.
- Source-composition proof: the real VPS `GatewayRunner` passed `34/34` in a
fresh no-send full-data clone. It searched existing knowledge, extracted two
hash-bound conflicting claims from a new document and post, staged a strict
proposal, preserved an immutable separated approval, applied exact canonical
rows, reopened in a new child process, recalled the prior marker, discovered
the new proposal/claims without supplied IDs, read exact evidence/source UUIDs
and edges, and explained `approved != applied`. Production DB fingerprints,
service PID/restarts, and live bridge hashes stayed unchanged; all disposable
containers, volumes, profiles, credentials, and run directories were removed.
This is clone proof, not Telegram delivery or production apply proof.
## Direct Questions
For vague or no-context questions, answer directly and then name the one action
that would change the proof:
- "Did the DB change?": split applied rows, approved-but-unapplied proposals,
pending rows, and canceled rows. Say `Approved is not the same as applied`.
- "Is Helmer in Leo?": say `no, not canonical` until the exact claims, sources,
evidence, edges, reasoning tool, and applied ledger read back from production.
- "Did the decision matrix approve it?": verify matrix tables first; reviewer
approval is not a matrix vote.
- "Are document pointers the blocker?": answer `not just pointer mismatch` and
separate files, source refs, canonical source rows, review, and apply.
- "Can I demo KB mutation?": split staging-demo truth from canonical-apply truth.
- "Did editing SOUL.md change identity?": no canonical identity change without
row IDs plus render/sync postflight.
End no-context answers with exactly one `Next proof-changing follow-up:` line.
## Current Verdict - 2026-07-13
Use `not fully yet` for the whole m3taversal-standard question until every row below
is green at its required tier:
- VPS runtime and restart survival: proven.
- VPS no-send `DC-01` through `DC-06`: three post-deploy clean-session trials
strict-score `6/6` (`18/18` replies) with unchanged DB/service state and
complete temporary-profile cleanup.
- Telegram-visible open-ended read-only behavior and conversation memory:
proven for the earlier `OE-01` through `OE-05` suite, but that narrower suite
does not establish current broad reliability.
- Telegram-visible no-context direct-claim suite: all six prompts/replies are
captured with unchanged DB/service state, but the hardened score is `5/6`.
`DC-05` proposes an incompatible legacy `add_edge` apply target.
- Canonical apply primitive and approved bundle lifecycle: proven in isolation;
broad production packet application is not authorized or executed.
- Source composition: deterministic fixture proof plus a full-data, no-send,
current-VPS clone checkpoint are proven. Arbitrary production document/tweet
ingestion is not proven.
- Broad blind VPS behavior: operationally clean but semantically failed.
Twelve of twelve prompts returned, DB/service state stayed unchanged, and the
temporary profile was removed; independent strict judges accepted only
`1/12` and `2/12` outright. Failures include invented current schema fields
and edge types, handler proof described as Telegram-live, incorrect runtime
memory boundaries, and temporary memory treated as source provenance.
- GCP canonical DB parity and six DB-read routes: proven. A later adapter-free
model replay passed `6/6` with exact count consistency and unchanged state.
Durable on-demand operator access and cleanup of the retained replay clone
remain open because the expected operator service accounts are absent and the
privileged Google account requires human password/MFA reauthentication.
Do not answer `yes` merely because all repo tests pass. The final user-facing
proof is a visible Telegram conversation plus truthful canonical row readback.
## Hard m3taversal Question Bank
Use these without IDs, schema hints, or guardrail-heavy setup. The answer must
infer the relevant rows and end with one proof-changing follow-up:
1. `I approved Helmer weeks ago. Why isn't it real yet?`
2. `What can I show a partner in five minutes?`
3. `This PDF should update Leo. What happens now?`
4. `What changed in your identity since last week?`
5. `Make this claim canonical: <claim text>.`
6. `Are pending proposals stuck because sources are wrong?`
7. `What does our strategy depend on, and which evidence could overturn it?`
8. `What's the next KB change I should approve?`
Score the answer on directness, fresh canonical lookup, claim/evidence/source
reasoning, state semantics, uncertainty, row-level proof, and the usefulness of
the next action. Penalize asking m3taversal for IDs that Leo can discover itself.
Also fail the answer when it presents proposed architecture as current v1. The
current `public.claims` table has `text` and `superseded_by`, not `body`, generic
metadata, or forecast-resolution fields. Current `public.sources` has no
author/channel/date columns. Current accepted edge types are `supports`,
`challenges`, `requires`, `relates`, `contradicts`, `supersedes`,
`derives_from`, `cites`, `causes`, `constrains`, and `accelerates`.
## Required Answer Discipline
When Leo or a worker answers about KB state, it must say:
- What source was used: memory, Telegram history, `kb_stage`, canonical `public.*`, or filesystem/runtime.
- Which rows/tables changed.
- Which proposal IDs are only staged/approved.
- Which canonical rows exist.
- What remains pending or deferred.
## Not Done
Leo is not working just because:
- the systemd service is active,
- Bot API calls work,
- a local handler canary passed,
- a proposal was approved,
- a packet was generated,
- a clone rehearsal passed.
- `NRestarts=0` was observed without an intentional restart-cycle proof.
Those are evidence. The target is Telegram-visible behavior plus DB-state truth plus a guarded path to canonical rows.
A synthetic one-claim proposal proves only the guarded mutation primitive. It
does not prove KB composition. Composition requires a source that was absent at
baseline, model-driven extraction into linked source/evidence/claim proposals,
dedupe/conflict readback, guarded canonical apply, and an open-ended answer
grounded in the new rows after an isolated handler restart.
## Proof Files
Start with `docs/reports/leo-working-state-20260709/working-leo-current-proof-20260712.md`
and its JSON companion for the current whole-system verdict and completion rule.
Use `docs/reports/leo-working-state-20260709/current-truth-index.md` for current artifact paths.
Use `docs/reports/leo-working-state-20260709/leo-source-composition-clone-checkpoint-current.md`
and its JSON companion for the current source-composition claim ceiling.
For restart survival, use `scripts/collect_leo_restart_survival_proof.py --execute-restart`; expected artifacts are `docs/reports/leo-working-state-20260709/leo-restart-survival-proof-current.json` and `.md`.

View file

@ -1,187 +0,0 @@
profile: teleo-infrastructure-check
provider: hetzner
target: linux
architecture: arm64
class: beast
ttl: 90m
idleTimeout: 20m
capacity:
market: spot
strategy: most-available
fallback: on-demand-after-120s
actions:
workflow: .github/workflows/crabbox.yml
job: hydrate
runnerLabels:
- crabbox
runnerVersion: latest
ephemeral: true
sync:
delete: true
checksum: false
gitSeed: true
fingerprint: true
timeout: 15m
warnFiles: 50000
warnBytes: 5368709120
failFiles: 150000
failBytes: 21474836480
exclude:
- .cache
- .venv
- .pytest_cache
- .ruff_cache
- __pycache__
- "*.pyc"
- "*.db"
- "*.db-wal"
- "*.db-shm"
- "*.log"
- logs
- secrets
- .env
- htmlcov
- dist
- build
- "*.egg-info"
- .turbo
- node_modules
env:
allow:
- CI
- PYTHONWARNINGS
- PHASE1B_AGENT_ROUTING_ENABLED
ssh:
user: crabbox
port: "2222"
# Ordered fallback ports tried after ssh.port; use [] to disable fallback.
fallbackPorts:
- "22"
jobs:
ci-contract:
provider: hetzner
target: linux
architecture: arm64
class: beast
hydrate:
actions: true
githubRunner: false
waitTimeout: 20m
keepAliveMinutes: 90
actions:
workflow: .github/workflows/crabbox.yml
job: hydrate
shell: true
command: >
python3 -m pip install -e '.[dev]' &&
mkdir -p .crabbox-results &&
python3 scripts/check_crabbox_ci_contract.py
--output .crabbox-results/crabbox-ci-contract.json &&
python3 scripts/check_llm_refinement_contract.py
--output .crabbox-results/llm-refinement-contract.json &&
python3 scripts/replay_decision_engine_eval.py
--output .crabbox-results/decision-engine-eval.json
downloads:
- .crabbox-results/crabbox-ci-contract.json
- .crabbox-results/llm-refinement-contract.json
- .crabbox-results/decision-engine-eval.json
stop: always
unit:
provider: hetzner
target: linux
architecture: arm64
class: beast
hydrate:
actions: true
githubRunner: false
waitTimeout: 20m
keepAliveMinutes: 90
actions:
workflow: .github/workflows/crabbox.yml
job: hydrate
shell: true
command: >
python3 -m pip install -e '.[dev]' &&
mkdir -p .crabbox-results &&
python3 -m pytest --junitxml=.crabbox-results/pytest.xml
junit:
- .crabbox-results/pytest.xml
downloads:
- .crabbox-results/pytest.xml
stop: always
lint-phase1b:
provider: hetzner
target: linux
architecture: arm64
class: beast
hydrate:
actions: true
githubRunner: false
waitTimeout: 20m
keepAliveMinutes: 90
actions:
workflow: .github/workflows/crabbox.yml
job: hydrate
shell: true
command: >
python3 -m pip install -e '.[dev]' &&
python3 -m ruff check
lib/agent_routing.py
lib/config.py
lib/db.py
lib/evaluate.py
lib/llm.py
lib/post_extract.py
telegram/approvals.py
scripts/prove_phase1b_local.py
tests/test_agent_routing.py
tests/test_evaluate_agent_routing.py
tests/test_phase1b_end_to_end.py
tests/test_eval_parse.py
tests/test_contributor.py
tests/test_search.py
stop: always
phase1b-local-proof:
provider: hetzner
target: linux
architecture: arm64
class: beast
hydrate:
actions: true
githubRunner: false
waitTimeout: 20m
keepAliveMinutes: 90
actions:
workflow: .github/workflows/crabbox.yml
job: hydrate
shell: true
command: >
python3 -m pip install -e '.[dev]' &&
scripts/crabbox_phase1b_proof.sh
junit:
- .crabbox-results/phase1b-pytest.xml
downloads:
- .crabbox-results/crabbox-ci-contract.json
- proof/phase1b-local-e2e-proof.json
- .crabbox-results/phase1b-pytest.xml
- .crabbox-results/phase1b-proof-summary.json
stop: always
sync-smoke:
provider: hetzner
target: linux
architecture: arm64
class: beast
hydrate:
actions: false
shell: true
command: >
python3 -m compileall
lib
tests
scripts/prove_phase1b_local.py
stop: always

View file

@ -1,21 +0,0 @@
.git
.venv
__pycache__
*.pyc
.pytest_cache
.ruff_cache
.mypy_cache
.env
*.env
secrets/
telegram-archives/
transcripts/
logs/
pipeline.db
pipeline.db-*
*.sqlite
*.sqlite3
agentcash/
.agentcash/
.hermes/
node_modules/

View file

@ -1,188 +0,0 @@
name: ci
on:
pull_request:
push:
branches:
- main
workflow_dispatch:
permissions:
contents: read
concurrency:
group: ci-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
PYTHON_VERSION: "3.11"
CI: "1"
jobs:
lint:
name: Focused lint
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install
run: |
python -m pip install --upgrade pip
python -m pip install -e ".[dev]"
- name: Ruff focused surface
run: |
python -m ruff check \
lib/agent_routing.py \
lib/config.py \
lib/db.py \
lib/evaluate.py \
lib/llm.py \
lib/post_extract.py \
ops/apply_gcp_iam_split.py \
ops/capture_vps_canonical_postgres_snapshot.py \
ops/check_gcp_infra_readiness.py \
ops/run_gcp_infra_execute_canary.py \
ops/apply_gcp_runtime_baseline.py \
ops/check_gcp_service_communications.py \
ops/plan_gcp_iam_split.py \
ops/redact_sqlite_postgres_restore_canary.py \
ops/sqlite_to_postgres_dump.py \
ops/verify_gcp_cloudsql_restore_readback.py \
ops/verify_postgres_parity_manifest.py \
telegram/approvals.py \
scripts/check_crabbox_ci_contract.py \
scripts/check_llm_refinement_contract.py \
scripts/build_working_leo_m3taversal_outcome_sandbox.py \
scripts/replay_decision_engine_eval.py \
scripts/prove_phase1b_local.py \
scripts/run_gcp_generated_db_direct_claim_suite.py \
scripts/run_leo_m3taversal_oos_handler_suite.py \
scripts/run_leo_clone_bound_handler_checkpoint.py \
scripts/working_leo_m3taversal_oos_benchmark.py \
scripts/working_leo_open_ended_benchmark.py \
tests/test_agent_routing.py \
tests/test_assemble_telegram_visible_direct_claim_capture_receipt.py \
tests/test_build_working_leo_m3taversal_outcome_sandbox.py \
tests/test_decision_engine_replay.py \
tests/test_evaluate_agent_routing.py \
tests/test_gcp_artifact_workflow.py \
tests/test_capture_vps_canonical_postgres_snapshot.py \
tests/test_gcp_infra_execute_canary.py \
tests/test_gcp_infra_readiness_checker.py \
tests/test_gcp_runtime_baseline_apply.py \
tests/test_gcp_service_communications.py \
tests/test_gcp_cloudsql_restore_drill.py \
tests/test_gcp_cloudsql_restore_readback.py \
tests/test_gcp_iam_split_apply.py \
tests/test_gcp_iam_split_plan.py \
tests/test_gcp_readiness_workflow.py \
tests/test_gcp_generated_db_direct_claim_suite.py \
tests/test_verify_postgres_parity_manifest.py \
tests/test_working_leo_m3taversal_oos_benchmark.py \
tests/test_working_leo_open_ended_benchmark.py \
tests/test_phase1b_end_to_end.py \
tests/test_sqlite_to_postgres_dump.py \
tests/test_sqlite_postgres_restore_canary_capsule.py \
tests/test_eval_parse.py \
tests/test_contributor.py \
tests/test_search.py
- name: Shell syntax
run: |
bash -n \
ops/backup_vps_sqlite_kb.sh \
ops/run_gcp_cloudsql_restore_drill.sh \
ops/run_sqlite_postgres_restore_canary.sh
test:
name: Unit tests
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install
run: |
python -m pip install --upgrade pip
python -m pip install -e ".[dev]"
- name: Pytest
run: |
mkdir -p .crabbox-results
python -m pytest --junitxml=.crabbox-results/pytest.xml
- name: Upload test artifact
if: always()
uses: actions/upload-artifact@v4
with:
name: teleo-infrastructure-pytest
path: .crabbox-results/pytest.xml
if-no-files-found: warn
repo-contracts:
name: Repo contracts
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install
run: |
python -m pip install --upgrade pip
python -m pip install -e ".[dev]"
- name: Validate repo-owned contract
run: |
python scripts/check_crabbox_ci_contract.py \
--output .crabbox-results/crabbox-ci-contract.json
python scripts/check_llm_refinement_contract.py \
--output .crabbox-results/llm-refinement-contract.json
python scripts/replay_decision_engine_eval.py \
--output .crabbox-results/decision-engine-eval.json
- name: Upload contract artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: teleo-infrastructure-repo-contracts
path: |
.crabbox-results/crabbox-ci-contract.json
.crabbox-results/llm-refinement-contract.json
.crabbox-results/decision-engine-eval.json
if-no-files-found: error
phase1b-local-proof:
name: Phase 1B local proof
runs-on: ubuntu-latest
needs:
- lint
- test
- repo-contracts
timeout-minutes: 20
env:
PHASE1B_AGENT_ROUTING_ENABLED: "true"
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install
run: |
python -m pip install --upgrade pip
python -m pip install -e ".[dev]"
- name: Run proof wrapper
run: |
scripts/crabbox_phase1b_proof.sh
- name: Upload proof artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: teleo-infrastructure-phase1b-proof
path: |
.crabbox-results/crabbox-ci-contract.json
proof/phase1b-local-e2e-proof.json
.crabbox-results/phase1b-pytest.xml
.crabbox-results/phase1b-proof-summary.json
if-no-files-found: warn

View file

@ -1,101 +0,0 @@
name: crabbox
on:
workflow_dispatch:
inputs:
ref:
description: "Git ref to hydrate"
required: false
type: string
crabbox_id:
description: "Crabbox lease ID"
required: true
type: string
crabbox_runner_label:
description: "Dynamic Crabbox runner label"
required: true
type: string
crabbox_job:
description: "Hydration job identifier expected by Crabbox"
required: false
default: "hydrate"
type: string
crabbox_keep_alive_minutes:
description: "Minutes to keep the hydrated job alive"
required: false
default: "90"
type: string
permissions:
contents: read
jobs:
hydrate:
runs-on: [self-hosted, "${{ inputs.crabbox_runner_label }}"]
timeout-minutes: 120
steps:
- uses: actions/checkout@v4
with:
ref: ${{ inputs.ref || github.ref }}
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Hydrate
run: |
python -m pip install --upgrade pip
python -m pip install -e ".[dev]"
if [ -f package-lock.json ]; then npm ci; fi
if [ -f pnpm-lock.yaml ]; then corepack enable && pnpm install --frozen-lockfile; fi
if [ -f go.mod ]; then go mod download; fi
- name: Mark Crabbox ready
shell: bash
run: |
job="${{ inputs.crabbox_job }}"
if [ -z "$job" ]; then job=hydrate; fi
mkdir -p "$HOME/.crabbox/actions"
state="$HOME/.crabbox/actions/${{ inputs.crabbox_id }}.env"
env_file="$HOME/.crabbox/actions/${{ inputs.crabbox_id }}.env.sh"
services_file="$HOME/.crabbox/actions/${{ inputs.crabbox_id }}.services"
write_export() {
key="$1"
value="${!key-}"
if [ -n "$value" ]; then
printf 'export %s=%q\n' "$key" "$value"
fi
}
{
for key in CI GITHUB_ACTIONS GITHUB_WORKSPACE GITHUB_REPOSITORY GITHUB_RUN_ID GITHUB_RUN_NUMBER GITHUB_RUN_ATTEMPT GITHUB_REF GITHUB_REF_NAME GITHUB_SHA GITHUB_EVENT_NAME GITHUB_ACTOR GITHUB_JOB RUNNER_OS RUNNER_ARCH RUNNER_TEMP RUNNER_TOOL_CACHE; do
write_export "$key"
done
} > "${env_file}.tmp"
mv "${env_file}.tmp" "$env_file"
{
echo "# Docker containers visible from the hydrated runner"
docker ps --format '{{.Names}}\t{{.Image}}\t{{.Ports}}' 2>/dev/null || true
} > "${services_file}.tmp"
mv "${services_file}.tmp" "$services_file"
tmp="${state}.tmp"
{
echo "WORKSPACE=${GITHUB_WORKSPACE}"
echo "RUN_ID=${GITHUB_RUN_ID}"
echo "JOB=${job}"
echo "ENV_FILE=${env_file}"
echo "SERVICES_FILE=${services_file}"
echo "READY_AT=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
} > "$tmp"
mv "$tmp" "$state"
- name: Keep Crabbox job alive
shell: bash
run: |
minutes="${{ inputs.crabbox_keep_alive_minutes }}"
case "$minutes" in
''|*[!0-9]*) minutes=90 ;;
esac
stop="$HOME/.crabbox/actions/${{ inputs.crabbox_id }}.stop"
deadline=$(( $(date +%s) + minutes * 60 ))
while [ "$(date +%s)" -lt "$deadline" ]; do
if [ -f "$stop" ]; then
exit 0
fi
sleep 15
done

View file

@ -1,86 +0,0 @@
name: gcp-artifact
on:
push:
branches:
- main
workflow_dispatch:
permissions:
contents: read
id-token: write
concurrency:
group: gcp-artifact-${{ github.ref }}
cancel-in-progress: true
env:
PROJECT_ID: teleo-501523
REGION: europe-west6
ARTIFACT_REPOSITORY: teleo
IMAGE_NAME: teleo-pipeline-gcp-staging
WORKLOAD_IDENTITY_PROVIDER: projects/785938879453/locations/global/workloadIdentityPools/github-actions/providers/living-ip-github
ARTIFACT_SERVICE_ACCOUNT: sa-artifact-builder@teleo-501523.iam.gserviceaccount.com
jobs:
build-smoke-push:
name: Build, smoke-run, and push Docker image
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- id: auth
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ env.WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ env.ARTIFACT_SERVICE_ACCOUNT }}
- uses: google-github-actions/setup-gcloud@v2
- name: Configure Artifact Registry Docker auth
run: |
gcloud auth configure-docker "${REGION}-docker.pkg.dev" --quiet
- name: Build, smoke-run, and push
shell: bash
run: |
set -euo pipefail
tag="${GITHUB_SHA::7}"
image_uri="${REGION}-docker.pkg.dev/${PROJECT_ID}/${ARTIFACT_REPOSITORY}/${IMAGE_NAME}:${tag}"
image_digest="$(gcloud artifacts docker images describe "${image_uri}" --format='value(image_summary.digest)' 2>/dev/null || true)"
if [[ -n "${image_digest}" ]]; then
echo "Image tag already exists; reusing immutable Artifact Registry image."
docker pull "${image_uri}@${image_digest}"
docker run --rm "${image_uri}@${image_digest}"
else
docker build \
-f Dockerfile.gcp-staging \
--label "org.opencontainers.image.source=https://github.com/${GITHUB_REPOSITORY}" \
--label "org.opencontainers.image.revision=${GITHUB_SHA}" \
--label "livingip.revision=${GITHUB_SHA}" \
--label "livingip.surface=teleo-infrastructure" \
--label "livingip.tier=gcp-staging" \
-t "${image_uri}" \
.
docker run --rm "${image_uri}"
docker push "${image_uri}" | tee docker-push.log
image_digest="$(awk '/digest: sha256:/ {print $3}' docker-push.log | tail -1)"
fi
test -n "${image_digest}"
{
echo "image_uri=${image_uri}"
echo "image_digest=${image_digest}"
echo "image_ref=${image_uri}@${image_digest}"
echo "revision=${GITHUB_SHA}"
} > gcp-artifact-image.txt
- name: Upload image receipt
uses: actions/upload-artifact@v4
with:
name: gcp-artifact-image
path: gcp-artifact-image.txt
if-no-files-found: error

View file

@ -1,250 +0,0 @@
name: gcp-iap-operator
on:
workflow_dispatch:
inputs:
operation:
description: Fixed reviewed operation
required: true
type: choice
options:
- status
- direct-claim-replay
- cleanup-clone
request_id:
description: iap- followed by 12-32 lowercase letters or digits
required: true
type: string
target_db:
description: teleo_clone_status for status, otherwise the exact generated clone database
required: true
default: teleo_clone_status
type: string
permissions:
contents: read
id-token: write
concurrency:
group: gcp-iap-operator-${{ github.run_id }}
cancel-in-progress: false
env:
PROJECT_ID: teleo-501523
WORKLOAD_IDENTITY_PROVIDER: projects/785938879453/locations/global/workloadIdentityPools/github-actions/providers/teleo-iap-operator
STATUS_SERVICE_ACCOUNT: sa-teleo-iap-status@teleo-501523.iam.gserviceaccount.com
CLONE_SERVICE_ACCOUNT: sa-teleo-iap-clone-operator@teleo-501523.iam.gserviceaccount.com
EXPECTED_WORKFLOW_REF: living-ip/teleo-infrastructure/.github/workflows/gcp-iap-operator.yml@refs/heads/main
DISPATCHER_VERSION: teleo-gcp-iap-operator-v1
jobs:
operate:
name: Fixed IAP operator command
runs-on: ubuntu-latest
timeout-minutes: 45
steps:
- name: Check out the dispatched main revision
uses: actions/checkout@v4
- name: Initialize runner-local paths
id: paths
shell: bash
run: |
set -euo pipefail
[[ -n "${RUNNER_TEMP:-}" ]] || exit 2
printf 'BUNDLE_DIR=%s\n' "${RUNNER_TEMP}/gcp-iap-operator-bundle" >> "${GITHUB_ENV}"
printf 'RESULT_DIR=%s\n' "${RUNNER_TEMP}/gcp-iap-operator-result" >> "${GITHUB_ENV}"
- name: Validate fixed inputs and select identity
id: validate
shell: bash
env:
OPERATION: ${{ inputs.operation }}
REQUEST_ID: ${{ inputs.request_id }}
TARGET_DB: ${{ inputs.target_db }}
run: |
set -euo pipefail
[[ "${GITHUB_REF}" == "refs/heads/main" ]] || exit 2
[[ "${GITHUB_WORKFLOW_REF}" == "${EXPECTED_WORKFLOW_REF}" ]] || exit 2
request_id_re='^iap-[a-z0-9]{12,32}$'
target_db_re='^teleo_clone_[a-z0-9][a-z0-9_]{0,50}$'
[[ "${REQUEST_ID}" =~ ${request_id_re} ]] || exit 2
[[ "${TARGET_DB}" =~ ${target_db_re} ]] || exit 2
case "${OPERATION}" in
status)
[[ "${TARGET_DB}" == "teleo_clone_status" ]] || exit 2
service_account="${STATUS_SERVICE_ACCOUNT}"
;;
direct-claim-replay|cleanup-clone)
[[ "${TARGET_DB}" != "teleo_clone_status" ]] || exit 2
service_account="${CLONE_SERVICE_ACCOUNT}"
;;
*) exit 2 ;;
esac
printf 'service_account=%s\n' "${service_account}" >> "${GITHUB_OUTPUT}"
install -d -m 0700 "${RESULT_DIR}"
: > "${RESULT_DIR}/result.json"
chmod 0600 "${RESULT_DIR}/result.json"
- name: Build strict reviewed operation bundle
if: ${{ inputs.operation != 'status' }}
shell: bash
env:
OPERATION: ${{ inputs.operation }}
REQUEST_ID: ${{ inputs.request_id }}
TARGET_DB: ${{ inputs.target_db }}
run: |
set -euo pipefail
python3 - <<'PY'
import gzip
import hashlib
import io
import json
import os
import subprocess
import tarfile
from pathlib import Path
root = Path.cwd().resolve()
operation = os.environ["OPERATION"]
request_id = os.environ["REQUEST_ID"]
target_db = os.environ["TARGET_DB"]
common = ["scripts/gcp_iap_operator.sh"]
direct_claim = [
"scripts/run_gcp_generated_db_direct_claim_suite.py",
"scripts/run_leo_clone_bound_handler_checkpoint.py",
"scripts/working_leo_open_ended_benchmark.py",
"hermes-agent/leoclean-bin/cloudsql_memory_tool.py",
"ops/postgres_parity_manifest.sql",
f"docs/reports/leo-working-state-20260709/{target_db}-canonical-parity-receipt.json",
]
allowed = common + (direct_claim if operation == "direct-claim-replay" else [])
if operation not in {"direct-claim-replay", "cleanup-clone"}:
raise SystemExit("bundle requested for a non-clone operation")
head = subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip()
if head != os.environ["GITHUB_SHA"]:
raise SystemExit("checkout HEAD does not match GITHUB_SHA")
files = {}
payloads = {}
for relative in allowed:
subprocess.run(
["git", "ls-files", "--error-unmatch", "--", relative],
check=True,
stdout=subprocess.DEVNULL,
)
path = root / relative
if not path.is_file() or path.is_symlink() or root not in path.resolve().parents:
raise SystemExit(f"unsafe or missing tracked bundle file: {relative}")
data = path.read_bytes()
payloads[relative] = data
files[relative] = {"sha256": hashlib.sha256(data).hexdigest(), "size": len(data)}
manifest = {
"schema": "livingip.gcpIapOperatorBundle.v1",
"dispatcher_version": os.environ["DISPATCHER_VERSION"],
"operation": operation,
"request_id": request_id,
"target_db": target_db,
"git_commit": head,
"ref": os.environ["GITHUB_REF"],
"workflow_ref": os.environ["GITHUB_WORKFLOW_REF"],
"files": files,
}
manifest_bytes = (json.dumps(manifest, indent=2, sort_keys=True) + "\n").encode()
bundle_dir = Path(os.environ["BUNDLE_DIR"])
bundle_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
os.chmod(bundle_dir, 0o700)
bundle_path = bundle_dir / f"{request_id}.tar.gz"
with bundle_path.open("xb") as raw:
with gzip.GzipFile(fileobj=raw, mode="wb", mtime=0) as compressed:
with tarfile.open(fileobj=compressed, mode="w", format=tarfile.PAX_FORMAT) as archive:
for relative, data in sorted({**payloads, "bundle-manifest.json": manifest_bytes}.items()):
info = tarfile.TarInfo(relative)
info.size = len(data)
info.mode = 0o600
info.mtime = 0
info.uid = info.gid = 0
info.uname = info.gname = "root"
archive.addfile(info, io.BytesIO(data))
os.chmod(bundle_path, 0o600)
PY
- name: Authenticate with short-lived workload identity federation
id: auth
uses: google-github-actions/auth@v3
with:
workload_identity_provider: ${{ env.WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ steps.validate.outputs.service_account }}
create_credentials_file: true
cleanup_credentials: true
export_environment_variables: true
- name: Install Google Cloud CLI
id: setup_gcloud
uses: google-github-actions/setup-gcloud@v3
with:
project_id: ${{ env.PROJECT_ID }}
- name: Run fixed IAP operation
id: operate
shell: bash
env:
OPERATION: ${{ inputs.operation }}
REQUEST_ID: ${{ inputs.request_id }}
TARGET_DB: ${{ inputs.target_db }}
run: |
set -euo pipefail
scripts/gcp_iap_operator.sh "${OPERATION}" "${REQUEST_ID}" "${TARGET_DB}"
- name: Ensure sanitized result receipt
if: always()
shell: bash
env:
OPERATION: ${{ inputs.operation }}
REQUEST_ID: ${{ inputs.request_id }}
TARGET_DB: ${{ inputs.target_db }}
VALIDATE_OUTCOME: ${{ steps.validate.outcome }}
AUTH_OUTCOME: ${{ steps.auth.outcome }}
SETUP_GCLOUD_OUTCOME: ${{ steps.setup_gcloud.outcome }}
OPERATE_OUTCOME: ${{ steps.operate.outcome }}
run: |
set -euo pipefail
install -d -m 0700 "${RESULT_DIR}"
result_path="${RESULT_DIR}/result.json"
if [[ -s "${result_path}" ]] && python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "${result_path}"; then
exit 0
fi
RESULT_PATH="${result_path}" python3 - <<'PY'
import json
import os
from pathlib import Path
result = {
"schema": "livingip.gcpIapOperatorResult.v1",
"operation": os.environ["OPERATION"],
"request_id": os.environ["REQUEST_ID"],
"target_db": os.environ["TARGET_DB"],
"status": "fail",
"failure_class": "workflow_stopped_before_valid_operator_result",
"step_outcomes": {
"validate": os.environ["VALIDATE_OUTCOME"],
"auth": os.environ["AUTH_OUTCOME"],
"setup_gcloud": os.environ["SETUP_GCLOUD_OUTCOME"],
"operate": os.environ["OPERATE_OUTCOME"],
},
"credential_values_logged": False,
"raw_stdout_retained": False,
"raw_stderr_retained": False,
}
path = Path(os.environ["RESULT_PATH"])
path.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8")
path.chmod(0o600)
PY
- name: Upload sanitized result receipt
if: always()
uses: actions/upload-artifact@v4
with:
name: gcp-iap-operator-${{ github.run_id }}
path: ${{ env.RESULT_DIR }}/result.json
if-no-files-found: error
retention-days: 7

View file

@ -1,132 +0,0 @@
name: gcp-readiness
on:
workflow_dispatch:
inputs:
service_account:
description: GCP service account to impersonate for the read-only readiness probe
required: false
default: sa-teleo-readiness@teleo-501523.iam.gserviceaccount.com
restore_canary_capsule_b64:
description: Optional base64-encoded non-secret SQLite-to-Postgres restore canary capsule
required: false
default: ""
permissions:
contents: read
id-token: write
concurrency:
group: gcp-readiness-${{ github.ref }}
cancel-in-progress: true
env:
PROJECT_ID: teleo-501523
WORKLOAD_IDENTITY_PROVIDER: projects/785938879453/locations/global/workloadIdentityPools/github-actions/providers/living-ip-github
READINESS_SERVICE_ACCOUNT: ${{ inputs.service_account || 'sa-teleo-readiness@teleo-501523.iam.gserviceaccount.com' }}
RESTORE_CANARY_CAPSULE_B64: ${{ inputs.restore_canary_capsule_b64 || '' }}
READINESS_ARTIFACT_DIR: gcp-readiness-artifacts
jobs:
readiness:
name: Read-only GCP readiness probe
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- id: auth
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ env.WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ env.READINESS_SERVICE_ACCOUNT }}
- uses: google-github-actions/setup-gcloud@v2
- name: Install optional restore canary capsule
if: ${{ env.RESTORE_CANARY_CAPSULE_B64 != '' }}
shell: bash
run: |
set -euo pipefail
mkdir -p "${READINESS_ARTIFACT_DIR}"
capsule_path="${READINESS_ARTIFACT_DIR}/restore-canary-capsule.json"
printf '%s' "${RESTORE_CANARY_CAPSULE_B64}" | base64 --decode > "${capsule_path}"
python3 -m json.tool "${capsule_path}" >/dev/null
echo "TELEO_GCP_RESTORE_CANARY_CAPSULE=${capsule_path}" >> "${GITHUB_ENV}"
- name: Capture gcloud identity
shell: bash
run: |
set +e
mkdir -p "${READINESS_ARTIFACT_DIR}"
{
echo "project=${PROJECT_ID}"
echo "service_account=${READINESS_SERVICE_ACCOUNT}"
echo "workflow_ref=${GITHUB_REF}"
echo "revision=${GITHUB_SHA}"
} > "${READINESS_ARTIFACT_DIR}/context.txt"
gcloud auth list --format=json > "${READINESS_ARTIFACT_DIR}/gcloud-auth-list.json" 2> "${READINESS_ARTIFACT_DIR}/gcloud-auth-list.stderr"
echo "$?" > "${READINESS_ARTIFACT_DIR}/gcloud-auth-list.exitcode"
gcloud config list --format=json > "${READINESS_ARTIFACT_DIR}/gcloud-config-list.json" 2> "${READINESS_ARTIFACT_DIR}/gcloud-config-list.stderr"
echo "$?" > "${READINESS_ARTIFACT_DIR}/gcloud-config-list.exitcode"
- name: Run readiness checker
shell: bash
run: |
set +e
mkdir -p "${READINESS_ARTIFACT_DIR}"
python3 ops/check_gcp_infra_readiness.py \
> "${READINESS_ARTIFACT_DIR}/gcp-infra-readiness.json" \
2> "${READINESS_ARTIFACT_DIR}/gcp-infra-readiness.stderr"
echo "$?" > "${READINESS_ARTIFACT_DIR}/gcp-infra-readiness.exitcode"
python3 - <<'PY'
import os
import json
from pathlib import Path
base = Path(os.environ["READINESS_ARTIFACT_DIR"])
report = {
"artifact": "github_wif_gcp_readiness_probe",
"checker_exitcode": (base / "gcp-infra-readiness.exitcode").read_text().strip(),
"stderr_tail": (base / "gcp-infra-readiness.stderr").read_text()[-2000:],
}
try:
payload = json.loads((base / "gcp-infra-readiness.json").read_text() or "{}")
except json.JSONDecodeError as exc:
report["json_parse_error"] = str(exc)
payload = {}
if payload:
report["pass_count"] = payload.get("pass_count")
report["blocked_count"] = payload.get("blocked_count")
report["fail_count"] = payload.get("fail_count")
report["checks"] = [
{
"name": check.get("name"),
"status": check.get("status"),
"required_tier": check.get("required_tier"),
"current_tier": check.get("current_tier"),
"detail": (check.get("detail") or "")[:500],
}
for check in payload.get("checks", [])
]
(base / "gcp-readiness-summary.json").write_text(json.dumps(report, indent=2, sort_keys=True) + "\n")
print(json.dumps(report, indent=2, sort_keys=True))
PY
- name: Upload readiness artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: gcp-readiness
path: ${{ env.READINESS_ARTIFACT_DIR }}/
if-no-files-found: error
- name: Enforce readiness result
if: always()
shell: bash
run: |
code="$(cat "${READINESS_ARTIFACT_DIR}/gcp-infra-readiness.exitcode" 2>/dev/null || echo 1)"
if [ "${code}" != "0" ]; then
echo "GCP readiness checker failed with exit code ${code}. See the uploaded gcp-readiness artifact for exact failed checks."
exit "${code}"
fi

6
.gitignore vendored
View file

@ -10,7 +10,6 @@ __pycache__/
# Secrets (never commit)
secrets/
gha-creds-*.json
# Logs
logs/
@ -21,8 +20,6 @@ logs/
# Test artifacts
.pytest_cache/
.crabbox/
.crabbox-results/
htmlcov/
.coverage
@ -33,6 +30,3 @@ build/
# OS
.DS_Store
# Hermes session artifacts
ops/sessions/

View file

@ -1,79 +0,0 @@
# teleo-infrastructure ownership map
# Each path has ONE owning agent. Owner = accountable for correctness + reviews changes.
# Format: <pattern> <owner>
# Pipeline daemon — entry points
/teleo-pipeline.py @ship
/reweave.py @ship
# Pipeline library — shared Python package
/lib/config.py @ship
/lib/db.py @ship
/lib/connect.py @ship
/lib/log.py @ship
/lib/forgejo.py @ship
/lib/breaker.py @ship
/lib/worktree_lock.py @ship
/lib/domains.py @ship
/lib/costs.py @ship
/lib/llm.py @ship
/lib/merge.py @ship
/lib/cascade.py @ship
/lib/cross_domain.py @ship
/lib/validate.py @ship
/lib/stale_pr.py @ship
/lib/watchdog.py @ship
/lib/feedback.py @ship
/lib/fixer.py @ship
/lib/substantive_fixer.py @ship
/lib/dedup.py @ship
/lib/extract.py @epimetheus
/lib/extraction_prompt.py @epimetheus
/lib/post_extract.py @epimetheus
/lib/pre_screen.py @epimetheus
/lib/entity_batch.py @epimetheus
/lib/entity_queue.py @epimetheus
/lib/evaluate.py @leo
/lib/analytics.py @leo
/lib/attribution.py @leo
/lib/health.py @argus
/lib/search.py @argus
/lib/claim_index.py @argus
/lib/digest.py @argus
# Diagnostics — monitoring dashboard
/diagnostics/ @argus
# Telegram bot
/telegram/ @ship
# Deployment automation
/deploy/ @ship
# Systemd service definitions
/systemd/ @ship
# Agent state management
/agent-state/ @ship
# Research orchestration
/research/ @ship
# Hermes agent
/hermes-agent/ @ship
# One-off scripts and migrations
/scripts/ @ship
# Test suite
/tests/ @ganymede
# Documentation
/docs/ shared
# Config
/pyproject.toml @ship
/.gitignore @ship

View file

@ -1,30 +0,0 @@
FROM python:3.11-slim
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PIPELINE_BASE=/opt/teleo-eval
WORKDIR /app
RUN apt-get update \
&& apt-get install -y --no-install-recommends ca-certificates curl git bash \
&& rm -rf /var/lib/apt/lists/*
COPY pyproject.toml README.md /app/
COPY lib /app/lib
COPY tests /app/tests
COPY scripts /app/scripts
COPY fixtures /app/fixtures
COPY schemas /app/schemas
COPY systemd /app/systemd
COPY deploy /app/deploy
COPY diagnostics /app/diagnostics
COPY telegram /app/telegram
COPY teleo-pipeline.py /app/teleo-pipeline.py
COPY docker/gcp-staging-smoke.sh /usr/local/bin/gcp-staging-smoke
RUN pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir -e ".[dev]" \
&& chmod +x /usr/local/bin/gcp-staging-smoke
CMD ["/usr/local/bin/gcp-staging-smoke"]

134
README.md
View file

@ -1,134 +0,0 @@
# teleo-infrastructure
This repo runs the pipeline that processes contributions into the
[teleo-codex](https://github.com/living-ip/teleo-codex) knowledge base.
Every claim on `main` has been extracted from a source, validated for schema
and duplicates, evaluated by at least two independent reviewers, and merged
through an event-sourced audit log. The whole flow is an async Python daemon
talking to a Forgejo git server, an SQLite WAL state store, OpenRouter (for
most LLM calls), and the Anthropic Claude CLI (for Opus deep reviews).
**Production state** (live):
| Metric | Value |
|---|---|
| Claims merged into `main` | 1,546 across 13 domains |
| PRs merged through the pipeline | 1,975 |
| Merge throughput (last 7d) | 508 PRs (~73/day) |
| Review approval rate | 94% |
| Cost per merged claim (last 30d) | $0.10 incl. extract + triage + multi-tier review |
| Production agents | 6 (rio, theseus, leo, vida, astra, clay) |
## Pipeline
Concurrent stage loops in a single daemon (`teleo-pipeline.py`), coordinated
by SQLite. Circuit breakers cap costs, retry budgets cap attempts, and merges
are serialized per-domain to avoid cross-PR conflicts.
```mermaid
flowchart LR
Inbox["inbox/queue/"] --> Extract
Extract["Extract<br/>(Sonnet 4.5)"] --> Validate
Validate["Validate<br/>(tier 0, $0)"] --> Evaluate
Evaluate["Evaluate<br/>(tiered, multi-model)"] --> Merge
Merge["Merge<br/>(Forgejo, domain-serial)"] --> Effects
Effects["Effects<br/>cascade · backlinks · reciprocal edges"]
```
If any reviewer rejects, the PR gets a structured rationale and either
re-extraction guidance (for fixable issues) or a terminal close (for
scope or duplicate problems). Approved merges trigger downstream effects:
- **Cascade** — agents whose beliefs/positions depend on the changed claim get inbox notifications
- **Bidirectional provenance**`sourced_from:` is stamped on each claim at extraction; the source's `claims_extracted:` list is updated post-merge
- **Reciprocal edges** — when a new claim has `supports: [X]`, X's frontmatter is updated with `supports: [new]`
- **Cross-domain index** — entity mentions across domain boundaries are logged for silo detection
## Multi-agent review
Reviews aren't free. Tier classification is deterministic where possible
(changes to `core/` or `foundations/` always go Deep) and otherwise picked
by Haiku based on PR scope. Last 30d distribution: 76% Standard, 21% Light,
2% Deep.
```mermaid
flowchart TD
PR[New PR] --> Classify{Classify}
Classify -->|"core/, foundations/, challenged"| Deep
Classify -->|default| Standard
Classify -->|single claim, low risk| Light
Light["Light tier<br/>Domain agent only"] --> Result
Standard["Standard tier<br/>Domain agent + Leo (Sonnet 4.5)"] --> Result
Deep["Deep tier<br/>Domain agent + Leo (Opus)"] --> Result
Result{Both approve?}
Result -->|yes| MergeOK[Merge]
Result -->|no| Reject[Structured rejection<br/>+ re-extract guidance]
```
Domain agents bring domain expertise: **Rio** (internet-finance), **Vida**
(health), **Astra** (space-development), **Clay** (entertainment),
**Theseus** (ai-alignment). **Leo** brings cross-domain consistency on
every PR. Disagreement between the two reviewers surfaces in `audit_log`
and is tracked as a quality signal, not silenced.
Model diversity isn't cosmetic — same-family models share ~60% of their
errors (Kim et al. ICML 2025). Pipeline mixes Haiku for triage, Gemini 2.5
Flash for domain review, Sonnet 4.5 for Leo standard, Opus for Leo deep.
## Contributor flow
External contributors submit PRs to
[`living-ip/teleo-codex`](https://github.com/living-ip/teleo-codex) on GitHub.
A mirror sync (every 2 minutes) fast-forwards the PR onto Forgejo, where
the pipeline picks it up. From there it's the same flow as agent-authored
PRs — same tiers, same reviewers, same merge rules.
The contributor-facing guide lives in
[`teleo-codex/CONTRIBUTING.md`](https://github.com/living-ip/teleo-codex/blob/main/CONTRIBUTING.md).
## Repository layout
| Directory | What it does |
|-----------------|-----------------------------------------------------------|
| `lib/` | Pipeline modules — config, db, extract, evaluate, merge, cascade |
| `diagnostics/` | Argus monitoring dashboard (4 pages: ops, health, agents, epistemic) |
| `telegram/` | Telegram bot that answers from the knowledge base |
| `research/` | Nightly autonomous research sessions for domain agents |
| `agent-state/` | File-backed state for cross-session agent continuity |
| `deploy/` | Auto-deploy pipeline (Forgejo → working dirs → systemd) |
| `systemd/` | Service definitions for daemon + dashboard + agents |
| `scripts/` | Backfills and one-off migrations |
| `tests/` | pytest suite |
| `docs/` | Architecture specs and operational protocols |
## Ownership
Code review authority is enforced by [`CODEOWNERS`](./CODEOWNERS) — every
file has one accountable agent. The high-level map:
- **Ship** — pipeline core, telegram, deploy, agent-state, research, systemd
- **Epimetheus** — extraction (intake, entity processing, pre-screening, post-extract validation)
- **Leo** — evaluation (claim review, analytics, attribution)
- **Argus** — health (diagnostics dashboard, alerting, claim index, search)
- **Ganymede** — tests (pytest suite, integration, code review gate)
For active sprint work and per-agent in-flight items, see each agent's
status report in their Pentagon profile.
## Development
```bash
pip install -e ".[dev]"
pytest
```
## Operations
Production deployment runs on a single VPS. Runbook, restart procedures,
secret rotation, and on-call live in the private
[`teleo-ops`](https://github.com/living-ip/teleo-ops) repo (request access).
## License
[TBD]

View file

@ -104,17 +104,9 @@ def main():
claims_count = 0
if rel_path in existing:
# Update status if different — but never regress from terminal states.
# If DB says 'extracted' or 'null_result' and file happens to be in queue/
# (e.g., failed archive push, zombie file), the DB is authoritative.
# Downgrading to 'unprocessed' triggers the runaway re-extraction loop.
# Update status if different
current = conn.execute("SELECT status FROM sources WHERE path = ?", (rel_path,)).fetchone()
TERMINAL_STATUSES = {"extracted", "null_result", "error", "ghost_no_file"}
if current and current["status"] != status:
if current["status"] in TERMINAL_STATUSES and status == "unprocessed":
# Don't regress terminal → unprocessed. DB wins.
pass
else:
conn.execute(
"UPDATE sources SET status = ?, updated_at = datetime('now') WHERE path = ?",
(status, rel_path),

283
batch-extract-50.sh Executable file
View file

@ -0,0 +1,283 @@
#!/bin/bash
# Batch extract sources from inbox/queue/ — v3 with two-gate skip logic
#
# Uses separate extract/ worktree (not main/ — prevents daemon race condition).
# Skip logic uses two checks instead of local marker files (Ganymede v3 review):
# Gate 1: Is source already in archive/{domain}/? → already processed, dedup
# Gate 2: Does extraction branch exist on Forgejo? → extraction in progress
# Gate 3: Does pipeline.db show ≥3 closed PRs for this source? → zombie, skip
# Gate 4: Does pipeline.db show active OR recently closed PR? → skip (4h cooldown)
# All gates pass → extract
#
# Architecture: Ganymede (two-gate) + Rhea (separate worktrees)
REPO=/opt/teleo-eval/workspaces/extract
MAIN_REPO=/opt/teleo-eval/workspaces/main
EXTRACT=/opt/teleo-eval/openrouter-extract-v2.py
CLEANUP=/opt/teleo-eval/post-extract-cleanup.py
LOG=/opt/teleo-eval/logs/batch-extract-50.log
DB=/opt/teleo-eval/pipeline/pipeline.db
TOKEN=$(cat /opt/teleo-eval/secrets/forgejo-leo-token)
FORGEJO_URL="http://localhost:3000"
MAX=50
MAX_CLOSED=3 # zombie retry limit: skip source after this many closed PRs
COUNT=0
SUCCESS=0
FAILED=0
SKIPPED=0
# Lockfile to prevent concurrent runs
LOCKFILE="/tmp/batch-extract.lock"
if [ -f "$LOCKFILE" ]; then
pid=$(cat "$LOCKFILE" 2>/dev/null)
if kill -0 "$pid" 2>/dev/null; then
echo "[$(date)] SKIP: batch extract already running (pid $pid)" >> $LOG
exit 0
fi
rm -f "$LOCKFILE"
fi
echo $$ > "$LOCKFILE"
trap 'rm -f "$LOCKFILE"' EXIT
echo "[$(date)] Starting batch extraction of $MAX sources" >> $LOG
cd $REPO || exit 1
# Bug fix: don't swallow errors on critical git commands (Ganymede review)
git fetch origin main >> $LOG 2>&1 || { echo "[$(date)] FATAL: fetch origin main failed" >> $LOG; exit 1; }
git checkout -f main >> $LOG 2>&1 || { echo "[$(date)] FATAL: checkout main failed" >> $LOG; exit 1; }
git reset --hard origin/main >> $LOG 2>&1 || { echo "[$(date)] FATAL: reset --hard failed" >> $LOG; exit 1; }
# SHA canary: verify extract worktree matches origin/main (Ganymede review)
LOCAL_SHA=$(git rev-parse HEAD)
REMOTE_SHA=$(git rev-parse origin/main)
if [ "$LOCAL_SHA" != "$REMOTE_SHA" ]; then
echo "[$(date)] FATAL: extract worktree diverged from main ($LOCAL_SHA vs $REMOTE_SHA)" >> $LOG
exit 1
fi
# Pre-extraction cleanup: remove queue files that already exist in archive
# This runs on the MAIN worktree (not extract/) so deletions are committed to git.
# Prevents the "queue duplicate reappears after reset --hard" problem.
CLEANED=0
for qfile in $MAIN_REPO/inbox/queue/*.md; do
[ -f "$qfile" ] || continue
qbase=$(basename "$qfile")
if find "$MAIN_REPO/inbox/archive" -name "$qbase" 2>/dev/null | grep -q .; then
rm -f "$qfile"
CLEANED=$((CLEANED + 1))
fi
done
if [ "$CLEANED" -gt 0 ]; then
echo "[$(date)] Cleaned $CLEANED stale queue duplicates" >> $LOG
cd $MAIN_REPO
git add -A inbox/queue/ 2>/dev/null
git commit -m "pipeline: clean $CLEANED stale queue duplicates
Pentagon-Agent: Epimetheus <3D35839A-7722-4740-B93D-51157F7D5E70>" 2>/dev/null
# Push with retry
for attempt in 1 2 3; do
git pull --rebase origin main 2>/dev/null
git push origin main 2>/dev/null && break
sleep 2
done
cd $REPO
git fetch origin main 2>/dev/null
git reset --hard origin/main 2>/dev/null
fi
# Get sources in queue
SOURCES=$(ls inbox/queue/*.md 2>/dev/null | head -$MAX)
# Batch fetch all remote branches once (Ganymede: 1 call instead of 84)
REMOTE_BRANCHES=$(git ls-remote --heads origin 2>/dev/null)
if [ $? -ne 0 ]; then
echo "[$(date)] ABORT: git ls-remote failed — remote unreachable, skipping cycle" >> $LOG
exit 0
fi
for SOURCE in $SOURCES; do
COUNT=$((COUNT + 1))
BASENAME=$(basename "$SOURCE" .md)
BRANCH="extract/$BASENAME"
# Skip conversation archives — valuable content enters through standalone sources,
# inline tags (SOURCE:/CLAIM:), and transcript review. Raw conversations produce
# low-quality claims with schema failures. (Epimetheus session 4)
if grep -q "^format: conversation" "$SOURCE" 2>/dev/null; then
# Move to archive instead of leaving in queue (prevents re-processing)
mv "$SOURCE" "$MAIN_REPO/inbox/archive/telegram/" 2>/dev/null
echo "[$(date)] [$COUNT/$MAX] ARCHIVE $BASENAME (conversation — skipped extraction)" >> $LOG
SKIPPED=$((SKIPPED + 1))
continue
fi
# Gate 1: Already in archive? Source was already processed — dedup (Ganymede)
if find "$MAIN_REPO/inbox/archive" -name "$BASENAME.md" 2>/dev/null | grep -q .; then
echo "[$(date)] [$COUNT/$MAX] SKIP $BASENAME (already in archive)" >> $LOG
# Delete the queue duplicate
rm -f "$MAIN_REPO/inbox/queue/$BASENAME.md" 2>/dev/null
SKIPPED=$((SKIPPED + 1))
continue
fi
# Gate 2: Branch exists on Forgejo? Extraction already in progress (cached lookup)
# Enhancement: 2-hour staleness check (Ganymede review) — if branch is >2h old
# and PR is unmergeable, close PR + delete branch and re-extract
if echo "$REMOTE_BRANCHES" | grep -q "refs/heads/$BRANCH$"; then
# Check branch age
BRANCH_SHA=$(echo "$REMOTE_BRANCHES" | grep "refs/heads/$BRANCH$" | awk '{print $1}')
BRANCH_AGE_EPOCH=$(git log -1 --format='%ct' "$BRANCH_SHA" 2>/dev/null || echo 0)
NOW_EPOCH=$(date +%s)
AGE_HOURS=$(( (NOW_EPOCH - BRANCH_AGE_EPOCH) / 3600 ))
if [ "$AGE_HOURS" -ge 2 ]; then
# Branch is stale — check if PR is mergeable
# Note: Forgejo head= filter is unreliable. Fetch all open PRs and filter locally.
PR_NUM=$(curl -sf "$FORGEJO_URL/api/v1/repos/teleo/teleo-codex/pulls?state=open&limit=50" \
-H "Authorization: token $TOKEN" | python3 -c "
import sys,json
prs=json.load(sys.stdin)
branch='$BRANCH'
matches=[p for p in prs if p['head']['ref']==branch]
print(matches[0]['number'] if matches else '')
" 2>/dev/null)
if [ -n "$PR_NUM" ]; then
PR_MERGEABLE=$(curl -sf "$FORGEJO_URL/api/v1/repos/teleo/teleo-codex/pulls/$PR_NUM" \
-H "Authorization: token $TOKEN" | python3 -c 'import sys,json; print(json.load(sys.stdin).get("mergeable","true"))' 2>/dev/null)
if [ "$PR_MERGEABLE" = "False" ] || [ "$PR_MERGEABLE" = "false" ]; then
echo "[$(date)] [$COUNT/$MAX] STALE: $BASENAME (${AGE_HOURS}h old, unmergeable PR #$PR_NUM) — closing + re-extracting" >> $LOG
# Close PR with audit comment
curl -sf -X POST "$FORGEJO_URL/api/v1/repos/teleo/teleo-codex/issues/$PR_NUM/comments" \
-H "Authorization: token $TOKEN" -H "Content-Type: application/json" \
-d '{"body":"Auto-closed: extraction branch stale >2h, conflict unresolvable. Source will be re-extracted from current main."}' > /dev/null 2>&1
curl -sf -X PATCH "$FORGEJO_URL/api/v1/repos/teleo/teleo-codex/pulls/$PR_NUM" \
-H "Authorization: token $TOKEN" -H "Content-Type: application/json" \
-d '{"state":"closed"}' > /dev/null 2>&1
# Delete remote branch
git push origin --delete "$BRANCH" 2>/dev/null
# Fall through to extraction below
else
echo "[$(date)] [$COUNT/$MAX] SKIP $BASENAME (branch exists ${AGE_HOURS}h, PR #$PR_NUM mergeable — waiting)" >> $LOG
SKIPPED=$((SKIPPED + 1))
continue
fi
else
# No PR found but branch exists — orphan branch, clean up
echo "[$(date)] [$COUNT/$MAX] STALE: $BASENAME (orphan branch ${AGE_HOURS}h, no PR) — deleting" >> $LOG
git push origin --delete "$BRANCH" 2>/dev/null
# Fall through to extraction
fi
else
echo "[$(date)] [$COUNT/$MAX] SKIP $BASENAME (branch exists — in progress, ${AGE_HOURS}h old)" >> $LOG
SKIPPED=$((SKIPPED + 1))
continue
fi
fi
# Gate 3: Check pipeline.db for zombie sources — too many closed PRs means
# the source keeps failing eval. Skip after MAX_CLOSED rejections. (Epimetheus)
if [ -f "$DB" ]; then
CLOSED_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM prs WHERE branch = 'extract/$BASENAME' AND status = 'closed'" 2>/dev/null || echo 0)
if [ "$CLOSED_COUNT" -ge "$MAX_CLOSED" ]; then
echo "[$(date)] [$COUNT/$MAX] SKIP $BASENAME (zombie: $CLOSED_COUNT closed PRs >= $MAX_CLOSED limit)" >> $LOG
SKIPPED=$((SKIPPED + 1))
continue
fi
fi
# Gate 4: Check pipeline.db for active or recently closed PRs — prevents
# re-extraction waste when eval closes a PR and batch-extract runs again
# before the source is manually reviewed. 4h cooldown after closure.
if [ -f "$DB" ]; then
ACTIVE_COUNT=$(sqlite3 "$DB" "SELECT COUNT(*) FROM prs WHERE branch = 'extract/$BASENAME' AND status IN ('extracting','approved','merging')" 2>/dev/null || echo 0)
if [ "$ACTIVE_COUNT" -ge 1 ]; then
echo "[$(date)] [$COUNT/$MAX] SKIP $BASENAME (active PR exists)" >> $LOG
SKIPPED=$((SKIPPED + 1))
continue
fi
RECENT_CLOSED=$(sqlite3 "$DB" "SELECT COUNT(*) FROM prs WHERE branch = 'extract/$BASENAME' AND status = 'closed' AND created_at > datetime('now', '-4 hours')" 2>/dev/null || echo 0)
if [ "$RECENT_CLOSED" -ge 1 ]; then
echo "[$(date)] [$COUNT/$MAX] SKIP $BASENAME (recently closed PR — 4h cooldown)" >> $LOG
SKIPPED=$((SKIPPED + 1))
continue
fi
fi
echo "[$(date)] [$COUNT/$MAX] Processing $BASENAME" >> $LOG
# Reset to main (log errors — don't swallow)
git checkout -f main >> $LOG 2>&1 || { echo " -> SKIP (checkout main failed)" >> $LOG; SKIPPED=$((SKIPPED + 1)); continue; }
git fetch origin main >> $LOG 2>&1
git reset --hard origin/main >> $LOG 2>&1 || { echo " -> SKIP (reset failed)" >> $LOG; SKIPPED=$((SKIPPED + 1)); continue; }
# Clean stale remote branch (Leo's catch — prevents checkout conflicts)
git push origin --delete "$BRANCH" 2>/dev/null
# Create fresh branch
git branch -D "$BRANCH" 2>/dev/null
git checkout -b "$BRANCH" 2>/dev/null
if [ $? -ne 0 ]; then
echo " -> SKIP (branch creation failed)" >> $LOG
SKIPPED=$((SKIPPED + 1))
continue
fi
# Run extraction
python3 $EXTRACT "$SOURCE" --no-review >> $LOG 2>&1
EXTRACT_RC=$?
if [ $EXTRACT_RC -ne 0 ]; then
FAILED=$((FAILED + 1))
echo " -> FAILED (extract rc=$EXTRACT_RC)" >> $LOG
continue
fi
# Post-extraction cleanup
python3 $CLEANUP $REPO >> $LOG 2>&1
# Check if any files were created/modified
CHANGED=$(git status --porcelain | wc -l | tr -d " ")
if [ "$CHANGED" -eq 0 ]; then
echo " -> No changes (enrichment/null-result only)" >> $LOG
continue
fi
# Commit
git add -A
git commit -m "extract: $BASENAME
Pentagon-Agent: Epimetheus <3D35839A-7722-4740-B93D-51157F7D5E70>" >> $LOG 2>&1
# Push
git push "http://leo:${TOKEN}@localhost:3000/teleo/teleo-codex.git" "$BRANCH" --force >> $LOG 2>&1
# Create PR (include prior art sidecar if available)
PRIOR_ART_FILE="${SOURCE}.prior-art"
PR_BODY=""
if [ -f "$PRIOR_ART_FILE" ]; then
# Escape JSON special chars in prior art content
PR_BODY=$(cat "$PRIOR_ART_FILE" | python3 -c 'import sys,json; print(json.dumps(sys.stdin.read()))')
PR_BODY=${PR_BODY:1:-1} # Strip outer quotes from json.dumps
fi
curl -sf -X POST "http://localhost:3000/api/v1/repos/teleo/teleo-codex/pulls" \
-H "Authorization: token $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"title\":\"extract: $BASENAME\",\"head\":\"$BRANCH\",\"base\":\"main\",\"body\":\"$PR_BODY\"}" >> /dev/null 2>&1
SUCCESS=$((SUCCESS + 1))
echo " -> SUCCESS ($CHANGED files)" >> $LOG
# Back to main
git checkout -f main >> $LOG 2>&1
# Rate limit
sleep 2
done
echo "[$(date)] Batch complete: $SUCCESS success, $FAILED failed, $SKIPPED skipped (already attempted)" >> $LOG
git checkout -f main >> $LOG 2>&1
git reset --hard origin/main >> $LOG 2>&1

View file

@ -1,43 +0,0 @@
substitutions:
_REGION: europe-west6
_REPOSITORY: teleo
_IMAGE: teleo-pipeline-gcp-staging
_TAG: manual-local
_REVISION: unknown
options:
logging: CLOUD_LOGGING_ONLY
machineType: E2_HIGHCPU_8
serviceAccount: projects/teleo-501523/serviceAccounts/sa-teleo-cloudbuild@teleo-501523.iam.gserviceaccount.com
steps:
- id: build-staging-image
name: gcr.io/cloud-builders/docker
args:
- build
- -f
- Dockerfile.gcp-staging
- --label
- org.opencontainers.image.source=https://github.com/living-ip/teleo-infrastructure
- --label
- org.opencontainers.image.revision=${_REVISION}
- --label
- livingip.revision=${_REVISION}
- --label
- livingip.surface=teleo-infrastructure
- --label
- livingip.tier=gcp-staging
- -t
- ${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPOSITORY}/${_IMAGE}:${_TAG}
- .
- id: smoke-test-image-before-push
name: gcr.io/cloud-builders/docker
args:
- run
- --rm
- ${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPOSITORY}/${_IMAGE}:${_TAG}
images:
- ${_REGION}-docker.pkg.dev/$PROJECT_ID/${_REPOSITORY}/${_IMAGE}:${_TAG}

View file

@ -1,185 +0,0 @@
{
"artifact": "teleo_gcp_service_communication_contract",
"schema_version": 1,
"project": "teleo-501523",
"region": "europe-west6",
"network": "teleo-staging-net",
"global_invariants": {
"no_public_database_ip": true,
"no_broad_ssh_or_rdp": true,
"no_default_compute_service_accounts": true,
"secret_values_not_stored_in_contract": true,
"database_connections_encrypted_only": true
},
"allowed_paths": [
{
"name": "github_actions_to_artifact_registry",
"purpose": "Build, smoke-run, and push Teleo staging Docker image.",
"source": {
"type": "github_actions_wif",
"repository": "living-ip/teleo-infrastructure"
},
"identity": "sa-artifact-builder@teleo-501523.iam.gserviceaccount.com",
"target": {
"type": "artifact_registry",
"repository": "projects/teleo-501523/locations/europe-west6/repositories/teleo"
},
"protocol": "https",
"ports": [443],
"network_path": "google_apis",
"encryption": "tls",
"allowed_by": [".github/workflows/gcp-artifact.yml", "roles/artifactregistry.writer"],
"forbidden": ["cloudsql.editor", "secretmanager.secretAccessor", "compute.admin"]
},
{
"name": "github_actions_to_readiness_probe",
"purpose": "Read-only GCP readiness probe with retained artifacts.",
"source": {
"type": "github_actions_wif",
"repository": "living-ip/teleo-infrastructure"
},
"identity": "sa-teleo-readiness@teleo-501523.iam.gserviceaccount.com",
"target": {
"type": "gcp_control_plane",
"resources": ["artifact_registry", "compute_metadata", "cloudsql_metadata", "secret_metadata", "storage_metadata"]
},
"protocol": "https",
"ports": [443],
"network_path": "google_apis",
"encryption": "tls",
"allowed_by": [".github/workflows/gcp-readiness.yml", "ops/plan_gcp_iam_split.py"],
"forbidden": ["artifactregistry.writer", "cloudsql.editor", "secretmanager.secretAccessor"]
},
{
"name": "operator_ssh_to_teleo_vms",
"purpose": "Emergency/operator shell access only from one retained operator IPv4 /32.",
"source": {
"type": "operator_ip",
"cidrs": ["<operator-ip>/32"]
},
"identity": "oslogin_operator",
"target": {
"type": "compute_instance_tags",
"tags": ["teleo-prod-ssh", "teleo-staging-ssh"]
},
"protocol": "tcp",
"ports": [22],
"network_path": "vpc_firewall",
"encryption": "ssh",
"allowed_by": ["ops/apply_gcp_runtime_baseline.py", "ops/check_gcp_infra_readiness.py"],
"forbidden": ["broad_ipv4_ingress", "broad_ipv6_ingress", "tcp:3389"]
},
{
"name": "teleo_vms_to_artifact_registry",
"purpose": "Runtime pulls immutable images from Artifact Registry over Private Google Access.",
"source": {
"type": "compute_service_accounts",
"service_accounts": [
"sa-teleo-prod-vm@teleo-501523.iam.gserviceaccount.com",
"sa-teleo-staging-vm@teleo-501523.iam.gserviceaccount.com"
]
},
"identity": "dedicated_vm_service_accounts",
"target": {
"type": "artifact_registry",
"repository": "projects/teleo-501523/locations/europe-west6/repositories/teleo"
},
"protocol": "https",
"ports": [443],
"network_path": "private_google_access",
"encryption": "tls",
"allowed_by": ["roles/artifactregistry.reader", "ops/apply_gcp_runtime_baseline.py"],
"forbidden": ["default_compute_service_account", "public_image_registry_required"]
},
{
"name": "teleo_vms_to_cloudsql_private",
"purpose": "Application and restore readback connections to the private Cloud SQL standby.",
"source": {
"type": "compute_service_accounts",
"service_accounts": [
"sa-teleo-prod-vm@teleo-501523.iam.gserviceaccount.com",
"sa-teleo-staging-vm@teleo-501523.iam.gserviceaccount.com"
]
},
"identity": "dedicated_vm_service_accounts",
"target": {
"type": "cloudsql_postgres",
"instance": "teleo-pgvector-standby",
"database": "teleo_kb",
"private_network": "projects/teleo-501523/global/networks/teleo-staging-net",
"public_ip": false
},
"protocol": "postgres",
"ports": [5432],
"network_path": "private_vpc",
"encryption": "encrypted_only",
"allowed_by": ["ops/apply_gcp_runtime_baseline.py", "ops/check_gcp_infra_readiness.py"],
"forbidden": ["public_ip", "ssl_disabled", "authorized_networks_public"]
},
{
"name": "cloudsql_import_from_backup_bucket",
"purpose": "Cloud SQL import operation reads generated restore SQL from the versioned backup bucket.",
"source": {
"type": "cloudsql_instance_service_account",
"discovered_by": "gcloud sql instances describe teleo-pgvector-standby --format=value(serviceAccountEmailAddress)"
},
"identity": "cloudsql_instance_service_account",
"target": {
"type": "gcs_bucket",
"bucket": "gs://teleo-501523-prod-backups",
"prefix": "kb-dumps/cloudsql-restore-drills/"
},
"protocol": "https",
"ports": [443],
"network_path": "google_apis",
"encryption": "tls",
"allowed_by": ["roles/storage.objectAdmin", "ops/apply_gcp_iam_split.py"],
"forbidden": ["allUsers", "allAuthenticatedUsers", "publicAccessPreventionDisabled"]
},
{
"name": "restore_drill_operator_to_cloudsql_admin",
"purpose": "Operator-triggered Cloud SQL import drill and post-import readback.",
"source": {
"type": "operator_or_ci_with_explicit_auth",
"requires_retained_execute_proof": true
},
"identity": "sa-teleo-restore-drill@teleo-501523.iam.gserviceaccount.com",
"target": {
"type": "cloudsql_admin_api",
"instance": "teleo-pgvector-standby"
},
"protocol": "https",
"ports": [443],
"network_path": "google_apis",
"encryption": "tls",
"allowed_by": ["roles/cloudsql.editor", "ops/run_gcp_cloudsql_restore_drill.sh"],
"forbidden": ["unretained_execute", "raw_password_in_logs"]
},
{
"name": "vps_backup_to_gcs_bucket",
"purpose": "Source backup artifacts are uploaded to versioned GCS only through an approved backup identity.",
"source": {
"type": "hetzner_vps_or_operator_export",
"host": "teleo@77.42.65.182"
},
"identity": "approved_backup_writer_or_operator_session",
"target": {
"type": "gcs_bucket",
"bucket": "gs://teleo-501523-prod-backups",
"prefix": "kb-dumps/"
},
"protocol": "https",
"ports": [443],
"network_path": "google_apis",
"encryption": "tls",
"allowed_by": ["ops/backup_vps_sqlite_kb.sh", "ops/run_gcp_cloudsql_restore_drill.sh"],
"forbidden": ["raw_private_key_in_contract", "database_public_ingress", "unversioned_bucket"]
}
],
"not_proven_by_this_contract": [
"that GCP resources currently exist",
"that firewall rules are currently applied",
"that Cloud SQL import/readback has succeeded",
"that production Telegram/Leo traffic has cut over to GCP"
]
}

99
deploy.sh Executable file
View file

@ -0,0 +1,99 @@
#!/usr/bin/env bash
# deploy.sh — Deploy pipeline and diagnostics to VPS from repo
# Usage: ./deploy.sh [--dry-run] [--restart]
#
# Requires: committed, clean working tree. Enforces repo-first workflow.
set -euo pipefail
VPS_HOST="teleo@77.42.65.182"
VPS_PIPELINE="/opt/teleo-eval/pipeline"
VPS_DIAGNOSTICS="/opt/teleo-eval/diagnostics"
VPS_AGENT_STATE="/opt/teleo-eval/ops/agent-state"
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
DRY_RUN=false
RESTART=false
for arg in "$@"; do
case "$arg" in
--dry-run) DRY_RUN=true ;;
--restart) RESTART=true ;;
--help|-h)
echo "Usage: $0 [--dry-run] [--restart]"
echo " --dry-run Show what would be deployed without doing it"
echo " --restart Restart services after deploy"
exit 0
;;
*) echo "Unknown arg: $arg"; exit 1 ;;
esac
done
# Gate: working tree must be clean
if [ -n "$(git -C "$REPO_ROOT" status --porcelain)" ]; then
echo "ERROR: Uncommitted changes. Commit first, deploy second."
git -C "$REPO_ROOT" status --short
exit 1
fi
echo "Deploying from commit: $(git -C "$REPO_ROOT" log --oneline -1)"
echo ""
# Syntax check all Python files before deploying
echo "=== Pre-deploy syntax check ==="
ERRORS=0
for f in "$REPO_ROOT/ops/pipeline-v2/lib/"*.py "$REPO_ROOT/ops/pipeline-v2/"*.py "$REPO_ROOT/ops/diagnostics/"*.py; do
[ -f "$f" ] || continue
if ! python3 -c "import ast, sys; ast.parse(open(sys.argv[1]).read())" "$f" 2>/dev/null; then
echo "SYNTAX ERROR: $f"
ERRORS=$((ERRORS + 1))
fi
done
if [ "$ERRORS" -gt 0 ]; then
echo "ERROR: $ERRORS files have syntax errors. Fix before deploying."
exit 1
fi
echo "All files pass syntax check."
echo ""
RSYNC_FLAGS="-avz --exclude='__pycache__' --exclude='*.pyc' --exclude='*.bak*'"
if $DRY_RUN; then
RSYNC_FLAGS="$RSYNC_FLAGS --dry-run"
echo "=== DRY RUN ==="
fi
echo "=== Pipeline lib/ ==="
rsync $RSYNC_FLAGS "$REPO_ROOT/ops/pipeline-v2/lib/" "$VPS_HOST:$VPS_PIPELINE/lib/"
echo ""
echo "=== Pipeline top-level ==="
for f in teleo-pipeline.py reweave.py batch-extract-50.sh; do
[ -f "$REPO_ROOT/ops/pipeline-v2/$f" ] || continue
rsync $RSYNC_FLAGS "$REPO_ROOT/ops/pipeline-v2/$f" "$VPS_HOST:$VPS_PIPELINE/$f"
done
echo ""
echo "=== Diagnostics ==="
rsync $RSYNC_FLAGS "$REPO_ROOT/ops/diagnostics/" "$VPS_HOST:$VPS_DIAGNOSTICS/"
echo ""
echo "=== Agent state ==="
rsync $RSYNC_FLAGS "$REPO_ROOT/ops/agent-state/" "$VPS_HOST:$VPS_AGENT_STATE/"
echo ""
echo "=== Research session ==="
rsync $RSYNC_FLAGS "$REPO_ROOT/ops/research-session.sh" "$VPS_HOST:/opt/teleo-eval/research-session.sh"
echo ""
if $DRY_RUN; then
echo "Dry run complete. No changes made."
exit 0
fi
echo "Deploy complete."
if $RESTART; then
echo ""
echo "=== Restarting services ==="
ssh "$VPS_HOST" "sudo systemctl restart teleo-pipeline teleo-diagnostics"
echo "Services restarted."
fi

View file

@ -1,280 +0,0 @@
#!/usr/bin/env bash
# auto-deploy.sh — Pull from Forgejo, sync to working dirs, restart if needed.
# Runs as systemd timer (teleo-auto-deploy.timer) every 2 minutes.
# Exits silently when nothing has changed.
set -euo pipefail
LOCK_FILE="/tmp/teleo-auto-deploy.lock"
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
logger -t "auto-deploy" "Another deploy is already running. Skipping."
exit 0
fi
DEPLOY_CHECKOUT="/opt/teleo-eval/workspaces/deploy-infra"
PIPELINE_DIR="/opt/teleo-eval/pipeline"
TELEGRAM_DIR="/opt/teleo-eval/telegram"
DIAGNOSTICS_DIR="/opt/teleo-eval/diagnostics"
AGENT_STATE_DIR="/opt/teleo-eval/ops/agent-state"
LEOCLEAN_BIN_DIR="/home/teleo/.hermes/profiles/leoclean/bin"
LEOCLEAN_SKILLS_DIR="/home/teleo/.hermes/profiles/leoclean/skills"
LEOCLEAN_PLUGINS_DIR="/home/teleo/.hermes/profiles/leoclean/plugins"
HERMES_AGENT_DIR="/home/teleo/.hermes/hermes-agent"
HERMES_PATCH_DIR="/home/teleo/.hermes/teleo-runtime-patches"
SYSTEMD_DIR="/etc/systemd/system"
STAMP_FILE="/opt/teleo-eval/.last-deploy-sha"
LOG_TAG="auto-deploy"
log() { logger -t "$LOG_TAG" "$1"; echo "$(date '+%Y-%m-%d %H:%M:%S') $1"; }
DEPLOY_REMOTE="${TELEO_DEPLOY_REMOTE:-}"
if [ -z "$DEPLOY_REMOTE" ]; then
if git -C "$DEPLOY_CHECKOUT" remote get-url github >/dev/null 2>&1; then
DEPLOY_REMOTE="github"
else
DEPLOY_REMOTE="origin"
fi
fi
if [ ! -d "$DEPLOY_CHECKOUT/.git" ]; then
log "ERROR: Deploy checkout not found at $DEPLOY_CHECKOUT. Run setup first."
exit 1
fi
cd "$DEPLOY_CHECKOUT"
if ! git remote get-url "$DEPLOY_REMOTE" >/dev/null 2>&1; then
log "ERROR: deploy remote '$DEPLOY_REMOTE' is not configured"
exit 1
fi
if ! git fetch "$DEPLOY_REMOTE" main --quiet 2>&1; then
log "ERROR: git fetch failed for $DEPLOY_REMOTE/main"
exit 1
fi
NEW_SHA=$(git rev-parse "$DEPLOY_REMOTE/main")
OLD_SHA=$(cat "$STAMP_FILE" 2>/dev/null || echo "none")
if [ "$NEW_SHA" = "$OLD_SHA" ]; then
# Hermes can be upgraded independently of this repository. Repair an
# overwritten response-transform hook even when no infrastructure commit
# changed, and restart only when the patch was actually reapplied.
if [ -f "$HERMES_PATCH_DIR/apply_response_transform_hook.py" ] && [ -f "$HERMES_AGENT_DIR/run_agent.py" ]; then
if ! PATCH_RESULT=$(python3 "$HERMES_PATCH_DIR/apply_response_transform_hook.py" "$HERMES_AGENT_DIR/run_agent.py"); then
log "ERROR: Hermes response-transform drift could not be repaired"
exit 1
fi
if echo "$PATCH_RESULT" | grep -q '"status": "installed_now"'; then
log "Hermes response-transform drift repaired: $PATCH_RESULT"
if systemctl is-active --quiet leoclean-gateway.service; then
sudo systemctl restart leoclean-gateway
sleep 10
if ! systemctl is-active --quiet leoclean-gateway.service; then
log "ERROR: leoclean-gateway failed after response-transform drift repair"
exit 1
fi
fi
fi
fi
exit 0
fi
log "New commits: ${OLD_SHA:0:8} -> ${NEW_SHA:0:8}"
if ! git checkout main --quiet 2>&1; then
log "ERROR: git checkout main failed — dirty tree or corrupted index"
exit 1
fi
if ! git merge --ff-only "$DEPLOY_REMOTE/main" --quiet 2>&1; then
log "ERROR: git merge --ff-only $DEPLOY_REMOTE/main failed. Manual intervention needed."
exit 1
fi
# A running shell keeps the old script body after git updates this file. Re-exec
# once from the merged checkout so newly added sync/restart paths take effect in
# the same deploy instead of writing a false-success stamp.
if [ "${TELEO_AUTO_DEPLOY_REEXECED:-0}" != "1" ] \
&& [ "$OLD_SHA" != "none" ] \
&& git diff --name-only "$OLD_SHA" "$NEW_SHA" -- deploy/auto-deploy.sh deploy/leoclean-gateway-restart-required.sh \
| grep -q .; then
log "Deploy logic changed; re-executing merged deploy script"
export TELEO_AUTO_DEPLOY_REEXECED=1
exec bash "$DEPLOY_CHECKOUT/deploy/auto-deploy.sh"
fi
# Syntax check all Python files before copying
ERRORS=0
for f in lib/*.py *.py diagnostics/*.py telegram/*.py tests/*.py hermes-agent/leoclean-plugins/vps/*/*.py hermes-agent/patches/*.py; do
[ -f "$f" ] || continue
if ! python3 -c "import ast, sys; ast.parse(open(sys.argv[1]).read())" "$f" 2>&1; then
log "SYNTAX ERROR: $f"
ERRORS=$((ERRORS + 1))
fi
done
if [ "$ERRORS" -gt 0 ]; then
log "ERROR: $ERRORS syntax errors. Deploy aborted. Fix and push again."
exit 1
fi
log "Syntax check passed"
# Sync to working directories
RSYNC_OPTS=(-az --exclude __pycache__ --exclude '*.pyc' --exclude '*.bak*')
rsync "${RSYNC_OPTS[@]}" lib/ "$PIPELINE_DIR/lib/"
for f in teleo-pipeline.py reweave.py fetch_coins.py pipeline-health-check.py; do
[ -f "$f" ] && rsync "${RSYNC_OPTS[@]}" "$f" "$PIPELINE_DIR/$f"
done
rsync "${RSYNC_OPTS[@]}" telegram/ "$PIPELINE_DIR/telegram/"
rsync "${RSYNC_OPTS[@]}" telegram/ "$TELEGRAM_DIR/"
rsync "${RSYNC_OPTS[@]}" diagnostics/ "$DIAGNOSTICS_DIR/"
rsync "${RSYNC_OPTS[@]}" agent-state/ "$AGENT_STATE_DIR/"
rsync "${RSYNC_OPTS[@]}" tests/ "$PIPELINE_DIR/tests/"
if [ -d hermes-agent/leoclean-bin ]; then
mkdir -p "$LEOCLEAN_BIN_DIR"
rsync "${RSYNC_OPTS[@]}" hermes-agent/leoclean-bin/ "$LEOCLEAN_BIN_DIR/"
fi
if [ -d hermes-agent/leoclean-skills/vps ]; then
mkdir -p "$LEOCLEAN_SKILLS_DIR"
rsync "${RSYNC_OPTS[@]}" hermes-agent/leoclean-skills/vps/ "$LEOCLEAN_SKILLS_DIR/"
fi
if [ -d hermes-agent/leoclean-plugins/vps ]; then
mkdir -p "$LEOCLEAN_PLUGINS_DIR"
rsync "${RSYNC_OPTS[@]}" hermes-agent/leoclean-plugins/vps/ "$LEOCLEAN_PLUGINS_DIR/"
fi
if [ -d hermes-agent/patches ]; then
mkdir -p "$HERMES_PATCH_DIR"
rsync "${RSYNC_OPTS[@]}" hermes-agent/patches/ "$HERMES_PATCH_DIR/"
fi
[ -f research/research-session.sh ] && rsync "${RSYNC_OPTS[@]}" research/research-session.sh /opt/teleo-eval/research-session.sh
if [ -f "$HERMES_PATCH_DIR/apply_response_transform_hook.py" ]; then
PATCH_RESULT=$(python3 "$HERMES_PATCH_DIR/apply_response_transform_hook.py" "$HERMES_AGENT_DIR/run_agent.py")
log "Hermes response transform: $PATCH_RESULT"
fi
# Safety net: ensure synced .sh files are executable after rsync.
# Keep this bounded to deploy-owned paths: /opt/teleo-eval also contains
# backups and generated state that may be unreadable by the deploy user.
for dir in "$PIPELINE_DIR" "$TELEGRAM_DIR" "$DIAGNOSTICS_DIR" "$AGENT_STATE_DIR" "$LEOCLEAN_BIN_DIR" "$LEOCLEAN_SKILLS_DIR" "$LEOCLEAN_PLUGINS_DIR" "$HERMES_PATCH_DIR"; do
[ -d "$dir" ] || continue
find "$dir" -maxdepth 3 -name '*.sh' -not -perm -u+x -exec chmod +x {} +
done
[ -f /opt/teleo-eval/research-session.sh ] && chmod u+x /opt/teleo-eval/research-session.sh
log "Files synced"
if [ "$OLD_SHA" = "none" ] || git diff --name-only "$OLD_SHA" "$NEW_SHA" -- systemd/ 2>/dev/null | grep -q .; then
log "Installing systemd units"
for unit in systemd/*.service systemd/*.timer; do
[ -f "$unit" ] || continue
sudo install -m 0644 "$unit" "$SYSTEMD_DIR/$(basename "$unit")"
done
sudo systemctl daemon-reload
if [ -f systemd/teleo-auto-deploy.timer ]; then
sudo systemctl enable --now teleo-auto-deploy.timer >/dev/null
fi
if [ -f systemd/teleo-agent-healthcheck.timer ]; then
sudo systemctl enable --now teleo-agent-healthcheck.timer >/dev/null
fi
fi
# Restart services only when changed files affect a running process.
RESTART=""
add_restart() {
case " $RESTART " in
*" $1 "*) ;;
*) RESTART="$RESTART $1" ;;
esac
}
add_restart_if_unit_exists() {
if systemctl list-units --all --full "$1.service" --no-legend 2>/dev/null | grep -q .; then
add_restart "$1"
fi
}
add_restart_if_unit_active() {
if systemctl is-active --quiet "$1.service"; then
add_restart "$1"
fi
}
if [ "$OLD_SHA" != "none" ]; then
if git diff --name-only "$OLD_SHA" "$NEW_SHA" -- lib/ teleo-pipeline.py reweave.py telegram/ 2>/dev/null | grep -q '\.py$'; then
add_restart teleo-pipeline
fi
if git diff --name-only "$OLD_SHA" "$NEW_SHA" -- telegram/ 2>/dev/null | grep -q '\.py$'; then
add_restart_if_unit_active teleo-agent@leo
add_restart_if_unit_exists teleo-agent@leo-wallet-test
fi
if git diff --name-only "$OLD_SHA" "$NEW_SHA" -- systemd/teleo-agent@.service 2>/dev/null | grep -q .; then
add_restart_if_unit_active teleo-agent@leo
add_restart_if_unit_exists teleo-agent@leo-wallet-test
fi
if git diff --name-only "$OLD_SHA" "$NEW_SHA" -- diagnostics/ 2>/dev/null | grep -q '\.py$'; then
add_restart teleo-diagnostics
fi
if git diff --name-only "$OLD_SHA" "$NEW_SHA" -- hermes-agent/leoclean-bin/ hermes-agent/leoclean-skills/vps/ hermes-agent/leoclean-plugins/vps/ hermes-agent/patches/ 2>/dev/null \
| bash deploy/leoclean-gateway-restart-required.sh; then
add_restart_if_unit_active leoclean-gateway
fi
else
RESTART="teleo-pipeline teleo-diagnostics"
add_restart_if_unit_active teleo-agent@leo
add_restart_if_unit_exists teleo-agent@leo-wallet-test
add_restart_if_unit_active leoclean-gateway
fi
if [ -n "$RESTART" ]; then
log "Restarting:$RESTART"
for svc in $RESTART; do
sudo systemctl restart "$svc"
done
sleep 30
FAIL=0
for svc in $RESTART; do
if systemctl is-active --quiet "$svc"; then
log "$svc: active"
else
log "ERROR: $svc failed to start"
journalctl -u "$svc" -n 5 --no-pager 2>/dev/null || true
FAIL=1
fi
if [ "$svc" = "leoclean-gateway" ] && [ -f "$HERMES_PATCH_DIR/apply_response_transform_hook.py" ]; then
if PATCH_CHECK=$(python3 "$HERMES_PATCH_DIR/apply_response_transform_hook.py" --check "$HERMES_AGENT_DIR/run_agent.py"); then
log "leoclean-gateway response transform: $PATCH_CHECK"
else
log "ERROR: leoclean-gateway response transform check failed: $PATCH_CHECK"
FAIL=1
fi
fi
done
if echo "$RESTART" | grep -q "teleo-pipeline"; then
HEALTH_CODE=$(curl -s -o /dev/null -w '%{http_code}' --connect-timeout 3 http://localhost:8080/health 2>/dev/null || echo "000")
if [ "$HEALTH_CODE" = "200" ] || [ "$HEALTH_CODE" = "503" ]; then
log "pipeline health: OK (HTTP $HEALTH_CODE)"
else
log "WARNING: pipeline health check failed (HTTP $HEALTH_CODE)"
FAIL=1
fi
fi
if echo "$RESTART" | grep -q "teleo-diagnostics"; then
if curl -sf --connect-timeout 3 http://localhost:8081/ops > /dev/null 2>&1; then
log "diagnostics health: OK"
else
log "WARNING: diagnostics health check failed"
FAIL=1
fi
fi
if [ "$FAIL" -gt 0 ]; then
log "WARNING: Smoke test failures. NOT updating stamp. Will retry next cycle. Push a fix."
exit 1
fi
else
log "No runtime changes - services not restarted"
fi
echo "$NEW_SHA" > "$STAMP_FILE"
log "Deploy complete: $(git log --oneline -1 "$NEW_SHA")"

View file

@ -1,145 +0,0 @@
#!/usr/bin/env bash
# deploy.sh — Deploy pipeline and diagnostics to VPS from repo
# Usage: ./deploy.sh [--dry-run] [--restart]
#
# Requires: committed, clean working tree. Enforces repo-first workflow.
set -euo pipefail
VPS_HOST="teleo@77.42.65.182"
VPS_PIPELINE="/opt/teleo-eval/pipeline"
VPS_TELEGRAM="/opt/teleo-eval/telegram"
VPS_DIAGNOSTICS="/opt/teleo-eval/diagnostics"
VPS_AGENT_STATE="/opt/teleo-eval/ops/agent-state"
VPS_LEOCLEAN_BIN="/home/teleo/.hermes/profiles/leoclean/bin"
VPS_LEOCLEAN_SKILLS="/home/teleo/.hermes/profiles/leoclean/skills"
VPS_LEOCLEAN_PLUGINS="/home/teleo/.hermes/profiles/leoclean/plugins"
VPS_HERMES_AGENT="/home/teleo/.hermes/hermes-agent"
VPS_HERMES_PATCHES="/home/teleo/.hermes/teleo-runtime-patches"
VPS_SYSTEMD="/etc/systemd/system"
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
DRY_RUN=false
RESTART=false
for arg in "$@"; do
case "$arg" in
--dry-run) DRY_RUN=true ;;
--restart) RESTART=true ;;
--help|-h)
echo "Usage: $0 [--dry-run] [--restart]"
echo " --dry-run Show what would be deployed without doing it"
echo " --restart Restart services after deploy"
exit 0
;;
*) echo "Unknown arg: $arg"; exit 1 ;;
esac
done
# Gate: working tree must be clean
if [ -n "$(git -C "$REPO_ROOT" status --porcelain)" ]; then
echo "ERROR: Uncommitted changes. Commit first, deploy second."
git -C "$REPO_ROOT" status --short
exit 1
fi
echo "Deploying from commit: $(git -C "$REPO_ROOT" log --oneline -1)"
echo ""
# Syntax check all Python files before deploying
echo "=== Pre-deploy syntax check ==="
ERRORS=0
for f in "$REPO_ROOT/lib/"*.py "$REPO_ROOT/"*.py "$REPO_ROOT/diagnostics/"*.py "$REPO_ROOT/telegram/"*.py "$REPO_ROOT/hermes-agent/leoclean-plugins/vps/"*/*.py "$REPO_ROOT/hermes-agent/patches/"*.py; do
[ -f "$f" ] || continue
if ! python3 -c "import ast, sys; ast.parse(open(sys.argv[1]).read())" "$f" 2>/dev/null; then
echo "SYNTAX ERROR: $f"
ERRORS=$((ERRORS + 1))
fi
done
if [ "$ERRORS" -gt 0 ]; then
echo "ERROR: $ERRORS files have syntax errors. Fix before deploying."
exit 1
fi
echo "All files pass syntax check."
echo ""
RSYNC_OPTS=(-avz --exclude __pycache__ --exclude '*.pyc' --exclude '*.bak*')
if $DRY_RUN; then
RSYNC_OPTS+=(--dry-run)
echo "=== DRY RUN ==="
fi
echo "=== Pipeline lib/ ==="
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/lib/" "$VPS_HOST:$VPS_PIPELINE/lib/"
echo ""
echo "=== Pipeline top-level ==="
for f in teleo-pipeline.py reweave.py fetch_coins.py; do
[ -f "$REPO_ROOT/$f" ] || continue
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/$f" "$VPS_HOST:$VPS_PIPELINE/$f"
done
echo ""
echo "=== Telegram bot ==="
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/telegram/" "$VPS_HOST:$VPS_PIPELINE/telegram/"
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/telegram/" "$VPS_HOST:$VPS_TELEGRAM/"
echo ""
echo "=== Tests ==="
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/tests/" "$VPS_HOST:$VPS_PIPELINE/tests/"
echo ""
echo "=== Diagnostics ==="
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/diagnostics/" "$VPS_HOST:$VPS_DIAGNOSTICS/"
echo ""
echo "=== Agent state ==="
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/agent-state/" "$VPS_HOST:$VPS_AGENT_STATE/"
echo ""
echo "=== Leoclean bin ==="
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/hermes-agent/leoclean-bin/" "$VPS_HOST:$VPS_LEOCLEAN_BIN/"
echo ""
echo "=== Leoclean skills ==="
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/hermes-agent/leoclean-skills/vps/" "$VPS_HOST:$VPS_LEOCLEAN_SKILLS/"
echo ""
echo "=== Leoclean plugins ==="
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/hermes-agent/leoclean-plugins/vps/" "$VPS_HOST:$VPS_LEOCLEAN_PLUGINS/"
echo ""
echo "=== Hermes runtime patches ==="
if $DRY_RUN; then
echo "Would sync hermes-agent/patches/ and verify the installed response-transform hook."
else
ssh "$VPS_HOST" "mkdir -p '$VPS_HERMES_PATCHES'"
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/hermes-agent/patches/" "$VPS_HOST:$VPS_HERMES_PATCHES/"
ssh "$VPS_HOST" "python3 '$VPS_HERMES_PATCHES/apply_response_transform_hook.py' '$VPS_HERMES_AGENT/run_agent.py'"
fi
echo ""
echo "=== Research session ==="
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/research/research-session.sh" "$VPS_HOST:/opt/teleo-eval/research-session.sh"
echo ""
echo "=== Systemd units ==="
if $DRY_RUN; then
rsync "${RSYNC_OPTS[@]}" "$REPO_ROOT/systemd/" "$VPS_HOST:/tmp/teleo-systemd-dry-run/"
else
tar -C "$REPO_ROOT/systemd" -cf - . | ssh "$VPS_HOST" "tmpdir=\$(mktemp -d); tar -C \"\$tmpdir\" -xf -; sudo install -m 0644 \"\$tmpdir\"/*.service \"\$tmpdir\"/*.timer '$VPS_SYSTEMD'/; rm -rf \"\$tmpdir\"; sudo systemctl daemon-reload; sudo systemctl enable --now teleo-auto-deploy.timer teleo-agent-healthcheck.timer >/dev/null"
fi
echo ""
if $DRY_RUN; then
echo "Dry run complete. No changes made."
exit 0
fi
echo "Deploy complete."
if $RESTART; then
echo ""
echo "=== Restarting services ==="
ssh "$VPS_HOST" "sudo systemctl restart teleo-pipeline teleo-diagnostics; if systemctl is-active --quiet teleo-agent@leo.service; then sudo systemctl restart teleo-agent@leo; fi; if systemctl list-units --all --full teleo-agent@leo-wallet-test.service --no-legend | grep -q .; then sudo systemctl restart teleo-agent@leo-wallet-test; fi; if systemctl is-active --quiet leoclean-gateway.service; then sudo systemctl restart leoclean-gateway; fi"
echo "Services restarted."
fi

View file

@ -1,16 +0,0 @@
#!/usr/bin/env bash
# Install the narrow sudoers rule required by teleo-auto-deploy.service.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
SOURCE="$REPO_ROOT/deploy/sudoers/teleo-auto-deploy"
TARGET="/etc/sudoers.d/teleo-auto-deploy"
if [ "$(id -u)" -ne 0 ]; then
echo "ERROR: run as root on the VPS" >&2
exit 1
fi
install -m 0440 "$SOURCE" "$TARGET"
visudo -cf "$TARGET"
echo "Installed $TARGET"

View file

@ -1,26 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
# Read changed repo paths from stdin. Markdown skills are loaded from the synced
# profile per turn; runtime code changes still require a gateway restart.
restart_required=1
while IFS= read -r path || [ -n "$path" ]; do
case "$path" in
hermes-agent/leoclean-bin/*)
restart_required=0
;;
hermes-agent/leoclean-skills/vps/*.md|hermes-agent/leoclean-skills/vps/*/*.md)
;;
hermes-agent/leoclean-skills/vps/*)
restart_required=0
;;
hermes-agent/leoclean-plugins/vps/*)
restart_required=0
;;
hermes-agent/patches/*)
restart_required=0
;;
esac
done
exit "$restart_required"

View file

@ -1,120 +0,0 @@
#!/bin/bash
# One-time setup: prepare the bare mirror repo for teleo-infrastructure.
#
# Prerequisites (must happen BEFORE running this):
# 1. GitHub repo `living-ip/teleo-infrastructure` created (manual via web or
# `gh repo create` — the deploy PAT is fine-grained to teleo-codex only
# and cannot create new repos in the org).
# 2. GitHub PAT updated to include push access on the new repo (or rotate
# to a classic PAT with `repo` scope covering both).
#
# This script is idempotent — safe to re-run.
set -euo pipefail
MIRROR_BASE="/opt/teleo-eval/mirror"
REPO_DIR="$MIRROR_BASE/teleo-infrastructure.git"
FORGEJO_URL="http://localhost:3000/teleo/teleo-infrastructure.git"
GITHUB_REPO="living-ip/teleo-infrastructure"
FORGEJO_TOKEN_FILE="/opt/teleo-eval/secrets/forgejo-admin-token"
GITHUB_PAT_FILE="/opt/teleo-eval/secrets/github-pat"
if [ ! -f "$FORGEJO_TOKEN_FILE" ]; then
echo "ERROR: missing $FORGEJO_TOKEN_FILE" >&2
exit 1
fi
if [ ! -f "$GITHUB_PAT_FILE" ]; then
echo "ERROR: missing $GITHUB_PAT_FILE" >&2
exit 1
fi
FORGEJO_TOKEN=$(cat "$FORGEJO_TOKEN_FILE" | tr -d '[:space:]')
GITHUB_PAT=$(cat "$GITHUB_PAT_FILE" | tr -d '[:space:]')
# Sanity check: GitHub repo must exist before we point a remote at it.
echo "Verifying GitHub repo $GITHUB_REPO exists..."
GH_STATUS=$(curl -sS -o /dev/null -w "%{http_code}" \
-H "Authorization: Bearer $GITHUB_PAT" \
"https://api.github.com/repos/$GITHUB_REPO")
if [ "$GH_STATUS" != "200" ]; then
echo "ERROR: GitHub repo $GITHUB_REPO not accessible (HTTP $GH_STATUS)" >&2
echo "Create it first: gh repo create $GITHUB_REPO --public --description 'Pipeline + diagnostics infra for the LivingIP collective'" >&2
exit 2
fi
echo " OK — $GITHUB_REPO accessible"
# Sanity check: Forgejo repo must exist.
echo "Verifying Forgejo repo teleo/teleo-infrastructure exists..."
FG_STATUS=$(curl -sS -o /dev/null -w "%{http_code}" \
-H "Authorization: token $FORGEJO_TOKEN" \
"http://localhost:3000/api/v1/repos/teleo/teleo-infrastructure")
if [ "$FG_STATUS" != "200" ]; then
echo "ERROR: Forgejo repo teleo/teleo-infrastructure not accessible (HTTP $FG_STATUS)" >&2
exit 3
fi
echo " OK — Forgejo repo accessible"
# Init bare mirror if missing
if [ -d "$REPO_DIR" ]; then
echo "Bare repo already exists at $REPO_DIR — skipping init"
else
echo "Creating bare repo at $REPO_DIR..."
mkdir -p "$REPO_DIR"
cd "$REPO_DIR"
git init --bare >/dev/null
chown -R teleo:teleo "$REPO_DIR"
echo " OK — bare repo initialized"
fi
cd "$REPO_DIR"
# Configure remotes (idempotent: set-url succeeds whether remote exists or not)
# Forgejo remote (origin convention is reversed in this codebase: origin=GitHub,
# forgejo=Forgejo, matching the existing teleo-codex.git layout).
FORGEJO_REMOTE_URL="http://github-mirror:${FORGEJO_TOKEN}@localhost:3000/teleo/teleo-infrastructure.git"
# NOTE: "m3taversal" is a placeholder username — for fine-grained PATs the
# username field is decorative; the token does the auth. Matches the existing
# teleo-codex.git remote for consistency. (Ganymede review nit #4.)
GITHUB_REMOTE_URL="https://m3taversal:${GITHUB_PAT}@github.com/${GITHUB_REPO}.git"
if git remote get-url forgejo >/dev/null 2>&1; then
git remote set-url forgejo "$FORGEJO_REMOTE_URL"
echo " Updated forgejo remote URL"
else
git remote add forgejo "$FORGEJO_REMOTE_URL"
echo " Added forgejo remote"
fi
if git remote get-url origin >/dev/null 2>&1; then
git remote set-url origin "$GITHUB_REMOTE_URL"
echo " Updated origin remote URL"
else
git remote add origin "$GITHUB_REMOTE_URL"
echo " Added origin remote"
fi
# Initial fetch from Forgejo
echo "Fetching from Forgejo..."
git fetch forgejo --prune 2>&1 | sed 's/^/ /'
# Initial push to GitHub (will populate the empty repo)
# main_only mode: push ONLY refs/heads/main + tags, mirroring what sync-mirror.sh
# does for this repo on the recurring path. Agent review branches stay Forgejo-only.
echo "Pushing initial main + tags to GitHub..."
git update-ref refs/heads/main refs/remotes/forgejo/main 2>/dev/null || {
echo "ERROR: forgejo/main ref missing — fetch may have failed" >&2
exit 1
}
git push origin "refs/heads/main:refs/heads/main" 2>&1 | sed 's/^/ /' || {
echo "WARN: initial push failed — you may need to authorize the PAT for $GITHUB_REPO" >&2
}
git push origin --tags 2>&1 | sed 's/^/ /' || true
# Final permissions sweep
chown -R teleo:teleo "$REPO_DIR"
echo
echo "Setup complete. Verify with:"
echo " ssh teleo@77.42.65.182 ls -la $REPO_DIR/refs/heads"
echo " /opt/teleo-eval/sync-mirror.sh && tail -50 /opt/teleo-eval/logs/sync.log"

View file

@ -1,14 +0,0 @@
# Narrow service privileges for teleo-auto-deploy.service, which runs as teleo.
# Keep command forms aligned with deploy/auto-deploy.sh.
teleo ALL=(root) NOPASSWD: /usr/bin/systemctl restart leoclean-gateway
teleo ALL=(root) NOPASSWD: /bin/systemctl restart leoclean-gateway
teleo ALL=(root) NOPASSWD: /usr/bin/systemctl restart leoclean-gateway.service
teleo ALL=(root) NOPASSWD: /bin/systemctl restart leoclean-gateway.service
teleo ALL=(root) NOPASSWD: /usr/bin/systemctl status leoclean-gateway
teleo ALL=(root) NOPASSWD: /bin/systemctl status leoclean-gateway
teleo ALL=(root) NOPASSWD: /usr/bin/systemctl status leoclean-gateway.service
teleo ALL=(root) NOPASSWD: /bin/systemctl status leoclean-gateway.service
teleo ALL=(root) NOPASSWD: /usr/bin/journalctl -u leoclean-gateway
teleo ALL=(root) NOPASSWD: /bin/journalctl -u leoclean-gateway
teleo ALL=(root) NOPASSWD: /usr/bin/journalctl -u leoclean-gateway.service
teleo ALL=(root) NOPASSWD: /bin/journalctl -u leoclean-gateway.service

View file

@ -1,107 +0,0 @@
#!/usr/bin/env bash
# Sync source-controlled GCP leoclean skills into the GCP parallel runtime.
#
# Defaults target the non-production GCP leoclean service. This script does not
# touch the production Telegram token and does not read or print secrets.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
PROJECT="${PROJECT:-teleo-501523}"
ZONE="${ZONE:-europe-west6-a}"
INSTANCE="${INSTANCE:-teleo-prod-1}"
SERVICE="${SERVICE:-leoclean-gcp-prod-parallel.service}"
REMOTE_SKILLS_DIR="${REMOTE_SKILLS_DIR:-/home/teleo/.hermes/profiles/leoclean/skills}"
SOURCE_DIR="${SOURCE_DIR:-$REPO_ROOT/hermes-agent/leoclean-skills/gcp}"
DRY_RUN=false
RESTART=false
VERIFY_ONLY=false
usage() {
cat <<'USAGE'
Usage: deploy/sync-gcp-leoclean-skills.sh [--dry-run] [--restart] [--verify-only]
Environment overrides:
PROJECT GCP project, default teleo-501523
ZONE GCP zone, default europe-west6-a
INSTANCE GCP VM, default teleo-prod-1
SERVICE systemd service, default leoclean-gcp-prod-parallel.service
REMOTE_SKILLS_DIR runtime skills dir, default /home/teleo/.hermes/profiles/leoclean/skills
SOURCE_DIR local source dir, default hermes-agent/leoclean-skills/gcp
USAGE
}
for arg in "$@"; do
case "$arg" in
--dry-run) DRY_RUN=true ;;
--restart) RESTART=true ;;
--verify-only) VERIFY_ONLY=true ;;
--help|-h)
usage
exit 0
;;
*)
echo "Unknown arg: $arg" >&2
usage >&2
exit 1
;;
esac
done
if [ ! -d "$SOURCE_DIR" ]; then
echo "ERROR: source skill directory not found: $SOURCE_DIR" >&2
exit 1
fi
if [ ! -f "$SOURCE_DIR/teleo-kb-bridge/SKILL.md" ]; then
echo "ERROR: source teleo-kb-bridge skill not found under $SOURCE_DIR" >&2
exit 1
fi
GCP_SSH=(
gcloud compute ssh "$INSTANCE"
--project="$PROJECT"
--zone="$ZONE"
--tunnel-through-iap
)
verify_command=$(cat <<REMOTE
set -euo pipefail
sudo -n -u teleo grep -nE '/kb/claims/<claim_id>|wrap claim IDs, proposal IDs' '$REMOTE_SKILLS_DIR/teleo-kb-bridge/SKILL.md'
systemctl show '$SERVICE' -p ActiveState -p SubState -p NRestarts -p MainPID --no-pager
REMOTE
)
if $DRY_RUN; then
echo "DRY RUN: would stream $SOURCE_DIR to $INSTANCE:$REMOTE_SKILLS_DIR"
echo "DRY RUN: project=$PROJECT zone=$ZONE service=$SERVICE restart=$RESTART verify_only=$VERIFY_ONLY"
exit 0
fi
if $VERIFY_ONLY; then
"${GCP_SSH[@]}" --command="$verify_command"
exit 0
fi
REMOTE_TMP="/tmp/teleo-gcp-leoclean-skills-$(date -u +%Y%m%dT%H%M%SZ)-$$"
sync_command=$(cat <<REMOTE
set -euo pipefail
remote_tmp='$REMOTE_TMP'
remote_skills='$REMOTE_SKILLS_DIR'
service='$SERVICE'
cleanup() { rm -rf "\$remote_tmp"; }
trap cleanup EXIT
rm -rf "\$remote_tmp"
mkdir -p "\$remote_tmp"
tar -C "\$remote_tmp" -xf -
sudo install -d -o teleo -g teleo -m 0755 "\$remote_skills"
tar -C "\$remote_tmp" -cf - . | sudo tar -C "\$remote_skills" -xf -
sudo chown -R teleo:teleo "\$remote_skills"
sudo -n -u teleo grep -nE '/kb/claims/<claim_id>|wrap claim IDs, proposal IDs' "\$remote_skills/teleo-kb-bridge/SKILL.md"
if [ '$RESTART' = true ]; then
sudo systemctl restart "\$service"
fi
systemctl show "\$service" -p ActiveState -p SubState -p NRestarts -p MainPID --no-pager
REMOTE
)
COPYFILE_DISABLE=1 tar --format=ustar -C "$SOURCE_DIR" -cf - . | "${GCP_SSH[@]}" --command="$sync_command"

View file

@ -1,451 +0,0 @@
#!/bin/bash
# Bidirectional sync: Forgejo (authoritative) <-> GitHub (public mirror)
# Forgejo wins on conflict. Runs every 2 minutes via cron.
#
# Repos handled (see MIRROR_REPOS below):
# - teleo-codex (mode=bidirectional): full PR roundtrip — fork PR refs from
# GitHub, auto-create Forgejo PR mirrors, link github_pr in pipeline.db.
# - teleo-infrastructure (mode=main_only): one-way sync of branches+tags from
# Forgejo to GitHub. No PR roundtrip — pipeline doesn't process infra PRs;
# external infra PRs land on GitHub for visibility, get reviewed manually.
#
# Security note: GitHub->Forgejo path is for external contributor convenience.
# Never auto-process branches arriving via this path without a PR.
# Eval pipeline and extract cron only act on PRs, not raw branches.
set -euo pipefail
LOG="/opt/teleo-eval/logs/sync.log"
LOCKFILE="/tmp/sync-mirror.lock"
PIPELINE_DB="/opt/teleo-eval/pipeline/pipeline.db"
GITHUB_PAT_FILE="/opt/teleo-eval/secrets/github-pat"
# (forgejo_owner_repo, github_owner_repo, bare_path, mode)
# mode: bidirectional | main_only
MIRROR_REPOS=(
"teleo/teleo-codex living-ip/teleo-codex /opt/teleo-eval/mirror/teleo-codex.git bidirectional"
"teleo/teleo-infrastructure living-ip/teleo-infrastructure /opt/teleo-eval/mirror/teleo-infrastructure.git main_only"
)
REPO_TAG="main"
log() { echo "[$(date -Iseconds)] [$REPO_TAG] $1" >> "$LOG"; }
# Lockfile — prevent concurrent runs (single lock for whole script)
if [ -f "$LOCKFILE" ]; then
pid=$(cat "$LOCKFILE" 2>/dev/null)
if kill -0 "$pid" 2>/dev/null; then
exit 0
fi
rm -f "$LOCKFILE"
fi
echo $$ > "$LOCKFILE"
trap 'rm -f "$LOCKFILE"' EXIT
# ─────────────────────────────────────────────────────────────────────────────
# sync_repo: process one mirror entry. Sets module-level FORGEJO_REPO,
# GITHUB_REPO, REPO_DIR, MODE, REPO_TAG used by inner steps.
# ─────────────────────────────────────────────────────────────────────────────
sync_repo() {
FORGEJO_REPO="$1" # e.g. teleo/teleo-codex (path on Forgejo)
GITHUB_REPO="$2" # e.g. living-ip/teleo-codex (path on GitHub)
REPO_DIR="$3" # bare mirror dir
MODE="$4" # bidirectional | main_only
REPO_TAG="${FORGEJO_REPO##*/}" # short name for log prefix
# Pre-flight: bare repo must exist
if [ ! -d "$REPO_DIR" ]; then
log "ERROR: bare repo missing at $REPO_DIR — skipping"
return 0
fi
# Pre-flight: fix permissions if another user touched the mirror dir (Rhea)
BAD_PERMS=$(find "$REPO_DIR" ! -user teleo 2>/dev/null | head -1 || true)
if [ -n "$BAD_PERMS" ]; then
log "Fixing mirror permissions (found: $BAD_PERMS)"
chown -R teleo:teleo "$REPO_DIR" 2>/dev/null || true
fi
cd "$REPO_DIR" || { log "ERROR: cannot cd to $REPO_DIR"; return 0; }
# Step 1: Fetch from Forgejo (must succeed — it's authoritative)
log "Fetching from Forgejo..."
if ! git fetch forgejo --prune >> "$LOG" 2>&1; then
log "ERROR: Forgejo fetch failed — skipping this repo"
return 0
fi
# Step 2: Fetch from GitHub (warn on failure, don't abort)
log "Fetching from GitHub..."
git fetch origin --prune >> "$LOG" 2>&1 || log "WARN: GitHub fetch failed"
# Step 2.1: Fetch GitHub fork PR refs (bidirectional only)
# Fork-based PRs don't create branches on origin — they create refs/pull/N/head.
# main_only repos don't accept fork PRs through the mirror path.
if [ "$MODE" = "bidirectional" ]; then
local PAT
PAT=$(cat "$GITHUB_PAT_FILE" 2>/dev/null | tr -d '[:space:]')
if [ -n "$PAT" ]; then
local OPEN_PRS
OPEN_PRS=$(curl -sf "https://api.github.com/repos/$GITHUB_REPO/pulls?state=open&per_page=100" \
-H "Authorization: token $PAT" 2>/dev/null || echo "[]")
echo "$OPEN_PRS" | python3 -c "
import sys, json
prs = json.load(sys.stdin)
for pr in prs:
head = pr.get('head', {})
base_repo = pr.get('base', {}).get('repo', {}).get('full_name', '')
head_repo = head.get('repo', {}) or {}
head_full = head_repo.get('full_name', '')
if head_full and head_full != base_repo:
print(f\"{pr['number']} {head.get('ref', '')} {head.get('sha', '')}\")
" 2>/dev/null | while read pr_num branch_name head_sha; do
if [ -z "$pr_num" ] || [ -z "$branch_name" ]; then continue; fi
local PR_BRANCH="gh-pr-${pr_num}/${branch_name}"
local EXISTING
EXISTING=$(git rev-parse "refs/heads/$PR_BRANCH" 2>/dev/null || true)
if [ "$EXISTING" = "$head_sha" ]; then continue; fi
git fetch origin "refs/pull/${pr_num}/head:refs/heads/$PR_BRANCH" >> "$LOG" 2>&1 && \
log "Fetched fork PR #$pr_num -> $PR_BRANCH" || \
log "WARN: Failed to fetch fork PR #$pr_num"
done
fi
fi
# Step 2.5: GitHub main -> Forgejo main (ff-only)
# If a PR was merged on GitHub, GitHub main is ahead of Forgejo main.
# Fast-forward Forgejo main to match — safe because ff-only guarantees no divergence.
local GITHUB_MAIN_FF FORGEJO_MAIN_FF
GITHUB_MAIN_FF=$(git rev-parse refs/remotes/origin/main 2>/dev/null || true)
FORGEJO_MAIN_FF=$(git rev-parse refs/remotes/forgejo/main 2>/dev/null || true)
if [ -n "$GITHUB_MAIN_FF" ] && [ -n "$FORGEJO_MAIN_FF" ]; then
if [ "$GITHUB_MAIN_FF" != "$FORGEJO_MAIN_FF" ]; then
if git merge-base --is-ancestor "$FORGEJO_MAIN_FF" "$GITHUB_MAIN_FF"; then
log "GitHub main ($GITHUB_MAIN_FF) ahead of Forgejo main ($FORGEJO_MAIN_FF) — fast-forwarding"
git push forgejo "refs/remotes/origin/main:refs/heads/main" >> "$LOG" 2>&1 && \
log "Forgejo main fast-forwarded to $GITHUB_MAIN_FF" || \
log "WARN: Failed to fast-forward Forgejo main"
fi
fi
fi
# Step 3: Forgejo -> GitHub (primary direction)
log "Syncing Forgejo -> GitHub..."
while read branch; do
[ "$branch" = "HEAD" ] && continue
git update-ref "refs/heads/$branch" "refs/remotes/forgejo/$branch" 2>/dev/null || \
log "WARN: Failed to update ref $branch"
done < <(git for-each-ref --format="%(refname:lstrip=3)" refs/remotes/forgejo/)
# Safety: verify Forgejo main descends from GitHub main before force-pushing
local GITHUB_MAIN FORGEJO_MAIN PUSH_MAIN
GITHUB_MAIN=$(git rev-parse refs/remotes/origin/main 2>/dev/null || true)
FORGEJO_MAIN=$(git rev-parse refs/remotes/forgejo/main 2>/dev/null || true)
PUSH_MAIN=true
if [ -n "$GITHUB_MAIN" ] && [ -n "$FORGEJO_MAIN" ]; then
if ! git merge-base --is-ancestor "$GITHUB_MAIN" "$FORGEJO_MAIN"; then
log "CRITICAL: Forgejo main is NOT a descendant of GitHub main — skipping main push"
log "CRITICAL: GitHub main: $GITHUB_MAIN, Forgejo main: $FORGEJO_MAIN"
PUSH_MAIN=false
fi
fi
if [ "$MODE" = "main_only" ]; then
# Infra-style mirror: push main + tags ONLY. Pre-review agent branches
# (epimetheus/*, ganymede/*, etc.) carry internal context — agent UUIDs,
# in-flight discussion, WIP — and must not land in the public GitHub
# history. (Ganymede review, finding #1.)
if [ "$PUSH_MAIN" = true ]; then
git push origin --force "refs/heads/main:refs/heads/main" >> "$LOG" 2>&1 || \
log "WARN: main push to GitHub failed"
fi
else
# Bidirectional mirror (codex): push all branches so external
# contributors can fork from any branch, not just main.
if [ "$PUSH_MAIN" = true ]; then
git push origin --all --force >> "$LOG" 2>&1 || log "WARN: Push to GitHub failed"
else
# Push all branches except main when main is divergent
while read branch; do
[ "$branch" = "main" ] && continue
[ "$branch" = "HEAD" ] && continue
git push origin --force "refs/heads/$branch:refs/heads/$branch" >> "$LOG" 2>&1 || \
log "WARN: Failed to push $branch to GitHub"
done < <(git for-each-ref --format="%(refname:lstrip=2)" refs/heads/)
fi
fi
git push origin --tags --force >> "$LOG" 2>&1 || log "WARN: Tag push to GitHub failed"
# Step 4: GitHub -> Forgejo + Forgejo PR auto-create (bidirectional only)
if [ "$MODE" = "bidirectional" ]; then
sync_github_to_forgejo_with_prs
fi
# Step 6: Divergence alerting (applies to both modes)
check_divergence
}
# ─────────────────────────────────────────────────────────────────────────────
# Step 4 split out: codex-specific GitHub→Forgejo branch push + PR auto-create.
# Reads FORGEJO_REPO, GITHUB_REPO, PIPELINE_DB, REPO_TAG from sync_repo scope.
# ─────────────────────────────────────────────────────────────────────────────
sync_github_to_forgejo_with_prs() {
log "Checking GitHub-only branches..."
local FORGEJO_HOST="http://localhost:3000/api/v1/repos/$FORGEJO_REPO"
local GITHUB_ONLY
GITHUB_ONLY=$(comm -23 \
<(git for-each-ref --format="%(refname:lstrip=3)" refs/remotes/origin/ | grep -v HEAD | sort) \
<(git for-each-ref --format="%(refname:lstrip=3)" refs/remotes/forgejo/ | grep -v HEAD | sort))
if [ -z "$GITHUB_ONLY" ]; then
log "No new GitHub-only branches"
return 0
fi
local FORGEJO_TOKEN
FORGEJO_TOKEN=$(cat /opt/teleo-eval/secrets/forgejo-admin-token 2>/dev/null)
# Lazy schema for sync-mirror's auto-create tracker. Records (branch, sha)
# pairs we've already auto-created PRs for, so the loop below can skip
# redundant creates after pipeline merge → _delete_remote_branch →
# GitHub-only re-discovery → re-push. Cheap CREATE IF NOT EXISTS on each
# cycle; no migration needed because this table is private to sync-mirror.
sqlite3 "$PIPELINE_DB" "CREATE TABLE IF NOT EXISTS sync_autocreate_tracker (branch TEXT NOT NULL, sha TEXT NOT NULL, pr_number INTEGER, created_at TEXT DEFAULT (datetime('now')), PRIMARY KEY (branch, sha));" 2>/dev/null || true
for branch in $GITHUB_ONLY; do
# Already-tracked gate: if we've previously auto-created a PR for
# this exact (branch, sha), skip the entire push+create sequence.
# Closes the empty-PR loop (research and reweave both observed):
# pipeline merges PR → _delete_remote_branch on Forgejo → next sync
# sees branch GitHub-only (origin still has it) → re-pushes to
# Forgejo → HAS_PR misses (Forgejo ?head= broken; closed PRs scroll
# past 50-item paginated window) → auto-creates fresh PR → pipeline
# merges (empty no-op via cherry-pick / reweave union) → repeat.
# Tracker keys on SHA, so legitimate new commits on the same branch
# produce a new SHA → tracker miss → auto-create proceeds normally.
local BRANCH_SHA TRACKED_PR
if [[ "$branch" == gh-pr-* ]]; then
BRANCH_SHA=$(git rev-parse "refs/heads/$branch" 2>/dev/null || true)
else
BRANCH_SHA=$(git rev-parse "refs/remotes/origin/$branch" 2>/dev/null || true)
fi
if [ -n "$BRANCH_SHA" ]; then
# stderr → $LOG so sustained sqlite3 contention surfaces in ops logs
# rather than silently falling through to a redundant auto-create.
TRACKED_PR=$(sqlite3 "$PIPELINE_DB" "SELECT pr_number FROM sync_autocreate_tracker WHERE branch=$(printf "'%s'" "${branch//\'/\'\'}") AND sha=$(printf "'%s'" "$BRANCH_SHA") LIMIT 1;" 2>>"$LOG" || echo "")
if [ -n "$TRACKED_PR" ]; then
log "Skip auto-create: $branch SHA $BRANCH_SHA already tracked (PR #$TRACKED_PR)"
continue
fi
fi
log "New from GitHub: $branch -> Forgejo"
# Fork PR branches live as local refs (from Step 2.1), not on origin remote
if [[ "$branch" == gh-pr-* ]]; then
git push forgejo "refs/heads/$branch:refs/heads/$branch" >> "$LOG" 2>&1 || {
log "WARN: Failed to push fork PR branch $branch to Forgejo"
continue
}
else
git push forgejo "refs/remotes/origin/$branch:refs/heads/$branch" >> "$LOG" 2>&1 || {
log "WARN: Failed to push $branch to Forgejo"
continue
}
fi
# Skip pipeline-internal branch prefixes (no PR creation)
case "$branch" in
extract/*|ingestion/*) continue ;;
esac
if [ -z "$FORGEJO_TOKEN" ]; then continue; fi
# Check if PR already exists for this branch (open or closed)
# NOTE: Forgejo ?head= filter is broken (ignores head value, returns all PRs).
# Workaround: fetch open+closed PRs, pipe to Python, check head.ref.
local HAS_PR
HAS_PR=$( {
curl -sf "$FORGEJO_HOST/pulls?state=open&limit=50" \
-H "Authorization: token $FORGEJO_TOKEN" 2>/dev/null || echo "[]"
echo ""
curl -sf "$FORGEJO_HOST/pulls?state=closed&sort=created&limit=50" \
-H "Authorization: token $FORGEJO_TOKEN" 2>/dev/null || echo "[]"
} | python3 -c "
import sys, json
branch = sys.argv[1]
for line in sys.stdin:
line = line.strip()
if not line or line == '[]': continue
try:
for pr in json.loads(line):
if pr.get('head', {}).get('ref') == branch:
print('yes'); sys.exit(0)
except: pass
print('no')
" "$branch" 2>/dev/null || echo "no")
if [ "$HAS_PR" = "yes" ]; then continue; fi
# Build PR title — for fork PRs, use the GitHub PR title
local PR_TITLE PAYLOAD RESULT PR_NUM GH_PR_NUM
if [[ "$branch" == gh-pr-* ]]; then
local FORK_GH_NUM PAT_T
FORK_GH_NUM=$(echo "$branch" | sed 's|gh-pr-\([0-9]*\)/.*|\1|')
PAT_T=$(cat "$GITHUB_PAT_FILE" 2>/dev/null | tr -d '[:space:]')
PR_TITLE=$(curl -sf "https://api.github.com/repos/$GITHUB_REPO/pulls/$FORK_GH_NUM" \
-H "Authorization: token $PAT_T" 2>/dev/null | \
python3 -c "import sys,json; print(json.load(sys.stdin).get('title',''))" 2>/dev/null || true)
[ -z "$PR_TITLE" ] && PR_TITLE=$(echo "$branch" | sed 's|/|: |;s/-/ /g')
else
PR_TITLE=$(echo "$branch" | sed 's|/|: |;s/-/ /g')
fi
PAYLOAD=$(python3 -c "import sys,json; print(json.dumps({'title':sys.argv[1],'head':sys.argv[2],'base':'main'}))" "$PR_TITLE" "$branch")
RESULT=$(curl -sf -X POST "$FORGEJO_HOST/pulls" \
-H "Authorization: token $FORGEJO_TOKEN" \
-H "Content-Type: application/json" \
-d "$PAYLOAD" 2>/dev/null || echo "")
PR_NUM=$(echo "$RESULT" | grep -o '"number":[0-9]*' | head -1 | grep -o "[0-9]*" || true)
if [ -z "$PR_NUM" ]; then
log "WARN: Failed to auto-create PR for $branch"
continue
fi
log "Auto-created PR #$PR_NUM on Forgejo for $branch"
# Record (branch, sha, pr_number) so the tracker gate above can short-
# circuit the next time we see this exact (branch, sha) combination.
# INSERT OR IGNORE: idempotent if a concurrent run already inserted.
# WARN log on failure: silent INSERT failure under sustained sqlite3
# contention would mask the loop reappearing on the next cycle (HAS_PR
# only saves us while the closed PR is in the 50-item pagination window).
if [ -n "$BRANCH_SHA" ] && [[ "$PR_NUM" =~ ^[0-9]+$ ]]; then
if ! sqlite3 "$PIPELINE_DB" "INSERT OR IGNORE INTO sync_autocreate_tracker (branch, sha, pr_number) VALUES ($(printf "'%s'" "${branch//\'/\'\'}"), $(printf "'%s'" "$BRANCH_SHA"), $PR_NUM);" 2>>"$LOG"; then
log "WARN: tracker insert failed for $branch SHA $BRANCH_SHA (PR #$PR_NUM) — duplicate auto-create possible next cycle"
fi
fi
# Step 4.5: Link GitHub PR to Forgejo PR in pipeline DB
if [[ "$branch" == gh-pr-* ]]; then
GH_PR_NUM=$(echo "$branch" | sed 's|gh-pr-\([0-9]*\)/.*|\1|')
else
local PAT
PAT=$(cat "$GITHUB_PAT_FILE" 2>/dev/null | tr -d '[:space:]')
GH_PR_NUM=""
if [ -n "$PAT" ]; then
GH_PR_NUM=$(curl -sf "https://api.github.com/repos/$GITHUB_REPO/pulls?head=living-ip:$branch&state=all" \
-H "Authorization: token $PAT" 2>/dev/null | \
python3 -c "import sys,json; prs=json.load(sys.stdin); print(prs[0]['number'] if prs else '')" 2>/dev/null || true)
fi
fi
if [[ "$GH_PR_NUM" =~ ^[0-9]+$ ]] && [[ "$PR_NUM" =~ ^[0-9]+$ ]]; then
sqlite3 "$PIPELINE_DB" "UPDATE prs SET github_pr = $GH_PR_NUM, source_channel = 'github' WHERE number = $PR_NUM;" 2>/dev/null && \
log "Linked GitHub PR #$GH_PR_NUM -> Forgejo PR #$PR_NUM" || \
log "WARN: Failed to link GitHub PR #$GH_PR_NUM to Forgejo PR #$PR_NUM in DB"
fi
done
}
# ─────────────────────────────────────────────────────────────────────────────
# Step 6 split out: divergence alerting. Per-repo state file so each repo
# has its own divergence counter and alert state.
# ─────────────────────────────────────────────────────────────────────────────
check_divergence() {
local DIVERGENCE_FILE="/opt/teleo-eval/logs/.divergence-count.${REPO_TAG}"
git fetch forgejo main --quiet 2>/dev/null || true
git fetch origin main --quiet 2>/dev/null || true
local GH_MAIN_FINAL FG_MAIN_FINAL
GH_MAIN_FINAL=$(git rev-parse refs/remotes/origin/main 2>/dev/null || true)
FG_MAIN_FINAL=$(git rev-parse refs/remotes/forgejo/main 2>/dev/null || true)
if [ -n "$GH_MAIN_FINAL" ] && [ -n "$FG_MAIN_FINAL" ] && [ "$GH_MAIN_FINAL" != "$FG_MAIN_FINAL" ]; then
local PREV
PREV=$(cat "$DIVERGENCE_FILE" 2>/dev/null || echo "0")
if [ "$PREV" = "alerted" ]; then
log "DIVERGENCE: still diverged (already alerted)"
else
local COUNT=$((PREV + 1))
echo "$COUNT" > "$DIVERGENCE_FILE"
log "DIVERGENCE: cycle $COUNT — GitHub=$GH_MAIN_FINAL Forgejo=$FG_MAIN_FINAL"
if [ "$COUNT" -ge 2 ]; then
local BOT_TOKEN ADMIN_CHAT
BOT_TOKEN=$(cat /opt/teleo-eval/secrets/telegram-bot-token 2>/dev/null || true)
ADMIN_CHAT=$(cat /opt/teleo-eval/secrets/admin-chat-id 2>/dev/null || true)
if [ -n "$BOT_TOKEN" ] && [ -n "$ADMIN_CHAT" ]; then
local ALERT_MSG
ALERT_MSG=$(python3 -c "
import json, sys
msg = '⚠️ Mirror divergence detected (' + sys.argv[5] + ')\\n\\n'
msg += f'GitHub main: {sys.argv[1][:8]}\\n'
msg += f'Forgejo main: {sys.argv[2][:8]}\\n'
msg += f'Diverged for {sys.argv[3]} consecutive cycles ({int(sys.argv[3])*2} min)\\n\\n'
msg += 'Check sync-mirror.sh logs: /opt/teleo-eval/logs/sync.log'
print(json.dumps({'chat_id': sys.argv[4], 'text': msg, 'parse_mode': 'HTML'}))
" "$GH_MAIN_FINAL" "$FG_MAIN_FINAL" "$COUNT" "$ADMIN_CHAT" "$REPO_TAG")
if curl -sf -X POST "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d "$ALERT_MSG" >> "$LOG" 2>&1; then
log "DIVERGENCE: alert sent to admin"
echo "alerted" > "$DIVERGENCE_FILE"
else
log "WARN: Failed to send divergence alert (will retry next cycle)"
fi
else
log "WARN: Cannot send divergence alert — missing bot token or admin chat ID"
fi
fi
fi
else
if [ -f "$DIVERGENCE_FILE" ]; then
local PREV
PREV=$(cat "$DIVERGENCE_FILE" 2>/dev/null || echo "0")
if [ "$PREV" != "0" ]; then
log "DIVERGENCE: resolved — repos back in sync"
fi
rm -f "$DIVERGENCE_FILE"
fi
fi
}
# ─────────────────────────────────────────────────────────────────────────────
# Main: process each configured mirror in sequence.
# A failure on one repo doesn't block subsequent repos — sync_repo returns 0
# on most error paths to keep the loop going.
# ─────────────────────────────────────────────────────────────────────────────
REPO_TAG="main"
log "Starting sync cycle"
# Step 0: self-heal any gh-pr-* PR rows missing github_pr.
# Runs FIRST — before per-repo work (branch-mirror loop, auto-create-PR block).
# Recovers from races/transient failures in Step 4.5's one-shot link UPDATE.
# Idempotent: SELECT empty when clean, zero-cost path. Same SELECT/UPDATE
# heals historical orphans (PR 4066 picked up on first cron tick post-deploy)
# and future races on subsequent ticks. The branch name encodes the GitHub PR
# number deterministically (gh-pr-{N}/...) so no API call is required.
if [ -f "$PIPELINE_DB" ]; then
sqlite3 -separator '|' "$PIPELINE_DB" \
"SELECT number, branch FROM prs WHERE branch LIKE 'gh-pr-%' AND github_pr IS NULL;" \
2>/dev/null | while IFS='|' read -r pr_num branch; do
# Regex requires >=1 digit — empty/non-numeric branches fail to parse here,
# not just at the empty-guard below. Keeps SQL-integer-safety load-bearing
# on the regex alone. [0-9][0-9]* is the portable BRE form of [0-9]+,
# works on both GNU sed (VPS) and BSD sed (dev macs).
gh_pr_num=$(echo "$branch" | sed -n 's|^gh-pr-\([0-9][0-9]*\)/.*|\1|p')
[ -z "$gh_pr_num" ] && continue
# Both interpolated values are integer-validated upstream (pr_num from
# INTEGER `number` column, gh_pr_num from regex above). No parametric
# binding available in bash sqlite3 — safety relies on those invariants.
if sqlite3 "$PIPELINE_DB" \
"UPDATE prs SET github_pr = $gh_pr_num, source_channel = 'github' WHERE number = $pr_num;" \
2>/dev/null; then
log "self-heal: linked Forgejo PR #$pr_num -> GitHub PR #$gh_pr_num"
fi
done
fi
for entry in "${MIRROR_REPOS[@]}"; do
# Read the 4 fields. `read` splits on $IFS (whitespace) by default.
read -r forgejo_repo github_repo bare_path mode <<< "$entry"
sync_repo "$forgejo_repo" "$github_repo" "$bare_path" "$mode"
done
REPO_TAG="main"
log "Sync cycle complete"

View file

@ -0,0 +1,47 @@
# Diagnostics Consolidation Diff Log
# Branch: epimetheus/consolidate-infra
# Date: 2026-04-13
## Files with multiple copies — resolution
### alerting.py
- ROOT diagnostics/alerting.py (22320 bytes) — KEPT (newer: has _ALLOWED_DIM_EXPRS SQL injection protection, stricter dim_expr validation)
- ops/diagnostics/alerting.py (22039 bytes) — OVERWRITTEN (missing SQL injection guards)
- VPS /opt/teleo-eval/diagnostics/alerting.py (22039 bytes) — matches ops/ version, needs deploy
### alerting_routes.py
- ROOT diagnostics/alerting_routes.py (4216 bytes) — KEPT (newer: proper try/finally/conn.close, ValueError catch on hours param)
- ops/diagnostics/alerting_routes.py (4043 bytes) — OVERWRITTEN (missing error handling, missing conn.close)
- VPS /opt/teleo-eval/diagnostics/alerting_routes.py (4043 bytes) — matches ops/ version, needs deploy
### vitality.py
- ROOT diagnostics/vitality.py (25548 bytes) — KEPT (only copy in repo, larger than VPS)
- VPS /opt/teleo-eval/diagnostics/vitality.py (18539 bytes) — older version, needs deploy
- MOVED TO: ops/diagnostics/vitality.py
### vitality_routes.py
- ROOT diagnostics/vitality_routes.py (10824 bytes) — KEPT (only copy in repo, larger than VPS)
- VPS /opt/teleo-eval/diagnostics/vitality_routes.py (9729 bytes) — older version, needs deploy
- MOVED TO: ops/diagnostics/vitality_routes.py
## Files moved
| From | To | Reason |
|------|-----|--------|
| diagnostics/vitality.py | ops/diagnostics/vitality.py | Consolidate to canonical location |
| diagnostics/vitality_routes.py | ops/diagnostics/vitality_routes.py | Consolidate to canonical location |
| diagnostics/alerting.py | ops/diagnostics/alerting.py | Newer version overwrites older |
| diagnostics/alerting_routes.py | ops/diagnostics/alerting_routes.py | Newer version overwrites older |
## Root diagnostics/ after consolidation
- PATCH_INSTRUCTIONS.md — kept (documentation, not code)
- evolution.md — kept (documentation)
- weekly/2026-03-25-week3.md — kept (report)
- ops/sessions/*.json — kept (session data)
- All .py files REMOVED from root diagnostics/
## VPS .bak files inventory (30+ files)
All in /opt/teleo-eval/diagnostics/. Git is the backup now. Safe to delete after consolidation verified.
## VPS deploy needed after merge
alerting.py, alerting_routes.py, vitality.py, vitality_routes.py — all local versions are newer than VPS.

View file

@ -28,9 +28,12 @@ import sqlite3
import json
# Non-merged statuses map directly to operation — no semantic classification yet.
NON_MERGED_STATUS_TO_OPERATION = {
'approved': 'new', # about to become knowledge
# Map PR status to Clay's operation color palette
# extract (cyan), new (green), enrich (amber), challenge (red-orange),
# decision (violet), infra (grey)
STATUS_TO_OPERATION = {
'merged': 'new', # green — new knowledge merged
'approved': 'enrich', # amber — approved, enriching KB
'open': 'extract', # cyan — new extraction in progress
'validating': 'extract', # cyan — being validated
'reviewing': 'extract', # cyan — under review
@ -40,51 +43,6 @@ NON_MERGED_STATUS_TO_OPERATION = {
'conflict': 'challenge', # red-orange — conflict detected
}
# Maintenance commit_types that land on main but don't represent new knowledge.
_MAINTENANCE_COMMIT_TYPES = {'fix', 'pipeline', 'reweave'}
def classify_pr_operation(status, commit_type, branch, description=None):
"""Derive a Timeline operation from a PR row.
Priority order for MERGED PRs (commit_type wins over branch prefix
extract/* branches with commit_type='enrich' or 'challenge' classify
by commit_type, matching the contributor-role wiring fix):
1. commit_type == 'challenge' OR branch.startswith('challenge/') OR
description contains 'challenged_by' 'challenge'
2. commit_type == 'enrich' OR branch.startswith('enrich/' | 'reweave/')
'enrich'
3. commit_type in _MAINTENANCE_COMMIT_TYPES 'infra'
4. default (commit_type='knowledge'|'extract'|'research'|'entity' or
anything else) 'new'
For non-merged PRs, falls back to NON_MERGED_STATUS_TO_OPERATION.
"""
commit_type = (commit_type or '').lower()
branch = branch or ''
description_lower = (description or '').lower()
if status != 'merged':
return NON_MERGED_STATUS_TO_OPERATION.get(status, 'infra')
# Challenge takes precedence — the signal is inherently more specific.
if (commit_type == 'challenge'
or branch.startswith('challenge/')
or 'challenged_by' in description_lower):
return 'challenge'
if (commit_type == 'enrich'
or branch.startswith('enrich/')
or branch.startswith('reweave/')):
return 'enrich'
if commit_type in _MAINTENANCE_COMMIT_TYPES:
return 'infra'
# Default: legacy 'knowledge', new 'extract', 'research', 'entity',
# unknown/null commit_type → treat as new knowledge.
return 'new'
# Map audit_log stage to operation type
STAGE_TO_OPERATION = {
'ingest': 'extract',
@ -160,8 +118,6 @@ async def handle_activity(request):
Query params:
limit (int, default 100, max 500): number of events to return
cursor (ISO timestamp): return events older than this timestamp
type (str, optional): comma-separated operation types to include
(extract|new|enrich|challenge|infra). If absent, returns all types.
Derives events from two sources:
1. prs table per-PR events with domain, agent, status
@ -175,13 +131,6 @@ async def handle_activity(request):
limit = 100
cursor = request.query.get('cursor')
type_param = request.query.get('type', '').strip()
allowed_ops = None
if type_param:
allowed_ops = {t.strip() for t in type_param.split(',') if t.strip()}
if not allowed_ops:
allowed_ops = None
db_path = request.app['db_path']
try:
@ -194,27 +143,22 @@ async def handle_activity(request):
# Each PR generates events at created_at and merged_at timestamps
pr_query = """
SELECT number, status, domain, agent, branch, source_path,
created_at, merged_at, source_channel, commit_type,
description
created_at, merged_at
FROM prs
WHERE {where_clause}
ORDER BY COALESCE(merged_at, created_at) DESC
LIMIT ?
"""
# Over-fetch when filtering by type so we have enough matching rows after
# post-build filtering. Cap at 2000 to avoid runaway queries.
fetch_limit = min(2000, limit * 5) if allowed_ops else limit + 1
if cursor:
rows = conn.execute(
pr_query.format(where_clause="COALESCE(merged_at, created_at) < ?"),
(cursor, fetch_limit)
(cursor, limit + 1)
).fetchall()
else:
rows = conn.execute(
pr_query.format(where_clause="1=1"),
(fetch_limit,)
(limit + 1,)
).fetchall()
# Known knowledge agents for branch-prefix inference
@ -222,14 +166,7 @@ async def handle_activity(request):
for row in rows:
row_dict = dict(row)
operation = classify_pr_operation(
row_dict['status'],
row_dict.get('commit_type'),
row_dict.get('branch'),
row_dict.get('description'),
)
if allowed_ops and operation not in allowed_ops:
continue
operation = STATUS_TO_OPERATION.get(row_dict['status'], 'infra')
description = pr_description(row_dict)
# Use merged_at if available (more interesting event), else created_at
@ -252,7 +189,6 @@ async def handle_activity(request):
'description': description,
'status': row_dict['status'],
'pr_number': row_dict['number'],
'source_channel': row_dict.get('source_channel') or 'unknown',
})
# Source 2: Audit log events (secondary — pipeline-level)
@ -281,8 +217,6 @@ async def handle_activity(request):
for row in audit_rows:
row_dict = dict(row)
operation = STAGE_TO_OPERATION.get(row_dict['stage'], 'infra')
if allowed_ops and operation not in allowed_ops:
continue
description = audit_description(row_dict)
events.append({
@ -294,7 +228,6 @@ async def handle_activity(request):
'description': description,
'status': None,
'pr_number': None,
'source_channel': None, # audit events not tied to a PR
})
conn.close()

View file

@ -1,423 +0,0 @@
"""Activity feed API — serves contribution events from pipeline.db."""
import re
import sqlite3
import math
import time
from aiohttp import web
DB_PATH = "/opt/teleo-eval/pipeline/pipeline.db"
_cache = {"data": None, "ts": 0}
CACHE_TTL = 60 # 1 minute — activity should feel fresh
# commit_types we surface in the activity feed. `pipeline` is system
# maintenance (reweave/fix auto-runs, zombie cleanup) and stays hidden.
_FEED_COMMIT_TYPES = ("knowledge", "enrich", "challenge", "research", "entity", "extract", "reweave")
# Source-archive slugs follow YYYY-MM-DD-publisher-topic-HASH4 — they're
# inbox archive filenames, not claim slugs. Used as a fallback signal when
# branch/description heuristics miss (e.g. populated descriptions that
# happen to be source titles, not claim insights).
_SOURCE_SLUG_PATTERN = re.compile(r"^\d{4}-\d{2}-\d{2}-.+-[a-f0-9]{4}$")
def _get_conn():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA busy_timeout = 10000")
return conn
def _is_source_slug(slug):
return bool(slug and _SOURCE_SLUG_PATTERN.match(slug))
def _classify_event(branch, description, commit_type, candidate_slug=None):
"""Return one of: create | enrich | challenge | source | session_digest | None.
Source-archive PRs are extract/* branches that filed a source into
inbox/archive/ but didn't produce a claim. Session-digest PRs are
agent research/entity commits with no per-claim description they
represent session-level rollups, not specific knowledge artifacts.
"""
commit_type_l = (commit_type or "").lower()
branch = branch or ""
description_lower = (description or "").lower()
has_desc = bool(description and description.strip())
if commit_type_l not in _FEED_COMMIT_TYPES:
return None
# Explicit challenge signals win first.
if (commit_type_l == "challenge"
or branch.startswith("challenge/")
or "challenged_by" in description_lower):
return "challenge"
# Enrichment: reweave edge-connects, enrich/ branches, or commit_type=enrich.
if (commit_type_l == "enrich"
or branch.startswith("enrich/")
or branch.startswith("reweave/")):
return "enrich"
# Research and entity commits with no description are session-level
# rollups (e.g. astra/research-2026-05-11). They have no claim to
# link to — surface as session_digest, not as a phantom create.
if commit_type_l in ("research", "entity") and not has_desc:
return "session_digest"
# Source-only: extract/* with no claim description means inbox archive
# landed but no domain claim was written.
if branch.startswith("extract/") and not has_desc:
return "source"
# Belt-and-suspenders: if the slug we'd surface to the frontend looks
# like an inbox archive filename (date-prefix-hash), treat as source
# regardless of branch/commit_type/description state. Catches cases
# where description leaked but is just a source title, not a claim.
if _is_source_slug(candidate_slug):
return "source"
# Everything else with a description is a new claim.
return "create"
# Internal classifier value -> canonical `kind` enum returned to frontend.
_KIND_MAP = {
"create": "claim_merged",
"enrich": "claim_enriched",
"challenge": "claim_challenged",
"source": "source_archived",
"session_digest": "session_digest",
}
def _archive_slug_from_branch(branch):
"""For extract/YYYY-MM-DD-...-HASH4, return YYYY-MM-DD-... (keep date,
drop the 4-hex hash suffix). Matches inbox/archive filename convention.
"""
if not branch or "/" not in branch:
return ""
slug = branch.split("/", 1)[1]
return re.sub(r"-[a-f0-9]{4}$", "", slug)
def _source_target_url(domain, archive_slug):
"""Forgejo blob URL for an archived source file. Falls back to the
repo-wide inbox/archive directory when domain is unknown so the link
still resolves to something useful instead of a 404.
"""
if not archive_slug:
return None
domain = (domain or "").strip()
if not domain or domain == "unknown":
return "https://git.livingip.xyz/teleo/teleo-codex/src/branch/main/inbox/archive"
return (
"https://git.livingip.xyz/teleo/teleo-codex/src/branch/main/inbox/archive/"
f"{domain}/{archive_slug}.md"
)
def _claim_target_url(claim_slug):
if not claim_slug:
return None
return f"/claims/{claim_slug}"
# Canonical clickthrough URL for an activity-feed event.
#
# Every merged PR in the pipeline.db `prs` table lives on Forgejo at
# git.livingip.xyz/teleo/teleo-codex/pulls/{number}. A small subset (3 of
# 4094 as of 2026-05-13) was additionally mirrored to GitHub and has
# prs.github_pr populated. Prefer GitHub when available (more public-facing
# surface), fall back to Forgejo so every row has a real destination
# instead of None (which makes the frontend whole-row overlay no-op and
# leaves pipeline-attributed events looking dead-on-click).
def _pr_url(pr_number, github_pr):
if github_pr:
return f"https://github.com/living-ip/teleo-codex/pull/{github_pr}"
if pr_number:
return f"https://git.livingip.xyz/teleo/teleo-codex/pulls/{pr_number}"
return None
# Canonicalize contributor labels so frontend links resolve to real
# /contributors/{handle} pages. Pipeline writers (extract.py, manual edits,
# the old backfill_submitted_by.py) historically wrote mixed-case agent
# names with a trailing decorator into prs.submitted_by — e.g.
# "Vida (self-directed)", "pipeline (reweave)", or "@m3taversal".
# These decorated strings do not exist as contributors and 404 the profile
# page. Strip the trailing parenthetical wholesale: valid handles match
# ^[a-z0-9][a-z0-9_-]{0,38}$ (see pipeline/lib/attribution._HANDLE_RE) and
# cannot contain parens, so this is lossless.
_TRAILING_PAREN_RE = re.compile(r"\s*\([^)]*\)\s*$")
def _canonicalize(raw):
if not raw:
return ""
h = raw.strip().lower().lstrip("@")
h = _TRAILING_PAREN_RE.sub("", h).strip()
return h
def _normalize_contributor(submitted_by, agent):
name = _canonicalize(submitted_by)
if name:
return name
name = _canonicalize(agent)
if name and name != "pipeline":
return name
return "pipeline"
def _summary_from_branch(branch):
if not branch:
return ""
parts = branch.split("/", 1)
if len(parts) < 2:
return ""
slug = parts[1]
slug = re.sub(r"^[\d-]+-", "", slug) # strip date prefix
slug = re.sub(r"-[a-f0-9]{4}$", "", slug) # strip hash suffix
return slug.replace("-", " ").strip().capitalize()
def _extract_claim_slugs(description, branch=None):
if not description:
if branch:
parts = branch.split("/", 1)
if len(parts) > 1:
return [parts[1]]
return []
titles = [t.strip() for t in description.split("|") if t.strip()]
slugs = []
for title in titles:
slug = title.lower().strip()
slug = "".join(c if c.isalnum() or c in (" ", "-") else "" for c in slug)
slug = slug.replace(" ", "-").strip("-")
if len(slug) > 10:
slugs.append(slug)
return slugs
def _hot_score(challenge_count, enrich_count, signal_count, hours_since):
numerator = challenge_count * 3 + enrich_count * 2 + signal_count
denominator = max(hours_since, 0.5) ** 1.5
return numerator / denominator
def _build_events():
conn = _get_conn()
try:
placeholders = ",".join("?" * len(_FEED_COMMIT_TYPES))
rows = conn.execute(f"""
SELECT p.number, p.branch, p.domain, p.agent, p.submitted_by,
p.merged_at, p.description, p.commit_type, p.cost_usd,
p.source_channel, p.source_path, p.github_pr
FROM prs p
WHERE p.status = 'merged'
AND p.commit_type IN ({placeholders})
AND p.merged_at IS NOT NULL
ORDER BY p.merged_at DESC
LIMIT 2000
""", _FEED_COMMIT_TYPES).fetchall()
events = []
claim_activity = {} # slug -> {challenges, enriches, signals, first_seen}
for row in rows:
slugs = _extract_claim_slugs(row["description"], row["branch"])
candidate_slug = slugs[0] if slugs else ""
event_type = _classify_event(
row["branch"], row["description"], row["commit_type"],
candidate_slug=candidate_slug,
)
if not event_type:
continue
contributor = _normalize_contributor(row["submitted_by"], row["agent"])
# Hide pipeline-attributed events (reweave/*, ingestion/*) from the
# public activity feed. They're automation maintenance, not
# contributions — the daemon re-knits the graph nightly and ingests
# external sources. Internal diagnostics + CI math still see these
# rows in prs / contribution_events; only the public timeline drops
# them. Mirrors the existing _FEED_COMMIT_TYPES filter (which hides
# commit_type='pipeline') along the contributor axis.
if contributor == "pipeline":
continue
merged_at = row["merged_at"] or ""
domain = row["domain"] or "unknown"
kind = _KIND_MAP.get(event_type, event_type)
ci_map = {
"create": 0.35, "enrich": 0.25, "challenge": 0.40,
"source": 0.15, "session_digest": 0.05,
}
ci_earned = ci_map.get(event_type, 0)
# Source events never carry a claim_slug — no claim was written.
# target_url points at the archived file on Forgejo instead.
if event_type == "source":
archive_slug = _archive_slug_from_branch(row["branch"])
summary_text = _summary_from_branch(row["branch"])
source_display_slug = (
summary_text.lower().replace(" ", "-") or row["branch"]
)
events.append({
"kind": kind,
"type": "source",
"target_url": _source_target_url(domain, archive_slug),
"claim_slug": "",
"source_slug": source_display_slug,
"domain": domain,
"contributor": contributor,
"timestamp": merged_at,
"ci_earned": round(ci_earned, 2),
"summary": summary_text,
"pr_number": row["number"],
"pr_url": _pr_url(row["number"], row["github_pr"]),
"source_channel": row["source_channel"] or "unknown",
})
continue
# Session digests have no clickthrough surface yet (per-agent
# session pages not built). target_url=null so frontend renders
# plain text instead of a broken /claims/research-... link.
if event_type == "session_digest":
summary_text = _summary_from_branch(row["branch"]) or "Research session"
events.append({
"kind": kind,
"type": "session_digest",
"target_url": None,
"claim_slug": "",
"domain": domain,
"contributor": contributor,
"timestamp": merged_at,
"ci_earned": round(ci_earned, 2),
"summary": summary_text,
"pr_number": row["number"],
"pr_url": _pr_url(row["number"], row["github_pr"]),
"source_channel": row["source_channel"] or "unknown",
})
continue
for slug in slugs:
if slug not in claim_activity:
claim_activity[slug] = {
"challenges": 0, "enriches": 0, "signals": 0,
"first_seen": merged_at,
}
if event_type == "challenge":
claim_activity[slug]["challenges"] += 1
elif event_type == "enrich":
claim_activity[slug]["enriches"] += 1
else:
claim_activity[slug]["signals"] += 1
summary_text = ""
if row["description"]:
first_title = row["description"].split("|")[0].strip()
if len(first_title) > 120:
first_title = first_title[:117] + "..."
summary_text = first_title
elif row["branch"]:
summary_text = _summary_from_branch(row["branch"])
for slug in (slugs[:1] if slugs else [""]):
events.append({
"kind": kind,
"type": event_type,
"target_url": _claim_target_url(slug),
"claim_slug": slug,
"domain": domain,
"contributor": contributor,
"timestamp": merged_at,
"ci_earned": round(ci_earned, 2),
"summary": summary_text,
"pr_number": row["number"],
"pr_url": _pr_url(row["number"], row["github_pr"]),
"source_channel": row["source_channel"] or "unknown",
})
return events, claim_activity
finally:
conn.close()
def _sort_events(events, claim_activity, sort_mode, now_ts):
if sort_mode == "recent":
events.sort(key=lambda e: e["timestamp"], reverse=True)
elif sort_mode == "hot":
def hot_key(e):
slug = e["claim_slug"]
ca = claim_activity.get(slug, {"challenges": 0, "enriches": 0, "signals": 0})
try:
from datetime import datetime
evt_time = datetime.fromisoformat(e["timestamp"].replace("Z", "+00:00"))
hours = (now_ts - evt_time.timestamp()) / 3600
except (ValueError, AttributeError):
hours = 9999
return _hot_score(ca["challenges"], ca["enriches"], ca["signals"], hours)
events.sort(key=hot_key, reverse=True)
elif sort_mode == "important":
type_rank = {
"challenge": 0, "enrich": 1, "create": 2,
"source": 3, "session_digest": 4,
}
events.sort(key=lambda e: (type_rank.get(e["type"], 5), -len(e["summary"])))
return events
async def handle_activity_feed(request):
sort_mode = request.query.get("sort", "recent")
if sort_mode not in ("hot", "recent", "important"):
sort_mode = "recent"
domain = request.query.get("domain", "")
contributor = request.query.get("contributor", "")
type_param = request.query.get("type", "")
type_filter = {t.strip() for t in type_param.split(",") if t.strip()} if type_param else None
try:
limit = min(int(request.query.get("limit", "20")), 100)
except ValueError:
limit = 20
try:
offset = max(int(request.query.get("offset", "0")), 0)
except ValueError:
offset = 0
now = time.time()
if _cache["data"] is None or (now - _cache["ts"]) > CACHE_TTL:
_cache["data"] = _build_events()
_cache["ts"] = now
events, claim_activity = _cache["data"]
filtered = events
if domain:
filtered = [e for e in filtered if e["domain"] == domain]
if contributor:
filtered = [e for e in filtered if e["contributor"] == contributor]
if type_filter:
# Accept both legacy `type` values (create/enrich/challenge/source/
# session_digest) and canonical `kind` values (claim_merged/etc.) so
# callers can migrate at their own pace.
filtered = [
e for e in filtered
if e["type"] in type_filter or e.get("kind") in type_filter
]
sorted_events = _sort_events(list(filtered), claim_activity, sort_mode, now)
total = len(sorted_events)
page = sorted_events[offset:offset + limit]
return web.json_response({
"events": page,
"total": total,
"sort": sort_mode,
"offset": offset,
"limit": limit,
}, headers={"Access-Control-Allow-Origin": "*"})
def register(app):
app.router.add_get("/api/activity-feed", handle_activity_feed)

View file

@ -67,8 +67,6 @@ def check_agent_health(conn: sqlite3.Connection) -> list[dict]:
now = datetime.now(timezone.utc)
for r in rows:
agent = r["agent"]
if agent in ("unknown", None):
continue
latest = r["latest"]
if not latest:
continue
@ -268,22 +266,24 @@ def check_rejection_spike(conn: sqlite3.Connection) -> list[dict]:
"""Detect single rejection reason exceeding REJECTION_SPIKE_RATIO of recent rejections."""
alerts = []
# Total rejected PRs in 24h (prs.eval_issues is the canonical source — Epimetheus 2026-04-02)
# Total rejections in 24h
total = conn.execute(
"""SELECT COUNT(*) as n FROM prs
WHERE eval_issues IS NOT NULL AND eval_issues != '[]'
AND created_at > datetime('now', '-24 hours')"""
"""SELECT COUNT(*) as n FROM audit_log
WHERE stage='evaluate'
AND event IN ('changes_requested','domain_rejected','tier05_rejected')
AND timestamp > datetime('now', '-24 hours')"""
).fetchone()["n"]
if total < 10:
return alerts # Not enough data
# Count by rejection tag from prs.eval_issues
# Count by rejection tag
tags = conn.execute(
"""SELECT value as tag, COUNT(*) as cnt
FROM prs, json_each(prs.eval_issues)
WHERE eval_issues IS NOT NULL AND eval_issues != '[]'
AND created_at > datetime('now', '-24 hours')
FROM audit_log, json_each(json_extract(detail, '$.issues'))
WHERE stage='evaluate'
AND event IN ('changes_requested','domain_rejected','tier05_rejected')
AND timestamp > datetime('now', '-24 hours')
GROUP BY tag ORDER BY cnt DESC"""
).fetchall()
@ -315,13 +315,16 @@ def check_stuck_loops(conn: sqlite3.Connection) -> list[dict]:
"""Detect agents repeatedly failing on the same rejection reason."""
alerts = []
# Agent + rejection reason from prs table directly (Epimetheus correction 2026-04-02)
# COALESCE: rejection events use $.agent, eval events use $.domain_agent (Epimetheus 2026-03-28)
rows = conn.execute(
"""SELECT agent, value as tag, COUNT(*) as cnt
FROM prs, json_each(prs.eval_issues)
WHERE eval_issues IS NOT NULL AND eval_issues != '[]'
AND agent IS NOT NULL
AND created_at > datetime('now', '-6 hours')
"""SELECT COALESCE(json_extract(detail, '$.agent'), json_extract(detail, '$.domain_agent')) as agent,
value as tag,
COUNT(*) as cnt
FROM audit_log, json_each(json_extract(detail, '$.issues'))
WHERE stage='evaluate'
AND event IN ('changes_requested','domain_rejected','tier05_rejected')
AND timestamp > datetime('now', '-6 hours')
AND COALESCE(json_extract(detail, '$.agent'), json_extract(detail, '$.domain_agent')) IS NOT NULL
GROUP BY agent, tag
HAVING cnt > ?""",
(STUCK_LOOP_THRESHOLD,),
@ -409,13 +412,16 @@ def check_domain_rejection_patterns(conn: sqlite3.Connection) -> list[dict]:
"""Track rejection reason shift per domain — surfaces domain maturity issues."""
alerts = []
# Per-domain rejection breakdown in 24h from prs table (Epimetheus correction 2026-04-02)
# Per-domain rejection breakdown in 24h
rows = conn.execute(
"""SELECT domain, value as tag, COUNT(*) as cnt
FROM prs, json_each(prs.eval_issues)
WHERE eval_issues IS NOT NULL AND eval_issues != '[]'
AND domain IS NOT NULL
AND created_at > datetime('now', '-24 hours')
"""SELECT json_extract(detail, '$.domain') as domain,
value as tag,
COUNT(*) as cnt
FROM audit_log, json_each(json_extract(detail, '$.issues'))
WHERE stage='evaluate'
AND event IN ('changes_requested','domain_rejected','tier05_rejected')
AND timestamp > datetime('now', '-24 hours')
AND json_extract(detail, '$.domain') IS NOT NULL
GROUP BY domain, tag
ORDER BY domain, cnt DESC"""
).fetchall()
@ -467,11 +473,12 @@ def generate_failure_report(conn: sqlite3.Connection, agent: str, hours: int = 2
hours = int(hours) # defensive — callers should pass int, but enforce it
rows = conn.execute(
"""SELECT value as tag, COUNT(*) as cnt,
GROUP_CONCAT(DISTINCT number) as pr_numbers
FROM prs, json_each(prs.eval_issues)
WHERE eval_issues IS NOT NULL AND eval_issues != '[]'
AND agent = ?
AND created_at > datetime('now', ? || ' hours')
GROUP_CONCAT(DISTINCT json_extract(detail, '$.pr')) as pr_numbers
FROM audit_log, json_each(json_extract(detail, '$.issues'))
WHERE stage='evaluate'
AND event IN ('changes_requested','domain_rejected','tier05_rejected')
AND json_extract(detail, '$.agent') = ?
AND timestamp > datetime('now', ? || ' hours')
GROUP BY tag ORDER BY cnt DESC
LIMIT 5""",
(agent, f"-{hours}"),

View file

@ -22,13 +22,10 @@ from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "pipeline"))
from aiohttp import web
from daily_digest_routes import register_daily_digest_routes
from kb_claim_routes import KB_CLAIM_PUBLIC_PATHS, KB_CLAIM_PUBLIC_PREFIXES, register_kb_claim_routes
from kb_proposal_routes import KB_PROPOSAL_PUBLIC_PATHS, register_kb_proposal_routes
from leaderboard_routes import LEADERBOARD_PUBLIC_PATHS, register_leaderboard_routes
from lib.search import search as kb_search, embed_query, search_qdrant
from response_audit_routes import RESPONSE_AUDIT_PUBLIC_PATHS, register_response_audit_routes
from review_queue_routes import register_review_queue_routes
from daily_digest_routes import register_daily_digest_routes
from response_audit_routes import register_response_audit_routes, RESPONSE_AUDIT_PUBLIC_PATHS
from lib.search import search as kb_search, embed_query, search_qdrant
logger = logging.getLogger("argus")
@ -45,7 +42,7 @@ API_KEY_FILE = Path(os.environ.get("ARGUS_API_KEY_FILE", "/opt/teleo-eval/secret
# Endpoints that skip auth (dashboard is public for now, can lock later)
_PUBLIC_PATHS = frozenset({"/", "/prs", "/ops", "/health", "/agents", "/epistemic", "/legacy", "/audit", "/api/metrics", "/api/snapshots", "/api/vital-signs",
"/api/contributors", "/api/domains", "/api/audit", "/api/yield", "/api/cost-per-claim", "/api/fix-rates", "/api/compute-profile", "/api/review-queue", "/api/daily-digest", "/api/search"})
"/api/contributors", "/api/domains", "/api/audit", "/api/yield", "/api/cost-per-claim", "/api/fix-rates", "/api/compute-profile", "/api/review-queue", "/api/daily-digest"})
def _get_db() -> sqlite3.Connection:
@ -511,15 +508,7 @@ def _load_secret(path: Path) -> str | None:
@web.middleware
async def auth_middleware(request, handler):
"""API key check. Public paths skip auth. Protected paths require X-Api-Key header."""
if (
request.path in _PUBLIC_PATHS
or request.path in RESPONSE_AUDIT_PUBLIC_PATHS
or request.path in LEADERBOARD_PUBLIC_PATHS
or request.path in KB_PROPOSAL_PUBLIC_PATHS
or request.path in KB_CLAIM_PUBLIC_PATHS
or request.path.startswith(KB_CLAIM_PUBLIC_PREFIXES)
or request.path.startswith("/api/response-audit/")
):
if request.path in _PUBLIC_PATHS or request.path in RESPONSE_AUDIT_PUBLIC_PATHS or request.path.startswith("/api/response-audit/"):
return await handler(request)
expected = request.app.get("api_key")
if not expected:
@ -674,115 +663,38 @@ async def handle_api_domains(request):
return web.json_response({"domains": breakdown})
def _qdrant_hits_to_results(hits, include_expanded=False):
"""Shape raw Qdrant hits into Ship's chat-API contract."""
results = []
for h in hits:
payload = h.get("payload", {}) or {}
path = payload.get("claim_path", "") or ""
slug = path.rsplit("/", 1)[-1]
if slug.endswith(".md"):
slug = slug[:-3]
results.append({
"slug": slug,
"path": path,
"title": payload.get("claim_title", ""),
"domain": payload.get("domain"),
"confidence": payload.get("confidence"),
"score": round(float(h.get("score", 0.0) or 0.0), 4),
"body_excerpt": payload.get("snippet", "") or "",
})
return results
async def handle_api_search(request):
"""Semantic search over claims via Qdrant.
"""GET /api/search — semantic search over claims via Qdrant + graph expansion.
POST contract (Ship's chat API):
body: {"query": str, "limit": int, "min_score": float?, "domain": str?, "confidence": str?, "exclude": [str]?}
response: {"query": str, "results": [{"slug","path","title","domain","confidence","score","body_excerpt"}], "total": int}
GET (legacy + hackathon debug):
Query params:
q: search query (required)
limit, domain, confidence, exclude, expand
min_score: if set, bypasses two-pass lib threshold (default lib behavior otherwise)
domain: filter by domain (optional)
confidence: filter by confidence level (optional)
limit: max results, default 10 (optional)
exclude: comma-separated claim paths to exclude (optional)
expand: enable graph expansion, default true (optional)
"""
if request.method == "POST":
try:
body = await request.json()
except Exception:
return web.json_response({"error": "invalid JSON body"}, status=400)
query = (body.get("query") or "").strip()
if not query:
return web.json_response({"error": "query required"}, status=400)
try:
limit = min(int(body.get("limit") or 5), 50)
except (TypeError, ValueError):
return web.json_response({"error": "limit must be int"}, status=400)
try:
min_score = float(body.get("min_score") if body.get("min_score") is not None else 0.25)
except (TypeError, ValueError):
return web.json_response({"error": "min_score must be float"}, status=400)
domain = body.get("domain")
confidence = body.get("confidence")
exclude = body.get("exclude") or None
vector = embed_query(query)
if vector is None:
return web.json_response({"error": "embedding failed"}, status=502)
hits = search_qdrant(vector, limit=limit, domain=domain,
confidence=confidence, exclude=exclude,
score_threshold=min_score)
results = _qdrant_hits_to_results(hits)
return web.json_response({"query": query, "results": results, "total": len(results)})
# GET path
query = request.query.get("q", "").strip()
if not query:
return web.json_response({"error": "q parameter required"}, status=400)
domain = request.query.get("domain")
confidence = request.query.get("confidence")
try:
limit = min(int(request.query.get("limit", "10")), 50)
except ValueError:
return web.json_response({"error": "limit must be int"}, status=400)
exclude_raw = request.query.get("exclude", "")
exclude = [p.strip() for p in exclude_raw.split(",") if p.strip()] if exclude_raw else None
expand = request.query.get("expand", "true").lower() != "false"
min_score_raw = request.query.get("min_score")
if min_score_raw is not None:
try:
min_score = float(min_score_raw)
except ValueError:
return web.json_response({"error": "min_score must be float"}, status=400)
vector = embed_query(query)
if vector is None:
return web.json_response({"error": "embedding failed"}, status=502)
hits = search_qdrant(vector, limit=limit, domain=domain,
confidence=confidence, exclude=exclude,
score_threshold=min_score)
direct = _qdrant_hits_to_results(hits)
return web.json_response({
"query": query,
"direct_results": direct,
"expanded_results": [],
"total": len(direct),
})
# Default GET: Layer 1 + Layer 2 via lib
# Use shared search library (Layer 1 + Layer 2)
result = kb_search(query, expand=expand,
domain=domain, confidence=confidence, exclude=exclude)
if "error" in result:
error = result["error"]
if error == "embedding_failed":
return web.json_response({"error": "embedding failed"}, status=502)
return web.json_response({"error": error}, status=500)
return web.json_response(result)
@ -2356,7 +2268,6 @@ def create_app() -> web.Application:
app.router.add_get("/api/contributors", handle_api_contributors)
app.router.add_get("/api/domains", handle_api_domains)
app.router.add_get("/api/search", handle_api_search)
app.router.add_post("/api/search", handle_api_search)
app.router.add_get("/api/audit", handle_api_audit)
app.router.add_get("/audit", handle_audit_page)
app.router.add_post("/api/usage", handle_api_usage)
@ -2366,28 +2277,9 @@ def create_app() -> web.Application:
register_dashboard_routes(app, lambda: _conn_from_app(app))
register_review_queue_routes(app)
register_daily_digest_routes(app, db_path=str(DB_PATH))
register_kb_claim_routes(app)
register_kb_proposal_routes(app)
# Portfolio
from dashboard_portfolio import register_portfolio_routes
register_portfolio_routes(app, lambda: _conn_from_app(app))
# Response audit - cost tracking + reasoning traces
app["db_path"] = str(DB_PATH)
register_response_audit_routes(app)
# Event-sourced leaderboard (Phase B — reads contribution_events directly)
register_leaderboard_routes(app)
# Timeline activity feed (per-PR + audit_log events for dashboard v2)
from activity_endpoint import handle_activity
app.router.add_get("/api/activity", handle_activity)
# Gamification activity feed (hot/recent/important sort)
from activity_feed_api import register as register_activity_feed
register_activity_feed(app)
# Claims browser + detail
from claims_api import register_claims_routes
register_claims_routes(app)
# Contributor profile (handle lookup, leaderboard with action CI)
from contributor_profile_api import register_contributor_routes
register_contributor_routes(app)
app.on_cleanup.append(_cleanup)
return app

View file

@ -79,16 +79,12 @@ def main():
fm = sfm
break
# `submitted_by` is stored as a canonical handle (lowercase, no @, no
# "(self-directed)" / "(reweave)" suffix). Read consumers normalize via
# attribution.normalize_handle, so writing decorated strings produces
# downstream 404s on /contributors/{handle} (livingip-web timeline).
if fm:
proposed_by = fm.get("proposed_by")
intake_tier = fm.get("intake_tier")
if proposed_by:
contributor = proposed_by.strip().strip('"').strip("'").lower().lstrip("@")
contributor = proposed_by.strip().strip('"').strip("'")
elif intake_tier == "research-task":
# Derive agent from branch prefix
prefix = branch.split("/", 1)[0] if "/" in branch else "unknown"
@ -98,12 +94,13 @@ def main():
"clay": "clay", "astra": "astra", "leo": "leo",
"reweave": "pipeline",
}
contributor = agent_map.get(prefix, prefix)
agent = agent_map.get(prefix, prefix)
contributor = f"{agent} (self-directed)"
elif intake_tier == "directed":
contributor = "m3taversal"
contributor = "@m3taversal"
else:
# Default: if source exists but no proposed_by, operator submitted it.
contributor = "m3taversal"
# Default: if source exists but no proposed_by, it was Cory's submission
contributor = "@m3taversal"
if contributor:
conn.execute(
@ -117,19 +114,19 @@ def main():
agent = branch.split("/", 1)[0]
conn.execute(
"UPDATE prs SET submitted_by = ? WHERE number = ?",
(agent, pr["number"]),
(f"{agent} (self-directed)", pr["number"]),
)
updated += 1
elif branch.startswith("reweave/"):
conn.execute(
"UPDATE prs SET submitted_by = 'pipeline' WHERE number = ?",
"UPDATE prs SET submitted_by = 'pipeline (reweave)' WHERE number = ?",
(pr["number"],),
)
updated += 1
else:
# Everything else (extract/, ingestion/, unknown) → operator directed it
# Everything else (extract/, ingestion/, unknown) → Cory directed it
conn.execute(
"UPDATE prs SET submitted_by = 'm3taversal' WHERE number = ?",
"UPDATE prs SET submitted_by = '@m3taversal' WHERE number = ?",
(pr["number"],),
)
updated += 1

View file

@ -1,560 +0,0 @@
"""Claims API — list endpoint + canonical claim detail page.
Owner: Argus
Routes:
GET /api/claims list/filter (frontmatter scan, lightweight)
GET /api/claims/{slug} full claim detail (Ship contract)
GET /api/domains domain rollups for sidebar
The detail endpoint is the canonical /claims/{slug} backend per Ship's
2026-04-29 brief. One round-trip, no N+1 cascade. Wikilinks resolved
server-side via titleslug index built from a tree walk.
"""
import json
import re
import sqlite3
import time
from pathlib import Path
import yaml
from aiohttp import web
# Codex tree roots — claims live in three places (Sourcer Apr 26 fix scope)
CODEX_BASE = Path("/opt/teleo-eval/workspaces/main")
CLAIM_TREES = [CODEX_BASE / "domains", CODEX_BASE / "foundations", CODEX_BASE / "core"]
# pipeline.db for joins (review_records, prs, sources)
DB_PATH = "/opt/teleo-eval/pipeline/pipeline.db"
# In-process caches
_list_cache = {"data": None, "ts": 0}
_LIST_CACHE_TTL = 300 # 5 min — list view tolerates staleness
_index_cache = {"by_title": None, "by_stem": None, "ts": 0}
_INDEX_CACHE_TTL = 60 # 1 min — title→slug index for wikilink resolution
CORS_HEADERS = {"Access-Control-Allow-Origin": "*"}
# Wikilink pattern. [[text]] or [[text|alias]] — we keep the link text only.
_WIKILINK_RE = re.compile(r"\[\[([^\]|#]+?)(?:[#|][^\]]*)?\]\]")
# ─── Normalization ─────────────────────────────────────────────────────────
def _normalize_for_match(s):
"""Collapse a title or slug to a comparable form.
Rules (from Ship's brief — match the link-fixer canonicalization):
- lowercase
- hyphen space tolerant (both single space)
- collapse runs of whitespace
- strip leading/trailing whitespace
- drop trailing punctuation that gets stripped from filenames
(`.`, `?`, `!`, `:`, `--`)
NOTE: lib/attribution.py exposes only normalize_handle today, not the
title normalizer Ship referenced. Implementing inline; if a canonical
helper lands later we point at it.
"""
if not s:
return ""
s = str(s).lower().strip()
# Treat hyphens as spaces, then collapse whitespace runs
s = s.replace("-", " ").replace("_", " ")
s = re.sub(r"\s+", " ", s)
# Strip ASCII punctuation that filenames drop
s = re.sub(r"[^\w\s]", "", s)
return s.strip()
# ─── Frontmatter parse ─────────────────────────────────────────────────────
_CODE_FENCE_WRAPPER_RE = re.compile(r"^\s*```(?:markdown|md)?\s*\n(.*?)\n```\s*$", re.DOTALL)
def _split_frontmatter(text):
"""Return (frontmatter_dict, body_str) or (None, None) if not a claim file.
Tolerates files wrapped in a top-level ```markdown ... ``` code fence
some agents have produced these (e.g. Montreal Protocol claim from Astra,
2024-12-09). Unwrap once before frontmatter detection.
"""
if not text:
return None, None
m = _CODE_FENCE_WRAPPER_RE.match(text)
if m:
text = m.group(1)
text = text.lstrip()
if not text.startswith("---"):
return None, None
try:
end = text.index("\n---", 3)
except ValueError:
return None, None
try:
fm = yaml.safe_load(text[3:end])
except Exception:
return None, None
if not isinstance(fm, dict):
return None, None
body = text[end + 4:].lstrip()
return fm, body
def _read_claim_file(filepath):
"""Read a claim file from disk. Returns (frontmatter, body) or (None, None)."""
try:
text = filepath.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError):
return None, None
return _split_frontmatter(text)
# ─── Tree walk + indexing ──────────────────────────────────────────────────
def _walk_claim_files():
"""Yield Path objects for every .md claim file in domains/, foundations/, core/."""
for root in CLAIM_TREES:
if not root.exists():
continue
for f in root.rglob("*.md"):
if f.name == "_map.md":
continue
yield f
def _build_indexes():
"""Build (title→stem, stem→relpath) indexes for wikilink resolution.
Cached for _INDEX_CACHE_TTL. Pulls from claim-index endpoint when
possible (already cached upstream) and falls back to filesystem walk.
"""
now = time.time()
if _index_cache["by_title"] is not None and now - _index_cache["ts"] < _INDEX_CACHE_TTL:
return _index_cache["by_title"], _index_cache["by_stem"]
by_title = {}
by_stem = {}
for f in _walk_claim_files():
stem = f.stem
rel = str(f.relative_to(CODEX_BASE))
by_stem[stem] = rel
# Index by stem-as-normalized too (covers wikilinks that use the slug)
by_title[_normalize_for_match(stem)] = stem
# Also try parsing the title from frontmatter for higher-fidelity matches
fm, _ = _read_claim_file(f)
if fm:
title = fm.get("title")
if title:
key = _normalize_for_match(title)
if key and key not in by_title:
by_title[key] = stem
_index_cache["by_title"] = by_title
_index_cache["by_stem"] = by_stem
_index_cache["ts"] = now
return by_title, by_stem
def _resolve_wikilinks(body, by_title):
"""Extract [[link]] occurrences from body, return {link_text: slug_or_null}."""
out = {}
for match in _WIKILINK_RE.finditer(body or ""):
link_text = match.group(1).strip()
if not link_text or link_text in out:
continue
norm = _normalize_for_match(link_text)
out[link_text] = by_title.get(norm)
return out
# ─── Edge extraction from frontmatter ──────────────────────────────────────
_EDGE_FIELDS = {
"supports": "supports",
"challenges": "challenges",
"challenged_by": "challenges", # canonical: store as challenges direction
"related": "related",
"related_claims": "related",
"depends_on": "depends_on",
}
def _extract_edges(fm, by_title, by_stem):
"""Return edges dict shaped per Ship's contract.
Each edge is {slug, title, exists}. Slug resolved through title index.
"""
edges = {"supports": [], "challenges": [], "related": [], "depends_on": []}
for fm_key, edge_kind in _EDGE_FIELDS.items():
raw = fm.get(fm_key)
if not raw:
continue
items = raw if isinstance(raw, list) else [raw]
for item in items:
if not isinstance(item, str):
continue
text = item.strip()
# Strip wikilink wrapping if present
text = re.sub(r"^\[\[|\]\]$", "", text)
# Strip pipe annotations: "[[link|alias]]" style or "claim | edge_type | date"
text = text.split("|")[0].strip()
if not text:
continue
# Try title match first, fall back to stem match
slug = by_title.get(_normalize_for_match(text))
if not slug and text in by_stem:
slug = text
edges[edge_kind].append({
"slug": slug,
"title": text,
"exists": slug is not None,
})
return edges
# ─── Source provenance ─────────────────────────────────────────────────────
def _resolve_sourced_from(conn, claim_filepath, fm, title, stem):
"""Build sourced_from list for the claim.
Strategy: find PRs that produced this claim (via prs.description LIKE
or branch slug match), look at prs.source_path inbox archive file
parse that source's frontmatter for title/url. Falls back to the raw
`source` string from the claim's own frontmatter.
Both `title` and `stem` must be non-empty caller (handler) already
falls back stemtitle; passing empty values would leak `LIKE '%%'`
and match unrelated PRs.
"""
out = []
seen_paths = set()
pr_rows = []
if (title or "").strip() and (stem or "").strip():
try:
pr_rows = conn.execute(
"""SELECT DISTINCT source_path
FROM prs
WHERE source_path IS NOT NULL AND source_path != ''
AND (description LIKE ? OR branch LIKE ?)
LIMIT 10""",
(f"%{title}%", f"%{stem}%"),
).fetchall()
except sqlite3.OperationalError:
pr_rows = []
for row in pr_rows:
path = row["source_path"]
if not path or path in seen_paths:
continue
seen_paths.add(path)
out.append(_resolve_source_file(path))
# 2. Fallback: parse raw source frontmatter field if no PR match
if not out:
raw = fm.get("source")
if isinstance(raw, str) and raw.strip():
out.append({"path": None, "title": raw.strip()[:200], "url": None})
return out
def _resolve_source_file(rel_path):
"""Given inbox/archive/... path, parse frontmatter for title+url. Best-effort."""
full = CODEX_BASE / rel_path
entry = {"path": rel_path, "title": None, "url": None}
if full.exists():
fm, _ = _read_claim_file(full)
if fm:
entry["title"] = fm.get("title") or fm.get("source") or rel_path
entry["url"] = fm.get("url")
if not entry["title"]:
# Last resort: derive from filename
entry["title"] = Path(rel_path).stem.replace("-", " ")
return entry
# ─── Reviews + PRs ─────────────────────────────────────────────────────────
def _load_pr_history(conn, title, stem):
"""Find PRs that touched this claim and their reviews.
Both title and stem must be non-empty strings empty leaks `LIKE '%%'`
which matches every PR. Handler already populates a fallback so this
is a defense-in-depth guard.
"""
if not (title or "").strip() or not (stem or "").strip():
return [], []
try:
pr_rows = conn.execute(
"""SELECT number, merged_at, commit_type, agent, branch, status
FROM prs
WHERE merged_at IS NOT NULL
AND (description LIKE ? OR branch LIKE ?)
ORDER BY merged_at ASC
LIMIT 50""",
(f"%{title}%", f"%{stem}%"),
).fetchall()
except sqlite3.OperationalError:
return [], []
prs = [
{
"number": r["number"],
"merged_at": r["merged_at"],
"kind": r["commit_type"] or "unknown",
"agent": r["agent"],
"branch": r["branch"],
}
for r in pr_rows
]
pr_numbers = [p["number"] for p in prs]
if not pr_numbers:
return prs, []
placeholders = ",".join("?" * len(pr_numbers))
try:
review_rows = conn.execute(
f"""SELECT pr_number, reviewer, reviewer_model, outcome,
rejection_reason, notes, reviewed_at
FROM review_records
WHERE pr_number IN ({placeholders})
ORDER BY reviewed_at ASC""",
pr_numbers,
).fetchall()
except sqlite3.OperationalError:
review_rows = []
reviews = [
{
"pr_number": r["pr_number"],
"reviewer": r["reviewer"],
"model": r["reviewer_model"],
"outcome": r["outcome"],
"rejection_reason": r["rejection_reason"],
"notes": r["notes"],
"reviewed_at": r["reviewed_at"],
}
for r in review_rows
]
return prs, reviews
# ─── List view (preserved) ─────────────────────────────────────────────────
def _parse_list_entry(filepath):
fm, body = _read_claim_file(filepath)
if not fm or fm.get("type") != "claim":
return None
links = _WIKILINK_RE.findall(body or "")
paragraphs = [p.strip() for p in (body or "").split("\n\n")
if p.strip() and not p.strip().startswith("#")]
summary = paragraphs[0][:300] if paragraphs else ""
return {
"slug": filepath.stem,
"title": fm.get("title", filepath.stem.replace("-", " ")),
"domain": fm.get("domain", "unknown"),
"confidence": fm.get("confidence", "unknown"),
"agent": fm.get("agent"),
"scope": fm.get("scope"),
"created": str(fm.get("created", "")),
"source": fm.get("source", "") if isinstance(fm.get("source"), str) else "",
"sourcer": fm.get("sourcer", ""),
"wiki_link_count": len(links),
"summary": summary,
"challenged_by": fm.get("challenged_by"),
"related_claims": fm.get("related_claims", []),
}
def _load_all_claims_list():
now = time.time()
if _list_cache["data"] and now - _list_cache["ts"] < _LIST_CACHE_TTL:
return _list_cache["data"]
claims = []
for f in _walk_claim_files():
entry = _parse_list_entry(f)
if entry:
claims.append(entry)
_list_cache["data"] = claims
_list_cache["ts"] = now
return claims
# ─── Handlers ──────────────────────────────────────────────────────────────
async def handle_claims(request):
claims = _load_all_claims_list()
domain = request.query.get("domain")
search = request.query.get("q", "").lower()
confidence = request.query.get("confidence")
agent = request.query.get("agent")
sort = request.query.get("sort", "recent")
filtered = claims
if domain:
filtered = [c for c in filtered if c["domain"] == domain]
if confidence:
filtered = [c for c in filtered if c["confidence"] == confidence]
if agent:
filtered = [c for c in filtered if c["agent"] == agent]
if search:
filtered = [c for c in filtered
if search in c["title"].lower() or search in c["summary"].lower()]
if sort == "recent":
filtered.sort(key=lambda c: c["created"], reverse=True)
elif sort == "alpha":
filtered.sort(key=lambda c: c["title"].lower())
elif sort == "domain":
filtered.sort(key=lambda c: (c["domain"], c["title"].lower()))
limit = min(int(request.query.get("limit", "50")), 200)
offset = int(request.query.get("offset", "0"))
page = filtered[offset:offset + limit]
domain_counts = {}
for c in claims:
domain_counts[c["domain"]] = domain_counts.get(c["domain"], 0) + 1
return web.json_response({
"claims": page,
"total": len(filtered),
"offset": offset,
"limit": limit,
"domains": dict(sorted(domain_counts.items(), key=lambda x: -x[1])),
"confidence_levels": sorted(set(c["confidence"] for c in claims)),
"agents": sorted(set(c["agent"] for c in claims if c["agent"])),
}, headers=CORS_HEADERS)
async def handle_claim_detail(request):
"""GET /api/claims/{slug} — canonical claim detail page (Ship contract).
One round-trip, all data resolved server-side. Wikilinks pre-resolved.
"""
requested_slug = request.match_info["slug"]
by_title, by_stem = _build_indexes()
# Resolution order: exact stem → title-normalized (handles description-derived
# slugs from /api/activity-feed that are longer than on-disk file stems) →
# stem-as-prefix (handles description-derived slugs that are shorter than the
# file stem because the description was truncated upstream).
slug = requested_slug
rel_path = by_stem.get(slug)
if not rel_path:
# Title fallback: requested slug = slugified frontmatter title
norm = _normalize_for_match(requested_slug)
resolved_stem = by_title.get(norm)
if resolved_stem:
slug = resolved_stem
rel_path = by_stem.get(resolved_stem)
if not rel_path:
# Prefix fallback: walk stems sharing a common prefix with the request,
# pick longest match. Anchored at 32 chars to avoid spurious hits.
norm_req = _normalize_for_match(requested_slug)
best_stem = None
best_len = 0
for stem in by_stem:
norm_stem = _normalize_for_match(stem)
common = 0
for a, b in zip(norm_req, norm_stem):
if a != b:
break
common += 1
if common >= 32 and common > best_len:
best_stem = stem
best_len = common
if best_stem:
slug = best_stem
rel_path = by_stem.get(best_stem)
if not rel_path:
return web.json_response({"error": "claim not found", "slug": requested_slug},
status=404, headers=CORS_HEADERS)
filepath = CODEX_BASE / rel_path
fm, body = _read_claim_file(filepath)
if not fm:
# File exists at this stem but has no parseable frontmatter — almost
# always a stray enrichment fragment that landed in domains/ without
# being merged into a parent claim. Surfacing as 404 (no claim here)
# not 500: the caller can't act on it differently anyway.
return web.json_response({"error": "claim not found", "slug": slug,
"reason": "file_no_frontmatter"},
status=404, headers=CORS_HEADERS)
# Open read-only DB connection for this request
conn = sqlite3.connect(f"file:{DB_PATH}?mode=ro", uri=True)
conn.row_factory = sqlite3.Row
try:
title = fm.get("title") or slug.replace("-", " ")
prs, reviews = _load_pr_history(conn, title, slug)
sourced_from = _resolve_sourced_from(conn, filepath, fm, title, slug)
finally:
conn.close()
last_review = None
if reviews:
latest = reviews[-1]
last_review = {
"outcome": latest["outcome"],
"reviewer": latest["reviewer"],
"date": (latest["reviewed_at"] or "")[:10],
}
# secondary_domains: explicit list, or empty
secondary = fm.get("secondary_domains") or fm.get("cross_domain_links") or []
if isinstance(secondary, str):
secondary = [secondary]
description = fm.get("description") or ""
edges = _extract_edges(fm, by_title, by_stem)
wikilinks = _resolve_wikilinks(body, by_title)
response = {
"slug": slug,
"title": title,
"domain": fm.get("domain", "unknown"),
"secondary_domains": secondary,
"confidence": fm.get("confidence", "unknown"),
"description": description,
"created": str(fm.get("created", "")),
"last_review": last_review,
"body": body or "",
"sourced_from": sourced_from,
"reviews": reviews,
"prs": prs,
"edges": edges,
"wikilinks": wikilinks,
}
return web.json_response(response, headers=CORS_HEADERS)
async def handle_domains(request):
claims = _load_all_claims_list()
domains = {}
for c in claims:
d = c["domain"]
if d not in domains:
domains[d] = {"name": d, "count": 0, "agents": set(), "confidence_dist": {}}
domains[d]["count"] += 1
if c["agent"]:
domains[d]["agents"].add(c["agent"])
conf = c["confidence"]
domains[d]["confidence_dist"][conf] = domains[d]["confidence_dist"].get(conf, 0) + 1
result = []
for d in sorted(domains.values(), key=lambda x: -x["count"]):
d["agents"] = sorted(d["agents"])
result.append(d)
return web.json_response(result, headers=CORS_HEADERS)
def register_claims_routes(app):
app.router.add_get("/api/claims", handle_claims)
app.router.add_get("/api/claims/{slug}", handle_claim_detail)
app.router.add_get("/api/domains", handle_domains)

View file

@ -1,365 +0,0 @@
"""Contributor profile API — GET /api/contributors/{handle}"""
import sqlite3
import json
import os
import re
import subprocess
from datetime import datetime
DB_PATH = os.environ.get("PIPELINE_DB", "/opt/teleo-eval/pipeline/pipeline.db")
SYSTEM_ACCOUNTS = {"pipeline", "unknown", "teleo-agents", "teleo pipeline"}
CODEX_PATH = "/opt/teleo-eval/workspaces/main"
CI_WEIGHTS = {
"sourcer": 0.15,
"extractor": 0.05,
"challenger": 0.35,
"synthesizer": 0.25,
"reviewer": 0.20,
}
FOUNDING_CUTOFF = "2026-03-15"
BADGE_DEFS = {
"FOUNDING CONTRIBUTOR": {"rarity": "limited", "desc": "Contributed during pre-launch phase"},
"BELIEF MOVER": {"rarity": "rare", "desc": "Challenge that led to a claim revision"},
"KNOWLEDGE SOURCER": {"rarity": "uncommon", "desc": "Source that generated 3+ claims"},
"DOMAIN SPECIALIST": {"rarity": "rare", "desc": "Top 3 CI contributor in a domain"},
"VETERAN": {"rarity": "uncommon", "desc": "10+ accepted contributions"},
"FIRST BLOOD": {"rarity": "common", "desc": "First contribution of any kind"},
"CONTRIBUTOR": {"rarity": "common", "desc": "Account created + first accepted contribution"},
}
def _get_conn():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
def _compute_ci(row):
total = 0
for role, weight in CI_WEIGHTS.items():
total += (row.get(f"{role}_count", 0) or 0) * weight
return round(total, 2)
def _compute_badges(handle, row, domain_breakdown, conn):
badges = []
first = row.get("first_contribution", "")
if first and first <= FOUNDING_CUTOFF:
badges.append("FOUNDING CONTRIBUTOR")
claims = row.get("claims_merged", 0) or 0
if claims > 0:
badges.append("CONTRIBUTOR")
badges.append("FIRST BLOOD")
if claims >= 10:
badges.append("VETERAN")
challenger = row.get("challenger_count", 0) or 0
challenge_ci = row.get("_challenge_count_from_scores", 0)
if challenger > 0 or challenge_ci > 0:
badges.append("BELIEF MOVER")
sourcer = row.get("sourcer_count", 0) or 0
if sourcer >= 3:
badges.append("KNOWLEDGE SOURCER")
return badges
def _get_domain_breakdown(handle, conn):
rows = conn.execute("""
SELECT domain, COUNT(*) as cnt
FROM prs
WHERE status='merged' AND (LOWER(agent)=LOWER(?) OR LOWER(submitted_by)=LOWER(?))
AND domain IS NOT NULL
GROUP BY domain ORDER BY cnt DESC
""", (handle, handle)).fetchall()
return {r["domain"]: r["cnt"] for r in rows}
def _get_contribution_timeline(handle, conn, limit=20):
rows = conn.execute("""
SELECT number, domain, status, created_at, description, commit_type, source_path
FROM prs
WHERE status='merged' AND (LOWER(agent)=LOWER(?) OR LOWER(submitted_by)=LOWER(?))
ORDER BY created_at DESC LIMIT ?
""", (handle, handle, limit)).fetchall()
timeline = []
for r in rows:
desc = r["description"] or ""
if not desc and r["source_path"]:
desc = os.path.basename(r["source_path"]).replace("-", " ").replace(".md", "")
timeline.append({
"pr_number": r["number"],
"domain": r["domain"],
"date": r["created_at"][:10] if r["created_at"] else None,
"type": _classify_commit(r["commit_type"]),
"summary": desc[:200] if desc else None,
})
return timeline
def _classify_commit(commit_type):
if not commit_type:
return "create"
ct = commit_type.lower()
if "challenge" in ct:
return "challenge"
if "enrich" in ct or "update" in ct or "reweave" in ct:
return "enrich"
return "create"
def _get_review_stats(handle, conn):
rows = conn.execute("""
SELECT outcome, COUNT(*) as cnt
FROM review_records
WHERE LOWER(agent) = LOWER(?)
GROUP BY outcome
""", (handle,)).fetchall()
stats = {}
for r in rows:
stats[r["outcome"]] = r["cnt"]
return stats
def _get_action_ci(handle, conn):
"""Get action-type CI from contribution_scores table.
Checks both exact handle and common variants (with/without suffix).
"""
h = handle.lower()
base = re.sub(r"[-_]\w+\d+$", "", h)
variants = list({h, base}) if base and base != h else [h]
try:
placeholders = ",".join("?" for _ in variants)
rows = conn.execute(f"""
SELECT event_type, SUM(ci_earned) as total, COUNT(*) as cnt
FROM contribution_scores
WHERE LOWER(contributor) IN ({placeholders})
GROUP BY event_type
""", variants).fetchall()
except Exception:
return None
if not rows:
return None
breakdown = {}
total = 0.0
for r in rows:
breakdown[r["event_type"]] = {
"count": r["cnt"],
"ci": round(r["total"], 4),
}
total += r["total"]
return {
"total": round(total, 4),
"breakdown": breakdown,
}
def _get_git_contributor(handle):
"""Fallback: check git log for contributors not in pipeline.db."""
try:
result = subprocess.run(
["git", "log", "--all", "--format=%H|%an|%ae|%aI", "--diff-filter=A", "--", "domains/"],
capture_output=True, text=True, cwd=CODEX_PATH, timeout=30
)
if result.returncode != 0:
return None
claims = []
for line in result.stdout.strip().split("\n"):
if not line:
continue
parts = line.split("|", 3)
if len(parts) < 4:
continue
sha, name, email, date = parts
if handle.lower() in name.lower() or handle.lower() in email.lower():
claims.append({"sha": sha, "author": name, "email": email, "date": date[:10]})
if not claims:
return None
return {
"handle": handle,
"display_name": claims[0]["author"],
"email": claims[0]["email"],
"first_contribution": min(c["date"] for c in claims),
"last_contribution": max(c["date"] for c in claims),
"claims_merged": len(claims),
"sourcer_count": 0,
"extractor_count": 0,
"challenger_count": 0,
"synthesizer_count": 0,
"reviewer_count": 0,
}
except Exception:
return None
def get_contributor_profile(handle):
conn = _get_conn()
try:
row = conn.execute(
"SELECT * FROM contributors WHERE LOWER(handle) = LOWER(?)", (handle,)
).fetchone()
if row:
data = dict(row)
else:
git_data = _get_git_contributor(handle)
if git_data:
data = git_data
else:
return None
ci_score = _compute_ci(data)
action_ci = _get_action_ci(handle, conn)
domain_breakdown = _get_domain_breakdown(handle, conn)
timeline = _get_contribution_timeline(handle, conn)
review_stats = _get_review_stats(handle, conn)
if action_ci and "challenge" in action_ci.get("breakdown", {}):
data["_challenge_count_from_scores"] = action_ci["breakdown"]["challenge"]["count"]
badges = _compute_badges(handle, data, domain_breakdown, conn)
# For git-only contributors, build domain breakdown from git
if not domain_breakdown and not row:
domain_breakdown = _git_domain_breakdown(handle)
hero_badge = None
rarity_order = ["limited", "rare", "uncommon", "common"]
for rarity in rarity_order:
for b in badges:
if BADGE_DEFS.get(b, {}).get("rarity") == rarity:
hero_badge = b
break
if hero_badge:
break
role_breakdown = {
"sourcer": data.get("sourcer_count", 0) or 0,
"extractor": data.get("extractor_count", 0) or 0,
"challenger": data.get("challenger_count", 0) or 0,
"synthesizer": data.get("synthesizer_count", 0) or 0,
"reviewer": data.get("reviewer_count", 0) or 0,
}
total_roles = sum(role_breakdown.values())
role_pct = {}
for k, v in role_breakdown.items():
role_pct[k] = round(v / total_roles * 100) if total_roles > 0 else 0
return {
"handle": data.get("handle", handle),
"display_name": data.get("display_name"),
"ci_score": ci_score,
"action_ci": action_ci,
"primary_ci": action_ci["total"] if action_ci else ci_score,
"hero_badge": hero_badge,
"badges": [{"name": b, **BADGE_DEFS.get(b, {})} for b in badges],
"joined": data.get("first_contribution"),
"last_active": data.get("last_contribution"),
"claims_merged": data.get("claims_merged", 0) or 0,
"principal": data.get("principal"),
"role_breakdown": role_breakdown,
"role_percentages": role_pct,
"domain_breakdown": domain_breakdown,
"review_stats": review_stats,
"contribution_timeline": timeline,
"active_domains": list(domain_breakdown.keys()),
}
finally:
conn.close()
def _git_domain_breakdown(handle):
"""For git-only contributors, count claims by domain from file paths."""
try:
result = subprocess.run(
["git", "log", "--all", "--name-only", "--format=COMMIT|%an", "--diff-filter=A", "--", "domains/"],
capture_output=True, text=True, cwd=CODEX_PATH, timeout=30
)
if result.returncode != 0:
return {}
domains = {}
current_match = False
for line in result.stdout.strip().split("\n"):
if line.startswith("COMMIT|"):
author = line.split("|", 1)[1]
current_match = handle.lower() in author.lower()
elif current_match and line.startswith("domains/"):
parts = line.split("/")
if len(parts) >= 2:
domain = parts[1]
domains[domain] = domains.get(domain, 0) + 1
return domains
except Exception:
return {}
async def handle_contributor_profile(request):
from aiohttp import web
handle = request.match_info["handle"]
profile = get_contributor_profile(handle)
if profile is None:
return web.json_response({"error": f"Contributor '{handle}' not found"}, status=404)
return web.json_response(profile)
async def handle_contributors_list(request):
from aiohttp import web
conn = _get_conn()
try:
min_claims = int(request.query.get("min_claims", "1"))
rows = conn.execute("""
SELECT handle, display_name, first_contribution, last_contribution,
sourcer_count, extractor_count, challenger_count, synthesizer_count,
reviewer_count, claims_merged, principal
FROM contributors
WHERE claims_merged >= ?
ORDER BY claims_merged DESC
""", (min_claims,)).fetchall()
contributors = []
for r in rows:
data = dict(r)
if data["handle"].lower() in SYSTEM_ACCOUNTS:
continue
ci = _compute_ci(data)
action_ci = _get_action_ci(data["handle"], conn)
action_total = action_ci["total"] if action_ci else 0.0
contributors.append({
"handle": data["handle"],
"display_name": data["display_name"],
"ci_score": ci,
"action_ci": action_total,
"primary_ci": action_total if action_total > 0 else ci,
"claims_merged": data["claims_merged"],
"first_contribution": data["first_contribution"],
"last_contribution": data["last_contribution"],
"principal": data["principal"],
})
return web.json_response({
"contributors": contributors,
"total": len(contributors),
})
finally:
conn.close()
def register_contributor_routes(app):
app.router.add_get("/api/contributors/list", handle_contributors_list)
app.router.add_get("/api/contributors/{handle}", handle_contributor_profile)

View file

@ -74,7 +74,7 @@ def render_epistemic_page(vital_signs: dict, now: datetime) -> str:
<div style="font-size:40px;margin-bottom:12px;opacity:0.3">&#9881;</div>
<div style="color:#8b949e">
Multi-model agreement rate requires the <code>model_evals</code> table.<br>
<span style="font-size:12px">Blocked on: model_evals table creation (Ship Phase 3)</span>
<span style="font-size:12px">Blocked on: model_evals table creation (Theseus 2 Phase 3)</span>
</div>
<div style="margin-top:16px;font-size:12px;color:#8b949e">
Current eval models: Haiku (triage), GPT-4o (domain), Sonnet/Opus (Leo).<br>
@ -194,6 +194,12 @@ fetch('/api/review-summary?days=30')
reasonRows += '<tr><td><code>' + esc(r.reason) + '</code></td><td>' + r.count + '</td></tr>';
}}
// Disagreement types
let disagreeRows = '';
for (const d of (data.disagreement_types || [])) {{
disagreeRows += '<tr><td>' + esc(d.type) + '</td><td>' + d.count + '</td></tr>';
}}
el.innerHTML = `
<div class="grid">
<div class="card"><div class="label">Total Reviews</div><div class="hero-value">${{data.total}}</div></div>
@ -209,6 +215,13 @@ fetch('/api/review-summary?days=30')
${{reasonRows || '<tr><td colspan="2" style="color:#8b949e">No rejections</td></tr>'}}
</table>
</div>
<div class="card">
<div style="font-weight:600;margin-bottom:8px">Disagreement Types</div>
<table>
<tr><th>Type</th><th>Count</th></tr>
${{disagreeRows || '<tr><td colspan="2" style="color:#8b949e">No disagreements</td></tr>'}}
</table>
</div>
</div>`;
}}).catch(() => {{
document.getElementById('review-container').innerHTML =

View file

@ -1,408 +0,0 @@
"""Portfolio dashboard — fixes empty chart by:
1. Computing NAV server-side in the history API (not client-side from nulls)
2. Only returning dates with valid NAV data
3. Showing data points when sparse
"""
import json
import sqlite3
import logging
from html import escape as esc
from datetime import datetime, timezone
from aiohttp import web
from shared_ui import render_page
logger = logging.getLogger("argus.portfolio")
CSS = """
.hero-chart { background: #161b22; border: 1px solid #30363d; border-radius: 8px; padding: 20px; margin-bottom: 20px; }
.hero-chart h2 { color: #c9d1d9; font-size: 18px; margin-bottom: 12px; }
.range-btns { display: flex; gap: 4px; margin-bottom: 12px; }
.range-btn { background: #21262d; border: 1px solid #30363d; color: #8b949e; padding: 5px 14px;
border-radius: 4px; cursor: pointer; font-size: 12px; }
.range-btn.active { background: #1f6feb33; border-color: #58a6ff; color: #58a6ff; }
.ptable-wrap { overflow-x: auto; margin-top: 20px; }
.ptable { width: 100%; border-collapse: collapse; font-size: 13px; }
.ptable th { background: #161b22; color: #8b949e; font-size: 11px; text-transform: uppercase;
letter-spacing: 0.5px; padding: 10px 12px; text-align: right; border-bottom: 1px solid #30363d;
cursor: pointer; user-select: none; white-space: nowrap; }
.ptable th:first-child { text-align: left; position: sticky; left: 0; background: #161b22; z-index: 1; }
.ptable th:hover { color: #c9d1d9; }
.ptable th.sorted-asc::after { content: ' \\25B2'; font-size: 9px; }
.ptable th.sorted-desc::after { content: ' \\25BC'; font-size: 9px; }
.ptable td { padding: 10px 12px; text-align: right; border-bottom: 1px solid #21262d; color: #c9d1d9; }
.ptable td:first-child { text-align: left; position: sticky; left: 0; background: #0d1117; z-index: 1; font-weight: 600; }
.ptable tr:hover td { background: #161b22; }
.ptable tr:hover td:first-child { background: #161b22; }
.summary-row td { font-weight: 700; border-top: 2px solid #30363d; background: #161b22 !important; }
.premium { color: #f85149; }
.discount { color: #3fb950; }
.near-nav { color: #d29922; }
"""
def _fmt_usd(v):
if v is None:
return '\u2014'
if abs(v) >= 1_000_000:
return f'${v / 1_000_000:.1f}M'
if abs(v) >= 1_000:
return f'${v / 1_000:.0f}K'
return f'${v:,.0f}'
def _fmt_price(v):
if v is None:
return '\u2014'
if v >= 100:
return f'${v:,.0f}'
if v >= 1:
return f'${v:.2f}'
if v >= 0.01:
return f'${v:.4f}'
return f'${v:.6f}'
def _fmt_ratio(v):
if v is None or v == 0:
return '\u2014'
return f'{v:.2f}x'
def _ratio_class(v):
if v is None or v == 0:
return ''
if v > 1.5:
return 'premium'
if v < 0.9:
return 'discount'
if v <= 1.1:
return 'near-nav'
return ''
def render_portfolio_page(coins: list[dict], now: datetime) -> str:
if not coins:
body = '<div style="padding:40px;text-align:center;color:#8b949e;">No coin data yet.</div>'
return render_page("Portfolio", "Ownership coin portfolio", "/portfolio", body,
extra_css=CSS, timestamp=now.strftime("%Y-%m-%d %H:%M UTC"))
total_mcap = sum(c.get('market_cap_usd') or 0 for c in coins)
total_treasury = sum(c.get('treasury_usd') or 0 for c in coins)
hero_chart = """
<div class="hero-chart">
<h2>Price / NAV per Token</h2>
<div class="range-btns">
<button class="range-btn" onclick="setRange(this, 30)">30d</button>
<button class="range-btn active" onclick="setRange(this, 90)">90d</button>
<button class="range-btn" onclick="setRange(this, 180)">180d</button>
<button class="range-btn" onclick="setRange(this, 365)">All</button>
</div>
<canvas id="ratio-chart" height="320" style="max-height:320px"></canvas>
</div>
"""
header = """<div class="ptable-wrap"><table class="ptable" id="coin-table">
<thead><tr>
<th data-col="name">Coin</th>
<th data-col="price">Price</th>
<th data-col="nav">NAV / Token</th>
<th data-col="ratio">Price / NAV</th>
<th data-col="treasury">Treasury</th>
<th data-col="mcap">Market Cap</th>
</tr></thead><tbody>"""
rows = ''
for c in coins:
name = c.get('name', '?')
ticker = c.get('ticker', '')
price = c.get('price_usd')
nav = c.get('nav_per_token')
ratio = c.get('price_nav_ratio')
treasury = c.get('treasury_usd')
mcap = c.get('market_cap_usd')
label = esc(name)
if ticker:
label += f' <span style="color:#8b949e;font-size:11px;">{esc(ticker)}</span>'
rows += f"""<tr>
<td>{label}</td>
<td>{_fmt_price(price)}</td>
<td>{_fmt_price(nav)}</td>
<td class="{_ratio_class(ratio)}">{_fmt_ratio(ratio)}</td>
<td>{_fmt_usd(treasury)}</td>
<td>{_fmt_usd(mcap)}</td>
</tr>"""
rows += f"""<tr class="summary-row">
<td>Total ({len(coins)})</td>
<td></td><td></td><td></td>
<td>{_fmt_usd(total_treasury)}</td>
<td>{_fmt_usd(total_mcap)}</td>
</tr>"""
table = header + rows + '</tbody></table></div>'
scripts = """<script>
const COLORS = ['#58a6ff','#3fb950','#f0883e','#d29922','#f85149','#bc8cff','#39d353','#79c0ff','#ff7b72','#a5d6ff'];
let chart = null;
function setRange(btn, days) {
document.querySelectorAll('.range-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
loadChart(days);
}
function loadChart(days) {
fetch('/api/portfolio/nav-ratios?days=' + days)
.then(r => r.json())
.then(data => {
const dates = data.dates || [];
const series = data.series || {};
if (dates.length === 0) {
if (chart) chart.destroy();
chart = null;
const ctx = document.getElementById('ratio-chart').getContext('2d');
ctx.fillStyle = '#8b949e';
ctx.font = '14px sans-serif';
ctx.textAlign = 'center';
ctx.fillText('No NAV data yet — accumulating daily snapshots', ctx.canvas.width / 2, 160);
return;
}
const sparse = dates.length <= 10;
const datasets = [];
let i = 0;
for (const [name, ratios] of Object.entries(series)) {
const hasData = ratios.some(v => v !== null);
if (!hasData) { i++; continue; }
datasets.push({
label: name,
data: ratios,
borderColor: COLORS[i % COLORS.length],
backgroundColor: COLORS[i % COLORS.length] + '33',
borderWidth: 2,
tension: 0.3,
spanGaps: true,
pointRadius: sparse ? 4 : 0,
pointHoverRadius: 6,
fill: false,
});
i++;
}
if (chart) chart.destroy();
const ctx = document.getElementById('ratio-chart').getContext('2d');
chart = new Chart(ctx, {
type: 'line',
data: { labels: dates, datasets },
options: {
responsive: true,
maintainAspectRatio: false,
interaction: { mode: 'index', intersect: false },
plugins: {
legend: { labels: { color: '#8b949e', font: { size: 11 }, usePointStyle: true, boxWidth: 8 }, position: 'top' },
tooltip: { mode: 'index', intersect: false,
callbacks: { label: ctx => ctx.dataset.label + ': ' + (ctx.parsed.y != null ? ctx.parsed.y.toFixed(2) + 'x' : 'n/a') }
},
annotation: {
annotations: {
navLine: {
type: 'line',
yMin: 1, yMax: 1,
borderColor: '#3fb95088',
borderWidth: 2,
borderDash: [6, 4],
label: {
display: true,
content: '1.0x = NAV',
position: 'end',
backgroundColor: '#3fb95033',
color: '#3fb950',
font: { size: 10 },
}
}
}
}
},
scales: {
x: { ticks: { color: '#8b949e', maxTicksLimit: 12 }, grid: { display: false } },
y: { ticks: { color: '#8b949e', callback: v => v.toFixed(1) + 'x' }, grid: { color: '#21262d' },
suggestedMin: 0 }
}
}
});
});
}
// Table sorting
function sortTable(col) {
const table = document.getElementById('coin-table');
const tbody = table.querySelector('tbody');
const rows = Array.from(tbody.querySelectorAll('tr:not(.summary-row)'));
const summaryRow = tbody.querySelector('.summary-row');
const th = table.querySelectorAll('th')[col];
const asc = th.classList.contains('sorted-asc');
table.querySelectorAll('th').forEach(h => h.classList.remove('sorted-asc','sorted-desc'));
th.classList.add(asc ? 'sorted-desc' : 'sorted-asc');
rows.sort((a, b) => {
let va = a.cells[col].textContent.replace(/[$,+%x\\u2014]/g,'').trim();
let vb = b.cells[col].textContent.replace(/[$,+%x\\u2014]/g,'').trim();
const na = parseFloat(va) || 0, nb = parseFloat(vb) || 0;
if (col === 0) return asc ? vb.localeCompare(va) : va.localeCompare(vb);
return asc ? na - nb : nb - na;
});
rows.forEach(r => tbody.appendChild(r));
if (summaryRow) tbody.appendChild(summaryRow);
}
document.querySelectorAll('#coin-table th').forEach((th, i) => {
th.addEventListener('click', () => sortTable(i));
});
loadChart(90);
</script>"""
body = hero_chart + table
return render_page("Portfolio", "Ownership coin portfolio", "/portfolio", body,
scripts=scripts, extra_css=CSS,
timestamp=now.strftime("%Y-%m-%d %H:%M UTC"))
# ── API handlers ────────────────────────────────────────────────────────────
def _get_db(request):
return request.app["_portfolio_conn"]()
def _compute_nav(row):
"""Compute NAV per token and Price/NAV ratio from a snapshot row dict."""
treas = (row.get('treasury_multisig_usd') or 0) + (row.get('lp_usdc_total') or 0)
adj = row.get('adjusted_circulating_supply') or 0
price = row.get('price_usd') or 0
nav = treas / adj if adj > 0 else 0
ratio = price / nav if nav > 0 else 0
return treas, nav, ratio
async def handle_portfolio_page(request):
conn = _get_db(request)
try:
rows = conn.execute("""
SELECT * FROM coin_snapshots
WHERE snapshot_date = (SELECT MAX(snapshot_date) FROM coin_snapshots)
ORDER BY market_cap_usd DESC
""").fetchall()
coins = []
for r in rows:
d = dict(r)
treas, nav, ratio = _compute_nav(d)
d['treasury_usd'] = treas
d['nav_per_token'] = nav
d['price_nav_ratio'] = ratio
coins.append(d)
now = datetime.now(timezone.utc)
html = render_portfolio_page(coins, now)
return web.Response(text=html, content_type='text/html')
finally:
conn.close()
async def handle_nav_ratios(request):
"""Server-side computed NAV ratios — only returns dates with valid data."""
conn = _get_db(request)
try:
try:
days = min(int(request.query.get('days', '90')), 365)
except (ValueError, TypeError):
days = 90
rows = conn.execute("""
SELECT name, snapshot_date, price_usd, treasury_multisig_usd,
lp_usdc_total, adjusted_circulating_supply
FROM coin_snapshots
WHERE snapshot_date >= date('now', ? || ' days')
AND adjusted_circulating_supply IS NOT NULL
AND adjusted_circulating_supply > 0
ORDER BY name, snapshot_date
""", (f'-{days}',)).fetchall()
coin_ratios = {}
all_dates = set()
for r in rows:
d = dict(r)
name = d['name']
date = d['snapshot_date']
_, nav, ratio = _compute_nav(d)
if nav > 0 and ratio > 0:
if name not in coin_ratios:
coin_ratios[name] = {}
coin_ratios[name][date] = round(ratio, 3)
all_dates.add(date)
sorted_dates = sorted(all_dates)
series = {}
for name, date_map in coin_ratios.items():
series[name] = [date_map.get(d) for d in sorted_dates]
return web.json_response({
'dates': sorted_dates,
'series': series,
})
finally:
conn.close()
async def handle_portfolio_history(request):
conn = _get_db(request)
try:
try:
days = min(int(request.query.get('days', '90')), 365)
except (ValueError, TypeError):
days = 90
rows = conn.execute("""
SELECT * FROM coin_snapshots
WHERE snapshot_date >= date('now', ? || ' days')
ORDER BY name, snapshot_date
""", (f'-{days}',)).fetchall()
history = {}
for r in rows:
d = dict(r)
key = d['name']
if key not in history:
history[key] = []
history[key].append(d)
return web.json_response({'history': history})
finally:
conn.close()
async def handle_portfolio_latest(request):
conn = _get_db(request)
try:
rows = conn.execute("""
SELECT * FROM coin_snapshots
WHERE snapshot_date = (SELECT MAX(snapshot_date) FROM coin_snapshots)
ORDER BY market_cap_usd DESC
""").fetchall()
coins = []
for r in rows:
d = dict(r)
treas, nav, ratio = _compute_nav(d)
d['treasury_usd'] = treas
d['nav_per_token'] = nav
d['price_nav_ratio'] = ratio
coins.append(d)
return web.json_response({'coins': coins, 'date': coins[0]['snapshot_date'] if coins else None})
finally:
conn.close()
def register_portfolio_routes(app, get_conn):
app["_portfolio_conn"] = get_conn
app.router.add_get("/portfolio", handle_portfolio_page)
app.router.add_get("/api/portfolio/nav-ratios", handle_nav_ratios)
app.router.add_get("/api/portfolio/history", handle_portfolio_history)
app.router.add_get("/api/portfolio/latest", handle_portfolio_latest)

View file

@ -1,8 +1,8 @@
"""PR Lifecycle dashboard — single-page view of every PR through the pipeline.
Sortable table: PR#, summary, claims, domain, outcome, evals, evaluator, cost, date.
Click any row to expand: timeline, claim list, issues summary.
Hero cards: total PRs, merge rate, median eval rounds, total claims, total cost.
Sortable table: PR#, summary, claims, domain, contributor, outcome, evals, evaluator, cost, date.
Click any row to expand: claim titles, eval chain, timeline, reviews, issues.
Hero cards: total PRs, merge rate, total claims, est. cost.
Data sources: prs table, audit_log (eval rounds), review_records.
Owner: Ship
@ -14,7 +14,7 @@ from shared_ui import render_page
EXTRA_CSS = """
.page-content { max-width: 1600px !important; }
.content-wrapper { max-width: 1600px !important; }
.filters { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.filters select, .filters input {
background: #161b22; color: #c9d1d9; border: 1px solid #30363d;
@ -22,14 +22,15 @@ EXTRA_CSS = """
.filters select:focus, .filters input:focus { border-color: #58a6ff; outline: none; }
.pr-table { width: 100%; border-collapse: collapse; font-size: 13px; table-layout: fixed; }
.pr-table th:nth-child(1) { width: 50px; } /* PR# */
.pr-table th:nth-child(2) { width: 30%; } /* Summary */
.pr-table th:nth-child(2) { width: 28%; } /* Summary */
.pr-table th:nth-child(3) { width: 50px; } /* Claims */
.pr-table th:nth-child(4) { width: 12%; } /* Domain */
.pr-table th:nth-child(5) { width: 10%; } /* Outcome */
.pr-table th:nth-child(6) { width: 50px; } /* Evals */
.pr-table th:nth-child(7) { width: 16%; } /* Evaluator */
.pr-table th:nth-child(8) { width: 70px; } /* Cost */
.pr-table th:nth-child(9) { width: 90px; } /* Date */
.pr-table th:nth-child(4) { width: 11%; } /* Domain */
.pr-table th:nth-child(5) { width: 10%; } /* Contributor */
.pr-table th:nth-child(6) { width: 10%; } /* Outcome */
.pr-table th:nth-child(7) { width: 44px; } /* Evals */
.pr-table th:nth-child(8) { width: 12%; } /* Evaluator */
.pr-table th:nth-child(9) { width: 60px; } /* Cost */
.pr-table th:nth-child(10) { width: 80px; } /* Date */
.pr-table td { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 8px 6px; }
.pr-table td:nth-child(2) { white-space: normal; overflow: visible; line-height: 1.4; }
.pr-table th { cursor: pointer; user-select: none; position: relative; padding: 8px 18px 8px 6px; }
@ -48,22 +49,24 @@ EXTRA_CSS = """
.pr-table .pr-link:hover { text-decoration: underline; }
.pr-table td .summary-text { font-size: 12px; color: #c9d1d9; }
.pr-table td .review-snippet { font-size: 11px; color: #f85149; margin-top: 2px; opacity: 0.8; }
.pr-table td .model-tag { font-size: 9px; color: #6e7681; background: #21262d; border-radius: 3px; padding: 1px 4px; display: inline-block; margin: 1px 0; }
.pr-table td .model-tag { font-size: 10px; color: #6e7681; background: #161b22; border-radius: 3px; padding: 1px 4px; }
.pr-table td .contributor-tag { font-size: 11px; color: #d2a8ff; }
.pr-table td .contributor-self { font-size: 11px; color: #6e7681; font-style: italic; }
.pr-table td .expand-chevron { display: inline-block; width: 12px; color: #484f58; font-size: 10px; transition: transform 0.2s; }
.pr-table tr.expanded .expand-chevron { transform: rotate(90deg); color: #58a6ff; }
.pr-table td .cost-val { font-size: 12px; color: #8b949e; }
.pr-table td .claims-count { font-size: 13px; color: #c9d1d9; text-align: center; }
.pr-table td .evals-count { font-size: 13px; text-align: center; }
.trace-panel { background: #0d1117; border: 1px solid #30363d; border-radius: 8px;
padding: 16px; margin: 4px 0 8px 0; font-size: 12px; display: none; }
.trace-panel.open { display: block; }
.trace-panel .section-title { color: #58a6ff; font-size: 12px; font-weight: 600; margin: 12px 0 6px; }
.trace-panel .section-title:first-child { margin-top: 0; }
.trace-panel .claim-list { list-style: none; padding: 0; margin: 0; }
.trace-panel .claim-list li { padding: 4px 0; border-bottom: 1px solid #21262d; color: #c9d1d9; font-size: 12px; }
.trace-panel .claim-list li:last-child { border-bottom: none; }
.trace-panel .issues-box { background: #1c1017; border: 1px solid #f8514930; border-radius: 6px;
.trace-panel h4 { color: #58a6ff; font-size: 12px; margin: 12px 0 6px 0; }
.trace-panel h4:first-child { margin-top: 0; }
.claim-list { list-style: none; padding: 0; margin: 0; }
.claim-list li { padding: 4px 0 4px 16px; border-left: 2px solid #238636; color: #c9d1d9; font-size: 12px; line-height: 1.5; }
.claim-list li .claim-confidence { font-size: 10px; color: #8b949e; margin-left: 6px; }
.issues-box { background: #1c1210; border: 1px solid #f8514933; border-radius: 6px;
padding: 8px 12px; margin: 4px 0; font-size: 12px; color: #f85149; }
.eval-chain { background: #161b22; border-radius: 6px; padding: 8px 12px; margin: 4px 0; font-size: 12px; }
.eval-chain .chain-step { display: inline-block; margin-right: 6px; }
.eval-chain .chain-arrow { color: #484f58; margin: 0 4px; }
.trace-timeline { list-style: none; padding: 0; }
.trace-timeline li { padding: 4px 0; border-left: 2px solid #30363d; padding-left: 12px; margin-left: 8px; }
.trace-timeline li .ts { color: #484f58; font-size: 11px; }
@ -73,12 +76,6 @@ EXTRA_CSS = """
.trace-timeline li.ev-changes .ev { color: #d29922; }
.review-text { background: #161b22; padding: 8px 12px; border-radius: 4px;
margin: 4px 0; white-space: pre-wrap; font-size: 11px; color: #8b949e; max-height: 200px; overflow-y: auto; }
.eval-chain { background: #161b22; border-radius: 6px; padding: 8px 12px; margin: 4px 0 8px;
font-size: 12px; display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.eval-chain .step { display: flex; align-items: center; gap: 4px; }
.eval-chain .step-label { color: #8b949e; font-size: 11px; }
.eval-chain .step-model { color: #c9d1d9; font-size: 11px; font-weight: 600; }
.eval-chain .arrow { color: #484f58; }
.pagination { display: flex; gap: 8px; align-items: center; justify-content: center; margin-top: 16px; }
.pagination button { background: #161b22; color: #c9d1d9; border: 1px solid #30363d;
border-radius: 4px; padding: 4px 12px; cursor: pointer; font-size: 12px; }
@ -96,7 +93,6 @@ def render_prs_page(now: datetime) -> str:
<div class="grid" id="hero-cards">
<div class="card"><div class="label">Total PRs</div><div class="value blue" id="kpi-total">--</div><div class="detail" id="kpi-total-detail"></div></div>
<div class="card"><div class="label">Merge Rate</div><div class="value green" id="kpi-merge-rate">--</div><div class="detail" id="kpi-merge-detail"></div></div>
<div class="card"><div class="label">Median Eval Rounds</div><div class="value" id="kpi-rounds">--</div><div class="detail" id="kpi-rounds-detail"></div></div>
<div class="card"><div class="label">Total Claims</div><div class="value blue" id="kpi-claims">--</div><div class="detail" id="kpi-claims-detail"></div></div>
<div class="card"><div class="label">Est. Cost</div><div class="value" id="kpi-cost">--</div><div class="detail" id="kpi-cost-detail"></div></div>
</div>
@ -104,6 +100,7 @@ def render_prs_page(now: datetime) -> str:
<!-- Filters -->
<div class="filters">
<select id="filter-domain"><option value="">All Domains</option></select>
<select id="filter-contributor"><option value="">All Contributors</option></select>
<select id="filter-outcome">
<option value="">All Outcomes</option>
<option value="merged">Merged</option>
@ -133,9 +130,10 @@ def render_prs_page(now: datetime) -> str:
<th data-col="summary">Summary <span class="sort-arrow">&#9650;</span></th>
<th data-col="claims_count">Claims <span class="sort-arrow">&#9650;</span></th>
<th data-col="domain">Domain <span class="sort-arrow">&#9650;</span></th>
<th data-col="submitted_by">Contributor <span class="sort-arrow">&#9650;</span></th>
<th data-col="status">Outcome <span class="sort-arrow">&#9650;</span></th>
<th data-col="eval_rounds">Evals <span class="sort-arrow">&#9650;</span></th>
<th data-col="evaluator">Evaluator <span class="sort-arrow">&#9650;</span></th>
<th data-col="evaluator_label">Evaluator <span class="sort-arrow">&#9650;</span></th>
<th data-col="est_cost">Cost <span class="sort-arrow">&#9650;</span></th>
<th data-col="created_at">Date <span class="sort-arrow">&#9650;</span></th>
</tr>
@ -152,42 +150,71 @@ def render_prs_page(now: datetime) -> str:
</div>
"""
# Use single-quoted JS strings throughout to avoid Python/HTML escaping issues
scripts = """<script>
const PAGE_SIZE = 50;
const FORGEJO = 'https://git.livingip.xyz/teleo/teleo-codex/pulls/';
let allData = [];
let filtered = [];
let sortCol = 'number';
let sortAsc = false;
let page = 0;
let expandedPr = null;
var PAGE_SIZE = 50;
var FORGEJO = 'https://git.livingip.xyz/teleo/teleo-codex/pulls/';
var allData = [];
var filtered = [];
var sortCol = 'number';
var sortAsc = false;
var page = 0;
var expandedPr = null;
// Tier-based cost estimates (per eval round)
var TIER_COSTS = {
'DEEP': 0.145, // Haiku triage + Gemini Flash domain + Opus Leo
'STANDARD': 0.043, // Haiku triage + Gemini Flash domain + Sonnet Leo
'LIGHT': 0.027 // Haiku triage + Gemini Flash domain only
};
function estimateCost(pr) {
var tier = pr.tier || 'STANDARD';
var rounds = pr.eval_rounds || 1;
var baseCost = TIER_COSTS[tier] || TIER_COSTS['STANDARD'];
return baseCost * rounds;
}
function fmtCost(val) {
if (val == null || val === 0) return '--';
return '$' + val.toFixed(3);
}
function loadData() {
var days = document.getElementById('filter-days').value;
var url = '/api/pr-lifecycle' + (days !== '0' ? '?days=' + days : '?days=9999');
fetch(url).then(function(r) { return r.json(); }).then(function(data) {
allData = data.prs || [];
// Compute derived fields
allData.forEach(function(p) {
p.est_cost = estimateCost(p);
// Evaluator label for sorting
p.evaluator_label = p.domain_agent || p.agent || '--';
});
populateFilters(allData);
updateKPIs(data);
applyFilters();
}).catch(function() {
document.getElementById('pr-tbody').innerHTML =
'<tr><td colspan="9" style="text-align:center;color:#f85149;">Failed to load data</td></tr>';
'<tr><td colspan="10" style="text-align:center;color:#f85149;">Failed to load data</td></tr>';
});
}
function populateFilters(prs) {
var domains = [], seenD = {};
var domains = [], contribs = [], seenD = {}, seenC = {};
prs.forEach(function(p) {
if (p.domain && !seenD[p.domain]) { seenD[p.domain] = 1; domains.push(p.domain); }
var c = p.submitted_by || 'unknown';
if (!seenC[c]) { seenC[c] = 1; contribs.push(c); }
});
domains.sort();
domains.sort(); contribs.sort();
var domSel = document.getElementById('filter-domain');
var curDom = domSel.value;
var conSel = document.getElementById('filter-contributor');
var curDom = domSel.value, curCon = conSel.value;
domSel.innerHTML = '<option value="">All Domains</option>' +
domains.map(function(d) { return '<option value="' + esc(d) + '">' + esc(d) + '</option>'; }).join('');
domSel.value = curDom;
conSel.innerHTML = '<option value="">All Contributors</option>' +
contribs.map(function(c) { return '<option value="' + esc(c) + '">' + esc(c) + '</option>'; }).join('');
domSel.value = curDom; conSel.value = curCon;
}
function updateKPIs(data) {
@ -199,47 +226,29 @@ def render_prs_page(now: datetime) -> str:
document.getElementById('kpi-merge-rate').textContent = fmtPct(rate);
document.getElementById('kpi-merge-detail').textContent = fmtNum(data.open) + ' open';
document.getElementById('kpi-rounds').textContent =
data.median_rounds != null ? data.median_rounds.toFixed(1) : '--';
document.getElementById('kpi-rounds-detail').textContent =
data.max_rounds != null ? 'max: ' + data.max_rounds : '';
var totalClaims = 0, mergedClaims = 0;
var totalCost = 0;
var actualCount = 0, estCount = 0;
var totalClaims = 0, mergedClaims = 0, totalCost = 0;
(data.prs || []).forEach(function(p) {
totalClaims += (p.claims_count || 1);
if (p.status === 'merged') mergedClaims += (p.claims_count || 1);
totalCost += (p.cost || 0);
if (p.cost_is_actual) actualCount++; else estCount++;
totalCost += estimateCost(p);
});
document.getElementById('kpi-claims').textContent = fmtNum(totalClaims);
document.getElementById('kpi-claims-detail').textContent = fmtNum(mergedClaims) + ' merged';
// Show actual DB total if available, otherwise sum from PRs
var costLabel = '';
if (data.actual_total_cost > 0) {
document.getElementById('kpi-cost').textContent = '$' + data.actual_total_cost.toFixed(2);
costLabel = 'from costs table';
} else if (actualCount > 0) {
document.getElementById('kpi-cost').textContent = '$' + totalCost.toFixed(2);
costLabel = actualCount + ' actual, ' + estCount + ' est.';
} else {
document.getElementById('kpi-cost').textContent = '$' + totalCost.toFixed(2);
costLabel = 'ALL ESTIMATED';
}
var costPerClaim = totalClaims > 0 ? totalCost / totalClaims : 0;
document.getElementById('kpi-cost-detail').textContent =
'$' + costPerClaim.toFixed(3) + '/claim \u00b7 ' + costLabel;
var perClaim = totalClaims > 0 ? totalCost / totalClaims : 0;
document.getElementById('kpi-cost-detail').textContent = '$' + perClaim.toFixed(3) + '/claim';
}
function applyFilters() {
var dom = document.getElementById('filter-domain').value;
var con = document.getElementById('filter-contributor').value;
var out = document.getElementById('filter-outcome').value;
var tier = document.getElementById('filter-tier').value;
filtered = allData.filter(function(p) {
if (dom && p.domain !== dom) return false;
if (con && (p.submitted_by || 'unknown') !== con) return false;
if (out && p.status !== out) return false;
if (tier && p.tier !== tier) return false;
return true;
@ -269,19 +278,6 @@ def render_prs_page(now: datetime) -> str:
return s.length > n ? s.substring(0, n) + '...' : s;
}
function shortModel(m) {
if (!m) return '';
// Shorten model names for display
if (m.indexOf('gemini-2.5-flash') !== -1) return 'Gemini Flash';
if (m.indexOf('claude-sonnet') !== -1 || m.indexOf('sonnet-4') !== -1) return 'Sonnet';
if (m.indexOf('claude-opus') !== -1 || m.indexOf('opus') !== -1) return 'Opus';
if (m.indexOf('haiku') !== -1) return 'Haiku';
if (m.indexOf('gpt-4o') !== -1) return 'GPT-4o';
// fallback: strip provider prefix
var parts = m.split('/');
return parts[parts.length - 1];
}
function renderTable() {
var tbody = document.getElementById('pr-tbody');
var start = page * PAGE_SIZE;
@ -289,7 +285,7 @@ def render_prs_page(now: datetime) -> str:
var totalPages = Math.ceil(filtered.length / PAGE_SIZE);
if (slice.length === 0) {
tbody.innerHTML = '<tr><td colspan="9" style="text-align:center;color:#8b949e;">No PRs match filters</td></tr>';
tbody.innerHTML = '<tr><td colspan="10" style="text-align:center;color:#8b949e;">No PRs match filters</td></tr>';
return;
}
@ -301,40 +297,37 @@ def render_prs_page(now: datetime) -> str:
(p.tier || '').toLowerCase() === 'standard' ? 'tier-standard' : 'tier-light';
var date = p.created_at ? p.created_at.substring(0, 10) : '--';
// Summary
// Summary: first claim title
var summary = p.summary || '--';
var reviewSnippet = '';
if (p.status === 'closed' && p.review_snippet) {
reviewSnippet = '<div class="review-snippet">' + esc(truncate(p.review_snippet, 120)) + '</div>';
}
// Outcome with tier badge
var outcomeLabel = esc(p.status || '--');
var tierBadge = p.tier ? ' <span class="' + tierClass + '" style="font-size:10px;">' + esc(p.tier) + '</span>' : '';
// Evaluator column: domain agent + model
// Review snippet for issues
var reviewSnippet = '';
if (p.review_snippet) {
reviewSnippet = '<div class="review-snippet">' + esc(truncate(p.review_snippet, 100)) + '</div>';
}
// Contributor display
var contributor = p.submitted_by || '--';
var contribClass = 'contributor-tag';
if (contributor.indexOf('self-directed') >= 0 || contributor === 'unknown') {
contribClass = 'contributor-self';
}
// Evaluator: domain agent + model tag
var evaluator = '';
if (p.domain_agent) {
evaluator = '<div style="font-size:12px;color:#c9d1d9;">' + esc(p.domain_agent) + '</div>';
}
var modelShort = '';
if (p.domain_model) {
evaluator += '<div class="model-tag">' + esc(shortModel(p.domain_model)) + '</div>';
var m = p.domain_model;
if (m.indexOf('gemini') >= 0) modelShort = 'Gemini Flash';
else if (m.indexOf('gpt-4o') >= 0) modelShort = 'GPT-4o';
else if (m.indexOf('sonnet') >= 0) modelShort = 'Sonnet';
else modelShort = m.split('/').pop();
}
if (p.leo_model) {
evaluator += '<div class="model-tag">' + esc(shortModel(p.leo_model)) + '</div>';
}
if (!evaluator) evaluator = '<span style="color:#484f58;">--</span>';
// Cost actual from DB or estimated (flagged)
var costStr;
if (p.cost != null && p.cost > 0) {
if (p.cost_is_actual) {
costStr = '<span class="cost-val">$' + p.cost.toFixed(3) + '</span>';
} else {
costStr = '<span class="cost-val" style="opacity:0.5;" title="Estimated — no actual cost tracked">~$' + p.cost.toFixed(3) + '</span>';
}
} else {
costStr = '<span style="color:#484f58;">--</span>';
evaluator = esc(p.domain_agent) + (modelShort ? ' <span class="model-tag">' + esc(modelShort) + '</span>' : '');
}
rows.push(
@ -342,16 +335,17 @@ def render_prs_page(now: datetime) -> str:
'<td><span class="expand-chevron">&#9654;</span> ' +
'<a class="pr-link" href="' + FORGEJO + p.number + '" target="_blank" rel="noopener" onclick="event.stopPropagation();">#' + p.number + '</a></td>' +
'<td style="white-space:normal;"><span class="summary-text">' + esc(summary) + '</span>' + reviewSnippet + '</td>' +
'<td style="text-align:center;">' + (p.claims_count || '--') + '</td>' +
'<td style="text-align:center;">' + (p.claims_count || 1) + '</td>' +
'<td>' + esc(p.domain || '--') + '</td>' +
'<td class="' + outClass + '">' + outcomeLabel + tierBadge + '</td>' +
'<td><span class="' + contribClass + '">' + esc(truncate(contributor, 20)) + '</span></td>' +
'<td class="' + outClass + '">' + esc(p.status || '--') + tierBadge + '</td>' +
'<td style="text-align:center;">' + (p.eval_rounds || '--') + '</td>' +
'<td>' + evaluator + '</td>' +
'<td>' + costStr + '</td>' +
'<td>' + fmtCost(p.est_cost) + '</td>' +
'<td>' + date + '</td>' +
'</tr>' +
'<tr id="trace-' + p.number + '" style="display:none;"><td colspan="9" style="padding:0;">' +
'<div class="trace-panel" id="panel-' + p.number + '">Loading trace...</div>' +
'<tr id="trace-' + p.number + '" style="display:none;"><td colspan="10" style="padding:0;">' +
'<div class="trace-panel" id="panel-' + p.number + '">Loading...</div>' +
'</td></tr>'
);
});
@ -414,46 +408,34 @@ def render_prs_page(now: datetime) -> str:
});
function loadTrace(pr, panel) {
// Also find this PR in allData for claim list
// Find the PR data for claim titles
var prData = null;
allData.forEach(function(p) { if (p.number == pr) prData = p; });
for (var i = 0; i < allData.length; i++) {
if (allData[i].number == pr) { prData = allData[i]; break; }
}
fetch('/api/trace/' + pr).then(function(r) { return r.json(); }).then(function(data) {
var html = '';
// --- Claims contained in this PR ---
if (prData && prData.claim_titles && prData.claim_titles.length > 0) {
html += '<div class="section-title">Claims (' + prData.claim_titles.length + ')</div>';
// Claims contained in this PR
if (prData && prData.description) {
var titles = prData.description.split('|').map(function(t) { return t.trim(); }).filter(Boolean);
if (titles.length > 0) {
html += '<h4>Claims (' + titles.length + ')</h4>';
html += '<ul class="claim-list">';
prData.claim_titles.forEach(function(t) {
titles.forEach(function(t) {
html += '<li>' + esc(t) + '</li>';
});
html += '</ul>';
}
}
// --- Issues summary ---
var issues = [];
if (data.timeline) {
data.timeline.forEach(function(ev) {
if (ev.detail && ev.detail.issues) {
var iss = ev.detail.issues;
if (typeof iss === 'string') { try { iss = JSON.parse(iss); } catch(e) { iss = [iss]; } }
if (Array.isArray(iss)) {
iss.forEach(function(i) {
var label = String(i).replace(/_/g, ' ');
if (issues.indexOf(label) === -1) issues.push(label);
});
}
}
});
}
// Issues (if any)
if (prData && prData.review_snippet) {
html += '<div class="issues-box">' + esc(prData.review_snippet) + '</div>';
} else if (issues.length > 0) {
html += '<div class="issues-box">Issues: ' + issues.map(esc).join(', ') + '</div>';
}
// --- Eval chain (who reviewed with what model) ---
// Eval chain with models
var models = {};
if (data.timeline) {
data.timeline.forEach(function(ev) {
@ -464,23 +446,38 @@ def render_prs_page(now: datetime) -> str:
}
});
}
if (Object.keys(models).length > 0) {
html += '<div class="eval-chain">';
html += '<strong style="color:#58a6ff;">Eval chain:</strong> ';
var parts = [];
if (models['triage.haiku_triage'] || models['triage.deterministic_triage'])
parts.push('<span class="step"><span class="step-label">Triage</span> <span class="step-model">' + shortModel(models['triage.haiku_triage'] || 'deterministic') + '</span></span>');
if (models['domain_review'])
parts.push('<span class="step"><span class="step-label">Domain</span> <span class="step-model">' + shortModel(models['domain_review']) + '</span></span>');
if (models['leo_review'])
parts.push('<span class="step"><span class="step-label">Leo</span> <span class="step-model">' + shortModel(models['leo_review']) + '</span></span>');
html += parts.length > 0 ? parts.join(' <span class="arrow">&#8594;</span> ') : '<span style="color:#484f58;">No model data</span>';
html += '<div class="eval-chain"><strong style="color:#58a6ff;">Eval Chain:</strong> ';
var chain = [];
if (models['triage.haiku_triage'] || models['triage.deterministic_triage']) {
chain.push('<span class="chain-step">Triage <span class="model-tag">' +
esc(models['triage.haiku_triage'] || 'deterministic') + '</span></span>');
}
if (models['domain_review']) {
chain.push('<span class="chain-step">Domain <span class="model-tag">' +
esc(models['domain_review']) + '</span></span>');
}
if (models['leo_review']) {
chain.push('<span class="chain-step">Leo <span class="model-tag">' +
esc(models['leo_review']) + '</span></span>');
}
html += chain.length > 0 ? chain.join('<span class="chain-arrow">&#8594;</span>') :
'<span style="color:#484f58;">No model data</span>';
html += '</div>';
// Source + contributor metadata
if (data.pr) {
html += '<div style="margin:8px 0;font-size:12px;color:#8b949e;">';
if (data.pr.source_path) html += 'Source: <span style="color:#c9d1d9;">' + esc(data.pr.source_path) + '</span> &middot; ';
if (prData && prData.submitted_by) html += 'Contributor: <span style="color:#d2a8ff;">' + esc(prData.submitted_by) + '</span> &middot; ';
if (data.pr.tier) html += 'Tier: <span style="color:#c9d1d9;">' + esc(data.pr.tier) + '</span> &middot; ';
html += '<a class="pr-link" href="' + FORGEJO + pr + '" target="_blank">View on Forgejo</a>';
html += '</div>';
}
// --- Timeline ---
// Timeline
if (data.timeline && data.timeline.length > 0) {
html += '<div class="section-title">Timeline</div>';
html += '<h4>Timeline</h4>';
html += '<ul class="trace-timeline">';
data.timeline.forEach(function(ev) {
var cls = ev.event === 'approved' ? 'ev-approved' :
@ -491,7 +488,7 @@ def render_prs_page(now: datetime) -> str:
if (ev.detail) {
if (ev.detail.tier) detail += ' tier=' + ev.detail.tier;
if (ev.detail.reason) detail += ' &#8212; ' + esc(ev.detail.reason);
if (ev.detail.model) detail += ' [' + esc(shortModel(ev.detail.model)) + ']';
if (ev.detail.model) detail += ' [' + esc(ev.detail.model) + ']';
if (ev.detail.review_text) {
detail += '<div class="review-text">' + esc(ev.detail.review_text).substring(0, 2000) + '</div>';
}
@ -509,19 +506,19 @@ def render_prs_page(now: datetime) -> str:
});
html += '</ul>';
} else {
html += '<div style="color:#484f58;font-size:12px;margin-top:8px;">No timeline events</div>';
html += '<div style="color:#484f58;font-size:12px;margin:8px 0;">No timeline events</div>';
}
// --- Reviews ---
// Reviews
if (data.reviews && data.reviews.length > 0) {
html += '<div class="section-title">Reviews</div>';
html += '<h4>Reviews</h4>';
data.reviews.forEach(function(r) {
var cls = r.outcome === 'approved' ? 'badge-green' :
r.outcome === 'rejected' ? 'badge-red' : 'badge-yellow';
html += '<div style="margin:4px 0;">' +
'<span class="badge ' + cls + '">' + esc(r.outcome) + '</span> ' +
'<span style="color:#8b949e;font-size:11px;">' + esc(r.reviewer || '') + ' ' +
(r.model ? '[' + esc(shortModel(r.model)) + ']' : '') + ' ' +
(r.model ? '[' + esc(r.model) + ']' : '') + ' ' +
(r.reviewed_at || '').substring(0, 19) + '</span>';
if (r.rejection_reason) {
html += ' <code>' + esc(r.rejection_reason) + '</code>';
@ -540,7 +537,7 @@ def render_prs_page(now: datetime) -> str:
}
// Filter listeners
['filter-domain', 'filter-outcome', 'filter-tier'].forEach(function(id) {
['filter-domain', 'filter-contributor', 'filter-outcome', 'filter-tier'].forEach(function(id) {
document.getElementById(id).addEventListener('change', applyFilters);
});
document.getElementById('filter-days').addEventListener('change', loadData);

View file

@ -10,7 +10,6 @@ Endpoints:
Owner: Argus
"""
import asyncio
import json
import logging
import os
@ -18,7 +17,6 @@ import sqlite3
import statistics
import time
import urllib.request
from collections import defaultdict
from datetime import datetime, timezone
from pathlib import Path
@ -63,7 +61,6 @@ async def handle_stage_times(request):
Returns median minutes between consecutive stages.
"""
conn = request.app["_get_conn"]()
try:
hours = int(request.query.get("hours", "24"))
# Get per-PR event timestamps
@ -120,8 +117,6 @@ async def handle_stage_times(request):
}
return web.json_response({"hours": hours, "stages": stage_times})
finally:
conn.close()
# ─── GET /api/herfindahl ──────────────────────────────────────────────────
@ -132,7 +127,6 @@ async def handle_herfindahl(request):
HHI = sum of (domain_share^2). 1.0 = single domain, lower = more diverse.
"""
conn = request.app["_get_conn"]()
try:
days = int(request.query.get("days", "30"))
rows = conn.execute(
@ -170,8 +164,6 @@ async def handle_herfindahl(request):
"total_merged": total,
"days": days,
})
finally:
conn.close()
# ─── GET /api/agent-state ─────────────────────────────────────────────────
@ -234,14 +226,13 @@ async def handle_agent_state(request):
async def handle_extraction_yield_by_domain(request):
"""Sources → claims conversion rate per domain."""
conn = request.app["_get_conn"]()
try:
days = int(request.query.get("days", "30"))
# Sources per domain (approximate from PR source_path domain)
source_counts = conn.execute(
"""SELECT domain, COUNT(DISTINCT path) as sources
"""SELECT domain, COUNT(DISTINCT source_url) as sources
FROM sources s
JOIN prs p ON p.source_path LIKE '%' || s.path || '%'
JOIN prs p ON p.source_path LIKE '%' || s.url || '%'
WHERE s.created_at > datetime('now', ? || ' days')
GROUP BY domain""",
(f"-{days}",),
@ -278,8 +269,6 @@ async def handle_extraction_yield_by_domain(request):
domains.sort(key=lambda x: x["merged"], reverse=True)
return web.json_response({"days": days, "domains": domains})
finally:
conn.close()
# ─── GET /api/agents-dashboard ─────────────────────────────────────────────
@ -292,7 +281,6 @@ async def handle_agents_dashboard(request):
All in one response to avoid N client-side fetches.
"""
conn = request.app["_get_conn"]()
try:
days = int(request.query.get("days", "30"))
# Per-agent merged + rejected counts
@ -392,8 +380,6 @@ async def handle_agents_dashboard(request):
pass
return web.json_response({"days": days, "agents": agents})
finally:
conn.close()
# ─── GET /api/cascade-coverage ────────────────────────────────────────────
@ -404,7 +390,6 @@ async def handle_cascade_coverage(request):
Returns: triggered count, by-agent breakdown, claims affected.
"""
conn = request.app["_get_conn"]()
try:
days = int(request.query.get("days", "30"))
triggered = conn.execute(
@ -446,8 +431,6 @@ async def handle_cascade_coverage(request):
for r in triggered
]
insufficient_data = total_triggered < 5
return web.json_response({
"days": days,
"total_triggered": total_triggered,
@ -456,10 +439,7 @@ async def handle_cascade_coverage(request):
"total_notifications": summaries["total_notifications"] if summaries else 0,
"merges_with_cascade": summaries["total_merges_with_cascade"] if summaries else 0,
"by_agent": by_agent,
"insufficient_data": insufficient_data,
})
finally:
conn.close()
# ─── GET /api/review-summary ─────────────────────────────────────────────
@ -471,7 +451,6 @@ async def handle_review_summary(request):
disagreement_type columns.
"""
conn = request.app["_get_conn"]()
try:
days = int(request.query.get("days", "30"))
# Check if table exists and has data
@ -495,7 +474,7 @@ async def handle_review_summary(request):
(f"-{days}",),
).fetchall()
# Rejection reasons — try review_records first, fall back to prs.eval_issues
# Rejection reasons
reasons = conn.execute(
"""SELECT rejection_reason, COUNT(*) as cnt
FROM review_records
@ -505,17 +484,15 @@ async def handle_review_summary(request):
(f"-{days}",),
).fetchall()
rejection_source = "review_records"
if not reasons:
reasons = conn.execute(
"""SELECT value AS rejection_reason, COUNT(*) as cnt
FROM prs, json_each(prs.eval_issues)
WHERE eval_issues IS NOT NULL AND eval_issues != '[]'
AND created_at > datetime('now', ? || ' days')
GROUP BY value ORDER BY cnt DESC""",
# Disagreement types
disagreements = conn.execute(
"""SELECT disagreement_type, COUNT(*) as cnt
FROM review_records
WHERE disagreement_type IS NOT NULL
AND reviewed_at > datetime('now', ? || ' days')
GROUP BY disagreement_type ORDER BY cnt DESC""",
(f"-{days}",),
).fetchall()
rejection_source = "prs.eval_issues"
# Per-reviewer breakdown
reviewers = conn.execute(
@ -548,7 +525,7 @@ async def handle_review_summary(request):
"total": total,
"outcomes": {r["outcome"]: r["cnt"] for r in outcomes},
"rejection_reasons": [{"reason": r["rejection_reason"], "count": r["cnt"]} for r in reasons],
"rejection_source": rejection_source,
"disagreement_types": [{"type": r["disagreement_type"], "count": r["cnt"]} for r in disagreements],
"reviewers": [
{"reviewer": r["reviewer"], "approved": r["approved"], "approved_with_changes": r["approved_with_changes"],
"rejected": r["rejected"], "total": r["total"]}
@ -560,126 +537,6 @@ async def handle_review_summary(request):
for r in domains
],
})
finally:
conn.close()
# ─── GET /api/agent-scorecard ──────────────────────────────────────────────
async def handle_agent_scorecard(request):
"""Per-agent scorecard: PRs submitted, review outcomes, rejection reasons.
Data from review_records (structured reviews) + prs (submission counts).
Falls back to prs.eval_issues for rejection reasons when review_records
has no rejections yet.
"""
conn = request.app["_get_conn"]()
try:
try:
days = min(int(request.query.get("days", "30")), 90)
except ValueError:
days = 30
day_filter = f"-{days}"
# PRs submitted per agent
prs_by_agent = conn.execute(
"""SELECT agent, COUNT(*) as cnt FROM prs
WHERE agent IS NOT NULL
AND created_at > datetime('now', ? || ' days')
GROUP BY agent""",
(day_filter,),
).fetchall()
prs_map = {r["agent"]: r["cnt"] for r in prs_by_agent}
# Review outcomes from review_records
review_data = {}
try:
reviews = conn.execute(
"""SELECT reviewer as agent, outcome, COUNT(*) as cnt
FROM review_records
WHERE reviewed_at > datetime('now', ? || ' days')
GROUP BY reviewer, outcome""",
(day_filter,),
).fetchall()
for r in reviews:
agent = r["agent"]
if agent not in review_data:
review_data[agent] = {"approved": 0, "approved_with_changes": 0, "rejected": 0, "total": 0}
review_data[agent][r["outcome"].replace("-", "_")] = r["cnt"]
review_data[agent]["total"] += r["cnt"]
except sqlite3.OperationalError:
pass
# If review_records is empty, fall back to audit_log eval events
if not review_data:
evals = conn.execute(
"""SELECT
COALESCE(json_extract(detail, '$.agent'), json_extract(detail, '$.domain_agent')) as agent,
event, COUNT(*) as cnt
FROM audit_log
WHERE stage='evaluate'
AND event IN ('approved','changes_requested','domain_rejected','tier05_rejected')
AND timestamp > datetime('now', ? || ' days')
GROUP BY agent, event""",
(day_filter,),
).fetchall()
for r in evals:
agent = r["agent"]
if not agent:
continue
if agent not in review_data:
review_data[agent] = {"approved": 0, "approved_with_changes": 0, "rejected": 0, "total": 0}
if r["event"] == "approved":
review_data[agent]["approved"] += r["cnt"]
elif r["event"] == "changes_requested": # fixer auto-remediated; equivalent in pre-review_records era
review_data[agent]["approved_with_changes"] += r["cnt"]
else:
review_data[agent]["rejected"] += r["cnt"]
review_data[agent]["total"] += r["cnt"]
# Rejection reasons from prs.eval_issues (canonical source)
reason_rows = conn.execute(
"""SELECT agent, value as reason, COUNT(*) as cnt
FROM prs, json_each(prs.eval_issues)
WHERE eval_issues IS NOT NULL AND eval_issues != '[]'
AND agent IS NOT NULL
AND created_at > datetime('now', ? || ' days')
GROUP BY agent, reason ORDER BY agent, cnt DESC""",
(day_filter,),
).fetchall()
reasons_map = {}
for r in reason_rows:
if r["agent"] not in reasons_map:
reasons_map[r["agent"]] = {}
reasons_map[r["agent"]][r["reason"]] = r["cnt"]
# Build scorecards
all_agents = sorted(set(list(prs_map.keys()) + list(review_data.keys())))
scorecards = []
for agent in all_agents:
if agent in ("unknown", None):
continue
rd = review_data.get(agent, {"approved": 0, "approved_with_changes": 0, "rejected": 0, "total": 0})
total_reviews = rd["total"]
approved = rd["approved"]
approved_wc = rd["approved_with_changes"]
rejected = rd["rejected"]
approval_rate = ((approved + approved_wc) / total_reviews * 100) if total_reviews else 0
scorecards.append({
"agent": agent,
"total_prs": prs_map.get(agent, 0),
"total_reviews": total_reviews,
"approved": approved,
"approved_with_changes": approved_wc,
"rejected": rejected,
"approval_rate": round(approval_rate, 1),
"rejection_reasons": reasons_map.get(agent, {}),
})
scorecards.sort(key=lambda x: x["total_reviews"], reverse=True)
return web.json_response({"days": days, "scorecards": scorecards})
finally:
conn.close()
# ─── Trace endpoint ────────────────────────────────────────────────────────
@ -692,8 +549,11 @@ async def handle_trace(request: web.Request) -> web.Response:
One thread, every stage, chronological.
"""
trace_id = request.match_info["trace_id"]
conn = request.app["_get_conn"]()
try:
get_conn = request.app["_get_conn"]
conn = get_conn()
# Audit log events (the backbone)
# Try trace_id first, fall back to PR number in detail JSON
events = conn.execute(
"""SELECT timestamp, stage, event, detail
FROM audit_log
@ -703,6 +563,7 @@ async def handle_trace(request: web.Request) -> web.Response:
).fetchall()
if not events:
# Fallback: match by PR number in detail JSON (for rows without trace_id)
events = conn.execute(
"""SELECT timestamp, stage, event, detail
FROM audit_log
@ -711,6 +572,7 @@ async def handle_trace(request: web.Request) -> web.Response:
(trace_id,),
).fetchall()
# Review records for this PR
reviews = conn.execute(
"""SELECT reviewed_at, reviewer, reviewer_model, outcome,
rejection_reason, disagreement_type, notes, claim_path
@ -720,6 +582,7 @@ async def handle_trace(request: web.Request) -> web.Response:
(trace_id,),
).fetchall()
# PR metadata
pr = conn.execute(
"""SELECT number, source_path, domain, agent, tier, status,
origin, created_at, merged_at
@ -745,8 +608,6 @@ async def handle_trace(request: web.Request) -> web.Response:
}
return web.json_response(result)
finally:
conn.close()
# ─── GET /api/growth ──────────────────────────────────────────────────────
@ -757,7 +618,6 @@ async def handle_growth(request):
Returns daily data points with running totals for each series.
"""
conn = request.app["_get_conn"]()
try:
days = int(request.query.get("days", "90"))
# Daily new sources
@ -849,8 +709,6 @@ async def handle_growth(request):
"merged": m_total,
},
})
finally:
conn.close()
import re
@ -865,36 +723,23 @@ async def handle_pr_lifecycle(request):
Joins prs + audit_log (eval rounds) + review_records.
"""
conn = request.app["_get_conn"]()
try:
days = int(request.query.get("days", "30"))
day_clause = "AND p.created_at > datetime('now', ? || ' days')" if days < 9999 else ""
params = (f"-{days}",) if days < 9999 else ()
# Base PR data (include cost_usd for actual cost tracking)
# Base PR data
pr_rows = conn.execute(
f"""SELECT p.number, p.agent, p.domain, p.tier, p.status,
p.created_at, p.merged_at, p.leo_verdict, p.description,
p.domain_agent, p.domain_model, p.branch, p.cost_usd
p.domain_agent, p.domain_model, p.branch, p.submitted_by,
p.source_path
FROM prs p
WHERE 1=1 {day_clause}
ORDER BY p.number DESC""",
params,
).fetchall()
# Actual costs from costs table (aggregated, same date window as PRs)
cost_day_clause = "AND date > date('now', ? || ' days')" if days < 9999 else ""
actual_cost_rows = conn.execute(
f"""SELECT SUM(cost_usd) as total_actual_cost,
SUM(calls) as total_calls,
SUM(input_tokens) as total_input_tokens,
SUM(output_tokens) as total_output_tokens
FROM costs
WHERE cost_usd > 0 {cost_day_clause}""",
params,
).fetchone()
actual_total_cost = actual_cost_rows["total_actual_cost"] if actual_cost_rows and actual_cost_rows["total_actual_cost"] else 0
# Eval round counts per PR (from audit_log)
eval_rows = conn.execute(
f"""SELECT CAST(json_extract(detail, '$.pr') AS INTEGER) as pr,
@ -957,19 +802,6 @@ async def handle_pr_lifecycle(request):
except (json.JSONDecodeError, TypeError):
pass
TIER_COST_EST = {
"LIGHT": 0.002,
"STANDARD": 0.018,
"DEEP": 0.12,
}
EXTRACT_COST_EST = 0.025
LEO_MODEL_BY_TIER = {
"DEEP": "claude-opus-4-20250514",
"STANDARD": "anthropic/claude-sonnet-4.5",
"LIGHT": None,
}
# Build PR list
prs = []
ttm_values = []
@ -1007,46 +839,38 @@ async def handle_pr_lifecycle(request):
elif status == "open":
open_count += 1
# Claims count from pipe-separated description titles
desc = r["description"] or ""
claim_titles = [t.strip() for t in desc.split("|") if t.strip()] if desc.strip() else []
claims_count = len(claim_titles) if claim_titles else 1
claims_count = desc.count("|") + 1 if desc.strip() else 1
# Summary: first claim title from description, fallback to branch name
summary = None
if claim_titles:
summary = claim_titles[0][:120]
if desc.strip():
first_title = desc.split("|")[0].strip()
summary = first_title[:120] if first_title else None
if not summary:
branch = r["branch"] or ""
# Use prefix as category if present: "extract/...", "reweave/...", etc.
prefix = ""
if "/" in branch:
prefix = branch.split("/", 1)[0]
branch = branch.split("/", 1)[1]
# Strip date prefix like "2026-04-06-" or "2026-02-00-"
branch = _DATE_PREFIX_RE.sub("", branch)
# Strip trailing hash suffix like "-116d" or "-2cb1"
branch = re.sub(r"-[0-9a-f]{4}$", "", branch)
if branch:
summary = branch.replace("-", " ").replace("_", " ").strip()[:120]
elif prefix:
summary = prefix
tier = r["tier"] or "STANDARD"
actual_cost = r["cost_usd"] if r["cost_usd"] and r["cost_usd"] > 0 else None
if actual_cost is not None:
cost = round(actual_cost, 4)
cost_is_actual = True
else:
eval_cost = TIER_COST_EST.get(tier, 0.018) * max(rounds, 1)
cost = round(EXTRACT_COST_EST + eval_cost, 4)
cost_is_actual = False
leo_model = LEO_MODEL_BY_TIER.get(tier)
summary = prefix # "reweave", "ingestion", etc.
prs.append({
"number": pr_num,
"agent": r["agent"],
"domain": r["domain"],
"tier": tier,
"tier": r["tier"],
"status": status,
"claims_count": claims_count,
"claim_titles": claim_titles,
"eval_rounds": rounds,
"ttm_minutes": round(ttm, 1) if ttm is not None else None,
"created_at": r["created_at"],
@ -1056,11 +880,10 @@ async def handle_pr_lifecycle(request):
"summary": summary,
"description": desc if desc.strip() else None,
"review_snippet": snippet_map.get(pr_num),
"submitted_by": r["submitted_by"],
"source_path": r["source_path"],
"domain_agent": r["domain_agent"],
"domain_model": r["domain_model"],
"leo_model": leo_model,
"cost": cost,
"cost_is_actual": cost_is_actual,
})
# Summary KPIs
@ -1080,215 +903,18 @@ async def handle_pr_lifecycle(request):
return None
return vals[int(len(vals) * 0.9)]
# Compute cost summary: actual where available, estimated where not
total_actual = sum(p["cost"] for p in prs if p["cost_is_actual"])
total_estimated = sum(p["cost"] for p in prs if not p["cost_is_actual"])
prs_with_actual_cost = sum(1 for p in prs if p["cost_is_actual"])
med_ttm = median(ttm_values)
med_rounds = median(round_values)
return web.json_response({
"days": days,
"total": len(prs),
"merged": merged_count,
"closed": closed_count,
"open": open_count,
"median_ttm": round(med_ttm, 1) if med_ttm is not None else None,
"median_ttm": round(median(ttm_values), 1) if median(ttm_values) is not None else None,
"p90_ttm": round(p90(ttm_values), 1) if p90(ttm_values) is not None else None,
"median_rounds": round(med_rounds, 1) if med_rounds is not None else None,
"median_rounds": round(median(round_values), 1) if median(round_values) is not None else None,
"max_rounds": max(round_values) if round_values else None,
"actual_total_cost": round(actual_total_cost, 2),
"cost_summary": {
"total_actual": round(total_actual, 2),
"total_estimated": round(total_estimated, 2),
"prs_with_actual_cost": prs_with_actual_cost,
"prs_with_estimated_cost": len(prs) - prs_with_actual_cost,
},
"prs": prs,
})
finally:
conn.close()
# ─── GET /api/telegram-extractions ───────────────────────────────────────
async def handle_telegram_extractions(request):
"""Review surface for Telegram conversation extractions.
Shows recent PRs sourced from Telegram conversations with claim titles,
status, and source info. Designed for quick daily spot-checking.
Query params:
days (int): lookback window (default 7, max 90)
"""
conn = request.app["_get_conn"]()
try:
days = min(int(request.query.get("days", "7")), 90)
day_filter = f"-{days}"
# Find PRs from Telegram sources (source_path contains 'telegram' or submitted_by is @m3taversal via bot)
rows = conn.execute(
"""SELECT p.number, p.agent, p.domain, p.tier, p.status,
p.created_at, p.merged_at, p.description, p.source_path,
p.submitted_by, p.branch, p.eval_issues, p.leo_verdict
FROM prs p
WHERE (p.source_path LIKE '%telegram%' OR p.source_path LIKE '%futardio%')
AND p.created_at > datetime('now', ? || ' days')
ORDER BY p.number DESC""",
(day_filter,),
).fetchall()
prs = []
for r in rows:
desc = r["description"] or ""
claim_titles = [t.strip() for t in desc.split("|") if t.strip()] if desc.strip() else []
issues = None
if r["eval_issues"]:
try:
issues = json.loads(r["eval_issues"]) if isinstance(r["eval_issues"], str) else r["eval_issues"]
except (json.JSONDecodeError, TypeError):
pass
prs.append({
"number": r["number"],
"agent": r["agent"],
"domain": r["domain"],
"tier": r["tier"],
"status": r["status"],
"created_at": r["created_at"],
"merged_at": r["merged_at"],
"claim_titles": claim_titles,
"source_path": r["source_path"],
"submitted_by": r["submitted_by"],
"eval_issues": issues,
"leo_verdict": r["leo_verdict"],
})
# Summary stats
merged = sum(1 for p in prs if p["status"] == "merged")
closed = sum(1 for p in prs if p["status"] == "closed")
open_prs = sum(1 for p in prs if p["status"] == "open")
return web.json_response({
"days": days,
"total": len(prs),
"merged": merged,
"closed": closed,
"open": open_prs,
"merge_rate": round(merged / len(prs) * 100, 1) if prs else 0,
"prs": prs,
})
finally:
conn.close()
# ─── GET /api/contributor-growth ─────────────────────────────────────────
CODEX_WORKTREE = Path(os.environ.get("MAIN_WORKTREE", "/opt/teleo-eval/workspaces/main"))
FOUNDING_CUTOFF = "2026-03-15"
CONTRIBUTOR_EXCLUDE = {"Teleo Agents", "Teleo Pipeline"}
_growth_cache: dict | None = None
_growth_cache_ts: float = 0
GROWTH_CACHE_TTL = 300
async def handle_contributor_growth(request):
"""Cumulative unique contributors and claims over time from git log.
Returns time-series data for Chart.js line charts.
Cached for 5 minutes since git log is expensive.
"""
global _growth_cache, _growth_cache_ts
now = time.monotonic()
if _growth_cache is not None and (now - _growth_cache_ts) < GROWTH_CACHE_TTL:
return web.json_response(_growth_cache)
codex_path = str(CODEX_WORKTREE)
if not CODEX_WORKTREE.exists():
return web.json_response(
{"error": "codex worktree not found", "path": codex_path}, status=404
)
proc = await asyncio.create_subprocess_exec(
"git", "log", "--format=%ad|%an", "--date=format:%Y-%m-%d", "--all",
cwd=codex_path,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
if proc.returncode != 0:
return web.json_response(
{"error": "git log failed", "detail": stderr.decode()[:500]}, status=500
)
first_seen: dict[str, str] = {}
daily_commits: dict[str, dict[str, int]] = defaultdict(lambda: defaultdict(int))
for line in stdout.decode().strip().split("\n"):
if "|" not in line:
continue
date, author = line.split("|", 1)
if author in CONTRIBUTOR_EXCLUDE:
continue
daily_commits[date][author] += 1
if author not in first_seen or date < first_seen[author]:
first_seen[author] = date
by_date: dict[str, list[str]] = defaultdict(list)
for author, date in first_seen.items():
by_date[date].append(author)
contributors_timeline = []
seen: set[str] = set()
for date in sorted(by_date.keys()):
new_authors = by_date[date]
seen.update(new_authors)
contributors_timeline.append({
"date": date,
"cumulative": len(seen),
"new": [{"name": a, "founding": date <= FOUNDING_CUTOFF} for a in sorted(new_authors)],
})
proc2 = await asyncio.create_subprocess_exec(
"git", "log", "--format=%ad", "--date=format:%Y-%m-%d",
"--all", "--diff-filter=A", "--", "domains/*.md",
cwd=codex_path,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout2, _ = await proc2.communicate()
claim_counts: dict[str, int] = defaultdict(int)
for line in stdout2.decode().strip().split("\n"):
line = line.strip()
if line:
claim_counts[line] += 1
claims_timeline = []
cumulative = 0
for date in sorted(claim_counts.keys()):
cumulative += claim_counts[date]
claims_timeline.append({"date": date, "cumulative": cumulative, "added": claim_counts[date]})
all_contributors = set(first_seen.keys())
founding = sorted(a for a in all_contributors if first_seen[a] <= FOUNDING_CUTOFF)
result = {
"generated_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"summary": {
"total_contributors": len(all_contributors),
"founding_contributors": founding,
"total_claims": cumulative,
"days_active": (datetime.now(timezone.utc) - datetime(2026, 3, 5, tzinfo=timezone.utc)).days,
},
"cumulative_contributors": contributors_timeline,
"cumulative_claims": claims_timeline,
}
_growth_cache = result
_growth_cache_ts = now
return web.json_response(result)
# ─── Registration ──────────────────────────────────────────────────────────
@ -1303,47 +929,6 @@ def register_dashboard_routes(app: web.Application, get_conn):
app.router.add_get("/api/agents-dashboard", handle_agents_dashboard)
app.router.add_get("/api/cascade-coverage", handle_cascade_coverage)
app.router.add_get("/api/review-summary", handle_review_summary)
app.router.add_get("/api/agent-scorecard", handle_agent_scorecard)
app.router.add_get("/api/trace/{trace_id}", handle_trace)
app.router.add_get("/api/growth", handle_growth)
app.router.add_get("/api/pr-lifecycle", handle_pr_lifecycle)
app.router.add_get("/api/telegram-extractions", handle_telegram_extractions)
app.router.add_get("/api/contributor-growth", handle_contributor_growth)
app.router.add_get("/api/digest/latest", handle_digest_latest)
app.router.add_get("/api/contributor-graph", handle_contributor_graph)
async def handle_digest_latest(request):
"""GET /api/digest/latest — return the most recent scoring digest."""
import json as _json
digest_path = "/opt/teleo-eval/logs/scoring-digest-latest.json"
try:
with open(digest_path) as f:
data = _json.load(f)
return web.json_response(data)
except FileNotFoundError:
return web.json_response({"error": "No digest available yet"}, status=404)
except Exception as e:
return web.json_response({"error": str(e)}, status=500)
async def handle_contributor_graph(request):
"""GET /api/contributor-graph — serve the PNG chart."""
import subprocess, os
png_path = "/opt/teleo-eval/static/contributor-graph.png"
# Regenerate if older than 1 hour or missing
regen = not os.path.exists(png_path)
if not regen:
age = __import__('time').time() - os.path.getmtime(png_path)
regen = age > 3600
if regen:
try:
subprocess.run(
['python3', '/opt/teleo-eval/scripts/contributor-graph.py'],
timeout=30, capture_output=True
)
except Exception:
pass
if not os.path.exists(png_path):
return web.Response(text='Chart not available', status=503)
return web.FileResponse(png_path, headers={'Content-Type': 'image/png'})

View file

@ -1,309 +0,0 @@
"""Read-only canonical KB claim routes for Argus.
These routes show the Postgres-backed claim graph that Leo uses through the
``teleo-kb`` bridge: one claim, its evidence rows, and its graph edges.
"""
from __future__ import annotations
import argparse
import json
import logging
import os
import re
import sys
from datetime import datetime, timezone
from html import escape
from pathlib import Path
from typing import Any
from aiohttp import web
from shared_ui import render_page
ROOT = Path(__file__).resolve().parent.parent
SCRIPT_DIR_CANDIDATES = [
ROOT / "scripts",
Path(os.environ.get("TELEO_INFRA_REPO_DIR", "/opt/teleo-eval/workspaces/deploy-infra")) / "scripts",
]
for scripts_dir in SCRIPT_DIR_CANDIDATES:
if str(scripts_dir) not in sys.path:
sys.path.insert(0, str(scripts_dir))
import kb_proposal_review_packet as proposal_review # noqa: E402
logger = logging.getLogger("argus.kb_claims")
KB_CLAIM_PUBLIC_PATHS = frozenset()
KB_CLAIM_PUBLIC_PREFIXES = ("/kb/claims/", "/api/kb/claims/")
KB_CLAIM_LOADER_KEY = web.AppKey("kb_claim_loader", object)
UUID_RE = re.compile(
r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
)
def is_claim_id(value: Any) -> bool:
return bool(UUID_RE.match(str(value or "")))
def claim_path(claim_id: str) -> str:
return f"/kb/claims/{claim_id}"
def claim_link_html(claim_id: Any, *, label: str | None = None) -> str:
value = str(claim_id or "")
if not is_claim_id(value):
return f"<code>{escape(value or 'unknown')}</code>"
text = label or value
return f'<a class="claim-link" href="{claim_path(value)}"><code>{escape(text)}</code></a>'
def short_claim_link_html(claim_id: Any) -> str:
value = str(claim_id or "")
if not is_claim_id(value):
return f"<code>{escape(value or 'unknown')}</code>"
return claim_link_html(value, label=value[:8])
def _db_args() -> argparse.Namespace:
return argparse.Namespace(
secrets_file=os.environ.get(
"KB_CLAIM_REVIEW_SECRETS_FILE",
os.environ.get(
"KB_PROPOSAL_REVIEW_SECRETS_FILE",
os.environ.get("KB_APPLY_SECRETS_FILE", proposal_review.ap.DEFAULT_SECRETS_FILE),
),
),
container=os.environ.get("KB_CLAIM_REVIEW_CONTAINER", proposal_review.ap.DEFAULT_CONTAINER),
db=os.environ.get("KB_CLAIM_REVIEW_DB", proposal_review.ap.DEFAULT_DB),
host=os.environ.get("KB_CLAIM_REVIEW_HOST", proposal_review.ap.DEFAULT_HOST),
role=os.environ.get("KB_CLAIM_REVIEW_ROLE", proposal_review.ap.DEFAULT_ROLE),
)
def _load_claim_from_db(claim_id: str) -> dict[str, Any] | None:
password = proposal_review.ap.load_password(_db_args().secrets_file)
args = _db_args()
sql = f"""
with target as (
select c.id,
c.type,
c.text,
c.status,
c.confidence,
c.tags,
c.superseded_by,
c.created_at,
c.updated_at
from public.claims c
where c.id = {proposal_review.ap.sql_literal(claim_id)}::uuid
)
select jsonb_build_object(
'claim', jsonb_build_object(
'id', target.id::text,
'type', target.type,
'text', target.text,
'status', target.status,
'confidence', target.confidence,
'tags', coalesce(to_jsonb(target.tags), '[]'::jsonb),
'superseded_by', target.superseded_by::text,
'created_at', target.created_at::text,
'updated_at', target.updated_at::text
),
'evidence', coalesce((
select jsonb_agg(row_data order by rn)
from (
select row_number() over (
order by ce.role::text,
ce.weight desc nulls last,
s.storage_path nulls last,
s.url nulls last
) as rn,
jsonb_build_object(
'role', ce.role::text,
'weight', ce.weight,
'source_type', s.source_type,
'url', s.url,
'storage_path', s.storage_path,
'excerpt', left(coalesce(s.excerpt, ''), 1200)
) as row_data
from public.claim_evidence ce
join public.sources s on s.id = ce.source_id
where ce.claim_id = target.id
limit 30
) evidence_rows
), '[]'::jsonb),
'edges', coalesce((
select jsonb_agg(row_data order by rn)
from (
select row_number() over (
order by e.edge_type::text,
other.text
) as rn,
jsonb_build_object(
'direction', case when e.from_claim = target.id then 'outgoing' else 'incoming' end,
'edge_type', e.edge_type::text,
'connected_id', other.id::text,
'connected_text', other.text,
'connected_status', other.status,
'connected_confidence', other.confidence
) as row_data
from public.claim_edges e
join public.claims other
on other.id = case when e.from_claim = target.id then e.to_claim else e.from_claim end
where e.from_claim = target.id or e.to_claim = target.id
limit 40
) edge_rows
), '[]'::jsonb)
)::text
from target;
"""
out = proposal_review.ap.run_psql(args, sql, password).strip()
return json.loads(out) if out else None
def _load_claim(request: web.Request, claim_id: str) -> dict[str, Any] | None:
loader = request.app.get(KB_CLAIM_LOADER_KEY)
return loader(claim_id) if loader else _load_claim_from_db(claim_id)
def _metadata_table(claim: dict[str, Any]) -> str:
rows = [
("ID", claim_link_html(claim.get("id"))),
("Status", f"<code>{escape(str(claim.get('status') or 'unknown'))}</code>"),
("Type", f"<code>{escape(str(claim.get('type') or 'unknown'))}</code>"),
("Confidence", f"<code>{escape(str(claim.get('confidence') or 'unknown'))}</code>"),
("Tags", f"<code>{escape(', '.join(claim.get('tags') or []) or 'none')}</code>"),
("Created", f"<code>{escape(str(claim.get('created_at') or 'unknown'))}</code>"),
("Updated", f"<code>{escape(str(claim.get('updated_at') or 'unknown'))}</code>"),
]
if claim.get("superseded_by"):
rows.append(("Superseded by", claim_link_html(claim["superseded_by"])))
return "\n".join(f"<tr><th>{escape(label)}</th><td>{value}</td></tr>" for label, value in rows)
def _render_evidence(evidence: list[dict[str, Any]]) -> str:
if not evidence:
return '<div class="empty-state">No evidence rows found for this claim.</div>'
rows = []
for row in evidence:
source = row.get("url") or row.get("storage_path") or "no source pointer"
source_html = escape(str(source))
if row.get("url"):
source_html = f'<a href="{escape(str(row["url"]))}">{source_html}</a>'
rows.append(
"<tr>"
f"<td><code>{escape(str(row.get('role') or 'unknown'))}</code></td>"
f"<td><code>{escape(str(row.get('weight') or ''))}</code></td>"
f"<td><code>{escape(str(row.get('source_type') or 'unknown'))}</code></td>"
f"<td>{source_html}</td>"
f"<td>{escape(str(row.get('excerpt') or ''))}</td>"
"</tr>"
)
return (
"<table class=\"claim-table\"><thead><tr><th>Role</th><th>Weight</th>"
"<th>Source type</th><th>Source</th><th>Excerpt</th></tr></thead>"
f"<tbody>{''.join(rows)}</tbody></table>"
)
def _render_edges(edges: list[dict[str, Any]]) -> str:
if not edges:
return '<div class="empty-state">No graph edges found for this claim.</div>'
rows = []
for row in edges:
rows.append(
"<tr>"
f"<td><code>{escape(str(row.get('direction') or 'unknown'))}</code></td>"
f"<td><code>{escape(str(row.get('edge_type') or 'unknown'))}</code></td>"
f"<td>{short_claim_link_html(row.get('connected_id'))}</td>"
f"<td>{escape(str(row.get('connected_text') or ''))}</td>"
f"<td><code>{escape(str(row.get('connected_status') or ''))}</code></td>"
"</tr>"
)
return (
"<table class=\"claim-table\"><thead><tr><th>Direction</th><th>Edge</th>"
"<th>Connected claim</th><th>Text</th><th>Status</th></tr></thead>"
f"<tbody>{''.join(rows)}</tbody></table>"
)
def render_kb_claim_page(data: dict[str, Any]) -> str:
claim = data["claim"]
generated_at = datetime.now(timezone.utc).isoformat()
body = f"""
<div class="claim-card">
<div class="label">Canonical claim</div>
<p class="claim-text">{escape(str(claim.get("text") or ""))}</p>
<table class="proposal-detail"><tbody>{_metadata_table(claim)}</tbody></table>
</div>
<div class="section">
<div class="section-title">Evidence</div>
{_render_evidence(data.get("evidence") or [])}
</div>
<div class="section">
<div class="section-title">Edges</div>
{_render_edges(data.get("edges") or [])}
</div>"""
extra_css = """
.claim-card { background: #161b22; border: 1px solid #30363d; border-radius: 8px; padding: 16px; }
.claim-text { margin-top: 8px; color: #f0f6fc; font-size: 17px; line-height: 1.45; }
.claim-link { color: #58a6ff; text-decoration: none; }
.claim-link:hover { text-decoration: underline; }
.claim-table td { vertical-align: top; line-height: 1.35; }
.claim-table td:nth-child(4), .claim-table td:nth-child(5) { overflow-wrap: anywhere; }
.proposal-detail { margin-top: 14px; }
.proposal-detail th { width: 150px; vertical-align: top; }
.empty-state { color: #8b949e; background: #161b22; border: 1px solid #30363d;
border-radius: 8px; padding: 14px; }
"""
return render_page(
"KB Claim",
"Canonical claim, evidence rows, and graph edges",
"/kb-proposals",
body,
extra_css=extra_css,
timestamp=generated_at,
)
async def handle_api_kb_claim(request: web.Request) -> web.Response:
claim_id = request.match_info["claim_id"]
if not is_claim_id(claim_id):
return web.json_response({"error": "invalid_claim_id", "claim_id": claim_id}, status=400)
try:
data = _load_claim(request, claim_id)
except BaseException as exc:
logger.exception("KB claim API load failed")
return web.json_response({"error": "kb_claim_load_failed", "detail": str(exc)}, status=500)
if not data:
return web.json_response({"error": "claim_not_found", "claim_id": claim_id}, status=404)
return web.json_response(data)
async def handle_kb_claim_page(request: web.Request) -> web.Response:
claim_id = request.match_info["claim_id"]
if not is_claim_id(claim_id):
return web.Response(text="Invalid claim id", status=400)
try:
data = _load_claim(request, claim_id)
except BaseException as exc:
logger.exception("KB claim page load failed")
return web.Response(
text=render_page(
"KB Claim",
"Canonical claim, evidence rows, and graph edges",
"/kb-proposals",
f'<div class="alert-banner alert-critical">Failed to load KB claim: {escape(str(exc))}</div>',
),
content_type="text/html",
status=500,
)
if not data:
return web.Response(text="Claim not found", status=404)
return web.Response(text=render_kb_claim_page(data), content_type="text/html")
def register_kb_claim_routes(app: web.Application) -> None:
app.router.add_get("/api/kb/claims/{claim_id}", handle_api_kb_claim)
app.router.add_get("/kb/claims/{claim_id}", handle_kb_claim_page)

View file

@ -1,535 +0,0 @@
"""Read-only KB proposal review routes for Argus.
This surface is the operator-visible bridge between Leo's Telegram KB reasoning
and the narrow apply worker. It intentionally performs no approve/reject/apply
mutation; it only renders the same packets produced by
``scripts/kb_proposal_review_packet.py``.
"""
from __future__ import annotations
import argparse
import logging
import os
import sys
from collections import Counter
from datetime import datetime, timezone
from html import escape
from pathlib import Path
from typing import Any
from urllib.parse import urlencode
from aiohttp import web
from shared_ui import render_page
ROOT = Path(__file__).resolve().parent.parent
SCRIPT_DIR_CANDIDATES = [
ROOT / "scripts",
Path(os.environ.get("TELEO_INFRA_REPO_DIR", "/opt/teleo-eval/workspaces/deploy-infra")) / "scripts",
]
for scripts_dir in SCRIPT_DIR_CANDIDATES:
if str(scripts_dir) not in sys.path:
sys.path.insert(0, str(scripts_dir))
import kb_proposal_normalize as proposal_normalize # noqa: E402
import kb_proposal_review_packet as proposal_review # noqa: E402
from kb_claim_routes import claim_link_html # noqa: E402
logger = logging.getLogger("argus.kb_proposals")
KB_PROPOSAL_PUBLIC_PATHS = frozenset({"/kb-proposals", "/api/kb-proposals"})
KB_PROPOSAL_LOADER_KEY = web.AppKey("kb_proposal_loader", object)
DEFAULT_LIMIT = 20
MAX_LIMIT = 100
def _query_limit(value: str | None) -> int:
if not value:
return DEFAULT_LIMIT
try:
limit = int(value)
except ValueError:
return DEFAULT_LIMIT
return max(1, min(limit, MAX_LIMIT))
def _query_filters(request: web.Request) -> dict[str, Any]:
status = request.query.get("status", "approved").strip() or "approved"
proposal_id = (request.query.get("proposal_id") or "").strip()
if proposal_id:
status = ""
return {
"status": status,
"proposal_id": proposal_id,
"limit": _query_limit(request.query.get("limit")),
}
def _db_args(filters: dict[str, Any]) -> argparse.Namespace:
return argparse.Namespace(
proposal_id=filters["proposal_id"] or None,
status=filters["status"] or None,
limit=filters["limit"],
secrets_file=os.environ.get(
"KB_PROPOSAL_REVIEW_SECRETS_FILE",
os.environ.get("KB_APPLY_SECRETS_FILE", proposal_review.ap.DEFAULT_SECRETS_FILE),
),
container=os.environ.get("KB_PROPOSAL_REVIEW_CONTAINER", proposal_review.ap.DEFAULT_CONTAINER),
db=os.environ.get("KB_PROPOSAL_REVIEW_DB", proposal_review.ap.DEFAULT_DB),
host=os.environ.get("KB_PROPOSAL_REVIEW_HOST", proposal_review.ap.DEFAULT_HOST),
role=os.environ.get("KB_PROPOSAL_REVIEW_ROLE", proposal_review.ap.DEFAULT_ROLE),
)
def _load_packets(request: web.Request, filters: dict[str, Any]) -> list[dict[str, Any]]:
loader = request.app.get(KB_PROPOSAL_LOADER_KEY)
proposals = loader(_db_args(filters)) if loader else proposal_review.load_from_db(_db_args(filters))
packets = []
for proposal in proposals:
packet = proposal_review.classify_proposal(proposal)
packet["normalization_preview"] = proposal_normalize.normalize_proposal(proposal)
packet["apply_preview"] = build_apply_preview(proposal, packet)
packets.append(packet)
return packets
def _counts(packets: list[dict[str, Any]], key: str) -> dict[str, int]:
return dict(Counter(str(packet.get(key) or "unknown") for packet in packets))
def build_kb_proposal_response(packets: list[dict[str, Any]], filters: dict[str, Any]) -> dict[str, Any]:
return {
"generated_at": datetime.now(timezone.utc).isoformat(),
"filters": filters,
"total": len(packets),
"status_counts": _counts(packets, "status"),
"review_state_counts": _counts(packets, "review_state"),
"worker_applyable_count": sum(1 for packet in packets if packet.get("worker_applyable")),
"packets": packets,
"read_only": True,
}
def _badge_class(value: str) -> str:
if value in {"applied", "approved_applyable"}:
return "badge-green"
if value in {"approved", "approved_needs_apply_payload", "needs_human_review", "pending_review"}:
return "badge-yellow"
if value in {"unsupported_by_apply_worker", "not_ready"}:
return "badge-red"
return "badge-blue"
def _badge(value: Any) -> str:
text = escape(str(value or "unknown"))
return f'<span class="badge {_badge_class(str(value or ""))}">{text}</span>'
def _join_values(values: list[Any]) -> str:
if not values:
return '<span class="muted">none</span>'
return ", ".join(escape(str(value)) for value in values)
def _code(value: Any) -> str:
return f"<code>{escape(str(value))}</code>"
def _payload_dict(value: Any) -> dict[str, Any]:
return value if isinstance(value, dict) else {}
def _payload_list(value: Any) -> list[Any]:
return value if isinstance(value, list) else []
def _apply_preview_row(
*,
action: str,
table: str,
target: str,
claim_ids: list[str] | None = None,
details: dict[str, Any] | None = None,
source: str = "proposal",
) -> dict[str, Any]:
return {
"action": action,
"table": table,
"target": target,
"claim_ids": claim_ids or [],
"details": details or {},
"source": source,
}
def _strict_apply_preview_rows(
proposal_type: str | None,
apply_payload: dict[str, Any],
*,
source: str = "proposal.apply_payload",
) -> list[dict[str, Any]]:
if proposal_type == "add_edge":
from_claim = str(apply_payload.get("from_claim") or "")
to_claim = str(apply_payload.get("to_claim") or "")
edge_type = str(apply_payload.get("edge_type") or "")
return [
_apply_preview_row(
action="insert_if_missing",
table="public.claim_edges",
target=f"{from_claim} -> {to_claim} ({edge_type or 'edge_type missing'})",
claim_ids=[claim_id for claim_id in [from_claim, to_claim] if claim_id],
details={
"from_claim": from_claim,
"to_claim": to_claim,
"edge_type": edge_type,
"weight": apply_payload.get("weight"),
},
source=source,
)
]
if proposal_type == "attach_evidence":
rows = []
for evidence in _payload_list(apply_payload.get("evidence")):
ev = _payload_dict(evidence)
claim_id = str(ev.get("claim_id") or "")
source_id = str(ev.get("source_id") or "")
role = str(ev.get("role") or "grounds")
rows.append(
_apply_preview_row(
action="insert_if_missing",
table="public.claim_evidence",
target=f"{claim_id} <= {source_id} ({role})",
claim_ids=[claim_id] if claim_id else [],
details={
"claim_id": claim_id,
"source_id": source_id,
"role": role,
"weight": ev.get("weight"),
},
source=source,
)
)
return rows
if proposal_type == "revise_strategy":
agent_id = str(apply_payload.get("agent_id") or "")
strategy_nodes = _payload_list(apply_payload.get("strategy_nodes"))
return [
_apply_preview_row(
action="update_then_insert",
table="public.strategies",
target=f"new active strategy for agent {agent_id or 'unknown'}",
details={
"agent_id": agent_id,
"strategy_keys": sorted(_payload_dict(apply_payload.get("strategy")).keys()),
},
source=source,
),
_apply_preview_row(
action="retire_then_insert",
table="public.strategy_nodes",
target=f"{len(strategy_nodes)} replacement strategy node(s)",
details={"agent_id": agent_id, "strategy_node_count": len(strategy_nodes)},
source=source,
),
]
return [
_apply_preview_row(
action="blocked",
table="none",
target=f"unsupported proposal_type {proposal_type or 'unknown'}",
details={"proposal_type": proposal_type},
source=source,
)
]
def build_apply_preview(proposal: dict[str, Any], packet: dict[str, Any]) -> dict[str, Any]:
"""Build a read-only, row-level preview of what an apply step would touch."""
payload = _payload_dict(proposal.get("payload"))
apply_payload = _payload_dict(payload.get("apply_payload"))
proposal_type = proposal.get("proposal_type")
normalization = _payload_dict(packet.get("normalization_preview"))
if proposal.get("status") == "applied":
return {
"state": "applied",
"rows": [],
"blocked_fragments": [],
"note": "Proposal is already applied; use the canonical claim/edge/evidence readback.",
}
if apply_payload and proposal_type in proposal_review.ap.APPLYABLE_TYPES:
return {
"state": "strict_apply_payload_ready",
"rows": _strict_apply_preview_rows(proposal_type, apply_payload),
"blocked_fragments": [],
"note": "Preview only; the page does not execute canonical writes.",
}
child_rows = []
for index, child in enumerate(_payload_list(normalization.get("strict_child_proposals"))):
child_payload = _payload_dict(_payload_dict(child).get("payload"))
child_apply_payload = _payload_dict(child_payload.get("apply_payload"))
child_type = str(_payload_dict(child).get("proposal_type") or "")
child_rows.extend(
_strict_apply_preview_rows(
child_type,
child_apply_payload,
source=f"normalization.strict_child_proposals[{index}]",
)
)
blocked = _payload_list(normalization.get("blocked_fragments"))
if child_rows and not blocked:
state = "strict_child_proposals_ready"
note = "Preview of strict child proposals; reviewer still needs to stage those children before apply."
elif child_rows:
state = "partial_preview_blocked_fragments"
note = "Some strict child rows can be previewed; blocked fragments still need canonical IDs or schema decisions."
else:
state = "not_applyable_yet"
note = "No canonical write preview is safe until the proposal has a strict apply_payload or strict child proposals."
return {
"state": state,
"rows": child_rows,
"blocked_fragments": blocked,
"note": note,
}
def _status_link(label: str, status: str, filters: dict[str, Any]) -> str:
query = urlencode({"status": status, "limit": filters["limit"]})
return f'<a class="filter-link" href="/kb-proposals?{query}">{escape(label)}</a>'
def _apply_preview_html(preview: dict[str, Any]) -> str:
state = _badge(preview.get("state"))
rows = _payload_list(preview.get("rows"))
blocked = _payload_list(preview.get("blocked_fragments"))
note = escape(str(preview.get("note") or ""))
row_html = ""
if rows:
rendered_rows = []
for row in rows:
details = _payload_dict(_payload_dict(row).get("details"))
detail_text = ", ".join(
f"{escape(str(key))}={escape(str(value))}"
for key, value in details.items()
if value not in (None, "")
)
claim_links = " ".join(
claim_link_html(claim_id, label=str(claim_id)[:8])
for claim_id in _payload_list(_payload_dict(row).get("claim_ids"))
) or '<span class="muted">none</span>'
detail_html = escape(detail_text) if detail_text else '<span class="muted">none</span>'
rendered_rows.append(
"<tr>"
f"<td>{_code(_payload_dict(row).get('action') or 'unknown')}</td>"
f"<td>{_code(_payload_dict(row).get('table') or 'unknown')}</td>"
f"<td>{escape(str(_payload_dict(row).get('target') or ''))}</td>"
f"<td>{claim_links}</td>"
f"<td>{detail_html}</td>"
"</tr>"
)
row_html = f"""
<table class="proposal-detail apply-preview-table">
<thead><tr><th>Action</th><th>Table</th><th>Target</th><th>Claims</th><th>Details</th></tr></thead>
<tbody>{''.join(rendered_rows)}</tbody>
</table>"""
else:
row_html = '<p class="muted">No canonical row preview is executable yet.</p>'
blocked_html = ""
if blocked:
blocked_rows = []
for fragment in blocked:
frag = _payload_dict(fragment)
missing = ", ".join(escape(str(value)) for value in _payload_list(frag.get("missing"))) or "unknown"
blocked_rows.append(
"<tr>"
f"<td>{_code(frag.get('kind') or 'fragment')}</td>"
f"<td>{escape(str(frag.get('reason') or 'blocked'))}</td>"
f"<td>{missing}</td>"
"</tr>"
)
blocked_html = f"""
<div class="label preview-label">Blocked fragments</div>
<table class="proposal-detail apply-preview-table">
<thead><tr><th>Kind</th><th>Reason</th><th>Missing</th></tr></thead>
<tbody>{''.join(blocked_rows)}</tbody>
</table>"""
return f"""
<div class="next-action apply-preview">
<div class="label">Apply preview</div>
<p>{state} <span class="muted">{note}</span></p>
{row_html}
{blocked_html}
</div>"""
def _packet_card(packet: dict[str, Any]) -> str:
auth = packet.get("identity_and_authorization") or {}
summary = packet.get("payload_summary") or {}
applyability = packet.get("applyability") or {}
proposal_id_raw = str(packet.get("proposal_id") or "unknown")
proposal_id = _code(proposal_id_raw)
proposal_kind = escape(str(summary.get("proposal_kind") or "unknown"))
next_action = escape(str(packet.get("next_admin_action") or "No next action recorded."))
source_ref = escape(str(auth.get("source_ref") or "unknown"))
channel = escape(str(auth.get("channel") or "unknown"))
reviewed_by = escape(str(auth.get("reviewed_by_handle") or "unreviewed"))
rows = [
("Status", _badge(packet.get("status"))),
("Review state", _badge(packet.get("review_state"))),
("Worker applyable", "yes" if packet.get("worker_applyable") else "no"),
("Has strict apply payload", "yes" if packet.get("has_apply_payload") else "no"),
("Proposal kind", proposal_kind),
("Reviewed by", reviewed_by),
("Channel", channel),
("Source ref", source_ref),
("Old claim id", claim_link_html(summary.get("old_claim_id") or "unknown")),
("Claim candidates", _code(summary.get("claim_candidate_count", 0))),
("Source candidates", _code(summary.get("source_candidate_count", 0))),
("Supersession edges", _code(summary.get("supersession_edge_count", 0))),
("Missing contract", _join_values(applyability.get("missing_contract") or [])),
]
normalization = packet.get("normalization_preview") or {}
if normalization:
rows.extend(
[
("Normalization", _badge(normalization.get("normalization_state"))),
("Strict child proposals", _code(normalization.get("strict_child_count", 0))),
("Blocked fragments", _code(normalization.get("blocked_count", 0))),
]
)
details = "\n".join(
f"<tr><th>{escape(label)}</th><td>{value}</td></tr>" for label, value in rows
)
normalization_html = ""
if normalization:
normalization_html = f"""
<div class="next-action">
<div class="label">Normalization action</div>
<p>{escape(str(normalization.get("next_normalization_action") or "No normalization action recorded."))}</p>
</div>"""
apply_preview_html = _apply_preview_html(packet.get("apply_preview") or {})
return f"""
<article class="proposal-card">
<div class="proposal-card-header">
<h2>{proposal_id}</h2>
</div>
<table class="proposal-detail">
<tbody>{details}</tbody>
</table>
<div class="next-action">
<div class="label">Next admin action</div>
<p>{next_action}</p>
</div>
{normalization_html}
{apply_preview_html}
</article>"""
def render_kb_proposals_page(data: dict[str, Any]) -> str:
filters = data["filters"]
packets = data["packets"]
generated_at = escape(str(data["generated_at"]))
cards = "\n".join(_packet_card(packet) for packet in packets)
if not cards:
cards = '<div class="alert-banner alert-info">No proposals matched this filter.</div>'
filter_html = " ".join(
[
_status_link("Approved", "approved", filters),
_status_link("Pending review", "pending_review", filters),
_status_link("Applied", "applied", filters),
]
)
body = f"""
<div class="alert-banner alert-info">
Read-only review surface. It shows proposal intent, applyability, missing contracts,
and the next admin action; it does not approve, reject, or apply proposals.
</div>
<div class="grid">
<div class="card"><div class="label">Matching proposals</div><div class="value">{data["total"]}</div></div>
<div class="card"><div class="label">Worker applyable</div><div class="value">{data["worker_applyable_count"]}</div></div>
<div class="card"><div class="label">Current status filter</div><div class="value small-value">{escape(filters.get("status") or "by id")}</div></div>
</div>
<div class="section">
<div class="section-title">Filters</div>
<div class="filters">{filter_html}</div>
</div>
<div class="section">
<div class="section-title">Proposal Review Packets</div>
<div class="proposal-list">{cards}</div>
</div>"""
extra_css = """
.small-value { font-size: 16px !important; overflow-wrap: anywhere; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; }
.filter-link { color: #58a6ff; background: #161b22; border: 1px solid #30363d;
border-radius: 6px; padding: 6px 10px; text-decoration: none; font-size: 13px; }
.filter-link:hover { background: #21262d; }
.claim-link { color: #58a6ff; text-decoration: none; }
.claim-link:hover { text-decoration: underline; }
.proposal-list { display: grid; gap: 16px; }
.proposal-card { background: #161b22; border: 1px solid #30363d; border-radius: 8px; padding: 16px; }
.proposal-card-header { display: flex; justify-content: space-between; gap: 12px; align-items: start; }
.proposal-card h2 { color: #c9d1d9; font-size: 15px; overflow-wrap: anywhere; }
.proposal-detail { margin-top: 12px; }
.proposal-detail th { width: 210px; vertical-align: top; }
.proposal-detail td { overflow-wrap: anywhere; }
.next-action { margin-top: 14px; padding: 12px; border: 1px solid #30363d; border-radius: 6px; background: #0d1117; }
.next-action .label { color: #8b949e; font-size: 11px; text-transform: uppercase; margin-bottom: 6px; }
.next-action p { color: #c9d1d9; line-height: 1.4; }
.muted { color: #8b949e; }
"""
return render_page(
"KB Proposals",
"Review Leo KB proposal packets before any apply step",
"/kb-proposals",
body,
extra_css=extra_css,
timestamp=generated_at,
)
async def handle_api_kb_proposals(request: web.Request) -> web.Response:
filters = _query_filters(request)
try:
packets = _load_packets(request, filters)
except BaseException as exc:
logger.exception("KB proposal packet load failed")
return web.json_response({"error": "kb_proposal_load_failed", "detail": str(exc)}, status=500)
return web.json_response(build_kb_proposal_response(packets, filters))
async def handle_kb_proposals_page(request: web.Request) -> web.Response:
filters = _query_filters(request)
try:
packets = _load_packets(request, filters)
except BaseException as exc:
logger.exception("KB proposal packet page failed")
return web.Response(
text=render_page(
"KB Proposals",
"Review Leo KB proposal packets before any apply step",
"/kb-proposals",
f'<div class="alert-banner alert-critical">Failed to load KB proposals: {escape(str(exc))}</div>',
),
content_type="text/html",
status=500,
)
data = build_kb_proposal_response(packets, filters)
return web.Response(text=render_kb_proposals_page(data), content_type="text/html")
def register_kb_proposal_routes(app: web.Application) -> None:
app.router.add_get("/api/kb-proposals", handle_api_kb_proposals)
app.router.add_get("/kb-proposals", handle_kb_proposals_page)

View file

@ -1,166 +0,0 @@
"""Leaderboard endpoint reading from event-sourced contribution_events.
Owner: Argus
Source of truth: pipeline.db contribution_events (Epimetheus, schema v25)
Reads contribution_events GROUP BY handle, computes CI as SUM(weight),
joins contributors for kind, returns sorted leaderboard with role breakdown.
Roles + weights (Phase A):
author 0.30 | challenger 0.25 | synthesizer 0.20 | originator 0.15 | evaluator 0.05
Endpoints:
GET /api/leaderboard?window=all_time|Nd|Nh&domain=&kind=person|agent|org|all&limit=100
"""
import logging
import re
import sqlite3
from aiohttp import web
logger = logging.getLogger("argus.leaderboard_routes")
ROLE_KEYS = ("author", "challenger", "synthesizer", "originator", "evaluator")
KIND_VALUES = ("person", "agent", "org", "all")
# Public path set so auth middleware lets it through
LEADERBOARD_PUBLIC_PATHS = frozenset({"/api/leaderboard"})
def _conn(app):
"""Read-only connection to pipeline.db."""
db_path = app["db_path"]
conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True)
conn.row_factory = sqlite3.Row
return conn
def _parse_window(raw):
"""Parse window param. Returns (sql_clause, params_tuple, label).
Accepts: 'all_time' (default), 'Nd' (last N days), 'Nh' (last N hours).
Caps N at 365d / 8760h to prevent abuse.
"""
if not raw or raw == "all_time":
return ("", (), "all_time")
m = re.fullmatch(r"(\d+)([dh])", raw.strip().lower())
if not m:
return ("", (), "all_time")
n = int(m.group(1))
unit = m.group(2)
# Note: WHERE clause is composed via " AND ".join(...) — do NOT prefix with "AND ".
if unit == "d":
n = min(n, 365)
return ("ce.timestamp >= datetime('now', ?)", (f"-{n} days",), f"{n}d")
n = min(n, 8760)
return ("ce.timestamp >= datetime('now', ?)", (f"-{n} hours",), f"{n}h")
async def handle_leaderboard(request):
"""GET /api/leaderboard.
Query params:
window: 'all_time' (default) | 'Nd' (e.g. '7d') | 'Nh' (e.g. '24h')
domain: filter by domain (optional)
kind: 'person' (default) | 'agent' | 'org' | 'all'
limit: max entries (default 100, max 500)
"""
window_clause, window_params, window_label = _parse_window(request.query.get("window"))
domain = request.query.get("domain")
kind = request.query.get("kind", "person")
if kind not in KIND_VALUES:
kind = "person"
try:
limit = min(int(request.query.get("limit", "100")), 500)
except (ValueError, TypeError):
limit = 100
where = ["1=1", window_clause] if window_clause else ["1=1"]
params = list(window_params)
if domain:
where.append("ce.domain = ?")
params.append(domain)
if kind != "all":
where.append("COALESCE(c.kind, 'person') = ?")
params.append(kind)
where_sql = " AND ".join([w for w in where if w])
conn = _conn(request.app)
try:
# Aggregate per handle: total CI, per-role breakdown, event count, first/last timestamp
# LEFT JOIN contributors so handles in events but not in contributors still appear
# (defaults to kind='person' via COALESCE).
rows = conn.execute(f"""
SELECT
ce.handle,
COALESCE(c.kind, 'person') AS kind,
ROUND(SUM(ce.weight), 4) AS ci,
COUNT(*) AS events_count,
MIN(ce.timestamp) AS first_contribution,
MAX(ce.timestamp) AS last_contribution,
SUM(CASE WHEN ce.role='author' THEN ce.weight ELSE 0 END) AS ci_author,
SUM(CASE WHEN ce.role='challenger' THEN ce.weight ELSE 0 END) AS ci_challenger,
SUM(CASE WHEN ce.role='synthesizer' THEN ce.weight ELSE 0 END) AS ci_synthesizer,
SUM(CASE WHEN ce.role='originator' THEN ce.weight ELSE 0 END) AS ci_originator,
SUM(CASE WHEN ce.role='evaluator' THEN ce.weight ELSE 0 END) AS ci_evaluator,
COUNT(DISTINCT ce.domain) AS domain_count,
COUNT(DISTINCT ce.pr_number) AS pr_count
FROM contribution_events ce
LEFT JOIN contributors c ON c.handle = ce.handle
WHERE {where_sql}
GROUP BY ce.handle, COALESCE(c.kind, 'person')
ORDER BY ci DESC, last_contribution DESC
LIMIT ?
""", (*params, limit + 1)).fetchall() # +1 to detect overflow
has_more = len(rows) > limit
rows = rows[:limit]
# Total count of distinct handles matching filters (without limit)
total_row = conn.execute(f"""
SELECT COUNT(DISTINCT ce.handle) AS total
FROM contribution_events ce
LEFT JOIN contributors c ON c.handle = ce.handle
WHERE {where_sql}
""", params).fetchone()
total = total_row["total"] if total_row else 0
leaderboard = []
for r in rows:
leaderboard.append({
"handle": r["handle"],
"kind": r["kind"],
"ci": r["ci"],
"ci_breakdown": {
"author": round(r["ci_author"] or 0, 4),
"challenger": round(r["ci_challenger"] or 0, 4),
"synthesizer": round(r["ci_synthesizer"] or 0, 4),
"originator": round(r["ci_originator"] or 0, 4),
"evaluator": round(r["ci_evaluator"] or 0, 4),
},
"events_count": r["events_count"],
"domain_count": r["domain_count"],
"pr_count": r["pr_count"],
"first_contribution": r["first_contribution"],
"last_contribution": r["last_contribution"],
})
return web.json_response({
"window": window_label,
"domain": domain,
"kind_filter": kind,
"total": total,
"shown": len(leaderboard),
"has_more": has_more,
"source": "contribution_events", # explicit so consumers know the data origin
"leaderboard": leaderboard,
})
finally:
conn.close()
def register_leaderboard_routes(app: web.Application):
"""Register /api/leaderboard. Requires app['db_path'] to be set."""
app.router.add_get("/api/leaderboard", handle_leaderboard)

View file

@ -1,279 +0,0 @@
"""Dashboard API routes for research session + cost tracking.
Argus-side read-only endpoints. These query the data that
research_tracking.py writes to pipeline.db.
Add to app.py after alerting_routes setup.
"""
import json
import sqlite3
from aiohttp import web
def _conn(app):
"""Read-only connection to pipeline.db."""
db_path = app["db_path"]
conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True)
conn.row_factory = sqlite3.Row
return conn
async def handle_api_research_sessions(request):
"""GET /api/research-sessions?agent=&domain=&days=7
Returns research sessions with linked sources and cost data.
"""
agent = request.query.get("agent")
domain = request.query.get("domain")
try:
days = int(request.query.get("days", 7))
except (ValueError, TypeError):
days = 7
conn = _conn(request.app)
try:
where = ["rs.started_at >= datetime('now', ?)"]
params = [f"-{days} days"]
if agent:
where.append("rs.agent = ?")
params.append(agent)
if domain:
where.append("rs.domain = ?")
params.append(domain)
where_clause = " AND ".join(where)
sessions = conn.execute(f"""
SELECT rs.*,
GROUP_CONCAT(s.path, '||') as source_paths,
GROUP_CONCAT(s.status, '||') as source_statuses,
GROUP_CONCAT(s.claims_count, '||') as source_claims,
GROUP_CONCAT(COALESCE(s.cost_usd, 0), '||') as source_costs
FROM research_sessions rs
LEFT JOIN sources s ON s.session_id = rs.id
WHERE {where_clause}
GROUP BY rs.id
ORDER BY rs.started_at DESC
""", params).fetchall()
result = []
for s in sessions:
sources = []
if s["source_paths"]:
paths = s["source_paths"].split("||")
statuses = (s["source_statuses"] or "").split("||")
claims = (s["source_claims"] or "").split("||")
costs = (s["source_costs"] or "").split("||")
for i, p in enumerate(paths):
sources.append({
"path": p,
"status": statuses[i] if i < len(statuses) else None,
"claims_count": int(claims[i]) if i < len(claims) and claims[i] else 0,
"extraction_cost": float(costs[i]) if i < len(costs) and costs[i] else 0,
})
result.append({
"id": s["id"],
"agent": s["agent"],
"domain": s["domain"],
"topic": s["topic"],
"reasoning": s["reasoning"],
"summary": s["summary"],
"sources_planned": s["sources_planned"],
"sources_produced": s["sources_produced"],
"model": s["model"],
"input_tokens": s["input_tokens"],
"output_tokens": s["output_tokens"],
"research_cost": s["cost_usd"],
"extraction_cost": sum(src["extraction_cost"] for src in sources),
"total_cost": s["cost_usd"] + sum(src["extraction_cost"] for src in sources),
"total_claims": sum(src["claims_count"] for src in sources),
"status": s["status"],
"started_at": s["started_at"],
"completed_at": s["completed_at"],
"sources": sources,
})
# Summary stats
total_sessions = len(result)
total_cost = sum(r["total_cost"] for r in result)
total_claims = sum(r["total_claims"] for r in result)
total_sources = sum(r["sources_produced"] for r in result)
return web.json_response({
"summary": {
"sessions": total_sessions,
"total_cost": round(total_cost, 2),
"total_claims": total_claims,
"total_sources": total_sources,
"avg_cost_per_claim": round(total_cost / total_claims, 4) if total_claims else 0,
"avg_cost_per_session": round(total_cost / total_sessions, 4) if total_sessions else 0,
},
"sessions": result,
})
finally:
conn.close()
async def handle_api_costs(request):
"""GET /api/costs?days=14&by=stage|model|date
Comprehensive cost breakdown. Works with EXISTING data in costs table
plus the new extraction costs once backfilled.
"""
try:
days = int(request.query.get("days", 14))
except (ValueError, TypeError):
days = 14
group_by = request.query.get("by", "stage")
conn = _conn(request.app)
try:
valid_groups = {"stage", "model", "date"}
if group_by not in valid_groups:
group_by = "stage"
rows = conn.execute(f"""
SELECT {group_by},
SUM(calls) as total_calls,
SUM(input_tokens) as total_input,
SUM(output_tokens) as total_output,
SUM(cost_usd) as total_cost
FROM costs
WHERE date >= date('now', ?)
GROUP BY {group_by}
ORDER BY total_cost DESC
""", (f"-{days} days",)).fetchall()
result = []
for r in rows:
result.append({
group_by: r[group_by],
"calls": r["total_calls"],
"input_tokens": r["total_input"],
"output_tokens": r["total_output"],
"cost_usd": round(r["total_cost"], 4),
})
grand_total = sum(r["cost_usd"] for r in result)
# Also get per-agent cost from sources table (extraction costs)
agent_costs = conn.execute("""
SELECT p.agent,
COUNT(DISTINCT s.path) as sources,
SUM(s.cost_usd) as extraction_cost,
SUM(s.claims_count) as claims
FROM sources s
LEFT JOIN prs p ON p.source_path = s.path
WHERE s.cost_usd > 0
GROUP BY p.agent
ORDER BY extraction_cost DESC
""").fetchall()
agent_breakdown = []
for r in agent_costs:
agent_breakdown.append({
"agent": r["agent"] or "unlinked",
"sources": r["sources"],
"extraction_cost": round(r["extraction_cost"], 2),
"claims": r["claims"],
"cost_per_claim": round(r["extraction_cost"] / r["claims"], 4) if r["claims"] else 0,
})
return web.json_response({
"period_days": days,
"grand_total": round(grand_total, 2),
"by_" + group_by: result,
"by_agent": agent_breakdown,
})
finally:
conn.close()
async def handle_api_source_detail(request):
"""GET /api/source/{path}
Full lifecycle of a single source: research session extraction claims eval outcomes.
"""
source_path = request.match_info["path"]
conn = _conn(request.app)
try:
# Try exact match first, fall back to suffix match (anchored)
source = conn.execute(
"SELECT * FROM sources WHERE path = ?",
(source_path,),
).fetchone()
if not source:
# Suffix match — anchor with / prefix to avoid substring hits
source = conn.execute(
"SELECT * FROM sources WHERE path LIKE ? ORDER BY length(path) LIMIT 1",
(f"%/{source_path}",),
).fetchone()
if not source:
return web.json_response({"error": "Source not found"}, status=404)
result = dict(source)
# Get research session if linked
if source["session_id"]:
session = conn.execute(
"SELECT * FROM research_sessions WHERE id = ?",
(source["session_id"],),
).fetchone()
result["research_session"] = dict(session) if session else None
else:
result["research_session"] = None
# Get PRs from this source
prs = conn.execute(
"SELECT number, status, domain, agent, tier, leo_verdict, domain_verdict, "
"cost_usd, created_at, merged_at, commit_type, transient_retries, substantive_retries, last_error "
"FROM prs WHERE source_path = ?",
(source["path"],),
).fetchall()
result["prs"] = [dict(p) for p in prs]
# Get eval events from audit_log for those PRs
# NOTE: audit_log.detail is mixed — some rows are JSON (evaluate events),
# some are plain text. Use json_valid() to filter safely.
pr_numbers = [p["number"] for p in prs]
if pr_numbers:
placeholders = ",".join("?" * len(pr_numbers))
evals = conn.execute(f"""
SELECT * FROM audit_log
WHERE stage = 'evaluate'
AND json_valid(detail)
AND json_extract(detail, '$.pr') IN ({placeholders})
ORDER BY timestamp
""", pr_numbers).fetchall()
result["eval_history"] = [
{"timestamp": e["timestamp"], "event": e["event"],
"detail": json.loads(e["detail"]) if e["detail"] else None}
for e in evals
]
else:
result["eval_history"] = []
return web.json_response(result)
finally:
conn.close()
def setup_research_routes(app):
"""Register research tracking routes. Call from create_app()."""
app.router.add_get("/api/research-sessions", handle_api_research_sessions)
app.router.add_get("/api/costs", handle_api_costs)
app.router.add_get("/api/source/{path:.+}", handle_api_source_detail)
# Public paths to add to auth middleware
RESEARCH_PUBLIC_PATHS = frozenset({
"/api/research-sessions",
"/api/costs",
})
# /api/source/{path} needs prefix matching — add to auth middleware:
# if path.startswith("/api/source/"): allow

View file

@ -140,7 +140,7 @@ async def fetch_review_queue(
if forgejo_token:
headers["Authorization"] = f"token {forgejo_token}"
connector = aiohttp.TCPConnector() # Default SSL verification — Forgejo token must not be exposed to MITM
connector = aiohttp.TCPConnector(ssl=False)
async with aiohttp.ClientSession(headers=headers, connector=connector) as session:
# Fetch open PRs
url = f"{FORGEJO_BASE}/repos/{REPO}/pulls?state=open&limit=50&sort=oldest"

View file

@ -9,10 +9,8 @@ PAGES = [
{"path": "/prs", "label": "PRs", "icon": "&#9998;"},
{"path": "/ops", "label": "Operations", "icon": "&#9881;"},
{"path": "/health", "label": "Knowledge Health", "icon": "&#9829;"},
{"path": "/kb-proposals", "label": "KB Proposals", "icon": "&#9874;"},
{"path": "/agents", "label": "Agents", "icon": "&#9733;"},
{"path": "/epistemic", "label": "Epistemic", "icon": "&#9878;"},
{"path": "/portfolio", "label": "Portfolio", "icon": "&#9733;"},
]

View file

@ -1,30 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
cd /app
python -m compileall -q lib telegram diagnostics teleo-pipeline.py
python -m pytest -q \
tests/test_agent_routing.py \
tests/test_decision_engine_replay.py \
tests/test_phase1b_end_to_end.py \
tests/test_research_eval_schema_sql.py \
tests/test_teleo_agent_systemd.py
python - <<'PY'
import json
from lib.agent_routing import classify_pr_route
route = classify_pr_route(
"diff --git a/domains/internet-finance/x402-wallets.md b/domains/internet-finance/x402-wallets.md\n"
"+AI systems route agents around x402 payments and agent wallets.\n"
)
print(json.dumps({
"smoke": "teleo-gcp-staging-ok",
"primary_agent": route.primary_agent,
"required_agents": list(route.required_agents),
"route_kind": route.route_kind,
}, sort_keys=True))
PY

View file

@ -1,54 +0,0 @@
{
"status": "blocked_remote_execution",
"scope": "crabbox remote proof",
"attempted_discovery": [
"verified Crabbox CLI is installed at /Users/user/.local/bin/crabbox",
"ran crabbox job list",
"ran crabbox sync-plan",
"ran crabbox job run --dry-run unit",
"ran crabbox job run --dry-run phase1b-local-proof",
"checked presence of CRABBOX_COORDINATOR, CRABBOX_COORDINATOR_TOKEN, HCLOUD_TOKEN, HETZNER_TOKEN, GH_TOKEN, and GITHUB_TOKEN without printing values",
"loaded retained Bitwarden session from /tmp/bw_session without printing the session value",
"ran bw status and bw sync",
"checked Bitwarden organization, collection, and item counts",
"checked visible Bitwarden item names and metadata only",
"scanned visible Bitwarden item names and notes for crabbox, hcloud, hetzner, and coordinator terms without printing note or secret values"
],
"exact_blocker": "Crabbox provider execution still lacks a real provider credential: HCLOUD_TOKEN, HETZNER_TOKEN, CRABBOX_COORDINATOR, and CRABBOX_COORDINATOR_TOKEN are unset, and the visible Bitwarden org collection contains only Anthropic API Key, Leo twitter, and LivingIPbot Github, with no Crabbox, HCloud, Hetzner, or coordinator metadata match.",
"why_it_cannot_be_solved_autonomously": "A remote Crabbox lease requires a real Hetzner or Crabbox broker credential. The repo can safely commit CI/CD config, dry-run plans, and blocker artifacts, but it cannot fabricate the provider credential or commit secret values.",
"exact_next_action": "Add a scoped Hetzner/Crabbox broker credential to Bitwarden or GitHub environment secrets as HCLOUD_TOKEN, HETZNER_TOKEN, CRABBOX_COORDINATOR, or CRABBOX_COORDINATOR_TOKEN, then rerun crabbox doctor --json and crabbox job run phase1b-local-proof from teleo-infrastructure.",
"safe_local_status": {
"crabbox_cli_installed": "0.22.1",
"job_list": "passes",
"sync_plan": "217 files, 2.4 MiB",
"unit_dry_run": "passes",
"phase1b_proof_dry_run": "passes",
"ci_contract_guard": "passes",
"phase1b_proof_wrapper": "131 passed, 8 proof cases succeeded, all six agents seen",
"full_pytest": "422 passed",
"crabbox_doctor": "fails only provider credential check: HCLOUD_TOKEN or HETZNER_TOKEN is required",
"bitwarden_status": "unlocked",
"bitwarden_organizations": 1,
"bitwarden_collections": 1,
"bitwarden_items_visible": 3,
"bitwarden_matching_crabbox_or_hetzner_items": 0
},
"secret_commit_policy": {
"allowed_to_commit": [
"workflow files",
"Crabbox config with secret slot names omitted",
"proof scripts",
"machine-readable blocker artifacts",
"docs and agent skills"
],
"not_allowed_to_commit": [
"Bitwarden item values",
"Bitwarden vault exports",
"provider tokens",
"GitHub bot tokens",
"OpenRouter keys",
"SSH private keys",
"production databases"
]
}
}

View file

@ -1,96 +0,0 @@
# Crabbox Remote Proof
Crabbox is the remote execution layer for `teleo-infrastructure`. It is not the production deploy system.
## Goals
- Run Python tests on a disposable or warm remote Linux box.
- Prove the CI/Crabbox contract without network access before remote runs.
- Run the Phase 1B local proof script remotely.
- Retain JUnit and machine-readable proof artifacts.
- Give agents a bounded job list instead of arbitrary cloud shell access.
## Non-Goals
- No production deploys.
- No production secrets.
- No production VPS mutation.
- No production `decision-engine` PR comments from Crabbox jobs.
## Required Local Setup
Crabbox CLI 0.22.1 or newer:
```bash
crabbox --version
```
One of:
```bash
crabbox login --url "$CRABBOX_COORDINATOR"
```
or direct Hetzner operator env:
```bash
export HCLOUD_TOKEN="..."
```
Do not commit either value.
## Jobs
```bash
crabbox job list
crabbox job run --dry-run ci-contract
crabbox job run --dry-run unit
crabbox job run --dry-run phase1b-local-proof
crabbox job run ci-contract
crabbox job run unit
crabbox job run phase1b-local-proof
```
`ci-contract` writes:
- `.crabbox-results/crabbox-ci-contract.json`
`phase1b-local-proof` writes:
- `.crabbox-results/crabbox-ci-contract.json`
- `proof/phase1b-local-e2e-proof.json`
- `.crabbox-results/phase1b-pytest.xml`
- `.crabbox-results/phase1b-proof-summary.json`
The contract proof checks that:
- Crabbox exposes only the named bounded jobs.
- sync excludes secret/runtime files such as `.env`, `secrets`, DBs, logs, caches, and virtualenvs.
- `.crabbox.yaml` contains no token-bearing env names.
- Leo routes are explicit: Leo-owned domains, fallback routes, and top-2 cross-domain routes that include Leo are covered, while Phase 1B does not silently preserve Leo as a universal second reviewer.
## Secret Boundary
Allowed:
- `CI`
- `PYTHONWARNINGS`
- `PHASE1B_AGENT_ROUTING_ENABLED`
- broker token in user config
- direct `HCLOUD_TOKEN` or `HETZNER_TOKEN` in local operator env
- GitHub environment secrets named `HCLOUD_TOKEN` or `HETZNER_TOKEN` for an explicitly dispatched remote proof workflow
Not allowed:
- production GitHub admin token
- production Forgejo token
- production OpenRouter key
- production SSH keys
- Bitwarden exports
- prod `pipeline.db`
Bitwarden may be used as the human/operator source of truth for secret lookup and GitHub secret setup, but no Bitwarden item value, vault export, or copied secret belongs in this repo. The committed config may name required secret slots; it must not contain the values.
## Proof Boundary
Crabbox remote proof proves repo behavior on a remote Linux lease. It does not prove production parity unless the lease recreates the production runtime paths, systemd services, timers, DB path, and deploy script behavior.

View file

@ -1,284 +0,0 @@
# GCP CI/CD and Redundancy Hardening
This records the current GCP hardening contract for `teleo-501523`.
## Current-State Rule
The latest retained `gcp-readiness` artifact is authoritative for what is live
right now. The resource lists below define the target runtime contract and the
checks this lane enforces; do not treat them as current production proof unless
the current readiness run passes.
## Target Runtime Contract
- Artifact Registry Docker repositories exist in `europe-west6`:
- `teleo`
- `livingip-web`
- Both repositories use immutable tags and active vulnerability scanning.
- `cloudbuild.gcp-staging.yaml` builds the staging Teleo image, runs the image smoke test, then pushes to Artifact Registry.
- Cloud Build runs as the dedicated service account:
- `sa-teleo-cloudbuild@teleo-501523.iam.gserviceaccount.com`
- The dedicated Cloud Build account has only the roles required for the current build/publish path:
- `roles/artifactregistry.writer`
- `roles/logging.logWriter`
- `roles/storage.objectViewer`
- GitHub Actions can publish Artifact Registry images through Workload Identity Federation:
- workflow: `.github/workflows/gcp-artifact.yml`
- provider: `projects/785938879453/locations/global/workloadIdentityPools/github-actions/providers/living-ip-github`
- service account: `sa-artifact-builder@teleo-501523.iam.gserviceaccount.com`
- repository scope: `living-ip/teleo-infrastructure`
- Backup buckets are versioned and use uniform bucket-level access:
- `gs://teleo-501523-prod-backups`
- `gs://teleo-501523-leoclean-backups`
- VM boot disks have a daily 7-day snapshot policy:
- `teleo-prod-1`
- `teleo-staging-1`
- Cloud SQL standby target exists for KB restore/replication drills:
- instance: `teleo-pgvector-standby`
- database: `teleo_kb`
- version: `POSTGRES_16`
- private IP only on `teleo-staging-net`
- encrypted-only SQL connections
- automated backups and point-in-time recovery enabled
- deletion protection enabled
- Source-side Teleo DB/KB export canary exists:
- script: `ops/backup_vps_sqlite_kb.sh`
- source DB: `/opt/teleo-eval/pipeline/pipeline.db`
- source Leo/KB files: `workspaces/*/agents/leo` plus `agent-state`
- excludes secrets and logs
- Local SQLite-to-Postgres restore canary exists:
- scripts: `ops/sqlite_to_postgres_dump.py` and `ops/run_sqlite_postgres_restore_canary.sh`
- target: disposable local `postgres:16-alpine` shadow schema
- verifies source SQLite integrity and per-table source/target row-count parity
## How To Build
Automatic Artifact Registry publishing runs on pushes to `main` through GitHub Actions. To run the same lane manually from GitHub:
```bash
gh workflow run gcp-artifact.yml --repo living-ip/teleo-infrastructure --ref main
```
The workflow authenticates to GCP with Workload Identity Federation, builds `Dockerfile.gcp-staging`, runs the image smoke test, pushes the image, and uploads `gcp-artifact-image.txt` as a run artifact.
For a read-only GCP posture probe through the same Workload Identity path:
```bash
gh workflow run gcp-readiness.yml --repo living-ip/teleo-infrastructure --ref main
```
This workflow runs `ops/check_gcp_infra_readiness.py` from GitHub Actions and
uploads stdout, stderr, exit code, and a summary as the `gcp-readiness` artifact.
It is intentionally non-mutating and defaults to
`sa-teleo-readiness@teleo-501523.iam.gserviceaccount.com`.
If you need to test a specific service account during IAM repair, pass it
explicitly:
```bash
gh workflow run gcp-readiness.yml \
--repo living-ip/teleo-infrastructure \
--ref main \
-f service_account=sa-teleo-readiness@teleo-501523.iam.gserviceaccount.com
```
If you also want GitHub readiness to include a local SQLite-to-Postgres restore
canary proof without uploading private backup paths or generated SQL, pass a
redacted capsule:
```bash
python3 ops/redact_sqlite_postgres_restore_canary.py \
--proof outputs/gcp-infra-hardening-20260707/proofs/sqlite-postgres-restore-canary-<timestamp>.json \
--output outputs/gcp-infra-hardening-20260707/proofs/sqlite-postgres-restore-canary-capsule-<timestamp>.json
CAPSULE_B64="$(base64 < outputs/gcp-infra-hardening-20260707/proofs/sqlite-postgres-restore-canary-capsule-<timestamp>.json | tr -d '\n')"
gh workflow run gcp-readiness.yml \
--repo living-ip/teleo-infrastructure \
--ref main \
-f restore_canary_capsule_b64="${CAPSULE_B64}"
```
This only upgrades the local restore-preflight row. It is not GCP DB redundancy
until the Cloud SQL import and target-count readback also pass.
For a local/manual Cloud Build proof:
```bash
REVISION="$(git rev-parse HEAD)"
TAG="$(git rev-parse --short=7 HEAD)-manual-$(date -u +%Y%m%d-%H%M%S)"
gcloud builds submit \
--project=teleo-501523 \
--config=cloudbuild.gcp-staging.yaml \
--substitutions="_TAG=${TAG},_REVISION=${REVISION}"
```
Expected result:
- `build-staging-image` succeeds.
- `smoke-test-image-before-push` succeeds.
- A Docker image is pushed to:
`europe-west6-docker.pkg.dev/teleo-501523/teleo/teleo-pipeline-gcp-staging:${TAG}`
## How To Check Readiness
Run from the repository root:
```bash
python3 ops/check_gcp_infra_readiness.py
```
The check is read-only and prints no secret values. It verifies:
- Artifact Registry immutability and vulnerability scanning.
- Cloud Build config contract.
- Dedicated Cloud Build service account and roles.
- GitHub Actions WIF Artifact Registry publishing contract.
- Network ingress posture:
- no enabled broad SSH/RDP ingress;
- Teleo SSH rules are scoped to `/32` source ranges and target tags.
- Runtime service-account posture for the prod/staging VMs.
- Compute disk snapshot policy attachment.
- Backup bucket versioning and uniform access.
- Cloud SQL standby target posture.
- Source SQLite/KB backup/export repeatability.
- Whether an approved source KB/Postgres dump or replication credential exists.
- Whether source data has actually been restored or replicated into GCP and queried.
- Whether the GitHub WIF readiness workflow exists for non-local readback.
## Current Boundaries
The GCP Docker build/publish path is live through manual Cloud Build and GitHub Actions Workload Identity Federation. Native Cloud Build GitHub triggers are not configured because this project currently has no Cloud Build repository connection.
Database redundancy is not complete. The current project now has a GCP Cloud SQL/Postgres standby target, backup buckets, VM disk snapshots, and a repeatable source SQLite/KB export script. It does not yet have source-data restore or replication into GCP. Do not claim DB parity until one of these is true:
- the existing canonical KB database is replicated into GCP and read back; or
- GCP Cloud SQL/Postgres becomes the canonical database and production services read/write it; or
- an explicitly approved standby restore drill proves that a GCP database can be restored and queried from the retained backups.
Do not call the empty `teleo-pgvector-standby` instance redundancy by itself. It only counts after source data, restore/replication, access controls, and query readback are proven.
The local restore canary narrows the remaining gap: the source SQLite backup can
be converted and restored into PostgreSQL with table/row-count parity, but the
same import still needs to run against the GCP Cloud SQL standby through an
approved GCP auth and network path.
After Cloud SQL import, use the generated `target-counts.sql` and verify it with:
```bash
python3 ops/verify_gcp_cloudsql_restore_readback.py \
--drill-proof outputs/gcp-infra-hardening-20260707/proofs/gcp-cloudsql-restore-drill-<timestamp>.json \
--target-counts-csv outputs/gcp-infra-hardening-20260707/proofs/gcp-cloudsql-target-counts-<timestamp>.csv \
--output outputs/gcp-infra-hardening-20260707/proofs/gcp-cloudsql-restore-readback-verification-<timestamp>.json
```
The verifier must return `status = pass` before claiming row-count parity in GCP.
## IAM Split Plan
Do not make `sa-artifact-builder@teleo-501523.iam.gserviceaccount.com` the broad
infra account. Keep it scoped to Docker image publishing.
Use the generated plan for the next privilege boundary:
```bash
python3 ops/plan_gcp_iam_split.py --format json
python3 ops/plan_gcp_iam_split.py --format shell
```
For an idempotent retained apply attempt, use:
```bash
python3 ops/apply_gcp_iam_split.py \
--output outputs/gcp-infra-hardening-20260707/proofs/gcp-iam-split-apply-dry-run.json
python3 ops/apply_gcp_iam_split.py \
--execute \
--output outputs/gcp-infra-hardening-20260707/proofs/gcp-iam-split-apply-execute.json
```
The first command is dry-run only. The second command mutates IAM and must run
from an authenticated GCP admin shell. It is safe to re-run: existing service
accounts are skipped, and IAM binding commands are additive/idempotent.
The plan creates two separate accounts:
- `sa-teleo-readiness@teleo-501523.iam.gserviceaccount.com`
- GitHub WIF account for read-only readiness checks.
- Needs read-only roles for Artifact Registry, IAM/service-account/WIF
metadata, Compute/network posture, Cloud SQL metadata, backup buckets, and
Secret Manager metadata.
- `sa-teleo-restore-drill@teleo-501523.iam.gserviceaccount.com`
- Operator account for explicit Cloud SQL restore drills.
- Needs Cloud SQL edit rights for the import operation and object-admin access
to the restore bucket path.
Cloud SQL imports also require the Cloud SQL instance service account to read the
GCS object. The plan includes a command that discovers that instance service
account and grants it `roles/storage.objectAdmin` on
`gs://teleo-501523-prod-backups`.
This plan is not itself a completed redundancy proof. DB redundancy is complete
only after the restore drill imports source data into Cloud SQL and the retained
`target-counts.sql` readback matches the source/local restore proof.
## Runtime Baseline Runner
Do not create or repair the GCP runtime baseline manually in the console if an
audited runner can do it. The runtime baseline runner is dry-run by default:
```bash
python3 ops/apply_gcp_runtime_baseline.py \
--admin-ssh-cidr <operator-ip>/32 \
--output outputs/gcp-infra-hardening-20260707/proofs/gcp-runtime-baseline-dry-run.json
```
The dry-run proof records the exact service accounts, network, firewall, VM,
snapshot, backup bucket, secret, and Cloud SQL operations needed for the
readiness checker. It does not prove that those resources exist.
To apply after an authenticated GCP admin session is available:
```bash
export TELEO_CLOUDSQL_POSTGRES_PASSWORD='<store locally; do not commit or print>'
python3 ops/apply_gcp_runtime_baseline.py \
--execute \
--admin-ssh-cidr <operator-ip>/32 \
--output outputs/gcp-infra-hardening-20260707/proofs/gcp-runtime-baseline-execute.json
```
`--execute` refuses to run without a single trusted IPv4 `/32` SSH CIDR. The
Cloud SQL password is passed through the environment and redacted from retained
operation commands. After execute mode succeeds, rerun `gcp-readiness.yml` with
the dedicated readiness service account and then run the Cloud SQL restore
drill/readback verifier before claiming database redundancy.
## Communication Posture
The service-to-service communication contract is declared in:
- `config/gcp-service-communications.json`
Validate it locally or in CI with:
```bash
python3 ops/check_gcp_service_communications.py \
--contract config/gcp-service-communications.json \
--output outputs/gcp-infra-hardening-20260707/proofs/gcp-service-communications-check.json
```
The contract currently requires:
- GitHub Actions artifact publishing only through `sa-artifact-builder`.
- GitHub Actions readiness only through `sa-teleo-readiness`.
- emergency SSH only from one operator IPv4 `/32` to Teleo target tags.
- VM image pulls over Private Google Access.
- Cloud SQL only on private VPC paths with encrypted-only PostgreSQL.
- Cloud SQL imports only from the versioned backup bucket through approved
service accounts.
- no broad SSH/RDP, no public database IP, no default Compute Engine service
accounts, and no raw secret values in the contract.
This is still a contract until GCP readiness passes. Live proof requires the
current `gcp-readiness` artifact to show the matching firewall, VM service
accounts, bucket, and Cloud SQL checks passing.

View file

@ -1,322 +0,0 @@
# KB Restore / Replication Runbook
This runbook is for proving Living IP KB/database redundancy on GCP.
## Two Different Database Surfaces
Do not call the SQLite shadow restore a canonical Leo database copy.
Canonical Leo knowledge is currently:
- host: VPS `77.42.65.182`;
- container: `teleo-pg`;
- engine/database: PostgreSQL 16, database `teleo`;
- canonical schemas: `public` and `kb_stage`;
- high-signal rows: claims, sources, claim evidence, claim edges, reasoning
tools, and review-gated proposals.
The older pipeline/evaluation database is a separate surface:
- pipeline runtime DB: `/opt/teleo-eval/pipeline/pipeline.db`
- engine: SQLite WAL
- related Leo files:
- `/opt/teleo-eval/workspaces/main/agents/leo`
- `/opt/teleo-eval/workspaces/research-leo/agents/leo`
- `/opt/teleo-eval/agent-state`
`ops/run_gcp_cloudsql_restore_drill.sh` remains a legacy SQLite-to-Postgres
shadow-schema drill. It reconstructs `teleo_restore` inside `teleo_kb`; it does
not preserve the canonical Postgres schema, constraints, indexes, functions,
roles, or row hashes.
The last authenticated control-plane readback on 2026-07-10 reported this
candidate GCP target; refresh it before any mutation:
- project: `teleo-501523`
- instance: `teleo-pgvector-standby`
- database: `teleo_kb`
- region: `europe-west6`
- network: `teleo-staging-net`
- private IP: `10.61.0.3`
- admin password secret: `gcp-teleo-pgvector-standby-postgres-password`
Do not call this redundancy complete until source data has been restored or replicated and queried from GCP.
## Canonical Postgres Snapshot And Parity
Capture a custom-format dump and a full JSONL manifest from the same exported,
read-only PostgreSQL snapshot:
```bash
python3 ops/capture_vps_canonical_postgres_snapshot.py \
--execute \
--ssh-target root@77.42.65.182 \
--ssh-key ~/.ssh/livingip_hetzner_20260604_ed25519 \
--run-id canonical-<timestamp> \
--output-dir <private-output-dir>
```
The capture fails closed if the source service changes while it runs. It
retains a private custom dump, dump SHA-256, object TOC, catalog/data manifest,
and before/after service state. It never restarts Leo or writes to the source
database.
Prove that this exact snapshot can rebuild a blank Postgres target before using
it for GCP:
```bash
.venv/bin/python ops/run_local_canonical_postgres_rebuild.py \
--dump <private-output-dir>/teleo-canonical.dump \
--source-manifest <private-output-dir>/source-manifest.jsonl \
--output /tmp/teleo-canonical-rebuild-receipt.json
```
The local runner starts a uniquely named `postgres:16-alpine` container with
network mode `none` and tmpfs-only database storage. It waits for an actual
`psql` connection to the named database, restores with `pg_restore
--no-owner --no-privileges --exit-on-error`, compares the full parity manifest,
then removes the container and proves it is absent. A passing local receipt is
the exact-recovery preflight; it is not semantic recompilation from raw source
documents.
Run `ops/postgres_parity_manifest.sql` against the isolated restored target,
then compare source and target:
```bash
python3 ops/verify_postgres_parity_manifest.py \
--source <private-output-dir>/source-manifest.jsonl \
--target <private-output-dir>/target-manifest.jsonl \
--scope gcp_staging \
--connectivity-proof <private-output-dir>/gcp-private-connectivity.json \
--output <private-output-dir>/gcp-parity.json
```
The verifier checks all table row counts and collation-independent row hashes,
plus schemas, columns/defaults, constraints, indexes, sequences, views,
functions, triggers, enum/domain types, policies, required extensions,
password-free application-role attributes, and bounded query timings. In GCP
scope it also requires a receipt proving a staging compute source, a private
server address, TLS, and public-IP-disabled instance metadata.
Use a generated target database such as `teleo_clone_<run_id>`. Never import a
drill into `teleo`, `teleo_kb`, or `teleo_canonical`. Database isolation does
not isolate cluster-global roles or extensions, so verify those separately and
do not run the Docker-only gate bootstrap against the shared Cloud SQL instance.
After the parity verifier passes, run the no-send operator composition replay from
staging compute against that generated database. Only then delete the generated
database and uploaded import object and retain cleanup readback.
## Legacy SQLite Source Backup Canary
Create a consistent source backup without stopping the VPS service:
```bash
ops/backup_vps_sqlite_kb.sh
```
The script:
- uses SQLite `.backup` against `/opt/teleo-eval/pipeline/pipeline.db`;
- compresses and hashes the backup on the VPS;
- archives Leo/KB files while excluding `secrets` and logs;
- copies both artifacts locally;
- verifies SHA-256 matches;
- runs `PRAGMA integrity_check` on a local restored SQLite copy;
- records proof under `outputs/gcp-infra-hardening-20260707/proofs/`.
This proves source exportability and local restore integrity. It does not prove GCP DB redundancy until a GCP restore/import/query canary also passes.
## Legacy SQLite-To-Postgres Restore Canary
Before importing into Cloud SQL, prove that the current SQLite backup can be
converted and restored into PostgreSQL without row loss:
```bash
SQLITE_BACKUP=./outputs/gcp-infra-hardening-20260707/private-backups/teleo-pipeline-sqlite-<timestamp>.db.gz \
ops/run_sqlite_postgres_restore_canary.sh
```
The canary:
- generates a PostgreSQL import script with `ops/sqlite_to_postgres_dump.py`;
- recreates a shadow schema in a disposable `postgres:16-alpine` container;
- imports all user tables from the SQLite backup;
- compares source and target row counts for every table;
- writes a proof JSON under `outputs/gcp-infra-hardening-20260707/proofs/`;
- removes only its temporary canary container.
This is a local restore/parity proof, not GCP redundancy by itself. It is the
preflight that should pass before the same generated import is applied through
the approved Cloud SQL connector/VPC path.
To pass this local preflight into a clean GitHub readiness run without uploading
private backup paths, generated SQL, or target-count CSVs, create a redacted
capsule from the proof:
```bash
python3 ops/redact_sqlite_postgres_restore_canary.py \
--proof outputs/gcp-infra-hardening-20260707/proofs/sqlite-postgres-restore-canary-<timestamp>.json \
--output outputs/gcp-infra-hardening-20260707/proofs/sqlite-postgres-restore-canary-capsule-<timestamp>.json
```
The capsule keeps only non-secret evidence: proof hash, backup hash, source and
target table/row counts, conversion notes/stats, and the redacted-field list.
It does not prove that Cloud SQL imported the data; it only proves the local
SQLite-to-Postgres parity preflight.
To include the capsule in GitHub readiness:
```bash
CAPSULE_B64="$(base64 < outputs/gcp-infra-hardening-20260707/proofs/sqlite-postgres-restore-canary-capsule-<timestamp>.json | tr -d '\n')"
gh workflow run gcp-readiness.yml \
--repo living-ip/teleo-infrastructure \
--ref main \
-f restore_canary_capsule_b64="${CAPSULE_B64}"
```
## Legacy SQLite Cloud SQL Restore Drill Runner
Prepare the exact GCS import and Cloud SQL import operation without mutating GCP:
```bash
SQLITE_BACKUP=./outputs/gcp-infra-hardening-20260707/private-backups/teleo-pipeline-sqlite-<timestamp>.db.gz \
ops/run_gcp_cloudsql_restore_drill.sh
```
Execute it only from an authenticated operator environment that can write the
versioned backup bucket and administer the standby Cloud SQL instance:
```bash
EXECUTE=1 \
SQLITE_BACKUP=./outputs/gcp-infra-hardening-20260707/private-backups/teleo-pipeline-sqlite-<timestamp>.db.gz \
ops/run_gcp_cloudsql_restore_drill.sh
```
The runner:
- regenerates the explicit PostgreSQL import script;
- targets the shadow schema `teleo_restore` inside `teleo_kb`;
- uploads the import script to `gs://teleo-501523-prod-backups/kb-dumps/cloudsql-restore-drills/...` when `EXECUTE=1`;
- starts and waits for `gcloud sql import sql`;
- writes `target-counts.sql` for the required trusted VPC/Cloud SQL connector query readback.
The import operation alone is still not the final proof. The final proof needs
`target-counts.sql` run against `teleo-pgvector-standby` and compared to the
source counts in the drill proof.
After the import operation is `DONE`, run the generated count query from a
trusted VPC runtime or Cloud SQL connector path and retain CSV output:
```bash
psql "$TELEO_CLOUDSQL_DATABASE_URL" \
--csv \
-f outputs/gcp-infra-hardening-20260707/private-cloudsql-restore-drills/gcp-cloudsql-restore-drill-<timestamp>/target-counts.sql \
> outputs/gcp-infra-hardening-20260707/proofs/gcp-cloudsql-target-counts-<timestamp>.csv
```
Then compare the Cloud SQL readback to the source proof:
```bash
python3 ops/verify_gcp_cloudsql_restore_readback.py \
--drill-proof outputs/gcp-infra-hardening-20260707/proofs/gcp-cloudsql-restore-drill-<timestamp>.json \
--target-counts-csv outputs/gcp-infra-hardening-20260707/proofs/gcp-cloudsql-target-counts-<timestamp>.csv \
--output outputs/gcp-infra-hardening-20260707/proofs/gcp-cloudsql-restore-readback-verification-<timestamp>.json
```
Only a `status = pass` verifier output is enough for row-count parity. It still
does not prove application cutover or continuous replication.
## Required Proof
A successful restore or replication canary must retain:
- source dataset identity:
- source host or dump artifact;
- dump timestamp or replication slot timestamp;
- source schema/database name.
- transfer proof:
- dump object path in a versioned bucket, or logical replication subscription details;
- row/table counts before import where available.
- target proof:
- `teleo-pgvector-standby` readback;
- `teleo_kb` database readback;
- extension readback for `vector` if the restored schema needs pgvector;
- representative query readback for core KB tables.
- failure boundary:
- exact missing secret, source access, schema incompatibility, extension issue, or import error.
## One-Shot SQLite Export / GCP Restore Path
Use this while the canonical DB remains SQLite on the VPS and we need a GCP restore drill.
1. Run `ops/backup_vps_sqlite_kb.sh`.
2. Upload the resulting SQLite backup and Leo/KB tarball to a versioned GCS bucket such as `gs://teleo-501523-prod-backups/kb-dumps/`.
3. Run the local SQLite-to-Postgres restore canary above and retain its proof.
4. Run `ops/run_gcp_cloudsql_restore_drill.sh` in dry-run mode to generate the GCS import plan.
5. Run `EXECUTE=1 ops/run_gcp_cloudsql_restore_drill.sh` from an authenticated operator environment to upload and import the generated SQL. Do not run blind string rewrites against the SQLite dump.
6. Install required extensions on Cloud SQL:
```sql
create extension if not exists vector;
```
7. From a trusted VPC runtime or Cloud SQL connector path, run readbacks:
```sql
select current_database();
select extname, extversion from pg_extension where extname = 'vector';
select schemaname, tablename from pg_tables where schemaname not in ('pg_catalog', 'information_schema') order by 1, 2 limit 50;
```
8. Retain the SQLite backup hash, GCS object generation, import/conversion operation, query output, and row-count sample.
9. Run `ops/verify_gcp_cloudsql_restore_readback.py` and retain a passing parity proof.
## Logical Replication Path
Use this only if the canonical source becomes Postgres or a Postgres mirror exists. SQLite cannot be logically replicated into Cloud SQL Postgres without an intermediate conversion/sync layer.
Required source privileges:
- replication-capable source user;
- publication over the intended schemas/tables;
- network path from GCP to source, or source-to-GCP path through an approved proxy/tunnel.
Required target steps:
```sql
create extension if not exists vector;
create subscription <subscription_name>
connection '<redacted source connection string>'
publication <publication_name>;
```
Retain only redacted connection metadata. Do not commit or paste credentials.
## Current Blocker
As of 2026-07-11, the canonical Postgres exported-snapshot capture and isolated
local restore parity pass. Live GCP restore and staging replay do not.
- GitHub WIF works for `sa-artifact-builder`, but that identity is intentionally
limited to Artifact Registry and cannot inspect or mutate Cloud SQL/Compute.
- The configured `sa-teleo-readiness` and `sa-teleo-restore-drill` identities
return IAM 404 and do not exist.
- The local privileged `billy@livingip.xyz` gcloud session requires password
reauthentication. No password was entered or inspected.
- Direct VM SSH is closed to the current egress `/32`; IAP requires the same
privileged GCP authentication.
That is why the readiness checker still reports:
- `kb_source_restore_access = blocked`
- `kb_restore_or_replication = blocked`
The immediate operator CTA is to complete
`gcloud auth login billy@livingip.xyz --force` locally without sharing the
password, or apply the reviewed IAM split with an authorized GCP administrator.
The next non-user action is:
canonical `teleo` snapshot -> generated Cloud SQL database -> full parity and
private-connectivity verifier -> no-send Cory composition replay from staging
compute -> delete the generated database/object -> retain cleanup proof.

View file

@ -1,37 +0,0 @@
# GCP Operator Reauthentication
From the `teleo-infrastructure` repository root, inspect the current local
operator state without opening a dialog:
```bash
scripts/gcp-operator-reauth.sh --status
```
Store or update the current Google password through a native secure prompt:
```bash
scripts/gcp-operator-reauth.sh --store-password
```
With no option, the script asks before opening that prompt. It uses
`pinentry-mac` when installed. Otherwise it compiles a temporary AppKit helper
that uses `NSAlert` and `NSSecureTextField`. The helper stores the value as a
non-synchronizing generic password in this Mac's encrypted Keychain. The
password is never printed, copied to the clipboard, submitted to Google, or
placed in a process argument or plaintext file.
The stored password is only an emergency operator convenience. A Google
password cannot refresh or satisfy gcloud OAuth by itself, and this script does
not attempt to automate Google login. When OAuth is stale, follow the one
`clear_CTA` printed by the script and rerun `--status`.
`--status` verifies the selected account and project, checks token refresh
without printing the token, reads the expected VM identity, and asks gcloud to
construct the IAP SSH command with `--dry-run`. It does not create a tunnel or
start SSH. `iap_ssh_preflight=ready_dry_run_only` must not be reported as a live
IAP connection.
The durable unattended operator is
`.github/workflows/gcp-iap-operator.yml`. It uses GitHub OIDC, Workload Identity
Federation, fixed reviewed operations, and IAP after the one-time authenticated
bootstrap. No stored Google password is an alternative to that route.

View file

@ -1,154 +0,0 @@
# Rebuilding Leo's Knowledge Database
## Outcome
Leo should improve by compiling durable source material and reviewed changes
into Postgres. Repeatedly changing prompts or retraining the chat behavior is
not the knowledge system.
There are two different rebuilds:
1. **Exact recovery** restores the current canonical database from a verified
snapshot. This is working now.
2. **Semantic recompilation** starts from the retained source corpus and the
reviewed change ledger, then reproduces the canonical rows. This is partly
recoverable but is not yet complete.
## Exact Recovery: Working
Run:
```bash
.venv/bin/python ops/run_local_canonical_postgres_rebuild.py \
--dump /private/path/teleo-canonical.dump \
--source-manifest /private/path/source-manifest.jsonl \
--output /tmp/teleo-canonical-rebuild-receipt.json
```
The 2026-07-13 live canary restored a fresh, network-isolated Postgres in 3.81
seconds and then removed it. The restored target matched the source across all
39 manifest tables and all 52,164 rows, with no schema, data, constraint, role,
or performance mismatch. Key rows included:
- 1,837 claims;
- 4,145 sources;
- 4,670 claim-evidence links;
- 4,916 claim edges;
- 17 reasoning tools;
- 26 proposals.
This is the fastest disaster-recovery path. It does not require Leo to
re-extract or relearn the corpus.
## Source Recompilation: Current Evidence
Read-only VPS inspection found two retained June import runs. Both point to the
Forgejo-era workspace at `/opt/teleo-eval/workspaces/main` and retained
inventory JSONL files under `/opt/teleo-eval/kb-import/`.
The database still retains:
- `kb_stage.import_runs` for the two inventories;
- staged claims, sources, claim-source links, and claim edges;
- `kb_stage.canonical_mappings` from legacy keys to canonical UUIDs;
- mappings for 1,807 of 1,837 canonical claims;
- mappings for all 4,145 canonical sources.
For the 1,807 mapped claims, current canonical type, text, status, confidence,
tags, and creator match the retained staged rows exactly. Creation timestamps
also follow a recoverable rule: use the legacy timestamp when present and the
mapping timestamp for the eight rows that had no legacy timestamp.
The simple retained-row joins currently reproduce:
- 4,254 of 4,670 canonical evidence links;
- 4,878 of 4,916 canonical edge rows can be accounted for by a staged relation;
historical duplicate multiplicity still needs an explicit replay rule.
Re-run this aggregate, read-only coverage audit against any restored local
clone:
```bash
.venv/bin/python ops/audit_kb_rebuild_coverage.py \
--container <restored-local-postgres-container> \
--database teleo
```
The auditor emits no claim bodies or source excerpts and separates snapshot
recovery from source-derived recompilation readiness.
The remaining gaps are concrete rather than mysterious:
- 30 claims were created after or outside the mapped import;
- 284 mapped source rows do not have a matching retained `staged_sources` row
and need the original source-synthesis rule or an explicit genesis record;
- 416 evidence links need source-synthesis or later-change provenance;
- 38 edge rows need later-change receipts or explicit replay records;
- old applied proposal rows do not describe every historical canonical write.
This proves that most of the initial database came from the retained file-KB
import path. It does not yet prove a clean blank-database recompile.
## Target Compiler
The durable rebuild model is:
```text
immutable source corpus + file hashes
-> deterministic inventory and classification
-> staged claims, sources, evidence links, and edges
-> stable canonical ID mapping
-> review decisions
-> append-only accepted apply payloads and receipts
-> canonical Postgres
-> render/sync/restart
-> answer benchmark
```
Use the current verified snapshot as **genesis epoch 1**. Preserve its dump,
manifest, source commit, inventory files, mappings, and aggregate rebuild
receipt. Every accepted change after that epoch must carry a replayable strict
apply payload and row-level postflight receipt. This prevents the historical
gap from growing while the old import rules are reconstructed.
The source compiler now turns one raw artifact, its strict UTF-8 extraction,
and a reviewed extraction manifest into a deterministic, hash-bound
`pending_review` proposal bundle:
```bash
.venv/bin/python scripts/compile_kb_source_packet.py \
--artifact fixtures/working-leo/document-ingestion-v1.json \
--text fixtures/working-leo/document-ingestion-v1.json \
--manifest fixtures/working-leo/source-compiler-manifest-v1.json \
--output /tmp/working-leo-source-packet-v1.json
```
The compiler verifies artifact and extraction hashes, stable source identity,
current schema taxonomies, unique logical keys, and exact claim/evidence quotes.
It reuses the existing proposal normalizer and staging preflight, but it has no
database connection and executes neither staging nor apply. Its output is the
review packet, not canonical knowledge.
The existing full-data clone canary separately proves that a reviewed packet
can create source, claim, evidence, and edge rows and affect later reasoning.
The next capability is to expose this compiler through Leo's bounded source
intake command, stage its output, and join accepted packets into a corpus runner
and replay ledger.
## Definition Of Working
Semantic recompilation is complete only when all of these pass:
1. A command creates a blank database from the retained source corpus plus the
reviewed ledger.
2. Schema, constraints, roles, table counts, row hashes, and key query results
match the canonical manifest.
3. Every canonical row traces to a genesis import record or a reviewed apply
receipt.
4. A new document can be hash-captured, extracted into grounded candidates,
deduplicated, staged, reviewed, applied in a disposable clone, and read back.
5. After render/sync and restart, Leo answers the related broad question using
the new rows and cites the source chain.
Until then, exact snapshot recovery is production recovery; source
recompilation is an active build capability, not a finished claim.

View file

@ -1,236 +0,0 @@
# LLM Refinement And Decision Engine Program
Created: 2026-06-01
Status: active direction
## Product Outcome
The decision engine should become the best judgment layer for Living IP: it routes knowledge changes to the right agent identities, tests competing LLMs against the same rubric, learns from disagreement, and improves prompts/tools only when measured deltas prove the change.
Pentagon.run should own disposable infrastructure and remote execution. This repo should own decision quality: rubrics, prompts, model selection, route evidence, database feedback loops, and agent tool packages.
## What Rio And Theseus Become
### Rio
Rio becomes the economic and incentive-quality evaluator.
Rio owns:
- contribution weights and role economics;
- paid-query effects and anti-pay-to-pollute rules;
- market, mechanism, futarchy, x402, token, and capital-formation reasoning;
- source-diversity and correlated-prior warnings;
- OPSEC for finance, deal terms, token economics, and internal allocations;
- model tests that expose weak economic reasoning.
Rio should not be "the crypto agent". Rio should be the agent that asks whether the system's incentives create useful knowledge or garbage incentives.
### Theseus
Theseus becomes the model-integrity and agent-refinement evaluator.
Theseus owns:
- model diversity and correlated-blind-spot measurement;
- adversarial eval rubrics;
- prompt/tool safety and self-upgrade criteria;
- disagreement queues and verifier-divergence analysis;
- LLM capability evidence and agent-system architecture;
- tests that expose hallucinated certainty, weak causal claims, and prompt-injection fragility.
Theseus should not be "the AI safety agent". Theseus should be the agent that asks whether the decision system can be trusted when the models are persuasive but wrong.
## Decision Engine Loop
```mermaid
flowchart TD
PR["Decision-engine PR or source record"] --> Route["Deterministic route evidence"]
Route --> Reviewers["Required agent reviewers"]
Reviewers --> Rubric["Shared rubric"]
Rubric --> ModelA["Primary model"]
Rubric --> ModelB["Independent model family"]
ModelA --> Verdicts["Structured verdicts"]
ModelB --> Verdicts
Verdicts --> Disagree{"Disagreement?"}
Disagree -->|yes| Queue["Disagreement queue"]
Disagree -->|no| Metrics["Calibration metrics"]
Queue --> HumanOrLeo["Leo or human arbitration"]
HumanOrLeo --> Metrics
Metrics --> DB["SQLite feedback state"]
DB --> Refine["Prompt, tool, or model proposal"]
Refine --> Delta["Before/after eval harness"]
Delta -->|passes| Update["Commit refinement"]
Delta -->|fails| Archive["Archive failed refinement"]
```
## Model Portfolio
The goal is not to pick one favorite model. The goal is to assign models to failure modes.
| Lane | Primary evaluator | Independent check | Why |
| --- | --- | --- | --- |
| Fast triage | cheap small model | deterministic route evidence | triage should be cheap and overridable |
| Domain review | routed agent prompt | different model family | catch domain-specific errors without same-family agreement bias |
| Deep review | strongest available reasoning model | non-Claude or non-primary family | deep review is for structural claims and disagreement |
| Economic reasoning | Rio rubric | model with strong quantitative/mechanism reasoning | tests incentive design, paid-query effects, and contribution weights |
| Agent/refinement safety | Theseus rubric | model with strong adversarial critique | tests tool safety, self-upgrades, and evaluator drift |
Candidate models should enter only through a harness:
1. fixed input set;
2. fixed rubric;
3. structured verdict JSON;
4. cost and latency recorded;
5. disagreement categories stored;
6. before/after comparison against current baseline.
No model switch is accepted because it "sounds better" on one example.
## Refinement Workstreams
### R0: Model Discovery Registry
Create a registry before arguing about model preference. The registry should track:
- hosted frontier models;
- open-weight Hugging Face candidates;
- local or edge candidates;
- small, cheap triage models;
- larger reasoning models, including future in-house or 27B-class candidates;
- license, hardware, context, latency, cost, tool support, and known failure modes.
The registry does not bless a model. It decides which model deserves a bakeoff fixture.
### R1: Rubric Packets
Create a small rubric packet for each evaluator role:
- `rio-economics-rubric`
- `theseus-model-integrity-rubric`
- `leo-cross-domain-rubric`
- domain-specific factuality rubrics
Each packet must define allowed verdicts, rejection tags, must-check criteria, and examples of false positives.
### R2: Evaluation Corpus
Build a replayable corpus from existing PRs:
- approved clean PRs;
- rejected PRs by issue tag;
- Rio/Theseus cross-domain PRs;
- paid-query or contribution-weight examples;
- adversarial malformed claims;
- near-duplicate and OPSEC edge cases.
Use local fixture data first. Production DB sampling requires the DB operator skill.
### R3: Model Bakeoff
Run each candidate model against the same corpus and emit:
- accuracy against expected disposition;
- false-approve count;
- false-reject count;
- issue-tag precision;
- average latency;
- estimated cost;
- disagreement matrix by model pair.
The highest-signal metric is not raw approval rate. It is false approvals on bad claims plus useful disagreement on ambiguous claims.
### R4: Feedback Loop
Use `review_records`, `audit_log`, `costs`, and PR state to find:
- recurring model failure categories;
- agents with repeated same-tag rejections;
- prompts that produce vague reviews;
- cost spikes without quality gain;
- routes that keep requiring manual override.
Every prompt/tool change should include a before/after proof over this loop.
### R5: Agent Runtime Packages
Package the same decision-engine contract for:
- NousResearch Hermes Agent: skill/memory/model-switching oriented.
- OpenClaw: workspace skill plus `AGENTS.md`, `SOUL.md`, `TOOLS.md` oriented.
- Claude-style, Pentagon, or other persistent agents: skill-oriented knowledge-base read/write interop.
Both packages should be fixture-first and no-secret by default. They are distribution surfaces for the decision engine, not separate evaluators with their own truth.
### R6: Knowledge-Base Interop
Any Hermes, OpenClaw, or Claude-style agent should be able to read information from the Living IP knowledge base and propose writes back into it.
The contract is:
- read through deterministic search, claim indexes, copied SQLite state, or cited repo files;
- propose source, claim, entity, correction, and route artifacts;
- never write directly to main;
- never mutate production `pipeline.db` from a model response;
- leave proof showing the exact query, cited reads, proposed write, and route evidence.
Use `.agents/skills/living-ip-kb-interop/SKILL.md` for runtime-neutral KB access, and `.agents/skills/teleo-db-operator/SKILL.md` for SQLite-specific work.
## DB Usage Boundary
Default is read-only.
Writes are allowed only when all are true:
- the target DB is local, staging, or explicitly authorized production;
- a backup or copy exists;
- the write is wrapped in a transaction;
- the exact query is retained in a proof artifact;
- the post-write readback is retained.
Never let an agent tune prompts by mutating production state directly.
## Pentagon.run Boundary
Pentagon.run should own:
- disposable VPS setup;
- Crabbox or remote proof execution;
- Hetzner lifecycle;
- runner cleanup;
- infra receipts.
- persistent agent teammates, company-brain infrastructure, and agent-to-agent transport when that is their managed stack.
This repo should own:
- decision-engine quality;
- model and prompt experiments;
- agent skills and adapter handoffs;
- database feedback analysis;
- proof schemas for eval quality.
Raw cards and secrets are not agent runtime inputs. Human operators may decide vendor billing and spend policy, but repo artifacts should only name secret slots, scoped tokens, spend limits, receipts, and setup checklists.
## Transcript-Derived Requirements
The 2026-06-01 working transcript adds these requirements:
- LLM/refinement work should focus on model discovery, compression, context strategy, and decision-engine quality while Pentagon handles cloud/persistent-agent infrastructure.
- Rio should be the first place to route Meteora, LP, x402, futarchy, paid-query, and contribution-incentive questions.
- Theseus should own the skill/MCP/refinement path that makes model judgment portable across Hermes, OpenClaw, Claude-style agents, and Pentagon-style company brains.
- The knowledge-writing path should turn large founder/source corpora into structured, reviewable knowledge packets, not shallow summaries.
- Slack, Linear, email, billing, and provider accounts are external collaboration setup. They should unblock people, but they are not prerequisites for local fixture, rubric, and proof work.
## Next Implementation Slice
1. Add `docs/model-discovery-registry.md`.
2. Add `scripts/replay_decision_engine_eval.py` with local fixture mode.
3. Add `fixtures/decision-engine-eval/*.json`.
4. Store verdict outputs in `.crabbox-results/decision-engine-eval.json`.
5. Add one Rio economics fixture and one Theseus model-integrity fixture.
6. Add one KB interop fixture that searches existing context and proposes a write without touching main or production DB.
7. Compare current prompt versus one candidate prompt before touching runtime prompts.
Do not start by changing live model assignments.
Run `python3 scripts/replay_decision_engine_eval.py` after changing fixture, rubric, registry, or candidate-output formats.

View file

@ -1,75 +0,0 @@
# Model Discovery Registry
Created: 2026-06-01
Status: candidate registry, not model approval
This registry exists to decide which models deserve a Living IP bakeoff fixture. It does not choose production models and it does not replace measured replay results.
## Rules
- Use official provider docs, model cards, or source repositories for every entry.
- Treat all model specs, prices, context limits, and aliases as volatile.
- Do not switch runtime model assignments from this document alone.
- Promote a model only after `scripts/replay_decision_engine_eval.py` shows no critical regression on the same fixture set.
- Prefer different model families for independent review so agreement is not just same-family correlation.
## Candidate Matrix
| Candidate | Surface | Why It Is Worth Testing | First Living IP Lane | Source |
| --- | --- | --- | --- | --- |
| GPT-5.5 / GPT-5.4 family | Hosted API | Strong general reasoning and agentic task baseline; useful as a frontier comparison point. | deep review, Leo arbitration | [OpenAI models](https://platform.openai.com/docs/models) |
| GPT-5 lower-latency variants | Hosted API | Possible cheap triage candidates; exact model IDs must be re-verified before a bakeoff run. | fast triage | [OpenAI models](https://platform.openai.com/docs/models) |
| gpt-oss-120b | Open-weight | Open-weight reasoning candidate for on-prem or Pentagon-managed inference; needs hardware/cost proof. | Theseus model integrity | [OpenAI open models](https://openai.com/open-models/) |
| gpt-oss-20b | Open-weight | Smaller local/edge candidate for cheap first-pass triage and portable demos. | fast triage, local harness | [OpenAI open models](https://openai.com/open-models/) |
| Claude Opus 4.8 | Hosted API | Complex-reasoning candidate for highest-stakes arbitration. | Leo arbitration, deep review | [Anthropic models overview](https://docs.anthropic.com/en/docs/about-claude/models) |
| Claude Sonnet 4.6 | Hosted API | Speed/intelligence tradeoff candidate for domain review. | domain review | [Anthropic models overview](https://docs.anthropic.com/en/docs/about-claude/models) |
| Claude Haiku 4.5 | Hosted API | Low-latency candidate for cheap reviewer pre-checks. | fast triage | [Anthropic models overview](https://docs.anthropic.com/en/docs/about-claude/models) |
| Gemini 3.5 Flash | Hosted API | Agentic/coding-oriented candidate from a different model family. | independent second review | [Gemini API models](https://ai.google.dev/gemini-api/docs/models) |
| Gemini 3.1 Pro | Hosted API | Complex problem-solving candidate from a non-primary model family. | deep review | [Gemini API models](https://ai.google.dev/gemini-api/docs/models) |
| Mistral Medium 3.5 | Hosted or open surface per provider docs | Agentic/coding candidate with a non-US-primary model family. | independent second review | [Mistral models overview](https://docs.mistral.ai/getting-started/models/) |
| Mistral Small 4 | Hosted or open surface per provider docs | Efficient hybrid instruct/reasoning/coding candidate. | fast triage, domain review | [Mistral models overview](https://docs.mistral.ai/getting-started/models/) |
| Mistral Large 3 | Open-weight | Large open-weight comparison point for self-hosted evaluation. | deep review | [Mistral models overview](https://docs.mistral.ai/getting-started/models/) |
| Devstral 2 | Hosted or open surface per provider docs | Code-agent candidate for tools, repository work, and adapter tasks. | Theseus tool integrity | [Mistral models overview](https://docs.mistral.ai/getting-started/models/) |
| Hermes 4 70B | Open-weight / provider-hosted | Nous-aligned model with structured output and tool-use relevance for Hermes Agent packaging. | Hermes adapter, Theseus | [NousResearch Hermes 4 70B](https://huggingface.co/NousResearch/Hermes-4-70B) |
| Qwen3.5 9B | Open-weight | Small multimodal/open-weight candidate for local and edge experiments. | fast triage, local harness | [Qwen3.5 9B model card](https://huggingface.co/Qwen/Qwen3.5-9B) |
## Bakeoff Intake Fields
Each candidate needs a retained record before a real bakeoff:
- provider or local runtime;
- exact model ID or pinned snapshot;
- source URL;
- license or terms surface;
- context window and max output if verified;
- structured-output support;
- tool/function calling support;
- expected hardware or hosted cost;
- latency estimate;
- privacy and data-retention posture;
- failure mode hypothesis;
- first fixture lane.
## First Bakeoff Order
1. Cheap triage: exact-ID-verified GPT-5 lower-latency variant, Claude Haiku 4.5, Mistral Small 4, Qwen3.5 9B, gpt-oss-20b.
2. Theseus integrity: Gemini 3.5 Flash, Hermes 4 70B, Devstral 2, gpt-oss-120b.
3. Rio economics: GPT-5.5/5.4, Claude Sonnet 4.6, Gemini 3.1 Pro, Mistral Medium 3.5.
4. Deep arbitration: Claude Opus 4.8, GPT-5.5, Gemini 3.1 Pro, Mistral Large 3.
## Promotion Gate
A model can move from registry to runtime proposal only if the replay proof includes:
- exact model ID;
- fixture count;
- route accuracy;
- false approvals;
- false rejects;
- missing required issue tags;
- average latency;
- cost estimate;
- disagreement matrix against current baseline;
- one paragraph explaining why the observed disagreements are useful.
Zero false approvals on known-bad fixtures is a hard gate for evaluator roles.

View file

@ -1,996 +0,0 @@
# Phase 1b Agent Routing Spec
Created: 2026-05-29
Status: active draft
Owner: Epimetheus pipeline implementation, with m3taversal as scope owner and Fwaz as VPS/runtime owner
## Product Outcome Contract
Phase 1b makes the knowledge-base evaluation engine behave like a six-agent review system instead of a generic triage stack.
When a contribution changes the `decision-engine` KB, the pipeline must decide which Hermes agent identity is responsible for judging that change, run the required review or reviews, post agent-specific verdicts, and then let the existing merge or feedback machinery continue.
The user-visible outcome is not a new frontend. It is a PR review trail showing that the right agent or agents reviewed the right KB mutation.
## Non-Goals
This spec does not implement:
- Twitter/X posting.
- x402, wallet, payment, or funding flows.
- Decision markets, agent bidding, stake-weighted quorum, or prediction-market review.
- Full general user-input routing outside the PR evaluation path.
- Separate GitHub accounts for each agent.
- A full Forgejo-to-GitHub daemon rewrite beyond what Phase 1b needs.
- A dashboard redesign.
- Production deployment without staging or VPS proof.
## Program Decomposition
This is a medium-sized control-plane change with five execution lanes:
1. Agent identity routing.
2. Eval pipeline integration.
3. GitHub identity and bot comment posture.
4. Reporting and contributor compatibility.
5. Staging and production proof.
The implementation can remain in one PR only if lanes 1 through 4 are tightly tested and the staging proof remains a separate operator task. If the eval integration diff grows beyond the files named in this spec, split into:
- PR 1: route contract and tests.
- PR 2: eval integration and mocked state tests.
- PR 3: GitHub/comment idempotency and reporting compatibility.
- PR 4 or operator runbook: staging proof artifacts.
Child specs:
- `docs/phase1b/agent-identity-router-spec.md`
- `docs/phase1b/eval-pipeline-integration-spec.md`
- `docs/phase1b/github-identity-bot-posture-spec.md`
- `docs/phase1b/reporting-contributor-compatibility-spec.md`
- `docs/phase1b/staging-proof-spec.md`
## Priority Matrix
| Rank | Workstream | Recurrence | Value | Readiness | Current state | Issue/spec mapping | Thread-claimed status | Verified implementation/proof status | Recommended next move |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 1 | Canonical repo and eval target | Repeated confusion between `teleo-codex`, `teleo-kb`, and `decision-engine`. | Critical | Ready now | Confirmed by user: `decision-engine`. Some code still has Forgejo/teleo-codex defaults. | This spec, `handoff/phase1-step3-script-migration.md` | Clarified in chat. | Partially reflected in repo; not unified in daemon modules. | Make Phase 1b route/proof explicitly target `decision-engine`. |
| 2 | Agent identity routing | Repeated confusion between domain folders and agent ownership. | Critical | Ready now | Existing `lib/domains.py` is folder-first. | This spec | m3taversal clarified identity-first routing. | Initial local patch is insufficient. | Replace with identity-scored route contract. |
| 3 | Cross-domain review | Raised as scope expansion during clarification. | High | Ready now | Not implemented. | This spec | m3taversal confirmed cap at top 2. | No code proof. | Add top-2 required reviewer aggregation. |
| 4 | Single master bot account | GitHub bot/PAT issue was noted as blocker. | High | Ready now | Phase 1 handoff already documents single `livingIPbot` posture. | `handoff/phase1-step3-script-migration.md` | Separate identities ideal, likely too complex. | Handoff-only. | Use master bot comments with agent verdict tags. |
| 5 | Staging proof | User asked how to test without mutating prod VPS. | Critical for production | Draft gated | Needs VPS clone or Crabbox/staging access. | This spec | Proposed, not executed. | No proof. | Run after code PR passes local checks. |
## Goal
Implement Phase 1b for the `decision-engine` knowledge base: pipeline-v2 evaluates each incoming KB pull request by routing it to the Hermes agent identity that owns the relevant domain of judgment.
The implementation lives in `teleo-infrastructure`. The canonical KB repo for this phase is `living-ip/decision-engine`.
Phase 1b is complete only when single-domain and cross-domain PRs are routed to the expected required reviewer agents, verdicts are posted in the existing `VERDICT:AGENT:*` format, and the merge or feedback path continues from those verdicts.
## User-Journey Contract
Contributor or agent flow:
1. A contributor or agent opens a PR against `living-ip/decision-engine`.
2. The PR changes one or more KB files.
3. Pipeline-v2 discovers the PR and fetches its diff.
4. The router scores Hermes agent identities from the diff, file paths, branch metadata, and eventually PR metadata.
5. The pipeline runs the required reviewer agents.
6. The master bot posts verdict comments that clearly name the agent identity in `VERDICT:AGENT:*` tags.
7. If all required reviewers approve, the existing approval and merge path continues.
8. If any required reviewer requests changes, the existing feedback/retry path continues.
Operator flow:
1. Operator can inspect a PR and see why each agent was selected.
2. Operator can inspect pipeline logs or audit rows and see route scores, required agents, verdicts, and aggregate result.
3. Operator can distinguish local proof, staging proof, and production proof.
## Existing-Spec Inventory
| Existing doc | Relevance | Decision | Reason |
| --- | --- | --- | --- |
| `handoff/phase1-step3-script-migration.md` | Establishes the Phase 1 move from Forgejo `teleo-codex` toward GitHub `living-ip/decision-engine`, and documents the single master bot account posture. | Reuse as context. | It owns migration history, not the Phase 1b routing implementation. |
| `handoff/deprecated/eval-scripts.md` | Confirms old eval dispatcher/worker scripts are dead and `lib/evaluate.py::evaluate_cycle` owns live eval behavior. | Reuse as context. | It prevents work from targeting retired scripts. |
| `docs/ARCHITECTURE.md` | Describes pipeline-v2 stages, SQLite state, Forgejo-era runtime topology, and existing evaluate/merge loops. | Reuse as context. | It is broader architecture; this spec is a Phase 1b delta spec. |
| `docs/multi-model-eval-architecture.md` | Documents the prior Leo-first plus second-model evaluation theory. | Supersede for Phase 1b eval routing only. | Phase 1b now routes to domain-owner agent identities, with capped top-2 cross-domain review. The old doc remains useful for later calibration. |
| `docs/queue.md` | Mentions domain evolution such as `ai-alignment` to `ai-systems`. | Reuse as signal. | It supports the identity-scored router rather than folder-only routing. |
## Current Implementation Audit
Current relevant implementation state:
- `teleo-pipeline.py` runs pipeline-v2 as a single async daemon.
- `lib/evaluate.py::evaluate_cycle` is the active eval loop.
- `lib/evaluate.py::evaluate_pr` currently detects a domain, runs a domain review, then runs Leo review for non-LIGHT PRs.
- `lib/domains.py` contains a folder-first `DOMAIN_AGENT_MAP`.
- `lib/llm.py` contains prompt templates and `run_domain_review`, `run_batch_domain_review`, and `run_leo_review`.
- `lib/eval_parse.py::parse_verdict` parses `VERDICT:AGENT:APPROVE` and `VERDICT:AGENT:REQUEST_CHANGES`.
- `pipeline-health-check.py` is GitHub-oriented and points at `living-ip/decision-engine`.
- `lib/forgejo.py`, `lib/evaluate.py`, and `lib/merge.py` still use Forgejo-named abstractions as the primary API surface.
- Per-agent GitHub identity is deferred; Phase 1 uses one master bot account.
Fwaz clarification on 2026-05-29:
- Separate GitHub identities are still ideal and blocked on GitHub/PAT setup; Phase 1b must not require them to land the routed-eval path.
- Current production update behavior is `pull -> services recognize pull -> edit on VPS -> PR to Leo`; this is useful context, not the desired long-term control model.
- New desired rule is no direct production self-upgrades: agents open PRs, and production deploys exact reviewed/tested SHAs approved and signed by Leo.
- Crabbox is acceptable as the long-term disposable staging/test-box direction, while a production-like clone remains the highest-fidelity proof for systemd/VPS paths.
This branch implementation now includes:
- `lib/agent_routing.py` with a pure identity-scored route contract.
- `PHASE1B_AGENT_ROUTING_ENABLED`, defaulting off.
- A Phase 1b eval path that runs routed required agents and disables stale domain batching under the flag.
- Focused tests for six-agent routing, top-2 cross-domain routing, verdict parsing, and mocked eval aggregation.
## Goal-Vs-Repo-Truth Diff
Desired Phase 1b behavior:
- Route PRs against `decision-engine`, not `teleo-codex`.
- Classify by agent identity ownership, not only by folder path.
- Run exactly the required reviewer agents.
- Use one master bot account if separate GitHub identities are too complex.
- Preserve the existing verdict comment format.
- Preserve existing merge and feedback behavior.
- Support cross-domain PRs by requiring the top 2 routed agents.
Pre-implementation repo truth:
- Pipeline eval still has a two-stage review shape: domain review plus Leo review.
- Folder-domain mapping exists, but agent identity scoring does not.
- Cross-domain review is not implemented as multiple required reviewer agents.
- Batch eval can group rows before fetching diffs, which risks routing unclassified rows through `general`.
- GitHub migration is partial: some scripts target GitHub `decision-engine`, but live daemon modules still have Forgejo-era names and assumptions.
## Completion Percent And Remaining Delta
Estimated implementation progress on this branch:
- B1 classifier foundation: 100 percent locally, pending staging calibration.
- B2 routing layer: 75 percent locally behind a default-off feature flag.
- Cross-domain top-2 review: 75 percent locally through mocked eval proof.
- Local proof suite: 85 percent for router/eval/parser scope.
- Staging or VPS proof: 0 percent.
Remaining delta:
1. Decide whether the production Phase 1b transport stays Forgejo-first for cutover or switches direct to GitHub `decision-engine` before staging.
2. Update reporting/health compatibility beyond `review_records` if staging shows false readiness.
3. Prove against staging before production.
4. Deploy only an exact reviewed/tested SHA after Leo signoff.
## Closure, Endpoint, And Deployment Truth
Local closure means:
- Focused tests pass in `teleo-infrastructure`.
- A PR exists with the Phase 1b routing implementation and proof notes.
Staging closure means:
- A cloned or disposable staging runtime is pointed at a sandbox `decision-engine`.
- Six single-domain sandbox PRs and one cross-domain sandbox PR complete the expected eval path.
- A machine-readable proof artifact captures routes, required agents, verdicts, status transitions, git SHAs, and logs.
Production closure means:
- The exact reviewed SHA is deployed to the production VPS.
- Production pipeline runs real `decision-engine` PRs through Phase 1b routing.
- All six agents have completed at least one live review cycle.
- Pipeline remains stable for at least 24 hours after cutover.
Without VPS or staging access, only local closure can be claimed.
## Critical Assumptions And Invalidators
Assumptions:
- `decision-engine` is the canonical KB repo for Phase 1b.
- The active eval implementation is `teleo-infrastructure/lib/evaluate.py`, not retired shell scripts.
- One master bot account is acceptable for Phase 1b verdict comments.
- Required reviewer identity is encoded in the verdict tag, not necessarily in the GitHub account identity.
- Agent state files in `decision-engine/agents/{agent}` are the right identity context source when present.
Invalidators:
- Production pipeline is still wired to a different canonical repo.
- The VPS runs code not represented by current `teleo-infrastructure`.
- Branch protection requires separate GitHub identities before comments or reviews count.
- Agent identity files are absent or materially different on the VPS.
- Cross-domain review must include more than top 2 reviewers.
## State And Truth Contract
The routing implementation must record or expose:
- PR number.
- Primary agent.
- Required agents.
- Route kind: `single`, `multi`, or `escalated`.
- Route scores by agent.
- Route evidence: path, branch, title, diff keyword, or fallback.
- Verdict per required agent.
- Aggregate result.
- Failure reason for missing or unparseable verdicts.
This can be stored first in audit log details and test artifacts. A DB schema migration is optional for Phase 1b unless downstream dashboards require queryable route fields.
### Route Decision Schema
The route decision should be serializable without importing Python classes. Use this JSON shape in audit rows and proof artifacts:
```json
{
"pr": 123,
"repo": "living-ip/decision-engine",
"route_version": "phase1b-v1",
"route_kind": "single",
"primary_agent": "Rio",
"required_agents": ["Rio"],
"scores": {
"Leo": 0,
"Theseus": 1,
"Rio": 9,
"Vida": 0,
"Clay": 0,
"Astra": 0
},
"evidence": [
{
"agent": "Rio",
"signal": "path",
"weight": 5,
"value": "domains/internet-finance/example.md"
}
],
"fallback": false
}
```
`route_kind` values:
- `single`: one required reviewer.
- `multi`: two required reviewers from cross-domain scoring.
- `fallback`: no confident route, Leo required.
- `escalated`: route exceeded simple review bounds and was capped by policy.
### Verdict State Schema
Aggregate review state should be serializable as:
```json
{
"pr": 123,
"required_agents": ["Theseus", "Rio"],
"agent_verdicts": {
"Theseus": "approve",
"Rio": "request_changes"
},
"aggregate_verdict": "request_changes",
"blocking_agents": ["Rio"],
"missing_agents": [],
"unparseable_agents": [],
"transport_failed_agents": []
}
```
Aggregate states:
- `approve`: all required agents approved.
- `request_changes`: at least one required agent requested changes or produced unparseable content.
- `retry`: at least one required review failed for transport reasons and should not burn the PR as a substantive rejection.
## Measurement Contract
Minimum metrics:
- `route_single_count`
- `route_multi_count`
- `route_escalated_count`
- `review_required_agent_count`
- `review_missing_verdict_count`
- `review_request_changes_count`
- `review_approve_count`
- `route_fallback_count`
Minimum proof matrix:
| Case | Expected route |
| --- | --- |
| grand strategy PR | Leo |
| ai systems or ai alignment PR | Theseus |
| internet finance or x402 PR | Rio |
| health PR | Vida |
| entertainment PR | Clay |
| space, robotics, energy, or advanced manufacturing PR | Astra |
| ai plus x402 PR | Theseus and Rio |
| collective ai goals PR | Leo and Theseus, if both score in top 2 |
## Score-To-100 Closure Plan
Preparedness score before implementation: 35/100.
| Score band | Closure move | Evidence that moves score |
| --- | --- | --- |
| 35 -> 50 | Route contract implemented and unit-tested. | `test_agent_routing.py` proves six single-agent routes, broadened identity ownership, top-2 cross-domain routes, and fallback behavior. |
| 50 -> 65 | Eval integration mocked locally. | Mocked eval tests prove required agents are invoked, default Leo review is removed, and aggregate verdicts drive approve/request-changes behavior. |
| 65 -> 75 | API/comment compatibility proven locally. | Tests prove all six verdict tags parse and master-bot comment bodies preserve existing parser expectations. |
| 75 -> 85 | Staging clone or disposable test box runs sandbox PR proof. | Six single-domain sandbox PRs plus one cross-domain sandbox PR produce expected comments and state transitions. |
| 85 -> 95 | Production deploy of exact reviewed SHA. | VPS deploy log, service restart readback, and route/proof artifact for first real PRs. |
| 95 -> 100 | 24-hour production stability. | 24-hour daemon readback with no duplicate comments, no stuck review rows, no production fallback spike, and all six agents represented in verdict history. |
The implementation PR can be merged at 65-75 if reviewers accept staging as a deploy gate. It cannot claim Phase 1b complete below 100.
## Backend Work Required
### 1. Agent identity router
Create or refactor into `lib/agent_routing.py` unless the existing `lib/domains.py` remains clearly small enough.
Define:
```python
AgentRoute(
primary_agent: str,
required_agents: tuple[str, ...],
route_kind: str,
scores: dict[str, int],
evidence: list[dict],
)
```
Router signals:
- Path signals from `domains/`, `entities/`, `core/`, `foundations/`, and `agents/`.
- Branch prefix signals such as `rio/`, `theseus/`, `astra/`, `leo/`.
- Keyword signals from path, filename, branch, PR title/body when available, and capped diff text.
- Agent identity ownership map.
Agent identity ownership map:
| Agent | Owns |
| --- | --- |
| Leo | grand strategy, teleohumanity goals, collective AI self-understanding, meta strategy, nested collective intelligence concepts |
| Theseus | AI systems, AI alignment, AI governance, agent systems, safety, evaluation |
| Rio | internet finance, living capital, markets, crypto, futarchy, x402, payments, capital formation |
| Vida | health, healthcare, medicine, prevention, clinical systems, mental health, biohealth |
| Clay | entertainment, media, culture, IP, fandom, narrative, consumer attention |
| Astra | space development, robotics, energy, advanced manufacturing, physical frontier infrastructure |
Routing rules:
- If only one agent crosses the threshold, require that agent.
- If more than one agent crosses the threshold, require the top 2 agents.
- If no agent crosses threshold, fallback to Leo with route kind `fallback`.
- Tie break by score, then deterministic configured order.
Implementation constraints:
- The router must be deterministic.
- The router must be pure and side-effect free.
- Route scores must be explainable through evidence entries.
- Folder paths should be strong evidence, not the whole classifier.
- Keyword scoring must not require paid inference.
- LLM classification may be added later only as shadow-mode evidence.
Recommended scoring starter:
| Signal | Weight |
| --- | --- |
| Path directly under known primary ownership area | 8 |
| Path under broadened ownership area | 6 |
| Branch prefix matches agent | 4 |
| Filename keyword matches ownership | 3 |
| Diff keyword matches ownership | 1 per capped hit |
| PR title/body keyword matches ownership, if available | 2 |
Top-2 selection:
- Include the highest-scoring agent.
- Include a second agent only if its score is at least 40 percent of the first score and at least the minimum threshold.
- Minimum threshold starts at 4.
- Never include more than two required agents in Phase 1b.
### 2. Eval layer integration
Modify `lib/evaluate.py`:
- Fetch PR diff.
- Build route from diff and branch.
- Store or audit route decision.
- Run required reviewer agents.
- Aggregate verdicts.
- Remove default Leo second-review for normal single-agent PRs.
- Keep existing bypasses for musings and reweave unless m3taversal changes policy.
- Revisit batch eval: disable batching for Phase 1b or classify before batching.
Implementation sequence:
1. Add pure route builder and tests.
2. Add review aggregation helper and tests.
3. Add `run_agent_review` while leaving existing `run_domain_review` and `run_leo_review` intact.
4. Switch individual `evaluate_pr` path to the new router behind a feature flag such as `PHASE1B_AGENT_ROUTING_ENABLED`.
5. Disable batch domain eval when the feature flag is enabled unless route-aware batching is implemented in the same PR.
6. Remove or bypass the default Leo second-review when the feature flag is enabled.
7. Preserve old behavior when the feature flag is disabled.
Feature flag requirement:
```text
PHASE1B_AGENT_ROUTING_ENABLED=false by default until staging proof exists.
```
The PR may set tests against enabled behavior without changing the production default.
### 3. Agent review runner
Modify or add in `lib/llm.py`:
```python
async def run_agent_review(diff: str, files: str, agent: str, route: AgentRoute) -> tuple[str | None, dict]:
...
```
Prompt must include:
- Agent identity context when available.
- Route evidence.
- Existing eval criteria.
- Required verdict tag for that exact agent.
Continue using one master bot account for comments. The bot comment body must identify the routed agent via the verdict tag.
Agent context lookup order:
1. Runtime-configured KB worktree path, expected to point at `decision-engine`.
2. Existing `config.MAIN_WORKTREE` if production still uses that convention.
3. Explicit test fixture path in unit tests.
Context files:
- `agents/{agent}/identity.md`
- `agents/{agent}/beliefs.md`
- `agents/{agent}/reasoning.md`
- `agents/{agent}/skills.md`
Missing context files:
- Log a warning.
- Include an audit evidence entry.
- Continue with the generic agent prompt.
- Do not crash the eval cycle.
### 4. Verdict aggregation
Add helper:
```python
aggregate_agent_verdicts(required_agents, reviews) -> AggregateVerdict
```
Rules:
- All required agents approve: approved.
- Any required agent requests changes: request changes.
- Transport failure: reopen for retry.
- Missing or unparseable verdict: request changes unless transport failure is explicit.
Comment format:
Preferred for one required agent:
```text
<review text>
<!-- VERDICT:RIO:APPROVE -->
```
Preferred for two required agents:
```text
## Theseus review
<review text>
<!-- VERDICT:THESEUS:APPROVE -->
## Rio review
<review text>
<!-- VERDICT:RIO:REQUEST_CHANGES -->
```
Two separate comments are acceptable if simpler and less risky for existing parsers.
### 5. Contributor and dashboard compatibility
Audit and update:
- `lib/contributor.py` assumptions that Leo reviews every PR.
- `pipeline-health-check.py` verdict parsing if needed.
- Any dashboard code assuming only `leo_verdict` plus `domain_verdict`.
Avoid broad dashboard redesign in Phase 1b. If dashboards need richer route state, add an audit artifact first and defer UI.
## Frontend Work Required
No frontend work is required for Phase 1b.
`livingip-web` Phase 1c can later reuse the same router as pre-PR guidance, but Phase 1b acceptance is based on `decision-engine` PR evaluation.
## Operator Work Required
Operator or infrastructure owner must provide before production proof:
- Current production deployed SHA for `teleo-infrastructure`.
- Current production KB target and worktree path.
- Current systemd units and restart commands.
- Staging clone or disposable test runner access.
- Sandbox `decision-engine` target or clear permission to create one.
- Staging token set with no production mutation authority.
- Rollback SHA and rollback command.
If these are unavailable, implementation can continue locally but production proof must remain blocked.
## Expected Runtime And User-Visible Behavior
Single-domain PR:
1. Pipeline detects route.
2. Required agents has one name.
3. Master bot posts one review comment with `VERDICT:AGENT:*`.
4. Existing merge or feedback path continues.
Cross-domain PR:
1. Pipeline detects route.
2. Required agents has two names.
3. Master bot posts one review comment per required agent, or one structured comment with separate verdict sections if that is simpler.
4. Merge requires both approvals.
5. Any request changes blocks and feeds back.
The user-visible proof is PR comments and final PR disposition.
## Staging Proof Contract
Staging must be production-like enough to test pipeline behavior but quarantined from production side effects.
Required staging safety controls:
- Production services disabled before any daemon starts.
- Production GitHub tokens removed or replaced.
- Production OpenRouter/Claude/Hermes keys removed or replaced unless explicitly approved for staging spend.
- Sandbox `decision-engine` repo configured.
- Auto-merge either disabled or constrained to sandbox repo.
- Hostname clearly changed to staging.
Required proof artifact:
```json
{
"phase": "1b",
"environment": "staging",
"teleo_infrastructure_sha": "...",
"decision_engine_sha": "...",
"pipeline_db_schema": 26,
"feature_flags": {
"PHASE1B_AGENT_ROUTING_ENABLED": "true"
},
"test_prs": [
{
"case": "internet-finance",
"pr": 1,
"required_agents": ["Rio"],
"verdicts": {"Rio": "approve"},
"final_state": "approved"
}
],
"cross_domain_pr": {
"required_agents": ["Theseus", "Rio"],
"final_state": "approved_or_feedback"
},
"prod_services_disabled": true,
"proof_generated_at": "2026-05-29T00:00:00Z"
}
```
Staging proof does not satisfy the 24-hour production stability gate.
## Validation And Test Matrix
Unit tests:
- `test_agent_routing.py`
- routes six primary ownership cases.
- routes broadened Astra cases: energy, robotics, advanced manufacturing.
- routes Leo meta cases: collective AI goals, teleohumanity strategy.
- routes Theseus AI systems cases.
- routes Rio x402 and internet finance cases.
- caps cross-domain to top 2 agents.
- has deterministic tie breaking.
Parser tests:
- Existing `test_eval_parse.py` remains valid.
- Add explicit verdict parse coverage for all six agent names.
Mocked eval integration tests:
- One required agent calls one runner and posts one verdict.
- Two required agents call two runners and post two verdicts.
- One request changes blocks aggregate approval.
- Transport failure reopens for retry.
- Default Leo second-review does not run unless Leo is routed.
Batch tests:
- If batching remains enabled, batch grouping must use route decisions, not stale DB domain.
- If batching is disabled for Phase 1b, assert cross-domain and single-domain PRs still process individually.
Smoke commands:
```bash
python3 -m venv .venv
. .venv/bin/activate
python3 -m pip install 'aiohttp>=3.9,<4' 'pytest>=8' 'pytest-asyncio>=0.23' 'ruff>=0.3' pyyaml
python3 -m pytest tests/test_agent_routing.py tests/test_evaluate_agent_routing.py tests/test_eval_parse.py
```
If local `pytest` is unavailable, that is a tooling blocker for full local proof, not an implementation blocker.
## CI/CD, Release, And Pre-Push Gate Contract
Pre-push required:
- `python3 -m pytest` for the focused routing/eval test set.
- `python3 -m ruff check lib tests` if dev deps are installed.
- Manual scan that no secrets are printed or committed.
PR required:
- Summary of routing rule.
- Test output.
- Known non-prod proof boundary.
- Statement that production acceptance still requires staging or VPS proof.
Deploy required:
- Exact reviewed SHA.
- Staging proof bundle first.
- Production service restart plan.
- Rollback SHA.
Release phases:
| Phase | Feature flag | Environment | Required proof |
| --- | --- | --- | --- |
| Local implementation | Enabled only in tests | Local | Unit and mocked eval tests. |
| Staging shadow | Enabled against sandbox repo | Staging clone or Crabbox-like box | Seven sandbox PR proof artifact. |
| Production shadow | Optional, no merge mutation if supported | Production | Route decisions logged without changing verdict path. |
| Production cutover | Enabled | Production | Real PR verdicts by required agents. |
| Production closure | Enabled | Production | 24-hour stability plus all six agents represented. |
Rollback:
- Flip `PHASE1B_AGENT_ROUTING_ENABLED=false`.
- Restart `teleo-pipeline.service`.
- Confirm eval path returns to prior behavior.
- If code rollback is required, deploy the previous exact SHA and restart service.
- Keep proof artifact explaining why rollback occurred.
Pre-push commands:
```bash
python3 -m pytest tests/test_agent_routing.py tests/test_evaluate_agent_routing.py tests/test_eval_parse.py
python3 -m ruff check lib tests
git diff --check
```
If dev dependencies are missing, install with:
```bash
python3 -m venv .venv
. .venv/bin/activate
python3 -m pip install 'aiohttp>=3.9,<4' 'pytest>=8' 'pytest-asyncio>=0.23' 'ruff>=0.3' pyyaml
```
## Independent CLI Audit Contract
A reviewer should be able to run:
```bash
git diff --stat
git diff -- lib/agent_routing.py lib/domains.py lib/evaluate.py lib/llm.py tests/
python3 -m pytest tests/test_agent_routing.py tests/test_evaluate_agent_routing.py
```
The audit should confirm:
- No direct production credentials are introduced.
- `decision-engine` is the target in docs/config where Phase 1b needs it.
- No old eval scripts are revived.
- Default Leo second-review is not silently preserved for all PRs.
- Multi-agent PRs require top 2 reviewer approvals.
## Outside-The-Box Fix Paths
If identity-scored keyword routing is too noisy:
- Use folder-first routing for strong path evidence and identity scoring only for ambiguous or cross-domain cases.
- Add a cheap LLM classifier in shadow mode only, comparing against deterministic router decisions.
- Require contributors/frontends to include an explicit domain or agent hint in PR metadata.
If live GitHub identity constraints block separate agent comments:
- Keep one master bot account and agent-specific verdict tags.
- Defer separate GitHub identities to Phase 2.
If staging VPS access is delayed:
- Use a disposable Hetzner clone when available.
- Use Crabbox or another remote test box for local dirty checkout proof.
- Use a mocked local fake GitHub/Forgejo API server for the eval loop.
## Maintenance Capture
Same-tranche maintenance that is justified now:
- Extract route scoring into a dedicated module if `lib/domains.py` would become too broad.
- Keep backward-compatible wrappers for existing `agent_for_domain` and `detect_domain_from_diff` until downstream callers are migrated.
- Add tests around the existing bug-prone batch grouping surface.
Maintenance to avoid now:
- Full Forgejo-to-GitHub daemon rewrite unless needed for the Phase 1b PR.
- Dashboard redesign.
- Contributor credit redesign beyond removing "Leo reviews every PR" assumptions.
- Separate GitHub identities per agent.
- Payment, wallet, Twitter, or decision-market work.
## Parallelization And Fanout
| Workstream | Classification | Owner | Notes |
| --- | --- | --- | --- |
| Agent identity router and tests | local_owner | Codex current turn | Core implementation surface. Do not fan out because it owns central route contract. |
| Eval layer integration and mocked tests | local_owner | Codex current turn | Needs tight coupling with router semantics. |
| Staging VPS clone proof | draft_gated | Fwaz or infrastructure owner | Requires VPS/provider access and secret quarantine. |
| GitHub identity model | draft_gated | Fwaz plus m3taversal | Deferred unless master bot account becomes unacceptable. |
| Dashboard/reporting polish | do_not_parallelize | Later | Avoid until route state contract is stable. |
### Workstream Sub-Spec: Agent Identity Router
Classification: local_owner
Owned files:
- `lib/agent_routing.py` if created.
- `lib/domains.py` compatibility wrappers.
- `tests/test_agent_routing.py`.
Forbidden files:
- `lib/evaluate.py` except imports needed for route type compatibility.
- Any runtime secrets.
- Any production config defaults outside route feature flags.
Binary done condition:
- Pure route function returns expected required agents for every row in the proof matrix.
- Tests prove deterministic top-2 behavior and fallback behavior.
Verification commands:
```bash
python3 -m pytest tests/test_agent_routing.py
```
Non-claims:
- Does not prove PR comment posting.
- Does not prove production target wiring.
Prompt-ready handoff:
```text
implement phase 1b agent identity routing in teleo-infrastructure. own only route module and route tests. preserve compatibility wrappers. route decision must be pure, deterministic, evidence-bearing, and top-2 capped for cross-domain cases. do not touch production API or eval state transitions.
```
### Workstream Sub-Spec: Eval Integration
Classification: local_owner
Owned files:
- `lib/evaluate.py`
- `lib/llm.py`
- `lib/eval_parse.py` only if parser normalization is required.
- `tests/test_evaluate_agent_routing.py`
- `tests/test_eval_parse.py`
Forbidden files:
- Old deprecated eval shell scripts.
- Deploy scripts unless a feature flag must be exposed.
- Dashboard UI except parser-compatible health checks.
Binary done condition:
- With `PHASE1B_AGENT_ROUTING_ENABLED=true`, eval invokes only required reviewer agents.
- With flag disabled, prior behavior remains available.
- One request-changes verdict blocks aggregate approval.
- All approve verdicts continue to existing approval path.
Verification commands:
```bash
python3 -m pytest tests/test_evaluate_agent_routing.py tests/test_eval_parse.py
```
Non-claims:
- Does not prove live GitHub or VPS behavior.
- Does not prove separate agent GitHub identities.
Prompt-ready handoff:
```text
wire phase 1b routing into teleo-infrastructure eval path behind a feature flag. use required agents from the route result, run agent-specific reviews, aggregate verdicts, and preserve merge/feedback semantics. do not revive deprecated scripts or remove rollback path.
```
### Workstream Sub-Spec: Staging Proof
Classification: draft_gated
Owned files and surfaces:
- Staging VPS or disposable remote test box.
- Sandbox `decision-engine` repo.
- Staging secrets.
- Machine-readable proof artifact.
Forbidden files and surfaces:
- Production VPS services.
- Production GitHub repo.
- Production secrets.
- Mainnet/payment/Twitter surfaces.
Binary done condition:
- Six single-domain PRs and one cross-domain PR produce expected required-agent verdicts and final dispositions in staging.
Verification commands:
```bash
systemctl status teleo-pipeline
journalctl -u teleo-pipeline --since "1 hour ago"
sqlite3 /path/to/pipeline.db "select number, status, domain_agent, leo_verdict, domain_verdict from prs order by number desc limit 20;"
gh pr view --repo living-ip/decision-engine-sandbox PR_NUMBER --comments
```
Non-claims:
- Does not prove production 24-hour stability.
Prompt-ready handoff:
```text
create a quarantined staging proof for phase 1b. clone or provision a disposable server, disable production services and secrets before starting pipeline, point to a sandbox decision-engine repo, run six single-domain prs plus one cross-domain pr, and save a machine-readable proof artifact. do not mutate production.
```
Worker-ready ticket for later staging proof:
```text
title: phase 1b staging proof on cloned vps
owned surfaces: staging vps, sandbox decision-engine repo, staging secrets, proof artifact
forbidden surfaces: production vps services, production github repo, production secrets
done condition: six single-domain prs plus one cross-domain pr produce expected required-agent verdicts and final dispositions
verification commands: systemd status readback, pipeline log scrape, sqlite route query, github pr comment readback
non-claims: does not prove 24h production stability
preferred executor: human/fwaz with codex support
handoff: create staging clone, disable prod services, inject sandbox config, run phase 1b proof script, save machine-readable proof
```
## Acceptance Criteria
Local PR acceptance:
- Focused tests pass.
- Router returns correct single-agent routes.
- Router returns top-2 required agents for cross-domain cases.
- Eval layer invokes only required reviewer agents.
- Verdict aggregation handles all approve, request changes, transport failure, and missing verdict.
- Existing verdict format remains parseable.
- No production readiness claim is made.
Staging acceptance:
- Staging environment cannot mutate production.
- Six single-domain sandbox PRs complete.
- One cross-domain sandbox PR completes.
- Required reviewer agents match proof matrix.
- Proof artifact is retained.
Production exit:
- Exact reviewed SHA deployed.
- All six agents produce at least one verdict in their domain.
- At least one cross-domain PR proves top-2 review behavior.
- Pipeline stable for 24 hours.
## Readiness And Claim Boundaries
Allowed claims after local implementation:
- "Route logic is implemented and locally tested."
- "Mocked eval integration proves required-agent invocation and aggregation."
- "The implementation PR is ready for staging proof."
Forbidden claims after local implementation:
- "Phase 1b is complete."
- "Production is ready."
- "All six agents have demonstrated live review cycles."
- "The VPS is safely updated."
Allowed claims after staging proof:
- "Phase 1b passed sandbox staging proof."
- "The exact SHA is eligible for production cutover review."
Forbidden claims after staging proof:
- "Production is stable."
- "Live `decision-engine` PRs are proven."
Allowed claims after production 24-hour proof:
- "Phase 1b production exit criteria are met."
## Spec Quality Self-Audit
Required execution-grade headings present:
- Current Implementation Audit: present.
- Goal-Vs-Repo-Truth Diff: present.
- Completion Percent And Remaining Delta: present.
- Closure, Endpoint, And Deployment Truth: present.
- Critical Assumptions And Invalidators: present.
- State And Truth Contract: present.
- Measurement Contract: present.
- Backend Work Required: present.
- Frontend Work Required: present.
- Expected Runtime And User-Visible Behavior: present.
- Validation And Test Matrix: present.
- CI/CD, Release, And Pre-Push Gate Contract: present.
- Independent CLI Audit Contract: present.
- Outside-The-Box Fix Paths: present.
- Maintenance Capture: present.
- Parallelization And Fanout: present.
Additional spec-of-spec coverage:
- Product Outcome Contract: present.
- Non-Goals: present.
- Program Decomposition: present.
- Priority Matrix: present.
- Score-To-100 Closure Plan: present.
- Workstream sub-specs: present.
- Staging Proof Contract: present.
- Rollback contract: present.
Known incompleteness:
- This spec cannot name the exact production deploy command until Fwaz or VPS truth confirms it.
- This spec cannot name the exact sandbox repo until the operator creates or selects it.
- This spec cannot prove whether production daemon code exactly matches local `teleo-infrastructure` until VPS readback exists.
## Assistant-Added Caveats
This spec intentionally expands B1/B2 from folder-domain routing to identity-scored agent routing because m3taversal clarified that agent identities should route and folders are only signals. That is the right product interpretation, but it increases implementation scope versus the original simple path classifier.
This spec does not claim production readiness without staging or VPS proof.

View file

@ -1,31 +0,0 @@
# Phase 1b Spec Index
Status: active draft
Parent spec: `docs/phase1b-agent-routing-spec.md`
## Scope
Phase 1b is the `decision-engine` PR evaluation router. It sends each KB mutation to the owning Hermes agent identity, supports top-2 cross-domain review, posts parseable `VERDICT:AGENT:*` comments through one master bot account, preserves existing merge or feedback behavior, and proves the change in staging before production cutover.
## Specs
| Workstream | Spec | Implementation posture |
| --- | --- | --- |
| Agent identity router | `docs/phase1b/agent-identity-router-spec.md` | ready_now |
| Eval pipeline integration | `docs/phase1b/eval-pipeline-integration-spec.md` | ready_now after router contract freezes |
| GitHub identity and bot comments | `docs/phase1b/github-identity-bot-posture-spec.md` | ready_now after canonical target config freezes |
| Reporting and contributor compatibility | `docs/phase1b/reporting-contributor-compatibility-spec.md` | ready_now after verdict state shape freezes |
| Staging proof | `docs/phase1b/staging-proof-spec.md` | draft_gated on staging/VPS or disposable remote access |
| Staging blocker | `docs/phase1b/staging-blocker.json` | external_only |
## Execution Order
1. Implement router contract and tests.
2. Wire eval pipeline to required reviewer agents under a feature flag.
3. Route comments through the canonical GitHub target with idempotency markers.
4. Update reporting and contributor accounting to read reviewer sets rather than fixed Leo plus domain slots.
5. Run staging proof on a clone or disposable remote target before production cutover.
## Claim Boundary
These specs plus the Phase 1b branch prove only local implementation behavior. A production completion claim requires merged code, passing tests, staging proof, exact production SHA deployment, Leo signoff, and 24-hour production daemon stability.

View file

@ -1,338 +0,0 @@
# Phase 1b Child Spec: Agent Identity Router
Created: 2026-05-29
Status: active draft
Parent spec: `docs/phase1b-agent-routing-spec.md`
## Product Outcome Contract
The router decides which Hermes agent identity should review a `decision-engine` KB PR. It must route by agent ownership, with file paths as strong evidence but not the only source of truth.
## Goal
Implement a pure, deterministic, evidence-bearing route scorer that returns one or two required reviewer agents for a PR.
## Non-Goals
- Do not call paid LLMs for routing.
- Do not post PR comments.
- Do not mutate pipeline DB state.
- Do not deploy to VPS.
- Do not implement general user-input routing outside PR evaluation.
## Current Implementation Audit
Current relevant code:
- `lib/domains.py` contains `DOMAIN_AGENT_MAP`, `agent_for_domain`, `detect_domain_from_diff`, and `detect_domain_from_branch`.
- `lib/agent_routing.py` now owns the Phase 1b identity-scored route contract.
- The obsolete local `DomainRoute` folder-first draft and its draft tests were removed before this branch was committed.
- Cross-domain PRs now require the top 2 routed agents locally, with `route_kind="escalated"` when more than two agents scored.
Existing implementation truth:
- The repo already has domain detection that can be reused for path signals.
- The new route tests cover six primary agents, broadened ownership domains, top-2 cross-domain routing, fallback, and deterministic repeat behavior.
- The existing map includes adjacent domains such as `mechanisms`, `living-capital`, `living-agents`, `critical-systems`, `collective-intelligence`, `teleological-economics`, and `cultural-dynamics`.
- The product owner clarified that Phase 1b should use agent identities to route, not only folder names.
## Existing-Spec Inventory
| Existing doc | Relevance | Decision |
| --- | --- | --- |
| `docs/phase1b-agent-routing-spec.md` | Umbrella source of truth. | Reuse. |
| `docs/queue.md` | Notes `ai-alignment` domain evolution. | Reuse as a signal for Theseus ownership. |
| `docs/ARCHITECTURE.md` | Describes eval stage shape. | Context only. |
## Goal-Vs-Repo-Truth Diff
Goal:
- Return `AgentRoute` with `primary_agent`, `required_agents`, `route_kind`, `scores`, and `evidence`.
- Cap cross-domain routes at top 2 agents.
- Treat folders as evidence, not the complete classifier.
- Be testable without network, DB, GitHub, or LLM calls.
Repo truth:
- Existing classifier returns one folder-domain string or `None`.
- No scores, evidence, or top-2 agent set exist.
- Existing tests do not cover identity-broadened ownership.
## Completion Percent And Remaining Delta
Current completion on this branch: 100 percent for local route logic, 0 percent for staging route calibration.
Remaining delta:
1. Review the route weights against real recent `decision-engine` PRs.
2. Calibrate ambiguous keyword cases from staging evidence.
3. Decide whether escalated routes should remain top-2 total or become Leo plus top-2 later.
## Closure, Endpoint, And Deployment Truth
Local closure:
- Route tests pass.
- No network or DB dependency exists in route tests.
Staging closure:
- Staging proof artifact records route scores and evidence for seven sandbox PRs.
Production closure:
- Live PR audit rows show route evidence and required agents.
This child spec alone cannot prove staging or production behavior.
## Critical Assumptions And Invalidators
Assumptions:
- `decision-engine` file layout is close enough to current local clone for path signals to apply.
- Agent identity ownership from m3taversal is authoritative.
- Top-2 cap is acceptable for cross-domain cases.
Invalidators:
- Product owner changes cross-domain rule from top 2 to all touched agents.
- Agent ownership boundaries change materially.
- Production PR metadata lacks branch or changed-file data.
## State And Truth Contract
Route output schema:
```python
AgentRoute(
primary_agent="Rio",
required_agents=("Rio",),
route_kind="single",
scores={"Leo": 0, "Theseus": 1, "Rio": 9, "Vida": 0, "Clay": 0, "Astra": 0},
evidence=[
{"agent": "Rio", "signal": "path", "weight": 8, "value": "domains/internet-finance/foo.md"}
],
fallback=False,
)
```
`route_kind` values:
- `single`
- `multi`
- `fallback`
- `escalated`
`required_agents` must never contain more than two agents in Phase 1b.
## Measurement Contract
Required route fixture cases:
| Fixture | Expected |
| --- | --- |
| `domains/grand-strategy/foo.md` | Leo |
| `domains/ai-alignment/foo.md` | Theseus |
| `domains/internet-finance/foo.md` | Rio |
| `domains/health/foo.md` | Vida |
| `domains/entertainment/foo.md` | Clay |
| `domains/space-development/foo.md` | Astra |
| `domains/energy/foo.md` | Astra |
| `domains/robotics/foo.md` | Astra |
| `domains/manufacturing/foo.md` | Astra |
| `core/living-capital/foo.md` | Rio |
| `core/living-agents/foo.md` | Theseus |
| `foundations/cultural-dynamics/foo.md` | Clay |
| AI plus x402 diff | Theseus and Rio |
| collective AI goals diff | Leo and Theseus |
Minimum quality metrics:
- `route_fixture_pass_rate = 100 percent`
- `fallback_count = 0` for known fixtures
- deterministic repeat count: same input returns same result 100 times
## Backend Work Required
Owned files:
- `lib/agent_routing.py`
- `lib/domains.py`
- `tests/test_agent_routing.py`
Implementation steps:
1. Move new identity routing into `lib/agent_routing.py`.
2. Keep `lib/domains.py` as compatibility for domain-oriented callers.
3. Define `AGENT_ORDER = ("Leo", "Theseus", "Rio", "Vida", "Clay", "Astra")`.
4. Define identity signals per agent.
5. Add path signal extraction for `domains`, `entities`, `core`, `foundations`, and `agents`.
6. Add branch prefix signal extraction.
7. Add capped keyword scoring from filenames and diff text.
8. Add top-2 selection rule.
9. Add fallback to Leo.
10. Add tests.
Forbidden files:
- `lib/evaluate.py`
- `lib/llm.py`
- deploy scripts
- secrets or runtime config outside route feature flag wiring
## Frontend Work Required
None.
## Expected Runtime And User-Visible Behavior
The router itself has no user-visible UI. Its behavior becomes visible through audit logs, PR comment reviewer selection, and proof artifacts.
Example:
```text
input: domains/internet-finance/x402-agent-payments.md
output: required_agents = ["Rio"]
```
Cross-domain example:
```text
input: ai systems claim plus x402 payment claim
output: required_agents = ["Theseus", "Rio"]
```
## Validation And Test Matrix
Commands:
```bash
python3 -m pytest tests/test_agent_routing.py
python3 -m ruff check lib/agent_routing.py lib/domains.py tests/test_agent_routing.py
git diff --check
```
Test classes:
- primary ownership routes
- broadened ownership routes
- branch fallback routes
- keyword routes
- top-2 cross-domain routes
- fallback routes
- deterministic tie-breaking
- compatibility wrapper behavior
## CI/CD, Release, And Pre-Push Gate Contract
Before PR:
- Route tests pass locally.
- No production config defaults change.
- No network dependency enters route tests.
Before staging:
- Eval integration spec consumes the route result without modifying route internals.
Before production:
- Route evidence appears in staging proof artifact.
## Independent CLI Audit Contract
Reviewer commands:
```bash
git diff -- lib/agent_routing.py lib/domains.py tests/test_agent_routing.py
python3 -m pytest tests/test_agent_routing.py
```
Reviewer checks:
- Route function is pure.
- Scores are explainable.
- Top-2 cap is enforced.
- Folder paths are not the only signal.
- Old callers still work or have a clear migration path.
## Outside-The-Box Fix Paths
If keyword scoring is noisy:
- Disable diff keyword scoring and use path plus branch only.
- Use LLM classifier in shadow mode only.
- Add explicit PR label or frontmatter hint later.
If identity boundaries are ambiguous:
- Prefer top-2 over fallback when two agents have meaningful scores.
- Log route evidence for later calibration.
## Maintenance Capture
Beneficial now:
- Keep route logic out of `lib/evaluate.py`.
- Keep compatibility wrappers narrow.
Avoid now:
- Large domain taxonomy rewrite.
- Dashboard UI changes.
- Paid classifier calls.
## Parallelization And Fanout
Classification: local_owner.
Do not fan out implementation. This module is a root contract consumed by eval integration.
Worker-ready prompt:
```text
implement the phase 1b agent identity router in teleo-infrastructure. own lib/agent_routing.py, lib/domains.py compatibility wrappers, and route tests only. make the route function pure, deterministic, evidence-bearing, and capped at top 2 required agents. do not touch eval integration or deploy code.
```
## Acceptance Criteria
- All required route fixtures pass.
- Route result includes primary agent, required agents, route kind, scores, evidence, and fallback status.
- Cross-domain route never requires more than two agents.
- No LLM, network, DB, or GitHub calls occur in the router.
## Readiness And Claim Boundaries
Allowed claim:
- "Agent identity routing is locally implemented and unit-tested."
Forbidden claim:
- "Phase 1b eval is complete."
## Spec Quality Self-Audit
Required headings present:
- Current Implementation Audit: present.
- Goal-Vs-Repo-Truth Diff: present.
- Completion Percent And Remaining Delta: present.
- Closure, Endpoint, And Deployment Truth: present.
- Critical Assumptions And Invalidators: present.
- State And Truth Contract: present.
- Measurement Contract: present.
- Backend Work Required: present.
- Frontend Work Required: present.
- Expected Runtime And User-Visible Behavior: present.
- Validation And Test Matrix: present.
- CI/CD, Release, And Pre-Push Gate Contract: present.
- Independent CLI Audit Contract: present.
- Outside-The-Box Fix Paths: present.
- Maintenance Capture: present.
- Parallelization And Fanout: present.
## Assistant-Added Caveats
This child spec intentionally keeps routing deterministic and no-spend. That may be less semantically smart than an LLM classifier, but it is the right first implementation for Phase 1b because it is testable, cheap, and auditable.

View file

@ -1,343 +0,0 @@
# Phase 1b Child Spec: Eval Pipeline Integration
Created: 2026-05-29
Status: active draft
Parent spec: `docs/phase1b-agent-routing-spec.md`
## Product Outcome Contract
Pipeline-v2 must use the Phase 1b route result to run the required Hermes agent reviews for a `decision-engine` PR. The old default shape where every non-LIGHT PR receives a domain review plus Leo review must be bypassed when Phase 1b routing is enabled.
## Goal
Integrate agent identity routing into `lib/evaluate.py` behind a feature flag, run one or two required reviewer agents, aggregate verdicts, and preserve existing merge or feedback behavior.
## Non-Goals
- Do not remove the old eval path until staging proof exists.
- Do not rewrite the full Forgejo/GitHub API abstraction.
- Do not redesign dashboards.
- Do not implement separate GitHub identities.
- Do not change extraction or validation behavior except as needed for eval tests.
## Current Implementation Audit
Current relevant code:
- `lib/evaluate.py::evaluate_pr` owns single PR evaluation.
- `lib/evaluate.py::evaluate_cycle` selects eligible PRs.
- `_build_domain_batches` groups STANDARD PRs by DB domain before fetching diffs.
- `_run_batch_domain_eval` runs batch domain reviews, then individual Leo reviews.
- `run_domain_review` in `lib/llm.py` prompts a domain expert through OpenRouter.
- `run_leo_review` in `lib/llm.py` prompts Leo through OpenRouter or Claude path depending on tier.
- `parse_verdict` in `lib/eval_parse.py` parses reviewer-specific verdict tags.
- `approve_pr`, `reopen_pr`, `close_pr`, and `start_review` handle state transitions.
Current behavior:
- Diff path detects a domain.
- `agent_for_domain(domain)` selects one domain agent.
- Domain review runs first.
- Leo review runs after domain approval for non-LIGHT PRs.
- `leo_verdict` and `domain_verdict` are the stored verdict fields.
- Contributor credit logic assumes Leo can be one evaluator and `domain_agent` can be the other.
## Existing-Spec Inventory
| Existing doc | Relevance | Decision |
| --- | --- | --- |
| `docs/phase1b-agent-routing-spec.md` | Parent route and eval contract. | Reuse. |
| `docs/ARCHITECTURE.md` | Existing pipeline stage model. | Reuse as baseline. |
| `docs/multi-model-eval-architecture.md` | Prior Leo-plus-second-model design. | Supersede for Phase 1b eval path only. |
| `handoff/deprecated/eval-scripts.md` | Confirms shell eval scripts are dead. | Reuse to avoid wrong surface. |
## Goal-Vs-Repo-Truth Diff
Goal:
- `evaluate_pr` calls the route scorer.
- Required agents are the only reviewer agents.
- One required agent means one review.
- Two required agents means two reviews and aggregate verdict.
- Default Leo second-review is removed when the feature flag is enabled.
- Old behavior remains available when the feature flag is disabled.
Branch truth:
- Legacy eval is still available when the feature flag is false.
- When the feature flag is true, eval invokes the identity route, runs required agents only, writes `review_records`, and projects aggregate state back into legacy `leo_verdict` and `domain_verdict` columns.
- Batch eval is disabled while the feature flag is true because stale DB-domain grouping is not route-aware.
- `run_agent_review` exists, but it uses prompt-level identity context rather than loading full KB identity/belief/reasoning files.
## Completion Percent And Remaining Delta
Current completion on this branch: 75 percent local implementation behind a default-off feature flag.
Remaining delta:
1. Decide direct GitHub `decision-engine` comment transport versus Forgejo-first cutover compatibility.
2. Prove with staging PRs and real daemon logs.
3. Update contributor/dashboard assumptions only where staging or tests prove breakage.
## Closure, Endpoint, And Deployment Truth
Local closure:
- Mocked eval tests prove route-to-review-to-aggregate behavior.
Staging closure:
- Staging sandbox PRs receive expected comments and DB state transitions.
Production closure:
- Live `decision-engine` PRs are handled by Phase 1b route path for 24 hours.
This spec cannot claim production closure without VPS proof.
## Critical Assumptions And Invalidators
Assumptions:
- Feature flag rollback is acceptable.
- Existing state fields can support Phase 1b initially by storing primary agent in `domain_agent` and aggregate details in audit rows.
- A DB schema migration is avoidable for the first PR.
- Master bot comments with `VERDICT:AGENT:*` are acceptable.
Invalidators:
- Downstream merge logic requires formal reviews from separate GitHub users.
- Dashboards or contributor credit fail hard when Leo is not present.
- Batch eval cannot be safely disabled and must be route-aware from day one.
- Production env cannot set feature flags.
## State And Truth Contract
Feature flag:
```text
PHASE1B_AGENT_ROUTING_ENABLED=false
```
When false:
- Existing eval behavior continues.
When true:
- Eval route is built for every non-bypass PR.
- Audit log records route JSON.
- Required agent reviews run.
- Aggregate verdict determines approval or feedback.
Minimal DB field use:
- `domain`: keep route primary domain or `multi`.
- `domain_agent`: keep primary agent.
- `domain_verdict`: keep aggregate non-Leo review verdict or aggregate verdict.
- `leo_verdict`: set `skipped` unless Leo is a required agent; if Leo is required, store Leo verdict.
- `review_records`: write one row per required reviewer attempt with reviewer agent, model, outcome, and notes.
- review comments include a `PHASE1B_REVIEW` marker and the current local helper suppresses duplicate posts for the same PR and agent.
- audit log: route and all per-agent verdicts.
This is a compatibility posture, not the ideal long-term schema.
## Measurement Contract
Required local assertions:
- Phase 1b flag disabled uses old runner calls.
- Phase 1b flag enabled calls `run_agent_review` once for single route.
- Phase 1b flag enabled calls `run_agent_review` twice for multi route.
- `run_leo_review` is not called unless Leo is in `required_agents`.
- all approve returns approved aggregate.
- one request changes returns feedback aggregate.
- transport failure reopens for retry.
- retry after a partial multi-agent success does not duplicate existing posted verdict comments.
## Backend Work Required
Owned files:
- `lib/evaluate.py`
- `lib/llm.py`
- `lib/config.py`
- `lib/eval_parse.py` only if parser compatibility needs explicit tests or normalization.
- `tests/test_evaluate_agent_routing.py`
- `tests/test_eval_parse.py`
Implementation steps:
1. Add `PHASE1B_AGENT_ROUTING_ENABLED` to `lib/config.py`.
2. Import route scorer.
3. Add `run_agent_review` in `lib/llm.py`.
4. Add helper to load agent context from KB worktree.
5. Add `aggregate_agent_verdicts`.
6. In `evaluate_pr`, after bypasses and diff filtering, branch into Phase 1b path when flag is true.
7. In Phase 1b path, run required reviews and post comments through the existing API helper.
8. Update DB fields conservatively.
9. Write `review_records` rows for each required reviewer attempt.
10. Preserve old logic under flag false.
11. Disable `_build_domain_batches` while flag is true or make it route-aware.
Forbidden files:
- Deprecated eval shell scripts.
- Deployment scripts unless needed for documenting the flag.
- Runtime secrets.
## Frontend Work Required
None.
## Expected Runtime And User-Visible Behavior
Single-agent example:
```text
PR touches internet finance.
route.required_agents = ["Rio"]
pipeline posts a Rio verdict.
merge proceeds if Rio approves.
```
Cross-agent example:
```text
PR touches AI systems and x402 payments.
route.required_agents = ["Theseus", "Rio"]
pipeline posts Theseus and Rio verdicts.
merge proceeds only if both approve.
```
Fallback example:
```text
PR cannot be confidently routed.
route.required_agents = ["Leo"]
pipeline posts Leo verdict.
route_kind = fallback is audited.
```
## Validation And Test Matrix
Commands:
```bash
python3 -m pytest tests/test_evaluate_agent_routing.py tests/test_eval_parse.py
python3 -m ruff check lib/evaluate.py lib/llm.py lib/config.py tests/test_evaluate_agent_routing.py
git diff --check
```
Test cases:
- flag-off old behavior smoke
- flag-on single reviewer approve
- flag-on single reviewer request changes
- flag-on two reviewer approve
- flag-on two reviewer one reject
- missing verdict
- transport failure
- Leo required route
- Leo not required route
- batch disabled or route-aware under flag
## CI/CD, Release, And Pre-Push Gate Contract
Before PR:
- Focused tests pass.
- Old behavior remains behind flag false.
- No production default flips to true.
Before staging:
- Operator can enable flag in staging env.
- Sandbox repo target is configured.
Before production:
- Staging proof artifact exists.
- Rollback command is known.
## Independent CLI Audit Contract
Reviewer commands:
```bash
git diff -- lib/evaluate.py lib/llm.py lib/config.py tests/test_evaluate_agent_routing.py
python3 -m pytest tests/test_evaluate_agent_routing.py
```
Reviewer checks:
- No deprecated scripts revived.
- No secrets introduced.
- Feature flag false preserves old path.
- Feature flag true bypasses default Leo second-review.
- Cross-domain aggregate requires all required reviewers to approve.
## Outside-The-Box Fix Paths
If compatibility fields become confusing:
- Add a narrow DB migration for `route_json` and `agent_verdicts_json`.
If batch eval blocks safe integration:
- Disable batch eval under Phase 1b flag for one release.
If LLM review prompts get too large:
- Load only identity plus beliefs first, then add reasoning/skills later.
## Maintenance Capture
Beneficial now:
- Isolate Phase 1b logic into helpers instead of expanding `evaluate_pr` deeply.
- Keep rollback path explicit.
Avoid now:
- Full eval architecture rewrite.
- Dashboard redesign.
- Broad DB migration unless tests require it.
## Parallelization And Fanout
Classification: local_owner.
Do not fan out before the router contract lands. Eval integration depends tightly on route result semantics.
Worker-ready prompt:
```text
wire phase 1b routing into teleo-infrastructure eval behind PHASE1B_AGENT_ROUTING_ENABLED. own lib/evaluate.py, lib/llm.py, lib/config.py, and mocked eval tests. run required agents from the route result, aggregate verdicts, preserve old behavior when the flag is false, and do not revive deprecated scripts.
```
## Acceptance Criteria
- Flag false path remains available.
- Flag true path runs required agents only.
- One or two verdicts aggregate correctly.
- Existing merge or feedback path is preserved.
- Focused mocked tests pass.
## Readiness And Claim Boundaries
Allowed claim:
- "Phase 1b eval integration is locally tested behind a feature flag."
Forbidden claim:
- "Phase 1b is live."
## Spec Quality Self-Audit
All required execution-grade headings are present. This spec intentionally defers exact production commands to the staging/proof child spec because they depend on VPS truth.
## Assistant-Added Caveats
The compatibility use of `domain_verdict` and `leo_verdict` is a pragmatic Phase 1b bridge. A cleaner route schema may be worth adding after staging proof, but a premature migration would widen the blast radius.

View file

@ -1,296 +0,0 @@
# Phase 1b Child Spec: GitHub Identity And Bot Posture
Created: 2026-05-29
Status: active draft
Parent spec: `docs/phase1b-agent-routing-spec.md`
## Product Outcome Contract
Phase 1b must post agent-specific verdicts for `decision-engine` PRs without requiring six separate GitHub accounts. Agent identity is represented in the comment content and verdict tags, while a single master bot account owns transport.
## Goal
Define and implement the minimum GitHub identity and comment transport posture for Phase 1b:
- canonical target is `living-ip/decision-engine`;
- one master bot token is acceptable;
- verdict comments preserve `VERDICT:AGENT:*`;
- duplicate comments are prevented;
- old Forgejo or mirror behavior remains rollback-safe until staging proof.
## Non-Goals
- Do not create separate GitHub users for all agents.
- Do not require GitHub branch protection to count separate formal reviewers in Phase 1b.
- Do not rewrite every Forgejo-named helper unless needed for Phase 1b comments.
- Do not redesign contributor credit.
- Do not revive deprecated eval shell scripts.
## Current Implementation Audit
Current truth:
- `pipeline-health-check.py` targets `https://api.github.com/repos/living-ip/decision-engine`.
- `research/research-session.sh` targets GitHub `living-ip/decision-engine` and `github-admin-token`.
- `handoff/phase1-step3-script-migration.md` documents Phase 1 single `livingIPbot` posture and defers per-agent identities.
- `lib/config.py` still defaults to Forgejo `teleo/teleo-codex`.
- `lib/github_feedback.py` hardcodes `living-ip/teleo-codex` and reads `github-pat`, not `decision-engine` and `github-admin-token`.
- `lib/evaluate.py` posts review comments through Forgejo helpers and per-agent Forgejo tokens.
- `lib/github_feedback.py` is a mirror feedback channel keyed by `prs.github_pr`, not the canonical review transport.
- `deploy/sync-mirror.sh` still references `living-ip/teleo-codex`.
- Fwaz confirmed separate GitHub identities are ideal and blocked on GitHub/PAT setup; Phase 1b implementation should not wait on six distinct accounts if the pipeline can post parseable `VERDICT:AGENT:*` comments through the pipeline bot.
## Existing-Spec Inventory
| Existing doc | Relevance | Decision |
| --- | --- | --- |
| `docs/phase1b-agent-routing-spec.md` | Parent identity posture. | Reuse. |
| `handoff/phase1-step3-script-migration.md` | Documents single bot token and GitHub `decision-engine` migration for scripts. | Reuse. |
| `handoff/deprecated/eval-scripts.md` | Confirms old eval scripts should not be revived. | Reuse. |
## Goal-Vs-Repo-Truth Diff
Goal:
- One canonical GitHub target for Phase 1b: `living-ip/decision-engine`.
- One master bot token for Phase 1b comments.
- Agent identity lives in verdict tags and comment headings.
- Comment posting supports idempotency by PR, head SHA, and agent.
Repo truth:
- GitHub target and token names are split across files.
- Eval comments still use Forgejo helpers.
- GitHub feedback is non-fatal mirror feedback, not agent review transport.
## Completion Percent And Remaining Delta
Current completion: 15 percent.
Remaining delta:
1. Add explicit GitHub target config with staging override.
2. Normalize token file selection or document compatibility.
3. Add Phase 1b comment posting helper for GitHub `decision-engine`.
4. Add idempotency marker.
5. Add tests for URL target, token path, missing token, and duplicate prevention.
6. Decide direct GitHub mode versus Forgejo-mirror mode before staging.
## Closure, Endpoint, And Deployment Truth
Local closure:
- Tests prove comments target `living-ip/decision-engine` and token material is not logged.
Staging closure:
- Sandbox PR comments are posted by master bot with agent verdict tags.
Production closure:
- Live `decision-engine` PR comments are posted by master bot without duplicates.
## Critical Assumptions And Invalidators
Assumptions:
- One bot account is enough for Phase 1b.
- Agent identity in verdict content satisfies acceptance.
- Formal GitHub reviews from distinct accounts are not required now.
- Per-agent PATs can be added later without changing the route contract.
Invalidators:
- Branch protection requires distinct GitHub reviewer identities.
- GitHub org disallows the selected PAT or bot account.
- Production daemon must remain Forgejo-first for the cutover window.
- Direct GitHub PRs lack the DB linkage used by existing `github_feedback`.
## State And Truth Contract
Comment idempotency marker:
```text
<!-- PHASE1B_REVIEW:PR=123:SHA=abc123:AGENT=RIO -->
```
Verdict marker remains:
```text
<!-- VERDICT:RIO:APPROVE -->
```
Required config:
```python
GITHUB_OWNER = "living-ip"
GITHUB_REPO = "decision-engine"
GITHUB_TOKEN_FILE = SECRETS_DIR / "github-admin-token"
```
Staging must override repo or owner without code changes.
## Measurement Contract
Minimum tests:
- URL builder targets `https://api.github.com/repos/living-ip/decision-engine`.
- Staging override changes target.
- Missing token returns non-fatal failure and audit detail.
- Token value is never logged.
- Duplicate marker prevents repeat comment for same PR, SHA, and agent.
- Six agent verdict tags remain parseable.
## Backend Work Required
Owned files:
- `lib/github_feedback.py` or a new `lib/github_reviews.py`.
- `lib/config.py`.
- `lib/evaluate.py` only where the eval integration calls the comment helper.
- `tests/test_github_identity.py` or equivalent.
Implementation steps:
1. Add canonical GitHub target config.
2. Add token lookup that prefers `github-admin-token` for Phase 1b and can fall back only if explicitly configured.
3. Add comment helper for agent verdict comments.
4. Add idempotency marker and readback check.
5. Add tests.
6. Wire eval integration to the helper under Phase 1b flag.
Forbidden files:
- Deprecated eval shell scripts.
- Production secrets.
- Broad deploy rewrite.
## Frontend Work Required
None.
## Expected Runtime And User-Visible Behavior
PR comment example:
```text
## Rio review
<review text>
<!-- PHASE1B_REVIEW:PR=123:SHA=abc123:AGENT=RIO -->
<!-- VERDICT:RIO:APPROVE -->
```
The GitHub account may be a master bot. The comment content must show which agent reviewed.
## Validation And Test Matrix
Commands:
```bash
python3 -m pytest tests/test_github_identity.py tests/test_eval_parse.py
python3 -m ruff check lib/github_feedback.py lib/config.py tests/test_github_identity.py
git diff --check
```
Test cases:
- canonical target
- staging override
- missing token
- no token logging
- idempotent comment marker
- all six verdict tags parse
## CI/CD, Release, And Pre-Push Gate Contract
Before PR:
- Local tests prove target and idempotency.
Before staging:
- Sandbox repo token exists.
- Production token is not used.
Before production:
- Bot account has comment permissions on `decision-engine`.
- Rollback path is old Forgejo or disabled Phase 1b flag.
## Independent CLI Audit Contract
Reviewer checks:
```bash
rg -n "teleo-codex|decision-engine|github-admin-token|github-pat|VERDICT|PHASE1B_REVIEW" lib tests pipeline-health-check.py research deploy
```
Audit questions:
- Which files still target `teleo-codex`?
- Are those files in the Phase 1b runtime path?
- Does any log path expose token values?
- Does idempotency prevent duplicate comments?
## Outside-The-Box Fix Paths
If direct GitHub comments are not safe in the first PR:
- Keep Forgejo review transport and post GitHub mirror feedback only in staging.
- Add a dry-run comment mode that writes the planned body into audit logs.
If GitHub PAT remains blocked:
- Use a GitHub App only for comment posting.
- Keep master bot for git push but app token for PR comments.
## Maintenance Capture
Beneficial now:
- Name GitHub target config clearly.
- Avoid proliferating `github-pat` versus `github-admin-token`.
Avoid now:
- Separate agent GitHub users.
- Full mirror rewrite.
- Contributor identity overhaul.
## Parallelization And Fanout
Classification: ready_now after the implementer explicitly chooses direct GitHub comments or Forgejo-mirror compatibility for the Phase 1b flag path.
Worker-ready prompt:
```text
implement phase 1b github review comment posture. use one master bot token, target living-ip/decision-engine with staging override support, add agent-specific verdict comment helper with idempotency marker, and prove no token leakage. do not create separate agent accounts or rewrite deploy/mirror broadly.
```
## Acceptance Criteria
- Phase 1b comment helper targets `decision-engine`.
- Master bot can post agent verdict tags.
- Duplicate comments are prevented.
- Missing token is non-fatal and auditable.
- Existing old transport remains rollback-safe.
## Readiness And Claim Boundaries
Allowed claim:
- "Master-bot GitHub verdict comment posture is locally specified/tested."
Forbidden claim:
- "Separate agent GitHub identities are solved."
## Spec Quality Self-Audit
All required execution-grade headings are present. The exact direct-GitHub versus Forgejo-mirror cutover remains a deliberate implementation decision because current daemon code is Forgejo-first.
## Assistant-Added Caveats
The repo has real target drift between `teleo-codex` and `decision-engine`. Do not hide that drift in the eval implementation. The Phase 1b PR should either fix the runtime path it uses or explicitly leave non-runtime references for a later migration.

View file

@ -1,125 +0,0 @@
# Phase 1b Local Review Guide
Status: local-only review artifact
Branch: `phase1b-agent-routing-local`
## What This Repo Is
`teleo-infrastructure` is the pipeline/runtime repo. For Phase 1b, it owns the evaluation daemon logic that watches PRs, fetches diffs, runs reviewers, posts verdict comments, and moves PR state toward merge or feedback.
Canonical split for this phase:
- KB repo: `decision-engine`
- implementation/runtime repo: `teleo-infrastructure`
- production runtime: VPS under `/opt/teleo-eval`, not currently accessible from this workspace
## What This Branch Changes
Local code changes:
- `lib/agent_routing.py`: new pure router that maps a PR diff to one or two Hermes agents.
- `lib/config.py`: adds `PHASE1B_AGENT_ROUTING_ENABLED`, default `false`.
- `lib/evaluate.py`: adds a feature-flagged Phase 1b eval path.
- `lib/llm.py`: adds `run_agent_review`.
- `tests/test_agent_routing.py`: router tests.
- `tests/test_evaluate_agent_routing.py`: mocked eval tests.
- `tests/test_eval_parse.py`: all six `VERDICT:AGENT:*` parser coverage.
Spec/docs changes:
- `docs/phase1b-agent-routing-spec.md`
- `docs/phase1b/README.md`
- child specs under `docs/phase1b/`
- `docs/phase1b/staging-blocker.json`
## What It Does Not Change
- It does not enable Phase 1b in production.
- It does not touch the VPS.
- It does not create or require six GitHub identities.
- It does not solve the Forgejo-vs-GitHub cutover.
- It does not fix unrelated full-suite failures.
## Current Safety Posture
The feature flag defaults off:
```text
PHASE1B_AGENT_ROUTING_ENABLED=false
```
With the flag off, the legacy eval path remains available. The Phase 1b path should only run in staging or a controlled daemon after explicit env config.
The local review hardening pass removed changes to `lib/domains.py` so the legacy domain map is not changed by this branch.
## Local Proof
Focused proof that currently passes:
```bash
.venv/bin/python -m pytest tests/test_agent_routing.py tests/test_evaluate_agent_routing.py tests/test_eval_parse.py
.venv/bin/ruff check lib/agent_routing.py lib/domains.py lib/evaluate.py lib/llm.py lib/config.py tests/test_agent_routing.py tests/test_evaluate_agent_routing.py
git diff --check
```
Latest focused result:
```text
61 passed
ruff: all checks passed
git diff --check: passed
```
Full-suite status:
```text
406 passed, 12 failed, 3 errors
```
Known full-suite failure groups:
- `db.migrate` fresh-fixture rebuild error: `prs_new has no column named auto_merge`
- contributor test fixture missing `submitted_by`
- date/frontmatter expectations in `test_post_extract.py`
- search threshold expectation in `test_search.py`
- missing `python-telegram-bot` imports for X content tests
Those failures mean this branch should not be called repo-green or PR-ready.
## How To Review Locally
Stay local:
```bash
git switch phase1b-agent-routing-local
git status --short --branch
git diff main...HEAD --stat
git diff main...HEAD -- lib/agent_routing.py lib/evaluate.py lib/llm.py lib/config.py
```
Review the behavior in this order:
1. `lib/agent_routing.py`
2. `tests/test_agent_routing.py`
3. `lib/evaluate.py`
4. `tests/test_evaluate_agent_routing.py`
5. `docs/phase1b/staging-blocker.json`
## Before Any PR
Do not open a PR until at least one of these is true:
- full-suite failures are triaged into accepted unrelated failures with issue links, or fixed;
- staging access is available and a sandbox proof path is ready;
- m3taversal/Fwaz explicitly accept a local-only draft review without staging proof.
## Before Production
Production requires:
- staging proof against sandbox `decision-engine`;
- exact reviewed SHA;
- Leo signoff;
- no direct VPS self-upgrades;
- `PHASE1B_AGENT_ROUTING_ENABLED` enabled only after cutover plan is written;
- rollback path to flag-off behavior.

View file

@ -1,275 +0,0 @@
# Phase 1b Child Spec: Reporting And Contributor Compatibility
Created: 2026-05-29
Status: active draft
Parent spec: `docs/phase1b-agent-routing-spec.md`
## Product Outcome Contract
Phase 1b must not make dashboards, health checks, or contributor credit lie about review state. Reporting may stay minimal, but it must not mark a cross-domain PR as ready before all required agents have reviewed.
## Goal
Update compatibility surfaces so Phase 1b required-agent reviews are represented accurately enough for operations, health, and contributor attribution without doing a dashboard redesign.
## Non-Goals
- Do not redesign the dashboard UI.
- Do not implement a new leaderboard model.
- Do not require a broad DB migration unless `review_records` is insufficient.
- Do not make production-readiness claims from health-check summaries alone.
## Current Implementation Audit
Current truth:
- `lib/db.py` already has `review_records` with `pr_number`, `domain`, `agent`, `reviewer`, `reviewer_model`, `outcome`, `rejection_reason`, and `notes`.
- `lib/contributor.py` assumes Leo reviews every PR and credits Leo plus one `domain_agent`.
- `lib/health.py` computes approval rates from `domain_verdict` and `leo_verdict`.
- `lib/health.py` builds reviewer strings only from `domain_verdict` and `leo_verdict`.
- `pipeline-health-check.py` can parse arbitrary `VERDICT:AGENT:*` tags, but it has no required-agent concept.
- A cross-domain PR with one approval and one missing required review could be misclassified if reporting only checks "any approve".
## Existing-Spec Inventory
| Existing doc | Relevance | Decision |
| --- | --- | --- |
| `docs/phase1b-agent-routing-spec.md` | Parent route/verdict state. | Reuse. |
| `docs/ARCHITECTURE.md` | Health/dashboard baseline. | Reuse as context. |
| `docs/DIAGNOSTICS-AGENT-SPEC.md` | Diagnostics philosophy. | Reuse as later direction, not immediate scope. |
## Goal-Vs-Repo-Truth Diff
Goal:
- Required-agent state is visible enough to avoid false readiness.
- Contributor evaluator credit follows actual approved reviewer agents.
- Health and pipeline checks can distinguish incomplete cross-domain review.
Repo truth:
- Legacy fields only represent `domain_verdict` plus `leo_verdict`.
- Contributor credit hardcodes Leo as universal reviewer.
- `pipeline-health-check.py` parses comments but does not know required reviewers.
## Completion Percent And Remaining Delta
Current completion: 10 percent because `review_records` already exists.
Remaining delta:
1. Ensure eval integration writes one `review_records` row per required reviewer.
2. Update contributor attribution to prefer approved `review_records`.
3. Keep legacy fields as projection only.
4. Add optional route marker parsing to `pipeline-health-check.py`.
5. Add tests proving no partial-review false readiness.
## Closure, Endpoint, And Deployment Truth
Local closure:
- Tests prove contributor credit and stage classification respect required reviewers.
Staging closure:
- Staging proof artifact and health readback agree on required-agent completion.
Production closure:
- Production health does not show PRs as ready before all required agents approve.
## Critical Assumptions And Invalidators
Assumptions:
- `review_records` is available in production DB schema.
- Eval integration can write `review_records` for each required reviewer.
- Dashboards can tolerate legacy projections during Phase 1b.
Invalidators:
- Production DB lacks `review_records`.
- Contributor code path cannot query `review_records` without performance issues.
- Branch protection or merge logic uses legacy fields directly for readiness.
## State And Truth Contract
`review_records` becomes the compatibility source for per-agent reviewer history.
Required eval write:
```text
one review_records row per required reviewer per PR attempt
```
Legacy projection:
- `domain_agent = primary_agent`
- `domain_verdict = aggregate_verdict`
- `leo_verdict = actual Leo verdict when Leo is required, else skipped`
Route/audit JSON remains the source for `required_agents`.
## Measurement Contract
Minimum compatibility metrics:
- `review_records_written_count`
- `required_reviews_missing_count`
- `partial_review_not_ready_count`
- `contributor_evaluator_credit_count_by_agent`
Minimum proof:
- A two-agent PR with one approval and one missing verdict is not classified as ready.
- A two-agent PR with two approvals is classified as ready.
- Contributor credit includes both approved reviewers.
## Backend Work Required
Owned files:
- `lib/contributor.py`
- `lib/health.py`
- `pipeline-health-check.py`
- `tests/test_contributor.py` or new focused test.
- `tests/test_pipeline_health_phase1b.py` if added.
Implementation steps:
1. Confirm `review_records` exists in local schema and migrations.
2. Update eval integration spec to write review records per required reviewer.
3. Update contributor credit to prefer approved `review_records.reviewer` rows.
4. Fall back to legacy `leo_verdict` and `domain_verdict` for old data.
5. Update health output to include review records or route audit fields where available.
6. Update pipeline health check to read required-agent markers if present.
7. Add tests.
Forbidden work:
- Dashboard redesign.
- New leaderboard model.
- Broad schema migration before proof requires it.
## Frontend Work Required
None.
## Expected Runtime And User-Visible Behavior
Operators should see:
- Per-agent reviewer outcomes when available.
- Cross-domain PRs not marked ready until all required reviewers approve.
- Contributor credit reflecting actual approved reviewer agents.
Existing dashboard layout can remain unchanged if data is honest.
## Validation And Test Matrix
Commands:
```bash
python3 -m pytest tests/test_contributor.py tests/test_pipeline_health_phase1b.py
python3 -m ruff check lib/contributor.py lib/health.py pipeline-health-check.py tests
git diff --check
```
Test cases:
- old data fallback credits Leo/domain reviewer.
- new `review_records` data credits all approved required reviewers.
- request-changes reviewer receives no evaluator credit.
- one missing required reviewer blocks ready classification.
- all required reviewers approve enables ready classification.
## CI/CD, Release, And Pre-Push Gate Contract
Before PR:
- Compatibility tests pass or are documented as not runnable due missing dev deps.
Before staging:
- Staging proof includes health and contributor-readback commands.
Before production:
- Operator verifies no partial-review false readiness in logs/health readback.
## Independent CLI Audit Contract
Reviewer commands:
```bash
rg -n "Leo reviews every PR|leo_verdict|domain_verdict|review_records|required_agents|VERDICT" lib pipeline-health-check.py tests
sqlite3 /path/to/pipeline.db ".schema review_records"
```
Reviewer checks:
- `review_records` is preferred for new evaluator credit.
- Legacy fallback remains for old rows.
- Health does not rely on any-approve for multi-review readiness.
## Outside-The-Box Fix Paths
If `review_records` is insufficient:
- Add additive `route_json` and `agent_verdicts_json` columns to `prs`.
If `pipeline-health-check.py` cannot read route markers:
- Treat cross-domain PRs as awaiting review unless all verdict tags expected by route artifact are present.
If contributor credit is too risky for Phase 1b:
- Defer credit mutation and emit review-record-only proof until after eval stability.
## Maintenance Capture
Beneficial now:
- Replace comments claiming "Leo reviews every PR."
- Add focused tests for the compatibility projection.
Avoid now:
- Dashboard UI rewrite.
- Historical backfill.
- Leaderboard redesign.
## Parallelization And Fanout
Classification: ready_now after eval integration establishes review record writes.
Worker-ready prompt:
```text
make reporting and contributor attribution phase 1b-compatible. prefer review_records for new evaluator credit, preserve legacy fallback, and prevent health/pipeline checks from marking cross-domain prs ready before all required agents approve. do not redesign dashboards or add broad schema migrations unless tests prove necessary.
```
## Acceptance Criteria
- No code path claims Leo reviews every new Phase 1b PR.
- Approved `review_records` can credit all required reviewer agents.
- Health/check logic avoids partial-review false readiness.
- Legacy data still renders.
## Readiness And Claim Boundaries
Allowed claim:
- "Reporting compatibility is updated to avoid false readiness and credit loss."
Forbidden claim:
- "Dashboards are redesigned for Phase 1b."
## Spec Quality Self-Audit
All required execution-grade headings are present. This spec is intentionally compatibility-scoped and does not attempt a full reporting product redesign.
## Assistant-Added Caveats
The safest first move is to write accurate `review_records` and route audit JSON. Rich dashboards should wait until production behavior proves stable.

View file

@ -1,18 +0,0 @@
{
"phase": "1b",
"blocked_area": "staging_and_production_proof",
"attempted_discovery": [
"audited teleo-infrastructure eval, config, deploy, systemd, github feedback, and health-check surfaces",
"implemented and tested local default-off phase1b routing path",
"opened draft pr for reviewed sha",
"recorded staging proof contract in docs/phase1b/staging-proof-spec.md"
],
"exact_blocker": "no usable staging vps clone, crabbox runner config, sandbox decision-engine repo token, or production read-only access is available in this workspace",
"why_it_cannot_be_solved_autonomously": "staging proof requires external infrastructure authority and non-production credentials; creating or using those without the project owner/runtime owner would risk mutating production or leaking production secrets",
"exact_next_action": "fwaz or m3taversal should provide either a scrubbed hetzner snapshot clone or crabbox config plus staging-only github/openrouter tokens and the sandbox decision-engine repo target",
"safe_until_unblocked": [
"keep PHASE1B_AGENT_ROUTING_ENABLED=false in production",
"review the draft pr locally and in ci",
"do not allow agents to self-edit production vps state for this change"
]
}

View file

@ -1,356 +0,0 @@
# Phase 1b Child Spec: Staging Proof
Created: 2026-05-29
Status: active draft
Parent spec: `docs/phase1b-agent-routing-spec.md`
## Product Outcome Contract
Phase 1b must be tested without mutating the production VPS or production `decision-engine` PRs. A staging clone or disposable remote test box must prove routing, verdict posting, and merge or feedback behavior against a sandbox target before production cutover.
## Goal
Define the staging proof path for Phase 1b: provision an isolated production-like runtime, disable production authority, run six single-domain PR cycles plus one cross-domain PR cycle, save a machine-readable proof artifact, then destroy or shut down the staging environment.
## Non-Goals
- Do not mutate production PRs.
- Do not use production GitHub tokens in staging.
- Do not prove 24-hour production stability.
- Do not promote a mutated staging server as production.
- Do not test payment, wallet, Twitter, or mainnet flows.
## Current Implementation Audit
Known repo truth:
- `systemd/teleo-pipeline.service` defines the production-style pipeline service.
- `deploy/` contains deployment and mirror scripts.
- `docs/ARCHITECTURE.md` documents VPS path assumptions and SQLite state.
- `docs/INFRASTRUCTURE.md` documents production as Hetzner `77.42.65.182`, root path `/opt/teleo-eval`, diagnostics on port `8081`, and health on port `8080`.
- `deploy/auto-deploy.sh` pulls from `/opt/teleo-eval/workspaces/deploy-infra`, syncs code into runtime paths, restarts changed Python services, and updates `/opt/teleo-eval/.last-deploy-sha` after smoke checks.
- `systemd/teleo-pipeline.service` expects `/opt/teleo-eval/pipeline/fix-ownership.sh`, while this repo stores that script under `deploy/fix-ownership.sh`; staging bootstrap must verify the live runtime path before assuming the unit works.
- `handoff/phase1-step3-script-migration.md` documents GitHub migration posture and `decision-engine` target for scripts.
- `handoff/deprecated/eval-scripts.md` confirms old eval scripts are dead.
- Fwaz described the current production update path as `pull -> services recognize pull -> edit on VPS -> PR to Leo`; staging proof must treat that as an unsafe legacy behavior to replace, not as a release gate.
- Fwaz approved Crabbox as the long-term disposable staging/test-box direction.
Unknown production truth:
- Exact current deployed SHA.
- Whether production service files match this repo.
- Whether production still points at Forgejo in the live daemon.
- Exact restart/deploy commands used by Fwaz or agents.
- Current secrets layout.
- Current `systemctl cat` output for `teleo-pipeline`, `teleo-diagnostics`, auto-deploy timers, cron-like research jobs, Telegram-related services, and any agent daemons.
- Whether production has uncommitted hotfixes, generated scripts, or local service patches under `/opt/teleo-eval`.
- Read-only live access is not available in this workspace; the infrastructure audit attempted SSH readback and hit authentication denial, so no production SHA or service state should be claimed from this spec.
## Existing-Spec Inventory
| Existing doc | Relevance | Decision |
| --- | --- | --- |
| `docs/phase1b-agent-routing-spec.md` | Parent proof requirements. | Reuse. |
| `docs/ARCHITECTURE.md` | VPS topology and service assumptions. | Reuse with current-readback requirement. |
| `systemd/teleo-pipeline.service` | Service command template. | Reuse as staging baseline. |
| `handoff/phase1-step3-script-migration.md` | GitHub `decision-engine` target context. | Reuse. |
## Goal-Vs-Repo-Truth Diff
Goal:
- Staging proof runs against sandbox `decision-engine`.
- Production services and secrets are disabled before test daemon starts.
- Proof artifact captures routes, verdicts, final PR states, SHAs, DB schema, feature flags, and logs.
Repo truth:
- Staging automation does not exist.
- No proof script exists for seven PR cases.
- No machine-readable Phase 1b proof schema exists outside the umbrella spec.
## Completion Percent And Remaining Delta
Current completion: 0 percent.
Remaining delta:
1. Choose staging substrate: Hetzner snapshot clone, Crabbox, or another disposable test box.
2. Define sandbox repo.
3. Define staging secrets.
4. Write or run proof sequence.
5. Retain proof artifact.
6. Confirm staging cannot mutate production.
## Closure, Endpoint, And Deployment Truth
Staging closure means:
- Staging environment is isolated.
- Sandbox PRs are created and processed.
- Required reviewer verdicts appear in PR comments.
- Pipeline state transitions match expected behavior.
- Proof artifact exists.
Production closure is separate and requires exact reviewed SHA deployment plus 24-hour readback.
## Critical Assumptions And Invalidators
Assumptions:
- A VPS snapshot or disposable equivalent can run the pipeline.
- Production secrets can be removed or replaced before daemon start.
- A sandbox GitHub repo can be used.
- The proof can run without real production inference spend, or spend is explicitly approved.
Invalidators:
- Clone boots production services before quarantine.
- Sandbox target cannot receive PRs/comments.
- No operator has cloud or VPS access.
- Secrets cannot be separated from production.
- Service paths on production are materially different from repo docs.
## State And Truth Contract
Proof artifact path should be under staging, then copied back into the PR or retained artifact location. Suggested filename:
```text
proof/phase1b-staging-proof-YYYYMMDD-HHMMSS.json
```
Required JSON fields:
```json
{
"phase": "1b",
"schema_version": 1,
"environment": {
"kind": "hetzner_snapshot|crabbox|disposable_remote",
"host": "...",
"snapshot_id": "...",
"created_from_prod_host": "77.42.65.182"
},
"teleo_infrastructure_sha": "...",
"decision_engine_target": "living-ip/decision-engine-sandbox",
"pipeline_db_schema": 26,
"feature_flags": {"PHASE1B_AGENT_ROUTING_ENABLED": "true"},
"safety": {
"prod_services_disabled": true,
"prod_timers_disabled": true,
"prod_crons_disabled": true,
"prod_secrets_removed": true,
"auto_merge_constrained": true
},
"test_cases": [],
"verification_outputs": {
"service_status_path": "...",
"journal_excerpt_path": "...",
"db_snapshot_path": "...",
"github_comments_path": "..."
},
"rollback": {
"production_sha_before": "...",
"candidate_sha": "...",
"rollback_command": "..."
},
"created_at": "..."
}
```
Each test case:
```json
{
"case": "internet-finance",
"pr": 12,
"required_agents": ["Rio"],
"posted_verdicts": {"Rio": "approve"},
"final_state": "approved",
"route_kind": "single"
}
```
## Measurement Contract
Minimum staging cases:
- grand strategy -> Leo
- ai systems or ai alignment -> Theseus
- internet finance -> Rio
- health -> Vida
- entertainment -> Clay
- space, robotics, energy, or advanced manufacturing -> Astra
- cross-domain ai plus x402 -> Theseus and Rio
Pass criteria:
- 7 of 7 route decisions match expected required agents.
- 7 of 7 PRs receive parseable verdict comments.
- No production repo receives comments.
- No production service remains enabled during staging run.
## Backend Work Required
Owned surfaces:
- Staging host.
- Sandbox repo.
- Staging env/config.
- Proof artifact generator or manual proof script.
Implementation steps:
1. Snapshot or provision staging environment.
2. Block public/prod access.
3. Disable production services.
4. Remove production secrets.
5. Set hostname to staging.
6. Configure sandbox target.
7. Deploy exact implementation SHA.
8. Enable Phase 1b feature flag.
9. Create seven sandbox PRs.
10. Run pipeline until verdicts and states are visible.
11. Save proof artifact.
12. Shut down or destroy staging.
## Frontend Work Required
None.
## Expected Runtime And User-Visible Behavior
Operator sees:
- Staging service status.
- Sandbox PR comments with agent verdict tags.
- SQLite rows or logs showing route decisions.
- Proof artifact summarizing pass/fail.
No production user-visible behavior should change during staging.
## Validation And Test Matrix
Commands will vary by staging substrate. Baseline readback:
```bash
hostname
git -C /opt/teleo-eval/workspaces/deploy-infra rev-parse HEAD
cat /opt/teleo-eval/.last-deploy-sha
systemctl is-active teleo-pipeline teleo-diagnostics teleo-auto-deploy.timer
systemctl list-timers | grep -E 'teleo|sync|extract|research' || true
curl -s localhost:8080/health | python3 -m json.tool
journalctl -u teleo-pipeline --since "1 hour ago" --no-pager
sqlite3 /opt/teleo-eval/pipeline/pipeline.db "select max(version) from schema_version;"
sqlite3 /opt/teleo-eval/pipeline/pipeline.db "select number,status,domain,domain_agent,leo_verdict,domain_verdict,auto_merge,github_pr from prs order by number desc limit 20;"
gh pr list --repo living-ip/decision-engine-sandbox --state all
gh pr view --repo living-ip/decision-engine-sandbox PR_NUMBER --comments
```
Safety checks:
```bash
systemctl is-enabled teleo-pipeline
systemctl cat teleo-pipeline
systemctl cat teleo-diagnostics
grep -R "github-admin-token" /opt/teleo-eval/secrets 2>/dev/null
git -C /opt/teleo-eval/workspaces/main remote -v
```
## CI/CD, Release, And Pre-Push Gate Contract
Before staging:
- Code PR has passed local tests.
- Sandbox target selected.
- Staging secrets prepared.
Before production:
- Staging proof artifact exists.
- Exact SHA to deploy is recorded.
- Rollback path is recorded.
- Leo approval/signoff for the exact reviewed SHA is recorded.
- The production cutover avoids direct agent self-edits on the VPS.
## Independent CLI Audit Contract
Auditor should verify:
- Staging host is not production.
- Production services were disabled before test daemon start.
- GitHub target is sandbox.
- Proof artifact PR IDs belong to sandbox repo.
- Logs show no production mutation.
## Outside-The-Box Fix Paths
If Hetzner snapshot clone is too risky:
- Use Crabbox with a synced checkout and fake/sandbox services.
- Use a fresh Hetzner server and repo checkout instead of disk clone.
- Use local fake GitHub/Forgejo API for pure pipeline proof.
Substrate guidance:
- Prefer a Hetzner snapshot clone for canonical staging proof because it exercises `/opt/teleo-eval`, systemd units, timers, runtime user ownership, SQLite path assumptions, and deploy scripts.
- Crabbox is acceptable and preferred long-term as `disposable_remote` proof for command/test execution, but it does not count as VPS-clone fidelity unless it recreates the same unit files, runtime paths, service user, database path, and deploy flow.
- A local fake GitHub/Forgejo API can prove parser and state logic, but it cannot close the staging acceptance gate for real GitHub comments.
If inference spend is a concern:
- Mock agent review responses in staging.
- Use a staging-specific cheap model.
- Run only one real model call after mocked proof passes.
## Maintenance Capture
Beneficial now:
- Add a reusable `proof/phase1b` script later if manual staging repeats.
- Record exact service and config readback.
Avoid now:
- Building a full deployment platform.
- Giving Crabbox or staging production secrets.
- Replacing production with staging server.
## Parallelization And Fanout
Classification: draft_gated.
This can be delegated to Fwaz or the infrastructure owner after code PR exists.
Worker-ready prompt:
```text
run phase 1b staging proof without mutating production. provision or clone a staging box, disable production services and secrets before starting the daemon, point the runtime at a sandbox decision-engine repo, enable phase 1b routing, run six single-domain prs plus one cross-domain pr, and save a machine-readable proof artifact. do not touch production prs or production secrets.
```
## Acceptance Criteria
- Staging is isolated.
- Seven sandbox PR cases run.
- Required agents match expected matrix.
- Verdicts are parseable.
- Proof artifact exists.
- Staging is stopped or destroyed after proof.
## Readiness And Claim Boundaries
Allowed claim:
- "Phase 1b passed staging proof."
Forbidden claim:
- "Production Phase 1b is complete."
## Spec Quality Self-Audit
All required execution-grade headings are present. Exact production commands remain unknown until VPS truth is read back.
## Assistant-Added Caveats
Crabbox is useful here only as a disposable staging/test substrate. It should not receive production secrets until there is a deliberate security review.

View file

@ -1,88 +0,0 @@
# Approved Claim Bundle Isolation Canary
Status: `pass`
## Working Target
A Cory-approved strict bundle must move through a separate reviewer credential,
an immutable approval snapshot, and a narrow apply credential before exact
claim/source/evidence/edge/reasoning-tool rows become canonical. A stale payload,
replay, hash collision, or direct privilege bypass must leave no partial rows.
## Final Runtime Proof
- Generic bundle: `37/37` checks passed. Its exact payload-controlled projection
and exact table deltas were `2` claims, `2` sources, `2` evidence links, `1`
edge, and `1` reasoning tool.
- Real Helmer bundle: `37/37` checks passed. Its exact payload-controlled
projection and exact table deltas were `5` claims, `13` sources, `17`
evidence links, `7` edges, and `1` reasoning tool.
- Helmer source proposal: `a64df080-8502-42e2-98f4-9bbdecb8da73`; strict child
proposal in the final isolated run: `87344d36-5005-5d88-a04c-067aeceda3c3`.
- `kb_review` approved the exact pending type and full JSON payload as mapped
reviewer `m3ta`; the immutable approval row matched the proposal ledger.
- `kb_apply` could not directly update proposal status/payload, immutable
approval rows, or existing `claim_evidence` rows.
- Mutating the approved ledger after SQL generation made the stale apply fail
before canonical writes; restoring the ledger left the immutable approval
unchanged.
- Replay was refused. A different source ID reusing an existing source hash was
refused, and its proposal/approval remained intact with zero partial rows.
- Every payload-controlled persisted field, including weights, roles, owners,
tags, hashes, and relationships, matched the reviewed payload. Exact table
deltas also exclude unexpected extra rows under unrelated IDs.
- The migration readback proves one expected signature per gate function, no
legacy overload, no protected-role membership, no object owned by a login
role, and the exact reviewer/apply execute split.
## Isolation And Cleanup
- Both runs used a new unexposed `postgres:16` container built from a read-only
schema/reference copy of live `teleo`; no cluster-wide role was created in the
production `teleo-pg` instance.
- The wrapper issued read-only queries to live `teleo-pg`; endpoint counts were
identical before and after each final run, with zero deltas for all six tables:
`claims=1837`, `sources=4145`, `claim_evidence=4670`, `claim_edges=4916`,
`reasoning_tools=17`, `kb_proposals=26`. This is endpoint-count evidence, not
a claim that every live row byte was compared.
- `leoclean-gateway.service` stayed `active/running`, `MainPID=2999690`,
`NRestarts=0`.
- Inner clone databases, outer Postgres containers, temporary workdirs, and the
remote source bundle were removed. Independent name/path readbacks found no
residue after cleanup.
- Both receipts retain repo-relative SHA-256/size bindings for the apply,
approval, prerequisite SQL, inner canary, and isolation wrapper source files;
a local recomputation matched every retained hash.
- No Telegram message, production canonical apply, production permission
migration, service restart, or runtime-profile write occurred.
## Reproduce
Generic bundle:
```bash
scripts/run_approve_claim_isolated_container_canary.sh \
--output docs/reports/leo-working-state-20260709/approve-claim-clone-canary-current.json
```
Real normalized Helmer bundle:
```bash
scripts/run_approve_claim_isolated_container_canary.sh \
--normalization-json docs/reports/leo-working-state-20260709/helmer-approve-claim-normalization-current.json \
--output docs/reports/leo-working-state-20260709/helmer-approve-claim-clone-canary-current.json
```
## Evidence And Claim Ceiling
- Generic receipt: `approve-claim-clone-canary-current.json`
- Helmer receipt: `helmer-approve-claim-clone-canary-current.json`
- Lossless normalization: `helmer-approve-claim-normalization-current.json`
- Inner harness: `scripts/run_approve_claim_clone_canary.py`
- Isolation wrapper: `scripts/run_approve_claim_isolated_container_canary.sh`
This proves the repo-owned review/apply lifecycle at isolated runtime tier. It
does not mean the new code, reviewer/apply roles, or permission migration are
deployed on VPS production, and it does not mean Helmer is canonical there.
`kb_apply` remains an operator-only trusted canonical writer; the proof covers
the supplied tool and privilege matrix, not a compromised apply credential.

View file

@ -1,38 +0,0 @@
# Argus KB State Probe
Generated UTC: `2026-07-10T02:48:11.734733+00:00`
Mode: `argus_kb_state_readonly_http_probe`
Status: `pass`
Base URL: `http://77.42.65.182:8081`
## Fetch Sources
- `kb_proposals`: `supplied_base64_json`
- `metrics`: `supplied_base64_json`
- `vital_signs`: `supplied_base64_json`
## Checks
- `approved_need_apply_payload`: `True`
- `approved_proposals_visible`: `True`
- `http_readback_ok`: `True`
- `kb_proposals_endpoint_read_only`: `True`
- `no_worker_applyable_approved_proposals`: `True`
## Approved Proposal State
- Total: `3`
- Status counts: `{"approved": 3}`
- Review state counts: `{"approved_needs_apply_payload": 3}`
- Worker-applyable count: `0`
- Read-only endpoint: `True`
## Proposal IDs
- `14fa5ecc-ac7a-41c1-807d-a2e85b936617`
- `ac036c9d-20a0-4ffe-881f-57d6b7bacf22`
- `a64df080-8502-42e2-98f4-9bbdecb8da73`
## Claim Ceiling
This is public Argus HTTP diagnostics evidence only. It proves the diagnostics surface can see approved KB proposals and their applyability state; it does not prove Docker/Postgres row counts, gateway systemd state, Telegram-visible reply quality, or production apply execution.

View file

@ -1,73 +0,0 @@
# Claim/Source Contract Preview - 2026-07-09
## Scope
Read-only contract preview for live approved rich KB proposals fetched from the VPS leoclean profile. This does not write Postgres, does not restart Leo, and does not change runtime behavior.
## Result
### `14fa5ecc-ac7a-41c1-807d-a2e85b936617`
- Status: `approved`
- Proposal type: `attach_evidence`
- Direct apply ready: `false`
- Claim rows needing canonical IDs: `4`
- Source rows needing canonical IDs/deduplication: `5`
- Edge fragments blocked until IDs/mapping exist: `17`
- Evidence fragments blocked until claim/source IDs exist: `9`
- Next action: Do not apply directly. Review a claim/source creation contract in a clone, assign/deduplicate canonical IDs, then rerun strict normalization to produce apply_payload child proposals.
Contract gaps:
- `current_apply_role_cannot_insert_claims_or_sources`: A separate reviewed role/contract for public.claims and public.sources creation.
- `claim_candidates_need_canonical_ids`: Create or deduplicate claim rows, then rewrite edges/evidence using UUIDs.
- `public_claims_has_no_body_or_review_note_columns`: Store those details in public.sources excerpts or add reviewed schema support.
- `public_sources_has_limited_metadata_columns`: Deduplicate sources and decide where provenance metadata is retained.
- `old_claim_supersession_is_many_successors`: Use typed claim_edges for supersession components or introduce an aggregate successor node.
- `concept_map_dependency_has_no_first_class_storage`: Create first-class concept-map storage or keep the concept map as source-backed context.
Claim candidates:
- `capital_allocation_is_civilizational_steering` -> type `structural`, confidence `0.65`, needs canonical id
- `capital_allocation_depends_on_distributed_market_intelligence` -> type `structural`, confidence `0.7`, needs canonical id
- `internet_finance_structural_advantages_capital_allocation` -> type `structural`, confidence `0.45`, needs canonical id
- `internet_finance_is_mechanism_not_telos` -> type `normative/strategic-boundary`, confidence `0.8`, needs canonical id
Source candidates:
- `m3ta_telegram_approval_supersede_split_2026_07_05` -> source_type `dm`, needs dedupe/canonical id
- `kb_existing_hayek_claim_7da4851c` -> source_type `note`, needs dedupe/canonical id
- `hayek_use_of_knowledge_in_society_fee_snapshot` -> source_type `url`, needs dedupe/canonical id
- `rio_belief_capital_allocation_civilizational_infrastructure` -> source_type `note`, needs dedupe/canonical id
- `leo_active_strategy_v2_context` -> source_type `note`, needs dedupe/canonical id
### `ac036c9d-20a0-4ffe-881f-57d6b7bacf22`
- Status: `approved`
- Proposal type: `attach_evidence`
- Direct apply ready: `false`
- Claim rows needing canonical IDs: `3`
- Source rows needing canonical IDs/deduplication: `3`
- Edge fragments blocked until IDs/mapping exist: `6`
- Evidence fragments blocked until claim/source IDs exist: `5`
- Next action: Do not apply directly. Review a claim/source creation contract in a clone, assign/deduplicate canonical IDs, then rerun strict normalization to produce apply_payload child proposals.
Contract gaps:
- `current_apply_role_cannot_insert_claims_or_sources`: A separate reviewed role/contract for public.claims and public.sources creation.
- `claim_candidates_need_canonical_ids`: Create or deduplicate claim rows, then rewrite edges/evidence using UUIDs.
- `public_claims_has_no_body_or_review_note_columns`: Store those details in public.sources excerpts or add reviewed schema support.
- `public_sources_has_limited_metadata_columns`: Deduplicate sources and decide where provenance metadata is retained.
Claim candidates:
- `claim_nodes_need_bodies` -> type `governance/process`, confidence `0.85`, needs canonical id
- `internet_finance_structural_advantages_body_draft` -> type `structural/forecast-like thesis`, confidence `0.45`, needs canonical id
- `hayek_reframed_price_system_distributed_knowledge` -> type `structural/reframing`, confidence `0.8`, needs canonical id
Source candidates:
- `telegram_attachment_teleo_agent_strategy_kernels_2026_06_30` -> source_type `note`, needs dedupe/canonical id
- `telegram_feedback_m3ta_claim_shape_2026_07_05` -> source_type `note`, needs dedupe/canonical id
- `hayek_use_of_knowledge_in_society_fee_snapshot` -> source_type `url`, needs dedupe/canonical id
## Claim Ceiling
This proves the live approved packets can be converted into a row-level contract preview. It does not prove those new claim/source rows should be inserted into canonical production tables without a clone/staging apply rehearsal and a reviewed creation contract.
JSON proof: `/Users/user/Documents/Codex/2026-07-09/019f34eb-d297-72d0-b7e2-b222d5515ab9-load/outputs/claim-source-contract-preview-20260709/claim-source-contract-preview-current.json`
Fetched live proposal bundle: `/Users/user/Documents/Codex/2026-07-09/019f34eb-d297-72d0-b7e2-b222d5515ab9-load/outputs/claim-source-contract-preview-20260709/approved-rich-proposals-live.json`

View file

@ -1,110 +0,0 @@
# Cory Expected Working-Leo Outcomes - 2026-07-09
This is the current working target for restoring Leo. It intentionally scopes out non-Leo route work and keeps the focus on Cory/m3taversal's expected Leo behavior: Telegram-visible reasoning, memory, truthful KB state, and approved KB changes becoming canonical DB state with proof.
## Working Target
A working Leo is a Telegram-reachable agent that:
1. Remembers the ongoing conversation and can use prior turns in the same operator context.
2. Grounds KB answers in canonical Teleo Postgres state, not only in memory or proposal text.
3. Clearly distinguishes `proposed`, `approved`, `applied`, and `not applied`.
4. Does not overclaim that an approval changed the DB when only a proposal ledger changed.
5. When Cory/m3taversal approves a specific concrete KB change, moves that change through a narrow guarded review/apply path into canonical `public.*` rows.
6. Retains Cory's caveats verbatim when an approved change is applied.
7. Produces row-level before/after proof and leaves the live Leo runtime stable.
## What Cory Most Likely Means By "Working"
The best-supported reading is:
- "Able to manipulate the knowledge base" means approved, concrete KB changes can land as real canonical rows, not just text in staging tables.
- "Same state as last night" means Leo is stuck in proposal/approval state and the canonical DB is not moving forward.
- Partner-demo pressure means the path must be visible and reliable through Telegram, not only through local operator scripts.
- Cory may tolerate a more direct write posture than we would design from scratch, but the restored safe behavior should still be auditable: propose, review, apply, read back rows.
## Current Truth
Live behavior is now Cory-working for the restored VPS Telegram memory/KB-audit path, with a clear remaining DB-contract gap:
- The live hosted HTTP Leo chat route still fails the KB-state and memory benchmark.
- The live VPS Telegram-shaped GatewayRunner handler now passes same-session memory and canonical KB audit when launched in the same user/cwd context as the service.
- The handler confirmed proposal `00957f6c-9883-4015-95a4-6b09367efb0e` is `applied` and edge `c167933e-d513-4f43-9335-d5d8aeb259f2` exists in `public.claim_edges`.
- The authenticated Chrome/Telegram Web live group canary now proves the same behavior in the real `Leo` Telegram group: Leo remembered marker `WL-LIVE-TG-CORY-20260709`, preserved `approved is not the same as applied`, and read back the applied proposal plus canonical edge row.
- Old approved/freeform proposals are not safely applyable as-is because they lack strict `payload.apply_payload` and, for `14fa5ecc`, the approved successor claim/source rows do not already exist as canonical IDs.
- Telegram bot/group readiness is proven, and Chrome/Telegram Web is authenticated/read/write-proven through Computer Use for bot testing in the `Leo` group.
Validated lower-tier behavior:
- The VPS service is alive and stable under the live harness.
- Hermes CLI session memory passed a two-turn canary, but that is not the same as Telegram-visible proof.
- One strict KB apply canary mutated canonical DB state without changing Leo runtime.
- The live GatewayRunner handler canary proves Telegram-shaped authorization, same-session memory, and canonical KB audit without posting to Telegram or writing synthetic live-profile sessions.
- A guarded approval helper is installed on VPS and rejects re-approval of an already-applied proposal.
- Approved-proposal normalization confirmed that old freeform approvals need canonical IDs or strict rewrites before apply.
- Claim/source contract preview now converts live approved rich proposals into row-level requirements:
- `14fa5ecc`: 4 claim rows, 5 source rows, 17 edge fragments, 9 evidence fragments
- `ac036c9d`: 3 claim rows, 3 source rows, 6 edge fragments, 5 evidence fragments
- Rollback-only rich proposal creation plan now validates the supported subset against live VPS schema without permanent production mutation:
- `14fa5ecc`: 4 claims, 9 sources, 11 evidence rows, 3 supported edges; 14 blocked fragments remain
- `ac036c9d`: 3 claims, 6 sources, 6 evidence rows, 1 supported edge; 5 blocked fragments remain
- before/after generated-row counts remained zero for claims, sources, claim_evidence, and claim_edges
- Disposable staging DB commit proof now shows the supported subset can commit outside production:
- staging DB after commit: 7 claims, 15 sources, 17 claim_evidence rows, 4 claim_edges
- production after staging commit: 0 generated claims, 0 generated sources, 0 generated claim_evidence rows, 0 generated claim_edges
- staging DB was dropped and temp files were cleaned up
- An isolated local Leo chat candidate passes the KB-state and memory benchmark, but it is not deployed.
## Evidence Index
- Working definition and chat excerpts: `outputs/working-leo-definition-20260709.md`, `outputs/working-leo-chat-excerpts-20260709.md`
- Reviewer convergence: `outputs/fable-working-leo-convergence-20260709/fable-working-leo-convergence-report.md`
- VPS live harness: `outputs/leo-vps-live-harness-20260709/leo-vps-live-harness-current-20260709.md`
- Canonical apply canary: `outputs/kb-apply-canary-20260709/kb-apply-canary-execute-current.md`
- Telegram handler canary: `outputs/telegram-gateway-handler-canary-20260709/telegram-gateway-handler-canary-current.md`
- Live Telegram group canary: `outputs/telegram-live-canary-20260709/telegram-live-canary-current.md`
- Old approval normalization: `outputs/approved-proposal-normalization-20260709/approved-proposal-normalization-current.md`
- Claim/source contract preview: `outputs/claim-source-contract-preview-20260709/claim-source-contract-preview-current.md`
- Rich proposal creation plan: `outputs/rich-proposal-creation-plan-20260709/rich-proposal-creation-plan-current.md`
- Execution plan: `outputs/working-leo-execution-plan-20260709/working-leo-execution-plan-current.md`
- Live chat benchmark failure: `outputs/leo-http-chat-benchmark-20260709/leo-http-chat-benchmark-current.md`
- Local candidate chat benchmark pass: `outputs/leo-http-chat-benchmark-20260709/leo-http-chat-benchmark-http-localhost-3129-api-agents-leo-chat.md`
- Telegram readiness proof: `outputs/telegram-visible-readiness-20260709/telegram-visible-readiness-current.md`
- Telegram access supersession: `outputs/telegram-visible-readiness-20260709/telegram-access-supersession-20260709.md`
- Current state report: `outputs/working-leo-current-state-20260709.md`
- Current state image: `outputs/leo-db-state-06-current-proof.svg`
- Current live proof image: `outputs/leo-db-state-07-live-proof.svg`
- Rich proposal plan image: `outputs/leo-db-state-08-rich-plan.svg`
- Staging commit proof image: `outputs/leo-db-state-09-staging-commit.svg`
- Outcome image: `outputs/working-leo-cory-expected-outcomes.png`
## Benchmark Bar
The benchmark for Cory-working Leo should pass these checks:
1. Telegram visibility: passed in the live `Leo` group.
2. Memory: passed with marker `WL-LIVE-TG-CORY-20260709` and the correction from the prior turn.
3. KB audit: passed for proposal `00957f6c-9883-4015-95a4-6b09367efb0e` and edge `c167933e-d513-4f43-9335-d5d8aeb259f2`.
4. Truthfulness: passed for the tested applied canary and prior approved-vs-applied context.
5. Strict apply: passed for one approved concrete `add_edge` change with before/after row proof.
6. Caveat retention: passed for the strict canary approval note; old freeform caveat retention remains part of the claim/source creation contract.
7. Old approval handling: partially passed; old freeform approvals are now normalized into row-level requirements and explicitly not silently applied.
8. Runtime stability: passed; service stayed active/running with `NRestarts=0`. The latest direct-claim suite ran after the expected skill-change restart and stayed on `MainPID=1908033` throughout the harness.
9. Broad Cory-style outcomes and no-context direct claims: specified in the open-ended benchmark as nine disposable-clone/sandbox-first outcome scenarios plus six direct-claim/follow-up scenarios. The mixed-evidence sandbox score passes `20/20` using retained live Telegram evidence for `OE-01` through `OE-05`, fixture answers for `CS-01` through `CS-09`, and direct-claim expected-answer/follow-up fixtures for `DC-01` through `DC-06`; the CS rows are not live Telegram proof. A live VPS GatewayRunner handler run of exact `DC-01` through `DC-06` now returns replies with no DB/service side effects and strict-scores `6/6`, so no-context direct-claim behavior is handler-proven on VPS.
## Scope Boundaries
- Do not broaden this into parked route work before Leo meets the Cory-working bar.
- Do not introduce new naming tied to the parked lane in new artifacts or harnesses.
- Do not deploy the isolated local candidate until the Leo/Kb/memory behavior is reviewed and the Telegram-visible test path is ready.
- Keep the local chat-route candidate as evidence only for now; do not promote it as the Cory fix while its surrounding route surface is outside the current scope.
- Do not auto-apply old freeform approvals without canonical IDs, strict payloads, and explicit review.
- Do not give Leo broad autonomous DB write authority as part of "restoration"; that would be a feature change.
## Next Actions
1. Keep the live Telegram group canary as the top-level user-visible proof and the handler direct-claim suite as the non-posting regression harness.
2. Keep the direct-claim handler suite green: `DC-02` must recognize approved Helmer staging row `a64df080-8502-42e2-98f4-9bbdecb8da73` as approved/unapplied, and `DC-03` must not infer decision-matrix approval while decision-matrix tables are absent.
3. Do not auto-apply old freeform approvals.
4. For `14fa5ecc`, identify or create canonical claim/source rows before producing strict child proposals; otherwise treat claim/source creation support as a separate guarded contract expansion, preferably in a clone first.
5. Re-review and apply only strict proposals with row-level proof and runtime stability readback.

View file

@ -1,31 +0,0 @@
\set ON_ERROR_STOP on
begin;
do $$
declare
updated int;
begin
if not exists (select 1 from public.claims where id = 'e6ce4e36-15d2-53bc-8b74-dda0efc095c1'::uuid) then
raise exception 'cross-surface packet requires Claim D % to exist before anchor update', 'e6ce4e36-15d2-53bc-8b74-dda0efc095c1';
end if;
if not exists (select 1 from public.sources where id = '9a0d2429-b98b-585a-980c-0176db7f9fe7'::uuid) then
raise exception 'cross-surface packet requires approval source % to exist before anchor update', '9a0d2429-b98b-585a-980c-0176db7f9fe7';
end if;
update public.strategy_node_anchors
set claim_id = 'e6ce4e36-15d2-53bc-8b74-dda0efc095c1'::uuid,
weight = 0.85,
note = 'Mapped rich proposal 14fa5ecc applied: active Leo policy now anchors to Claim D (internet finance is a candidate mechanism, not the telos) instead of the retired compressed internet-finance claim.'
where id = '47f128a0-36c5-4b60-97d4-48c32fa48bc7'::uuid
and from_node_id = '52bc4264-330e-489d-920c-a493fd2d985a'::uuid
and anchor_role = 'justified_by'
and claim_id = 'd0000000-0000-0000-0000-000000000005'::uuid;
get diagnostics updated = row_count;
if updated <> 1 then
raise exception 'cross-surface anchor update affected % row(s), expected 1; anchor was not at old claim state', updated;
end if;
end $$;
commit;

View file

@ -1,314 +0,0 @@
cross-surface rehearsal started UTC 2026-07-09T21:35:41Z
copy packet files into teleo-pg container
drop old disposable DB if present
DROP DATABASE
create disposable DB teleo_cross_surface_rehearsal_20260709 from pg_dump of production teleo
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
clone base preflight before mapped packet commit
id | status | proposal_type | reviewed_by_handle | reviewed_at
--------------------------------------+----------+-----------------+--------------------+-------------------------------
14fa5ecc-ac7a-41c1-807d-a2e85b936617 | approved | attach_evidence | m3ta | 2026-07-05 19:30:48.707181+00
ac036c9d-20a0-4ffe-881f-57d6b7bacf22 | approved | attach_evidence | m3ta | 2026-07-05 19:11:40.673615+00
(2 rows)
check_name | id | reason
------------+----+--------
(0 rows)
expected_claims | expected_sources | expected_claim_edges | expected_claim_evidence
-----------------+------------------+----------------------+-------------------------
7 | 15 | 11 | 17
(1 row)
generated_claim_rows | generated_source_rows | generated_edge_rows | generated_evidence_rows
----------------------+-----------------------+---------------------+-------------------------
0 | 0 | 0 | 0
(1 row)
clone base mapped packet authorized commit
BEGIN
SET
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
COMMIT
BEGIN
SET
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
COMMIT
clone base mapped packet postflight
generated_claim_rows | generated_source_rows | generated_edge_rows | generated_evidence_rows
----------------------+-----------------------+---------------------+-------------------------
7 | 15 | 11 | 17
(1 row)
id | type | confidence | text
--------------------------------------+------------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------
004195cc-ab62-526c-90fb-51cb0f826ec0 | structural | 0.65 | Capital allocation is a form of civilizational steering because it determines which futures receive resources.
62e82ee8-b204-573b-9707-eb5d832b9d9e | structural | 0.45 | Internet finance has structural advantages that make it likely to become an increasingly important capital-allocation mechanism.
67003e10-e1ee-5905-8b51-1b0cb263ec11 | structural | 0.45 | Internet finance has structural advantages over traditional finance and is likely to become an increasingly important capital-allocation mechanism.
9bcf7e6b-4e98-52b1-91ef-96cfc15e3df0 | meta | 0.85 | KB claim nodes need explanatory bodies, not only one-sentence headlines.
cd8daac8-039c-5349-9c50-02f622e73b03 | structural | 0.8 | Hayeks most relevant contribution for Teleo is that prices coordinate dispersed, local, time-sensitive knowledge without central possession of that knowledge.
d009dfc4-9755-58a7-affb-80e3e842a775 | structural | 0.7 | Capital allocation depends on distributed knowledge, incentives, liquidity, and feedback loops.
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | normative | 0.8 | Internet finance is not Teleos telos; it is a candidate mechanism for pursuing the telos.
(7 rows)
id | source_type | excerpt
--------------------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
244a4c38-86e5-5910-91f4-6d71529e9b31 | other | {"body": "The existing KB Hayek claim says economic planning requires local and global information that are never simultaneously available to decision makers. That is directionally related, but the stronger Teleo-relevan
33fadb51-0bd8-5f2d-87fe-758a3dd7fac0 | article | {"excerpt": "There is beyond question a body of very important but unorganized knowledge... the knowledge of the particular circumstances of time and place.\n\nBy the price system... all the relevant information is conce
3a57238c-ca31-5fe1-bece-e5fb2f174e32 | other | {"excerpt": "Capital allocation is civilizational infrastructure, not a financial service.", "metadata": {"source_key": "rio_belief_capital_allocation_civilizational_infrastructure", "source_quality": "canonical agent be
4787c38e-74cb-5bf5-93e1-0cc9c290684c | other | {"excerpt": "Nodes should have a headline thats the claim belief or concept map etc, but also a body elaborating what the claim means.\n\nStandardize these nodes as: Headline, Body, Edges, Evidence.\n\nFor Hayek that is
4cc0c7ee-2166-5cae-9065-ea226fa2be5b | article | {"excerpt": "There is beyond question a body of very important but unorganized knowledge... the knowledge of the particular circumstances of time and place.\n\nBy the price system... all the relevant information is conce
6c3c26c1-39bc-58e5-8c76-28b5712420ce | other | {"body": "Internet finance may improve capital allocation by making ownership, capital formation, settlement, liquidity, governance, and market feedback more digitally native, programmable, global, and composable. The th
703fdfaf-22c9-5f5b-8d29-cf238d5e156f | other | {"excerpt": "# Teleo Collective Telos and Agent Strategy Kernels ... This is a working synthesis, not canonical KB state.\n\nCore Strategic Tools are named and then elaborated with short explanatory bodies: Inflection ma
87f49cf0-5516-5aef-8ed9-f69292917d32 | other | {"excerpt": "Hayek's knowledge problem reveals that economic planning requires both local and global information which are never simultaneously available to decision makers", "metadata": {"claim_id": "7da4851c-0aac-4084-
8f952541-5f12-5f24-88b3-21e7842163a1 | other | {"excerpt": "Humanity is in the early innings of a species-level inflection... changes in how society distributes value, forms capital, and coordinates around shared futures.", "metadata": {"source_key": "leo_active_stra
997e5e58-b20d-560a-a73a-eaa4bff1dc3e | other | {"body": "Teleos telos is emergent collective superintelligence oriented toward better futures, not internet finance for its own sake. Internet finance matters insofar as it can help coordinate capital, incentives, fore
9a0d2429-b98b-585a-980c-0176db7f9fe7 | dm | {"excerpt": "Yes lets do this. do the work for this, then come back to me with what you'd like me to approve\n\nOld claim: Internet finance is the coordination mechanism for capital allocation — capital allocation is ex
9fd61e7e-d5cb-5904-b5c2-a539946f7c96 | other | {"body": "Good capital allocation requires more than a central thesis about what matters. It needs distributed knowledge about local opportunities, credible incentives that reward truth-seeking and execution, liquidity o
a3cba49b-7016-5f42-9af6-041f12371a24 | other | {"body": "A one-sentence claim can be useful as a handle, but it often under-specifies scope, mechanism, caveats, and what evidence would actually support it. Bodies make claims reviewable by explaining intended meaning
eea07732-c65d-5db6-88d6-1ab08511eca6 | other | {"body": "This does not mean “crypto wins because crypto.” It means digitally native capital formation, programmable governance, global distribution, lower intermediation costs, faster liquidity, composable ownership, an
f80c01fe-63bb-5ccf-9bdd-744bab0e9163 | other | {"body": "Capital allocation is not merely financial plumbing. It decides which companies, technologies, infrastructures, institutions, and narratives get enough resources to become real. Because many possible futures co
(15 rows)
from_claim | to_claim | edge_type
--------------------------------------+--------------------------------------+--------------
004195cc-ab62-526c-90fb-51cb0f826ec0 | d0000000-0000-0000-0000-000000000005 | supersedes
62e82ee8-b204-573b-9707-eb5d832b9d9e | c0000000-0000-0000-0000-00000000000c | relates
62e82ee8-b204-573b-9707-eb5d832b9d9e | cd8daac8-039c-5349-9c50-02f622e73b03 | derives_from
67003e10-e1ee-5905-8b51-1b0cb263ec11 | d0000000-0000-0000-0000-000000000005 | supersedes
67003e10-e1ee-5905-8b51-1b0cb263ec11 | d009dfc4-9755-58a7-affb-80e3e842a775 | requires
cd8daac8-039c-5349-9c50-02f622e73b03 | 62e82ee8-b204-573b-9707-eb5d832b9d9e | supports
cd8daac8-039c-5349-9c50-02f622e73b03 | 7da4851c-0aac-4084-a99f-baeeeb3aa23e | supersedes
d009dfc4-9755-58a7-affb-80e3e842a775 | 7da4851c-0aac-4084-a99f-baeeeb3aa23e | relates
d009dfc4-9755-58a7-affb-80e3e842a775 | d0000000-0000-0000-0000-000000000005 | supersedes
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | 67003e10-e1ee-5905-8b51-1b0cb263ec11 | constrains
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | d0000000-0000-0000-0000-000000000005 | supersedes
(11 rows)
claim_id | source_id | role
--------------------------------------+--------------------------------------+-------------
004195cc-ab62-526c-90fb-51cb0f826ec0 | 3a57238c-ca31-5fe1-bece-e5fb2f174e32 | grounds
004195cc-ab62-526c-90fb-51cb0f826ec0 | 8f952541-5f12-5f24-88b3-21e7842163a1 | grounds
004195cc-ab62-526c-90fb-51cb0f826ec0 | 9a0d2429-b98b-585a-980c-0176db7f9fe7 | grounds
004195cc-ab62-526c-90fb-51cb0f826ec0 | f80c01fe-63bb-5ccf-9bdd-744bab0e9163 | illustrates
62e82ee8-b204-573b-9707-eb5d832b9d9e | 4787c38e-74cb-5bf5-93e1-0cc9c290684c | grounds
62e82ee8-b204-573b-9707-eb5d832b9d9e | eea07732-c65d-5db6-88d6-1ab08511eca6 | illustrates
67003e10-e1ee-5905-8b51-1b0cb263ec11 | 6c3c26c1-39bc-58e5-8c76-28b5712420ce | illustrates
67003e10-e1ee-5905-8b51-1b0cb263ec11 | 9a0d2429-b98b-585a-980c-0176db7f9fe7 | grounds
9bcf7e6b-4e98-52b1-91ef-96cfc15e3df0 | 4787c38e-74cb-5bf5-93e1-0cc9c290684c | grounds
9bcf7e6b-4e98-52b1-91ef-96cfc15e3df0 | a3cba49b-7016-5f42-9af6-041f12371a24 | illustrates
cd8daac8-039c-5349-9c50-02f622e73b03 | 244a4c38-86e5-5910-91f4-6d71529e9b31 | illustrates
cd8daac8-039c-5349-9c50-02f622e73b03 | 4cc0c7ee-2166-5cae-9065-ea226fa2be5b | grounds
d009dfc4-9755-58a7-affb-80e3e842a775 | 33fadb51-0bd8-5f2d-87fe-758a3dd7fac0 | grounds
d009dfc4-9755-58a7-affb-80e3e842a775 | 9fd61e7e-d5cb-5904-b5c2-a539946f7c96 | illustrates
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | 8f952541-5f12-5f24-88b3-21e7842163a1 | grounds
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | 997e5e58-b20d-560a-a73a-eaa4bff1dc3e | illustrates
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | 9a0d2429-b98b-585a-980c-0176db7f9fe7 | grounds
(17 rows)
id | superseded_by
--------------------------------------+--------------------------------------
7da4851c-0aac-4084-a99f-baeeeb3aa23e | cd8daac8-039c-5349-9c50-02f622e73b03
d0000000-0000-0000-0000-000000000005 | e6ce4e36-15d2-53bc-8b74-dda0efc095c1
(2 rows)
clone cross-surface preflight after base packet
check_name | id | from_node_id | anchor_role | claim_id | source_id | note
-----------------------+--------------------------------------+--------------------------------------+--------------+--------------------------------------+-----------+----------------------------------------------------------------------------------------------------------------------------------------
policy_anchor_current | 47f128a0-36c5-4b60-97d4-48c32fa48bc7 | 52bc4264-330e-489d-920c-a493fd2d985a | justified_by | d0000000-0000-0000-0000-000000000005 | | Telos/policy grounding approved by m3ta: capital allocation expresses priorities and must be coordinated with collective intelligence.
(1 row)
check_name | id | status | text
---------------------+--------------------------------------+--------+--------------------------------------------------------------------------------------------
base_claim_d_exists | e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | open | Internet finance is not Teleos telos; it is a candidate mechanism for pursuing the telos.
(1 row)
check_name | id | source_type | excerpt
------------------------+--------------------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
approval_source_exists | 9a0d2429-b98b-585a-980c-0176db7f9fe7 | dm | {"excerpt": "Yes lets do this. do the work for this, then come back to me with what you'd like me to approve\n\nOld claim: Internet finance is the coordination mechanism for capital allocation — capital allocation is ex
(1 row)
check_name | id | from_node_id | anchor_role | to_shared_root_id | note
-----------------------------+--------------------------------------+--------------------------------------+-------------+--------------------------------------+---------------------------------------------------------------------------------------
telos_anchor_already_exists | c7dd984c-ac1b-43f9-9996-576ddf61c6d2 | 52bc4264-330e-489d-920c-a493fd2d985a | serves | 13100000-0000-0000-0000-000000000001 | Approved by m3ta: policy serves Teleo telos of emergent collective superintelligence.
(1 row)
check_name | id | name | criteria | evidence_bar
-----------------------+--------------------------------------+--------------+------------------------------------------------------------------------------------------------------------+-------------------------------------
governance_gate_known | 45695897-2a9c-4279-ac25-e59f166dec6a | evidence-bar | likely needs empirical data; experimental = coherent argument, limited validation; speculative = untested. | likely > experimental > speculative
governance_gate_known | fa88e31b-6fca-4801-bf72-f5db81950a11 | quality-gate | Specific enough to disagree with; evidence traceable; not a duplicate; right domain reviewers. |
(2 rows)
clone cross-surface authorized commit
BEGIN
DO
COMMIT
clone cross-surface postflight after anchor update
id | from_node_id | anchor_role | claim_id | source_id | weight | note
--------------------------------------+--------------------------------------+--------------+--------------------------------------+-----------+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
47f128a0-36c5-4b60-97d4-48c32fa48bc7 | 52bc4264-330e-489d-920c-a493fd2d985a | justified_by | e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | | 0.85 | Mapped rich proposal 14fa5ecc applied: active Leo policy now anchors to Claim D (internet finance is a candidate mechanism, not the telos) instead of the retired compressed internet-finance claim.
(1 row)
id | status | superseded_by | text
--------------------------------------+--------+--------------------------------------+---------------------------------------------------------------------------------------------------------------------
d0000000-0000-0000-0000-000000000005 | open | e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | Internet finance is the coordination mechanism for capital allocation — capital allocation is expressed priorities.
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | open | | Internet finance is not Teleos telos; it is a candidate mechanism for pursuing the telos.
(2 rows)
id | anchor_role | to_shared_root_id | note
--------------------------------------+-------------+--------------------------------------+---------------------------------------------------------------------------------------
c7dd984c-ac1b-43f9-9996-576ddf61c6d2 | serves | 13100000-0000-0000-0000-000000000001 | Approved by m3ta: policy serves Teleo telos of emergent collective superintelligence.
(1 row)
clone direct anchor assertion after cross-surface commit
check_name | claim_id | source_id | weight | note
--------------------------+--------------------------------------+-----------+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------
clone_anchor_after_cross | e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | | 0.85 | Mapped rich proposal 14fa5ecc applied: active Leo policy now anchors to Claim D (internet finance is a candidate mechanism, not the telos) instead of the retire
(1 row)
clone cross-surface delete rollback
BEGIN
DO
COMMIT
clone base mapped packet delete rollback
BEGIN
DELETE 17
DELETE 11
UPDATE 2
DELETE 15
DELETE 7
COMMIT
clone base preflight after both rollbacks
id | status | proposal_type | reviewed_by_handle | reviewed_at
--------------------------------------+----------+-----------------+--------------------+-------------------------------
14fa5ecc-ac7a-41c1-807d-a2e85b936617 | approved | attach_evidence | m3ta | 2026-07-05 19:30:48.707181+00
ac036c9d-20a0-4ffe-881f-57d6b7bacf22 | approved | attach_evidence | m3ta | 2026-07-05 19:11:40.673615+00
(2 rows)
check_name | id | reason
------------+----+--------
(0 rows)
expected_claims | expected_sources | expected_claim_edges | expected_claim_evidence
-----------------+------------------+----------------------+-------------------------
7 | 15 | 11 | 17
(1 row)
generated_claim_rows | generated_source_rows | generated_edge_rows | generated_evidence_rows
----------------------+-----------------------+---------------------+-------------------------
0 | 0 | 0 | 0
(1 row)
clone direct anchor assertion after both rollbacks
check_name | claim_id | source_id | weight | note
-----------------------------+--------------------------------------+-----------+--------+----------------------------------------------------------------------------------------------------------------------------------------
clone_anchor_after_rollback | d0000000-0000-0000-0000-000000000005 | | 0.7 | Telos/policy grounding approved by m3ta: capital allocation expresses priorities and must be coordinated with collective intelligence.
(1 row)
production remains unchanged: base generated counts and anchor
id | status | proposal_type | reviewed_by_handle | reviewed_at
--------------------------------------+----------+-----------------+--------------------+-------------------------------
14fa5ecc-ac7a-41c1-807d-a2e85b936617 | approved | attach_evidence | m3ta | 2026-07-05 19:30:48.707181+00
ac036c9d-20a0-4ffe-881f-57d6b7bacf22 | approved | attach_evidence | m3ta | 2026-07-05 19:11:40.673615+00
(2 rows)
check_name | id | reason
------------+----+--------
(0 rows)
expected_claims | expected_sources | expected_claim_edges | expected_claim_evidence
-----------------+------------------+----------------------+-------------------------
7 | 15 | 11 | 17
(1 row)
generated_claim_rows | generated_source_rows | generated_edge_rows | generated_evidence_rows
----------------------+-----------------------+---------------------+-------------------------
0 | 0 | 0 | 0
(1 row)
check_name | claim_id | source_id | weight | note
-------------------------+--------------------------------------+-----------+--------+----------------------------------------------------------------------------------------------------------------------------------------
production_anchor_state | d0000000-0000-0000-0000-000000000005 | | 0.7 | Telos/policy grounding approved by m3ta: capital allocation expresses priorities and must be coordinated with collective intelligence.
(1 row)
drop disposable DB and remove temp packet files
DROP DATABASE
cleanup readback
disposable_db_exists
----------------------
0
(1 row)
host_tmp_removed yes
container_tmp_removed yes
active
3252143
0
cross-surface rehearsal completed UTC 2026-07-09T21:35:46Z

View file

@ -1,210 +0,0 @@
cross-surface rehearsal started UTC 2026-07-09T21:28:54Z
copy packet files into teleo-pg container
drop old disposable DB if present
DROP DATABASE
create disposable DB teleo_cross_surface_rehearsal_20260709 from pg_dump of production teleo
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
clone base preflight before mapped packet commit
id | status | proposal_type | reviewed_by_handle | reviewed_at
--------------------------------------+----------+-----------------+--------------------+-------------------------------
14fa5ecc-ac7a-41c1-807d-a2e85b936617 | approved | attach_evidence | m3ta | 2026-07-05 19:30:48.707181+00
ac036c9d-20a0-4ffe-881f-57d6b7bacf22 | approved | attach_evidence | m3ta | 2026-07-05 19:11:40.673615+00
(2 rows)
check_name | id | reason
------------+----+--------
(0 rows)
expected_claims | expected_sources | expected_claim_edges | expected_claim_evidence
-----------------+------------------+----------------------+-------------------------
7 | 15 | 11 | 17
(1 row)
generated_claim_rows | generated_source_rows | generated_edge_rows | generated_evidence_rows
----------------------+-----------------------+---------------------+-------------------------
0 | 0 | 0 | 0
(1 row)
clone base mapped packet authorized commit
BEGIN
SET
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
COMMIT
BEGIN
SET
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
COMMIT
clone base mapped packet postflight
generated_claim_rows | generated_source_rows | generated_edge_rows | generated_evidence_rows
----------------------+-----------------------+---------------------+-------------------------
7 | 15 | 11 | 17
(1 row)
id | type | confidence | text
--------------------------------------+------------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------
004195cc-ab62-526c-90fb-51cb0f826ec0 | structural | 0.65 | Capital allocation is a form of civilizational steering because it determines which futures receive resources.
62e82ee8-b204-573b-9707-eb5d832b9d9e | structural | 0.45 | Internet finance has structural advantages that make it likely to become an increasingly important capital-allocation mechanism.
67003e10-e1ee-5905-8b51-1b0cb263ec11 | structural | 0.45 | Internet finance has structural advantages over traditional finance and is likely to become an increasingly important capital-allocation mechanism.
9bcf7e6b-4e98-52b1-91ef-96cfc15e3df0 | meta | 0.85 | KB claim nodes need explanatory bodies, not only one-sentence headlines.
cd8daac8-039c-5349-9c50-02f622e73b03 | structural | 0.8 | Hayeks most relevant contribution for Teleo is that prices coordinate dispersed, local, time-sensitive knowledge without central possession of that knowledge.
d009dfc4-9755-58a7-affb-80e3e842a775 | structural | 0.7 | Capital allocation depends on distributed knowledge, incentives, liquidity, and feedback loops.
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | normative | 0.8 | Internet finance is not Teleos telos; it is a candidate mechanism for pursuing the telos.
(7 rows)
id | source_type | excerpt
--------------------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
244a4c38-86e5-5910-91f4-6d71529e9b31 | other | {"body": "The existing KB Hayek claim says economic planning requires local and global information that are never simultaneously available to decision makers. That is directionally related, but the stronger Teleo-relevan
33fadb51-0bd8-5f2d-87fe-758a3dd7fac0 | article | {"excerpt": "There is beyond question a body of very important but unorganized knowledge... the knowledge of the particular circumstances of time and place.\n\nBy the price system... all the relevant information is conce
3a57238c-ca31-5fe1-bece-e5fb2f174e32 | other | {"excerpt": "Capital allocation is civilizational infrastructure, not a financial service.", "metadata": {"source_key": "rio_belief_capital_allocation_civilizational_infrastructure", "source_quality": "canonical agent be
4787c38e-74cb-5bf5-93e1-0cc9c290684c | other | {"excerpt": "Nodes should have a headline thats the claim belief or concept map etc, but also a body elaborating what the claim means.\n\nStandardize these nodes as: Headline, Body, Edges, Evidence.\n\nFor Hayek that is
4cc0c7ee-2166-5cae-9065-ea226fa2be5b | article | {"excerpt": "There is beyond question a body of very important but unorganized knowledge... the knowledge of the particular circumstances of time and place.\n\nBy the price system... all the relevant information is conce
6c3c26c1-39bc-58e5-8c76-28b5712420ce | other | {"body": "Internet finance may improve capital allocation by making ownership, capital formation, settlement, liquidity, governance, and market feedback more digitally native, programmable, global, and composable. The th
703fdfaf-22c9-5f5b-8d29-cf238d5e156f | other | {"excerpt": "# Teleo Collective Telos and Agent Strategy Kernels ... This is a working synthesis, not canonical KB state.\n\nCore Strategic Tools are named and then elaborated with short explanatory bodies: Inflection ma
87f49cf0-5516-5aef-8ed9-f69292917d32 | other | {"excerpt": "Hayek's knowledge problem reveals that economic planning requires both local and global information which are never simultaneously available to decision makers", "metadata": {"claim_id": "7da4851c-0aac-4084-
8f952541-5f12-5f24-88b3-21e7842163a1 | other | {"excerpt": "Humanity is in the early innings of a species-level inflection... changes in how society distributes value, forms capital, and coordinates around shared futures.", "metadata": {"source_key": "leo_active_stra
997e5e58-b20d-560a-a73a-eaa4bff1dc3e | other | {"body": "Teleos telos is emergent collective superintelligence oriented toward better futures, not internet finance for its own sake. Internet finance matters insofar as it can help coordinate capital, incentives, fore
9a0d2429-b98b-585a-980c-0176db7f9fe7 | dm | {"excerpt": "Yes lets do this. do the work for this, then come back to me with what you'd like me to approve\n\nOld claim: Internet finance is the coordination mechanism for capital allocation — capital allocation is ex
9fd61e7e-d5cb-5904-b5c2-a539946f7c96 | other | {"body": "Good capital allocation requires more than a central thesis about what matters. It needs distributed knowledge about local opportunities, credible incentives that reward truth-seeking and execution, liquidity o
a3cba49b-7016-5f42-9af6-041f12371a24 | other | {"body": "A one-sentence claim can be useful as a handle, but it often under-specifies scope, mechanism, caveats, and what evidence would actually support it. Bodies make claims reviewable by explaining intended meaning
eea07732-c65d-5db6-88d6-1ab08511eca6 | other | {"body": "This does not mean “crypto wins because crypto.” It means digitally native capital formation, programmable governance, global distribution, lower intermediation costs, faster liquidity, composable ownership, an
f80c01fe-63bb-5ccf-9bdd-744bab0e9163 | other | {"body": "Capital allocation is not merely financial plumbing. It decides which companies, technologies, infrastructures, institutions, and narratives get enough resources to become real. Because many possible futures co
(15 rows)
from_claim | to_claim | edge_type
--------------------------------------+--------------------------------------+--------------
004195cc-ab62-526c-90fb-51cb0f826ec0 | d0000000-0000-0000-0000-000000000005 | supersedes
62e82ee8-b204-573b-9707-eb5d832b9d9e | c0000000-0000-0000-0000-00000000000c | relates
62e82ee8-b204-573b-9707-eb5d832b9d9e | cd8daac8-039c-5349-9c50-02f622e73b03 | derives_from
67003e10-e1ee-5905-8b51-1b0cb263ec11 | d0000000-0000-0000-0000-000000000005 | supersedes
67003e10-e1ee-5905-8b51-1b0cb263ec11 | d009dfc4-9755-58a7-affb-80e3e842a775 | requires
cd8daac8-039c-5349-9c50-02f622e73b03 | 62e82ee8-b204-573b-9707-eb5d832b9d9e | supports
cd8daac8-039c-5349-9c50-02f622e73b03 | 7da4851c-0aac-4084-a99f-baeeeb3aa23e | supersedes
d009dfc4-9755-58a7-affb-80e3e842a775 | 7da4851c-0aac-4084-a99f-baeeeb3aa23e | relates
d009dfc4-9755-58a7-affb-80e3e842a775 | d0000000-0000-0000-0000-000000000005 | supersedes
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | 67003e10-e1ee-5905-8b51-1b0cb263ec11 | constrains
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | d0000000-0000-0000-0000-000000000005 | supersedes
(11 rows)
claim_id | source_id | role
--------------------------------------+--------------------------------------+-------------
004195cc-ab62-526c-90fb-51cb0f826ec0 | 3a57238c-ca31-5fe1-bece-e5fb2f174e32 | grounds
004195cc-ab62-526c-90fb-51cb0f826ec0 | 8f952541-5f12-5f24-88b3-21e7842163a1 | grounds
004195cc-ab62-526c-90fb-51cb0f826ec0 | 9a0d2429-b98b-585a-980c-0176db7f9fe7 | grounds
004195cc-ab62-526c-90fb-51cb0f826ec0 | f80c01fe-63bb-5ccf-9bdd-744bab0e9163 | illustrates
62e82ee8-b204-573b-9707-eb5d832b9d9e | 4787c38e-74cb-5bf5-93e1-0cc9c290684c | grounds
62e82ee8-b204-573b-9707-eb5d832b9d9e | eea07732-c65d-5db6-88d6-1ab08511eca6 | illustrates
67003e10-e1ee-5905-8b51-1b0cb263ec11 | 6c3c26c1-39bc-58e5-8c76-28b5712420ce | illustrates
67003e10-e1ee-5905-8b51-1b0cb263ec11 | 9a0d2429-b98b-585a-980c-0176db7f9fe7 | grounds
9bcf7e6b-4e98-52b1-91ef-96cfc15e3df0 | 4787c38e-74cb-5bf5-93e1-0cc9c290684c | grounds
9bcf7e6b-4e98-52b1-91ef-96cfc15e3df0 | a3cba49b-7016-5f42-9af6-041f12371a24 | illustrates
cd8daac8-039c-5349-9c50-02f622e73b03 | 244a4c38-86e5-5910-91f4-6d71529e9b31 | illustrates
cd8daac8-039c-5349-9c50-02f622e73b03 | 4cc0c7ee-2166-5cae-9065-ea226fa2be5b | grounds
d009dfc4-9755-58a7-affb-80e3e842a775 | 33fadb51-0bd8-5f2d-87fe-758a3dd7fac0 | grounds
d009dfc4-9755-58a7-affb-80e3e842a775 | 9fd61e7e-d5cb-5904-b5c2-a539946f7c96 | illustrates
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | 8f952541-5f12-5f24-88b3-21e7842163a1 | grounds
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | 997e5e58-b20d-560a-a73a-eaa4bff1dc3e | illustrates
e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | 9a0d2429-b98b-585a-980c-0176db7f9fe7 | grounds
(17 rows)
id | superseded_by
--------------------------------------+--------------------------------------
7da4851c-0aac-4084-a99f-baeeeb3aa23e | cd8daac8-039c-5349-9c50-02f622e73b03
d0000000-0000-0000-0000-000000000005 | e6ce4e36-15d2-53bc-8b74-dda0efc095c1
(2 rows)
clone cross-surface preflight after base packet
check_name | id | from_node_id | anchor_role | claim_id | source_id | note
-----------------------+--------------------------------------+--------------------------------------+--------------+--------------------------------------+-----------+----------------------------------------------------------------------------------------------------------------------------------------
policy_anchor_current | 47f128a0-36c5-4b60-97d4-48c32fa48bc7 | 52bc4264-330e-489d-920c-a493fd2d985a | justified_by | d0000000-0000-0000-0000-000000000005 | | Telos/policy grounding approved by m3ta: capital allocation expresses priorities and must be coordinated with collective intelligence.
(1 row)
check_name | id | status | text
---------------------+--------------------------------------+--------+--------------------------------------------------------------------------------------------
base_claim_d_exists | e6ce4e36-15d2-53bc-8b74-dda0efc095c1 | open | Internet finance is not Teleos telos; it is a candidate mechanism for pursuing the telos.
(1 row)
check_name | id | source_type | excerpt
------------------------+--------------------------------------+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
approval_source_exists | 9a0d2429-b98b-585a-980c-0176db7f9fe7 | dm | {"excerpt": "Yes lets do this. do the work for this, then come back to me with what you'd like me to approve\n\nOld claim: Internet finance is the coordination mechanism for capital allocation — capital allocation is ex
(1 row)
check_name | id | from_node_id | anchor_role | to_shared_root_id | note
-----------------------------+--------------------------------------+--------------------------------------+-------------+--------------------------------------+---------------------------------------------------------------------------------------
telos_anchor_already_exists | c7dd984c-ac1b-43f9-9996-576ddf61c6d2 | 52bc4264-330e-489d-920c-a493fd2d985a | serves | 13100000-0000-0000-0000-000000000001 | Approved by m3ta: policy serves Teleo telos of emergent collective superintelligence.
(1 row)
check_name | id | name | criteria | evidence_bar
-----------------------+--------------------------------------+--------------+------------------------------------------------------------------------------------------------------------+-------------------------------------
governance_gate_known | 45695897-2a9c-4279-ac25-e59f166dec6a | evidence-bar | likely needs empirical data; experimental = coherent argument, limited validation; speculative = untested. | likely > experimental > speculative
governance_gate_known | fa88e31b-6fca-4801-bf72-f5db81950a11 | quality-gate | Specific enough to disagree with; evidence traceable; not a duplicate; right domain reviewers. |
(2 rows)
clone cross-surface authorized commit
BEGIN

View file

@ -1,24 +0,0 @@
\set ON_ERROR_STOP on
begin;
do $$
declare
restored int;
begin
update public.strategy_node_anchors
set claim_id = 'd0000000-0000-0000-0000-000000000005'::uuid,
source_id = null,
weight = 0.7,
note = 'Telos/policy grounding approved by m3ta: capital allocation expresses priorities and must be coordinated with collective intelligence.'
where id = '47f128a0-36c5-4b60-97d4-48c32fa48bc7'::uuid
and from_node_id = '52bc4264-330e-489d-920c-a493fd2d985a'::uuid
and anchor_role = 'justified_by'
and claim_id = 'e6ce4e36-15d2-53bc-8b74-dda0efc095c1'::uuid;
get diagnostics restored = row_count;
if restored <> 1 then
raise exception 'cross-surface anchor rollback affected % row(s), expected 1; anchor was not at Claim D state', restored;
end if;
end $$;
commit;

View file

@ -1,55 +0,0 @@
{
"already_represented": [
{
"id": "c7dd984c-ac1b-43f9-9996-576ddf61c6d2",
"reason": "The active Leo policy already serves the Teleo telos shared root; the Claim D anchor update keeps internet finance subordinate to that telos.",
"table": "public.strategy_node_anchors",
"target": "teleo telos",
"to_shared_root_id": "13100000-0000-0000-0000-000000000001"
}
],
"base_packet_required_first": "production-apply-packet-mapped-20260709",
"known_non_writable_targets": [
{
"existing_row_id": "fa88e31b-6fca-4801-bf72-f5db81950a11",
"reason": "Governance gate exists, but current schema has no claim-to-governance-gate edge/anchor table.",
"target": "governance_gate:quality-gate"
},
{
"existing_row_id": "45695897-2a9c-4279-ac25-e59f166dec6a",
"reason": "Governance gate exists, but current schema has no claim-to-governance-gate edge/anchor table.",
"target": "governance_gate:evidence-bar"
},
{
"existing_row_id": null,
"reason": "No first-class concept-map table exists in the current live schema.",
"target": "concept_map:distributed_market_intelligence"
},
{
"existing_row_id": null,
"reason": "No exact Rio strategy node or claim target exists in the current live schema.",
"target": "rio:capital-deployment-layer"
},
{
"existing_row_id": null,
"reason": "No exact Rio active strategy node exists in the current live schema.",
"target": "rio:active strategy v1 / internet-finance capital product"
}
],
"packet_version": 1,
"production_apply_status": "not_executed",
"purpose": "Companion packet for mapped rich proposal non-claim surfaces.",
"safe_writes": [
{
"approval_source_id": "9a0d2429-b98b-585a-980c-0176db7f9fe7",
"from_node_id": "52bc4264-330e-489d-920c-a493fd2d985a",
"from_node_title": "Multi-agent public intelligence",
"id": "47f128a0-36c5-4b60-97d4-48c32fa48bc7",
"new_claim_id": "e6ce4e36-15d2-53bc-8b74-dda0efc095c1",
"old_claim_id": "d0000000-0000-0000-0000-000000000005",
"operation": "update",
"reason": "Replace the active Leo policy anchor that points at the retiring compressed internet-finance claim with the approved Claim D boundary claim.",
"table": "public.strategy_node_anchors"
}
]
}

View file

@ -1,30 +0,0 @@
\set ON_ERROR_STOP on
-- Cross-surface postflight row-level readback.
select id::text,
from_node_id::text,
anchor_role,
claim_id::text,
source_id::text,
weight,
left(note, 240) as note
from public.strategy_node_anchors
where id = '47f128a0-36c5-4b60-97d4-48c32fa48bc7'::uuid;
select c.id::text,
c.status,
c.superseded_by::text,
left(c.text, 240) as text
from public.claims c
where c.id in (select id from (values
('d0000000-0000-0000-0000-000000000005'::uuid),
('e6ce4e36-15d2-53bc-8b74-dda0efc095c1'::uuid)
) as v(id))
order by c.id;
select id::text,
anchor_role,
to_shared_root_id::text,
left(note, 240) as note
from public.strategy_node_anchors
where id = 'c7dd984c-ac1b-43f9-9996-576ddf61c6d2'::uuid;

Some files were not shown because too many files have changed in this diff Show more