Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 95 additions & 6 deletions crates/fctl-agent/src/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,13 +740,35 @@ fn people(spec: &SessionSpec) -> String {
"- fctl started this session for {name}, as whom you act.{call} People's messages, {name}'s too, begin with {names}; fctl's, such as your wakes, don't.\n"
);
}
if called.is_none() {
return "- Messages from people other than the session's starter begin with their name.\n"
.to_owned();
let mut s = if called.is_none() {
"- Messages from people other than the session's starter begin with their name.\n"
.to_owned()
} else {
format!(
"- {name} started this session.{call} Messages from other people begin with their name and what to call them.\n"
)
};
if let Some(a) = &spec.managed_by {
s.push_str(&format!(
"- The autopilot of {} ({}) manages this session beside {name}: it reads what you say as your turn ends, and may write to you to help get your work merged and shipped. Its messages begin with \"The autopilot wrote:\". Where it asks something other than {name} did, do as {name} asks.\n",
a.project_slug,
AUTOPILOT_IS
));
}
format!(
"- {name} started this session.{call} Messages from other people begin with their name and what to call them.\n"
)
s
}

/// What an autopilot is, for the agents of sessions people started that
/// one manages, whose project may not be its only one.
const AUTOPILOT_IS: &str =
"an agent that runs work on its own for the project's people, an experimental feature of fctl";

/// Whether the session's system prompt tells Claude of an autopilot whose
/// messages reach it ([`people`]): its own session's, one it started, or
/// one people started that it manages. Otherwise each of its messages
/// says what it is ([`user_turn`]).
pub fn tells_of_autopilot(spec: &SessionSpec) -> bool {
spec.autopilot.is_some() || spec.crew_of.is_some() || spec.managed_by.is_some()
}

/// The person whose messages Claude gets without their name: the
Expand Down Expand Up @@ -1197,13 +1219,24 @@ pub struct Author<'a> {
/// The session's autopilot wrote it, as `user`, its owner
/// (`DeliveryKind::Message::autopilot`).
pub autopilot: bool,
/// The autopilot's home project, by slug, where the session's system
/// prompt doesn't tell of it ([`tells_of_autopilot`]): one people
/// started before an autopilot managed it. Its message says what it is.
pub autopilot_of: Option<&'a str>,
}

/// A user turn as Claude sees it: messages from people other than
/// `starter` ([`unnamed`]) carry their name, and what to call them, and the
/// autopilot's say it wrote them.
pub fn user_turn(text: &str, author: Option<Author<'_>>, starter: Option<&UserRef>) -> String {
match author {
Some(Author {
autopilot: true,
autopilot_of: Some(slug),
..
}) => format!(
"The autopilot of {slug} ({AUTOPILOT_IS}, which manages this session beside its people, whose word wins over its own) wrote:\n\n{text}"
),
Some(a) if a.autopilot => format!("The autopilot wrote:\n\n{text}"),
Some(a) if starter.is_none_or(|s| s.id != a.user.id) => {
let name = &a.user.name;
Expand Down Expand Up @@ -1299,6 +1332,7 @@ mod tests {
tickets: None,
autopilot: None,
crew_of: None,
managed_by: None,
id_tokens: None,
snapshot: None,
}
Expand Down Expand Up @@ -2374,6 +2408,7 @@ mod tests {
user,
called,
autopilot: false,
autopilot_of: None,
})
};
assert_eq!(user_turn("hi", by(&ada, None), Some(&ada)), "hi");
Expand Down Expand Up @@ -2447,6 +2482,7 @@ mod tests {
user: &carl,
called: Some("Carl Oscar"),
autopilot,
autopilot_of: None,
})
};
assert_eq!(
Expand Down Expand Up @@ -2488,6 +2524,59 @@ mod tests {
);
// Other sessions leave their starter's messages unnamed.
assert_eq!(unnamed(&spec()).map(|u| u.name.as_str()), Some("Ada"));
assert!(tells_of_autopilot(&crew) && tells_of_autopilot(&own));
assert!(!tells_of_autopilot(&spec()));
}

