@team-agent/installer 0.5.126 → 0.5.128
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +1 -1
- package/Cargo.toml +1 -1
- package/README.md +11 -0
- package/crates/team-agent/src/cli/adapters.rs +3 -13
- package/crates/team-agent/src/cli/arguments.rs +4 -0
- package/crates/team-agent/src/cli/diagnose.rs +68 -91
- package/crates/team-agent/src/cli/emit.rs +61 -28
- package/crates/team-agent/src/cli/grok_slot.rs +88 -3
- package/crates/team-agent/src/cli/intent/runtime.rs +38 -9
- package/crates/team-agent/src/cli/intent/runtime_tests.rs +147 -11
- package/crates/team-agent/src/cli/intent/tests.rs +10 -12
- package/crates/team-agent/src/cli/intent.rs +1 -1
- package/crates/team-agent/src/cli/mod.rs +38 -9
- package/crates/team-agent/src/cli/models.rs +789 -186
- package/crates/team-agent/src/cli/schedule.rs +175 -0
- package/crates/team-agent/src/cli/send/persist.rs +0 -36
- package/crates/team-agent/src/cli/send/presentation.rs +1 -1
- package/crates/team-agent/src/cli/send.rs +108 -110
- package/crates/team-agent/src/cli/spec.rs +6 -0
- package/crates/team-agent/src/cli/status_port/brief.rs +5 -67
- package/crates/team-agent/src/cli/tests/leader_watch.rs +12 -62
- package/crates/team-agent/src/cli/tests/mod.rs +3 -3
- package/crates/team-agent/src/cli/tests/named_address.rs +11 -10
- package/crates/team-agent/src/cli/tests/run_delegation.rs +12 -9
- package/crates/team-agent/src/cli/tests/status_send.rs +70 -273
- package/crates/team-agent/src/compiler/tests.rs +39 -27
- package/crates/team-agent/src/compiler.rs +206 -6
- package/crates/team-agent/src/contract_runtime/backend.rs +181 -11
- package/crates/team-agent/src/contract_runtime/commands.rs +338 -7
- package/crates/team-agent/src/contract_runtime/config.rs +30 -19
- package/crates/team-agent/src/contract_runtime/config_tests.rs +24 -4
- package/crates/team-agent/src/contract_runtime/discovery.rs +12 -1
- package/crates/team-agent/src/contract_runtime/fork.rs +3 -10
- package/crates/team-agent/src/contract_runtime/framework.rs +28 -14
- package/crates/team-agent/src/contract_runtime/mcp.rs +19 -24
- package/crates/team-agent/src/contract_runtime/prompt.rs +2 -2
- package/crates/team-agent/src/contract_runtime/registry.rs +93 -130
- package/crates/team-agent/src/coordinator/steps/abnormal.rs +45 -5
- package/crates/team-agent/src/coordinator/tests/api_error_recovery.rs +8 -29
- package/crates/team-agent/src/coordinator/tests/energy.rs +4 -4
- package/crates/team-agent/src/coordinator/tests/health_sync.rs +44 -2
- package/crates/team-agent/src/coordinator/tests/main_preserved.rs +2 -2
- package/crates/team-agent/src/coordinator/tests/spine.rs +1 -1
- package/crates/team-agent/src/coordinator/tests/takeover.rs +3 -3
- package/crates/team-agent/src/coordinator/tick.rs +1 -89
- package/crates/team-agent/src/db/message_store.rs +7 -65
- package/crates/team-agent/src/direct_delivery.rs +368 -0
- package/crates/team-agent/src/inbox_store.rs +169 -0
- package/crates/team-agent/src/leader/registry.rs +5 -0
- package/crates/team-agent/src/leader/start.rs +2 -1
- package/crates/team-agent/src/leader/tests/lease_api.rs +2 -2
- package/crates/team-agent/src/leader/tests/prompt_contract.rs +1 -0
- package/crates/team-agent/src/lib.rs +2 -0
- package/crates/team-agent/src/lifecycle/launch/add_agent.rs +33 -0
- package/crates/team-agent/src/lifecycle/launch/add_agent_state.rs +206 -1
- package/crates/team-agent/src/lifecycle/launch/clone_agent.rs +28 -2
- package/crates/team-agent/src/lifecycle/launch/fork_agent.rs +36 -179
- package/crates/team-agent/src/lifecycle/launch/fork_entry.rs +87 -6
- package/crates/team-agent/src/lifecycle/launch/mcp_config.rs +13 -488
- package/crates/team-agent/src/lifecycle/launch/pi_mcp.rs +351 -81
- package/crates/team-agent/src/lifecycle/launch/pi_mcp_extension.js +3 -4
- package/crates/team-agent/src/lifecycle/launch/quick_start.rs +26 -2
- package/crates/team-agent/src/lifecycle/launch/spawn.rs +41 -31
- package/crates/team-agent/src/lifecycle/launch.rs +27 -12
- package/crates/team-agent/src/lifecycle/profile_launch.rs +318 -64
- package/crates/team-agent/src/lifecycle/restart/agent.rs +1 -0
- package/crates/team-agent/src/lifecycle/restart/avatar.rs +1798 -0
- package/crates/team-agent/src/lifecycle/restart/claude_fork.rs +163 -0
- package/crates/team-agent/src/lifecycle/restart/common.rs +298 -133
- package/crates/team-agent/src/lifecycle/restart/remove.rs +129 -0
- package/crates/team-agent/src/lifecycle/restart/selection.rs +5 -5
- package/crates/team-agent/src/lifecycle/restart.rs +4 -0
- package/crates/team-agent/src/lifecycle/role_config.rs +79 -27
- package/crates/team-agent/src/lifecycle/tests/acceptance_b_batch_red.rs +1 -1
- package/crates/team-agent/src/lifecycle/tests/agent_ops.rs +8 -12
- package/crates/team-agent/src/lifecycle/tests/behavioral_diff_264_red.rs +37 -240
- package/crates/team-agent/src/lifecycle/tests/claude_compatible_config_red.rs +325 -0
- package/crates/team-agent/src/lifecycle/tests/claude_profile_launch_red.rs +320 -8
- package/crates/team-agent/src/lifecycle/tests/cli_argv_routing_red.rs +329 -70
- package/crates/team-agent/src/lifecycle/tests/clone_fork_copilot_perms_red.rs +378 -4
- package/crates/team-agent/src/lifecycle/tests/communication_mode_runtime_contract_red.rs +25 -39
- package/crates/team-agent/src/lifecycle/tests/copilot_provider_red.rs +7 -11
- package/crates/team-agent/src/lifecycle/tests/core.rs +39 -36
- package/crates/team-agent/src/lifecycle/tests/f032_startup_prompt_best_effort_red.rs +22 -8
- package/crates/team-agent/src/lifecycle/tests/fixtures/pi_native_mcp_host.mjs +2 -1
- package/crates/team-agent/src/lifecycle/tests/grok_require_explicit_model_red.rs +15 -133
- package/crates/team-agent/src/lifecycle/tests/harvest2_a_batch_red.rs +60 -20
- package/crates/team-agent/src/lifecycle/tests/host_cotenant_death_p0_contract.rs +30 -67
- package/crates/team-agent/src/lifecycle/tests/issue238_codex_effort_contract.rs +3 -31
- package/crates/team-agent/src/lifecycle/tests/lane_ops.rs +194 -169
- package/crates/team-agent/src/lifecycle/tests/launch_spawn.rs +164 -282
- package/crates/team-agent/src/lifecycle/tests/lifecycle_rollback_red.rs +15 -78
- package/crates/team-agent/src/lifecycle/tests/main_preserved.rs +7 -7
- package/crates/team-agent/src/lifecycle/tests/mcp_tool_name_format_red.rs +6 -6
- package/crates/team-agent/src/lifecycle/tests/phase_b_contracts.rs +75 -69
- package/crates/team-agent/src/lifecycle/tests/phase_golden.rs +2 -10
- package/crates/team-agent/src/lifecycle/tests/pi_catalog_red.rs +18 -6
- package/crates/team-agent/src/lifecycle/tests/pi_command_red.rs +70 -52
- package/crates/team-agent/src/lifecycle/tests/pi_compiler_red.rs +210 -0
- package/crates/team-agent/src/lifecycle/tests/pi_dual_entry_red.rs +157 -4
- package/crates/team-agent/src/lifecycle/tests/pi_executable_mcp_red.rs +526 -10
- package/crates/team-agent/src/lifecycle/tests/pi_fork_session_inheritance_red.rs +1419 -0
- package/crates/team-agent/src/lifecycle/tests/pi_lifecycle_doctor_red.rs +68 -1
- package/crates/team-agent/src/lifecycle/tests/pi_native_mcp_capability_red.rs +34 -12
- package/crates/team-agent/src/lifecycle/tests/pi_session_delivery_red.rs +94 -2
- package/crates/team-agent/src/lifecycle/tests/quick_start_worker_readiness_red.rs +14 -15
- package/crates/team-agent/src/lifecycle/tests/restart.rs +12 -153
- package/crates/team-agent/src/lifecycle/tests/restart_build_before_destroy_0540_contract.rs +27 -5
- package/crates/team-agent/src/lifecycle/tests/restart_liveness_red.rs +59 -10
- package/crates/team-agent/src/lifecycle/tests/restart_rebind_hotfix_252_red.rs +47 -6
- package/crates/team-agent/src/lifecycle/tests/restart_session_capture_red.rs +3 -1374
- package/crates/team-agent/src/lifecycle/tests/resume_recover_red.rs +27 -10
- package/crates/team-agent/src/lifecycle/tests/status_credential_redaction_contract.rs +7 -7
- package/crates/team-agent/src/lifecycle/tests/subprep_codex_trust_roundtrip_red.rs +67 -313
- package/crates/team-agent/src/lifecycle/tests/swallow_batch4_semantics_red.rs +254 -2
- package/crates/team-agent/src/lifecycle/tests/team_in_team_identity_scope_red.rs +2 -2
- package/crates/team-agent/src/lifecycle/tests/team_in_team_sibling_quick_start_red.rs +1 -1
- package/crates/team-agent/src/lifecycle/tests/team_in_team_state_scope_red.rs +2 -2
- package/crates/team-agent/src/lifecycle/tests/team_key_retirement_txn_red.rs +4 -4
- package/crates/team-agent/src/lifecycle/tests/upgrade_compat_0211_red.rs +74 -19
- package/crates/team-agent/src/lifecycle/tests/verify_rs031_window_consistency_red.rs +38 -39
- package/crates/team-agent/src/lifecycle/tests/worker_spawn_env_red.rs +51 -88
- package/crates/team-agent/src/lifecycle/tests.rs +3 -4
- package/crates/team-agent/src/lifecycle/types.rs +18 -5
- package/crates/team-agent/src/lifecycle/worker_command_context.rs +43 -50
- package/crates/team-agent/src/mcp_server/readiness.rs +130 -149
- package/crates/team-agent/src/mcp_server/readiness_tests.rs +86 -109
- package/crates/team-agent/src/mcp_server/tests/scoped.rs +21 -31
- package/crates/team-agent/src/mcp_server/tests/send.rs +72 -107
- package/crates/team-agent/src/mcp_server/tests/tools.rs +10 -12
- package/crates/team-agent/src/mcp_server/tools.rs +70 -196
- package/crates/team-agent/src/messaging/delivery.rs +143 -253
- package/crates/team-agent/src/messaging/delivery_consumption_tests.rs +5 -5
- package/crates/team-agent/src/messaging/helpers.rs +1 -19
- package/crates/team-agent/src/messaging/mod.rs +1 -0
- package/crates/team-agent/src/messaging/reminders.rs +316 -0
- package/crates/team-agent/src/messaging/scheduler.rs +7 -1
- package/crates/team-agent/src/messaging/tests/dup_inject.rs +255 -150
- package/crates/team-agent/src/messaging/tests/main_preserved.rs +2 -2
- package/crates/team-agent/src/messaging/tests/runtime.rs +9 -115
- package/crates/team-agent/src/messaging/tests/spine.rs +44 -24
- package/crates/team-agent/src/messaging/tests/wave2.rs +13 -20
- package/crates/team-agent/src/messaging/types.rs +1 -0
- package/crates/team-agent/src/provider/adapter.rs +208 -254
- package/crates/team-agent/src/provider/adapters/claude.rs +78 -0
- package/crates/team-agent/src/provider/adapters/claude_fork.rs +131 -0
- package/crates/team-agent/src/provider/adapters/mod.rs +7 -7
- package/crates/team-agent/src/provider/adapters/pi.rs +106 -0
- package/crates/team-agent/src/provider/classify.rs +1 -66
- package/crates/team-agent/src/provider/model_catalog.rs +201 -13
- package/crates/team-agent/src/provider/session/capture.rs +111 -111
- package/crates/team-agent/src/provider/session/context_fork/claude.rs +123 -0
- package/crates/team-agent/src/provider/session/context_fork/outcome.rs +10 -18
- package/crates/team-agent/src/provider/session/context_fork.rs +87 -74
- package/crates/team-agent/src/provider/session/mod.rs +2 -3
- package/crates/team-agent/src/provider/session_scan/claude.rs +523 -0
- package/crates/team-agent/src/provider/session_scan/common.rs +47 -47
- package/crates/team-agent/src/provider/session_scan/pi.rs +34 -1
- package/crates/team-agent/src/provider/session_scan.rs +13 -13
- package/crates/team-agent/src/provider/tests/adapter.rs +127 -121
- package/crates/team-agent/src/provider/tests/classify.rs +0 -53
- package/crates/team-agent/src/provider/tests/command_helpers.rs +11 -19
- package/crates/team-agent/src/provider/tests/idle.rs +13 -19
- package/crates/team-agent/src/provider/types.rs +0 -4
- package/crates/team-agent/src/provider/wire.rs +34 -20
- package/crates/team-agent/src/state/abnormal_watch/tests/mod.rs +1 -1
- package/crates/team-agent/src/state/persist.rs +6 -6
- package/crates/team-agent/src/state/projection.rs +1 -1
- package/crates/team-agent/src/state/repository/tests.rs +2 -2
- package/crates/team-agent/src/test_support/models_timeout_fixture.rs +144 -0
- package/crates/team-agent/src/tmux_backend/tests.rs +0 -151
- package/crates/team-agent/src/tmux_backend.rs +9 -43
- package/package.json +4 -4
- package/skills/team-agent/SKILL.md +7 -8
- package/skills/team-agent/command-coverage.json +15 -1
- package/skills/team-agent/docs/reference/team-agent-operator.md +5 -3
- package/skills/team-agent/references/team-agent-operator.md +5 -3
- package/crates/team-agent/src/lifecycle/launch/grok_per_seat.rs +0 -260
- package/crates/team-agent/src/lifecycle/tests/codex_weak_window_attribution_red.rs +0 -683
- package/crates/team-agent/src/lifecycle/tests/grok_effort_argv_red.rs +0 -247
- package/crates/team-agent/src/lifecycle/tests/grok_mcp_overlay_red.rs +0 -507
- package/crates/team-agent/src/lifecycle/tests/grok_restart_resume_red.rs +0 -300
- package/crates/team-agent/src/provider/adapters/codex.rs +0 -101
- package/crates/team-agent/src/provider/adapters/grok.rs +0 -67
- package/crates/team-agent/src/provider/session/context_fork/codex.rs +0 -514
- package/crates/team-agent/src/provider/session_scan/codex.rs +0 -326
- package/crates/team-agent/src/provider/session_scan/grok.rs +0 -288
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
//! Durable leader reminders; the existing coordinator is the only timer process.
|
|
2
|
+
use std::path::Path;
|
|
3
|
+
|
|
4
|
+
use chrono::{Duration, SecondsFormat, Utc};
|
|
5
|
+
use rusqlite::{params, OptionalExtension};
|
|
6
|
+
use serde::{Deserialize, Serialize};
|
|
7
|
+
use serde_json::{json, Value};
|
|
8
|
+
|
|
9
|
+
use super::MessagingError;
|
|
10
|
+
use crate::{event_log::EventLog, message_store::MessageStore};
|
|
11
|
+
|
|
12
|
+
#[derive(Serialize, Deserialize)]
|
|
13
|
+
struct Reminder {
|
|
14
|
+
text: String,
|
|
15
|
+
trigger: String,
|
|
16
|
+
interval_ms: Option<i64>,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
pub(crate) fn parse_duration(raw: &str) -> Result<Duration, MessagingError> {
|
|
20
|
+
let split = raw.find(|c: char| !c.is_ascii_digit()).unwrap_or(raw.len());
|
|
21
|
+
let (amount, unit) = raw.split_at(split);
|
|
22
|
+
let multiplier = match unit {
|
|
23
|
+
"ms" => 1,
|
|
24
|
+
"s" => 1_000,
|
|
25
|
+
"m" => 60_000,
|
|
26
|
+
"h" => 3_600_000,
|
|
27
|
+
"d" => 86_400_000,
|
|
28
|
+
_ => 0,
|
|
29
|
+
};
|
|
30
|
+
amount.parse::<i64>().ok()
|
|
31
|
+
.and_then(|amount| amount.checked_mul(multiplier))
|
|
32
|
+
.filter(|ms| *ms > 0)
|
|
33
|
+
.and_then(Duration::try_milliseconds)
|
|
34
|
+
.filter(|duration| Utc::now().checked_add_signed(*duration).is_some())
|
|
35
|
+
.ok_or_else(|| MessagingError::Validation("duration must be a positive integer followed by ms, s, m, h or d, within the supported date range".into()))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
pub(crate) fn parse_id(id: &str) -> Option<i64> {
|
|
39
|
+
let hex = id.strip_prefix("sched_")?;
|
|
40
|
+
if hex.is_empty() || !hex.bytes().all(|c| c.is_ascii_hexdigit()) {
|
|
41
|
+
return None;
|
|
42
|
+
}
|
|
43
|
+
i64::from_str_radix(hex, 16).ok().filter(|id| *id > 0)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fn view(id: i64, reminder: &Reminder) -> Value {
|
|
47
|
+
json!({"id": format!("sched_{id:x}"), "trigger": reminder.trigger,
|
|
48
|
+
"text": reminder.text, "status": "active"})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
pub(crate) fn add(
|
|
52
|
+
workspace: &Path,
|
|
53
|
+
owner: &str,
|
|
54
|
+
text: &str,
|
|
55
|
+
duration: &str,
|
|
56
|
+
recurring: bool,
|
|
57
|
+
) -> Result<Value, MessagingError> {
|
|
58
|
+
let interval = parse_duration(duration)?;
|
|
59
|
+
let now = Utc::now();
|
|
60
|
+
let due = now.checked_add_signed(interval).ok_or_else(|| {
|
|
61
|
+
MessagingError::Validation("duration exceeds supported date range".into())
|
|
62
|
+
})?;
|
|
63
|
+
let reminder = Reminder {
|
|
64
|
+
text: text.to_owned(),
|
|
65
|
+
trigger: format!("{} {duration}", if recurring { "every" } else { "in" }),
|
|
66
|
+
interval_ms: recurring.then_some(interval.num_milliseconds()),
|
|
67
|
+
};
|
|
68
|
+
let store = MessageStore::open(workspace)?;
|
|
69
|
+
let conn = crate::db::schema::open_db(store.db_path())?;
|
|
70
|
+
conn.execute(
|
|
71
|
+
"insert into scheduled_events (due_at, target, kind, payload_json, status, created_at, owner_team_id) values (?1, 'leader', 'reminder', ?2, 'pending', ?3, ?4)",
|
|
72
|
+
params![due.to_rfc3339_opts(SecondsFormat::Nanos, false), serde_json::to_string(&reminder)?, now.to_rfc3339(), owner],
|
|
73
|
+
)?;
|
|
74
|
+
let mut value = view(conn.last_insert_rowid(), &reminder);
|
|
75
|
+
value["ok"] = json!(true);
|
|
76
|
+
Ok(value)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
pub(crate) fn list(workspace: &Path, owner: &str) -> Result<Vec<Value>, MessagingError> {
|
|
80
|
+
let store = MessageStore::open(workspace)?;
|
|
81
|
+
let conn = crate::db::schema::open_db(store.db_path())?;
|
|
82
|
+
let mut statement = conn.prepare("select id, payload_json from scheduled_events where kind='reminder' and owner_team_id=?1 and status='pending' order by id")?;
|
|
83
|
+
let rows = statement.query_map([owner], |row| {
|
|
84
|
+
Ok((row.get::<_, i64>(0)?, row.get::<_, String>(1)?))
|
|
85
|
+
})?;
|
|
86
|
+
rows.map(|row| {
|
|
87
|
+
let (id, payload) = row?;
|
|
88
|
+
Ok(view(id, &serde_json::from_str(&payload)?))
|
|
89
|
+
})
|
|
90
|
+
.collect()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
pub(crate) fn cancel(workspace: &Path, owner: &str, id: i64) -> Result<bool, MessagingError> {
|
|
94
|
+
let store = MessageStore::open(workspace)?;
|
|
95
|
+
let conn = crate::db::schema::open_db(store.db_path())?;
|
|
96
|
+
// An occurrence already fired is not recalled.
|
|
97
|
+
Ok(conn.execute("update scheduled_events set status='cancelled' where id=?1 and kind='reminder' and owner_team_id=?2 and status='pending'", params![id, owner])? == 1)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
pub(super) fn fire(
|
|
101
|
+
workspace: &Path,
|
|
102
|
+
id: i64,
|
|
103
|
+
_event_log: &EventLog,
|
|
104
|
+
) -> Result<Value, MessagingError> {
|
|
105
|
+
let store = MessageStore::open(workspace)?;
|
|
106
|
+
let mut conn = crate::db::schema::open_db(store.db_path())?;
|
|
107
|
+
// Serialize occurrences/cancellation, never wait for a delivery receipt.
|
|
108
|
+
let tx = conn.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
|
|
109
|
+
let row = tx.query_row(
|
|
110
|
+
"select due_at, payload_json, owner_team_id from scheduled_events where id=?1 and kind='reminder' and status='pending'",
|
|
111
|
+
[id], |row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?, row.get::<_, String>(2)?)),
|
|
112
|
+
).optional()?;
|
|
113
|
+
let Some((due_at, payload, owner)) = row else {
|
|
114
|
+
return Ok(json!({"ok": true, "cancelled": true}));
|
|
115
|
+
};
|
|
116
|
+
let reminder: Reminder = serde_json::from_str(&payload)?;
|
|
117
|
+
let directory = crate::direct_delivery::directory(workspace, Some(&owner))
|
|
118
|
+
.map_err(MessagingError::Routing)?;
|
|
119
|
+
// The inbox is durable independently of physical delivery. A stable id also
|
|
120
|
+
// makes replay after an append/before timer commit safe without ledger rows.
|
|
121
|
+
let message_id = format!("reminder_{id}_{due_at}");
|
|
122
|
+
let inbox = crate::inbox_store::read(workspace, "leader", usize::MAX, Some(&owner))
|
|
123
|
+
.map_err(std::io::Error::other)?;
|
|
124
|
+
if !inbox["messages"].as_array().into_iter().flatten()
|
|
125
|
+
.any(|record| record["message_id"] == message_id)
|
|
126
|
+
{
|
|
127
|
+
let mut record = crate::inbox_store::record("scheduler", "leader", &reminder.text, Some(&owner), "reminder");
|
|
128
|
+
record["message_id"] = json!(message_id);
|
|
129
|
+
crate::inbox_store::append(workspace, "leader", &record)
|
|
130
|
+
.map_err(std::io::Error::other)?;
|
|
131
|
+
}
|
|
132
|
+
let next_due = reminder
|
|
133
|
+
.interval_ms
|
|
134
|
+
.and_then(Duration::try_milliseconds)
|
|
135
|
+
.filter(|duration| *duration > Duration::zero())
|
|
136
|
+
.and_then(|interval| Utc::now().checked_add_signed(interval))
|
|
137
|
+
.map(|due| due.to_rfc3339_opts(SecondsFormat::Nanos, false));
|
|
138
|
+
let mut result = json!({"ok": true, "message_id": message_id, "next_due_at": next_due, "delivery": "inbox"});
|
|
139
|
+
// Compare due_at as well as status: concurrent ticks or cancellation cannot
|
|
140
|
+
// advance the same occurrence twice, or resurrect a cancelled reminder.
|
|
141
|
+
tx.execute(
|
|
142
|
+
"update scheduled_events set status=?1, due_at=coalesce(?2, due_at), fired_at=?3, result_json=?4 where id=?5 and status='pending' and due_at=?6",
|
|
143
|
+
params![if next_due.is_some() { "pending" } else { "done" }, next_due, Utc::now().to_rfc3339(), result.to_string(), id, due_at],
|
|
144
|
+
)?;
|
|
145
|
+
tx.commit()?;
|
|
146
|
+
// Once consumed, physical delivery is best effort: a crash or detached pane
|
|
147
|
+
// still leaves the reminder in the inbox, never awaiting a legacy receipt.
|
|
148
|
+
match directory.send("leader", &format!("Team Agent reminder:\n\n{}", reminder.text)) {
|
|
149
|
+
Ok(_) => result["delivery"] = json!("physical"),
|
|
150
|
+
Err(error) => result["delivery_error"] = json!(error),
|
|
151
|
+
}
|
|
152
|
+
conn.execute(
|
|
153
|
+
"update scheduled_events set result_json=?1 where id=?2 and json_extract(result_json, '$.message_id')=?3",
|
|
154
|
+
params![result.to_string(), id, message_id],
|
|
155
|
+
)?;
|
|
156
|
+
Ok(result)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
#[cfg(test)]
|
|
160
|
+
mod tests {
|
|
161
|
+
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
|
162
|
+
use super::*;
|
|
163
|
+
|
|
164
|
+
struct TestWorkspace(std::path::PathBuf);
|
|
165
|
+
impl TestWorkspace {
|
|
166
|
+
fn path(&self) -> &Path {
|
|
167
|
+
&self.0
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
impl Drop for TestWorkspace {
|
|
171
|
+
fn drop(&mut self) {
|
|
172
|
+
let _ = std::fs::remove_dir_all(&self.0);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
fn workspace() -> TestWorkspace {
|
|
176
|
+
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
|
|
177
|
+
let dir = TestWorkspace(std::env::temp_dir().join(format!(
|
|
178
|
+
"ta-reminders-{}-{}",
|
|
179
|
+
std::process::id(),
|
|
180
|
+
NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
|
181
|
+
)));
|
|
182
|
+
let state = json!({"active_team_key": "alpha", "team_key": "alpha", "teams": {
|
|
183
|
+
"alpha": {"team_key": "alpha", "agents": {}},
|
|
184
|
+
"beta": {"team_key": "beta", "agents": {}}
|
|
185
|
+
}});
|
|
186
|
+
let path = crate::state::persist::runtime_state_path(dir.path());
|
|
187
|
+
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
|
|
188
|
+
std::fs::write(path, state.to_string()).unwrap();
|
|
189
|
+
dir
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
#[test]
|
|
193
|
+
fn durations_and_ids_are_strict_and_bounded() {
|
|
194
|
+
for (raw, milliseconds) in [
|
|
195
|
+
("1ms", 1),
|
|
196
|
+
("2s", 2000),
|
|
197
|
+
("3m", 180000),
|
|
198
|
+
("1h", 3600000),
|
|
199
|
+
("1d", 86400000),
|
|
200
|
+
] {
|
|
201
|
+
assert_eq!(
|
|
202
|
+
parse_duration(raw).unwrap().num_milliseconds(),
|
|
203
|
+
milliseconds
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
for raw in [
|
|
207
|
+
"",
|
|
208
|
+
"0s",
|
|
209
|
+
"-1s",
|
|
210
|
+
"1",
|
|
211
|
+
"1.5s",
|
|
212
|
+
"NaNs",
|
|
213
|
+
"1w",
|
|
214
|
+
"1s2m",
|
|
215
|
+
"999999999999999999999d",
|
|
216
|
+
"9223372036854775807ms",
|
|
217
|
+
] {
|
|
218
|
+
assert!(parse_duration(raw).is_err(), "{raw}");
|
|
219
|
+
}
|
|
220
|
+
assert_eq!(parse_id("sched_1a"), Some(26));
|
|
221
|
+
for raw in [
|
|
222
|
+
"1",
|
|
223
|
+
"sched_",
|
|
224
|
+
"sched_-1",
|
|
225
|
+
"sched_+1",
|
|
226
|
+
"sched_0",
|
|
227
|
+
"sched_ffffffffffffffff",
|
|
228
|
+
] {
|
|
229
|
+
assert_eq!(parse_id(raw), None, "{raw}");
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
#[test]
|
|
234
|
+
fn list_and_cancel_are_persistent_and_team_scoped() {
|
|
235
|
+
let dir = workspace();
|
|
236
|
+
let added = add(dir.path(), "alpha", "check status", "1h", true).unwrap();
|
|
237
|
+
assert_eq!(added["trigger"], "every 1h");
|
|
238
|
+
assert_eq!(added["status"], "active");
|
|
239
|
+
let id = parse_id(added["id"].as_str().unwrap()).unwrap();
|
|
240
|
+
assert_eq!(list(dir.path(), "alpha").unwrap().len(), 1);
|
|
241
|
+
assert!(list(dir.path(), "beta").unwrap().is_empty());
|
|
242
|
+
assert!(!cancel(dir.path(), "beta", id).unwrap());
|
|
243
|
+
assert!(cancel(dir.path(), "alpha", id).unwrap());
|
|
244
|
+
assert!(!cancel(dir.path(), "alpha", id).unwrap());
|
|
245
|
+
assert!(list(dir.path(), "alpha").unwrap().is_empty());
|
|
246
|
+
let log = EventLog::new(dir.path());
|
|
247
|
+
assert_eq!(fire(dir.path(), id, &log).unwrap()["cancelled"], true);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
#[test]
|
|
251
|
+
fn one_shot_stores_once_and_uses_owner_not_active_team() {
|
|
252
|
+
let dir = workspace();
|
|
253
|
+
let added = add(dir.path(), "beta", "beta reminder", "1ms", false).unwrap();
|
|
254
|
+
let id = parse_id(added["id"].as_str().unwrap()).unwrap();
|
|
255
|
+
let log = EventLog::new(dir.path());
|
|
256
|
+
let result = fire(dir.path(), id, &log).unwrap();
|
|
257
|
+
assert_eq!(result["delivery"], "inbox");
|
|
258
|
+
assert!(result["delivery_error"].is_string());
|
|
259
|
+
fire(dir.path(), id, &log).unwrap();
|
|
260
|
+
let inbox = crate::inbox_store::read(dir.path(), "leader", usize::MAX, None).unwrap();
|
|
261
|
+
let records = inbox["messages"].as_array().unwrap();
|
|
262
|
+
assert_eq!(records.len(), 1);
|
|
263
|
+
assert_eq!(records[0]["team"], "beta");
|
|
264
|
+
assert_eq!(records[0]["recipient"], "leader");
|
|
265
|
+
assert_eq!(records[0]["kind"], "reminder");
|
|
266
|
+
assert_eq!(records[0]["content"], "beta reminder");
|
|
267
|
+
let store = MessageStore::open(dir.path()).unwrap();
|
|
268
|
+
let conn = crate::db::schema::open_db(store.db_path()).unwrap();
|
|
269
|
+
assert_eq!(conn.query_row("select count(*) from messages", [], |row| row.get::<_, i64>(0)).unwrap(), 0);
|
|
270
|
+
assert!(list(dir.path(), "beta").unwrap().is_empty());
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
#[test]
|
|
274
|
+
fn recurring_occurrences_are_unique_and_crash_replay_is_idempotent() {
|
|
275
|
+
let dir = workspace();
|
|
276
|
+
let added = add(dir.path(), "alpha", "periodic", "1s", true).unwrap();
|
|
277
|
+
let id = parse_id(added["id"].as_str().unwrap()).unwrap();
|
|
278
|
+
let log = EventLog::new(dir.path());
|
|
279
|
+
let store = MessageStore::open(dir.path()).unwrap();
|
|
280
|
+
let conn = crate::db::schema::open_db(store.db_path()).unwrap();
|
|
281
|
+
let original_due: String = conn.query_row(
|
|
282
|
+
"select due_at from scheduled_events where id=?1", [id], |row| row.get(0),
|
|
283
|
+
).unwrap();
|
|
284
|
+
let count = || crate::inbox_store::read(dir.path(), "leader", usize::MAX, None).unwrap()["messages"].as_array().unwrap().len();
|
|
285
|
+
fire(dir.path(), id, &log).unwrap();
|
|
286
|
+
assert_eq!(list(dir.path(), "alpha").unwrap().len(), 1);
|
|
287
|
+
// Simulate the crash window after inbox append, before timer commit.
|
|
288
|
+
conn.execute("update scheduled_events set due_at=?1 where id=?2", params![original_due, id]).unwrap();
|
|
289
|
+
fire(dir.path(), id, &log).unwrap();
|
|
290
|
+
assert_eq!(count(), 1);
|
|
291
|
+
fire(dir.path(), id, &log).unwrap();
|
|
292
|
+
assert_eq!(count(), 2);
|
|
293
|
+
assert!(cancel(dir.path(), "alpha", id).unwrap());
|
|
294
|
+
fire(dir.path(), id, &log).unwrap();
|
|
295
|
+
assert_eq!(count(), 2);
|
|
296
|
+
assert_eq!(conn.query_row("select count(*) from messages", [], |row| row.get::<_, i64>(0)).unwrap(), 0);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
#[test]
|
|
300
|
+
fn concurrent_fires_consume_one_occurrence_and_one_inbox_record() {
|
|
301
|
+
let dir = workspace();
|
|
302
|
+
let added = add(dir.path(), "alpha", "once", "1ms", false).unwrap();
|
|
303
|
+
let id = parse_id(added["id"].as_str().unwrap()).unwrap();
|
|
304
|
+
let barrier = std::sync::Barrier::new(2);
|
|
305
|
+
std::thread::scope(|scope| {
|
|
306
|
+
let handles: Vec<_> = (0..2).map(|_| scope.spawn(|| {
|
|
307
|
+
barrier.wait();
|
|
308
|
+
fire(dir.path(), id, &EventLog::new(dir.path())).unwrap()
|
|
309
|
+
})).collect();
|
|
310
|
+
let results: Vec<_> = handles.into_iter().map(|handle| handle.join().unwrap()).collect();
|
|
311
|
+
assert_eq!(results.iter().filter(|result| result["cancelled"] == true).count(), 1);
|
|
312
|
+
});
|
|
313
|
+
let inbox = crate::inbox_store::read(dir.path(), "leader", usize::MAX, None).unwrap();
|
|
314
|
+
assert_eq!(inbox["messages"].as_array().unwrap().len(), 1);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
@@ -53,6 +53,7 @@ pub fn fire_due_scheduled_events(
|
|
|
53
53
|
let fire_one = || -> Result<serde_json::Value, MessagingError> {
|
|
54
54
|
let scheduled_kind = parse_scheduled_kind(&kind)?;
|
|
55
55
|
let result = match scheduled_kind {
|
|
56
|
+
ScheduledKind::Reminder => super::reminders::fire(workspace, id, event_log)?,
|
|
56
57
|
ScheduledKind::Send => {
|
|
57
58
|
let payload: serde_json::Value = serde_json::from_str(&payload_json)?;
|
|
58
59
|
let outcome = deliver_stored_message(
|
|
@@ -132,6 +133,11 @@ pub fn fire_due_scheduled_events(
|
|
|
132
133
|
|
|
133
134
|
let (status, result_json) = match fire_one() {
|
|
134
135
|
Ok(result) => {
|
|
136
|
+
// Reminder dispatch owns recurrence advancement and cancellation.
|
|
137
|
+
if kind == "reminder" {
|
|
138
|
+
fired.push(id);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
135
141
|
let ok = result
|
|
136
142
|
.get("ok")
|
|
137
143
|
.and_then(serde_json::Value::as_bool)
|
|
@@ -158,7 +164,7 @@ pub fn fire_due_scheduled_events(
|
|
|
158
164
|
// The row-status write stays `?`: a DB write failure is a real store fault, not a
|
|
159
165
|
// per-event poison, and should surface (consistent with the rest of the fn).
|
|
160
166
|
conn.execute(
|
|
161
|
-
"update scheduled_events set status = ?2, fired_at = ?3, result_json = ?4 where id = ?1",
|
|
167
|
+
"update scheduled_events set status = ?2, fired_at = ?3, result_json = ?4 where id = ?1 and status = 'pending'",
|
|
162
168
|
params![id, status, chrono::Utc::now().to_rfc3339(), result_json],
|
|
163
169
|
)?;
|
|
164
170
|
fired.push(id);
|