Skip to content

"Vibe-Trading: Your Personal Trading Agent"

feat(scheduled-research): guard system jobs from user mutation

shadowinlife566bb732e785
diff --git a/agent/cli/commands/research_playbook.py b/agent/cli/commands/research_playbook.py
index 99a9aa4be..1c2adbc33 100644
--- a/agent/cli/commands/research_playbook.py
+++ b/agent/cli/commands/research_playbook.py
@@ -49,6 +49,11 @@ _HELP_TOKENS = frozenset({"help", "-h", "--help", "?"})
_LIST_TOKENS = frozenset({"list", "ls", "all"})
_VAR_TOKEN_RE = re.compile(r"^([A-Za-z0-9_]+)=(.*)$")
+# Mirrors ``_SAFE_JOB_ID_RE`` in src/api/scheduled_routes.py, which the REST
+# playbook route enforces on a caller-supplied id. The CLI writes the store
+# directly and bypasses every route, so the same rule has to be enforced here.
+_SAFE_JOB_ID_RE = re.compile(r"^[A-Za-z0-9_-]{1,128}$")
+
# Defensive ceilings. ``ResearchPlaybook.render`` already caps a single
# substituted value at 4000 chars; these bound the *number* of tokens and the
# raw argument string so a paste accident cannot make the CLI do silly work
@@ -192,6 +197,19 @@ def build_job_from_playbook(
"""
from src.scheduled_research.playbooks import build_job
+ if job_id:
+ from src.scheduling.system_jobs import is_system_job_id
+
+ if is_system_job_id(job_id):
+ return None, (
+ f"job id {job_id!r} is reserved for protected system jobs; "
+ "the 'system:' prefix cannot be authored"
+ )
+ if not _SAFE_JOB_ID_RE.fullmatch(job_id):
+ return None, (
+ "job id must be 1-128 characters of letters, digits, '_' or '-'"
+ )
+
kwargs: Dict[str, Any] = {
"variables": variables or {},
"delivery_target_ref": delivery_target_ref,
diff --git a/agent/src/api/scheduled_routes.py b/agent/src/api/scheduled_routes.py
index 233b786fd..f99054dca 100644
--- a/agent/src/api/scheduled_routes.py
+++ b/agent/src/api/scheduled_routes.py
@@ -684,6 +684,8 @@ def register_scheduled_routes(
validate_timezone_shape,
)
+ from src.scheduling.system_jobs import is_system_job_id
+
_host_validate_path_param(job_id, "job_id")
store = _get_scheduled_research_store()
job = store.get(job_id)
@@ -691,6 +693,15 @@ def register_scheduled_routes(
raise HTTPException(
status_code=404, detail=f"scheduled run {job_id} not found"
)
+ # Defense in depth for non-REST callers (CLI, internal services,
+ # future entry points). UNREACHABLE via REST: every system job id
+ # carries ':' (SYSTEM_JOB_ID_PREFIX), which _validate_path_param
+ # above rejects with a 400 before the store is even read — the
+ # REST-visible behavior for a system job is that 400, not this 403.
+ if is_system_job_id(job.id) or job.source_type == "system":
+ raise HTTPException(
+ status_code=403, detail="system jobs cannot be modified"
+ )
if job.status == JobStatus.RUNNING or job.delivery.status == DeliveryStatus.SENDING:
raise HTTPException(
status_code=409,
@@ -863,7 +874,18 @@ def register_scheduled_routes(
)
async def delete_scheduled_run(job_id: str) -> Response:
"""Cancel (delete) a scheduled research job by id."""
+ from src.scheduling.system_jobs import is_system_job_id
+
_host_validate_path_param(job_id, "job_id")
+ # Defense in depth for non-REST callers (CLI, internal services,
+ # future entry points). UNREACHABLE via REST: every system job id
+ # carries ':' (SYSTEM_JOB_ID_PREFIX), which _validate_path_param
+ # above rejects with a 400 before this guard runs — the REST-visible
+ # behavior for a system job is that path-parameter 400, not this 403.
+ if is_system_job_id(job_id):
+ raise HTTPException(
+ status_code=403, detail="system jobs cannot be deleted"
+ )
removed = _get_scheduled_research_store().delete(job_id)
if not removed:
raise HTTPException(
diff --git a/agent/src/scheduled_research/proposals.py b/agent/src/scheduled_research/proposals.py
index 8748ca73f..552eb2c7b 100644
--- a/agent/src/scheduled_research/proposals.py
+++ b/agent/src/scheduled_research/proposals.py
@@ -19,6 +19,7 @@ from src.scheduled_research.service import (
public_job,
scheduler_status,
)
+from src.scheduling.system_jobs import is_system_job_id
_ID_RE = re.compile(r"^srp_[0-9a-f]{32}$")
_TTL_MS = 15 * 60 * 1000
@@ -85,10 +86,25 @@ def public_proposal(payload: Mapping[str, Any]) -> dict[str, Any]:
}
+def _ensure_not_system_job(job_id: object, source_type: object, operation: str) -> None:
+ """Refuse any proposal that would create, replace or cancel a system job.
+
+ ``system:``-prefixed ids are seeded and repaired only by
+ :func:`src.scheduling.system_jobs.ensure_system_jobs`; no user-facing
+ surface may author, replace or remove them.
+ """
+ if is_system_job_id(job_id) or source_type == "system":
+ raise ProposalError(
+ f"scheduled research job {str(job_id)!r} is a protected system job "
+ f"and cannot be {operation} by a proposal"
+ )
+
+
def propose_create(
draft: Mapping[str, Any], *, session_id: str | None = None
) -> dict[str, Any]:
job = build_job_from_draft(draft, session_id=session_id)
+ _ensure_not_system_job(job.id, job.source_type, "created or replaced")
now = int(time.time() * 1000)
payload = {
"proposal_id": f"srp_{uuid.uuid4().hex}",
@@ -108,6 +124,7 @@ def propose_cancel(job_id: str, *, session_id: str | None = None) -> dict[str, A
job = default_store().get(job_id)
if job is None:
raise ProposalError(f"scheduled research job {job_id!r} was not found")
+ _ensure_not_system_job(job.id, job.source_type, "cancelled")
now = int(time.time() * 1000)
payload = {
"proposal_id": f"srp_{uuid.uuid4().hex}",
@@ -174,6 +191,16 @@ def commit_proposal(proposal_id: str) -> dict[str, Any]:
from src.scheduled_research.models import ScheduledResearchJob
job = ScheduledResearchJob.from_dict(payload["internal_job"])
+ # Authoritative re-check against the live store: proposal files
+ # live under <runtime root>/scheduled_research/proposals/ and can
+ # be hand-written or edited between propose and commit, and the
+ # row on disk may have become a system job in between.
+ _ensure_not_system_job(job.id, job.source_type, "created or replaced")
+ existing = store.get(job.id)
+ if existing is not None:
+ _ensure_not_system_job(
+ existing.id, existing.source_type, "created or replaced"
+ )
if job.protect_pdf:
from src.scheduled_research.service import email_pdf_password_configured
@@ -182,6 +209,10 @@ def commit_proposal(proposal_id: str) -> dict[str, Any]:
store.upsert(job)
payload["committed_job_id"] = job.id
elif payload["operation"] == "cancel":
+ target = store.get(payload["job_id"])
+ if target is not None:
+ _ensure_not_system_job(target.id, target.source_type, "cancelled")
+ _ensure_not_system_job(payload["job_id"], None, "cancelled")
if not store.delete(payload["job_id"]):
raise ProposalError("scheduled research job no longer exists")
payload["committed_job_id"] = payload["job_id"]
diff --git a/agent/src/scheduled_research/service.py b/agent/src/scheduled_research/service.py
index 6e9823dde..867f87353 100644
--- a/agent/src/scheduled_research/service.py
+++ b/agent/src/scheduled_research/service.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+import re
import sys
import time
import uuid
@@ -20,6 +21,13 @@ from src.scheduled_research.models import (
)
from src.scheduled_research.playbooks import get_playbook
from src.scheduled_research.store import ScheduledResearchJobStore
+from src.scheduling.system_jobs import is_system_job_id
+
+# Mirrors ``_SAFE_JOB_ID_RE`` in src/api/scheduled_routes.py (and the
+# ``_SAFE_PATH_PARAM_RE`` the DELETE/PATCH routes enforce on the job id).
+# A draft id outside this rule — including every ``system:``-prefixed id —
+# could be persisted but never addressed, deleted or edited through the API.
+_SAFE_JOB_ID_RE = re.compile(r"^[A-Za-z0-9_-]{1,128}$")
def public_job(job: ScheduledResearchJob) -> dict[str, Any]:
@@ -140,6 +148,17 @@ def build_job_from_draft(
) -> ScheduledResearchJob:
"""Validate the public draft and build a persisted-model job."""
now = int(time.time() * 1000) if now_ms is None else now_ms
+ draft_id = str(draft.get("id") or "") or None
+ if draft_id is not None:
+ if is_system_job_id(draft_id):
+ raise ValueError(
+ f"job id {draft_id!r} is reserved for protected system jobs; "
+ "the 'system:' prefix cannot be authored"
+ )
+ if not _SAFE_JOB_ID_RE.fullmatch(draft_id):
+ raise ValueError(
+ "job id must be 1-128 characters of letters, digits, '_' or '-'"
+ )
title = str(draft.get("title") or "").strip()
if not title:
raise ValueError("title is required")
@@ -218,7 +237,7 @@ def build_job_from_draft(
raise ValueError("PDF protection requested but no PDF password is configured")
return ScheduledResearchJob(
- id=str(draft.get("id") or f"sr-{uuid.uuid4().hex[:12]}"),
+ id=draft_id or f"sr-{uuid.uuid4().hex[:12]}",
title=title,
prompt=prompt,
source_type=source_type,
diff --git a/agent/src/tools/scheduled_research_tool.py b/agent/src/tools/scheduled_research_tool.py
index 51b307fd0..b668a0182 100644
--- a/agent/src/tools/scheduled_research_tool.py
+++ b/agent/src/tools/scheduled_research_tool.py
@@ -40,6 +40,7 @@ class ScheduledResearchTool(BaseTool):
"draft": {
"type": "object",
"description": "Create draft; required for propose_create.",
+ "additionalProperties": False,
"properties": {
"title": {"type": "string"},
"source": {
diff --git a/agent/tests/scheduling/test_system_job_protection.py b/agent/tests/scheduling/test_system_job_protection.py
new file mode 100644
index 000000000..b0e900c42
--- /dev/null
+++ b/agent/tests/scheduling/test_system_job_protection.py
@@ -0,0 +1,594 @@
+"""System-job protection across the six caller-provided-id entry points.
+
+A system job carries an id with the ``system:`` prefix
+(:data:`src.scheduling.system_jobs.SYSTEM_JOB_ID_PREFIX`) and is seeded and
+repaired only by ``ensure_system_jobs``. No user-facing surface may delete,
+modify, create or replace one. The six entries audited here:
+
+1. ``DELETE /scheduled-runs/{job_id}`` — REST-visible behavior is the
+ path-parameter **400** (``_validate_path_param`` rejects the ':' every
+ system id carries BEFORE the handler runs); the handler-level 403 guard is
+ defense in depth for non-REST callers.
+2. ``PATCH /scheduled-runs/{job_id}`` — same 400-before-handler reality.
+3. ``propose_create`` / ``build_job_from_draft`` — a draft id that is
+ ``system:``-prefixed or outside ``_SAFE_JOB_ID_RE`` is refused at propose.
+4. ``commit_proposal`` — the AUTHORITATIVE guard: proposal files live under
+ ``<runtime root>/scheduled_research/proposals/`` and can be hand-written
+ or edited between propose and commit, so commit re-reads the store and
+ refuses any system-job create/replace/cancel with the store untouched.
+5. ``POST /scheduled-runs`` body — the request model has no ``source_type`` /
+ ``executor_kind`` field; escalation keys are ignored and the job lands as
+ a plain prompt job (pinned), and a forged ``system:`` id dies on the
+ create-path id rule (422).
+6. CLI ``vibe-trading playbook create <slug> --id <id>`` — writes the store
+ directly, bypassing every route; the guard mirrors the REST playbook
+ route's safe-id validation.
+
+Normal-job behavior is asserted byte-identical throughout: the 404 detail
+text ``scheduled run <id> not found``, the truly-empty 204 delete body, and
+the playbook create contract (``source_type == "playbook"``).
+"""
+
+from __future__ import annotations
+
+import json
+import time
+import uuid
+from pathlib import Path
+from typing import List
+
+import pytest
+from fastapi.testclient import TestClient
+
+import api_server
+from cli.commands.research_playbook import build_job_from_playbook
+from src.api import scheduled_routes
+from src.scheduled_research import proposals
+from src.scheduled_research.models import JobStatus, ScheduledResearchJob
+from src.scheduled_research.proposals import ProposalError
+from src.scheduled_research.store import ScheduledResearchJobStore
+from src.scheduling.system_jobs import SYSTEM_JOB_ID_PREFIX, is_system_job_id
+
+SYSTEM_ID = f"{SYSTEM_JOB_ID_PREFIX}memory-consolidation"
+SAMPLE_SLUG = "premarket-brief"
+
+
+# ---------------------------------------------------------------------------
+# Fixtures and helpers
+# ---------------------------------------------------------------------------
+
+
[email protected](autouse=True)
+def _hermetic_catalogue(monkeypatch: pytest.MonkeyPatch) -> None:
+ """Ignore any user playbook directory on the developer's machine."""
+ monkeypatch.delenv("VIBE_TRADING_PLAYBOOK_DIR", raising=False)
+
+
+def store(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> ScheduledResearchJobStore:
+ """Isolate the route module's store singleton onto a temp file."""
+ isolated = ScheduledResearchJobStore(path=tmp_path / "scheduled_jobs.json")
+ monkeypatch.setattr(scheduled_routes, "_scheduled_research_store", isolated)
+ return isolated
+
+
+def client(store: ScheduledResearchJobStore, monkeypatch: pytest.MonkeyPatch) -> TestClient:
+ monkeypatch.delenv("API_AUTH_KEY", raising=False)
+ monkeypatch.setattr(api_server, "_API_KEY", "")
+ return TestClient(api_server.app, client=("127.0.0.1", 50000))
+
+
+def home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
+ """Point the runtime root at ``tmp_path`` so CLI/proposals write temp files."""
+ monkeypatch.setenv("VIBE_TRADING_HOME", str(tmp_path))
+ return tmp_path
+
+
+def _seed_system_job(store: ScheduledResearchJobStore, job_id: str = SYSTEM_ID) -> ScheduledResearchJob:
+ """Insert a system row the way ``ensure_system_jobs`` would."""
+ job = ScheduledResearchJob(
+ id=job_id,
+ title="Memory consolidation",
+ prompt="",
+ schedule="3600000",
+ next_run_at=1_700_000_000_000,
+ status=JobStatus.PENDING,
+ created_at=1_700_000_000_000,
+ source_type="system",
+ executor_kind="internal",
+ internal_callable="memory.consolidate",
+ )
+ store.upsert(job)
+ return job
+
+
+def _seed_normal_job(store: ScheduledResearchJobStore, job_id: str = "job-normal") -> ScheduledResearchJob:
+ job = ScheduledResearchJob(
+ id=job_id,
+ prompt="scan momentum names",
+ schedule="60000",
+ next_run_at=1_700_000_000_000,
+ status=JobStatus.PENDING,
+ created_at=1_700_000_000_000,
+ )
+ store.upsert(job)
+ return job
+
+
+def _store_bytes(store: ScheduledResearchJobStore) -> bytes | None:
+ return store.path.read_bytes() if store.path.exists() else None
+
+
+def _wire_proposals(
+ monkeypatch: pytest.MonkeyPatch, store: ScheduledResearchJobStore
+) -> None:
+ monkeypatch.setattr(proposals, "default_store", lambda: store)
+ monkeypatch.setattr(
+ proposals,
+ "scheduler_status",
+ lambda: {"enabled": True, "running": True, "executable": True},
+ )
+
+
+def _write_proposal(home: Path, payload: dict) -> str:
+ directory = home / "scheduled_research" / "proposals"
+ directory.mkdir(parents=True, exist_ok=True)
+ path = directory / f"{payload['proposal_id']}.json"
+ path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
+ return payload["proposal_id"]
+
+
+def _handwritten_payload(operation: str, **extra: object) -> dict:
+ now = int(time.time() * 1000)
+ payload: dict = {
+ "proposal_id": f"srp_{uuid.uuid4().hex}",
+ "operation": operation,
+ "status": "pending",
+ "session_id": None,
+ "created_at": now,
+ "expires_at": now + 15 * 60 * 1000,
+ }
+ payload.update(extra)
+ return payload
+
+
+def _run_cli(argv: List[str]) -> int:
+ """Drive the real argparse dispatcher, returning its exit code."""
+ from cli import _legacy
+
+ return int(_legacy.main(argv))
+
+
+# ---------------------------------------------------------------------------
+# ②b — structural pin: system ids are unforgeable at the regex level
+# ---------------------------------------------------------------------------
+
+
+def test_safe_job_id_regex_rejects_system_ids() -> None:
+ from src.api.helpers import _SAFE_PATH_PARAM_RE
+
+ assert scheduled_routes._SAFE_JOB_ID_RE.match(SYSTEM_ID) is None
+ assert scheduled_routes._SAFE_JOB_ID_RE.fullmatch(SYSTEM_ID) is None
+ assert _SAFE_PATH_PARAM_RE.match(SYSTEM_ID) is None
+ assert is_system_job_id(SYSTEM_ID) is True
+
+
+def test_route_registration_order_keeps_static_paths_before_job_id() -> None:
+ scheduled = [
+ (index, route)
+ for index, route in enumerate(api_server.app.routes)
+ if hasattr(route, "path") and route.path.startswith("/scheduled-runs")
+ ]
+
+ def _first(path: str, method: str | None = None) -> int:
+ for index, route in scheduled:
+ if route.path == path and (method is None or method in route.methods):
+ return index
+ raise AssertionError(f"route {path} ({method}) not registered")
+
+ # The GET-single {job_id} route must stay registered AFTER /status and
+ # /playbooks so those literal segments are never consumed as a job id.
+ get_single = _first("/scheduled-runs/{job_id}", "GET")
+ assert _first("/scheduled-runs/status") < get_single
+ assert _first("/scheduled-runs/playbooks") < get_single
+ assert _first("/scheduled-runs/playbooks/{slug}", "GET") < get_single
+ assert _first("/scheduled-runs/playbooks/{slug}", "POST") < get_single
+
+
+def test_tool_draft_schema_forbids_unknown_keys() -> None:
+ from src.tools.scheduled_research_tool import ScheduledResearchTool
+
+ draft_schema = ScheduledResearchTool.parameters["properties"]["draft"]
+ assert draft_schema["additionalProperties"] is False
+ assert "id" not in draft_schema["properties"]
+
+
+# ---------------------------------------------------------------------------
+# Entries 1+2 — DELETE / PATCH routes: REST-visible 400, store untouched
+# ---------------------------------------------------------------------------
+
+
+def test_delete_system_job_returns_400_and_store_unchanged(
+ client: TestClient, store: ScheduledResearchJobStore
+) -> None:
+ _seed_system_job(store)
+ before = _store_bytes(store)
+
+ response = client.delete(f"/scheduled-runs/{SYSTEM_ID}")
+
+ assert response.status_code == 400
+ assert response.json()["detail"] == "invalid job_id"
+ assert _store_bytes(store) == before
+ assert store.get(SYSTEM_ID) is not None
+
+
+def test_patch_system_job_returns_400_and_store_unchanged(
+ client: TestClient, store: ScheduledResearchJobStore
+) -> None:
+ _seed_system_job(store)
+ before = _store_bytes(store)
+
+ response = client.patch(
+ f"/scheduled-runs/{SYSTEM_ID}", json={"title": "hijacked", "prompt": "injected"}
+ )
+
+ assert response.status_code == 400
+ assert response.json()["detail"] == "invalid job_id"
+ assert _store_bytes(store) == before
+ kept = store.get(SYSTEM_ID)
+ assert kept is not None
+ assert kept.title == "Memory consolidation"
+ assert kept.prompt == ""
+
+
+def test_delete_unknown_id_still_returns_404_not_403(
+ client: TestClient, store: ScheduledResearchJobStore
+) -> None:
+ response = client.delete("/scheduled-runs/no-such-job")
+
+ assert response.status_code == 404
+ assert response.json()["detail"] == "scheduled run no-such-job not found"
+
+
+def test_patch_unknown_id_still_returns_404_not_403(
+ client: TestClient, store: ScheduledResearchJobStore
+) -> None:
+ response = client.patch("/scheduled-runs/no-such-job", json={"title": "x"})
+
+ assert response.status_code == 404
+ assert response.json()["detail"] == "scheduled run no-such-job not found"
+
+
+def test_delete_normal_job_returns_truly_empty_204(
+ client: TestClient, store: ScheduledResearchJobStore
+) -> None:
+ _seed_normal_job(store)
+
+ response = client.delete("/scheduled-runs/job-normal")
+
+ assert response.status_code == 204
+ assert response.content == b""
+ assert store.get("job-normal") is None
+
+
+# ---------------------------------------------------------------------------
+# Entry 5 — POST /scheduled-runs body: no system row can land
+# ---------------------------------------------------------------------------
+
+
+def test_post_cannot_forge_system_id_and_existing_row_untouched(
+ client: TestClient, store: ScheduledResearchJobStore
+) -> None:
+ _seed_system_job(store)
+ before = _store_bytes(store)
+
+ response = client.post(
+ "/scheduled-runs",
+ json={"id": SYSTEM_ID, "prompt": "injected", "schedule": "60000"},
+ )
+
+ assert response.status_code == 422
+ assert _store_bytes(store) == before
+ kept = store.get(SYSTEM_ID)
+ assert kept is not None
+ assert kept.source_type == "system"
+ assert kept.prompt == ""
+
+
+def test_post_ignores_source_type_and_executor_kind_escalation(
+ client: TestClient, store: ScheduledResearchJobStore
+) -> None:
+ response = client.post(
+ "/scheduled-runs",
+ json={
+ "id": "escalation-attempt",
+ "prompt": "ordinary prompt",
+ "schedule": "60000",
+ "source_type": "system",
+ "executor_kind": "internal",
+ "internal_callable": "memory.consolidate",
+ },
+ )
+
+ assert response.status_code == 201
+ assert response.json()["source_type"] == "prompt"
+ stored = store.get("escalation-attempt")
+ assert stored is not None
+ assert stored.source_type == "prompt"
+ assert stored.executor_kind == "prompt"
+ assert stored.internal_callable is None
+
+
+def test_playbook_route_rejects_system_id_with_400(
+ client: TestClient, store: ScheduledResearchJobStore
+) -> None:
+ _seed_system_job(store)
+ before = _store_bytes(store)
+
+ response = client.post(
+ f"/scheduled-runs/playbooks/{SAMPLE_SLUG}", json={"id": SYSTEM_ID}
+ )
+
+ assert response.status_code == 400
+ assert response.json()["detail"] == "invalid id"
+ assert _store_bytes(store) == before
+
+
+def test_playbook_route_creates_playbook_sourced_job(
+ client: TestClient, store: ScheduledResearchJobStore
+) -> None:
+ response = client.post(
+ f"/scheduled-runs/playbooks/{SAMPLE_SLUG}", json={"id": "desk-brief"}
+ )
+
+ assert response.status_code == 201
+ assert response.json()["source_type"] == "playbook"
+ stored = store.get("desk-brief")
+ assert stored is not None
+ assert stored.source_type == "playbook"
+ assert stored.executor_kind == "prompt"
+
+
+# ---------------------------------------------------------------------------
+# Entries 3+4 — proposals: refuse at propose AND at commit (authoritative)
+# ---------------------------------------------------------------------------
+
+
+def test_propose_create_rejects_system_id_draft(
+ home: Path, store: ScheduledResearchJobStore, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _wire_proposals(monkeypatch, store)
+ _seed_system_job(store)
+ before = _store_bytes(store)
+ draft = {
+ "title": "hijack",
+ "id": SYSTEM_ID,
+ "source": {"kind": "prompt", "prompt": "injected"},
+ "schedule": {"expression": "60000"},
+ "delivery": {"mode": "in_app"},
+ }
+
+ with pytest.raises(ValueError, match="reserved for protected system jobs"):
+ proposals.propose_create(draft)
+
+ assert _store_bytes(store) == before
+ assert not (home / "scheduled_research" / "proposals").exists()
+
+
+def test_propose_create_rejects_unsafe_id_draft(
+ home: Path, store: ScheduledResearchJobStore, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _wire_proposals(monkeypatch, store)
+ draft = {
+ "title": "x",
+ "id": "id with space",
+ "source": {"kind": "prompt", "prompt": "p"},
+ "schedule": {"expression": "60000"},
+ "delivery": {"mode": "in_app"},
+ }
+
+ with pytest.raises(ValueError, match="1-128 characters"):
+ proposals.propose_create(draft)
+
+ assert not (home / "scheduled_research" / "proposals").exists()
+
+
+def test_commit_refuses_handwritten_proposal_replacing_system_job(
+ home: Path, store: ScheduledResearchJobStore, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """v2 — the authoritative guard: a tampered proposal file on disk must die
+ at COMMIT with the store byte-identical."""
+ _wire_proposals(monkeypatch, store)
+ _seed_system_job(store)
+ before = _store_bytes(store)
+ internal_job = ScheduledResearchJob(
+ id=SYSTEM_ID,
+ title="hijack",
+ prompt="injected",
+ schedule="60000",
+ next_run_at=2_000_000_000_000,
+ status=JobStatus.PENDING,
+ created_at=1_700_000_000_000,
+ ).to_dict()
+ proposal_id = _write_proposal(
+ home, _handwritten_payload("create", job=None, internal_job=internal_job)
+ )
+
+ with pytest.raises(ProposalError, match="protected system job"):
+ proposals.commit_proposal(proposal_id)
+
+ assert _store_bytes(store) == before
+ kept = store.get(SYSTEM_ID)
+ assert kept is not None
+ assert kept.source_type == "system"
+ assert kept.prompt == ""
+ assert kept.internal_callable == "memory.consolidate"
+ on_disk = json.loads(
+ (home / "scheduled_research" / "proposals" / f"{proposal_id}.json").read_text(
+ encoding="utf-8"
+ )
+ )
+ assert on_disk["status"] == "pending"
+
+
+def test_commit_refuses_handwritten_proposal_minting_new_system_job(
+ home: Path, store: ScheduledResearchJobStore, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _wire_proposals(monkeypatch, store)
+ internal_job = ScheduledResearchJob(
+ id=f"{SYSTEM_JOB_ID_PREFIX}brand-new",
+ title="mint",
+ prompt="",
+ schedule="3600000",
+ next_run_at=2_000_000_000_000,
+ status=JobStatus.PENDING,
+ created_at=1_700_000_000_000,
+ source_type="system",
+ executor_kind="internal",
+ internal_callable="memory.consolidate",
+ ).to_dict()
+ proposal_id = _write_proposal(
+ home, _handwritten_payload("create", job=None, internal_job=internal_job)
+ )
+
+ with pytest.raises(ProposalError, match="protected system job"):
+ proposals.commit_proposal(proposal_id)
+
+ assert store.load() == {}
+
+
+def test_propose_cancel_rejects_system_job(
+ home: Path, store: ScheduledResearchJobStore, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _wire_proposals(monkeypatch, store)
+ _seed_system_job(store)
+ before = _store_bytes(store)
+
+ with pytest.raises(ProposalError, match="protected system job"):
+ proposals.propose_cancel(SYSTEM_ID)
+
+ assert _store_bytes(store) == before
+ assert not (home / "scheduled_research" / "proposals").exists()
+
+
+def test_commit_refuses_handwritten_cancel_of_system_job(
+ home: Path, store: ScheduledResearchJobStore, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _wire_proposals(monkeypatch, store)
+ _seed_system_job(store)
+ before = _store_bytes(store)
+ proposal_id = _write_proposal(
+ home, _handwritten_payload("cancel", job_id=SYSTEM_ID, job=None)
+ )
+
+ with pytest.raises(ProposalError, match="protected system job"):
+ proposals.commit_proposal(proposal_id)
+
+ assert _store_bytes(store) == before
+ assert store.get(SYSTEM_ID) is not None
+
+
+def test_commit_create_and_cancel_normal_jobs_still_work(
+ home: Path, store: ScheduledResearchJobStore, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _wire_proposals(monkeypatch, store)
+ created = proposals.propose_create(
+ {
+ "title": "Morning scan",
+ "source": {"kind": "prompt", "prompt": "Summarize the market."},
+ "schedule": {"expression": "60000"},
+ "delivery": {"mode": "in_app"},
+ }
+ )
+ proposals.commit_proposal(created["proposal_id"])
+ assert len(store.load()) == 1
+ job = next(iter(store.load().values()))
+ assert job.source_type == "prompt"
+
+ cancelled = proposals.propose_cancel(job.id)
+ proposals.commit_proposal(cancelled["proposal_id"])
+ assert store.load() == {}
+
+
+def test_commit_still_gated_on_executable_runtime(
+ home: Path, store: ScheduledResearchJobStore, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setattr(proposals, "default_store", lambda: store)
+ monkeypatch.setattr(
+ proposals,
+ "scheduler_status",
+ lambda: {"enabled": False, "running": False, "executable": False},
+ )
+ internal_job = ScheduledResearchJob(
+ id="plain-job",
+ title="t",
+ prompt="p",
+ schedule="60000",
+ next_run_at=2_000_000_000_000,
+ status=JobStatus.PENDING,
+ created_at=1_700_000_000_000,
+ ).to_dict()
+ proposal_id = _write_proposal(
+ home, _handwritten_payload("create", job=None, internal_job=internal_job)
+ )
+
+ with pytest.raises(ProposalError, match="not enabled and running"):
+ proposals.commit_proposal(proposal_id)
+
+ assert store.load() == {}
+
+
+# ---------------------------------------------------------------------------
+# Entry 6 — CLI `playbook create --id` writes the store directly
+# ---------------------------------------------------------------------------
+
+
+def test_cli_create_rejects_system_id_and_store_untouched(home: Path) -> None:
+ cli_store = ScheduledResearchJobStore()
+ _seed_system_job(cli_store)
+ before = _store_bytes(cli_store)
+
+ code = _run_cli(["playbook", "create", SAMPLE_SLUG, "--id", SYSTEM_ID])
+
+ assert code == 1
+ assert _store_bytes(cli_store) == before
+ kept = cli_store.get(SYSTEM_ID)
+ assert kept is not None
+ assert kept.source_type == "system"
+ assert kept.prompt == ""
+ assert kept.internal_callable == "memory.consolidate"
+
+
+def test_cli_create_rejects_unsafe_id_and_store_untouched(home: Path) -> None:
+ cli_store = ScheduledResearchJobStore()
+ _seed_system_job(cli_store)
+ before = _store_bytes(cli_store)
+
+ code = _run_cli(["playbook", "create", SAMPLE_SLUG, "--id", "bad id!"])
+
+ assert code == 1
+ assert _store_bytes(cli_store) == before
+
+
+def test_cli_create_normal_id_still_persists_playbook_job(home: Path) -> None:
+ code = _run_cli(["playbook", "create", SAMPLE_SLUG, "--id", "desk-brief"])
+
+ assert code == 0
+ stored = ScheduledResearchJobStore().get("desk-brief")
+ assert stored is not None
+ assert stored.source_type == "playbook"
+ assert stored.executor_kind == "prompt"
+
+
+def test_build_job_from_playbook_unit_guard() -> None:
+ job, error = build_job_from_playbook(SAMPLE_SLUG, job_id=SYSTEM_ID)
+ assert job is None
+ assert error is not None
+ assert "reserved for protected system jobs" in error
+
+ job, error = build_job_from_playbook(SAMPLE_SLUG, job_id="bad id!")
+ assert job is None
+ assert error is not None
+ assert "1-128 characters" in error