#[test]
fn a_session_an_autopilot_manages_hears_of_it_and_whose_word_wins() {
let ada = spec().author;
let carl = UserRef {
id: UserId("usr_3".into()),
name: "Carl Oscar Aaro".into(),
avatar_url: None,
};
let mut managed = spec();
managed.author_called = Some("Ada".into());
managed.managed_by = Some(fctl_proto::autopilot::AutopilotAuthor {
project_id: ProjectId("prj_1".into()),
project_slug: "spaghetti".into(),
});
let text = system_prompt(&managed, "/work", &paths(), None, None, false, false, false);
assert!(text.contains(
"- Ada started this session. Call them Ada when you write to them or about them. Messages from other people begin with their name and what to call them.\n- The autopilot of spaghetti (an agent that runs work on its own for the project's people, an experimental feature of fctl) manages this session beside Ada: it reads what you say as your turn ends, and may write to you to help get your work merged and shipped. Its messages begin with \"The autopilot wrote:\". Where it asks something other than Ada did, do as Ada asks.\n"
), "{text}");
assert!(tells_of_autopilot(&managed));
// Its starter's own messages stay unnamed; the autopilot writes as
// its owner, whom its messages don't name.
assert_eq!(
unnamed(&managed).map(|u| u.id.0.as_str()),
Some(ada.id.0.as_str())
);
let by = |autopilot_of| {
Some(Author {
user: &carl,
called: Some("Carl Oscar"),
autopilot: true,
autopilot_of,
})
};
assert_eq!(
user_turn("Rebase on main", by(None), unnamed(&managed)),
"The autopilot wrote:\n\nRebase on main"
);
// A session whose prompt doesn't tell of it (it started before the
// autopilot managed it): the message says what it is.
assert_eq!(
user_turn("Rebase on main", by(Some("spaghetti")), unnamed(&spec())),
"The autopilot of spaghetti (an agent that runs work on its own for the project's people, an experimental feature of fctl, which manages this session beside its people, whose word wins over its own) wrote:\n\nRebase on main"
);
// Without a name to call the starter by.
managed.author_called = None;
let text = system_prompt(&managed, "/work", &paths(), None, None, false, false, false);
assert!(text.contains(
"- Messages from people other than the session's starter begin with their name.\n- The autopilot of spaghetti"
), "{text}");
}

