@mrciphersmith/roomyx 0.10.2 → 0.11.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/CHANGELOG.md +69 -0
- package/README.md +32 -0
- package/package.json +1 -1
- package/src/cli.ts +11 -2
- package/src/log/schema.ts +76 -8
- package/src/log/store.ts +118 -10
- package/src/log/types.ts +35 -1
- package/src/log/write.ts +18 -32
- package/src/server/index.ts +31 -0
- package/src/server/tools/get-agent-delta.ts +16 -0
package/CHANGELOG.md
CHANGED
|
@@ -13,6 +13,75 @@ patch and never a minor. The rule, the reason, and an audit of the four of seven
|
|
|
13
13
|
releases that followed it are in
|
|
14
14
|
[`docs/roomyx/versioning.md`](docs/roomyx/versioning.md) and decision D-13.
|
|
15
15
|
|
|
16
|
+
## [0.11.0] — 2026-09-10
|
|
17
|
+
|
|
18
|
+
A minor, for the reason D-13 gives: `room.get_state` now returns a **changed
|
|
19
|
+
result for a caller who asked for nothing different**. The log format also gained
|
|
20
|
+
an optional field and two message kinds, which is additive on its own.
|
|
21
|
+
|
|
22
|
+
### Added
|
|
23
|
+
|
|
24
|
+
- **A room's state can change.** `goal_edit` and `add_participant` are message
|
|
25
|
+
kinds carrying the change in a new optional `change` field, and
|
|
26
|
+
`room.get_state` folds them: the header, then every edit in `seq` order, last
|
|
27
|
+
one winning. Before this, a room's state was its first line and nothing else —
|
|
28
|
+
so `goal_edit` and `add_participant` had **nowhere to land**, and
|
|
29
|
+
`updated_in_round` / `updated_by` sat in the schema with nothing writing them
|
|
30
|
+
since the format was defined.
|
|
31
|
+
- `room append --json <envelope>` accepts `change`, so an edit can be posted the
|
|
32
|
+
same way any other message is.
|
|
33
|
+
|
|
34
|
+
### Notes
|
|
35
|
+
|
|
36
|
+
- **The edit is a message, and that is the point.** The header cannot be
|
|
37
|
+
rewritten (append-only) and cannot be followed by a second one (every line
|
|
38
|
+
after the first must be a message, or the room becomes unreadable forever). A
|
|
39
|
+
message is therefore the only representation available — and because it is a
|
|
40
|
+
message it lands in the transcript, which is where this project already puts
|
|
41
|
+
state changes so a person can see them.
|
|
42
|
+
- **Reading is a fold, never a rewrite.** The file keeps the contract the room was
|
|
43
|
+
created with; bytes and mtime are untouched by any read. A test asserts both.
|
|
44
|
+
- **The body stays prose.** `"raise the pass mark to 85"`, with the structure
|
|
45
|
+
beside it in `change` — a JSON blob in the message pane is unreadable.
|
|
46
|
+
- **An edit kind with no `change` is a valid message that is not an edit**, and
|
|
47
|
+
the fold skips it. Making that ill-formed would have invented a new way for a
|
|
48
|
+
log to become unreadable, which is the one thing this format cannot afford.
|
|
49
|
+
A `change` whose `type` does not match its kind *is* ill-formed and is refused
|
|
50
|
+
by the writer.
|
|
51
|
+
- **Known divergence, recorded rather than fixed:** `archiveRoom` summarises a
|
|
52
|
+
closed room from its header, so `rooms history` shows the goal the room was
|
|
53
|
+
*created* with, not the one it ended with. Deciding which of those the index
|
|
54
|
+
should record is a follow-up.
|
|
55
|
+
|
|
56
|
+
## [0.10.3] — 2026-09-10
|
|
57
|
+
|
|
58
|
+
A patch. A new read tool, and no existing response changes.
|
|
59
|
+
|
|
60
|
+
### Added
|
|
61
|
+
|
|
62
|
+
- **`room.get_delta_for(agent_id, since_seq?)`** — what a participant has not
|
|
63
|
+
seen, by the convention the dispatcher used to apply by hand on every turn:
|
|
64
|
+
everything after that participant's own last message, excluding its own. The
|
|
65
|
+
rule is not new — `get_agent_detail` has always reported `lastSeenSeq` as the
|
|
66
|
+
maximum of an agent's own `seq` — but the cursor map and the filter were being
|
|
67
|
+
re-derived in a language model's context once per iteration. The answer also
|
|
68
|
+
reports `since_seq` and `cursor_from` (`agent-last-message` or `caller`), so an
|
|
69
|
+
empty delta means "nothing new" rather than "I passed the wrong cursor".
|
|
70
|
+
|
|
71
|
+
### Notes
|
|
72
|
+
|
|
73
|
+
- **It is a candidate delta, not a delivery record.** The server cannot see a
|
|
74
|
+
`SendMessage`: it knows a convention about the log and nothing else. The tool's
|
|
75
|
+
description says so, and a test asserts that wording **as a client receives
|
|
76
|
+
it** over the wire, because a string assertion in the tool's own unit test
|
|
77
|
+
cannot see what the wire says. A dispatcher may have failed to send a delta or
|
|
78
|
+
sent one twice, and no field here can tell.
|
|
79
|
+
- Its own messages are excluded at any cursor, including an explicit `0` — an
|
|
80
|
+
agent does not need to be told what it said.
|
|
81
|
+
- Stateless: the default cursor is derived from the log on every call. A cursor
|
|
82
|
+
the server *stored* would belong to one server instance, and the factory behind
|
|
83
|
+
`serve` runs once per session (the lesson `0.10.0` paid for).
|
|
84
|
+
|
|
16
85
|
## [0.10.2] — 2026-09-10
|
|
17
86
|
|
|
18
87
|
### Added
|
package/README.md
CHANGED
|
@@ -410,6 +410,38 @@ record; every subsequent line is a `message` record.
|
|
|
410
410
|
- `roster` entries are `{ id, name }` — `id` is what `room.get_agent_detail`
|
|
411
411
|
takes as `agent_id`; `name` is the display name used in `from`.
|
|
412
412
|
|
|
413
|
+
### Edits: how a room's state changes
|
|
414
|
+
|
|
415
|
+
The first line is a room's **original** state and it is never rewritten — the log
|
|
416
|
+
is append-only, and it cannot be followed by a second `state` line either, because
|
|
417
|
+
every line after the first must be a `message`. So an edit is a **message**:
|
|
418
|
+
|
|
419
|
+
```jsonl
|
|
420
|
+
{"type":"message","seq":5,"from":"owner","kind":"goal_edit","body":"raise the pass mark to 85","change":{"type":"goal_contract","goal_contract":{"version":1,"goal_statement":"...","criteria":"...","threshold":{"fail_below":60,"pass_at_or_above":85}}}}
|
|
421
|
+
{"type":"message","seq":6,"from":"owner","kind":"add_participant","body":"adding Omar","change":{"type":"roster","add":[{"id":"omar","name":"Омар"}]}}
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
- `change` is **optional**, so every message written by an earlier version still
|
|
425
|
+
validates. It is present only on the two edit kinds, and its `type` must match
|
|
426
|
+
the kind: `goal_edit` carries a `goal_contract`, `add_participant` carries
|
|
427
|
+
`roster`.
|
|
428
|
+
- **What a room reads as** is its first line *folded* with every edit, in `seq`
|
|
429
|
+
order, last one winning. That is what `room.get_state` returns. The file keeps
|
|
430
|
+
the original contract — the fold is an interpretation, never a rewrite — so a
|
|
431
|
+
transcript stays exactly as it was written.
|
|
432
|
+
- An edit is also an ordinary message, so it appears in the transcript: this
|
|
433
|
+
project puts state changes in the transcript as text precisely so a person can
|
|
434
|
+
see what happened, rather than having it land in a record nobody reads.
|
|
435
|
+
- A `goal_edit` with no `change` is a **valid message that simply is not an
|
|
436
|
+
edit** — the fold ignores it, and nothing about it is an error. Whether an edit
|
|
437
|
+
kind *should* carry its change is a rule about applying a message, not about the
|
|
438
|
+
message being well-formed, and an ill-formed line would make a room unreadable
|
|
439
|
+
forever (the log has no repair command). A `change` whose `type` does not match
|
|
440
|
+
its kind **is** ill-formed, and the writer refuses it.
|
|
441
|
+
- The body of an edit is prose (`"raise the pass mark to 85"`), not JSON: the
|
|
442
|
+
transcript is what a person reads, and the structure travels beside it in
|
|
443
|
+
`change`.
|
|
444
|
+
|
|
413
445
|
This format is this package's own design choice, not dictated by either JSON
|
|
414
446
|
Schema: the specification defers the on-disk representation to implementation,
|
|
415
447
|
and the schemas describe one message and one goal contract, not the log file as
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mrciphersmith/roomyx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "MCP server + terminal UI for startup-room: live room status, chat, per-agent modal, an installable room registry, and skill-sync.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
package/src/cli.ts
CHANGED
|
@@ -109,7 +109,7 @@ function bodyFromFile(file: string): string {
|
|
|
109
109
|
return raw.replace(/\r?\n$/, "");
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
const ENVELOPE_KEYS = ["from", "body", "kind", "in_reply_to"] as const;
|
|
112
|
+
const ENVELOPE_KEYS = ["from", "body", "kind", "in_reply_to", "change"] as const;
|
|
113
113
|
|
|
114
114
|
/**
|
|
115
115
|
* Parses one envelope, refusing keys that would silently vanish.
|
|
@@ -135,7 +135,7 @@ function envelopeFrom(text: string): AppendMessageOptions {
|
|
|
135
135
|
throw new ArgError(`--json has an unknown key "${key}". Allowed: ${ENVELOPE_KEYS.join(", ")}.`);
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
|
-
const { from, body, kind, in_reply_to: inReplyTo } = envelope;
|
|
138
|
+
const { from, body, kind, in_reply_to: inReplyTo, change } = envelope;
|
|
139
139
|
if (typeof from !== "string" || from === "") throw new ArgError('--json needs a non-empty "from".');
|
|
140
140
|
if (typeof body !== "string") throw new ArgError('--json needs a string "body".');
|
|
141
141
|
if (kind !== undefined && (typeof kind !== "string" || !(MESSAGE_KINDS as readonly string[]).includes(kind))) {
|
|
@@ -144,11 +144,20 @@ function envelopeFrom(text: string): AppendMessageOptions {
|
|
|
144
144
|
if (inReplyTo !== undefined && (typeof inReplyTo !== "number" || !Number.isInteger(inReplyTo) || inReplyTo < 1)) {
|
|
145
145
|
throw new ArgError('--json "in_reply_to" must be a whole number of at least 1.');
|
|
146
146
|
}
|
|
147
|
+
// The change is passed through unexamined here and validated where the rule
|
|
148
|
+
// lives (the log's own schema, imported by the writer). Re-checking its shape
|
|
149
|
+
// in the CLI would be a second copy of a rule the log already owns — and the
|
|
150
|
+
// two copies would drift, which is the failure `src/log/schema.ts` exists to
|
|
151
|
+
// make impossible.
|
|
152
|
+
if (change !== undefined && (typeof change !== "object" || change === null)) {
|
|
153
|
+
throw new ArgError('--json "change" must be an object.');
|
|
154
|
+
}
|
|
147
155
|
return {
|
|
148
156
|
from,
|
|
149
157
|
body,
|
|
150
158
|
kind: kind as MessageEnvelope["kind"],
|
|
151
159
|
inReplyTo: inReplyTo as number | undefined,
|
|
160
|
+
change: change as MessageEnvelope["change"],
|
|
152
161
|
};
|
|
153
162
|
}
|
|
154
163
|
|
package/src/log/schema.ts
CHANGED
|
@@ -25,8 +25,19 @@ export const MESSAGE_KINDS = [
|
|
|
25
25
|
"vote",
|
|
26
26
|
"status",
|
|
27
27
|
"research",
|
|
28
|
+
// D-19: the two kinds that change a room's state. The header cannot be
|
|
29
|
+
// rewritten and a second state line makes the room unreadable, so an edit is a
|
|
30
|
+
// message — and because it is a message it is also visible in the transcript,
|
|
31
|
+
// which is where this project already puts state changes.
|
|
32
|
+
"goal_edit",
|
|
33
|
+
"add_participant",
|
|
28
34
|
] as const;
|
|
29
35
|
|
|
36
|
+
/** The kinds that must carry a `change`, and which change each one means. */
|
|
37
|
+
export const EDIT_KINDS = { goal_edit: "goal_contract", add_participant: "roster" } as const;
|
|
38
|
+
|
|
39
|
+
export type EditKind = keyof typeof EDIT_KINDS;
|
|
40
|
+
|
|
30
41
|
export const rosterEntrySchema = z.object({ id: z.string(), name: z.string() });
|
|
31
42
|
|
|
32
43
|
export const goalContractSchema = z.object({
|
|
@@ -49,11 +60,68 @@ export const stateLineSchema = z.object({
|
|
|
49
60
|
roster: z.array(rosterEntrySchema),
|
|
50
61
|
});
|
|
51
62
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
63
|
+
/**
|
|
64
|
+
* What an edit message changes, as a discriminated payload.
|
|
65
|
+
*
|
|
66
|
+
* It travels in a field of its own rather than in `body` because the transcript
|
|
67
|
+
* is what a person reads: a JSON blob in the message pane is unreadable, and the
|
|
68
|
+
* body of an edit stays a sentence ("raise the pass mark to 85").
|
|
69
|
+
*/
|
|
70
|
+
export const messageChangeSchema = z.discriminatedUnion("type", [
|
|
71
|
+
z.object({ type: z.literal("goal_contract"), goal_contract: goalContractSchema }),
|
|
72
|
+
z.object({ type: z.literal("roster"), add: z.array(rosterEntrySchema).min(1) }),
|
|
73
|
+
]);
|
|
74
|
+
|
|
75
|
+
export type MessageChange = z.infer<typeof messageChangeSchema>;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The log's message contract, and the one place the edit rule is expressed.
|
|
79
|
+
*
|
|
80
|
+
* D-08 is why the rule lives here rather than in the writer and the reader
|
|
81
|
+
* separately: "a writer whose output its own reader rejects is the thing this
|
|
82
|
+
* module exists to make impossible". The writer refuses on this rule and the fold
|
|
83
|
+
* skips on it — two uses of one rule, not two rules.
|
|
84
|
+
*
|
|
85
|
+
* `change` is optional, which is what keeps the format change additive: every
|
|
86
|
+
* message written by every earlier version still validates.
|
|
87
|
+
*/
|
|
88
|
+
export const messageLineSchema = z
|
|
89
|
+
.object({
|
|
90
|
+
type: z.literal("message"),
|
|
91
|
+
seq: z.number().int().min(1),
|
|
92
|
+
from: z.string().min(1),
|
|
93
|
+
in_reply_to: z.number().int().min(1).optional(),
|
|
94
|
+
kind: messageKindSchema.optional(),
|
|
95
|
+
body: z.string(),
|
|
96
|
+
change: messageChangeSchema.optional(),
|
|
97
|
+
})
|
|
98
|
+
.superRefine((message, ctx) => {
|
|
99
|
+
// Shape rules only — deliberately not "an edit kind must carry a change".
|
|
100
|
+
//
|
|
101
|
+
// That rule would make the message *invalid*, and an invalid line makes a room
|
|
102
|
+
// unreadable forever: the log is append-only with no repair command. So a
|
|
103
|
+
// `goal_edit` with no change is a **valid message that simply is not an
|
|
104
|
+
// edit**, and the fold ignores it. Whether an edit kind carries its change is
|
|
105
|
+
// a rule about *applying* a message, not about the message being well-formed,
|
|
106
|
+
// and conflating the two would have invented a new way for a log to die.
|
|
107
|
+
if (message.change === undefined) return;
|
|
108
|
+
|
|
109
|
+
const expected = message.kind === undefined ? undefined : EDIT_KINDS[message.kind as EditKind];
|
|
110
|
+
if (expected === undefined) {
|
|
111
|
+
// A change on an ordinary message will never be applied by anything, so
|
|
112
|
+
// accepting it would let a writer believe it changed state it did not.
|
|
113
|
+
ctx.addIssue({
|
|
114
|
+
code: "custom",
|
|
115
|
+
path: ["change"],
|
|
116
|
+
message: `a "change" requires an edit kind (${Object.keys(EDIT_KINDS).join(", ")}), not "${message.kind ?? "none"}"`,
|
|
117
|
+
});
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (message.change.type !== expected) {
|
|
121
|
+
ctx.addIssue({
|
|
122
|
+
code: "custom",
|
|
123
|
+
path: ["change", "type"],
|
|
124
|
+
message: `kind "${message.kind}" carries a "${message.change.type}" change; it must be "${expected}"`,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
});
|
package/src/log/store.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
import { messageLineSchema, stateLineSchema } from "./schema";
|
|
3
|
-
import type {
|
|
3
|
+
import type { MessageChange } from "./schema";
|
|
4
|
+
import type { AgentDelta, AgentDetail, GoalContract, MessageEnvelope, RoomState, RosterEntry } from "./types";
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* `JSON.parse`, with the one thing it never says: where.
|
|
@@ -56,13 +57,6 @@ export function loadRoomLog(path: string): { state: RoomState; messages: Message
|
|
|
56
57
|
);
|
|
57
58
|
}
|
|
58
59
|
|
|
59
|
-
const state: RoomState = {
|
|
60
|
-
goal_contract: headerResult.data.goal_contract,
|
|
61
|
-
roster: headerResult.data.roster,
|
|
62
|
-
// The reader knows which file it read; the state header does not carry it.
|
|
63
|
-
log_path: path,
|
|
64
|
-
};
|
|
65
|
-
|
|
66
60
|
const messages: MessageEnvelope[] = rest.map((line, index) => {
|
|
67
61
|
const result = messageLineSchema.safeParse(
|
|
68
62
|
parseLine(line, () => `Invalid "message" line ${index + 2} in ${path}`),
|
|
@@ -75,14 +69,85 @@ export function loadRoomLog(path: string): { state: RoomState; messages: Message
|
|
|
75
69
|
return envelope;
|
|
76
70
|
});
|
|
77
71
|
|
|
72
|
+
const state: RoomState = {
|
|
73
|
+
// The header is the room's ORIGINAL contract and the base every edit is
|
|
74
|
+
// applied to; it is never reinterpreted as one of them.
|
|
75
|
+
...foldEdits({ goal_contract: headerResult.data.goal_contract, roster: headerResult.data.roster }, messages),
|
|
76
|
+
// The reader knows which file it read; the state header does not carry it.
|
|
77
|
+
log_path: path,
|
|
78
|
+
};
|
|
79
|
+
|
|
78
80
|
return { state, messages };
|
|
79
81
|
}
|
|
80
82
|
|
|
83
|
+
/**
|
|
84
|
+
* A room's current state, from its original state and every edit since.
|
|
85
|
+
*
|
|
86
|
+
* The reason this exists at all: a room's state lives in the first line and that
|
|
87
|
+
* line cannot be rewritten (append-only) and cannot be followed by a second one
|
|
88
|
+
* (`loadRoomLog` requires every following line to be a message, so a second state
|
|
89
|
+
* line makes the room unreadable forever). D-19 therefore makes an edit a
|
|
90
|
+
* *message* — which also puts it in the transcript, where this project already
|
|
91
|
+
* puts state changes so a person can see what happened.
|
|
92
|
+
*
|
|
93
|
+
* In `seq` order, last edit wins. A message whose kind is an edit kind but which
|
|
94
|
+
* carries no change is not an edit and is skipped: it is a valid message that
|
|
95
|
+
* happens to be tagged, and the schema deliberately does not make it invalid,
|
|
96
|
+
* because an invalid line would kill the room.
|
|
97
|
+
*
|
|
98
|
+
* Nothing here writes. The fold is an interpretation of an append-only log, and
|
|
99
|
+
* the header keeps the contract the room was created with.
|
|
100
|
+
*/
|
|
101
|
+
export function foldEdits(
|
|
102
|
+
base: { goal_contract: GoalContract; roster: RosterEntry[] },
|
|
103
|
+
messages: MessageEnvelope[],
|
|
104
|
+
): { goal_contract: GoalContract; roster: RosterEntry[] } {
|
|
105
|
+
let goalContract = base.goal_contract;
|
|
106
|
+
let roster = base.roster;
|
|
107
|
+
|
|
108
|
+
for (const message of [...messages].sort((a, b) => a.seq - b.seq)) {
|
|
109
|
+
const change: MessageChange | undefined = message.change;
|
|
110
|
+
if (change === undefined) continue;
|
|
111
|
+
if (change.type === "goal_contract") {
|
|
112
|
+
goalContract = {
|
|
113
|
+
...change.goal_contract,
|
|
114
|
+
// The schema has carried these since the beginning with nothing writing
|
|
115
|
+
// them; an edit is exactly what they were designed for.
|
|
116
|
+
updated_by: "owner",
|
|
117
|
+
updated_in_round: message.seq,
|
|
118
|
+
};
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
// "add", not "set": the roster grows, and replacing it would silently drop
|
|
122
|
+
// everyone an earlier message had introduced.
|
|
123
|
+
const known = new Set(roster.map((entry) => entry.id));
|
|
124
|
+
roster = [...roster, ...change.add.filter((entry) => !known.has(entry.id))];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return { goal_contract: goalContract, roster };
|
|
128
|
+
}
|
|
129
|
+
|
|
81
130
|
/** Messages with seq strictly greater than sinceSeq, in log order. */
|
|
82
131
|
export function getTranscript(messages: MessageEnvelope[], sinceSeq: number): MessageEnvelope[] {
|
|
83
132
|
return messages.filter((m) => m.seq > sinceSeq);
|
|
84
133
|
}
|
|
85
134
|
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The agent's own last `seq`, or 0 when it has never spoken.
|
|
138
|
+
*
|
|
139
|
+
* Extracted because two functions now need this rule and it is a *rule*, not an
|
|
140
|
+
* expression: "what counts as the agent's own last message" is the convention
|
|
141
|
+
* both `get_agent_detail` and `getAgentDelta` publish, and computing it twice
|
|
142
|
+
* means a change to it has to be made twice — which is how the two would come to
|
|
143
|
+
* disagree about what a participant has seen, silently, since both answers look
|
|
144
|
+
* plausible on their own.
|
|
145
|
+
*/
|
|
146
|
+
function lastOwnSeq(messages: MessageEnvelope[], agentId: string): number {
|
|
147
|
+
const own = messages.filter((m) => m.from === agentId);
|
|
148
|
+
return own.length > 0 ? Math.max(...own.map((m) => m.seq)) : 0;
|
|
149
|
+
}
|
|
150
|
+
|
|
86
151
|
/** A single participant's messages and last-seen seq, or an explicit not-found result. */
|
|
87
152
|
export function getAgentDetail(
|
|
88
153
|
messages: MessageEnvelope[],
|
|
@@ -95,6 +160,49 @@ export function getAgentDetail(
|
|
|
95
160
|
}
|
|
96
161
|
|
|
97
162
|
const own = messages.filter((m) => m.from === agentId);
|
|
98
|
-
|
|
99
|
-
|
|
163
|
+
return { found: true, agent, messages: own, lastSeenSeq: lastOwnSeq(messages, agentId) };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* The messages a participant has not seen, and the cursor that says so.
|
|
168
|
+
*
|
|
169
|
+
* This is the arithmetic the dispatcher re-derived in its own context on every
|
|
170
|
+
* turn — the phrase in the bundled skill is "everything since your last turn" —
|
|
171
|
+
* moved into the server that already knows how to compute it: `getAgentDetail`
|
|
172
|
+
* has always reported `lastSeenSeq` as the maximum of an agent's own `seq`.
|
|
173
|
+
*
|
|
174
|
+
* Two rules, and the second is the one that survives an explicit cursor:
|
|
175
|
+
*
|
|
176
|
+
* 1. The default cursor is that maximum, and the delta is `seq > cursor`.
|
|
177
|
+
* 2. Its own messages are excluded **at any cursor** — an agent does not need to
|
|
178
|
+
* be told what it said. With `sinceSeq: 0` this is the only rule doing work.
|
|
179
|
+
*
|
|
180
|
+
* Stateless on purpose. A cursor the server stored would belong to one server
|
|
181
|
+
* instance, and `serveMcpOverHttp` builds one per session (the lesson flow 005
|
|
182
|
+
* paid for); deriving it from the log on every call is what makes the answer
|
|
183
|
+
* shareable without any state at all.
|
|
184
|
+
*
|
|
185
|
+
* **What this is not:** a record of what was delivered. The convention is a
|
|
186
|
+
* good guess at what a participant has not seen, and a dispatcher may have
|
|
187
|
+
* failed to send a delta or sent one twice. `cursor_from` names which cursor was
|
|
188
|
+
* used so the guess is visible rather than implied.
|
|
189
|
+
*/
|
|
190
|
+
export function getAgentDelta(
|
|
191
|
+
messages: MessageEnvelope[],
|
|
192
|
+
roster: RosterEntry[],
|
|
193
|
+
agentId: string,
|
|
194
|
+
sinceSeq?: number,
|
|
195
|
+
): AgentDelta {
|
|
196
|
+
const agent = roster.find((r) => r.id === agentId);
|
|
197
|
+
if (!agent) return { found: false };
|
|
198
|
+
|
|
199
|
+
const since = sinceSeq ?? lastOwnSeq(messages, agentId);
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
found: true,
|
|
203
|
+
agent,
|
|
204
|
+
messages: messages.filter((m) => m.seq > since && m.from !== agentId),
|
|
205
|
+
since_seq: since,
|
|
206
|
+
cursor_from: sinceSeq === undefined ? "agent-last-message" : "caller",
|
|
207
|
+
};
|
|
100
208
|
}
|
package/src/log/types.ts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
|
+
import type { MessageChange } from "./schema";
|
|
2
|
+
|
|
1
3
|
export interface MessageEnvelope {
|
|
2
4
|
seq: number;
|
|
3
5
|
from: string;
|
|
4
6
|
in_reply_to?: number;
|
|
5
|
-
kind?:
|
|
7
|
+
kind?:
|
|
8
|
+
| "pitch"
|
|
9
|
+
| "question"
|
|
10
|
+
| "challenge"
|
|
11
|
+
| "answer"
|
|
12
|
+
| "vote"
|
|
13
|
+
| "status"
|
|
14
|
+
| "research"
|
|
15
|
+
| "goal_edit"
|
|
16
|
+
| "add_participant";
|
|
6
17
|
body: string;
|
|
18
|
+
/** Present only on an edit message; see `EDIT_KINDS` and D-19. */
|
|
19
|
+
change?: MessageChange;
|
|
7
20
|
}
|
|
8
21
|
|
|
9
22
|
export interface RosterEntry {
|
|
@@ -60,6 +73,27 @@ export interface RoomState {
|
|
|
60
73
|
dispatcherAttached?: boolean;
|
|
61
74
|
}
|
|
62
75
|
|
|
76
|
+
/**
|
|
77
|
+
* What a participant has not seen, according to the convention the dispatcher
|
|
78
|
+
* used to apply by hand: everything after that participant's own last message,
|
|
79
|
+
* excluding its own.
|
|
80
|
+
*
|
|
81
|
+
* `cursor_from` is not decoration. The server knows the convention; it does
|
|
82
|
+
* **not** know what was delivered to anyone — a dispatcher may have failed to
|
|
83
|
+
* send, or sent twice — and a caller must not have to guess which of the two
|
|
84
|
+
* produced the cursor it is looking at.
|
|
85
|
+
*/
|
|
86
|
+
export type AgentDelta =
|
|
87
|
+
| {
|
|
88
|
+
found: true;
|
|
89
|
+
agent: RosterEntry;
|
|
90
|
+
messages: MessageEnvelope[];
|
|
91
|
+
/** The cursor this delta was computed from. */
|
|
92
|
+
since_seq: number;
|
|
93
|
+
cursor_from: "agent-last-message" | "caller";
|
|
94
|
+
}
|
|
95
|
+
| { found: false };
|
|
96
|
+
|
|
63
97
|
export type AgentDetail =
|
|
64
98
|
| { found: true; agent: RosterEntry; messages: MessageEnvelope[]; lastSeenSeq: number }
|
|
65
99
|
| { found: false };
|
package/src/log/write.ts
CHANGED
|
@@ -20,6 +20,7 @@ function describeInvalid(error: z.ZodError): string {
|
|
|
20
20
|
const field = issue.path.join(".") || "message";
|
|
21
21
|
if (field === "kind") return `kind must be one of: ${LEGAL_KINDS}`;
|
|
22
22
|
if (field === "in_reply_to") return "in-reply-to must be a whole number of at least 1";
|
|
23
|
+
if (field === "change") return issue.message;
|
|
23
24
|
return `${field}: ${issue.message}`;
|
|
24
25
|
})
|
|
25
26
|
.join("; ");
|
|
@@ -47,6 +48,8 @@ export interface AppendMessageOptions {
|
|
|
47
48
|
body: string;
|
|
48
49
|
kind?: MessageEnvelope["kind"];
|
|
49
50
|
inReplyTo?: number;
|
|
51
|
+
/** Required by the edit kinds, refused on everything else — enforced by the schema. */
|
|
52
|
+
change?: MessageEnvelope["change"];
|
|
50
53
|
}
|
|
51
54
|
|
|
52
55
|
/** Refuses to overwrite: a room log is append-only, and clobbering one loses a session. */
|
|
@@ -111,20 +114,21 @@ export function appendMessages(
|
|
|
111
114
|
}
|
|
112
115
|
|
|
113
116
|
export function appendMessage(path: string, options: AppendMessageOptions): MessageEnvelope & { type: "message" } {
|
|
114
|
-
|
|
115
|
-
throw new Error(`${path} does not exist. Create it with \`roomyx room new\` first.`);
|
|
116
|
-
}
|
|
117
|
-
// Allocating `seq` and writing the line is one critical section, not two.
|
|
118
|
-
// `nextSeq` reads the whole file and returns max+1, and nothing stood between
|
|
119
|
-
// that and the append: three concurrent `roomyx room append` calls all
|
|
120
|
-
// allocated `seq: 1` and all three landed on disk. `seq` is the transcript
|
|
121
|
-
// cursor, so a client polling between two colliding writes advances past both
|
|
122
|
-
// and never receives the second message — on disk, invisible in the TUI,
|
|
123
|
-
// permanently.
|
|
117
|
+
// Delegates to the batch path rather than assembling the envelope again.
|
|
124
118
|
//
|
|
125
|
-
//
|
|
126
|
-
//
|
|
127
|
-
|
|
119
|
+
// It used to have its own inline construction and its own `writeMessage`, and
|
|
120
|
+
// the two copies had already drifted: this one silently dropped `change`, so an
|
|
121
|
+
// edit appended one message at a time was written without the thing that made
|
|
122
|
+
// it an edit — valid by the schema, accepted, and never applied. Fixing that
|
|
123
|
+
// instance by adding one spread would have left the class: two places building
|
|
124
|
+
// one envelope. There is now one implementation, and the only thing this
|
|
125
|
+
// function adds is the single-message shape of the answer.
|
|
126
|
+
const written = appendMessages(path, [options]);
|
|
127
|
+
const first = written[0];
|
|
128
|
+
if (first === undefined) {
|
|
129
|
+
throw new Error(`${path}: appending one message produced nothing. This is a bug: report it.`);
|
|
130
|
+
}
|
|
131
|
+
return first;
|
|
128
132
|
}
|
|
129
133
|
|
|
130
134
|
function buildMessage(seq: number, options: AppendMessageOptions): MessageEnvelope & { type: "message" } {
|
|
@@ -135,6 +139,7 @@ function buildMessage(seq: number, options: AppendMessageOptions): MessageEnvelo
|
|
|
135
139
|
body: options.body,
|
|
136
140
|
...(options.kind ? { kind: options.kind } : {}),
|
|
137
141
|
...(options.inReplyTo !== undefined ? { in_reply_to: options.inReplyTo } : {}),
|
|
142
|
+
...(options.change !== undefined ? { change: options.change } : {}),
|
|
138
143
|
};
|
|
139
144
|
}
|
|
140
145
|
|
|
@@ -143,25 +148,6 @@ function validate(message: MessageEnvelope & { type: "message" }): void {
|
|
|
143
148
|
if (!parsed.success) throw new LogWriteError(describeInvalid(parsed.error));
|
|
144
149
|
}
|
|
145
150
|
|
|
146
|
-
function writeMessage(path: string, options: AppendMessageOptions): MessageEnvelope & { type: "message" } {
|
|
147
|
-
const message: MessageEnvelope & { type: "message" } = {
|
|
148
|
-
type: "message",
|
|
149
|
-
seq: nextSeq(path),
|
|
150
|
-
from: options.from,
|
|
151
|
-
body: options.body,
|
|
152
|
-
...(options.kind ? { kind: options.kind } : {}),
|
|
153
|
-
...(options.inReplyTo !== undefined ? { in_reply_to: options.inReplyTo } : {}),
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
// Validated against the reader's own schema, before the write rather than
|
|
157
|
-
// after it. The log is append-only and roomyx ships no repair command, so a
|
|
158
|
-
// line the reader refuses is not a failed write — it is a room nobody can
|
|
159
|
-
// open again. Types alone did not hold this: they are erased at the CLI
|
|
160
|
-
// boundary, where `--kind` arrived as an unchecked string.
|
|
161
|
-
validate(message);
|
|
162
|
-
appendFileSync(path, JSON.stringify(message) + "\n");
|
|
163
|
-
return message;
|
|
164
|
-
}
|
|
165
151
|
|
|
166
152
|
/** `id:Name,id2:Name2` — the shape the CLI takes and the log stores. */
|
|
167
153
|
export function parseRoster(spec: string): RosterEntry[] {
|
package/src/server/index.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { getStateTool } from "./tools/get-state";
|
|
|
4
4
|
import { OWNER_COMMAND_KINDS, OwnerCommandQueue } from "./owner-queue";
|
|
5
5
|
import type { OwnerCommand, OwnerCommandEnvelope, OwnerCommandKind } from "./owner-queue";
|
|
6
6
|
import { getTranscriptTool } from "./tools/get-transcript";
|
|
7
|
+
import { getAgentDeltaTool } from "./tools/get-agent-delta";
|
|
7
8
|
import { getAgentDetailTool } from "./tools/get-agent-detail";
|
|
8
9
|
|
|
9
10
|
/**
|
|
@@ -138,6 +139,36 @@ export function createRoomMcpServer(logPath: string, options: RoomMcpServerOptio
|
|
|
138
139
|
}),
|
|
139
140
|
);
|
|
140
141
|
|
|
142
|
+
server.registerTool(
|
|
143
|
+
"room.get_delta_for",
|
|
144
|
+
{
|
|
145
|
+
title: "What one participant has not seen",
|
|
146
|
+
description:
|
|
147
|
+
"Everything after that participant's own last message, excluding its own — the convention " +
|
|
148
|
+
"\"everything since your last turn\", computed here instead of by hand. `since_seq` overrides " +
|
|
149
|
+
"the convention's cursor; the answer reports the cursor used and whether it came from the " +
|
|
150
|
+
"participant's last message or from the caller. " +
|
|
151
|
+
"This is what the participant has probably not seen: the server cannot see what was actually " +
|
|
152
|
+
"sent to it, so treat the result as a candidate delta, not as a delivery record.",
|
|
153
|
+
inputSchema: {
|
|
154
|
+
agent_id: z.string().min(1),
|
|
155
|
+
since_seq: z.number().int().min(0).optional(),
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
async ({ agent_id, since_seq }) => ({
|
|
159
|
+
content: [
|
|
160
|
+
{
|
|
161
|
+
type: "text",
|
|
162
|
+
text: JSON.stringify(
|
|
163
|
+
since_seq === undefined
|
|
164
|
+
? getAgentDeltaTool(logPath, agent_id)
|
|
165
|
+
: getAgentDeltaTool(logPath, agent_id, since_seq),
|
|
166
|
+
),
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
}),
|
|
170
|
+
);
|
|
171
|
+
|
|
141
172
|
server.registerTool(
|
|
142
173
|
"room.post_owner_command",
|
|
143
174
|
{
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { getAgentDelta, loadRoomLog } from "../../log/store";
|
|
2
|
+
import type { AgentDelta } from "../../log/types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* MCP tool `room.get_delta_for`: what a participant has not seen, by the
|
|
6
|
+
* convention of "everything after its own last message". Read-only.
|
|
7
|
+
*
|
|
8
|
+
* It returns the cursor it used and where that cursor came from, so an empty
|
|
9
|
+
* delta means "nothing new" and not "the caller passed the wrong cursor".
|
|
10
|
+
*/
|
|
11
|
+
export function getAgentDeltaTool(logPath: string, agentId: string, sinceSeq?: number): AgentDelta {
|
|
12
|
+
const { state, messages } = loadRoomLog(logPath);
|
|
13
|
+
return sinceSeq === undefined
|
|
14
|
+
? getAgentDelta(messages, state.roster, agentId)
|
|
15
|
+
: getAgentDelta(messages, state.roster, agentId, sinceSeq);
|
|
16
|
+
}
|