feat(editorial): wire serialized Stage runtime install and PMO proposal client
This commit is contained in:
1 parent
8869ce2583
commit
f7cf2c8f5d
8 files changed
+387
-4
No files matched your search
@@ -0,0 +1,112 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Bounded PMO proposal only. No approval, execution, cron or publication API."""
|
||||
import argparse
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import stat
|
||||
import tempfile
|
||||
import uuid
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
ENDPOINT = 'https://stageapi.canina.ir/api/internal/prepare-worker/editorial-proposals'
|
||||
|
||||
class ClientError(Exception):
|
||||
pass
|
||||
|
||||
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
||||
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
||||
return None
|
||||
|
||||
|
||||
def safe(path):
|
||||
if not path.is_absolute() or '..' in path.parts:
|
||||
raise ValueError('Absolute home required')
|
||||
for p in (path, *path.parents):
|
||||
if p.is_symlink(): raise ValueError('Symlink refused')
|
||||
return path
|
||||
|
||||
|
||||
def private_read(path):
|
||||
safe(path)
|
||||
fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
|
||||
with os.fdopen(fd, 'rb') as stream:
|
||||
s = os.fstat(stream.fileno())
|
||||
if not stat.S_ISREG(s.st_mode) or s.st_uid != os.getuid() or s.st_nlink != 1 or stat.S_IMODE(s.st_mode) not in (0o400, 0o600) or not 0 < s.st_size <= 16384:
|
||||
raise ValueError('Private runtime-owned regular file required')
|
||||
return stream.read(16385)
|
||||
|
||||
|
||||
def atomic(path, data):
|
||||
fd, name = tempfile.mkstemp(prefix='.pmo-', dir=path.parent)
|
||||
try:
|
||||
with os.fdopen(fd, 'wb') as stream:
|
||||
stream.write(data); stream.flush(); os.fsync(stream.fileno())
|
||||
os.chmod(name, 0o400); os.replace(name, path)
|
||||
finally:
|
||||
if os.path.exists(name): os.unlink(name)
|
||||
|
||||
|
||||
def submit(home, commit, proposal, *, endpoint=ENDPOINT, timeout=15):
|
||||
# endpoint override is an in-process disposable-test seam, never CLI/config input.
|
||||
safe(home)
|
||||
if not re.fullmatch('[0-9a-f]{40}', commit): raise ValueError('Full deployment SHA required')
|
||||
if not isinstance(proposal, dict) or set(proposal) != {'researchTopic', 'acceptanceCriteria'}:
|
||||
raise ValueError('Only owner topic and acceptanceCriteria accepted')
|
||||
topic, criteria = proposal['researchTopic'], proposal['acceptanceCriteria']
|
||||
if not isinstance(topic, str) or not topic.strip() or len(topic) > 180 or not isinstance(criteria, list) or not 1 <= len(criteria) <= 10 or any(not isinstance(v, str) or not v.strip() or len(v) > 500 for v in criteria):
|
||||
raise ValueError('Invalid proposal')
|
||||
token = private_read(home/'credentials/editorial-pmo-token').decode('ascii')
|
||||
if not re.fullmatch('[A-Za-z0-9_-]{64}', token): raise ValueError('Invalid credential')
|
||||
directory = safe(home/'editorial-v4/client-state')
|
||||
directory.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
if directory.stat().st_uid != os.getuid() or stat.S_IMODE(directory.stat().st_mode) != 0o700:
|
||||
raise ValueError('Private state directory required')
|
||||
lock = safe(directory/'lock')
|
||||
fd = os.open(lock, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, 'a') as stream:
|
||||
fcntl.flock(stream, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
body = {**proposal, 'pilotKind':'editorial-research-to-brief-v1', 'operationMode':'PREPARE', 'environment':'staging', 'scope':'EDITORIAL_RESEARCH_TO_BRIEF', 'commitSha':commit, 'budgetLimitCents':20}
|
||||
state = safe(directory/'proposal.json')
|
||||
if state.exists():
|
||||
stored = json.loads(private_read(state))
|
||||
request_id = stored.get('requestId')
|
||||
if str(uuid.UUID(request_id)) != request_id or {k:v for k,v in stored.items() if k != 'requestId'} != body:
|
||||
raise ValueError('Existing request differs; explicit owner review required')
|
||||
body = stored
|
||||
else:
|
||||
body['requestId'] = str(uuid.uuid4())
|
||||
atomic(state, json.dumps(body, sort_keys=True).encode())
|
||||
req = urllib.request.Request(endpoint, data=json.dumps(body).encode(), headers={'Authorization':'Bearer '+token, 'Content-Type':'application/json'}, method='POST')
|
||||
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect())
|
||||
try:
|
||||
with opener.open(req, timeout=timeout) as response:
|
||||
if response.status != 201: raise ClientError('Unexpected proposal status')
|
||||
raw = response.read(16385)
|
||||
if len(raw)>16384: raise ClientError('Response exceeds bound')
|
||||
result = json.loads(raw)
|
||||
if not isinstance(result, dict) or not isinstance(result.get('taskId'), str): raise ClientError('Invalid proposal response')
|
||||
# Do not reflect arbitrary backend response bodies or credential material.
|
||||
return {'requestId':body['requestId'], 'taskId':result['taskId']}
|
||||
except (urllib.error.URLError, TimeoutError, OSError, ValueError) as error:
|
||||
code = getattr(error, 'code', None)
|
||||
if isinstance(error, urllib.error.HTTPError): error.close()
|
||||
raise ClientError('Proposal rejected HTTP '+str(code) if isinstance(code,int) else 'Proposal unavailable; retry same saved request') from None
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--proposal-file', type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
try:
|
||||
home = Path(os.environ['HERMES_HOME'])
|
||||
deployment = json.loads(private_read(home/'editorial-v4/deployment.json'))
|
||||
proposal = json.loads(private_read(args.proposal_file))
|
||||
print(json.dumps(submit(home, deployment['commitSha'], proposal), sort_keys=True))
|
||||
except (OSError, ValueError, KeyError, ClientError):
|
||||
parser.exit(1, 'PMO proposal failed (private details suppressed); saved request retained\n')
|
||||
|
||||
if __name__ == '__main__': main()
|
||||
@@ -0,0 +1,44 @@
|
||||
# Stage editorial runtime wiring
|
||||
|
||||
This slice is executable but does not itself deploy or submit a live proposal. Parent must promote this commit through the existing authenticated, serialized Stage CI. No Production, cron activation, approval, executor or publishing changes.
|
||||
|
||||
`stage_runtime_provision` runs disposable tests and the clean approved-SHA gate before provisioning. It inspects the persistent target home and streams the reviewed Python source plus an exact 22-file package (installer, client, manifest and 19 allowlisted payloads) through `docker exec -i canina-hermes python3 -B`. It uses the container's configured identity, never host-UID writes into the home. The fixed package directory is `$HERMES_HOME/editorial-v4/runtime-package` (0700); source/package files and deployment marker are 0400. The 19 profile/status files are installed and verified by the existing installer; the six skill paths are checked by actual `hermes skills list` calls under each profile home. No profile config.yaml is replaced.
|
||||
|
||||
A dedicated random 64-character PMO credential is generated within the target at `credentials/editorial-pmo-token` (0400, runtime owner). Existing runtime-owned credentials directories may be 0700 or non-writable-by-others 0755 without changing unrelated files. Only the digest crosses back to CI. CI atomically extends the fixed backend four-field secret, preserving existing worker/CI identities and digests; conflicts fail closed. Backend grants remain 0400 and owned by its mapped UID/GID. Repeat Stage provisioning validates the optional canonical third PMO grant rather than rejecting its own installation. Clean approved SHA is checked again at the wiring entrypoint.
|
||||
|
||||
## Verify after successful Stage CI
|
||||
|
||||
On the remote host:
|
||||
|
||||
```sh
|
||||
docker exec canina-hermes sh -c 'python3 -B "$HERMES_HOME/editorial-v4/runtime-package/installer.py" verify --target-home "$HERMES_HOME"'
|
||||
for profile in pmo canina-competitor-worker canina-seo-worker canina-content-worker canina-vet-review-worker canina-qa-worker; do
|
||||
docker exec -e HERMES_HOME="/opt/data/profiles/$profile" canina-hermes /opt/hermes/bin/hermes skills list
|
||||
done
|
||||
```
|
||||
|
||||
Target home `/opt/data` was confirmed read-only during development. CI derives it from inspect rather than hardcoding it. These commands are not evidence that installation happened before promotion.
|
||||
|
||||
## Submit FROM target only
|
||||
|
||||
Owner/PMO supplies a private runtime-owned 0400 JSON file, e.g. `/opt/data/editorial-v4/owner-proposal.json`, containing exactly `researchTopic` and `acceptanceCriteria` as described in `backend/src/orchestration-worker/editorial-bridge.md`. No sample topic/content is authored by this provisioning slice.
|
||||
|
||||
```sh
|
||||
docker exec canina-hermes sh -c 'python3 -B "$HERMES_HOME/editorial-v4/runtime-package/client.py" --proposal-file "$HERMES_HOME/editorial-v4/owner-proposal.json"'
|
||||
```
|
||||
|
||||
The CLI accepts no URL, credential, actor, budget or commit override. It reads the deployment SHA installed by the approved CI rollout and posts only to `https://stageapi.canina.ir/api/internal/prepare-worker/editorial-proposals`, disables redirects and ambient proxies, sends Bearer PMO authentication, and fixes PREPARE/staging/editorial scope/pilot/budget20. Only taskId/requestId are returned. Confirm successful deployed SHA in CI before submitting. The stable request body is saved privately in `editorial-v4/client-state/proposal.json` before the first HTTP attempt. Retry the same invocation after timeout, denial or unknown outcome. Changed immutable proposal/SHA is refused; do not delete the state to retry an ambiguous request. This client intentionally represents one bounded pilot; starting a different proposal requires separate owner review, not automatic state reset.
|
||||
|
||||
The proposal endpoint creates Gate1 pending only; genuine independent authenticated panel review is still required. This commit performs no live approval and claims no ten-step editorial execution.
|
||||
|
||||
## Tests
|
||||
|
||||
```sh
|
||||
PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s scripts/tests
|
||||
PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s infra/hermes-editorial/tests
|
||||
PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s scripts -p test_editorial_provision.py
|
||||
bash -n scripts/rollout-stage.sh
|
||||
git diff --check
|
||||
```
|
||||
|
||||
Verified 15 existing script tests, six installer/client tests and one real disposable runtime-install/grant test. HTTP tests exercise a real local TCP server (401/403/409/redirect denial, bounded timeout, identical retry body and changed-SHA rejection). Stage wiring tests execute streamed source against a private home and real Compose rendering, preserving existing grants over repeat provisioning; Docker/DB reads are isolated fixtures and no live install occurs in tests.
|
||||
@@ -0,0 +1,57 @@
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
class ClientTests(unittest.TestCase):
|
||||
def test_client_exists(self):
|
||||
self.assertTrue((ROOT / 'client.py').is_file(), 'executable PMO client missing')
|
||||
|
||||
def test_http_boundary_and_stable_request(self):
|
||||
spec = importlib.util.spec_from_file_location('client', ROOT / 'client.py')
|
||||
c = importlib.util.module_from_spec(spec); spec.loader.exec_module(c)
|
||||
received = []
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
status = 201
|
||||
def do_POST(self):
|
||||
received.append((self.path, self.headers['Authorization'], json.loads(self.rfile.read(int(self.headers['Content-Length'])))))
|
||||
if self.status == 0:
|
||||
time.sleep(0.15)
|
||||
return
|
||||
self.send_response(self.status)
|
||||
if self.status == 302: self.send_header('Location', '/leaked')
|
||||
self.end_headers(); self.wfile.write(b'{"taskId":"fixture-task"}')
|
||||
def log_message(self, *args): pass
|
||||
server = HTTPServer(('127.0.0.1', 0), Handler)
|
||||
thread = threading.Thread(target=server.serve_forever, daemon=True); thread.start()
|
||||
try:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
home = Path(tmp); (home/'credentials').mkdir(mode=0o700)
|
||||
token = home/'credentials/editorial-pmo-token'; token.write_text('x'*64); token.chmod(0o400)
|
||||
proposal = {'researchTopic':'Owner supplied topic', 'acceptanceCriteria':['Cite dated sources']}
|
||||
endpoint = 'http://127.0.0.1:%s/api/internal/prepare-worker/editorial-proposals' % server.server_port
|
||||
a = c.submit(home, 'a'*40, proposal, endpoint=endpoint)
|
||||
b = c.submit(home, 'a'*40, proposal, endpoint=endpoint)
|
||||
self.assertEqual(a,b); self.assertEqual(received[0][2],received[1][2])
|
||||
self.assertEqual(received[0][2]['budgetLimitCents'],20)
|
||||
self.assertEqual(received[0][1], 'Bearer '+'x'*64)
|
||||
with self.assertRaises(ValueError): c.submit(home,'b'*40,proposal,endpoint=endpoint)
|
||||
for status in (401,403,409,302):
|
||||
Handler.status=status
|
||||
with self.assertRaises(c.ClientError): c.submit(home,'a'*40,proposal,endpoint=endpoint)
|
||||
self.assertEqual(len(received),6)
|
||||
Handler.status=0
|
||||
with self.assertRaises(c.ClientError): c.submit(home,'a'*40,proposal,endpoint=endpoint,timeout=0.02)
|
||||
self.assertEqual(received[-1][2]['requestId'],received[0][2]['requestId'])
|
||||
token.chmod(0o644)
|
||||
with self.assertRaises(ValueError): c.submit(home,'a'*40,proposal,endpoint=endpoint)
|
||||
finally:
|
||||
server.shutdown(); server.server_close(); thread.join()
|
||||
|
||||
if __name__ == '__main__': unittest.main()
|
||||
Reference in new issue
Block a user