Skip to content

Commit 441de4a

Browse files
feat(server,web,mobile): let plugins show statuses on threads
A plugin with the `status` capability (proposed API) can set and clear short statuses on threads. They reach clients through the existing thread status channel and show beside provider statuses (such as a Pi extension's) in the web and desktop thread header and the mobile status row, attributed to the plugin by name. Plugin statuses use their own capacity pool (3 sources per thread, 32 threads, 64 items), so provider statuses keep exactly the room they had. Each plugin process is limited to 16 statuses and 10 updates at once, then 2 per second, and everything it set is cleared when that process stops. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 63f6219 commit 441de4a

28 files changed

Lines changed: 986 additions & 62 deletions

‎apps/mobile/src/features/threads/ThreadContributionStatusStrip.tsx‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ export function useThreadContributionStatusStripInset(input: {
4949

5050
/**
5151
* Advisory statuses the thread's provider set, such as Pi extension
52-
* `setStatus` text, as one row of chips floating just under the navigation
53-
* header. It overlays the feed like the header does, so a status changing
54-
* never resizes the composer; the feed reserves its band through
55-
* `useThreadContributionStatusStripInset`. Renders nothing when the server
56-
* lacks the capability or the thread has no statuses. Pressing a chip shows
57-
* its tooltip and where it came from.
52+
* `setStatus` text, and statuses plugins set, as one row of chips floating
53+
* just under the navigation header. It overlays the feed like the header
54+
* does, so a status changing never resizes the composer; the feed reserves
55+
* its band through `useThreadContributionStatusStripInset`. Renders nothing
56+
* when the server lacks the capability or the thread has no statuses.
57+
* Pressing a chip shows its tooltip and where it came from.
5858
*/
5959
export function ThreadContributionStatusStrip(props: {
6060
readonly environmentId: EnvironmentId;
@@ -102,7 +102,9 @@ export function ThreadContributionStatusStrip(props: {
102102
onPress={() => Alert.alert(chip.details.title, chip.details.message)}
103103
>
104104
<View className="min-h-7 flex-row items-center gap-1.5 rounded-full border border-border-subtle bg-card-alt px-2.5">
105-
{chip.leadsSource ? <ProviderIcon provider={chip.driver} size={12} /> : null}
105+
{chip.leadsSource && chip.driver !== null ? (
106+
<ProviderIcon provider={chip.driver} size={12} />
107+
) : null}
106108
{dotClass === null ? null : (
107109
<View className={cn("h-1.5 w-1.5 shrink-0 rounded-full", dotClass)} />
108110
)}

‎apps/mobile/src/features/threads/thread-contribution-status-presentation.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ const entry = (
2929
});
3030

3131
describe("threadContributionStatusChips", () => {
32+
it("names a plugin as the source of its chips, without a provider icon", () => {
33+
const [chip] = threadContributionStatusChips([
34+
{
35+
threadId: ThreadId.make("thread-1"),
36+
source: { kind: "plugin", pluginId: "acme.ci", name: "CI watcher" },
37+
items: [{ key: "ci", text: "passing", tone: "success" }],
38+
},
39+
]);
40+
expect(chip).toMatchObject({
41+
driver: null,
42+
text: "passing",
43+
tone: "success",
44+
accessibilityLabel: "CI watcher status: passing",
45+
help: "Set by the CI watcher plugin.",
46+
});
47+
});
48+
3249
it("shows nothing for a thread without statuses", () => {
3350
expect(threadContributionStatusChips([])).toEqual([]);
3451
});

‎apps/mobile/src/features/threads/thread-contribution-status-presentation.ts‎

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
type ContributionStatusEntry,
3+
type ContributionStatusSource,
34
type ContributionStatusTone,
45
contributionStatusSourceKey,
56
PROVIDER_DISPLAY_NAMES,
@@ -9,8 +10,9 @@ import {
910
export interface ThreadContributionStatusChip {
1011
/** Source key plus item key, so a provider session taking over re-keys the chip. */
1112
readonly id: string;
12-
readonly driver: ProviderDriverKind;
13-
/** The first chip of each source carries that source's provider icon. */
13+
/** The provider that set it, or null for a plugin's status. */
14+
readonly driver: ProviderDriverKind | null;
15+
/** The first chip of each provider source carries that provider's icon. */
1416
readonly leadsSource: boolean;
1517
readonly text: string;
1618
readonly tone: ContributionStatusTone;
@@ -26,8 +28,13 @@ function providerLabel(driver: ProviderDriverKind): string {
2628
return PROVIDER_DISPLAY_NAMES[driver] ?? driver;
2729
}
2830

29-
function statusHelp(driver: ProviderDriverKind): string {
30-
const origin = driver === "pi" ? "a Pi extension" : providerLabel(driver);
31+
function sourceLabel(source: ContributionStatusSource): string {
32+
return source.kind === "plugin" ? source.name : providerLabel(source.driver);
33+
}
34+
35+
function statusHelp(source: ContributionStatusSource): string {
36+
if (source.kind === "plugin") return `Set by the ${source.name} plugin.`;
37+
const origin = source.driver === "pi" ? "a Pi extension" : providerLabel(source.driver);
3138
return `Set by ${origin}. It can lag a session change.`;
3239
}
3340

@@ -36,25 +43,24 @@ export function threadContributionStatusChips(
3643
entries: ReadonlyArray<ContributionStatusEntry>,
3744
): ReadonlyArray<ThreadContributionStatusChip> {
3845
return entries.flatMap((entry) => {
39-
const sourceKey = contributionStatusSourceKey(entry.source);
40-
const { driver } = entry.source;
41-
return entry.items.map((item, index) => {
42-
const help = statusHelp(driver);
43-
return {
44-
id: JSON.stringify([sourceKey, item.key]),
45-
driver,
46-
leadsSource: index === 0,
47-
text: item.text,
48-
tone: item.tone ?? "neutral",
49-
tooltip: item.tooltip ?? null,
50-
accessibilityLabel: `${providerLabel(driver)} status: ${item.text}`,
51-
help,
52-
details: {
53-
title: `${providerLabel(driver)} status`,
54-
message: [item.text, item.tooltip, help].filter(Boolean).join("\n\n"),
55-
},
56-
};
57-
});
46+
const { source } = entry;
47+
const sourceKey = contributionStatusSourceKey(source);
48+
const label = sourceLabel(source);
49+
const help = statusHelp(source);
50+
return entry.items.map((item, index) => ({
51+
id: JSON.stringify([sourceKey, item.key]),
52+
driver: source.kind === "plugin" ? null : source.driver,
53+
leadsSource: index === 0,
54+
text: item.text,
55+
tone: item.tone ?? "neutral",
56+
tooltip: item.tooltip ?? null,
57+
accessibilityLabel: `${label} status: ${item.text}`,
58+
help,
59+
details: {
60+
title: `${label} status`,
61+
message: [item.text, item.tooltip, help].filter(Boolean).join("\n\n"),
62+
},
63+
}));
5864
});
5965
}
6066

‎apps/server/src/contributions/ContributionStatusStore.test.ts‎

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { assert, describe, it } from "@effect/vitest";
22
import {
33
CONTRIBUTION_STATUS_MAX_ITEMS,
44
CONTRIBUTION_STATUS_MAX_ITEMS_PER_SOURCE,
5+
CONTRIBUTION_STATUS_MAX_PLUGIN_ITEMS,
6+
CONTRIBUTION_STATUS_MAX_PLUGIN_SOURCES_PER_THREAD,
7+
CONTRIBUTION_STATUS_MAX_PLUGIN_THREADS,
58
CONTRIBUTION_STATUS_MAX_THREADS,
9+
type ContributionStatusSnapshot,
610
type ContributionStatusSource,
711
ProviderDriverKind,
812
ProviderInstanceId,
@@ -38,6 +42,27 @@ const openHandle = Effect.fn("openHandle")(function* (
3842
return { handle, close: Scope.close(scope, Exit.void) };
3943
});
4044

45+
/** A plugin producer, which has its own capacity pool beside provider sessions. */
46+
const openPlugin = Effect.fn("openPlugin")(function* (
47+
store: ContributionStatusStore.ContributionStatusStoreShape,
48+
pluginId: string,
49+
threadId: ThreadId,
50+
) {
51+
const handle = yield* store
52+
.openSource({ kind: "plugin", pluginId, name: pluginId })
53+
.pipe(Scope.provide(yield* Scope.make()));
54+
yield* handle.bindThread(threadId);
55+
return handle;
56+
});
57+
58+
const countBy = (snapshot: ContributionStatusSnapshot, kind: ContributionStatusSource["kind"]) => {
59+
const entries = snapshot.entries.filter((entry) => entry.source.kind === kind);
60+
return {
61+
threads: new Set(entries.map((entry) => entry.threadId)).size,
62+
items: entries.reduce((total, entry) => total + entry.items.length, 0),
63+
};
64+
};
65+
4166
const itemsByThread = (store: ContributionStatusStore.ContributionStatusStoreShape) =>
4267
Effect.map(store.snapshot, (snapshot) =>
4368
Object.fromEntries(
@@ -169,10 +194,9 @@ describe("ContributionStatusStore", () => {
169194

170195
const snapshot = yield* store.snapshot;
171196
assert.deepStrictEqual(yield* itemsByThread(store), { [THREAD_A]: ["mode=new"] });
172-
assert.strictEqual(
173-
snapshot.entries[0]?.source.providerSessionId,
174-
ProviderSessionId.make("session-new"),
175-
);
197+
assert.deepInclude(snapshot.entries[0]?.source, {
198+
providerSessionId: ProviderSessionId.make("session-new"),
199+
});
176200
}),
177201
);
178202

@@ -277,4 +301,75 @@ describe("ContributionStatusStore", () => {
277301
);
278302
}),
279303
);
304+
it.effect("keeps every slot a provider session had on a thread full of plugins", () =>
305+
Effect.gen(function* () {
306+
const store = yield* ContributionStatusStore.make();
307+
for (let index = 0; index <= CONTRIBUTION_STATUS_MAX_PLUGIN_SOURCES_PER_THREAD; index += 1) {
308+
const plugin = yield* openPlugin(store, `acme.p${index}`, THREAD_A);
309+
yield* plugin.set({ key: "run", text: "on" });
310+
}
311+
// The plugin pool is full on this thread: the extra plugin was refused.
312+
assert.strictEqual(
313+
countBy(yield* store.snapshot, "plugin").items,
314+
CONTRIBUTION_STATUS_MAX_PLUGIN_SOURCES_PER_THREAD,
315+
);
316+
317+
const provider = yield* openHandle(store, "session-1", THREAD_A);
318+
yield* provider.handle.set({ key: "mode", text: "plan" });
319+
const kinds = (yield* store.snapshot).entries.map((entry) => entry.source.kind);
320+
assert.deepStrictEqual(kinds, ["provider-session", "plugin", "plugin", "plugin"]);
321+
}),
322+
);
323+
324+
it.effect("never lets plugins take thread or item capacity from provider sessions", () =>
325+
Effect.gen(function* () {
326+
const store = yield* ContributionStatusStore.make();
327+
// A provider status that exists before the plugins arrive.
328+
const existing = yield* openHandle(store, "provider-0", ThreadId.make("t-0"));
329+
yield* existing.handle.set({ key: "mode", text: "plan" });
330+
331+
// Plugins fill their whole pool: every plugin thread and every plugin item.
332+
const perThread =
333+
CONTRIBUTION_STATUS_MAX_PLUGIN_ITEMS / CONTRIBUTION_STATUS_MAX_PLUGIN_THREADS;
334+
for (let index = 0; index < CONTRIBUTION_STATUS_MAX_PLUGIN_THREADS; index += 1) {
335+
const plugin = yield* openPlugin(store, `acme.p${index}`, ThreadId.make(`t-${index}`));
336+
for (let item = 0; item < perThread; item += 1)
337+
yield* plugin.set({ key: `k${item}`, text: "on" });
338+
}
339+
const refused = yield* openPlugin(store, "acme.late", ThreadId.make("t-late"));
340+
yield* refused.set({ key: "k", text: "refused" });
341+
assert.deepStrictEqual(countBy(yield* store.snapshot, "plugin"), {
342+
threads: CONTRIBUTION_STATUS_MAX_PLUGIN_THREADS,
343+
items: CONTRIBUTION_STATUS_MAX_PLUGIN_ITEMS,
344+
});
345+
346+
// The existing provider source still adds a new key, and providers still
347+
// reach their own thread and item caps, on plugin threads and new ones.
348+
yield* existing.handle.set({ key: "branch", text: "main" });
349+
const perProvider = CONTRIBUTION_STATUS_MAX_ITEMS / CONTRIBUTION_STATUS_MAX_THREADS;
350+
for (let item = 2; item < perProvider; item += 1)
351+
yield* existing.handle.set({ key: `k${item}`, text: "on" });
352+
for (let index = 1; index < CONTRIBUTION_STATUS_MAX_THREADS; index += 1) {
353+
const provider = yield* openHandle(store, `provider-${index}`, ThreadId.make(`t-${index}`));
354+
for (let item = 0; item < perProvider; item += 1)
355+
yield* provider.handle.set({ key: `k${item}`, text: "on" });
356+
}
357+
const snapshot = yield* store.snapshot;
358+
assert.deepStrictEqual(countBy(snapshot, "provider-session"), {
359+
threads: CONTRIBUTION_STATUS_MAX_THREADS,
360+
items: CONTRIBUTION_STATUS_MAX_ITEMS,
361+
});
362+
assert.deepStrictEqual(countBy(snapshot, "plugin"), {
363+
threads: CONTRIBUTION_STATUS_MAX_PLUGIN_THREADS,
364+
items: CONTRIBUTION_STATUS_MAX_PLUGIN_ITEMS,
365+
});
366+
// The provider caps themselves are unchanged.
367+
const overflow = yield* openHandle(store, "provider-late", ThreadId.make("t-late"));
368+
yield* overflow.handle.set({ key: "mode", text: "refused" });
369+
assert.strictEqual(
370+
countBy(yield* store.snapshot, "provider-session").items,
371+
CONTRIBUTION_STATUS_MAX_ITEMS,
372+
);
373+
}),
374+
);
280375
});

‎apps/server/src/contributions/ContributionStatusStore.ts‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import {
44
CONTRIBUTION_STATUS_KEY_MAX_LENGTH,
55
CONTRIBUTION_STATUS_MAX_ITEMS,
66
CONTRIBUTION_STATUS_MAX_ITEMS_PER_SOURCE,
7+
CONTRIBUTION_STATUS_MAX_PLUGIN_ITEMS,
8+
CONTRIBUTION_STATUS_MAX_PLUGIN_SOURCES_PER_THREAD,
9+
CONTRIBUTION_STATUS_MAX_PLUGIN_THREADS,
710
CONTRIBUTION_STATUS_MAX_SOURCES_PER_THREAD,
811
CONTRIBUTION_STATUS_MAX_THREADS,
912
CONTRIBUTION_STATUS_TEXT_MAX_LENGTH,
@@ -108,7 +111,7 @@ const HAS_CONTROL_CHARACTER = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/;
108111
* contract allows, or contains a control character or lone UTF-16 surrogate
109112
* is rejected on set and clear alike.
110113
*/
111-
const isValidKey = (key: string) =>
114+
export const isValidContributionStatusKey = (key: string) =>
112115
key.length > 0 &&
113116
key.length <= CONTRIBUTION_STATUS_KEY_MAX_LENGTH &&
114117
key.isWellFormed() &&
@@ -120,7 +123,7 @@ const isValidKey = (key: string) =>
120123
* at most `maxLength` UTF-16 units without splitting a surrogate pair. A
121124
* truncated value ends in `…`.
122125
*/
123-
function normalizeContributionStatusText(raw: string, maxLength: number): string {
126+
export function normalizeContributionStatusText(raw: string, maxLength: number): string {
124127
const text = NodeUtil.stripVTControlCharacters(raw.toWellFormed())
125128
.replace(CONTROL_CHARACTERS, " ")
126129
.replace(/\s+/g, " ")
@@ -143,9 +146,24 @@ interface SourceSlot {
143146
/**
144147
* Which handles compete for one entry on a thread. A thread hosts one provider
145148
* session at a time, so every provider-session source shares a slot and a new
146-
* session takes the old one over; plugins will each get their own slot.
149+
* session takes the old one over; each plugin gets its own slot.
147150
*/
148-
const slotKey = (source: ContributionStatusSource) => source.kind;
151+
const slotKey = (source: ContributionStatusSource) =>
152+
source.kind === "plugin" ? contributionStatusSourceKey(source) : source.kind;
153+
154+
/** Capacity pools: plugins count only against their own, so they never crowd out provider statuses. */
155+
const poolLimits = (source: ContributionStatusSource) =>
156+
source.kind === "plugin"
157+
? {
158+
sources: CONTRIBUTION_STATUS_MAX_PLUGIN_SOURCES_PER_THREAD,
159+
threads: CONTRIBUTION_STATUS_MAX_PLUGIN_THREADS,
160+
items: CONTRIBUTION_STATUS_MAX_PLUGIN_ITEMS,
161+
}
162+
: {
163+
sources: CONTRIBUTION_STATUS_MAX_SOURCES_PER_THREAD,
164+
threads: CONTRIBUTION_STATUS_MAX_THREADS,
165+
items: CONTRIBUTION_STATUS_MAX_ITEMS,
166+
};
149167

150168
const sameItem = (left: ContributionStatusItem | undefined, right: ContributionStatusItem) =>
151169
left !== undefined &&
@@ -169,28 +187,33 @@ export const make = Effect.fn("contributions.status.make")(function* () {
169187
* Binding a thread only records ownership; capacity is taken by the first
170188
* visible item and returned by the last clear, so silent producers never
171189
* crowd out ones that show something, and a rejected producer's later set
172-
* is admitted once capacity frees up.
190+
* is admitted once capacity frees up. Each pool's caps count only its own
191+
* sources.
173192
*/
174193
const admitsNewItem = (threadId: ThreadId, slot: SourceSlot) => {
175194
if (slot.items.size >= CONTRIBUTION_STATUS_MAX_ITEMS_PER_SOURCE) return false;
195+
const limits = poolLimits(slot.source);
196+
const samePool = (other: SourceSlot) =>
197+
(other.source.kind === "plugin") === (slot.source.kind === "plugin");
176198
let items = 0;
177199
let visibleThreads = 0;
178200
for (const slots of threads.values()) {
179201
let visible = false;
180202
for (const other of slots.values()) {
203+
if (!samePool(other)) continue;
181204
items += other.items.size;
182205
visible ||= other.items.size > 0;
183206
}
184207
if (visible) visibleThreads += 1;
185208
}
186-
if (items >= CONTRIBUTION_STATUS_MAX_ITEMS) return false;
209+
if (items >= limits.items) return false;
187210
if (slot.items.size > 0) return true;
188211
let visibleSources = 0;
189212
for (const other of threads.get(threadId)?.values() ?? []) {
190-
if (other.items.size > 0) visibleSources += 1;
213+
if (samePool(other) && other.items.size > 0) visibleSources += 1;
191214
}
192-
if (visibleSources >= CONTRIBUTION_STATUS_MAX_SOURCES_PER_THREAD) return false;
193-
return visibleSources > 0 || visibleThreads < CONTRIBUTION_STATUS_MAX_THREADS;
215+
if (visibleSources >= limits.sources) return false;
216+
return visibleSources > 0 || visibleThreads < limits.threads;
194217
};
195218

196219
const currentSnapshot = (): ContributionStatusSnapshot => ({
@@ -273,7 +296,8 @@ export const make = Effect.fn("contributions.status.make")(function* () {
273296
update(() => {
274297
const slot = ownedSlot();
275298
const key = input.key;
276-
if (slot === undefined || threadId === null || !isValidKey(key)) return false;
299+
if (slot === undefined || threadId === null || !isValidContributionStatusKey(key))
300+
return false;
277301
const text = normalizeContributionStatusText(
278302
input.text,
279303
CONTRIBUTION_STATUS_TEXT_MAX_LENGTH,
@@ -301,7 +325,7 @@ export const make = Effect.fn("contributions.status.make")(function* () {
301325
clear: (key) =>
302326
update(() => {
303327
const slot = ownedSlot();
304-
if (slot === undefined || !isValidKey(key)) return false;
328+
if (slot === undefined || !isValidContributionStatusKey(key)) return false;
305329
return slot.items.delete(key);
306330
}),
307331
clearAll: update(() => {

0 commit comments

Comments
 (0)