@andrioid/paseo-attention-span 0.4.14 → 0.6.0
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/OVERVIEW.md +13 -5
- package/README.md +2 -2
- package/client/attention-screen.tsx +140 -143
- package/client/card.tsx +4 -2
- package/client/history.tsx +189 -0
- package/client/icons.ts +3 -1
- package/client/mark.ts +19 -0
- package/client/next-up.tsx +16 -2
- package/client/sessions.ts +50 -3
- package/client/still.tsx +70 -27
- package/client/ui.tsx +3 -3
- package/client/wrap-up.tsx +197 -0
- package/docs/architecture.md +17 -10
- package/docs/screenshots/desktop-dark-doing-acknowledge.png +0 -0
- package/docs/screenshots/desktop-dark-doing-clear.png +0 -0
- package/docs/screenshots/desktop-dark-doing-issue-sheet.png +0 -0
- package/docs/screenshots/desktop-dark-doing-reviewers.png +0 -0
- package/docs/screenshots/desktop-dark-doing-sheet.png +0 -0
- package/docs/screenshots/desktop-dark-doing-wrap-dirty.png +0 -0
- package/docs/screenshots/desktop-dark-doing-wrap-multi.png +0 -0
- package/docs/screenshots/desktop-dark-doing-wrap.png +0 -0
- package/docs/screenshots/desktop-dark-doing.png +0 -0
- package/docs/screenshots/desktop-dark-history-filtered.png +0 -0
- package/docs/screenshots/desktop-dark-history-groups.png +0 -0
- package/docs/screenshots/desktop-dark-history.png +0 -0
- package/docs/screenshots/desktop-dark-next-up-setup.png +0 -0
- package/docs/screenshots/desktop-dark-next-up.png +0 -0
- package/docs/screenshots/desktop-dark-settings.png +0 -0
- package/docs/screenshots/desktop-dark-setup.png +0 -0
- package/docs/screenshots/desktop-dark-waiting.png +0 -0
- package/docs/screenshots/phone-dark-doing.png +0 -0
- package/docs/screenshots/phone-dark-history.png +0 -0
- package/docs/screenshots/phone-light-next-up-filtered.png +0 -0
- package/index.client.tsx +6 -1
- package/index.server.ts +21 -2
- package/package.json +9 -3
- package/requirements.yaml +13 -9
- package/server/archive.ts +132 -0
- package/server/archived.ts +120 -0
- package/server/changes.ts +184 -26
- package/server/classify.ts +9 -2
- package/server/fixture.ts +71 -12
- package/server/github-types.ts +29 -5
- package/server/queries.ts +33 -7
- package/server/service.ts +90 -19
- package/server/store.ts +10 -3
- package/server/view.ts +14 -5
- package/shared/archive.ts +37 -0
- package/shared/attention.ts +85 -8
- package/shared/changes.ts +26 -11
- package/shared/got-it.ts +23 -0
- package/shared/history-report.ts +61 -0
- package/shared/rpc.ts +23 -4
- package/shared/still.ts +29 -7
- package/shared/wrap-up.ts +126 -0
- package/docs/screenshots/desktop-dark-history-reviewed.png +0 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { PluginHandlerContext } from "@getpaseo/plugin/server";
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
import { isIdle } from "../shared/attention";
|
|
5
|
+
import type { ArchiveResult, UnsavedSession } from "../shared/archive";
|
|
6
|
+
|
|
7
|
+
// Archiving finished work's sessions, safely [ATT-22]. Paseo's SDK has no restore, so an archive has no Undo here; instead
|
|
8
|
+
// nothing with unsaved work leaves without a confirmation. A worktree goes only when nothing else lives in it. Which
|
|
9
|
+
// sessions to archive, and what that acknowledges, is the service's business; this only archives what it's given.
|
|
10
|
+
|
|
11
|
+
const run = promisify(execFile);
|
|
12
|
+
|
|
13
|
+
// A session as Paseo's snapshot describes it; null where Paseo couldn't say.
|
|
14
|
+
export interface ArchiveSession {
|
|
15
|
+
agentId: string;
|
|
16
|
+
workspaceId: string | null;
|
|
17
|
+
title: string | null;
|
|
18
|
+
branch?: string;
|
|
19
|
+
dirty: boolean | null;
|
|
20
|
+
ahead: number | null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type Paseo = PluginHandlerContext["paseo"];
|
|
24
|
+
type ListedAgent = Awaited<ReturnType<Paseo["agents"]["list"]>>["entries"][number]["agent"];
|
|
25
|
+
type ListedWorkspace = Awaited<ReturnType<Paseo["workspaces"]["list"]>>["entries"][number];
|
|
26
|
+
|
|
27
|
+
const base = (session: ArchiveSession): UnsavedSession => ({ agentId: session.agentId, title: session.title, ...(session.branch ? { branch: session.branch } : {}) });
|
|
28
|
+
|
|
29
|
+
// What Paseo's snapshot says archiving would lose: clean only when it says both that nothing is uncommitted and that nothing
|
|
30
|
+
// is ahead; either one unknown is unknown [ATT-22]. The fallback when there's no directory to ask git.
|
|
31
|
+
export function unsaved(session: ArchiveSession): UnsavedSession | null {
|
|
32
|
+
if (session.dirty === false && session.ahead === 0) return null;
|
|
33
|
+
if (session.dirty === null || session.ahead === null) return { ...base(session), unknown: true };
|
|
34
|
+
return { ...base(session), ...(session.ahead ? { commits: session.ahead } : {}) };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// How many files are uncommitted and commits unpushed, from git itself, since Paseo's snapshot counts neither and can lag.
|
|
38
|
+
// Commits are those ahead of the upstream, or, with no upstream, those no remote has. Clean needs both answers: one git
|
|
39
|
+
// couldn't give makes the whole answer unknown, never zero [ATT-22].
|
|
40
|
+
export async function gitUnsaved(session: ArchiveSession, directory: string): Promise<UnsavedSession> {
|
|
41
|
+
const git = (args: string[]) => run("git", ["-C", directory, ...args], { timeout: 5000 }).then(({ stdout }) => stdout);
|
|
42
|
+
const count = (args: string[]) =>
|
|
43
|
+
git(["rev-list", "--count", ...args]).then((stdout) => {
|
|
44
|
+
const n = Number(stdout.trim());
|
|
45
|
+
if (!Number.isInteger(n)) throw new Error("not a count");
|
|
46
|
+
return n;
|
|
47
|
+
});
|
|
48
|
+
const files = await git(["status", "--porcelain"])
|
|
49
|
+
.then((stdout) => stdout.split("\n").filter((line) => line.trim()).length)
|
|
50
|
+
.catch(() => undefined);
|
|
51
|
+
const commits = await count(["@{upstream}..HEAD"])
|
|
52
|
+
.catch(() => count(["HEAD", "--not", "--remotes"]))
|
|
53
|
+
.catch(() => undefined);
|
|
54
|
+
const known = { ...(files ? { files } : {}), ...(commits ? { commits } : {}) };
|
|
55
|
+
return files === undefined || commits === undefined ? { ...base(session), unknown: true, ...known } : { ...base(session), ...known };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// What archiving would lose in a session: git's answer where there's a directory to ask, else Paseo's. Null when clean.
|
|
59
|
+
async function unsavedWork(session: ArchiveSession, workspace: ListedWorkspace | undefined): Promise<UnsavedSession | null> {
|
|
60
|
+
const directory = workspace?.workspaceDirectory ?? workspace?.projectRootPath;
|
|
61
|
+
if (!directory) return unsaved(session);
|
|
62
|
+
const found = await gitUnsaved(session, directory);
|
|
63
|
+
return found.unknown || found.files || found.commits ? found : null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Archives the given sessions, and the worktree behind each when nothing else lives in it [ATT-22]. A workspace shared with
|
|
67
|
+
// anything else stays, and a local checkout always does. `confirm` is the sheet that named unsaved work.
|
|
68
|
+
export async function archiveSessions(paseo: Paseo, agentIds: string[], confirm: boolean): Promise<ArchiveResult> {
|
|
69
|
+
const [agentList, workspaceList] = await Promise.all([paseo.agents.list(), paseo.workspaces.list()]);
|
|
70
|
+
const agents = new Map<string, ListedAgent>(agentList.entries.map(({ agent }) => [agent.id, agent]));
|
|
71
|
+
const workspaces = new Map<string, ListedWorkspace>(workspaceList.entries.map((workspace) => [workspace.id, workspace]));
|
|
72
|
+
const wanted = [...new Set(agentIds)];
|
|
73
|
+
const sessions: ArchiveSession[] = wanted.flatMap((id) => {
|
|
74
|
+
const agent = agents.get(id);
|
|
75
|
+
if (!agent) return [];
|
|
76
|
+
const git = agent.workspaceId ? workspaces.get(agent.workspaceId)?.gitRuntime : undefined;
|
|
77
|
+
return [
|
|
78
|
+
{
|
|
79
|
+
agentId: id,
|
|
80
|
+
workspaceId: agent.workspaceId ?? null,
|
|
81
|
+
title: agent.title ?? null,
|
|
82
|
+
...(git?.currentBranch ? { branch: git.currentBranch } : {}),
|
|
83
|
+
dirty: git?.isDirty ?? null,
|
|
84
|
+
ahead: git?.aheadOfOrigin ?? null,
|
|
85
|
+
},
|
|
86
|
+
];
|
|
87
|
+
});
|
|
88
|
+
const working = (entries: typeof agentList.entries) =>
|
|
89
|
+
sessions.find((session) => {
|
|
90
|
+
const agent = entries.find((entry) => entry.agent.id === session.agentId)?.agent;
|
|
91
|
+
return agent && !agent.archivedAt && !isIdle(agent);
|
|
92
|
+
});
|
|
93
|
+
const busy = working(agentList.entries);
|
|
94
|
+
if (busy) return { ok: false, reason: "working", message: `${busy.title ?? "A session"} is still working` };
|
|
95
|
+
if (!confirm) {
|
|
96
|
+
const dirty: UnsavedSession[] = [];
|
|
97
|
+
for (const session of sessions) {
|
|
98
|
+
const found = await unsavedWork(session, session.workspaceId ? workspaces.get(session.workspaceId) : undefined);
|
|
99
|
+
if (found) dirty.push(found);
|
|
100
|
+
}
|
|
101
|
+
if (dirty.length) return { ok: false, reason: "unsaved", sessions: dirty };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// The checks above can take a while: look again right before archiving, so a session that started working, or a session
|
|
105
|
+
// that moved into one of these worktrees meanwhile, is seen [ATT-22].
|
|
106
|
+
const latest = await paseo.agents.list();
|
|
107
|
+
const resumed = working(latest.entries);
|
|
108
|
+
if (resumed) return { ok: false, reason: "working", message: `${resumed.title ?? "A session"} is working again` };
|
|
109
|
+
|
|
110
|
+
// Archive the sessions; an already archived one counts as done.
|
|
111
|
+
for (const session of sessions) {
|
|
112
|
+
if (agents.get(session.agentId)?.archivedAt) continue;
|
|
113
|
+
await paseo.agents.ref(session.agentId).archive();
|
|
114
|
+
}
|
|
115
|
+
// A worktree goes only when it's Paseo's, every unarchived agent in it was one of these sessions, and it has no terminal of
|
|
116
|
+
// its own. If the terminals can't be read, it stays: better a leftover worktree than somebody's shell.
|
|
117
|
+
const warnings: string[] = [];
|
|
118
|
+
for (const workspaceId of new Set(sessions.flatMap((session) => (session.workspaceId ? [session.workspaceId] : [])))) {
|
|
119
|
+
const workspace = workspaces.get(workspaceId);
|
|
120
|
+
if (!workspace || workspace.workspaceKind !== "worktree") continue;
|
|
121
|
+
if (latest.entries.some(({ agent }) => agent.workspaceId === workspaceId && !agent.archivedAt && !wanted.includes(agent.id))) continue;
|
|
122
|
+
const terminals = await paseo.terminals.list({ workspaceId }).catch(() => null);
|
|
123
|
+
if (!terminals || terminals.entries.length > 0) continue;
|
|
124
|
+
// The sessions are put away either way; a worktree that wouldn't go is said, not swallowed.
|
|
125
|
+
const failed = await paseo.workspaces
|
|
126
|
+
.archive(workspaceId)
|
|
127
|
+
.then((result) => (result.error ? String(result.error) : null))
|
|
128
|
+
.catch((error: Error) => error.message);
|
|
129
|
+
if (failed) warnings.push(`${workspace.gitRuntime?.currentBranch ?? "its worktree"}: ${failed}`);
|
|
130
|
+
}
|
|
131
|
+
return warnings.length ? { ok: true, warning: `Archived, but couldn't remove ${warnings.join("; ")}` } : { ok: true };
|
|
132
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { PluginHandlerContext } from "@getpaseo/plugin/server";
|
|
2
|
+
|
|
3
|
+
// A session Attention Span started, archived in Paseo itself: once no session it started is left open on any part of the
|
|
4
|
+
// item's stack or issue group, what others did to that work up to the archive is acknowledged, so it doesn't come back as
|
|
5
|
+
// unread [HIS-07, ATT-22]. Only sessions Attention Span started count, by their item label; sessions linked only by their
|
|
6
|
+
// workspace don't, either way. A failed decision is kept in memory and made again on the next chance; archives while the
|
|
7
|
+
// server isn't running aren't noticed.
|
|
8
|
+
|
|
9
|
+
type Paseo = PluginHandlerContext["paseo"];
|
|
10
|
+
type ListedAgent = Awaited<ReturnType<Paseo["agents"]["list"]>>["entries"][number]["agent"];
|
|
11
|
+
type ListOptions = NonNullable<Parameters<Paseo["agents"]["list"]>[0]>;
|
|
12
|
+
|
|
13
|
+
// The markers and item labels of sessions Attention Span started, under its current and earlier names [ATT-07, SES-03].
|
|
14
|
+
const MARKERS = ["attention-span", "github-attention", "github-inbox"];
|
|
15
|
+
const ITEM_LABELS = MARKERS.map((marker) => `${marker}-item`);
|
|
16
|
+
// Agents another agent started belong to that agent's session.
|
|
17
|
+
const PARENT_AGENT_LABEL = "paseo.parent-agent-id";
|
|
18
|
+
// A decision that keeps failing is given up after this many tries.
|
|
19
|
+
const MAX_TRIES = 10;
|
|
20
|
+
|
|
21
|
+
export interface ArchiveEvent {
|
|
22
|
+
agentId: string;
|
|
23
|
+
parentAgentId: string | null;
|
|
24
|
+
archivedAt: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// What Paseo says of the archived session now; null when it couldn't be found.
|
|
28
|
+
export type FoundSession = Pick<ListedAgent, "labels" | "archivedAt"> | null;
|
|
29
|
+
|
|
30
|
+
export const itemKeyOf = (labels: Record<string, string> | undefined | null): string | undefined =>
|
|
31
|
+
ITEM_LABELS.map((label) => labels?.[label]).find(Boolean);
|
|
32
|
+
|
|
33
|
+
// The item whose notes the archive may acknowledge; "drop" when it acknowledges nothing, "wait" when it can't tell yet.
|
|
34
|
+
export function archivedItem(event: ArchiveEvent, found: FoundSession): { itemKey: string } | "drop" | "wait" {
|
|
35
|
+
if (event.parentAgentId) return "drop";
|
|
36
|
+
if (!found) return "wait";
|
|
37
|
+
if (found.labels?.[PARENT_AGENT_LABEL]) return "drop";
|
|
38
|
+
// Restored since, or archived again: this archive no longer stands, and a later one has its own event.
|
|
39
|
+
if (!found.archivedAt || Date.parse(found.archivedAt) !== Date.parse(event.archivedAt)) return "drop";
|
|
40
|
+
const itemKey = itemKeyOf(found.labels);
|
|
41
|
+
return itemKey ? { itemKey } : "drop";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Whether an open session Attention Span started is on any of the keys, which are compared ignoring case. The archived
|
|
45
|
+
// session itself counts too: if it's listed open, it was restored while this was being decided.
|
|
46
|
+
export function stillOpen(keys: string[], open: Pick<ListedAgent, "labels" | "archivedAt">[]): boolean {
|
|
47
|
+
const wanted = new Set(keys.map((key) => key.toLowerCase()));
|
|
48
|
+
return open.some((agent) => {
|
|
49
|
+
if (agent.archivedAt) return false;
|
|
50
|
+
const itemKey = itemKeyOf(agent.labels);
|
|
51
|
+
return !!itemKey && wanted.has(itemKey.toLowerCase());
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Every page of a list; a failed page fails the whole list, so it's never mistaken for an empty one.
|
|
56
|
+
async function listAll(paseo: Paseo, options: ListOptions): Promise<ListedAgent[]> {
|
|
57
|
+
const agents: ListedAgent[] = [];
|
|
58
|
+
let cursor: string | undefined;
|
|
59
|
+
for (;;) {
|
|
60
|
+
const result = await paseo.agents.list({ ...options, page: { limit: 200, ...(cursor ? { cursor } : {}) } });
|
|
61
|
+
agents.push(...result.entries.map((entry) => entry.agent));
|
|
62
|
+
if (!result.pageInfo.hasMore || !result.pageInfo.nextCursor) return agents;
|
|
63
|
+
cursor = result.pageInfo.nextCursor;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The archived session as Paseo has it now: its own snapshot, or else the list with archived sessions in it.
|
|
68
|
+
async function findSession(paseo: Paseo, agentId: string): Promise<FoundSession> {
|
|
69
|
+
const refreshed = (await paseo.agents.ref(agentId).refresh().catch(() => null))?.agent;
|
|
70
|
+
if (refreshed) return refreshed;
|
|
71
|
+
return (await listAll(paseo, { filter: { includeArchived: true } })).find((agent) => agent.id === agentId) ?? null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface ArchiveDeps {
|
|
75
|
+
groupKeysOf(itemKey: string): string[];
|
|
76
|
+
acknowledge(keys: string[], upTo: string): Promise<unknown>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export class ArchivedInPaseo {
|
|
80
|
+
// Agent id -> the archive still to decide, and how often that failed.
|
|
81
|
+
private pending = new Map<string, { event: ArchiveEvent; tries: number }>();
|
|
82
|
+
private running: Promise<void> = Promise.resolve();
|
|
83
|
+
|
|
84
|
+
constructor(private deps: ArchiveDeps) {}
|
|
85
|
+
|
|
86
|
+
// A new archive, and another try at the ones still pending. One at a time, so two never decide the same archive.
|
|
87
|
+
notice(paseo: Paseo, event?: ArchiveEvent): Promise<void> {
|
|
88
|
+
if (event) this.pending.set(event.agentId, { event, tries: 0 });
|
|
89
|
+
this.running = this.running.then(() => this.drain(paseo));
|
|
90
|
+
return this.running;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
get waiting(): number {
|
|
94
|
+
return this.pending.size;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private async drain(paseo: Paseo): Promise<void> {
|
|
98
|
+
for (const [agentId, job] of [...this.pending]) {
|
|
99
|
+
const done = await this.decide(paseo, job.event).catch((error) => {
|
|
100
|
+
console.error("Couldn't acknowledge an archived session's changes:", (error as Error).message);
|
|
101
|
+
return false;
|
|
102
|
+
});
|
|
103
|
+
if (this.pending.get(agentId) !== job) continue; // archived again meanwhile: the newer event is decided next time
|
|
104
|
+
if (done || ++job.tries >= MAX_TRIES) this.pending.delete(agentId);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// True once decided; false to try again later.
|
|
109
|
+
private async decide(paseo: Paseo, event: ArchiveEvent): Promise<boolean> {
|
|
110
|
+
if (event.parentAgentId) return true; // a sub-agent's archive is its parent session's business: nothing to look up
|
|
111
|
+
const item = archivedItem(event, await findSession(paseo, event.agentId));
|
|
112
|
+
if (item === "drop") return true;
|
|
113
|
+
if (item === "wait") return false;
|
|
114
|
+
const keys = this.deps.groupKeysOf(item.itemKey);
|
|
115
|
+
const open = (await Promise.all(MARKERS.map((marker) => listAll(paseo, { filter: { labels: { [marker]: "1" } } })))).flat();
|
|
116
|
+
if (stillOpen(keys, open)) return true;
|
|
117
|
+
await this.deps.acknowledge(keys, event.archivedAt);
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
}
|
package/server/changes.ts
CHANGED
|
@@ -1,27 +1,42 @@
|
|
|
1
|
-
import { isHandover, type OthersChange } from "../shared/changes";
|
|
1
|
+
import { isFinishedChange, isHandover, type OthersChange } from "../shared/changes";
|
|
2
2
|
import type { Item } from "../shared/attention";
|
|
3
|
-
import type { AssignmentEvent, AssignmentSubject, EndedPullRequest, LiveResult } from "./github-types";
|
|
3
|
+
import type { AssignmentEvent, AssignmentSubject, ClosedIssue, EndedPullRequest, LiveResult, ReopenSubject } from "./github-types";
|
|
4
4
|
import { prKey } from "./queries";
|
|
5
5
|
|
|
6
|
-
// Changed by others: what someone else did to my work, kept between refreshes so a note stays until Got it
|
|
7
|
-
// can list work that left my hands [
|
|
6
|
+
// Changed by others: what someone else did to my work, kept between refreshes so a note stays until Got it or an archive
|
|
7
|
+
// acknowledges it, and so History can list work that left my hands [HIS-06, HIS-07, ATT-22]. Pure, so every rule is
|
|
8
|
+
// testable; service.ts does the fetching.
|
|
9
|
+
|
|
10
|
+
// A PR of mine closed and waiting to be seen reopened [HIS-07].
|
|
11
|
+
export interface TrackedPr {
|
|
12
|
+
key: string;
|
|
13
|
+
number: number;
|
|
14
|
+
title: string;
|
|
15
|
+
url: string;
|
|
16
|
+
repo: string;
|
|
17
|
+
closedAt: string;
|
|
18
|
+
}
|
|
8
19
|
|
|
9
20
|
export interface ChangeLedger {
|
|
10
21
|
events: OthersChange[];
|
|
11
|
-
// Events I've tapped Got it on.
|
|
22
|
+
// Events I've tapped Got it on, or an archive acknowledged.
|
|
12
23
|
gotIt: string[];
|
|
13
24
|
// Issue node id -> key, from the last assigned search that worked. Null until the first, so nothing counts as gone then:
|
|
14
25
|
// only issues seen assigned to me can be noticed leaving.
|
|
15
26
|
assigned: Record<string, string> | null;
|
|
16
27
|
// PR node id -> its assignees when last looked up, so each change of hands costs one lookup [GH-01].
|
|
17
28
|
handed: Record<string, string>;
|
|
29
|
+
// PR node id -> what Attention Span saw closed, so a reopen by someone else is noticed [HIS-07].
|
|
30
|
+
tracked: Record<string, TrackedPr>;
|
|
18
31
|
}
|
|
19
32
|
|
|
20
|
-
export const EMPTY_LEDGER: ChangeLedger = { events: [], gotIt: [], assigned: null, handed: {} };
|
|
33
|
+
export const EMPTY_LEDGER: ChangeLedger = { events: [], gotIt: [], assigned: null, handed: {}, tracked: {} };
|
|
21
34
|
|
|
22
|
-
type Data = Pick<LiveResult, "mine" | "assigned" | "ended"
|
|
35
|
+
type Data = Pick<LiveResult, "mine" | "assigned" | "ended"> & Partial<Pick<LiveResult, "closedIssues">>;
|
|
23
36
|
// A lookup's answer per node id: the subject, null when it's gone or hidden, missing when the lookup failed.
|
|
24
37
|
export type Lookups = Map<string, AssignmentSubject | null>;
|
|
38
|
+
// Reopen lookups: the PR as it is now, null when it's gone or hidden, missing when the lookup failed.
|
|
39
|
+
export type ReopenLookups = Map<string, ReopenSubject | null>;
|
|
25
40
|
|
|
26
41
|
const same = (a: string | null | undefined, b: string | null | undefined) => !!a && !!b && a.toLowerCase() === b.toLowerCase();
|
|
27
42
|
const present = <T>(nodes: (T | null | undefined)[]) => nodes.filter((node): node is T => !!node && Object.keys(node).length > 0);
|
|
@@ -57,10 +72,19 @@ export function changeCandidates(ledger: ChangeLedger, data: Data, me: string):
|
|
|
57
72
|
return [...new Set([...prs, ...gone])];
|
|
58
73
|
}
|
|
59
74
|
|
|
75
|
+
// Tracked PRs worth asking about: ones Attention Span saw closed that the closed search no longer shows, as a reopen takes
|
|
76
|
+
// them out of it [HIS-07, GH-01]. One still listed is still closed, so it costs nothing; past the search's 50 results it's
|
|
77
|
+
// asked about too.
|
|
78
|
+
export function reopenCandidates(ledger: ChangeLedger, data?: Pick<LiveResult, "ended">): string[] {
|
|
79
|
+
const listed = new Set(present(data?.ended.nodes ?? []).flatMap((pr) => (pr.id ? [pr.id] : [])));
|
|
80
|
+
return Object.keys(ledger.tracked).filter((id) => !listed.has(id));
|
|
81
|
+
}
|
|
82
|
+
|
|
60
83
|
const isAssignment = (node: AssignmentEvent | Record<string, never>): node is AssignmentEvent => "__typename" in node && "createdAt" in node;
|
|
61
84
|
|
|
62
|
-
// Who took it out of my hands, from GitHub's assign and unassign events [
|
|
63
|
-
// doing, or the events don't show it. An issue closed by now stays silent:
|
|
85
|
+
// Who took it out of my hands, from GitHub's assign and unassign events [HIS-07]. Null when it's still mine, it was my own
|
|
86
|
+
// doing, or the events don't show it. An issue closed by now stays silent: its close has its own note and this one would
|
|
87
|
+
// say the same thing twice.
|
|
64
88
|
// The new owner is the latest of the current assignees to be assigned. It left my hands with whichever came last of my
|
|
65
89
|
// removal and the new owner's assignment, and whoever did that is named: "phil took over" when that's the new owner, else
|
|
66
90
|
// "dave handed it to carol". So a PR I unassigned myself from is still mine until someone else takes it.
|
|
@@ -83,7 +107,7 @@ export function handover(subject: AssignmentSubject, me: string): OthersChange |
|
|
|
83
107
|
return change(subject, subject.__typename === "PullRequest" ? "pr" : "issue", kind, by.createdAt, actor, to);
|
|
84
108
|
}
|
|
85
109
|
|
|
86
|
-
// A PR of mine someone else merged or closed unmerged [
|
|
110
|
+
// A PR of mine someone else merged or closed unmerged [HIS-07]. Whether it left my hands before it ended is up to nextLedger,
|
|
87
111
|
// which knows when that happened.
|
|
88
112
|
export function ended(pr: EndedPullRequest, me: string): OthersChange | null {
|
|
89
113
|
if (pr.merged) return same(pr.mergedBy?.login, me) || !pr.mergedAt ? null : change(pr, "pr", "merged", pr.mergedAt, pr.mergedBy?.login);
|
|
@@ -91,6 +115,60 @@ export function ended(pr: EndedPullRequest, me: string): OthersChange | null {
|
|
|
91
115
|
return same(closer, me) ? null : change(pr, "pr", "closed", pr.closedAt, closer);
|
|
92
116
|
}
|
|
93
117
|
|
|
118
|
+
// My PRs I closed without merging, from the closed search: finished work whose sessions Wrap up puts away, though no note
|
|
119
|
+
// and no week lists them [ATT-22].
|
|
120
|
+
export function ownClosed(data: Pick<LiveResult, "ended">, me: string): Item[] {
|
|
121
|
+
return present(data.ended.nodes)
|
|
122
|
+
.filter((pr) => !pr.merged && same(pr.timelineItems.nodes.at(-1)?.actor?.login, me))
|
|
123
|
+
.map((pr) => ({
|
|
124
|
+
key: prKey(pr),
|
|
125
|
+
kind: "pr" as const,
|
|
126
|
+
section: "done" as const,
|
|
127
|
+
draft: false,
|
|
128
|
+
repo: pr.repository.nameWithOwner,
|
|
129
|
+
number: pr.number,
|
|
130
|
+
title: pr.title,
|
|
131
|
+
url: pr.url,
|
|
132
|
+
chips: [],
|
|
133
|
+
actions: [],
|
|
134
|
+
children: [],
|
|
135
|
+
target: { kind: "pr" as const, repo: pr.repository.nameWithOwner, number: pr.number, title: pr.title, url: pr.url },
|
|
136
|
+
othersActivityAt: pr.closedAt,
|
|
137
|
+
closedAt: pr.closedAt,
|
|
138
|
+
closedUnmerged: true as const,
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// An issue assigned to me someone else closed, as completed or not planned [HIS-07]. My own closes are mine.
|
|
143
|
+
export function closedByOther(issue: ClosedIssue, me: string): OthersChange | null {
|
|
144
|
+
const closer = issue.timelineItems.nodes.at(-1)?.actor?.login;
|
|
145
|
+
if (same(closer, me)) return null;
|
|
146
|
+
const reason = issue.stateReason === "NOT_PLANNED" ? "not_planned" : issue.stateReason === "COMPLETED" ? "completed" : undefined;
|
|
147
|
+
return { ...change(issue, "issue", "issue_closed", issue.closedAt, closer), ...(reason ? { reason } : {}) };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// A PR Attention Span saw closed, now open again by someone else [HIS-07]. Only PRs already tracked count, so a first run
|
|
151
|
+
// doesn't announce every old reopen, and issues don't come here: they surface in Next up.
|
|
152
|
+
export function reopened(tracked: TrackedPr, subject: ReopenSubject, me: string): OthersChange | null {
|
|
153
|
+
if (subject.state !== "OPEN") return null;
|
|
154
|
+
const event = subject.timelineItems.nodes.at(-1);
|
|
155
|
+
const actor = event?.actor?.login;
|
|
156
|
+
if (!actor || same(actor, me)) return null;
|
|
157
|
+
return {
|
|
158
|
+
id: `reopened:${subject.id}:${event?.createdAt ?? tracked.closedAt}`,
|
|
159
|
+
kind: "reopened",
|
|
160
|
+
subjectId: subject.id,
|
|
161
|
+
subject: "pr",
|
|
162
|
+
key: tracked.key,
|
|
163
|
+
repo: tracked.repo,
|
|
164
|
+
number: tracked.number,
|
|
165
|
+
title: subject.title || tracked.title,
|
|
166
|
+
url: tracked.url,
|
|
167
|
+
actor,
|
|
168
|
+
at: event?.createdAt ?? new Date().toISOString(),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
94
172
|
function change(
|
|
95
173
|
subject: { id: string; number: number; title: string; url: string; repository: { nameWithOwner: string } },
|
|
96
174
|
kind: "pr" | "issue",
|
|
@@ -116,9 +194,18 @@ function change(
|
|
|
116
194
|
}
|
|
117
195
|
|
|
118
196
|
// The ledger after a refresh. New events count only within `windowDays` (mentionDays), so a first run catches up that far
|
|
119
|
-
// and no further; kept for `keepDays`, long enough for History. Work back in my hands drops its handover
|
|
120
|
-
// looked up is left to retry next time.
|
|
121
|
-
export function nextLedger(
|
|
197
|
+
// and no further; kept for `keepDays`, long enough for History. Work back in my hands drops its handover unless it was
|
|
198
|
+
// cleared, which stays in its week. What couldn't be looked up is left to retry next time.
|
|
199
|
+
export function nextLedger(
|
|
200
|
+
ledger: ChangeLedger,
|
|
201
|
+
data: Data,
|
|
202
|
+
me: string,
|
|
203
|
+
lookups: Lookups,
|
|
204
|
+
now: Date,
|
|
205
|
+
windowDays: number,
|
|
206
|
+
keepDays: number,
|
|
207
|
+
reopens: ReopenLookups = new Map(),
|
|
208
|
+
): ChangeLedger {
|
|
122
209
|
const since = (days: number) => new Date(now.getTime() - days * 86_400_000).toISOString();
|
|
123
210
|
const found: OthersChange[] = [];
|
|
124
211
|
const endedAt = new Map(present(data.ended.nodes).map((pr) => [pr.id, pr.mergedAt ?? pr.closedAt]));
|
|
@@ -127,7 +214,8 @@ export function nextLedger(ledger: ChangeLedger, data: Data, me: string, lookups
|
|
|
127
214
|
.map((subject) => (subject ? handover(subject, me) : null))
|
|
128
215
|
.filter((event): event is OthersChange => !!event && !(endedAt.has(event.subjectId) && event.at > endedAt.get(event.subjectId)!));
|
|
129
216
|
found.push(...handovers);
|
|
130
|
-
|
|
217
|
+
// A handover whose work came back to me no longer makes a later merge someone else's [HIS-06].
|
|
218
|
+
const handedAt = (id: string) => [...ledger.events, ...handovers].filter((event) => isHandover(event) && !event.returned && event.subjectId === id).map((event) => event.at);
|
|
131
219
|
for (const pr of present(data.ended.nodes)) {
|
|
132
220
|
// A PR someone else has whose hands aren't looked up yet waits a refresh, so a handover isn't reported as a merge.
|
|
133
221
|
if (handedOver(pr, me) && !lookups.has(pr.id) && ledger.handed[pr.id] !== signature(pr)) continue;
|
|
@@ -135,15 +223,56 @@ export function nextLedger(ledger: ChangeLedger, data: Data, me: string, lookups
|
|
|
135
223
|
// One that left my hands before it ended was a handover, not my work merged or closed.
|
|
136
224
|
if (event && !handedAt(pr.id).some((at) => at <= event.at)) found.push(event);
|
|
137
225
|
}
|
|
226
|
+
for (const issue of present(data.closedIssues?.nodes ?? [])) {
|
|
227
|
+
const event = closedByOther(issue, me);
|
|
228
|
+
if (event) found.push(event);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Track what Attention Span sees closed, so a later reopen by someone else is a change [HIS-07]. A merged PR can't reopen.
|
|
232
|
+
const tracked: Record<string, TrackedPr> = { ...ledger.tracked };
|
|
233
|
+
for (const pr of present(data.ended.nodes)) {
|
|
234
|
+
if (pr.merged || !pr.id) continue;
|
|
235
|
+
tracked[pr.id] = { key: prKey(pr), number: pr.number, title: pr.title, url: pr.url, repo: pr.repository.nameWithOwner, closedAt: pr.closedAt };
|
|
236
|
+
}
|
|
237
|
+
// A reopen takes the old close out of History's week and adds a note. A lookup that failed keeps the PR tracked.
|
|
238
|
+
const reopenedIds = new Set<string>();
|
|
239
|
+
for (const [id, subject] of reopens) {
|
|
240
|
+
const before = tracked[id];
|
|
241
|
+
if (!subject) {
|
|
242
|
+
delete tracked[id];
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
// Merged: it can't reopen any more. Still closed: keep watching it.
|
|
246
|
+
if (subject.state === "MERGED") {
|
|
247
|
+
delete tracked[id];
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (subject.state !== "OPEN") continue;
|
|
251
|
+
// Open again: the old close leaves its week whoever reopened it; only someone else's reopen is a note.
|
|
252
|
+
delete tracked[id];
|
|
253
|
+
reopenedIds.add(id);
|
|
254
|
+
const event = before ? reopened(before, subject, me) : null;
|
|
255
|
+
if (event) found.push(event);
|
|
256
|
+
}
|
|
257
|
+
|
|
138
258
|
// Mine again: my PRs with nobody else on them, and issues assigned to me.
|
|
139
259
|
const mineNow = new Set([...present(data.mine.nodes).filter((pr) => !handedOver(pr, me)).map((pr) => pr.id), ...Object.keys(assignedNow(data))]);
|
|
140
260
|
const known = new Set(ledger.events.map((event) => event.id));
|
|
141
261
|
const fresh = found.filter((event) => !known.has(event.id) && event.at >= since(windowDays));
|
|
262
|
+
const cleared = new Set(ledger.gotIt);
|
|
142
263
|
const events = [...ledger.events, ...fresh]
|
|
143
|
-
|
|
264
|
+
// A reopen leaves the closed PR's week; its note takes its place.
|
|
265
|
+
.filter((event) => !(event.kind === "closed" && reopenedIds.has(event.subjectId)))
|
|
266
|
+
.flatMap((event) => {
|
|
267
|
+
if (!isHandover(event) || !mineNow.has(event.subjectId)) return [event];
|
|
268
|
+
// Back in my hands: an uncleared handover goes; a cleared one stays in its week, marked so my later work is mine.
|
|
269
|
+
return cleared.has(event.id) ? [{ ...event, returned: true as const }] : [];
|
|
270
|
+
})
|
|
144
271
|
.filter((event) => event.at >= since(keepDays))
|
|
145
272
|
.sort((a, b) => b.at.localeCompare(a.at));
|
|
146
273
|
const ids = new Set(events.map((event) => event.id));
|
|
274
|
+
// Nothing older than the window is worth tracking, whether or not its lookup worked [GH-01].
|
|
275
|
+
for (const [id, pr] of Object.entries(tracked)) if (pr.closedAt < since(keepDays)) delete tracked[id];
|
|
147
276
|
|
|
148
277
|
// Issues that left my search and couldn't be looked up stay, so they're checked again.
|
|
149
278
|
const assigned = assignedNow(data);
|
|
@@ -154,18 +283,29 @@ export function nextLedger(ledger: ChangeLedger, data: Data, me: string, lookups
|
|
|
154
283
|
const before = ledger.handed[pr.id];
|
|
155
284
|
if (before !== undefined || lookups.has(pr.id)) handed[pr.id] = lookups.has(pr.id) ? pr.signature : before;
|
|
156
285
|
}
|
|
157
|
-
return { events, gotIt: ledger.gotIt.filter((id) => ids.has(id)), assigned, handed };
|
|
286
|
+
return { events, gotIt: ledger.gotIt.filter((id) => ids.has(id)), assigned, handed, tracked };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// An archive acknowledges exactly the events on its work at that moment, keyed by "owner/name#number" [ATT-22].
|
|
290
|
+
// `upTo` keeps what others did after then unread: a session archived in Paseo acknowledges only what came before it.
|
|
291
|
+
export function acknowledge(ledger: ChangeLedger, keys: string[], upTo?: string): ChangeLedger {
|
|
292
|
+
const wanted = new Set(keys.map((key) => key.toLowerCase()));
|
|
293
|
+
const before = (at: string) => !upTo || Date.parse(at) <= Date.parse(upTo);
|
|
294
|
+
const ids = ledger.events.filter((event) => wanted.has(event.key.toLowerCase()) && before(event.at)).map((event) => event.id);
|
|
295
|
+
return { ...ledger, gotIt: [...new Set([...ledger.gotIt, ...ids])] };
|
|
158
296
|
}
|
|
159
297
|
|
|
160
|
-
// What a refresh found, with the Got its tapped while it ran, as those changed the ledger after the refresh
|
|
298
|
+
// What a refresh found, with the Got its, and archives, tapped while it ran, as those changed the ledger after the refresh
|
|
299
|
+
// read it [HIS-07, ATT-22].
|
|
161
300
|
export function withGotIt(next: ChangeLedger, gotIt: string[]): ChangeLedger {
|
|
162
301
|
const ids = new Set(next.events.map((event) => event.id));
|
|
163
302
|
return { ...next, gotIt: gotIt.filter((id) => ids.has(id)) };
|
|
164
303
|
}
|
|
165
304
|
|
|
166
|
-
// The ledger as cards:
|
|
167
|
-
//
|
|
168
|
-
|
|
305
|
+
// The ledger as cards: History's unread notes, newest first, what was cleared into each week, and the finished work a live
|
|
306
|
+
// refresh saw before History's daily fetch did [HIS-07]. Unread notes expire after `windowDays`; cleared ones stay
|
|
307
|
+
// `historySince`, out of the week's copy [HIS-06, HIS-07].
|
|
308
|
+
export function changeItems(ledger: ChangeLedger, now: Date, windowDays: number, historySince: string): { notes: Item[]; history: Item[]; finished: Item[] } {
|
|
169
309
|
const since = new Date(now.getTime() - windowDays * 86_400_000).toISOString();
|
|
170
310
|
const gotIt = new Set(ledger.gotIt);
|
|
171
311
|
const base = (change: OthersChange): Omit<Item, "key" | "section"> => ({
|
|
@@ -180,18 +320,36 @@ export function changeItems(ledger: ChangeLedger, now: Date, windowDays: number,
|
|
|
180
320
|
children: [],
|
|
181
321
|
target: { kind: change.subject, repo: change.repo, number: change.number, title: change.title, url: change.url },
|
|
182
322
|
othersActivityAt: change.at,
|
|
323
|
+
closedAt: change.at,
|
|
183
324
|
change,
|
|
184
325
|
});
|
|
185
|
-
const
|
|
326
|
+
const notes = ledger.events
|
|
186
327
|
.filter((change) => !gotIt.has(change.id) && change.at >= since)
|
|
187
328
|
.map((change): Item => ({ ...base(change), key: `change:${change.id}`, section: "changed", actions: ["got_it"] }));
|
|
188
329
|
const history = ledger.events
|
|
189
|
-
|
|
190
|
-
.
|
|
191
|
-
|
|
330
|
+
// A merge or an issue close is the week's Finished, not a change repeated under it [HIS-07].
|
|
331
|
+
.filter((change) => gotIt.has(change.id) && !isFinishedChange(change) && change.at >= historySince)
|
|
332
|
+
.map((change): Item => ({ ...base(change), key: `handed:${change.id}`, section: "handed_off", actions: [] }));
|
|
333
|
+
// A merge or close someone else did is finished work at once, not only when History's daily fetch catches up [HIS-07].
|
|
334
|
+
const finished = ledger.events
|
|
335
|
+
.filter((change) => isFinishedChange(change) && change.at >= historySince)
|
|
336
|
+
.map(
|
|
337
|
+
(change): Item => ({
|
|
338
|
+
...base(change),
|
|
339
|
+
key: change.key,
|
|
340
|
+
section: "done",
|
|
341
|
+
// The week's cards say it in the finished wording, not the note's: "merged 2h ago by dave" [HIS-05, HIS-07].
|
|
342
|
+
change: undefined,
|
|
343
|
+
...(change.kind === "merged" && change.actor ? { mergedBy: change.actor } : {}),
|
|
344
|
+
...(change.kind === "issue_closed" && change.actor ? { closedBy: change.actor } : {}),
|
|
345
|
+
...(change.reason === "not_planned" ? { notPlanned: true as const, chips: [{ text: "not planned", tone: "neutral" as const }] } : {}),
|
|
346
|
+
}),
|
|
347
|
+
);
|
|
348
|
+
return { notes, history, finished };
|
|
192
349
|
}
|
|
193
350
|
|
|
194
|
-
// Keys of my PRs and issues
|
|
351
|
+
// Keys of my PRs and issues still out of my hands: a merge by their new owner isn't my finished work [HIS-06]. A cleared
|
|
352
|
+
// handover whose work came back to me doesn't hide what I later finish.
|
|
195
353
|
export function handedKeys(ledger: ChangeLedger): Set<string> {
|
|
196
|
-
return new Set(ledger.events.filter(isHandover).map((event) => event.key));
|
|
354
|
+
return new Set(ledger.events.filter((event) => isHandover(event) && !event.returned).map((event) => event.key));
|
|
197
355
|
}
|
package/server/classify.ts
CHANGED
|
@@ -435,7 +435,10 @@ export function classifyHistory(data: HistoryResult): Item[] {
|
|
|
435
435
|
};
|
|
436
436
|
for (const issue of compact(data.closed.nodes)) {
|
|
437
437
|
const notPlanned = issue.stateReason === "NOT_PLANNED";
|
|
438
|
-
|
|
438
|
+
// Who closed it, when it wasn't me [HIS-07].
|
|
439
|
+
const closer = issue.timelineItems?.nodes.at(-1)?.actor?.login;
|
|
440
|
+
const closedBy = closer && me && closer.toLowerCase() !== me ? { closedBy: closer } : {};
|
|
441
|
+
const item = kinded({ ...plain(issue, "issue", "done"), closedAt: issue.closedAt, chips: notPlanned ? [chip("not planned")] : [], ...closedBy, ...(notPlanned ? { notPlanned: true as const } : {}) }, issue);
|
|
439
442
|
// Not planned issues get no PRs under them: the PRs were still done, so they're listed on their own [HIS-03].
|
|
440
443
|
if (!notPlanned) closed.set(prKey(issue), item);
|
|
441
444
|
items.push(item);
|
|
@@ -450,7 +453,11 @@ export function classifyHistory(data: HistoryResult): Item[] {
|
|
|
450
453
|
if (issue) issue.children.push({ key: prKey(pr), number: pr.number, title: pr.title, url: pr.url, draft: false, chips: [], attention: false, ...mergedBy });
|
|
451
454
|
else items.push(kinded({ ...plain(pr, "pr", "done"), closedAt: pr.mergedAt, ...mergedBy }, pr));
|
|
452
455
|
}
|
|
453
|
-
for (const pr of compact(data.reviewed.nodes))
|
|
456
|
+
for (const pr of compact(data.reviewed.nodes)) {
|
|
457
|
+
const by = pr.mergedBy?.login;
|
|
458
|
+
const mergedBy = by && me && by.toLowerCase() !== me ? { mergedBy: by } : {};
|
|
459
|
+
items.push(kinded({ ...plain(pr, "pr", "reviewed"), closedAt: pr.mergedAt, person: author(pr.author, pr.mergedAt), ...mergedBy }, pr));
|
|
460
|
+
}
|
|
454
461
|
return items.sort(order);
|
|
455
462
|
}
|
|
456
463
|
|