pub mod build;
pub mod cost;
pub mod discovery;
pub mod scaffold;
mod setup;
use std::fmt;
use adk_core::audit_log::{AuditActor, AuditTrail};
use adk_harness::PlanStep;
use adk_harness::website::{CONNECTION_NOTICE, OnboardingStage, SiteRef};
use awp_types::{AutonomyLevel, Charter};
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::sync::Mutex;
pub use setup::{SetupGate, holding_page};
use crate::audit;
use crate::kinds::{CharterInputs, KindTemplate};
use crate::team::SiteTeam;
use crate::themes::ThemeId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Stage {
Start,
Discover,
Protect,
Interview,
Charter,
Build,
LaunchGate,
ConnectAndDeploy,
Run,
}
impl Stage {
pub const ALL: [Stage; 9] = [
Stage::Start,
Stage::Discover,
Stage::Protect,
Stage::Interview,
Stage::Charter,
Stage::Build,
Stage::LaunchGate,
Stage::ConnectAndDeploy,
Stage::Run,
];
pub fn number(self) -> usize {
Self::ALL.iter().position(|stage| *stage == self).map_or(0, |i| i + 1)
}
pub fn as_str(self) -> &'static str {
match self {
Self::Start => "start",
Self::Discover => "discover",
Self::Protect => "protect",
Self::Interview => "interview",
Self::Charter => "charter",
Self::Build => "build",
Self::LaunchGate => "launch_gate",
Self::ConnectAndDeploy => "connect_and_deploy",
Self::Run => "run",
}
}
pub fn span_name(self) -> &'static str {
match self {
Self::Start => "website.onboarding.start",
Self::Discover => "website.onboarding.discover",
Self::Protect => "website.onboarding.protect",
Self::Interview => "website.onboarding.interview",
Self::Charter => "website.onboarding.charter",
Self::Build => "website.onboarding.build",
Self::LaunchGate => "website.onboarding.launch_gate",
Self::ConnectAndDeploy => "website.onboarding.connect_and_deploy",
Self::Run => "website.onboarding.run",
}
}
pub fn span(self, site: &SiteRef) -> tracing::Span {
let id = site.id.to_string();
match self {
Self::Start => tracing::info_span!("website.onboarding.start", site.id = %id),
Self::Discover => tracing::info_span!("website.onboarding.discover", site.id = %id),
Self::Protect => tracing::info_span!("website.onboarding.protect", site.id = %id),
Self::Interview => tracing::info_span!("website.onboarding.interview", site.id = %id),
Self::Charter => tracing::info_span!("website.onboarding.charter", site.id = %id),
Self::Build => tracing::info_span!("website.onboarding.build", site.id = %id),
Self::LaunchGate => {
tracing::info_span!("website.onboarding.launch_gate", site.id = %id)
}
Self::ConnectAndDeploy => {
tracing::info_span!("website.onboarding.connect_and_deploy", site.id = %id)
}
Self::Run => tracing::info_span!("website.onboarding.run", site.id = %id),
}
}
pub fn steps(self) -> &'static [&'static str] {
match self {
Self::Start => &["start"],
Self::Discover => &["discover"],
Self::Protect => &["protect"],
Self::Interview => &["interview"],
Self::Charter => &["charter_draft", "charter_sign"],
Self::Build => &["build"],
Self::LaunchGate => &["gate"],
Self::ConnectAndDeploy => &["connect", "deploy"],
Self::Run => &["run"],
}
}
}
impl fmt::Display for Stage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "from", rename_all = "snake_case")]
pub enum StartingPoint {
ExistingSite {
url: String,
},
Presence {
profiles: Vec<String>,
},
Nothing,
}
impl StartingPoint {
pub fn sources(&self) -> &'static str {
match self {
Self::ExistingSite { .. } => {
"the existing site, through its WordPress REST API, an export file, a crawl or an \
isolated replay of its database, then the sanitiser"
}
Self::Presence { .. } => "the business profiles, listings, reviews and social pages",
Self::Nothing => "nothing: the interview and the charter",
}
}
}
fn step(
id: &str,
title: &str,
instruction: String,
after: &str,
gate: adk_harness::PlanGate,
) -> PlanStep {
PlanStep {
id: id.to_string(),
title: title.to_string(),
instruction,
depends_on: if after.is_empty() { Vec::new() } else { vec![after.to_string()] },
gate,
}
}
pub fn plan(start: &StartingPoint, template: Option<&KindTemplate>) -> Vec<PlanStep> {
use adk_harness::PlanGate;
let harness = |stage: OnboardingStage, after: &str| stage.step([after]);
let mut steps = vec![step(
"start",
"Start",
"Ask the owner for their site's URL, its name, or nothing at all, and a phone number \
for urgent alarms. Nothing else yet."
.into(),
"",
PlanGate::None,
)];
let mut discover = harness(OnboardingStage::Discover, "start");
discover.instruction = format!(
"{} Read {}. Propose the site kind with the reasons for it; the owner confirms it.",
discover.instruction,
start.sources()
);
steps.push(discover);
let protect = match start {
StartingPoint::ExistingSite { url } => format!(
"If {url} runs on a legacy runtime such as WordPress or PHP, offer the freeze as one \
decision card: a crawled snapshot served as a signed release, with the old server \
off the public path. Never delete the old site or its data."
),
_ => "There is no existing site to protect; record that and continue.".into(),
};
steps.push(step("protect", "Protect", protect, "discover", PlanGate::Approve));
steps.push(harness(OnboardingStage::Interview, "protect"));
let mut charter = harness(OnboardingStage::CharterDraft, "interview");
if let Some(template) = template {
charter.instruction = format!(
"{} Start from the {} template: its goals, KPIs and guardrails, every job at Observe \
or Propose for the first week, the budget with its running-cost estimate.",
charter.instruction, template.title
);
}
steps.push(charter);
steps.push(harness(OnboardingStage::CharterSign, "charter_draft"));
let mut build = harness(OnboardingStage::Build, "charter_sign");
let directions: Vec<String> =
ThemeId::ALL.iter().map(|id| format!("{id} ({})", id.direction())).collect();
build.instruction = format!(
"{} Every page names its goal. Use the owner's brand assets, or let the owner pick one \
of three directions: {}. Generated images only as labelled illustration, never the \
owner's people or premises.",
build.instruction,
directions.join("; ")
);
steps.push(build);
steps.push(harness(OnboardingStage::Gate, "build"));
steps.push(harness(OnboardingStage::Connect, "gate"));
steps.push(harness(OnboardingStage::Deploy, "connect"));
steps.push(step(
"run",
"Run",
"Propose the 30/60/90-day plan. Every job works at Propose for the first week; the \
KPI tracker measures from day 0; the first weekly review goes out on day 7 with the \
promotions the owner may decide."
.into(),
"deploy",
PlanGate::None,
));
steps
}
pub fn steward_instruction(
site_name: &str,
start: &StartingPoint,
template: Option<&KindTemplate>,
) -> String {
let mut text = format!(
"You are onboarding the owner of {site_name}. Work through these stages in order and \
tell the owner which stage you are in:\n"
);
for (number, step) in plan(start, template).iter().enumerate() {
text.push_str(&format!("{}. {} — {}\n", number + 1, step.title, step.instruction));
}
text.push_str(&format!(
"\nRules: evidence before questions; drafts with defaults, not blank forms; plain \
language in, structured charter out; no target without a baseline, and targets are the \
owner's; no invented facts, testimonials, team members, prices or credentials; nothing \
copied from competitors. Before the first connection say: \"{CONNECTION_NOTICE}\""
));
text
}
pub async fn draft_charter(
template: &KindTemplate,
team: &SiteTeam,
inputs: &CharterInputs,
trail: Option<&AuditTrail>,
) -> Charter {
let charter = template.draft_charter(team, inputs);
audit::record(
trail,
audit::CHARTER_DRAFTED,
AuditActor::default(),
json!({
"site": charter.site_id,
"charterVersion": charter.version,
"kind": charter.kind,
"valid": charter.validate().is_ok(),
}),
)
.await;
charter
}
#[derive(Debug)]
pub struct OnboardingProgress {
site: SiteRef,
audit: Option<AuditTrail>,
done: Mutex<Vec<Stage>>,
}
impl OnboardingProgress {
pub async fn start(site: SiteRef, start: &StartingPoint, audit: Option<AuditTrail>) -> Self {
audit::record(
audit.as_ref(),
audit::ONBOARDING_STARTED,
AuditActor::default(),
json!({ "site": site.id, "startingPoint": start }),
)
.await;
Self { site, audit, done: Mutex::new(Vec::new()) }
}
pub async fn complete(&self, stage: Stage, release: Option<u64>) {
stage.span(&self.site).in_scope(|| {
tracing::info!(
stage = stage.as_str(),
number = stage.number(),
"website.onboarding.stage"
);
});
let mut done = self.done.lock().await;
if !done.contains(&stage) {
done.push(stage);
}
drop(done);
audit::record(
self.audit.as_ref(),
audit::ONBOARDING_STAGE,
AuditActor::default(),
json!({ "site": self.site.id, "stage": stage, "number": stage.number(), "releaseSeq": release }),
)
.await;
if stage == Stage::Run {
audit::record(
self.audit.as_ref(),
audit::ONBOARDING_COMPLETED,
AuditActor::default(),
json!({ "site": self.site.id, "releaseSeq": release }),
)
.await;
}
}
pub fn resumed(
site: SiteRef,
audit: Option<AuditTrail>,
done: impl IntoIterator<Item = Stage>,
) -> Self {
Self { site, audit, done: Mutex::new(done.into_iter().collect()) }
}
pub async fn completed(&self) -> Vec<Stage> {
self.done.lock().await.clone()
}
pub async fn next(&self) -> Option<Stage> {
let done = self.done.lock().await;
Stage::ALL.into_iter().find(|stage| !done.contains(stage))
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RunPlan {
pub baselines_from: DateTime<Utc>,
pub propose_until: DateTime<Utc>,
pub first_review: DateTime<Utc>,
pub promotions: Vec<(String, AutonomyLevel)>,
pub phases: [Vec<String>; 3],
}
impl RunPlan {
pub fn new(charter: &Charter, launch: DateTime<Utc>) -> Self {
let kpis: Vec<&str> = charter.kpis.iter().map(|kpi| kpi.name.as_str()).collect();
Self {
baselines_from: launch,
propose_until: launch + Duration::days(7),
first_review: launch + Duration::days(7),
promotions: crate::jobs::starting_levels(charter),
phases: [
vec![
format!("Measure {} from day 0", kpis.join(", ")),
"Every job proposes; the owner approves, edits or rejects each proposal".into(),
"Day 7: the first weekly review, with the promotions the jobs earned".into(),
],
vec![
"Lock baselines when each KPI's measure-only period ends; the owner sets targets".into(),
"Fill content gaps the visitors' questions show".into(),
],
vec![
"Review goals and strategy against the baselines; propose charter changes".into(),
"Propose experiments where a KPI has a baseline and a target".into(),
],
],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_plan_compiles_as_a_plan_graph_for_every_starting_point() {
for start in [
StartingPoint::Nothing,
StartingPoint::Presence { profiles: vec!["https://maps.example/bakery".into()] },
StartingPoint::ExistingSite { url: "https://bakery.example".into() },
] {
let steps = plan(&start, Some(&crate::kinds::template(awp_types::SiteKind::Business)));
adk_harness::compile_plan(&steps, "onboarding").unwrap();
let covered: Vec<&str> =
Stage::ALL.iter().flat_map(|s| s.steps().iter().copied()).collect();
let ids: Vec<&str> = steps.iter().map(|s| s.id.as_str()).collect();
assert_eq!(ids, covered, "every stage's steps, in order");
}
}
#[test]
fn the_connect_step_says_the_notice_first() {
let steps = plan(&StartingPoint::Nothing, None);
let connect = steps.iter().find(|s| s.id == "connect").unwrap();
assert!(connect.instruction.contains(CONNECTION_NOTICE), "A-I19");
let instruction = steward_instruction("Juniper", &StartingPoint::Nothing, None);
assert!(instruction.contains("11. Run"));
}
}