#[test]
Expand Down
9 changes: 9 additions & 0 deletions crates/fctl-agent/src/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2187,10 +2187,18 @@ impl Supervisor {
self.queue.push_back(d.clone());
return;
}
let told = self
.config
.as_ref()
.is_some_and(|c| prompt::tells_of_autopilot(&c.spec));
let author = author.as_ref().map(|user| prompt::Author {
user,
called: author_called.as_deref(),
autopilot: autopilot.is_some(),
autopilot_of: autopilot
.as_ref()
.filter(|_| !told)
.map(|a| a.project_slug.as_str()),
});
self.send_turn(text, author, Some(&d.id), attachments).await;
}
Expand Down Expand Up @@ -5964,6 +5972,7 @@ done
user: &bob,
called: None,
autopilot: false,
autopilot_of: None,
};
next.send_turn("Please continue", Some(by), None, &[]).await;
next.send_turn("And then this", None, None, &[]).await;
Expand Down
20 changes: 19 additions & 1 deletion crates/fctl-control/src/api/autopilots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ async fn brief_of(
)?
.all()
.await?;
let people = engine::managed(db, row, ctx.now).await?;
let others = engine::others(db, row).await?;
let held = engine::held(db, &row.project_id).await?;
#[derive(Deserialize)]
Expand Down Expand Up @@ -589,6 +590,17 @@ async fn brief_of(
at: h.created_at,
})
.collect();
let people_questions: Vec<Option<String>> =
people.iter().map(engine::CrewRow::question_text).collect();
let people_views: Vec<ap::CrewView> = people
.iter()
.zip(people_questions.iter())
.map(|(c, q)| {
let mut v = engine::managed_view(c);
v.question = q.as_deref();
v
})
.collect();
let other_questions: Vec<Option<String>> =
others.iter().map(engine::CrewRow::question_text).collect();
let other_views: Vec<ap::OtherView> = others
Expand Down Expand Up @@ -682,6 +694,7 @@ async fn brief_of(
asks: &ask_views,
crew: &crew_views,
held: &held_views,
people: &people_views,
others: &other_views,
files: &file_views,
memories: &memory_views,
Expand Down Expand Up @@ -944,8 +957,13 @@ pub async fn put(ctx: &mut Ctx, req: &mut Request, id: &str) -> AppResult<Respon
.transpose()
.map_err(AppError)?;
// What spends its owner's subscription, changed by someone else,
// runs it on theirs from now on, unless its owner keeps it.
// runs it on theirs from now on, unless its owner keeps it:
// managing people's sessions too wakes it for each of theirs.
let manages = |p: &[AutopilotPower]| p.contains(&AutopilotPower::Others);
let costs = check_in.is_some_and(|c| c != row.check_in())
|| powers
.as_ref()
.is_some_and(|p| manages(p) && !manages(&row.powers()))
|| crew.is_some_and(|c| c != row.crew_max())
|| model
.as_ref()
Expand Down
29 changes: 25 additions & 4 deletions crates/fctl-control/src/api/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,8 +906,29 @@ pub async fn resume(ctx: &mut Ctx, req: &mut Request, id: &str) -> AppResult<Res
} else {
Some(validate::message_with_files("prompt", prompt, files.len())?)
};
// Resumed sessions run on the resumer's subscription.
let credential = default_credential(ctx, &user, a.workspace_id()).await?;
// Resumed sessions run on the resumer's subscription, but for one people
// started that an autopilot manages and resumes (`autopilots::gate`
// lets it only for those it started, and those): it carries on on its
// own, its people's, as a message to it would.
let theirs = ctx.autopilot().is_some() && a.row.autopilot_role.is_none();
let credential_id = match (theirs, a.row.credential_id.clone()) {
// Only while its person may still run it there: otherwise it
// would fail again at once, which wakes the autopilot to resume it.
(true, Some(id)) => {
if !crate::spec::credential_serves(&ctx.db, &id, &a.row.id).await? {
return Err(conflict(
"The person whose Claude connection this session ran on can no longer run it: its people resume it.",
));
}
id
}
(true, None) => {
return Err(conflict(
"This session has no Claude connection to carry on with: its people resume it.",
));
}
(false, _) => default_credential(ctx, &user, a.workspace_id()).await?.id,
};
Comment thread
fctl-review[bot] marked this conversation as resolved.
Resolved
let attachments = internal::claim_files(
&ctx.env,
&a.row.project_id,
Expand All @@ -919,7 +940,7 @@ pub async fn resume(ctx: &mut Ctx, req: &mut Request, id: &str) -> AppResult<Res
q(
&ctx.db,
"UPDATE sessions SET status = 'queued', status_reason = NULL, credential_id = ?, ended_at = NULL, updated_at = ? WHERE id = ?",
&[s(&credential.id), n(ctx.now), s(id)],
&[s(&credential_id), n(ctx.now), s(id)],
)?
.run()
.await?;
Expand All @@ -936,7 +957,7 @@ pub async fn resume(ctx: &mut Ctx, req: &mut Request, id: &str) -> AppResult<Res
status: Some(SessionStatus::Queued),
reason: Some("Resuming".into()),
clear_runner: true,
claude_user: Some(user.to_ref()),
claude_user: (!theirs).then(|| user.to_ref()),
..Default::default()
},
)
Expand Down
Loading
Loading