Projects: shareable, registry-backed, container-mounted; drop ticket board
Nightly Build / build (push) Successful in 6m27s
Nightly Build / build (push) Successful in 6m27s
Rework projects from single-user leftovers into shareable endeavours.
- DB: move `projects` from the owner bucket to the registry (system.db,
not encrypted); add `owner_user_id` + `slug` (drop free `path`); new
`project_members(project_id, user_id, can_write)` mirroring
`shared_folder_members`. Drop `project_tickets` entirely. Only user↔agent
conversations stay encrypted (per-user DB) — each member keeps a private
project chat. Registry home dissolves the cross-DB-FK problem.
- Filesystem/container: on disk `{WD}/projects/{owner_userid}/{slug}`,
agent/container path `projects/{owner_username}/{slug}`. Two-segment routing
in UserFs (ProjectMount + host_base_and_tail arm) and a second loop in
build_user_fs; read-only members get a :ro mount. Reuse the shared-folder
remount machinery (refresh_user_shared_folders -> refresh_user_mounts).
- Remove the ticket system: ProjectTicketManager, UserContext.tickets, its
wiring, and the project_tickets references in scheduled_jobs/cron.
- API: repoint handlers to the registry pool + membership scoping. Sharing is
self-service — owner or any write-member may add/remove members and set
read/write; only the owner deletes; the owner cannot be removed. New
POST/DELETE /api/projects/{id}/members[/{user_id}]. Seed `@fs_any allow
projects/*`; build_runtime_run_context sets working_directory to the agent
path and drops the host-path allow_fs_writes.
- Frontend: create form without the free path field, owner/read-write badges;
the detail page becomes header + description + sharing panel + Open chat + a
file-explorer placeholder (the future primary surface). i18n en/it/fr.
This commit is contained in:
@@ -1,106 +1,34 @@
|
||||
pub mod tickets;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use crate::db::projects::{self, Project};
|
||||
use crate::db::projects::Project;
|
||||
use crate::run_context::RunContext;
|
||||
|
||||
pub struct ProjectManager {
|
||||
db: Arc<SqlitePool>,
|
||||
}
|
||||
|
||||
impl ProjectManager {
|
||||
pub fn new(db: Arc<SqlitePool>) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
|
||||
pub async fn list(&self) -> Result<Vec<Project>> {
|
||||
projects::list(&self.db).await
|
||||
}
|
||||
|
||||
pub async fn get(&self, id: i64) -> Result<Option<Project>> {
|
||||
projects::get(&self.db, id).await
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
&self,
|
||||
name: &str,
|
||||
path: &str,
|
||||
description: &str,
|
||||
run_context: Option<&RunContext>,
|
||||
) -> Result<Project> {
|
||||
let rc_json = run_context.map(|rc| rc.to_db());
|
||||
projects::create(&self.db, name, path, description, rc_json.as_deref()).await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
&self,
|
||||
id: i64,
|
||||
name: &str,
|
||||
path: &str,
|
||||
description: &str,
|
||||
run_context: Option<&RunContext>,
|
||||
) -> Result<bool> {
|
||||
let rc_json = run_context.map(|rc| rc.to_db());
|
||||
projects::update(&self.db, id, name, path, description, rc_json.as_deref()).await
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: i64) -> Result<bool> {
|
||||
projects::delete(&self.db, id).await
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the runtime `RunContext` for working on `project`, layering project-runtime
|
||||
/// fields over an optional pre-resolved `base` RC (which carries static config set at
|
||||
/// creation time, e.g. `security_group`).
|
||||
/// Builds the runtime `RunContext` for working on `project`, layering the project's
|
||||
/// working directory + a context header over an optional pre-resolved `base` RC (which
|
||||
/// carries static config set at creation time, e.g. `security_group`).
|
||||
///
|
||||
/// Runtime fields computed here:
|
||||
/// - `working_directory` — always set to `project.path`.
|
||||
/// - `allow_fs_writes` — project tree + Skald's own `data/` directory.
|
||||
/// - `system_prompt` — project-context fragments prepended before any stored ones.
|
||||
///
|
||||
/// Shared by `ProjectTicketManager::start` (background ticket jobs) and the interactive
|
||||
/// project-chat session provisioning, so both work with identical context.
|
||||
pub fn build_runtime_run_context(project: &Project, base: Option<RunContext>) -> RunContext {
|
||||
/// `working_directory` is the **agent path** `projects/{owner_username}/{slug}` — the
|
||||
/// same namespace the fs-tools and `execute_cmd` route through (the host/container
|
||||
/// mapping is handled by `UserFs`). Writes there are auto-allowed by the seeded
|
||||
/// `projects/*` approval rule and physically gated by the per-member read-only mount,
|
||||
/// so no host-path `allow_fs_writes` grant is needed (that was the old single-user
|
||||
/// model, which predated per-user containers).
|
||||
pub fn build_runtime_run_context(
|
||||
project: &Project,
|
||||
owner_username: &str,
|
||||
base: Option<RunContext>,
|
||||
) -> RunContext {
|
||||
let mut rc = base.unwrap_or_default();
|
||||
|
||||
// Working directory is always the project path, overwritten at build time.
|
||||
rc.working_directory = Some(project.path.clone());
|
||||
rc.working_directory = Some(format!("projects/{owner_username}/{}", project.slug));
|
||||
|
||||
// Absolute path to Skald's own data directory (user personal data store).
|
||||
let skald_data = std::env::current_dir()
|
||||
.unwrap_or_default()
|
||||
.join("data")
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
|
||||
// Grant write access to the project tree and Skald's data directory.
|
||||
if !rc.allow_fs_writes.contains(&project.path) {
|
||||
rc.allow_fs_writes.push(project.path.clone());
|
||||
}
|
||||
if !rc.allow_fs_writes.contains(&skald_data) {
|
||||
rc.allow_fs_writes.push(skald_data.clone());
|
||||
}
|
||||
|
||||
// Build runtime context fragments and prepend before any stored ones.
|
||||
// Note: working directory is intentionally omitted here — the date/time/OS/WD
|
||||
// tail block in MessageBuilder already reflects the effective WD from RunContext.
|
||||
let project_header = if project.description.is_empty() {
|
||||
format!("You are working on project \"{}\".", project.name)
|
||||
} else {
|
||||
format!("You are working on project \"{}\". Description: {}", project.name, project.description)
|
||||
};
|
||||
let mut injected = vec![
|
||||
project_header,
|
||||
format!(
|
||||
"Personal user data is available at: {}. \
|
||||
Consult it when the task requires knowledge about the user.",
|
||||
skald_data
|
||||
),
|
||||
];
|
||||
"You are working on project \"{}\". Description: {}",
|
||||
project.name, project.description
|
||||
)
|
||||
};
|
||||
let mut injected = vec![project_header];
|
||||
injected.extend(std::mem::take(&mut rc.system_prompt));
|
||||
rc.system_prompt = injected;
|
||||
|
||||
|
||||
@@ -1,178 +0,0 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use sqlx::SqlitePool;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tracing::warn;
|
||||
|
||||
use core_api::system_bus::{SystemEvent, SystemEventBus};
|
||||
|
||||
use crate::cron::TaskManager;
|
||||
use crate::db::{project_tickets, project_tickets::ProjectTicket, projects};
|
||||
use crate::run_context::RunContext;
|
||||
|
||||
pub struct ProjectTicketManager {
|
||||
db: Arc<SqlitePool>,
|
||||
task_mgr: std::sync::OnceLock<Arc<TaskManager>>,
|
||||
}
|
||||
|
||||
impl ProjectTicketManager {
|
||||
pub fn new(db: Arc<SqlitePool>) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
db,
|
||||
task_mgr: std::sync::OnceLock::new(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_task_manager(&self, tm: Arc<TaskManager>) {
|
||||
let _ = self.task_mgr.set(tm);
|
||||
}
|
||||
|
||||
/// Subscribe to the system bus and react to `JobCompleted` events whose
|
||||
/// `origin_ref` starts with `"PROJECT_TASK:"`. Spawns a background task.
|
||||
pub fn start_listener(
|
||||
self: Arc<Self>,
|
||||
system_bus: Arc<SystemEventBus>,
|
||||
shutdown: CancellationToken,
|
||||
) -> tokio::task::JoinHandle<()> {
|
||||
tokio::spawn(async move {
|
||||
let mut rx = system_bus.subscribe();
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = shutdown.cancelled() => break,
|
||||
res = rx.recv() => {
|
||||
match res {
|
||||
Ok(SystemEvent::JobCompleted { origin_ref: Some(ref s), result, error, .. })
|
||||
if s.starts_with("PROJECT_TASK:") =>
|
||||
{
|
||||
if let Some(tid) = s.strip_prefix("PROJECT_TASK:")
|
||||
.and_then(|n| n.parse::<i64>().ok())
|
||||
{
|
||||
if let Err(e) = self.on_job_completed(
|
||||
tid,
|
||||
result.as_deref(),
|
||||
error.as_deref(),
|
||||
).await {
|
||||
warn!(error = %e, ticket_id = tid, "ticket completion failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {
|
||||
warn!("ProjectTicketManager: system_bus lagged by {n} events");
|
||||
}
|
||||
Err(tokio::sync::broadcast::error::RecvError::Closed) => break,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ── CRUD ─────────────────────────────────────────────────────────────────
|
||||
|
||||
pub async fn list(&self, project_id: i64) -> Result<Vec<ProjectTicket>> {
|
||||
project_tickets::list_for_project(&self.db, project_id).await
|
||||
}
|
||||
|
||||
pub async fn get(&self, id: i64) -> Result<Option<ProjectTicket>> {
|
||||
project_tickets::get(&self.db, id).await
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
&self,
|
||||
project_id: i64,
|
||||
title: &str,
|
||||
description: &str,
|
||||
agent_id: &str,
|
||||
run_context: Option<&RunContext>,
|
||||
) -> Result<ProjectTicket> {
|
||||
let rc_json = run_context.map(|rc| rc.to_db());
|
||||
let ticket = project_tickets::create(
|
||||
&self.db, project_id, title, description, agent_id, rc_json.as_deref(),
|
||||
).await?;
|
||||
projects::touch(&self.db, project_id).await?;
|
||||
Ok(ticket)
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: i64) -> Result<bool> {
|
||||
let ticket = project_tickets::get(&self.db, id).await?;
|
||||
let found = project_tickets::delete(&self.db, id).await?;
|
||||
if found {
|
||||
if let Some(t) = ticket {
|
||||
projects::touch(&self.db, t.project_id).await?;
|
||||
}
|
||||
}
|
||||
Ok(found)
|
||||
}
|
||||
|
||||
// ── Lifecycle ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Builds a runtime RunContext and starts the ticket as a background job.
|
||||
///
|
||||
/// The stored RC (ticket → project) carries only static config set at creation
|
||||
/// time (e.g. `security_group`). All runtime fields are computed here:
|
||||
/// - `working_directory` — always set to `project.path`
|
||||
/// - `allow_fs_writes` — project tree + Skald's own `data/` directory
|
||||
/// - `system_prompt` — project context fragments prepended before any stored ones
|
||||
pub async fn start(&self, ticket_id: i64) -> Result<()> {
|
||||
let task_mgr = self.task_mgr.get()
|
||||
.ok_or_else(|| anyhow!("ProjectTicketManager: task_manager not initialized"))?;
|
||||
|
||||
let ticket = project_tickets::get(&self.db, ticket_id).await?
|
||||
.ok_or_else(|| anyhow!("ticket {ticket_id} not found"))?;
|
||||
let project = projects::get(&self.db, ticket.project_id).await?
|
||||
.ok_or_else(|| anyhow!("project {} not found", ticket.project_id))?;
|
||||
|
||||
// Resolve base RC (ticket override → project default → empty), then layer the
|
||||
// project-runtime fields (WD, fs-write grants, project-context system prompt).
|
||||
// The stored RC carries only static config (e.g. security_group set at creation).
|
||||
let base: Option<RunContext> =
|
||||
ticket.run_context.as_deref().and_then(RunContext::from_db)
|
||||
.or_else(|| project.run_context.as_deref().and_then(RunContext::from_db));
|
||||
let rc = super::build_runtime_run_context(&project, base);
|
||||
|
||||
let origin_ref = format!("PROJECT_TASK:{ticket_id}");
|
||||
let rc_json = rc.to_db();
|
||||
|
||||
let job = task_mgr.spawn_async_job(
|
||||
&ticket.title,
|
||||
&ticket.description,
|
||||
&ticket.description,
|
||||
&ticket.agent_id,
|
||||
Some(&rc_json),
|
||||
&origin_ref,
|
||||
)?;
|
||||
|
||||
project_tickets::start(&self.db, ticket_id, job.id).await?;
|
||||
projects::touch(&self.db, ticket.project_id).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Called when a `SystemEvent::JobCompleted` with matching `origin_ref` is received.
|
||||
async fn on_job_completed(
|
||||
&self,
|
||||
ticket_id: i64,
|
||||
result: Option<&str>,
|
||||
error: Option<&str>,
|
||||
) -> Result<()> {
|
||||
let project_id = project_tickets::get(&self.db, ticket_id).await?
|
||||
.map(|t| t.project_id);
|
||||
project_tickets::complete(&self.db, ticket_id, result, error).await?;
|
||||
if let Some(pid) = project_id {
|
||||
projects::touch(&self.db, pid).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reset a ticket back to todo, clearing all run state.
|
||||
pub async fn reset(&self, ticket_id: i64) -> Result<()> {
|
||||
let project_id = project_tickets::get(&self.db, ticket_id).await?
|
||||
.map(|t| t.project_id);
|
||||
project_tickets::reset(&self.db, ticket_id).await?;
|
||||
if let Some(pid) = project_id {
|
||||
projects::touch(&self.db, pid).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user