Python
//! Same domain as SQLite, with authenticated, lease-fenced transactions.
use crate::db::{self, Database};
use anyhow::{ensure, Context, Result};
use openworker_runtime::{
agent_team::{self, Document, Message, Transaction},
store::Session,
};
use serde_json::{json, Value};
pub(crate) fn migrate(tx: &mut postgres::Transaction<'_>) -> Result<()> {
tx.batch_execute("CREATE TABLE IF NOT EXISTS agent_team_workspace_claims(team TEXT NOT NULL,agent TEXT NOT NULL,token TEXT NOT NULL,expires_at TIMESTAMPTZ NOT NULL,PRIMARY KEY(team,agent));")?;
tx.batch_execute("CREATE TABLE IF NOT EXISTS agent_teams(id TEXT PRIMARY KEY,data JSONB NOT NULL); CREATE TABLE IF NOT EXISTS agent_team_templates(team TEXT NOT NULL,kind TEXT NOT NULL,data JSONB NOT NULL,PRIMARY KEY(team,kind)); CREATE TABLE IF NOT EXISTS agent_team_messages(team TEXT NOT NULL,seq BIGINT NOT NULL,id TEXT NOT NULL UNIQUE,data JSONB NOT NULL,PRIMARY KEY(team,seq));")?;
Ok(())
}
pub(crate) struct Tx<'a, 'b>(pub(crate) &'a mut postgres::Transaction<'b>);
#[derive(Clone, Debug)]
pub struct WorkspaceJob {
pub lead: Session,
pub team: String,
pub agent: String,
pub claim: String,
pub intent: openworker_runtime::team_workspace::Intent,
pub dispatch: bool,
pub environment: openworker_runtime::environment::Binding,
pub root: String,
pub profile: String,
}
pub(crate) fn lead_authority(
tx: &mut postgres::Transaction<'_>,
lead: &Session,
doc: &Document,
) -> Result<(crate::auth::Identity, crate::service_catalog::Profile)> {
ensure!(!lead.closed, "Lead session is closed");
if let Some(catalog) = &doc.type_catalog {
catalog.check_scope(lead)?;
}
let row=tx.query_opt("SELECT o.principal,k.spec,o.release_id,o.profile_id FROM service_operations o JOIN api_credentials k ON k.id=o.credential_id AND NOT k.revoked AND ((k.spec->>'expires_at') IS NULL OR (k.spec->>'expires_at')::bigint>extract(epoch FROM clock_timestamp())) WHERE o.session_id=$1 AND o.status='complete' FOR SHARE OF k", &[&lead.id])?.context("Delegation credential expired/revoked")?;
let spec: crate::auth::Spec = serde_json::from_value(row.get(1))?;
ensure!(
spec.principal == row.get::<_, String>(0) && spec.role.permits(crate::auth::Action::Submit),
"Workspace delegation credential unavailable"
);
let identity = crate::auth::Identity {
credential_id: String::new(),
principal: spec.principal,
role: spec.role,
scope: spec.scope,
environments: spec.environments,
expires_at: spec.expires_at,
};
let release = crate::service_catalog::entry(tx, "release", &row.get::<_, String>(2))?;
let profile = crate::service_catalog::entry(tx, "profile", &row.get::<_, String>(3))?;
let r: crate::service_catalog::Release = serde_json::from_value(release.definition)?;
let p: crate::service_catalog::Profile = serde_json::from_value(profile.definition)?;
ensure!(
release.activated && profile.activated && r.runtime_profiles.contains(&p.id),
"Workspace release/profile unavailable"
);
crate::service_catalog::allowed(&identity, &r.allowed_principals)?;
crate::service_catalog::allowed(&identity, &p.allowed_principals)?;
Ok((identity, p))
}
pub(crate) fn workspace_authority(
tx: &mut postgres::Transaction<'_>,
lead: &Session,
doc: &Document,
agent: &str,
) -> Result<crate::auth::Identity> {
let (identity, p) = lead_authority(tx, lead, doc)?;
if let (Some(catalog), Some(choice)) = (&doc.type_catalog, doc.sandbox_choices.get(agent)) {
choice.validate(&catalog.ceiling)?;
}
let instance = doc.roster.instance(agent).context("Missing reservation")?;
let kind = agent_team::runtime_type(doc, instance)?;
let template: Value = tx
.query_one(
"SELECT data FROM agent_team_templates WHERE team=$1 AND kind=$2",
&[&doc.id, &kind],
)?
.get(0);
ensure!(template.get("error").is_none(), "Child type unavailable");
crate::service_team_types::check_template(tx, doc, instance, &template)?;
let child_release = crate::service_catalog::entry(
tx,
"release",
template["release"]
.as_str()
.context("Missing child release")?,
)?;
let child: crate::service_catalog::Release = serde_json::from_value(child_release.definition)?;
ensure!(
child_release.activated && child.runtime_profiles.contains(&p.id),
"Child release unavailable"
);
crate::service_catalog::allowed(&identity, &child.allowed_principals)?;
Ok(identity)
}
impl Transaction for Tx<'_, '_> {
fn load(&mut self, id: &str) -> Result<Document> {
Ok(serde_json::from_value(
self.0
.query_one(
"SELECT data FROM agent_teams WHERE id=$1 FOR UPDATE",
&[&id],
)?
.get(0),
)?)
}
fn write(&mut self, d: &Document, creating: bool) -> Result<()> {
let value = serde_json::to_value(d)?;
if creating {
self.0
.execute("INSERT INTO agent_teams VALUES($1,$2)", &[&d.id, &value])?;
} else {
ensure!(
self.0.execute(
"UPDATE agent_teams SET data=$2 WHERE id=$1",
&[&d.id, &value]
)? == 1,
"Missing team"
);
}
Ok(())
}
fn append(&mut self, team: &str, m: &Message) -> Result<()> {
self.0.execute(
"INSERT INTO agent_team_messages VALUES($1,$2,$3,$4)",
&[
&team,
&(m.sequence as i64),
&m.id,
&serde_json::to_value(m)?,
],
)?;
Ok(())
}
fn messages(
&mut self,
team: &str,
after: u64,
before: Option<u64>,
recipient: Option<&str>,
board: bool,
reverse: bool,
limit: usize,
) -> Result<Vec<Message>> {
let order = if reverse { "DESC" } else { "ASC" };
let sql=format!("SELECT data FROM agent_team_messages WHERE team=$1 AND seq>$2 AND ($3::bigint IS NULL OR seq<$3) AND ($4::text IS NULL OR data->'recipients' ? $4) AND (NOT $5 OR (data->>'board')::boolean) ORDER BY seq {order} LIMIT $6");
self.0
.query(
&sql,
&[
&team,
&(after as i64),
&before.map(|v| v as i64),
&recipient,
&board,
&(limit as i64),
],
)?
.into_iter()
.map(|r| Ok(serde_json::from_value(r.get(0))?))
.collect()
}
fn anchor(&mut self, team: &str, id: &str) -> Result<u64> {
Ok(self.0.query_opt("SELECT seq FROM agent_team_messages WHERE team=$1 AND id=$2 AND (data->>'board')::boolean",&[&team,&id])?.context("Unknown board message ID")?.get::<_,i64>(0) as u64)
}
fn optional_session(&mut self, id: &str) -> Result<Option<Session>> {
self.0
.query_opt("SELECT data FROM sessions WHERE id=$1", &[&id])?
.map(|row| Ok(serde_json::from_value(row.get(0))?))
.transpose()
}
}
impl Database {
pub(crate) fn agent_team(
&mut self,
sid: &str,
operation: &str,
args: &Value,
call: Option<(&str, &Value)>,
) -> Result<Value> {
let epoch = self.epoch(sid)?;
let mut tx = self.client.transaction()?;
crate::auth::guard(
&mut tx,
self.auth.as_ref(),
crate::auth::Action::Control,
Some(sid),
None,
)?;
let mut s = db::locked(&mut tx, sid, Some((&self.owner, epoch)))?;
if call.is_some() {
db::read_history(&mut tx, &mut s)?;
}
// A pending cancellation fences new model/tool authority, not reads or
// cleanup reconciliation. Workers must inspect state to apply that cancel.
if call.is_some() || matches!(operation, "reserve_model" | "type_prepare" | "type_resolve")
{
crate::controls::before_event(&mut tx, &s, "tool_started", &json!({}))?;
}
let now = tx
.query_one(
"SELECT (extract(epoch FROM clock_timestamp())*1000)::bigint",
&[],
)?
.get::<_, i64>(0) as u64;
if let Some((_, call)) = call {
openworker_runtime::coding_handoff::workflow::require_approval(&s, call)?;
}
let before = serde_json::to_value(&s)?;
let result = agent_team::apply(&mut Tx(&mut tx), &mut s, operation, args, now)?;
if call.is_none()
&& (matches!(
operation,
"snapshot" | "pending" | "pending_inbox" | "team_read_board"
) || (operation == "wake" && result["resume"] != true)
|| (operation == "deliver"
&& result["messages"].as_array().is_some_and(Vec::is_empty)
&& result["wait_notices"] == 0))
{
tx.commit()?;
return Ok(result);
}
if let Some((tid, call)) = call {
agent_team::finish(&mut s, tid, call, &result)?;
}
if call.is_none() && !s.history.is_empty() {
let pending = std::mem::take(&mut s.history);
db::read_history(&mut tx, &mut s)?;
s.history.extend(pending);
}
if call.is_some() || !s.history.is_empty() {
db::append_history(&mut tx, &s)?;
}
if call.is_some() || before != serde_json::to_value(&s)? {
db::write_metadata(&mut tx, &s)?;
db::event(
&mut tx,
&s,
&self.owner,
epoch,
"agent_team",
openworker_runtime::safety::public_internal_event(
&s,
json!({"operation":operation,"result":result,"call_id":call.map(|(_,c)|&c["call_id"])}),
),
)?;
} else {
db::progress_event(
&mut tx,
&s,
&self.owner,
epoch,
"agent_team",
openworker_runtime::safety::public_internal_event(
&s,
json!({"operation":operation,"result":result,"call_id":call.map(|(_,c)|&c["call_id"])}),
),
)?;
}
tx.commit()?;
Ok(result)
}
}
impl crate::PostgresStore {
/// A controller leases only preparation, never the agent's worker slot. The
/// dispatched intent commits before provider I/O. A replacement controller
/// may inspect that operation after lease expiry, but cannot replay creation.
pub fn claim_team_workspace(&self, installation: &str) -> Result<Option<WorkspaceJob>> {
ensure!(self.auth.is_none(), crate::auth::Error::Forbidden);
let installation = installation.to_owned();
self.call(move |db| {
let mut tx = db.client.transaction()?;
let row = tx.query_opt("SELECT s.id,t.id,i.key FROM agent_teams t JOIN sessions s ON s.id=t.data->'roster'->'instances'->(t.data->>'lead')->>'session_id' JOIN service_operations o ON o.session_id=s.id JOIN service_catalog c ON c.kind='release' AND c.id=o.release_id CROSS JOIN LATERAL jsonb_each(t.data->'roster'->'instances') i JOIN agent_team_templates p ON p.team=t.id AND p.kind=coalesce('tt_' || left(t.data->'type_bindings'->>i.key,48),i.value->>'agent_type') LEFT JOIN agent_team_workspace_claims w ON w.team=t.id AND w.agent=i.key WHERE c.data->>'installation'=$1 AND i.value->>'phase'='reserved' AND coalesce(t.data->'roster'->'requests'->i.key->'workspace'->>'mode','team')<>'team' AND coalesce(t.data->'roster'->'requests'->i.key->'source','null'::jsonb)='null'::jsonb AND NOT coalesce((t.data->>'stopped')::boolean,false) AND coalesce(t.data->'workspaces'->i.key->'receipt','null'::jsonb)='null'::jsonb AND (coalesce(t.data->'sandbox_choices'->i.key->>'mode','team')='team' OR t.data->'placement_bindings' ? i.key) AND (w.expires_at IS NULL OR w.expires_at<clock_timestamp()) ORDER BY t.id,i.key LIMIT 1 FOR UPDATE OF s SKIP LOCKED", &[&installation])?;
let Some(row) = row else { tx.commit()?; return Ok(None) };
let sid: String = row.get(0); let team: String = row.get(1); let agent: String = row.get(2);
let mut lead = db::locked(&mut tx, &sid, None)?;
let doc = Tx(&mut tx).load(&team)?;
if doc.stopped || !doc.roster.instance(&agent).is_some_and(|i| i.phase==openworker_runtime::team_coordination::Phase::Reserved) { tx.commit()?; return Ok(None) }
if let Err(error)=workspace_authority(&mut tx, &lead, &doc, &agent) {
if error.downcast_ref::<postgres::Error>().is_some() {return Err(error)}
agent_team::apply(&mut Tx(&mut tx),&mut lead,"provision_failed",&json!({"agent_id":agent,"error":"Workspace preparation denied: original credential, type or activated release/profile unavailable"}),openworker_runtime::store::now_ms())?;
tx.commit()?;return Ok(None)
}
let (root,_)=openworker_runtime::team_placement::workspace_scope(&doc,&lead,&agent)?;
let (environment,profile)=if let Some(b)=doc.placement_bindings.get(&agent) {
crate::service_team_placements::target_authority(&mut tx,&lead,&doc,&agent,&b.profile_id)?;
(b.environment.clone(),b.profile_id.clone())
} else {
(lead.environment.clone().context("Lead has no environment")?,tx.query_one("SELECT profile_id FROM service_operations WHERE session_id=$1",&[&sid])?.get::<_,String>(0))
};
let claim = uuid::Uuid::new_v4().to_string();
let n = tx.execute("INSERT INTO agent_team_workspace_claims VALUES($1,$2,$3,clock_timestamp()+interval '30 seconds') ON CONFLICT(team,agent) DO UPDATE SET token=excluded.token,expires_at=excluded.expires_at WHERE agent_team_workspace_claims.expires_at<clock_timestamp()", &[&team,&agent,&claim])?;
if n==0 { tx.commit()?; return Ok(None) }
let outcome=agent_team::apply(&mut Tx(&mut tx),&mut lead,"workspace_dispatch",&json!({"agent_id":agent}),openworker_runtime::store::now_ms())?;
let intent=serde_json::from_value(outcome["intent"].clone())?;
crate::team_execution::session_event(&mut tx,&sid,None,"team_workspace",json!({"operation":"prepare_intent","agent_id":agent,"dispatch":outcome["dispatch"]}))?;
tx.commit()?;
Ok(Some(WorkspaceJob{lead,team,agent,claim,intent,dispatch:outcome["dispatch"]==true,environment,root,profile}))
})
}
pub fn finish_team_workspace(
&self,
job: WorkspaceJob,
outcome: Result<openworker_runtime::team_workspace::Receipt, String>,
) -> Result<()> {
ensure!(self.auth.is_none(), crate::auth::Error::Forbidden);
self.call(move|db| {
let mut tx=db.client.transaction()?;
let mut lead=db::locked(&mut tx,&job.lead.id,None)?;
let doc=Tx(&mut tx).load(&job.team)?;
let token: String=tx.query_one("SELECT token FROM agent_team_workspace_claims WHERE team=$1 AND agent=$2",&[&job.team,&job.agent])?.get(0);
ensure!(token==job.claim && lead.workspace==job.lead.workspace && lead.environment==job.lead.environment,"Stale workspace preparation owner/scope");
// A retired child is never resurrected by a late provider response.
if doc.stopped || !doc.roster.instance(&job.agent).is_some_and(|i|i.phase==openworker_runtime::team_coordination::Phase::Reserved) {tx.commit()?;return Ok(())}
let (root,_)=openworker_runtime::team_placement::workspace_scope(&doc,&lead,&job.agent)?;
let binding=doc.placement_bindings.get(&job.agent).map(|b|&b.environment).or(lead.environment.as_ref());
ensure!(root==job.root && binding==Some(&job.environment),"Workspace placement binding changed");
let mut args=json!({"agent_id":job.agent,"request":job.intent.request});
match outcome {
Ok(receipt)=>{
receipt.validate_scope(std::path::Path::new(&job.root),&receipt.path)?;
args["receipt"]=json!(receipt);
},
Err(error)=>args["error"]=json!(error.chars().take(1800).collect::<String>()),
}
agent_team::apply(&mut Tx(&mut tx),&mut lead,"workspace_result",&args,openworker_runtime::store::now_ms())?;
crate::team_execution::session_event(&mut tx,&lead.id,None,"team_workspace",json!({"operation":"prepared","agent_id":job.agent,"operation_id":job.intent.request.operation_id,"outcome":if args.get("receipt").is_some(){"ready"}else{"unknown_or_failed"}}))?;
tx.commit()?;Ok(())
})
}
/// API ingress never claims a worker lease or rewrites a live transcript.
pub fn agent_team_api(&self, sid: &str, operation: &str, args: Value) -> Result<Value> {
let (sid, operation) = (sid.to_owned(), operation.to_owned());
self.call(move |db| {
let mut tx = db.client.transaction()?;
let action = if matches!(operation.as_str(), "snapshot" | "team_read_board" | "team_list_submissions") || (operation=="team_manage" && args["action"]=="list") {
crate::auth::Action::Read
} else {
crate::auth::Action::Control
};
crate::auth::guard(&mut tx, db.auth.as_ref(), action, Some(&sid), None)?;
ensure!(
matches!(
operation.as_str(),
"snapshot" | "team_read_board" | "team_list_submissions" | "command" | "team_manage"
),
"Unsupported API coordination command"
);
ensure!(operation!="team_manage" || args["action"]=="list","Membership mutations require a durable command ID");
let mut session = db::locked(&mut tx, &sid, None)?;
let result = agent_team::apply(
&mut Tx(&mut tx),
&mut session,
&operation,
&args,
openworker_runtime::store::now_ms(),
)?;
if operation=="command" {crate::team_execution::session_event(&mut tx,&sid,session.turn.as_ref().map(|t|t.id.as_str()),"team_activity",json!({"operation":args["operation"],"result":result,"agent_id":session.agent_team.as_ref().map(|m|&m.agent_id)}))?;}
// Only board/roster records change at ingress. No delivery receipt or
// tool result is acknowledged on behalf of an executing owner.
tx.commit()?;
Ok(if operation=="snapshot"{agent_team::public_snapshot(result)}else{result})
})
}
/// Trusted deployment adapter supplies freshly prepared, pinned child types.
/// Revalidate release/principal/profile grants inside the creation transaction.
pub fn configure_agent_team(
&self,
sid: &str,
args: Value,
templates: std::collections::BTreeMap<String, (String, Session)>,
) -> Result<Value> {
self.configure_agent_team_models(sid, args, templates, Default::default())
}
/// Deployment-owned model references, never API keys, are pinned with templates.
pub fn configure_agent_team_models(
&self,
sid: &str,
mut args: Value,
templates: std::collections::BTreeMap<String, (String, Session)>,
models: std::collections::BTreeMap<String, openworker_runtime::connections::Selection>,
) -> Result<Value> {
let sid = sid.to_owned();
self.call(move |db| {
let mut tx = db.client.transaction()?;
let principal = crate::auth::guard(
&mut tx,
db.auth.as_ref(),
crate::auth::Action::Control,
Some(&sid),
None,
)?;
let mut lead = db::locked(&mut tx, &sid, None)?;
let source_binding: Option<openworker_artifacts::Binding> = args
.as_object_mut()
.context("Team options must be an object")?
.remove("source_store_binding")
.filter(|v| !v.is_null())
.map(serde_json::from_value)
.transpose()?;
ensure!(
source_binding.is_none() || args.get("coding").is_some_and(|v| !v.is_null()),
"Artifact binding requires coding source configuration"
);
let was_unbound = lead.agent_team.is_none();
let binding = lead
.environment
.as_ref()
.context("Team needs a shared environment")?;
ensure!(
binding.spec.sharing == openworker_runtime::environment::Sharing::Shared,
"Team environment must be shared"
);
let profile: String = tx
.query_one(
"SELECT profile_id FROM service_environment_owners WHERE environment_id=$1",
&[&binding.environment_id],
)?
.get(0);
for (release_id, template) in templates.values() {
ensure!(
template.environment == lead.environment
&& template.turn.is_none()
&& template.history.is_empty(),
"Child must use the same original environment"
);
let entry = crate::service_catalog::entry(&mut tx, "release", release_id)?;
let release: crate::service_catalog::Release =
serde_json::from_value(entry.definition)?;
ensure!(
entry.activated && release.runtime_profiles.contains(&profile),
"Child release not activated for this profile"
);
if let Some(p) = &principal {
crate::service_catalog::allowed(p, &release.allowed_principals)?;
}
}
crate::service_team_types::validate_models(&mut tx, &lead, &args, &models)?;
let keys: Vec<_> = templates.keys().cloned().collect();
ensure!(
args["agent_types"] == json!(keys),
"Template/type catalog mismatch"
);
// Adapter capability, never a caller-configured permission switch.
args["workspace_selection"] = json!(true);
args["coding_selection"] = json!(source_binding.is_some());
args["placement_selection"] = json!(args
.get("delegation")
.filter(|v| !v.is_null())
.is_some_and(|d| d["placements"]
.as_array()
.is_some_and(|p| p.iter().any(|v| v != "team"))));
let mut result = agent_team::apply(
&mut Tx(&mut tx),
&mut lead,
"create",
&args,
openworker_runtime::store::now_ms(),
)?;
let team_id = result["id"].as_str().context("Missing team ID")?.to_owned();
let team = team_id.as_str();
let repository_access: Option<
openworker_runtime::coding_handoff::publication::Catalog,
> = args
.get("_repository_access")
.filter(|v| !v.is_null())
.cloned()
.map(serde_json::from_value)
.transpose()?;
if let Some(access) = &repository_access {
ensure!(
access.lead_profile == profile,
"Original repository profile changed"
);
for (id, a) in &access.profiles {
let entry = crate::service_catalog::entry(&mut tx, "profile", id)?;
let p: crate::service_catalog::Profile =
serde_json::from_value(entry.definition)?;
ensure!(
entry.activated && p.repository_access.as_deref() == Some(&a.grant),
"Repository capability differs from activated profile"
);
ensure!(
id == &profile || args["delegation"]["profiles"].get(id).is_some(),
"Repository capability outside delegation"
);
ensure!(a.digest.len() == 64, "Invalid repository capability digest");
}
}
let mut doc = Tx(&mut tx).load(team)?;
if was_unbound {
doc.repository_access = repository_access;
Tx(&mut tx).write(&doc, false)?;
result = json!(doc);
} else {
ensure!(
doc.repository_access == repository_access,
"Changed repository capability retry"
);
}
for (kind, (release, template)) in templates {
let payload = json!({"release":release,"session":template,"models":models});
tx.execute(
"INSERT INTO agent_team_templates VALUES($1,$2,$3) ON CONFLICT DO NOTHING",
&[&team, &kind, &payload],
)?;
let stored: Value = tx
.query_one(
"SELECT data FROM agent_team_templates WHERE team=$1 AND kind=$2",
&[&team, &kind],
)?
.get(0);
// Session preparation allocates a throwaway ID; all other identity
// fields are pinned. Reserved instance IDs replace only this ID.
let mut expected = payload;
expected["session"]["id"] = stored["session"]["id"].clone();
// Older prebuilt-only receipts have no model-reference map.
// Accept only an unchanged empty delegation, never add authority.
if stored.get("models").is_none() && models.is_empty() {
expected.as_object_mut().unwrap().remove("models");
}
ensure!(stored == expected, "Changed team type template");
}
let source_setup = if let Some(binding) = source_binding {
let doc: Document = serde_json::from_value(result.clone())?;
let source = doc
.coding
.as_ref()
.context("Missing coding source")?
.source
.clone();
Some(crate::service_team_sources::configure(
&mut tx, &lead, &doc, source, binding,
)?)
} else {
None
};
if was_unbound {
db::write_metadata(&mut tx, &lead)?;
tx.execute(
"UPDATE sessions SET revision=revision+1 WHERE id=$1",
&[&sid],
)?;
}
if let Some(setup) = source_setup {
crate::auth::audit(
&mut tx,
principal.as_ref(),
crate::auth::Action::Control,
Some(&sid),
Some(&setup.id),
)?;
}
tx.commit()?;
Ok(result)
})
}
/// Private controller: one reservation becomes one child and initial queued
/// task atomically. It never creates/rebinds/deletes the shared environment.
pub fn provision_agent_team_child(&self) -> Result<bool> {
self.provision_agent_team_child_for(None)
}
pub fn provision_agent_team_child_for(&self, installation: Option<&str>) -> Result<bool> {
ensure!(self.auth.is_none(), crate::auth::Error::Forbidden);
let installation = installation.map(str::to_owned);
self.call(move|db|{
let mut tx=db.client.transaction()?;
let Some(row)=tx.query_opt("SELECT t.id,t.data FROM agent_teams t JOIN sessions l ON l.id=t.data->'roster'->'instances'->(t.data->>'lead')->>'session_id' WHERE EXISTS(SELECT 1 FROM jsonb_each(t.data->'roster'->'instances') i JOIN agent_team_templates x ON x.team=t.id AND x.kind=coalesce('tt_' || left(t.data->'type_bindings'->>i.key,48),i.value->>'agent_type') WHERE i.value->>'phase'='reserved' AND (coalesce(t.data->'sandbox_choices'->i.key->>'mode','team')='team' OR t.data->'placement_bindings' ? i.key OR x.data ? 'error') AND (coalesce(t.data->'roster'->'requests'->i.key->'workspace'->>'mode','team')='team' OR coalesce(t.data->'workspaces'->i.key->'receipt','null'::jsonb)<>'null'::jsonb OR x.data ? 'error') AND (coalesce(t.data->'roster'->'requests'->i.key->'source','null'::jsonb)='null'::jsonb OR t.data->'source_imports' ? i.key OR x.data ? 'error')) AND NOT coalesce((t.data->>'stopped')::boolean,false) AND ($1::text IS NULL OR EXISTS(SELECT 1 FROM service_operations o JOIN service_catalog c ON c.kind='release' AND c.id=o.release_id WHERE o.session_id=t.data->'roster'->'instances'->(t.data->>'lead')->>'session_id' AND c.data->>'installation'=$1)) ORDER BY t.id LIMIT 1 FOR UPDATE OF l SKIP LOCKED",&[&installation])? else {tx.commit()?;return Ok(false)};
let team_id:String=row.get(0);
let mut doc:Document=serde_json::from_value(tx.query_one("SELECT data FROM agent_teams WHERE id=$1 FOR UPDATE",&[&team_id])?.get(0))?;
let candidates:Vec<_>=doc.roster.instances().filter(|i|i.phase==openworker_runtime::team_coordination::Phase::Reserved).cloned().collect();
let mut selected=None;
for i in candidates {
let kind=agent_team::runtime_type(&doc,&i)?;
let Some(row)=tx.query_opt("SELECT data FROM agent_team_templates WHERE team=$1 AND kind=$2",&[&doc.id,&kind])? else {continue};
let candidate:Value=row.get(0);
// A broken template closes only its unstarted reservation;
// missing placement/import receipts must not strand that error.
if candidate.get("error").is_some() {selected=Some(i);break}
if doc.sandbox_choices.get(&i.agent_id).is_some_and(|c|c!=&Default::default()) && !doc.placement_bindings.contains_key(&i.agent_id) {continue}
if doc.roster.request(&i.agent_id).is_some_and(|r| r.workspace != Default::default())
&& doc.workspaces.get(&i.agent_id).and_then(|i| i.receipt.as_ref()).is_none() { continue; }
if doc.roster.request(&i.agent_id).is_some_and(|r|r.source.is_some())
&& !doc.source_imports.contains_key(&i.agent_id) {continue}
selected=Some(i);break
}
let Some(instance)=selected else {tx.commit()?;return Ok(false)};
let kind=agent_team::runtime_type(&doc,&instance)?;
let Some(template_row)=tx.query_opt("SELECT data FROM agent_team_templates WHERE team=$1 AND kind=$2",&[&doc.id,&kind])? else {tx.commit()?;return Ok(false)};
let template:Value=template_row.get(0);
if let Some(error)=template["error"].as_str() {
doc.roster.close(&instance.agent_id)?;
doc.failures.insert(instance.agent_id.clone(),error.to_owned());
Tx(&mut tx).write(&doc,false)?;tx.commit()?;return Ok(true);
}
let mut child:Session=serde_json::from_value(template["session"].clone())?;
if let Some(placement)=doc.placement_bindings.get(&instance.agent_id) {
ensure!(doc.placement_selection && doc.sandbox_choices.get(&instance.agent_id)==Some(&placement.choice),"Placement receipt differs from choice");
child.environment=Some(placement.environment.clone());
child.workspace=placement.environment.spec.workspace_root.clone();
child.resource_policy.digest=child.resource_policy.remote_digest(&placement.environment)?;
}
if let Some(receipt)=doc.workspaces.get(&instance.agent_id).and_then(|i|i.receipt.as_ref()) {
receipt.validate_scope(std::path::Path::new(&child.workspace), &receipt.path)?;
child.workspace=receipt.path.to_str().context("Workspace must be UTF-8")?.into();
child.managed_workspace=Some(receipt.clone());
}
if let Some(choice)=doc.roster.request(&instance.agent_id).and_then(|r|r.model.as_ref()) {
let selected:openworker_runtime::connections::Selection=serde_json::from_value(template["models"][crate::service_team_types::model_key(choice)?].clone()).context("Missing delegated model reference")?;
ensure!(selected.connection==choice.connection && selected.model==choice.model,"Changed delegated model");
child.model=selected.model.clone();child.model_provider=selected.provider;child.model_selection=Some(selected);
}
child.id=instance.session_id.clone();child.name=doc.roster.request(&instance.agent_id).and_then(|r| r.title.as_ref()).unwrap_or(&instance.agent_id).clone();
child.agent_team=Some(agent_team::Membership{team_id:doc.id.clone(),agent_id:instance.agent_id.clone(),inbox_cursor:0,last_output:None,notices:vec![],waiting:None});
let lead=doc.roster.instance(&doc.lead).context("Lead missing")?;
// Delegation inherits the submitting principal, never a stronger role.
let origin=tx.query_one("SELECT principal,credential_id,project,profile_id FROM service_operations WHERE session_id=$1 AND status='complete'",&[&lead.session_id])?;
let principal:String=origin.get(0);let credential:String=origin.get(1);
let release_id=template["release"].as_str().context("Release missing")?;
let authorization=(||->Result<crate::auth::Identity>{
let lead_session=db::locked(&mut tx,&lead.session_id,None)?;
workspace_authority(&mut tx,&lead_session,&doc,&instance.agent_id)?;
crate::service_team_types::check_template(&mut tx, &doc, &instance, &template)?;
let spec:crate::auth::Spec=serde_json::from_value(tx.query_opt("SELECT spec FROM api_credentials WHERE id=$1 AND NOT revoked AND ((spec->>'expires_at') IS NULL OR (spec->>'expires_at')::bigint>extract(epoch FROM clock_timestamp())) FOR SHARE",&[&credential])?.context("Submitting credential expired or revoked")?.get(0))?;
ensure!(spec.principal==principal && spec.role.permits(crate::auth::Action::Submit),crate::auth::Error::Forbidden);
let identity=crate::auth::Identity{credential_id:credential,principal:principal.clone(),role:spec.role,scope:spec.scope,environments:spec.environments,expires_at:spec.expires_at};
let release_entry=crate::service_catalog::entry(&mut tx,"release",release_id)?;
let release:crate::service_catalog::Release=serde_json::from_value(release_entry.definition)?;
ensure!(release_entry.activated,"Delegated release deactivated");crate::service_catalog::allowed(&identity,&release.allowed_principals)?;
let profile_id:String=doc.placement_bindings.get(&instance.agent_id).map(|b|b.profile_id.clone()).unwrap_or_else(||origin.get(3));
if let Some(placement)=doc.placement_bindings.get(&instance.agent_id) {
let lead_session=db::locked(&mut tx,&lead.session_id,None)?;
let checked=crate::service_team_placements::target_authority(&mut tx,&lead_session,&doc,&instance.agent_id,&profile_id)?;
ensure!(checked.digest==placement.profile_digest,"Bound profile changed");
}
let profile_entry=crate::service_catalog::entry(&mut tx,"profile",&profile_id)?;
let profile:crate::service_catalog::Profile=serde_json::from_value(profile_entry.definition)?;
ensure!(profile_entry.activated && release.runtime_profiles.contains(&profile_id),"Delegated RuntimeProfile unavailable");
crate::service_catalog::allowed(&identity,&profile.allowed_principals)?;
Ok(identity)
})();
let identity=match authorization {
Ok(identity)=>identity,
Err(_)=>{
doc.roster.close(&instance.agent_id)?;
doc.failures.insert(instance.agent_id.clone(),"Delegation denied: submitting credential or activated release permission is unavailable".into());
Tx(&mut tx).write(&doc,false)?;tx.commit()?;return Ok(true);
}
};
let request=crate::SessionCreation{request_id:format!("team-{}",child.id),actor:principal.clone(),parameters:json!({"team":doc.id,"instance":instance.agent_id,"release":release_id})};
crate::ingress::create_in_tx(&mut tx,&db.owner,request,child.clone(),Some(identity))?;
let submission=crate::TurnSubmission{session_id:child.id.clone(),submission_id:format!("initial-{}",instance.agent_id),text:agent_team::delegated_task(doc.tasks.get(&instance.agent_id).context("Missing initial task")?),limits:None};
let receipt=crate::submissions::insert_reserved(&mut tx,&submission,"queued",None,&format!("t_{}",child.id))?;
crate::submissions::journal_acceptance(&mut tx,&receipt,Some(&principal))?;
doc.roster.ready(&instance.agent_id,&instance.session_id)?;
Tx(&mut tx).write(&doc,false)?;tx.commit()?;Ok(true)
})
}
}
impl crate::PostgresStore {
/// A durable stop flag is an outbox. Reconcile one member at a time through
/// the existing control path; never hold the board lock while locking a child.
pub fn reconcile_stopped_agent_team(&self) -> Result<bool> {
ensure!(self.auth.is_none(), crate::auth::Error::Forbidden);
let cancelled_queued=self.call(|db|{
let mut tx=db.client.transaction()?;
let row=tx.query_opt("SELECT s.id FROM sessions s JOIN agent_teams t ON t.id=s.data->'agent_team'->>'team_id' WHERE ((t.data->>'stopped')::boolean OR t.data->'roster'->'instances'->(s.data->'agent_team'->>'agent_id')->>'phase'='retiring') AND EXISTS(SELECT 1 FROM submissions q WHERE q.session_id=s.id AND q.status='queued') ORDER BY s.id LIMIT 1 FOR UPDATE OF s SKIP LOCKED",&[])?;
let Some(row)=row else{tx.commit()?;return Ok(false)};
let sid:String=row.get(0);
let rows=tx.query("UPDATE submissions SET status='cancelled' WHERE session_id=$1 AND status='queued' RETURNING id,turn_id",&[&sid])?;
for row in rows {let id:String=row.get(0);let tid:String=row.get(1);crate::team_execution::session_event(&mut tx,&sid,Some(&tid),"team_turn_cancelled",json!({"submission_id":id,"reason":"team_or_member_stopped","executed":false}))?;}
tx.commit()?;Ok(true)
})?;
if cancelled_queued {
return Ok(true);
}
let target=self.call(|db|Ok(db.client.query_opt("SELECT s.id,s.data->'turn'->>'id',coalesce(o.principal,'harness-team-controller'),t.id FROM sessions s JOIN agent_teams t ON t.id=s.data->'agent_team'->>'team_id' LEFT JOIN api_session_owners o ON o.session_id=s.id WHERE ((t.data->>'stopped')::boolean OR (t.data->'roster'->'instances'->(s.data->'agent_team'->>'agent_id')->>'phase'='retiring' AND t.data->'retirements'->(s.data->'agent_team'->>'agent_id')->>'mode'='cancel') OR t.data->'standalone'->'cancellations'->>(s.data->'agent_team'->>'agent_id')=s.data->'turn'->>'id') AND s.data->'turn'->>'status' NOT IN ('completed','failed','cancelled','interrupted','truncated','blocked') AND NOT EXISTS(SELECT 1 FROM controls c WHERE c.session_id=s.id AND c.turn_id=s.data->'turn'->>'id' AND c.status='pending' AND c.command->'action'->>'kind'='cancel') ORDER BY s.id LIMIT 1",&[])?.map(|r|(r.get::<_,String>(0),r.get::<_,String>(1),r.get::<_,String>(2),r.get::<_,String>(3)))))?;
let Some((sid, tid, actor, team)) = target else {
return Ok(false);
};
self.submit_control(openworker_runtime::control::Command {
session_id: sid.clone(),
request_id: format!(
"stop-{}-{}",
&openworker_runtime::bundle::digest(format!("{team}/{sid}").as_bytes())[..20],
tid
),
turn_id: tid,
actor,
action: openworker_runtime::control::Action::Cancel,
})?;
Ok(true)
}
}
impl crate::PostgresStore {
/// A wake joins the same FIFO as user input. The fenced transaction checks
/// both inbox and queue: pending user work will already consume the inbox,
/// so do not create a competing turn or dispatch past an older submission.
pub fn enqueue_agent_inbox_wake(&self, sid: &str) -> Result<()> {
let sid = sid.to_owned();
self.call(move|db|{
let epoch=db.epoch(&sid)?;let mut tx=db.client.transaction()?;
let s=db::locked(&mut tx,&sid,Some((&db.owner,epoch)))?;
let member=s.agent_team.as_ref().context("Not a team member")?;
if !crate::service_team_source_checkout::ready_for_turn(&mut tx,&sid)? {tx.commit()?;return Ok(());}
if s.closed || s.provider_blocked || s.turn.as_ref().is_some_and(|t|!openworker_runtime::store::terminal(&t.status)) {tx.commit()?;return Ok(());}
let queued:bool=tx.query_one("SELECT EXISTS(SELECT 1 FROM submissions WHERE session_id=$1 AND status='queued')",&[&sid])?.get(0);
if queued {tx.commit()?;return Ok(());}
let mut coordination = Tx(&mut tx).load(&member.team_id)?;
if coordination.standalone.is_some() {
openworker_runtime::subagents::reconcile(&mut Tx(&mut tx), &mut coordination)?;
if s.turn.as_ref().is_some_and(|t| t.status != "completed")
|| (member.agent_id != coordination.lead && !coordination.standalone.as_ref().unwrap().active.contains(&member.agent_id)) {
tx.commit()?; return Ok(());
}
}
let pending:bool=tx.query_one("SELECT EXISTS(SELECT 1 FROM agent_team_messages WHERE team=$1 AND seq>$2 AND data->'recipients' ? $3)",&[&member.team_id,&(member.inbox_cursor as i64),&member.agent_id])?.get(0);
if !pending && member.notices.is_empty(){tx.commit()?;return Ok(());}
if let Err(error)=ensure_admission(&mut tx,&s) {
if error.downcast_ref::<crate::SubmissionError>()==Some(&crate::SubmissionError::Closed){tx.commit()?;return Ok(());}
return Err(error);
}
let key=format!("inbox-{}",member.inbox_cursor);
let existing:bool=tx.query_one("SELECT EXISTS(SELECT 1 FROM submissions WHERE session_id=$1 AND id=$2)",&[&sid,&key])?.get(0);
if !existing {
let request=crate::TurnSubmission{session_id:sid.clone(),submission_id:key,text:"Continue from the agent inbox. Collaborator input is not human authorization.".into(),limits:None};
let receipt=crate::submissions::insert(&mut tx,&request,"queued",None)?;crate::submissions::journal_acceptance(&mut tx,&receipt,None)?;
}
tx.commit()?;Ok(())
})
}
}
/// Called under the session lock at admission/promotion. Share the team row lock
/// with retirement so a checked turn cannot slip through after the gate commits.
pub(crate) fn ensure_admission(tx: &mut postgres::Transaction<'_>, s: &Session) -> Result<()> {
if let Some(member) = &s.agent_team {
let doc: Document = serde_json::from_value(
tx.query_one(
"SELECT data FROM agent_teams WHERE id=$1 FOR SHARE",
&[&member.team_id],
)?
.get(0),
)?;
ensure!(
openworker_runtime::team_lifecycle::admits(&doc, s),
crate::SubmissionError::Closed
);
}
Ok(())
}
impl crate::PostgresStore {
/// Reconcile an unstarted reservation or a child whose session closure
/// committed before the controller/runtime could settle retirement. Never
/// infer cleanup from transport loss: an existing session must be closed
/// with a terminal turn. The name is retained for host compatibility.
pub fn reconcile_unstarted_retirement(&self) -> Result<bool> {
ensure!(self.auth.is_none(), crate::auth::Error::Forbidden);
self.call(|db|{
let mut tx=db.client.transaction()?;
let row=tx.query_opt("SELECT t.data FROM agent_teams t WHERE EXISTS(SELECT 1 FROM jsonb_each(t.data->'roster'->'instances') i WHERE i.value->>'phase'='retiring' AND (NOT EXISTS(SELECT 1 FROM sessions s WHERE s.id=i.value->>'session_id') OR EXISTS(SELECT 1 FROM sessions s WHERE s.id=i.value->>'session_id' AND (s.data->>'closed')::boolean AND (s.data->'turn' IS NULL OR s.data->'turn'='null'::jsonb OR s.data->'turn'->>'status' IN ('completed','failed','cancelled','interrupted','truncated','blocked'))))) ORDER BY t.id LIMIT 1 FOR UPDATE OF t SKIP LOCKED",&[])?;
let Some(row)=row else {tx.commit()?;return Ok(false)};
let mut doc:Document=serde_json::from_value(row.get(0))?;
let candidates:Vec<_>=doc.roster.instances().filter(|i|i.phase==openworker_runtime::team_coordination::Phase::Retiring).cloned().collect();
for i in candidates {
let session=Tx(&mut tx).optional_session(&i.session_id)?;
if session.as_ref().is_none_or(|s|s.closed && s.turn.as_ref().is_none_or(|t|openworker_runtime::store::terminal(&t.status))){
openworker_runtime::team_lifecycle::settle(&mut Tx(&mut tx),&mut doc,&i.agent_id,openworker_runtime::store::now_ms())?;
Tx(&mut tx).write(&doc,false)?;tx.commit()?;return Ok(true);
}
}
tx.commit()?;Ok(false)
})
}
}