Skip to content

Commit 43f1a28

Browse files
author
Carl
committed
Merge remote-tracking branch 'origin/main' into morganm/message-actions
* origin/main: Add optional channel templates, teams and personal group defaults (#181) Signed-off-by: Carl <c217fe6b9d958f41c3a5e030dccc7f626775a923089cb6491305eade75ea1f1b@buzz.block.builderlab.xyz>
2 parents 6b0a276 + a9f68a4 commit 43f1a28

77 files changed

Lines changed: 5781 additions & 346 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎AGENTS.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ callers, and relevant design docs; preserve documented product decisions and
4141
ownership boundaries. Resolve answerable questions from evidence; ask before
4242
deviating from agreed scope or product behavior.
4343

44+
Agent selection must consume `session.agentChoices`, not independently reconstruct
45+
native/legacy inventories. Keep usable partial choices distinct from complete
46+
inference evidence; preserve each action's membership/archive policy. See
47+
[shared agent selection](docs/agents.md#shared-agent-selection).
48+
4449
Target **9/10+ for minimalness, elegance, and correctness**: the smallest complete
4550
solution, clear ownership, and no known material defects. Prefer existing patterns
4651
and subtraction. No opportunistic refactors, speculative abstractions, or new

‎crates/plugin-manager/src/lib.rs‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ pub fn valid_id(id: &str) -> Result<()> {
4747
}
4848
pub fn bundled_manifests() -> Vec<Manifest> {
4949
vec![
50+
serde_json::from_str(include_str!(
51+
"../../../src/bundled/channel-templates/manifest.json"
52+
))
53+
.expect("channel templates manifest"),
5054
serde_json::from_str(include_str!(
5155
"../../../src/bundled/agent-activity/manifest.json"
5256
))
@@ -274,7 +278,7 @@ impl Manager {
274278
.bundled_overrides
275279
.get(&manifest.id)
276280
.copied()
277-
.unwrap_or(true);
281+
.unwrap_or(manifest.id != "buzz.channel-templates");
278282
PluginInfo {
279283
manifest,
280284
source: "bundled",
@@ -612,6 +616,30 @@ mod tests {
612616
use super::Manager;
613617
use std::fs;
614618

619+
#[test]
620+
fn templates_default_off_and_preserve_explicit_overrides() {
621+
let temp = tempfile::tempdir().unwrap();
622+
let manager = Manager::open(Some(temp.path().into()), "templates-test", false).unwrap();
623+
let enabled = |manager: &Manager| {
624+
manager
625+
.catalog()
626+
.unwrap()
627+
.plugins
628+
.into_iter()
629+
.find(|plugin| plugin.manifest.id == "buzz.channel-templates")
630+
.unwrap()
631+
.enabled
632+
};
633+
assert!(!enabled(&manager));
634+
manager.change("enable", "buzz.channel-templates").unwrap();
635+
let reopened = Manager::open(Some(temp.path().into()), "templates-test", false).unwrap();
636+
assert!(enabled(&reopened));
637+
reopened
638+
.change("disable", "buzz.channel-templates")
639+
.unwrap();
640+
assert!(!enabled(&manager));
641+
}
642+
615643
#[test]
616644
fn reload_rejects_enable_between_disk_read_and_commit() {
617645
let temp = tempfile::tempdir().unwrap();

‎dev/channel-kit.mjs‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { getPublicKey, nip44 } from "nostr-tools";
2+
import { eventDto } from "../src/features/relay/events.ts";
3+
import {
4+
coordinate,
5+
KIT_TAG,
6+
parseKitRecord,
7+
} from "../src/features/channel-templates/model.ts";
8+
9+
export function prepareChannelKit(raw, secret, community) {
10+
const record = parseKitRecord(raw, community);
11+
const key = nip44.v2.utils.getConversationKey(secret, getPublicKey(secret));
12+
try {
13+
return nip44.v2.encrypt(JSON.stringify(record), key);
14+
} finally {
15+
key.fill(0);
16+
}
17+
}
18+
/** Validate narrow ciphertext intent both before signing and before publishing. */
19+
export function admitChannelKit(event, secret, community) {
20+
if (
21+
event?.kind !== 30078 ||
22+
typeof event.content !== "string" ||
23+
event.content.length > 24 * 1024 ||
24+
!Array.isArray(event.tags)
25+
)
26+
throw new Error("Invalid private recipe event");
27+
const key = nip44.v2.utils.getConversationKey(secret, getPublicKey(secret));
28+
try {
29+
const record = parseKitRecord(
30+
JSON.parse(nip44.v2.decrypt(event.content, key)),
31+
community,
32+
);
33+
const ds = event.tags.filter((t) => t[0] === "d"),
34+
ts = event.tags.filter((t) => t[0] === "t");
35+
if (
36+
ds.length !== 1 ||
37+
ds[0].length !== 2 ||
38+
ds[0][1] !== coordinate(record) ||
39+
ts.length !== 1 ||
40+
ts[0].length !== 2 ||
41+
ts[0][1] !== KIT_TAG ||
42+
event.tags.some((t) => !["d", "t", "client-id"].includes(t[0]))
43+
)
44+
throw new Error("Invalid private recipe coordinate");
45+
return record;
46+
} finally {
47+
key.fill(0);
48+
}
49+
}
50+
export function decodeChannelKit(raw, secret, community) {
51+
if (
52+
!Array.isArray(raw) ||
53+
raw.length > 16 ||
54+
Buffer.byteLength(JSON.stringify(raw)) > 512 * 1024
55+
)
56+
throw new Error("Recipe decode capacity exceeded");
57+
return raw.map((value) => {
58+
const event = eventDto(value);
59+
if (event.pubkey !== getPublicKey(secret))
60+
throw new Error("Recipe belongs to another viewer");
61+
return {
62+
eventId: event.id,
63+
record: admitChannelKit(event, secret, community),
64+
};
65+
});
66+
}
67+
export function validCanvas(event) {
68+
return (
69+
event?.kind === 40100 &&
70+
typeof event.content === "string" &&
71+
Buffer.byteLength(event.content) <= 24 * 1024 &&
72+
Array.isArray(event.tags) &&
73+
event.tags.filter((t) => t[0] === "h").length === 1 &&
74+
event.tags.every(
75+
(t) =>
76+
Array.isArray(t) &&
77+
t.length === 2 &&
78+
(t[0] === "h"
79+
? /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(t[1])
80+
: t[0] === "client-id" &&
81+
typeof t[1] === "string" &&
82+
t[1].length <= 128),
83+
)
84+
);
85+
}

‎dev/channel-kit.test.mjs‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import { expect, it } from "vitest";
2+
import { keypair, signed } from "../src/features/relay/testing.ts";
3+
import {
4+
coordinate,
5+
KIT_TAG,
6+
parseKitRecord,
7+
resolveLineup,
8+
} from "../src/features/channel-templates/model.ts";
9+
import {
10+
admitChannelKit,
11+
decodeChannelKit,
12+
prepareChannelKit,
13+
validCanvas,
14+
} from "./channel-kit.mjs";
15+
16+
const community = "https://relay.example.test";
17+
const owner = keypair();
18+
const record = {
19+
version: 1,
20+
community,
21+
deleted: false,
22+
value: {
23+
type: "template",
24+
id: "daily",
25+
name: "Daily",
26+
description: "",
27+
teamIds: [],
28+
agents: [owner.pubkey],
29+
canvas: "Private working instructions",
30+
},
31+
};
32+
function encrypted(value = record, tags) {
33+
return signed(owner, {
34+
kind: 30078,
35+
content: prepareChannelKit(value, owner.secret, community),
36+
tags: tags ?? [
37+
["d", coordinate(value)],
38+
["t", KIT_TAG],
39+
],
40+
});
41+
}
42+
43+
it("self-encrypts private recipes and admits only the exact community/coordinate", () => {
44+
const event = encrypted();
45+
expect(event.content).not.toContain(record.value.canvas);
46+
expect(decodeChannelKit([event], owner.secret, community)).toEqual([
47+
{ eventId: event.id, record },
48+
]);
49+
expect(() =>
50+
admitChannelKit(event, owner.secret, "https://elsewhere.test"),
51+
).toThrow();
52+
expect(() => decodeChannelKit([event], keypair().secret, community)).toThrow(
53+
/another viewer/,
54+
);
55+
expect(() =>
56+
admitChannelKit(
57+
{ ...event, tags: [...event.tags, ["p", owner.pubkey]] },
58+
owner.secret,
59+
community,
60+
),
61+
).toThrow(/coordinate/);
62+
expect(() =>
63+
admitChannelKit(
64+
{
65+
...event,
66+
tags: [
67+
["d", "wrong"],
68+
["t", KIT_TAG],
69+
],
70+
},
71+
owner.secret,
72+
community,
73+
),
74+
).toThrow(/coordinate/);
75+
expect(() =>
76+
decodeChannelKit(Array(17).fill(event), owner.secret, community),
77+
).toThrow(/capacity/);
78+
});
79+
80+
it("rejects malformed/oversized recipes and mismatched ciphertext", () => {
81+
expect(() =>
82+
parseKitRecord(
83+
{
84+
...record,
85+
value: { ...record.value, agents: [owner.pubkey, owner.pubkey] },
86+
},
87+
community,
88+
),
89+
).toThrow(/selection/);
90+
expect(() =>
91+
prepareChannelKit(
92+
{ ...record, value: { ...record.value, canvas: "x".repeat(17 * 1024) } },
93+
owner.secret,
94+
community,
95+
),
96+
).toThrow(/16 KiB/);
97+
expect(() =>
98+
admitChannelKit(
99+
{ ...encrypted(), content: "broken" },
100+
owner.secret,
101+
community,
102+
),
103+
).toThrow();
104+
const tombstone = { ...record, deleted: true };
105+
expect(
106+
decodeChannelKit([encrypted(tombstone)], owner.secret, community)[0].record
107+
.deleted,
108+
).toBe(true);
109+
});
110+
111+
it("expands multiple teams and individual agents by exact key without namesake collapse", () => {
112+
const other = keypair().pubkey;
113+
const entries = ["first", "second"].map((id, i) => ({
114+
eventId: id,
115+
createdAt: 1,
116+
record: {
117+
...record,
118+
value: {
119+
type: "team",
120+
id,
121+
name: id,
122+
agents: i ? [other] : [owner.pubkey, other],
123+
},
124+
},
125+
}));
126+
const lineup = {
127+
teamIds: ["first", "second"],
128+
agents: [owner.pubkey],
129+
canvas: "",
130+
};
131+
const choices = [owner.pubkey, other].map((pubkey) => ({
132+
pubkey,
133+
name: "Namesake",
134+
}));
135+
expect(resolveLineup(lineup, entries, choices)).toEqual(choices);
136+
expect(() => resolveLineup(lineup, [], choices)).toThrow(
137+
/team is unavailable/,
138+
);
139+
expect(() => resolveLineup(lineup, entries, choices.slice(0, 1))).toThrow(
140+
/unavailable in this community/,
141+
);
142+
});
143+
144+
it("admits only bounded Canvas writes with one exact channel and no notification or privilege tags", () => {
145+
const canvas = {
146+
kind: 40100,
147+
content: "# Plan",
148+
tags: [["h", "11111111-1111-4111-8111-111111111111"]],
149+
};
150+
expect(validCanvas(canvas)).toBe(true);
151+
for (const tags of [
152+
[],
153+
[...canvas.tags, ...canvas.tags],
154+
[...canvas.tags, ["p", owner.pubkey]],
155+
[["h", "invalid"]],
156+
])
157+
expect(validCanvas({ ...canvas, tags })).toBe(false);
158+
expect(validCanvas({ ...canvas, content: "é".repeat(13 * 1024) })).toBe(
159+
false,
160+
);
161+
});

0 commit comments

Comments
 (0)