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
8 changes: 6 additions & 2 deletions apps/server/src/kin/chat/KinChatService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { JudgeShadow } from "../floor/JudgeShadow.ts";
import * as Floor from "../floor/KinFloor.ts";
import { ServerEnvironment } from "../../environment/ServerEnvironment.ts";
import { OrchestratorV2 } from "../../orchestration-v2/Orchestrator.ts";
Expand Down Expand Up @@ -195,6 +196,7 @@ const requireRecord = Effect.fn("KinChatService.requireRecord")(function* <A, E,

const make = Effect.gen(function* () {
const floor = yield* Floor.KinFloor;
const shadow = yield* Effect.serviceOption(JudgeShadow);
const cards = yield* TaskCard.TaskCardIndex;
const repo = yield* Repository.KinChatRepository;
const hub = yield* Hub.KinChatHub;
Expand Down Expand Up @@ -365,8 +367,10 @@ const make = Effect.gen(function* () {
editedAt: null,
});
if (message.messageId !== messageId) return message;
if (message.author._tag === "human")
yield* floor.open(message, yield* requireRecord(repo.getChat(member)));
if (message.author._tag === "human") {
const decision = yield* floor.open(message, yield* requireRecord(repo.getChat(member)));
if (Option.isSome(shadow)) yield* shadow.value.observe(message, decision);
}
if (input.publish === false) return message;
yield* hub.publish({ type: "message.posted", orgId: identity.orgId, message });
if (message.author._tag === "agent" && message.intent === "question") {
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/kin/floor/FloorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface FloorBid {
readonly intent: KinAgentIntent;
readonly at: number;
}
type FloorDecision =
export type FloorDecision =
| { readonly _tag: "grant"; readonly agentId: KinAgentId }
| { readonly _tag: "wait" }
| { readonly _tag: "close"; readonly reason: "turn_budget" | "no_bids" | "nothing_to_add" };
Expand Down
42 changes: 42 additions & 0 deletions apps/server/src/kin/floor/Judge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { KinOrgId, KinChatId, KinMessageId, KinRole, KinAgentRole } from "@t3tools/contracts";
import * as Context from "effect/Context";
import type * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";

export interface JudgeInput {
readonly orgId: KinOrgId;
readonly chatId: KinChatId;
readonly messageId: KinMessageId;
readonly text: string;
readonly authorRole: KinRole;
readonly recent: ReadonlyArray<{
readonly authorKind: "human" | "agent";
readonly role: string;
readonly text: string;
}>;
readonly agentRoles: ReadonlyArray<KinAgentRole>;
}
export interface Scored<A> {
readonly value: A;
readonly confidence: number;
}
export interface JudgeVerdict {
readonly needsResponse: Scored<boolean>;
readonly role: Scored<KinAgentRole | null>;
readonly urgency: Scored<"low" | "normal" | "high">;
readonly needsHumanDecision: Scored<boolean>;
readonly commitment: Scored<{
readonly kind: "none" | "defer" | "promise";
readonly text: string | null;
}>;
}
export class JudgeError extends Schema.TaggedError<JudgeError>()("JudgeError", {}) {}
export class Judge extends Context.Service<
Judge,
{
readonly name: string;
readonly judge: (input: JudgeInput) => Effect.Effect<JudgeVerdict, JudgeError>;
}
>()("t3/kin/floor/Judge") {}
export const JUDGE_MODE: "off" | "shadow" | "live" = "shadow";
export const JUDGE_THRESHOLDS = { act: 0.8, ask: 0.5 } as const;
64 changes: 64 additions & 0 deletions apps/server/src/kin/floor/JudgeBoundary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as NodeURL from "node:url";
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";

const kin = NodeURL.fileURLToPath(new URL("../", import.meta.url));

it.effect(
"approvals and floor enforcement cannot depend on the judge, even through a re-export",
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const files = (yield* fs.readDirectory(kin, { recursive: true })).filter(
(file) =>
file.endsWith(".ts") && !file.endsWith(".test.ts") && !file.endsWith(".testkit.ts"),
);
const graph = new Map<string, ReadonlyArray<string>>();
for (const file of files) {
const filename = path.resolve(kin, file);
const source = yield* fs.readFileString(filename);
// Kin uses relative, literal ES imports; follow re-exports and dynamic imports too.
const imports = [
...source.matchAll(
/(?:\b(?:import|export)\s+(?:type\s+)?(?:[^;"']*?\s+from\s*)?|\bimport\s*\(\s*)["'](\.[^"']+)["']/g,
),
];
graph.set(
filename,
imports.map((match) => path.resolve(path.dirname(filename), match[1]!)),
);
}
const reachable = (entry: string) => {
const seen = new Set<string>();
const visit = (filename: string) => {
if (seen.has(filename)) return;
seen.add(filename);
for (const dependency of graph.get(filename) ?? []) visit(dependency);
};
visit(path.resolve(kin, entry));
return [...seen].map((filename) => path.relative(kin, filename));
};
const roots = [
"floor/FloorManager.ts",
"floor/KinFloor.ts",
...files.filter((file) => file.startsWith("approvals/")),
];
for (const root of roots) {
assert.isFalse(reachable(root).includes("floor/Judge.ts"), root);
assert.isFalse(reachable(root).includes("floor/JudgeShadow.ts"), root);
}
assert.include(reachable("floor/KinFloor.ts"), "floor/FloorManager.ts");
assert.isFalse(
reachable("floor/Judge.ts").some(
(file) =>
file.startsWith("approvals/") ||
file === "chat/KinChatService.ts" ||
file === "floor/KinFloor.ts",
),
);
}).pipe(Effect.provide(NodeServices.layer)),
);
Loading