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
36 changes: 33 additions & 3 deletions apps/server/src/kin/chat/KinChatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ export class KinChatService extends Context.Service<
readonly title: string;
readonly model: string | null;
}) => Effect.Effect<KinWidget, ServiceError, OrchestratorV2 | ServerEnvironment>;
/** Server-only, like createTaskCard: Kin's worker launcher closes and reopens worker cards. */
readonly setTaskCardClosed: (input: {
readonly orgId: KinOrgId;
readonly chatId: KinChatId;
readonly threadId: ThreadId;
readonly closed: boolean;
}) => Effect.Effect<KinWidget, ServiceError>;
readonly act: (
identity: PersonCaller,
input: KinWidgetsActInput,
Expand Down Expand Up @@ -197,6 +204,12 @@ const requireRecord = Effect.fn("KinChatService.requireRecord")(function* <A, E,
return Option.isSome(value) ? value.value : yield* notFound;
});

const taskCardWidgetId = (input: { readonly chatId: KinChatId; readonly threadId: ThreadId }) =>
encodeAction([input.chatId, input.threadId]).pipe(
Effect.orDie,
Effect.map((key) => KinWidgetId.make(`task:${key}`)),
);

const make = Effect.gen(function* () {
const floor = yield* Floor.KinFloor;
const shadow = yield* JudgeShadow;
Expand Down Expand Up @@ -858,9 +871,7 @@ const make = Effect.gen(function* () {
(thread) => thread.orgId === input.orgId && thread.agentId === agent.agentId,
);
if (member === undefined || thread === undefined) return yield* notFound;
const widgetId = KinWidgetId.make(
`task:${yield* encodeAction([input.chatId, input.threadId]).pipe(Effect.orDie)}`,
);
const widgetId = yield* taskCardWidgetId(input);
const clientId = yield* encodeAction([
input.threadId,
input.authorAgentId,
Expand Down Expand Up @@ -942,6 +953,25 @@ const make = Effect.gen(function* () {
yield* sql.afterCommit(cards.track(widget));
return widget;
}, transaction),
setTaskCardClosed: Effect.fn("KinChatService.setTaskCardClosed")(function* (input) {
const existing = yield* requireRecord(
repo.getWidget({ orgId: input.orgId, widgetId: yield* taskCardWidgetId(input) }),
);
if (existing.type !== "task" || existing.chatId !== input.chatId) return yield* notFound;
if ((existing.closedAt !== null) === input.closed) return existing;
const widget = yield* repo.updateWidget({
...existing,
awaiting: input.closed ? [] : existing.awaiting,
expectedRevision: existing.revision,
closedAt: input.closed ? yield* now : null,
updatedAt: yield* now,
});
if (input.closed)
for (const item of yield* repo.waitingForWidget(widget)) yield* resolve(item, "closed");
yield* sql.afterCommit(cards.track(widget));
yield* hub.publish({ type: "widget.updated", orgId: input.orgId, widget });
return widget;
}, transaction),
upsertWidget: Effect.fn("KinChatService.upsertWidget")(function* (identity, input) {
const member = yield* memberFor(identity, input.chatId);
yield* canWrite(identity, member);
Expand Down
5 changes: 4 additions & 1 deletion apps/server/src/kin/lead/KinBudget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ describe("KinBudget", () => {
assert.deepStrictEqual(yield* budget.charge({ orgId, wakeupId, usage: [] }), full);
}
}
assert.strictEqual((yield* budget.get(orgId)).leadUnits, 411);
const spent = yield* budget.get(orgId);
assert.strictEqual(spent.leadUnits, 411);
// Lead turns' otherUsage (Claude subagents) is lead spend, never worker units.
assert.strictEqual(spent.workerUnits, 0);
}).pipe(Effect.provide(layer)),
);
it.effect("requires the same-org owner and validates limits before writing", () =>
Expand Down
156 changes: 102 additions & 54 deletions apps/server/src/kin/lead/KinBudget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
KinMessageId,
type KinOrgId,
type KinWakeupId,
type KinWorkerId,
type RunId,
type TurnTokenUsage,
type OrchestrationV2ProviderTurnTokenUsage,
} from "@t3tools/contracts";
Expand All @@ -25,7 +27,7 @@ import * as MemoryCache from "../memory/MemoryCache.ts";
import * as Queue from "./KinEventQueue.ts";

type Priority = "P0" | "P1" | "P2" | "P3" | "P4";
type Usage = {
export type Usage = {
readonly providerTurnId: string;
readonly turnTokenUsage?: TurnTokenUsage;
readonly tokenUsage?: OrchestrationV2ProviderTurnTokenUsage;
Expand Down Expand Up @@ -94,6 +96,13 @@ export class KinBudget extends Context.Service<
readonly wakeupId: KinWakeupId;
readonly usage: ReadonlyArray<Usage>;
}) => Effect.Effect<Charge, BudgetError>;
/** Worker runs bill worker units only; lead turns, including Claude's otherUsage, stay lead units. */
readonly chargeWorker: (input: {
readonly orgId: KinOrgId;
readonly workerId: KinWorkerId;
readonly runId: RunId;
readonly usage: ReadonlyArray<Usage>;
}) => Effect.Effect<{ readonly day: string; readonly units: number }, BudgetError>;
}
>()("t3/kin/lead/KinBudget") {}

Expand Down Expand Up @@ -177,6 +186,61 @@ const make = Effect.gen(function* () {
});
yield* hub.publish({ type: "message.posted", orgId, message });
});
const measure = Effect.fn("KinBudget.measure")(function* (
usage: ReadonlyArray<Usage>,
context: Readonly<Record<string, string>>,
) {
let units = 0;
const valid = (value: number | undefined) =>
value !== undefined && Number.isFinite(value) && value >= 0;
const count = (value: number | undefined) => (valid(value) ? value! : 0);
const ids = new Set<string>();
if (usage.length === 0)
yield* Effect.logWarning("Kin budget turn usage unavailable", {
...context,
});
for (const turn of usage) {
if (ids.has(turn.providerTurnId)) continue;
ids.add(turn.providerTurnId);
const ledger = turn.turnTokenUsage;
const complete =
ledger?.usageStatus === "complete" &&
valid(ledger.inputTokens) &&
valid(ledger.outputTokens) &&
(ledger.cachedInputTokens === undefined || valid(ledger.cachedInputTokens));
if (!complete) {
yield* Effect.logWarning("Kin budget using fallback usage", {
...context,
usageStatus: ledger?.usageStatus ?? "missing",
hasUsedTokens: valid(turn.usedTokens),
});
}
if (!complete && valid(turn.usedTokens)) {
units += count(turn.usedTokens);
continue;
}
const incoming = count(ledger?.inputTokens);
const cached = Math.min(incoming, count(ledger?.cachedInputTokens));
if (cached !== count(ledger?.cachedInputTokens))
yield* Effect.logWarning("Kin budget clamped cached usage", {
...context,
});
units +=
incoming -
cached +
0.1 * cached +
4 * count(ledger?.outputTokens) +
count(ledger?.otherUsage?.inputTokens) +
4 * count(ledger?.otherUsage?.outputTokens);
}
if (!Number.isFinite(units)) {
yield* Effect.logWarning("Kin budget usage overflow", {
...context,
});
units = 0;
}
return units;
});
return KinBudget.of({
get,
view: Effect.fn("KinBudget.view")(function* (people, orgId) {
Expand Down Expand Up @@ -240,59 +304,7 @@ const make = Effect.gen(function* () {
return `${Math.floor(share(yield* get(orgId)) * 100)}% of today left`;
}),
charge: Effect.fn("KinBudget.charge")(function* (input) {
let units = 0;
const valid = (value: number | undefined) =>
value !== undefined && Number.isFinite(value) && value >= 0;
const count = (value: number | undefined) => (valid(value) ? value! : 0);
const ids = new Set<string>();
if (input.usage.length === 0)
yield* Effect.logWarning("Kin budget turn usage unavailable", {
orgId: input.orgId,
wakeupId: input.wakeupId,
});
for (const turn of input.usage) {
if (ids.has(turn.providerTurnId)) continue;
ids.add(turn.providerTurnId);
const ledger = turn.turnTokenUsage;
const complete =
ledger?.usageStatus === "complete" &&
valid(ledger.inputTokens) &&
valid(ledger.outputTokens) &&
(ledger.cachedInputTokens === undefined || valid(ledger.cachedInputTokens));
if (!complete) {
yield* Effect.logWarning("Kin budget using fallback usage", {
orgId: input.orgId,
wakeupId: input.wakeupId,
usageStatus: ledger?.usageStatus ?? "missing",
hasUsedTokens: valid(turn.usedTokens),
});
}
if (!complete && valid(turn.usedTokens)) {
units += count(turn.usedTokens);
continue;
}
const incoming = count(ledger?.inputTokens);
const cached = Math.min(incoming, count(ledger?.cachedInputTokens));
if (cached !== count(ledger?.cachedInputTokens))
yield* Effect.logWarning("Kin budget clamped cached usage", {
orgId: input.orgId,
wakeupId: input.wakeupId,
});
units +=
incoming -
cached +
0.1 * cached +
4 * count(ledger?.outputTokens) +
count(ledger?.otherUsage?.inputTokens) +
4 * count(ledger?.otherUsage?.outputTokens);
}
if (!Number.isFinite(units)) {
yield* Effect.logWarning("Kin budget usage overflow", {
orgId: input.orgId,
wakeupId: input.wakeupId,
});
units = 0;
}
const units = yield* measure(input.usage, { orgId: input.orgId, wakeupId: input.wakeupId });
const tokensJson = yield* encodeJson(usageJsonValue(input.usage));
const { day } = yield* period(input.orgId);
return yield* sql.withTransaction(
Expand Down Expand Up @@ -324,6 +336,42 @@ const make = Effect.gen(function* () {
}),
);
}, Effect.mapError(mapError)),
chargeWorker: Effect.fn("KinBudget.chargeWorker")(function* (input) {
const units = yield* measure(input.usage, {
orgId: input.orgId,
workerId: input.workerId,
runId: input.runId,
});
const tokensJson = yield* encodeJson(usageJsonValue(input.usage));
const { day } = yield* period(input.orgId);
return yield* sql.withTransaction(
Effect.gen(function* () {
const worker =
yield* sql`SELECT 1 FROM kin_workers WHERE org_id = ${input.orgId} AND worker_id = ${input.workerId} ORDER BY worker_id`;
if (worker.length === 0) return yield* notFound;
const [stored] = yield* sql<{
day: string;
units: number;
tokensJson: string;
}>`SELECT day, units, tokens_json AS "tokensJson" FROM kin_worker_charges WHERE org_id = ${input.orgId} AND run_id = ${input.runId} ORDER BY run_id`;
if (stored !== undefined && (stored.tokensJson === tokensJson || units <= stored.units))
return { day: stored.day, units: stored.units };
const delta = Math.max(0, units - (stored?.units ?? 0));
yield* ensure(input.orgId, day);
const before = yield* snapshot(input.orgId, day);
if (!Number.isFinite(before.workerUnits + delta)) return yield* invalid;
const inserted =
yield* sql`INSERT INTO kin_worker_charges (org_id, worker_id, run_id, day, units, tokens_json)
VALUES (${input.orgId}, ${input.workerId}, ${input.runId}, ${day}, ${units}, ${tokensJson})
ON CONFLICT DO NOTHING RETURNING run_id`;
if (inserted.length === 0)
yield* sql`UPDATE kin_worker_charges SET units = ${units}, tokens_json = ${tokensJson}
WHERE org_id = ${input.orgId} AND run_id = ${input.runId}`;
yield* sql`UPDATE kin_budget SET worker_units = worker_units + ${delta} WHERE org_id = ${input.orgId} AND day = ${day}`;
return { day: stored?.day ?? day, units };
}),
);
}, Effect.mapError(mapError)),
});
});

Expand Down
14 changes: 13 additions & 1 deletion apps/server/src/kin/lead/LeadMemory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import * as Writer from "../memory/MemoryWriter.ts";
import * as Scan from "../memory/secretScan.ts";
import { fixtureItem } from "../memory/__fixtures__/items.ts";
import * as Lead from "./LeadMemory.ts";
import { KinWorkers } from "./Workers.ts";
import * as Registry from "./KinLeadRegistry.ts";
import { KinLeadToolkit } from "./mcp/tools.ts";
import { handlers } from "./mcp/handlers.ts";
Expand Down Expand Up @@ -68,6 +69,7 @@ const client = McpSchema.McpServerClient.of({
});
const registration = McpHttpServer.toolkitRegistration(KinLeadToolkit, handlers).pipe(
Layer.provideMerge(McpHttpServer.layerInProcessMcpServer),
Layer.provide(Layer.mock(KinWorkers)({})),
Layer.provide(
Layer.mock(Threads.ThreadManagementService)({
getThreadShell: (id) => Effect.succeed({ ...liveThreadShell(id), providerInstanceId }),
Expand Down Expand Up @@ -266,7 +268,17 @@ it.live(
);
assert.sameMembers(
yield* visible.pipe(Effect.provideService(McpInvocationContext, scope)),
["memory_search", "memory_read", "memory_write", "kin_defer", "kin_journal", "kin_end"],
[
"kin_delegate",
"kin_follow_up",
"kin_worker_stop",
"memory_search",
"memory_read",
"memory_write",
"kin_defer",
"kin_journal",
"kin_end",
],
);
for (const thread of [
undefined,
Expand Down
43 changes: 26 additions & 17 deletions apps/server/src/kin/lead/LeadMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,27 @@ export const authority = Layer.effect(
}),
);

/** Kin-tool caller: a registered lead thread in its running wake-up, never another agent. */
export const resolveLead = Effect.fn("LeadMemory.resolveLead")(function* (
scope: McpInvocationScope,
allowEnded = false,
) {
const identity = yield* KinIdentity;
const sql = yield* SqlClient.SqlClient;
const agent = yield* identity.resolveAgent(scope);
const [row] = yield* sql<{
wakeupId: string;
startedAt: number;
}>`SELECT w.wakeup_id AS "wakeupId", w.started_at AS "startedAt" FROM kin_wakeups w JOIN kin_leads l ON l.org_id = w.org_id AND l.agent_id = w.agent_id JOIN kin_agents a ON a.org_id = l.org_id AND a.agent_id = l.agent_id WHERE w.org_id = ${agent.orgId} AND w.agent_id = ${agent.agentId} AND w.thread_id = ${agent.threadId} AND l.thread_id = w.thread_id AND a.role = 'lead' AND w.status = 'running'`;
if (!row) return yield* denied();
if (!allowEnded) {
const ended =
yield* sql`SELECT 1 FROM kin_lead_tool_calls WHERE org_id = ${agent.orgId} AND wakeup_id = ${row.wakeupId} AND call_key = 'end'`;
if (ended.length > 0) return yield* denied();
}
return { ...agent, ...row };
});

const make = Effect.gen(function* () {
const digest = yield* Digest.Digest;
const hash = Effect.fn("LeadMemory.hash")((value: unknown) =>
Expand All @@ -235,23 +256,11 @@ const make = Effect.gen(function* () {
return yield* lock.withPermits(1)(operation);
});
const ended = new Map<KinOrgId, string>();
const resolve = Effect.fn("LeadMemory.resolve")(function* (
scope: McpInvocationScope,
allowEnded = false,
) {
const agent = yield* identity.resolveAgent(scope);
const [row] = yield* sql<{
wakeupId: string;
startedAt: number;
}>`SELECT w.wakeup_id AS "wakeupId", w.started_at AS "startedAt" FROM kin_wakeups w JOIN kin_leads l ON l.org_id = w.org_id AND l.agent_id = w.agent_id JOIN kin_agents a ON a.org_id = l.org_id AND a.agent_id = l.agent_id WHERE w.org_id = ${agent.orgId} AND w.agent_id = ${agent.agentId} AND w.thread_id = ${agent.threadId} AND l.thread_id = w.thread_id AND a.role = 'lead' AND w.status = 'running'`;
if (!row) return yield* denied();
if (!allowEnded) {
const ended =
yield* sql`SELECT 1 FROM kin_lead_tool_calls WHERE org_id = ${agent.orgId} AND wakeup_id = ${row.wakeupId} AND call_key = 'end'`;
if (ended.length > 0) return yield* denied();
}
return { ...agent, ...row };
});
const resolve = (scope: McpInvocationScope, allowEnded = false) =>
resolveLead(scope, allowEnded).pipe(
Effect.provideService(KinIdentity, identity),
Effect.provideService(SqlClient.SqlClient, sql),
);
const checkSource = Effect.fn("LeadMemory.checkSource")(function* (
ctx: Effect.Success<ReturnType<typeof resolve>>,
eventId: string,
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/kin/lead/LeadPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function encoded(value: unknown, capText = false): string {
);
}

function quote(label: string, value: unknown, capText = false): string {
export function quote(label: string, value: unknown, capText = false): string {
return `> BEGIN QUOTE: ${label}\n> ${encoded(value, capText)}\n> END QUOTE`;
}

Expand Down
Loading