Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

748 linesCodeBlameRaw
1//! What sessions touched, and the work items and pull requests they worked
2//! on.
3//!
4//! A session is tied to a work item or pull request in as many ways as it
5//! was (`LinkVia`): attached when it started or updated, a claim it made,
6//! a pull request it opened or a branch or commit of one, a
7//! `G1t-Session:` trailer on a commit pushed to one, and `#N` in its
8//! prompt, its task or a subagent's. Each way is a row of
9//! `session_links`; reading a session gathers its whole tree's
10//! (`gather_links`). Files two sessions touched make them overlap
11//! (`overlap.detected`, once per pair).
12
13use std::collections::{HashMap, HashSet};
14
15use g1t_contracts::Outcome;
16use g1t_contracts::repos::{Commit, LogArgs, RepoPath};
17use g1t_contracts::time::rfc3339;
18use g1t_contracts::work::*;
19use g1t_kit::now_ms;
20use serde::Deserialize;
21use worker::Result;
22
23use super::updates::push_delivery;
24use super::{SESSION_COLUMNS, SessionRow};
25use crate::items::{self, Named};
26use crate::Work;
27
28/// The start (`?2` characters) of the first prompt each session in `?1`
29/// (a JSON list of ids) recorded, by the session's own numbering.
30pub(crate) const FIRST_PROMPTS: &str = "SELECT session_id, text FROM (
31 SELECT ids.value AS session_id,
32 (SELECT substr(e.text, 1, ?2) FROM session_entries e
33 WHERE e.session_id = ids.value AND e.kind = 'prompt' ORDER BY e.session_seq LIMIT 1) AS text
34 FROM json_each(?1) AS ids)
35 WHERE text IS NOT NULL";
36
37/// The most `#N` of one text looked up.
38const MAX_REFS: usize = 10;
39/// The most work items and pull requests a session's links name.
40const MAX_LINKED: usize = 40;
41/// The most commits of one push read for trailers.
42const MAX_TRAILER_COMMITS: u32 = 20;
43/// The most subagents a tree shows.
44pub(crate) const MAX_TREE: u32 = 200;
45
46/// Ties session `?1` of repository `?2` to pull request `?4` (`?3` `pull`,
47/// `?7` empty) or work item `?7` (`?3` `item`, `?4` 0) by way of `?5`, at
48/// `?6`; nothing when it is tied that way already.
49pub(crate) const INSERT_LINK: &str = "INSERT INTO session_links (session_id, repo_id, kind, number, via, at, item_id)
50 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) ON CONFLICT DO NOTHING";
51
52/// Records that session `?1` of repository `?2` touched (`?3`) `?4`, at
53/// `?5`; returns it only the first time.
54pub(crate) const INSERT_TOUCH: &str = "INSERT INTO session_touches (session_id, repo_id, kind, value, at)
55 VALUES (?1, ?2, ?3, ?4, ?5) ON CONFLICT DO NOTHING RETURNING kind, value";
56
57/// Session `?1`'s subagents, and theirs, in the order they started.
58pub(crate) fn tree_sql() -> String {
59 format!(
60 "WITH RECURSIVE tree(id) AS (
61 SELECT id FROM sessions WHERE parent_id = ?1
62 UNION SELECT s.id FROM sessions s JOIN tree ON s.parent_id = tree.id)
63 SELECT {SESSION_COLUMNS} FROM sessions WHERE id IN (SELECT id FROM tree)
64 ORDER BY started_at, id LIMIT {MAX_TREE}"
65 )
66}
67
68/// Ends session `?1`'s subagents still going, and theirs, at `?2` for
69/// `?3`: only once `?1` itself ended at `?2`, so in the batch that ends it,
70/// a call that lost the race to end it ends nothing.
71pub(crate) fn end_tree_sql() -> String {
72 format!(
73 "WITH RECURSIVE tree(id) AS (
74 SELECT id FROM sessions WHERE parent_id = ?1
75 UNION SELECT s.id FROM sessions s JOIN tree ON s.parent_id = tree.id)
76 UPDATE sessions SET state = 'ended', ended_at = ?2, end_reason = ?3, last_seen_at = ?2
77 WHERE id IN (SELECT id FROM tree) AND state = 'active'
78 AND EXISTS (SELECT 1 FROM sessions WHERE id = ?1 AND state = 'ended' AND ended_at = ?2)
79 RETURNING {SESSION_COLUMNS}"
80 )
81}
82
83/// Session `?1`'s line: its parents up to the top, and its subagents and
84/// theirs. Never told of overlapping with each other.
85pub(crate) const FAMILY: &str = "WITH RECURSIVE
86 up(id, parent) AS (
87 SELECT id, parent_id FROM sessions WHERE id = ?1
88 UNION SELECT s.id, s.parent_id FROM sessions s JOIN up ON s.id = up.parent),
89 down(id) AS (
90 SELECT id FROM sessions WHERE parent_id = ?1
91 UNION SELECT s.id FROM sessions s JOIN down ON s.parent_id = down.id)
92 SELECT id FROM up UNION SELECT id FROM down";
93
94/// The files of `?2` (JSON) that active sessions of repository `?1` other
95/// than `?3` touched.
96pub(crate) const TOUCHED_BY_OTHERS: &str = "SELECT t.session_id AS session_id, t.value AS value
97 FROM session_touches t JOIN sessions s ON s.id = t.session_id
98 WHERE t.repo_id = ?1 AND t.kind = 'file' AND t.value IN (SELECT value FROM json_each(?2))
99 AND t.session_id <> ?3 AND s.state = 'active'";
100
101/// The sessions of repository `?1` that pushed the branch of one of the
102/// pull requests `?2` (JSON numbers).
103pub(crate) const BRANCH_SESSIONS: &str = "SELECT DISTINCT t.session_id AS id FROM session_touches t
104 JOIN pulls p ON p.repo_id = t.repo_id AND p.source_branch = t.value
105 AND p.fork_repo_id IS NULL AND p.head_repo_id IS NULL
106 WHERE t.repo_id = ?1 AND t.kind = 'branch' AND p.number IN (SELECT value FROM json_each(?2)) LIMIT 200";
107
108/// The sessions tied to repository `?1`'s pull requests numbered in `?2`
109/// (JSON): by a link, by saying so when they started, or by pushing the
110/// branch. A subagent is named with the session that started it.
111pub(crate) const PULL_SESSIONS: &str = "WITH asked(n) AS (SELECT value FROM json_each(?2)),
112 tied(number, session_id) AS (
113 SELECT l.number, l.session_id FROM session_links l WHERE l.repo_id = ?1 AND l.kind = 'pull' AND l.number IN asked
114 UNION SELECT s.pull, s.id FROM sessions s WHERE s.repo_id = ?1 AND s.pull IN asked
115 UNION SELECT p.number, t.session_id FROM session_touches t
116 JOIN pulls p ON p.repo_id = t.repo_id AND p.source_branch = t.value AND p.fork_repo_id IS NULL AND p.head_repo_id IS NULL
117 WHERE t.repo_id = ?1 AND t.kind = 'branch' AND p.number IN asked)
118 SELECT tied.number AS number, s.id AS id, s.harness AS harness, s.parent_id AS parent_id, s.started_at AS started_at,
119 (SELECT p.harness FROM sessions p WHERE p.id = s.parent_id) AS parent_harness
120 FROM tied JOIN sessions s ON s.id = tied.session_id
121 ORDER BY tied.number, s.started_at LIMIT 1000";
122
123/// A row of [`PULL_SESSIONS`].
124#[derive(Clone, Debug, Deserialize)]
125pub(crate) struct PullSessionRow {
126 pub number: u32,
127 pub id: String,
128 pub harness: String,
129 pub parent_id: Option<String>,
130 pub parent_harness: Option<String>,
131}
132
133/// Each pull request's sessions, a subagent's as the session that started
134/// it, each once, oldest first.
135pub(crate) fn pull_sessions_of(rows: Vec<PullSessionRow>) -> Vec<PullSessions> {
136 let mut out: Vec<PullSessions> = Vec::new();
137 for row in rows {
138 let (id, harness) = match (row.parent_id, row.parent_harness) {
139 (Some(parent), Some(harness)) => (parent, harness),
140 _ => (row.id, row.harness),
141 };
142 let session = SessionRef { id, harness: Harness::parse(&harness).unwrap_or(Harness::Other) };
143 match out.iter_mut().find(|entry| entry.number == row.number) {
144 Some(entry) if entry.sessions.iter().any(|known| known.id == session.id) => {}
145 Some(entry) => entry.sessions.push(session),
146 None => out.push(PullSessions { number: row.number, sessions: vec![session] }),
147 }
148 }
149 out
150}
151
152/// Announces the pair `?2`, `?3` of repository `?1` as overlapping on
153/// `?4` (JSON) at `?5`: returns it the first time, and again whenever a
154/// file it was not announced for joins, so a pair that first met on a
155/// trivial file still hears when it starts changing the same code.
156pub(crate) const INSERT_OVERLAP: &str = "INSERT INTO session_overlaps (repo_id, low, high, files, noticed_at)
157 VALUES (?1, ?2, ?3, ?4, ?5)
158 ON CONFLICT (repo_id, low, high) DO UPDATE SET
159 files = (SELECT json_group_array(value) FROM (
160 SELECT value FROM json_each(session_overlaps.files)
161 UNION SELECT value FROM json_each(excluded.files))),
162 noticed_at = excluded.noticed_at
163 WHERE EXISTS (SELECT 1 FROM json_each(excluded.files) AS added
164 WHERE added.value NOT IN (SELECT value FROM json_each(session_overlaps.files)))
165 RETURNING low";
166
167#[derive(Deserialize)]
168struct IdRow {
169 id: String,
170}
171
172#[derive(Deserialize)]
173struct NumberOnly {
174 number: u32,
175}
176
177#[derive(Deserialize)]
178pub(crate) struct LinkRow {
179 session_id: String,
180 kind: String,
181 /// A pull request's number, or a work item's in its workspace.
182 #[serde(default)]
183 number: Option<u32>,
184 via: String,
185}
186
187#[derive(Deserialize)]
188pub(crate) struct TouchRow {
189 session_id: String,
190 kind: String,
191 value: String,
192 at: String,
193}
194
195#[derive(Deserialize)]
196struct PairRow {
197 session_id: String,
198 value: String,
199}
200
201#[derive(Deserialize)]
202struct SkillRow {
203 session_id: String,
204 name: String,
205 uses: u32,
206}
207
208impl Work {
209 /// Ties `session_id` to a pull request or a work item one way. Never
210 /// fails what it follows: a problem is logged.
211 async fn link(&self, session_id: &str, repo_id: &str, kind: LinkKind, number: u32, item_id: &str, via: LinkVia) {
212 let at = rfc3339(now_ms());
213 let linked = async {
214 self.db
215 .prepare(INSERT_LINK)
216 .bind(&[
217 session_id.into(),
218 repo_id.into(),
219 kind.as_str().into(),
220 number.into(),
221 via.as_str().into(),
222 at.into(),
223 item_id.into(),
224 ])?
225 .run()
226 .await
227 };
228 if let Err(error) = linked.await {
229 worker::console_error!("session {session_id}: not tied to {} #{number}{item_id}: {error}", kind.as_str());
230 }
231 }
232
233 /// Ties `session_id` to pull request `number` of `repo_id` one way.
234 pub(crate) async fn link_pull(&self, session_id: &str, repo_id: &str, number: u32, via: LinkVia) {
235 self.link(session_id, repo_id, LinkKind::Pull, number, "", via).await;
236 }
237
238 /// Ties `session_id`, of `repo_id`, to work item `item_id` one way.
239 pub(crate) async fn link_item(&self, session_id: &str, repo_id: &str, item_id: &str, via: LinkVia) {
240 self.link(session_id, repo_id, LinkKind::Item, 0, item_id, via).await;
241 }
242
243 /// Ties a session to what `#N` in `text` names, read in its repository
244 /// (`refs`): one of its pull requests, or a work item of its workspace.
245 pub(crate) async fn link_refs(&self, session_id: &str, repo_id: &str, text: &str) -> Result<()> {
246 let Some(workspace) = items::repo_workspace(self, repo_id).await? else {
247 return Ok(());
248 };
249 for number in refs_in(text).into_iter().take(MAX_REFS) {
250 match items::resolve(self, repo_id, &workspace, number).await? {
251 Some(Named::PullHere) => self.link_pull(session_id, repo_id, number, LinkVia::Ref).await,
252 Some(Named::Item(id)) => self.link_item(session_id, repo_id, &id, LinkVia::Ref).await,
253 _ => {}
254 }
255 }
256 Ok(())
257 }
258
259 /// Ties a session to the pull requests of `branch`.
260 pub(crate) async fn link_branch(&self, session_id: &str, repo_id: &str, branch: &str) -> Result<()> {
261 let pulls = self
262 .db
263 .prepare(
264 "SELECT number FROM pulls WHERE repo_id = ? AND source_branch = ?
265 AND fork_repo_id IS NULL AND head_repo_id IS NULL",
266 )
267 .bind(&[repo_id.into(), branch.into()])?
268 .all()
269 .await?
270 .results::<NumberOnly>()?;
271 for pull in pulls {
272 self.link_pull(session_id, repo_id, pull.number, LinkVia::Branch).await;
273 }
274 Ok(())
275 }
276
277 /// Ties a session to the pull requests whose head is `commit` (a full
278 /// or abbreviated hash).
279 pub(crate) async fn link_commit(&self, session_id: &str, repo_id: &str, commit: &str) -> Result<()> {
280 let pulls = self
281 .db
282 .prepare("SELECT number FROM pulls WHERE repo_id = ? AND head_commit IS NOT NULL AND substr(head_commit, 1, length(?2)) = ?2")
283 .bind(&[repo_id.into(), commit.into()])?
284 .all()
285 .await?
286 .results::<NumberOnly>()?;
287 for pull in pulls {
288 self.link_pull(session_id, repo_id, pull.number, LinkVia::Commit).await;
289 }
290 Ok(())
291 }
292
293 /// Records what a session touched, ties it to what that names, and
294 /// tells it and the other sessions on the same files that they
295 /// overlap. Returns how many were new.
296 pub(crate) async fn record_touches(&self, session: &Session, touches: &[NewTouch]) -> Result<u32> {
297 let mut touches = tidy_touches(touches);
298 if touches.is_empty() {
299 return Ok(0);
300 }
301 // A work item is kept by its id: named by its number in the
302 // repository's workspace, one that is none is left out.
303 if touches.iter().any(|touch| touch.kind == TouchKind::Item) {
304 let workspace = items::repo_workspace(self, &session.repo_id).await?.unwrap_or_default();
305 let mut kept = Vec::with_capacity(touches.len());
306 for mut touch in touches {
307 if touch.kind == TouchKind::Item {
308 let Some(held) = items::by_number(self, &workspace, touch.value.parse().unwrap_or_default()).await? else { continue };
309 touch.value = held.item.id;
310 }
311 kept.push(touch);
312 }
313 touches = kept;
314 }
315 let at = rfc3339(now_ms());
316 let mut statements = Vec::with_capacity(touches.len());
317 for touch in &touches {
318 statements.push(self.db.prepare(INSERT_TOUCH).bind(&[
319 session.id.as_str().into(),
320 session.repo_id.as_str().into(),
321 touch.kind.as_str().into(),
322 touch.value.as_str().into(),
323 at.as_str().into(),
324 ])?);
325 }
326 let mut new: Vec<NewTouch> = Vec::new();
327 for result in self.db.batch(statements).await? {
328 for row in result.results::<serde_json::Value>()? {
329 if let (Some(kind), Some(value)) = (row["kind"].as_str().and_then(TouchKind::parse), row["value"].as_str()) {
330 new.push(NewTouch { kind, value: value.to_owned() });
331 }
332 }
333 }
334 let mut files = Vec::new();
335 for touch in &new {
336 let number = touch.value.parse::<u32>().ok();
337 match (touch.kind, number) {
338 (TouchKind::Pull, Some(number)) => self.link_pull(&session.id, &session.repo_id, number, LinkVia::Opened).await,
339 (TouchKind::Item, _) => self.link_item(&session.id, &session.repo_id, &touch.value, LinkVia::Ref).await,
340 (TouchKind::Branch, _) => self.link_branch(&session.id, &session.repo_id, &touch.value).await?,
341 (TouchKind::Commit, _) => self.link_commit(&session.id, &session.repo_id, &touch.value).await?,
342 (TouchKind::File, _) => files.push(touch.value.clone()),
343 _ => {}
344 }
345 }
346 if !files.is_empty() {
347 self.announce_session_overlaps(session, &files).await?;
348 }
349 Ok(new.len() as u32)
350 }
351
352 /// The ids of a session's line: its parents and its subagents.
353 pub(crate) async fn family(&self, session_id: &str) -> Result<HashSet<String>> {
354 Ok(self
355 .db
356 .prepare(FAMILY)
357 .bind(&[session_id.into()])?
358 .all()
359 .await?
360 .results::<IdRow>()?
361 .into_iter()
362 .map(|row| row.id)
363 .collect())
364 }
365
366 /// Tells `session` and each other active session that touched one of
367 /// `files` (outside its own line) that they overlap, once per pair.
368 async fn announce_session_overlaps(&self, session: &Session, files: &[String]) -> Result<()> {
369 let touched: Vec<(String, String)> = self
370 .db
371 .prepare(TOUCHED_BY_OTHERS)
372 .bind(&[session.repo_id.as_str().into(), serde_json::to_string(files)?.into(), session.id.as_str().into()])?
373 .all()
374 .await?
375 .results::<PairRow>()?
376 .into_iter()
377 .map(|row| (row.session_id, row.value))
378 .collect();
379 if touched.is_empty() {
380 return Ok(());
381 }
382 let family = self.family(&session.id).await?;
383 let at = rfc3339(now_ms());
384 for (other_id, files) in session_overlaps(&session.id, &family, &touched) {
385 let (low, high) = session_pair(&session.id, &other_id);
386 let Some(other) = self.session_row(&session.repo_id, &other_id).await? else {
387 continue;
388 };
389 let other = other.at(now_ms());
390 let ours = SessionOverlapDetected {
391 repo_id: session.repo_id.clone(),
392 number: session.pull.or(session.item),
393 session_id: session.id.clone(),
394 other_session_id: other.id.clone(),
395 other_actor: other.actor.username.clone(),
396 files: files.clone(),
397 item: session.item,
398 other_item: other.item,
399 };
400 let theirs = SessionOverlapDetected {
401 number: other.pull.or(other.item),
402 session_id: other.id.clone(),
403 other_session_id: session.id.clone(),
404 other_actor: session.actor.username.clone(),
405 item: other.item,
406 other_item: session.item,
407 ..ours.clone()
408 };
409 // The pair is noticed once, and each side told, in one change.
410 let mut statements = vec![self
411 .db
412 .prepare(INSERT_OVERLAP)
413 .bind(&[session.repo_id.as_str().into(), low.as_str().into(), high.as_str().into(), serde_json::to_string(&files)?.into(), at.as_str().into()])?];
414 let guard = "EXISTS (SELECT 1 FROM session_overlaps WHERE repo_id = ?7 AND low = ?8 AND high = ?9 AND noticed_at = ?10)";
415 let mut sent = Vec::new();
416 for event in [&ours, &theirs] {
417 let delivery = self.delivery(
418 "overlap.detected",
419 &session.repo_id,
420 serde_json::to_value(event)?,
421 &format!("{low}:{high}:{at}:{}", event.session_id),
422 guard,
423 vec![session.repo_id.as_str().into(), low.as_str().into(), high.as_str().into(), at.as_str().into()],
424 )?;
425 sent.push(push_delivery(&mut statements, delivery));
426 }
427 let new = match self.db.batch(statements).await?.into_iter().next() {
428 Some(result) => !result.results::<serde_json::Value>()?.is_empty(),
429 None => false,
430 };
431 if !new {
432 continue;
433 }
434 let actor = Some(session.actor.id.clone());
435 for (event, sent) in [ours, theirs].into_iter().zip(sent) {
436 self.delivered(sent, "overlap.detected", &session.repo_id, actor.clone(), serde_json::to_value(event)?).await?;
437 }
438 }
439 Ok(())
440 }
441
442 /// A session's subagents, and theirs, in the order they started, each
443 /// with how many it started.
444 pub(crate) async fn subagents_of(&self, session_id: &str, now: u64) -> Result<Vec<Session>> {
445 let rows = self.db.prepare(tree_sql()).bind(&[session_id.into()])?.all().await?.results::<SessionRow>()?;
446 let mut children: Vec<Session> = rows.into_iter().map(|row| row.at(now)).collect();
447 count_subagents(&mut children);
448 Ok(children)
449 }
450
451 /// How many subagents each of `sessions` started, filled in.
452 pub(crate) async fn fill_subagents(&self, sessions: &mut [Session]) -> Result<()> {
453 if sessions.is_empty() {
454 return Ok(());
455 }
456 let ids: Vec<&str> = sessions.iter().map(|session| session.id.as_str()).collect();
457 #[derive(Deserialize)]
458 struct Count {
459 parent_id: String,
460 n: u32,
461 }
462 let counts = self
463 .db
464 .prepare("SELECT parent_id, COUNT(*) AS n FROM sessions WHERE parent_id IN (SELECT value FROM json_each(?)) GROUP BY parent_id")
465 .bind(&[serde_json::to_string(&ids)?.into()])?
466 .all()
467 .await?
468 .results::<Count>()?;
469 let counts: HashMap<String, u32> = counts.into_iter().map(|count| (count.parent_id, count.n)).collect();
470 for session in sessions {
471 session.subagents = counts.get(&session.id).copied().unwrap_or_default();
472 }
473 Ok(())
474 }
475
476 /// Gives each session with no title the start of the first prompt it
477 /// recorded, as `first_prompt`, for saying what it is for.
478 pub(crate) async fn fill_first_prompts(&self, sessions: &mut [Session]) -> Result<()> {
479 let ids: Vec<&str> = sessions.iter().filter(|session| session.title.is_none()).map(|session| session.id.as_str()).collect();
480 if ids.is_empty() {
481 return Ok(());
482 }
483 #[derive(Deserialize)]
484 struct First {
485 session_id: String,
486 text: String,
487 }
488 let found = self
489 .db
490 .prepare(FIRST_PROMPTS)
491 .bind(&[serde_json::to_string(&ids)?.into(), (MAX_FIRST_PROMPT_CHARS as u32).into()])?
492 .all()
493 .await?
494 .results::<First>()?;
495 let found: HashMap<String, String> = found.into_iter().map(|first| (first.session_id, first.text)).collect();
496 for session in sessions.iter_mut().filter(|session| session.title.is_none()) {
497 session.first_prompt = found.get(&session.id).map(|text| text.trim().to_owned()).filter(|text| !text.is_empty());
498 }
499 Ok(())
500 }
501
502 /// What a tree of sessions touched, its links, and the skills it used.
503 pub(crate) async fn tree_facts(&self, repo_id: &str, ids: &[String]) -> Result<(Vec<SessionTouch>, Vec<SessionLink>, Vec<SessionSkill>)> {
504 let ids_json = serde_json::to_string(ids)?;
505 // A work item touched is shown by its number.
506 let touches = self
507 .db
508 .prepare(concat!(
509 "SELECT session_id, kind,
510 CASE WHEN kind = 'item'
511 THEN COALESCE(CAST(",
512 items::sql::item_number!("session_touches.value"),
513 " AS TEXT), value)
514 ELSE value END AS value,
515 at FROM session_touches
516 WHERE session_id IN (SELECT value FROM json_each(?)) ORDER BY at, rowid LIMIT 1000",
517 ))
518 .bind(&[ids_json.as_str().into()])?
519 .all()
520 .await?
521 .results::<TouchRow>()?;
522 // A pull request opened on a branch after the session pushed it.
523 let branches: HashSet<(String, String)> = touches
524 .iter()
525 .filter(|touch| touch.kind == "branch")
526 .map(|touch| (touch.session_id.clone(), touch.value.clone()))
527 .collect();
528 for (session_id, branch) in branches.iter().take(20) {
529 self.link_branch(session_id, repo_id, branch).await?;
530 }
531 let records: Vec<LinkRecord> = self
532 .db
533 .prepare(concat!(
534 "SELECT session_id, kind,
535 CASE WHEN kind = 'item' THEN ",
536 items::sql::item_number!("session_links.item_id"),
537 "
538 ELSE number END AS number,
539 via FROM session_links
540 WHERE session_id IN (SELECT value FROM json_each(?)) ORDER BY at",
541 ))
542 .bind(&[ids_json.as_str().into()])?
543 .all()
544 .await?
545 .results::<LinkRow>()?
546 .into_iter()
547 .filter_map(|row| {
548 let kind = match row.kind.as_str() {
549 "item" => LinkKind::Item,
550 "pull" => LinkKind::Pull,
551 _ => return None,
552 };
553 Some(LinkRecord { session_id: row.session_id, kind, number: row.number?, via: LinkVia::parse(&row.via)? })
554 })
555 .collect();
556 let workspace = items::repo_workspace(self, repo_id).await?.unwrap_or_default();
557 let mut known = HashMap::new();
558 let mut wanted: Vec<(LinkKind, u32)> = Vec::new();
559 for record in &records {
560 if !wanted.contains(&(record.kind, record.number)) {
561 wanted.push((record.kind, record.number));
562 }
563 }
564 for (kind, number) in wanted.into_iter().take(MAX_LINKED) {
565 let found = match kind {
566 LinkKind::Item => items::by_number(self, &workspace, number).await?.map(|held| (held.item.title, state_name(&held.item.state))),
567 LinkKind::Pull => self.pull(repo_id, number).await?.map(|pull| (pull.title, state_name(&pull.status))),
568 };
569 if let Some(found) = found {
570 known.insert((kind, number), found);
571 }
572 }
573 let links = gather_links(&records, &known);
574 let skills = self
575 .db
576 .prepare(
577 "SELECT session_id, text AS name, COUNT(*) AS uses FROM session_entries
578 WHERE session_id IN (SELECT value FROM json_each(?)) AND kind = 'skill'
579 GROUP BY session_id, text ORDER BY MIN(session_seq)",
580 )
581 .bind(&[ids_json.as_str().into()])?
582 .all()
583 .await?
584 .results::<SkillRow>()?
585 .into_iter()
586 .map(|row| SessionSkill { session_id: row.session_id, name: row.name, uses: row.uses })
587 .collect();
588 let touched = touches
589 .into_iter()
590 .filter_map(|row| {
591 Some(SessionTouch { session_id: row.session_id, kind: TouchKind::parse(&row.kind)?, value: row.value, at: row.at })
592 })
593 .collect();
594 Ok((touched, links, skills))
595 }
596
597 /// After a push moved `pulls` on `branch` to `after`: the sessions that
598 /// pushed that branch, and those its new commits name in
599 /// `G1t-Session:` trailers, are tied to them. Never fails the push's
600 /// handling: a problem is logged.
601 pub(crate) async fn link_push(&self, repo_id: &str, branch: &str, after: &str, commits: Option<u32>, pulls: &[Pull]) {
602 if pulls.is_empty() {
603 return;
604 }
605 let linked = async {
606 let pushed_by = self
607 .db
608 .prepare("SELECT DISTINCT session_id AS id FROM session_touches WHERE repo_id = ? AND kind = 'branch' AND value = ?")
609 .bind(&[repo_id.into(), branch.into()])?
610 .all()
611 .await?
612 .results::<IdRow>()?;
613 for session in &pushed_by {
614 for pull in pulls {
615 self.link_pull(&session.id, repo_id, pull.number, LinkVia::Branch).await;
616 }
617 }
618 // Trailers are read only while someone records sessions here.
619 let recording = self
620 .db
621 .prepare("SELECT id FROM sessions WHERE repo_id = ? AND state = 'active' LIMIT 1")
622 .bind(&[repo_id.into()])?
623 .first::<IdRow>(None)
624 .await?;
625 if recording.is_none() {
626 return Ok::<(), worker::Error>(());
627 }
628 let Some(pull) = pulls.first() else { return Ok(()) };
629 let path: Option<RepoPath> =
630 g1t_kit::call(&self.repos, "path_by_id", &g1t_contracts::repos::PathByIdArgs { id: repo_id.to_owned() }).await?;
631 let Some(path) = path else { return Ok(()) };
632 let log = LogArgs {
633 path,
634 viewer: self.owner_viewer(pull).await?,
635 git_ref: Some(after.to_owned()),
636 limit: commits.unwrap_or(MAX_TRAILER_COMMITS).clamp(1, MAX_TRAILER_COMMITS),
637 };
638 let Outcome::Ok(history) = g1t_kit::call::<_, Outcome<Vec<Commit>>>(&self.repos, "log", &log).await? else {
639 return Ok(());
640 };
641 let mut named: Vec<String> = Vec::new();
642 for commit in &history {
643 for id in session_trailers(&commit.message) {
644 if !named.contains(&id) {
645 named.push(id);
646 }
647 }
648 }
649 for id in named {
650 if self.session_row(repo_id, &id).await?.is_none() {
651 continue;
652 }
653 for pull in pulls {
654 self.link_pull(&id, repo_id, pull.number, LinkVia::Trailer).await;
655 }
656 }
657 Ok(())
658 };
659 if let Err(error) = linked.await {
660 worker::console_error!("push to {branch}: sessions not tied to its pull requests: {error}");
661 }
662 }
663
664 /// Sessions tied to a repository's pull request by a link, by id.
665 pub(crate) async fn linked_sessions(&self, repo_id: &str, number: u32) -> Result<Vec<String>> {
666 Ok(self
667 .db
668 .prepare("SELECT DISTINCT session_id AS id FROM session_links WHERE repo_id = ? AND kind = 'pull' AND number = ? LIMIT 200")
669 .bind(&[repo_id.into(), number.into()])?
670 .all()
671 .await?
672 .results::<IdRow>()?
673 .into_iter()
674 .map(|row| row.id)
675 .collect())
676 }
677
678 /// A repository's sessions tied to a work item by a link, by id.
679 pub(crate) async fn item_sessions(&self, repo_id: &str, item_id: &str) -> Result<Vec<String>> {
680 Ok(self
681 .db
682 .prepare("SELECT DISTINCT session_id AS id FROM session_links WHERE repo_id = ? AND kind = 'item' AND item_id = ? LIMIT 200")
683 .bind(&[repo_id.into(), item_id.into()])?
684 .all()
685 .await?
686 .results::<IdRow>()?
687 .into_iter()
688 .map(|row| row.id)
689 .collect())
690 }
691
692 /// Sessions that pushed one of `pulls`' branches, by id: tied to them
693 /// even when the pull request was opened after the push.
694 pub(crate) async fn branch_sessions(&self, repo_id: &str, pulls: &[u32]) -> Result<Vec<String>> {
695 if pulls.is_empty() {
696 return Ok(Vec::new());
697 }
698 Ok(self
699 .db
700 .prepare(BRANCH_SESSIONS)
701 .bind(&[repo_id.into(), serde_json::to_string(pulls)?.into()])?
702 .all()
703 .await?
704 .results::<IdRow>()?
705 .into_iter()
706 .map(|row| row.id)
707 .collect())
708 }
709
710 /// Sessions by id, as they read now.
711 pub(crate) async fn sessions_by_id(&self, repo_id: &str, ids: &[String]) -> Result<Vec<Session>> {
712 if ids.is_empty() {
713 return Ok(Vec::new());
714 }
715 let now = now_ms();
716 Ok(self
717 .db
718 .prepare(format!(
719 "SELECT {SESSION_COLUMNS} FROM sessions WHERE repo_id = ? AND id IN (SELECT value FROM json_each(?))"
720 ))
721 .bind(&[repo_id.into(), serde_json::to_string(ids)?.into()])?
722 .all()
723 .await?
724 .results::<SessionRow>()?
725 .into_iter()
726 .map(|row| row.at(now))
727 .collect())
728 }
729}
730
731/// A work item's state or a pull request's status, as its JSON name.
732fn state_name<T: serde::Serialize>(state: &T) -> String {
733 serde_json::to_value(state).ok().and_then(|value| value.as_str().map(str::to_owned)).unwrap_or_default()
734}
735
736/// Fills in how many subagents each session of a tree started, from the
737/// tree itself.
738pub(crate) fn count_subagents(sessions: &mut [Session]) {
739 let mut counts: HashMap<String, u32> = HashMap::new();
740 for session in sessions.iter() {
741 if let Some(parent) = &session.parent_id {
742 *counts.entry(parent.clone()).or_default() += 1;
743 }
744 }
745 for session in sessions.iter_mut() {
746 session.subagents = counts.get(&session.id).copied().unwrap_or_default();
747 }
748}