Patchwise Agent Studio: Oracle AI Agent Studio cycles
Browse files
coworker/api/app.py
CHANGED
|
@@ -67,7 +67,8 @@ def create_app(svc: Services | None = None, cfg: Settings | None = None, *, back
|
|
| 67 |
notify.get(svc).start()
|
| 68 |
from ..channels import telegram as tg_channel
|
| 69 |
tg_channel.start(svc, run_mgr) # the two-way bot: listens only when the bot token is set
|
| 70 |
-
from ..platform.agent_studio import Courier
|
|
|
|
| 71 |
courier = Courier(svc, run_mgr, watch_hours=cfg.agent_studio_watch_hours)
|
| 72 |
courier.start() # callbacks to Oracle AI Agent Studio, and the release watch
|
| 73 |
app.state.courier = courier
|
|
|
|
| 67 |
notify.get(svc).start()
|
| 68 |
from ..channels import telegram as tg_channel
|
| 69 |
tg_channel.start(svc, run_mgr) # the two-way bot: listens only when the bot token is set
|
| 70 |
+
from ..platform.agent_studio import AgentStudio, Courier
|
| 71 |
+
AgentStudio(svc).ensure_from_env() # the Fusion tool's client survives a restart without saved state
|
| 72 |
courier = Courier(svc, run_mgr, watch_hours=cfg.agent_studio_watch_hours)
|
| 73 |
courier.start() # callbacks to Oracle AI Agent Studio, and the release watch
|
| 74 |
app.state.courier = courier
|
coworker/platform/agent_studio.py
CHANGED
|
@@ -56,6 +56,12 @@ _AREA = re.compile(r"^[A-Z][A-Z0-9]{1,5}$")
|
|
| 56 |
AREA_ALIASES = {"CE": "CM", "PJ": "PPM", "REQ": "PO", "RCV": "PO"}
|
| 57 |
# Identifiers the Fusion side made for tests start with TEST_ (often a Fusion record number behind it): not personal data
|
| 58 |
TEST_PREFIX = "TEST_"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
MAX_PACKAGE = 2_000_000
|
| 60 |
|
| 61 |
# Delivery straight to Oracle Fusion (no relay): Patchwise signs in to the client's OCI IAM identity domain (client
|
|
@@ -155,10 +161,10 @@ def validate_test_data(test_data) -> dict[str, dict[str, list[str]]]:
|
|
| 155 |
continue
|
| 156 |
if len(s) > 200:
|
| 157 |
raise AgentStudioError("BAD_TEST_DATA", f"{code}.{key}: a value is longer than 200 characters")
|
| 158 |
-
test_id = s.upper().startswith(TEST_PREFIX)
|
| 159 |
for rx, what in _PII:
|
| 160 |
if test_id and what != "an email address":
|
| 161 |
-
continue # TEST_300000047382915
|
| 162 |
if rx.search(s):
|
| 163 |
raise AgentStudioError("NOT_SYNTHETIC", f"{code}.{key} looks like {what}: send synthetic test data only, "
|
| 164 |
f"no personal or payment data")
|
|
@@ -197,6 +203,46 @@ class AgentStudio:
|
|
| 197 |
self.svc.audit.append("agent_studio.client_updated", actor=actor, summary=client_id, payload={"client_id": client_id})
|
| 198 |
return self.client(client_id)
|
| 199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
def revoke_client(self, client_id: str, actor: str) -> None:
|
| 201 |
self.client(client_id)
|
| 202 |
self.db.update("agent_clients", "client_id", client_id, revoked_at=now_iso())
|
|
|
|
| 56 |
AREA_ALIASES = {"CE": "CM", "PJ": "PPM", "REQ": "PO", "RCV": "PO"}
|
| 57 |
# Identifiers the Fusion side made for tests start with TEST_ (often a Fusion record number behind it): not personal data
|
| 58 |
TEST_PREFIX = "TEST_"
|
| 59 |
+
# Reference data of the pod (item and account numbers, amounts, document numbers, organisations): long digit runs are
|
| 60 |
+
# normal there (item 100000012345, account 451.40.63500.000.000.000). An e-mail address is still refused in them.
|
| 61 |
+
REFERENCE_KEYS = frozenset({"item", "item_number", "quantity", "unit_price", "invoice_amount", "amount", "account",
|
| 62 |
+
"distribution_combination", "inventory_org", "organization", "project_number", "requisition_number",
|
| 63 |
+
"source_transaction_number", "purchase_order_number", "invoice_number", "receipt_number",
|
| 64 |
+
"journal_source", "journal_category", "journal_name", "batch_name", "ledger", "accounting_period"})
|
| 65 |
MAX_PACKAGE = 2_000_000
|
| 66 |
|
| 67 |
# Delivery straight to Oracle Fusion (no relay): Patchwise signs in to the client's OCI IAM identity domain (client
|
|
|
|
| 161 |
continue
|
| 162 |
if len(s) > 200:
|
| 163 |
raise AgentStudioError("BAD_TEST_DATA", f"{code}.{key}: a value is longer than 200 characters")
|
| 164 |
+
test_id = s.upper().startswith(TEST_PREFIX) or str(key) in REFERENCE_KEYS
|
| 165 |
for rx, what in _PII:
|
| 166 |
if test_id and what != "an email address":
|
| 167 |
+
continue # TEST_300000047382915, item 100000012345: identifiers and reference numbers, not personal data
|
| 168 |
if rx.search(s):
|
| 169 |
raise AgentStudioError("NOT_SYNTHETIC", f"{code}.{key} looks like {what}: send synthetic test data only, "
|
| 170 |
f"no personal or payment data")
|
|
|
|
| 203 |
self.svc.audit.append("agent_studio.client_updated", actor=actor, summary=client_id, payload={"client_id": client_id})
|
| 204 |
return self.client(client_id)
|
| 205 |
|
| 206 |
+
def ensure_from_env(self) -> list[str]:
|
| 207 |
+
"""Clients defined by the server's secrets (agent_studio.bootstrap_clients, i.e. FPT_SECRET_AGENT_STUDIO_BOOTSTRAP_CLIENTS):
|
| 208 |
+
a JSON list of {client_id, client_secret, name, callback_url?, release_watch_url?, signing_key?}. Each exists after
|
| 209 |
+
every start with that id and secret, so the Fusion tool keeps working on a server without saved state. A client an
|
| 210 |
+
admin revoked stays revoked. Returns the ids made or refreshed."""
|
| 211 |
+
raw = self.svc.vault.get("agent_studio.bootstrap_clients") or ""
|
| 212 |
+
if not raw.strip():
|
| 213 |
+
return []
|
| 214 |
+
try:
|
| 215 |
+
items = json.loads(raw)
|
| 216 |
+
except ValueError:
|
| 217 |
+
log.warning("FPT_SECRET_AGENT_STUDIO_BOOTSTRAP_CLIENTS is not JSON: ignored")
|
| 218 |
+
return []
|
| 219 |
+
done = []
|
| 220 |
+
for it in items if isinstance(items, list) else []:
|
| 221 |
+
cid, secret = str(it.get("client_id") or ""), str(it.get("client_secret") or "")
|
| 222 |
+
if not re.fullmatch(r"pwc_[0-9a-f]{16}", cid) or len(secret) < 24:
|
| 223 |
+
log.warning("a bootstrap client needs client_id pwc_<16 hex> and a secret of 24+ characters: skipped")
|
| 224 |
+
continue
|
| 225 |
+
row = self.db.one("SELECT * FROM agent_clients WHERE client_id=?", (cid,))
|
| 226 |
+
if row and row.get("revoked_at"):
|
| 227 |
+
continue
|
| 228 |
+
cb, rw = it.get("callback_url"), it.get("release_watch_url")
|
| 229 |
+
if row:
|
| 230 |
+
self.db.update("agent_clients", "client_id", cid, secret_hash=_hash(secret), name=str(it.get("name") or row["name"])[:80])
|
| 231 |
+
else:
|
| 232 |
+
self.db.insert("agent_clients", {"client_id": cid, "name": str(it.get("name") or "Oracle Fusion AI Agent Studio")[:80],
|
| 233 |
+
"secret_hash": _hash(secret), "callback_url": _check_url(cb, "callback address"),
|
| 234 |
+
"release_watch_url": _check_url(rw, "release watch address"),
|
| 235 |
+
"created_by": "server settings", "created_at": now_iso()})
|
| 236 |
+
if it.get("signing_key"):
|
| 237 |
+
self.svc.vault.set(f"agent_studio.{cid}.signing", str(it["signing_key"]))
|
| 238 |
+
elif not self.svc.vault.get(f"agent_studio.{cid}.signing"):
|
| 239 |
+
self.svc.vault.set(f"agent_studio.{cid}.signing", "pwk_" + secrets.token_urlsafe(32))
|
| 240 |
+
self.svc.redactor.add_secrets([secret])
|
| 241 |
+
done.append(cid)
|
| 242 |
+
if done:
|
| 243 |
+
self.svc.audit.append("agent_studio.clients_from_settings", actor="server settings", summary=", ".join(done), payload={"clients": done})
|
| 244 |
+
return done
|
| 245 |
+
|
| 246 |
def revoke_client(self, client_id: str, actor: str) -> None:
|
| 247 |
self.client(client_id)
|
| 248 |
self.db.update("agent_clients", "client_id", client_id, revoked_at=now_iso())
|
coworker/tests/test_agent_studio_bootstrap.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""The Fusion tool's OAuth client from the server's secrets: it exists after every start with the same id and secret
|
| 2 |
+
(a server without saved state, such as a Space before Supabase), an admin's revoke stands, bad entries are skipped."""
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
import unittest
|
| 8 |
+
from unittest import mock
|
| 9 |
+
|
| 10 |
+
from coworker.platform.agent_studio import AgentStudio
|
| 11 |
+
from coworker.tests.test_ai_scenarios import fusion_app
|
| 12 |
+
|
| 13 |
+
CID, SECRET = "pwc_00112233445566aa", "pws_" + "s" * 40
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TestBootstrapClients(unittest.TestCase):
|
| 17 |
+
@classmethod
|
| 18 |
+
def setUpClass(cls):
|
| 19 |
+
cls.app, cls.c, cls.svc, cls.h, _ = fusion_app()
|
| 20 |
+
|
| 21 |
+
def boot(self, items):
|
| 22 |
+
with mock.patch.dict(os.environ, {"FPT_SECRET_AGENT_STUDIO_BOOTSTRAP_CLIENTS": json.dumps(items)}):
|
| 23 |
+
return AgentStudio(self.svc).ensure_from_env()
|
| 24 |
+
|
| 25 |
+
def token(self, secret=SECRET):
|
| 26 |
+
return self.c.post("/oauth/token", data={"grant_type": "client_credentials", "client_id": CID, "client_secret": secret,
|
| 27 |
+
"scope": "patchwise.invoke"})
|
| 28 |
+
|
| 29 |
+
def test_a_the_client_exists_with_its_id_and_secret_and_again_after_a_restart(self):
|
| 30 |
+
self.assertEqual(self.boot([{"client_id": CID, "client_secret": SECRET, "name": "Fusion dev61"}]), [CID])
|
| 31 |
+
self.assertEqual(self.token().status_code, 200)
|
| 32 |
+
self.assertEqual(self.boot([{"client_id": CID, "client_secret": SECRET}]), [CID]) # idempotent
|
| 33 |
+
self.assertEqual(len([c for c in AgentStudio(self.svc).clients() if c["client_id"] == CID]), 1)
|
| 34 |
+
self.assertEqual(self.token("wrong-secret-" + "x" * 20).status_code, 401)
|
| 35 |
+
|
| 36 |
+
def test_b_bad_entries_are_skipped(self):
|
| 37 |
+
self.assertEqual(self.boot([{"client_id": "evil", "client_secret": SECRET}, {"client_id": "pwc_00112233445566bb", "client_secret": "short"}]), [])
|
| 38 |
+
|
| 39 |
+
def test_z_an_admins_revoke_stands(self):
|
| 40 |
+
self.boot([{"client_id": CID, "client_secret": SECRET}])
|
| 41 |
+
self.c.delete(f"/api/agent-studio/clients/{CID}", headers=self.h["admin"])
|
| 42 |
+
self.assertEqual(self.boot([{"client_id": CID, "client_secret": SECRET}]), [])
|
| 43 |
+
self.assertEqual(self.token().status_code, 401)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
unittest.main()
|
coworker/tests/test_agent_studio_v36.py
CHANGED
|
@@ -51,6 +51,15 @@ class TestAgentStudioV36(unittest.TestCase):
|
|
| 51 |
bad = self.c.post("/api/agent-studio/v1/test-data/check", json={"test_data": {"AP": {"supplier": "+1 415 555 0100"}}}, headers=self.auth).json()
|
| 52 |
self.assertEqual((bad["ok"], bad["code"]), (False, "NOT_SYNTHETIC"))
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
def test_b_every_v36_area_code_is_understood(self):
|
| 55 |
r = self.c.post("/api/agent-studio/v1/test-data/check", json={"test_data": TD}, headers=self.auth).json()
|
| 56 |
self.assertTrue(r["ok"], r)
|
|
|
|
| 51 |
bad = self.c.post("/api/agent-studio/v1/test-data/check", json={"test_data": {"AP": {"supplier": "+1 415 555 0100"}}}, headers=self.auth).json()
|
| 52 |
self.assertEqual((bad["ok"], bad["code"]), (False, "NOT_SYNTHETIC"))
|
| 53 |
|
| 54 |
+
def test_a_reference_numbers_of_the_pod_are_not_personal_data(self):
|
| 55 |
+
# the demo pod's item numbers are long digit runs (PO.item was refused as "a phone or account number")
|
| 56 |
+
ref = {"PO": {"item": "100000012345678", "quantity": "1", "account": "451.40.63500.000.000.000"}}
|
| 57 |
+
self.assertEqual(validate_test_data(ref)["PO"]["item"], ["100000012345678"])
|
| 58 |
+
# an e-mail address is refused even in a reference field; a long number in a person field still is
|
| 59 |
+
for td in ({"PO": {"item": "buyer@example.com"}}, {"AP": {"supplier": "100000012345678"}}):
|
| 60 |
+
r = self.c.post("/api/agent-studio/v1/test-data/check", json={"test_data": td}, headers=self.auth).json()
|
| 61 |
+
self.assertEqual((r["ok"], r["code"]), (False, "NOT_SYNTHETIC"), td)
|
| 62 |
+
|
| 63 |
def test_b_every_v36_area_code_is_understood(self):
|
| 64 |
r = self.c.post("/api/agent-studio/v1/test-data/check", json={"test_data": TD}, headers=self.auth).json()
|
| 65 |
self.assertTrue(r["ok"], r)
|