Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/server/src/auth/RpcAuthorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export const RPC_REQUIRED_SCOPES = {
[ORCHESTRATION_V2_WS_METHODS.subscribeThread]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.subscribeCommandOutput]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.subscribePromptSuggestion]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.reportTyping]: AuthOrchestrationOperateScope,
[ORCHESTRATION_V2_WS_METHODS.subscribeTyping]: AuthOrchestrationReadScope,
[WS_METHODS.projectsMutate]: AuthOrchestrationOperateScope,
[WS_METHODS.serverProbe]: AuthOrchestrationReadScope,
[WS_METHODS.serverGetConfig]: AuthOrchestrationReadScope,
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/observability/RpcInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const RPC_AGGREGATES = {
[ORCHESTRATION_V2_WS_METHODS.subscribeThread]: "orchestrationV2",
[ORCHESTRATION_V2_WS_METHODS.subscribeCommandOutput]: "orchestrationV2",
[ORCHESTRATION_V2_WS_METHODS.subscribePromptSuggestion]: "orchestrationV2",
[ORCHESTRATION_V2_WS_METHODS.reportTyping]: "orchestrationV2",
[ORCHESTRATION_V2_WS_METHODS.subscribeTyping]: "orchestrationV2",
[WS_METHODS.projectsMutate]: "orchestration",
[WS_METHODS.serverProbe]: "server",
[WS_METHODS.serverGetConfig]: "server",
Expand Down
186 changes: 186 additions & 0 deletions apps/server/src/orchestration-v2/HumanNote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import { assert, it } from "@effect/vitest";
import {
AuthSessionId,
CommandId,
MessageId,
ProjectId,
ProviderDriverKind,
ProviderInstanceId,
ThreadId,
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as TestClock from "effect/testing/TestClock";
import * as SqlitePersistence from "../persistence/Sqlite.ts";
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
import * as Orchestrator from "./Orchestrator.ts";
import * as ProjectionStore from "./ProjectionStore.ts";
import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";

const instanceId = ProviderInstanceId.make("codex");
const adapter = {
instanceId,
driver: ProviderDriverKind.make("codex"),
getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2),
planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" as const }),
openSession: () => Effect.die("A note must not start a provider session"),
} as ProviderAdapter.ProviderAdapterV2["Service"];
const layerDatabase = SqlitePersistence.layerMemory;
const layerTest = Layer.mergeAll(
layerDatabase,
ProjectionStore.layer.pipe(Layer.provide(layerDatabase)),
ProviderReplayHarness.layerWithRegistry(
{ name: "human-note" },
ProviderAdapterRegistry.layerFromAdapters([adapter]),
{ databaseLayer: layerDatabase, runEffectWorker: false },
),
);

const sentBy = (founder: "kameron" | "jeff") => ({
sessionId: AuthSessionId.make(`session:${founder}`),
device: "laptop",
deviceType: "desktop" as const,
founder,
});

const createThread = (threadId: ThreadId) =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
yield* orchestrator.dispatch({
type: "thread.create",
commandId: CommandId.make(`create:${threadId}`),
threadId,
projectId: ProjectId.make("project:human-note"),
title: "Before",
modelSelection: { instanceId, model: "gpt-5" },
runtimeMode: "approval-required",
interactionMode: "default",
branch: null,
worktreePath: null,
createdBy: "user",
creationSource: "web",
});
});

it.layer(layerTest)("human notes", (it) => {
it.effect("records a shared note, signs only that founder, and starts no run", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const projections = yield* ProjectionStore.ProjectionStoreV2;
const threadId = ThreadId.make("thread:human-note");
yield* createThread(threadId);
yield* orchestrator.dispatch({
type: "thread.settle",
commandId: CommandId.make("settle:human-note"),
threadId,
});
yield* orchestrator.dispatch({
type: "thread.metadata.update",
commandId: CommandId.make("share:human-note"),
threadId,
inboxRouting: {
owner: null,
assignee: null,
audience: "shared",
attentionKind: "FYI",
reason: "decision",
},
decisionRequest: {
founder: "kameron",
kind: "decision",
question: "Ship it?",
signOff: [
{ founder: "kameron", signed: false },
{ founder: "jeff", signed: false },
],
},
});
yield* TestClock.adjust("20 millis");
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make("note:kameron"),
threadId,
messageId: MessageId.make("message:note:kameron"),
text: " please look @Jeff ",
attachments: [],
humanNote: true,
dispatchMode: { type: "start_immediately" },
createdBy: "user",
creationSource: "web",
sentBy: sentBy("kameron"),
});
const projection = yield* projections.getThreadProjection(threadId);
assert.equal(projection.runs.length, 0);
assert.equal(projection.messages.length, 1);
assert.equal(projection.messages[0]?.text, "please look @Jeff");
assert.equal(projection.messages[0]?.humanNote, true);
assert.equal(projection.messages[0]?.runId, null);
const note = projection.turnItems.find((item) => item.type === "user_message");
assert.equal(note?.type === "user_message" ? note.humanNote : undefined, true);
assert.equal(note?.status, "completed");
assert.deepEqual(yield* projections.getTurnStartHistory(threadId), []);
const shell = yield* projections.getThreadShell(threadId);
assert.equal(shell?.settledOverride, null);
assert.deepEqual(shell?.attentionMentions, ["jeff"]);
assert.deepEqual(shell?.inbox?.decisionRequest?.signOff, [
{ founder: "kameron", signed: true },
{ founder: "jeff", signed: false },
]);

yield* TestClock.adjust("20 millis");
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make("note:jeff"),
threadId,
messageId: MessageId.make("message:note:jeff"),
text: "signed",
attachments: [],
humanNote: true,
dispatchMode: { type: "start_immediately" },
createdBy: "user",
creationSource: "web",
sentBy: sentBy("jeff"),
});
const closed = yield* projections.getThreadShell(threadId);
assert.equal(closed?.inbox?.decisionRequest, undefined);
assert.equal((yield* projections.getThreadProjection(threadId)).runs.length, 0);
assert.deepEqual(yield* projections.getTurnStartHistory(threadId), []);
}),
);

it.effect("rejects a note off a shared thread without waking it", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const projections = yield* ProjectionStore.ProjectionStoreV2;
const threadId = ThreadId.make("thread:human-note-private");
yield* createThread(threadId);
yield* orchestrator.dispatch({
type: "thread.settle",
commandId: CommandId.make("settle:private"),
threadId,
});
const refused = yield* orchestrator
.dispatch({
type: "message.dispatch",
commandId: CommandId.make("note:private"),
threadId,
messageId: MessageId.make("message:note:private"),
text: "just us",
attachments: [],
humanNote: true,
dispatchMode: { type: "start_immediately" },
createdBy: "user",
creationSource: "web",
sentBy: sentBy("kameron"),
})
.pipe(Effect.flip);
assert.equal(refused._tag, "OrchestratorDispatchError");
const shell = yield* projections.getThreadShell(threadId);
assert.equal(shell?.title, "Before");
assert.equal(shell?.settledOverride, "settled");
assert.equal((yield* projections.getThreadProjection(threadId)).messages.length, 0);
}),
);
});
74 changes: 74 additions & 0 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6997,6 +6997,21 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
}
: undefined,
);
if (command.humanNote === true) {
const noteText = command.text.trim();
if (
command.createdBy !== "user" ||
projection.thread.inbox?.audience !== "shared" ||
noteText.length === 0 ||
command.attachments.length > 0
) {
return yield* new OrchestratorDispatchError({
commandId: command.commandId,
commandType: command.type,
cause: "A note is a text message on a shared thread, and it does not wake the bot.",
});
}
}
if (command.steerFollowUpOfRunId !== undefined) {
const source = projection.runs.find((run) => run.id === command.steerFollowUpOfRunId);
if (
Expand Down Expand Up @@ -7257,6 +7272,65 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
});
projection = applyToProjection(projection, event);
}
if (command.humanNote === true) {
const now = yield* DateTime.now;
const text = command.text.trim();
const message: OrchestrationV2ConversationMessage = {
createdBy: command.createdBy,
creationSource: command.creationSource,
...(command.sentBy === undefined ? {} : { sentBy: command.sentBy }),
id: command.messageId,
threadId: command.threadId,
runId: null,
nodeId: null,
role: "user",
text,
attachments: [],
streaming: false,
humanNote: true,
createdAt: now,
updatedAt: now,
};
const noteItem: OrchestrationV2TurnItem = {
createdBy: command.createdBy,
creationSource: command.creationSource,
...(command.sentBy === undefined ? {} : { sentBy: command.sentBy }),
id: idAllocator.derive.userTurnItem({ messageId: command.messageId }),
threadId: command.threadId,
runId: null,
nodeId: null,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: yield* nextTurnItemOrdinal(projection),
status: "completed",
title: null,
startedAt: now,
completedAt: now,
updatedAt: now,
type: "user_message",
messageId: command.messageId,
inputIntent: "turn_start",
text,
attachments: [],
humanNote: true,
};
const emitEvent = emit(events, command);
yield* emitEvent({
type: "message.updated",
threadId: command.threadId,
occurredAt: now,
payload: message,
});
yield* emitEvent({
type: "turn-item.updated",
threadId: command.threadId,
occurredAt: now,
payload: noteItem,
});
return;
}
const userMessages = projection.messages.filter((message) => message.role === "user");
// The recent runs of a compacted thread can hold only a `/compact` while an older prompt
// exists. Every prompt's turn item is loaded; without one, every user message is checked.
Expand Down
Loading
Loading