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
15 changes: 14 additions & 1 deletion apps/server/src/kin/chat/KinChatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import * as SqlError from "effect/sql/SqlError";
import * as SecureRandom from "effect/crypto/SecureRandom";
import { toPersistenceSqlError } from "../../persistence/Errors.ts";
import { validateState } from "../widgets/registry.ts";
import { deferralCardActions, deferralCardActionsReady } from "../widgets/deferralCard.ts";
import * as Hub from "./KinChatHub.ts";
import * as Repository from "./KinChatRepository.ts";
import type { AgentIdentity, PersonIdentity } from "./KinIdentity.ts";
Expand Down Expand Up @@ -949,7 +950,11 @@ const make = Effect.gen(function* () {
reason: "use_approvals",
message: "Use the signed approvals service.",
});
if (input.type === "owner_check" || (input.type === "task" && input.widgetId === undefined))
if (
input.type === "owner_check" ||
input.type === "deferral" ||
(input.type === "task" && input.widgetId === undefined)
)
return yield* notAllowed;
let state = yield* validateState(input.type, input.state);
let widget: KinWidget;
Expand Down Expand Up @@ -1088,6 +1093,14 @@ const make = Effect.gen(function* () {
)
return yield* invalidState;
}
if (widget.type === "deferral") {
if (member.role !== "owner") return yield* notAllowed;
if (
!deferralCardActionsReady ||
!deferralCardActions.some((action) => action === request.actionId)
)
return yield* notAllowed;
}
const body = yield* encodeAction({
widgetId: request.widgetId,
revision: request.revision,
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/kin/lead/LeadMemory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ it.live(
.pipe(Effect.flip)).reason,
"not_found",
);
for (const due of ["bad", "2026-02-30", "2026-11-13T09:00:00"])
for (const due of ["bad", "2026-02-30", "2026-11-13T09:00:00", "1969-12-31", "0000-01-01"])
assert.strictEqual(
(yield* decodeDefer({ ...deferred, due }).pipe(Effect.result))._tag,
"Failure",
Expand Down
5 changes: 4 additions & 1 deletion apps/server/src/kin/lead/LeadMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const date = Schema.String.check(
expected: "a date or timestamp with explicit offset",
}),
);
const deferDue = date.check(
Schema.makeFilter((value) => /^20\d\d-/.test(value), { expected: "a date in 2000-2099" }),
);
const itemId = KinMemoryItemId.check(Schema.isPattern(/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,199}$/));
export const SearchInput = Schema.Struct({
kind: Schema.optional(MemoryItemFrontMatter.fields.kind),
Expand Down Expand Up @@ -65,7 +68,7 @@ export const WriteInput = Schema.Struct({
export const DeferInput = Schema.Struct({
itemId: Schema.optional(itemId),
what: TrimmedNonEmptyString.check(Schema.isMaxLength(8000)),
due: date,
due: deferDue,
onDue: Schema.Literals(["remind", "start", "ask", "chase"]),
chatId: KinChatId,
sourceEventId: TrimmedNonEmptyString,
Expand Down
7 changes: 7 additions & 0 deletions apps/server/src/kin/lead/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as OwnerCheck from "../triggers/ownerCheck.ts";
import * as TriggerScheduler from "../triggers/KinTriggerScheduler.ts";
import * as TriggerStore from "../triggers/KinTriggerStore.ts";
import * as TriggerInbox from "../triggers/KinTriggerInbox.ts";
import * as TriggerService from "../triggers/KinTriggerService.ts";
import { forkParked } from "../../serverActivation.ts";
import { KinOrgId, KinChatId, KinMessageId } from "@t3tools/contracts";
import * as SecureRandom from "effect/crypto/SecureRandom";
Expand Down Expand Up @@ -126,6 +127,12 @@ const withServices = <A, E, R>(entry: Layer.Layer<A, E, R>) =>
}),
).pipe(Layer.provide(KinChatRepository.layer)),
),
Layer.provide(
TriggerService.syncHooksLayer.pipe(
Layer.provide(TriggerService.layer),
Layer.provide(KinChatRepository.layer),
),
),
Layer.provide(KinPersistence.layer),
);

Expand Down
34 changes: 29 additions & 5 deletions apps/server/src/kin/memory/Memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ export class MemoryRecoveryHooks extends Context.Service<
}
>()("t3/kin/memory/Memory/MemoryRecoveryHooks") {}

export interface MemorySyncSnapshot {
readonly head: string;
readonly items: ReadonlyArray<MemoryCache.MemoryItem>;
readonly duplicateIds: ReadonlySet<string>;
}

/** Sees every loaded snapshot and owns its own once-per-head, retry and failure handling. */
export class MemorySyncHooks extends Context.Reference<{
readonly syncItems: (orgId: KinOrgId, snapshot: MemorySyncSnapshot) => Effect.Effect<void>;
}>("t3/kin/memory/Memory/MemorySyncHooks", {
defaultValue: () => ({ syncItems: () => Effect.void }),
}) {}

export class Memory extends Context.Service<
Memory,
{
Expand Down Expand Up @@ -185,9 +198,7 @@ const folders = ["objectives", "commitments", "deferrals", "decisions", "people"

const maxBlobBytes = 256 * 1024;

interface Snapshot {
readonly head: string;
readonly items: ReadonlyArray<MemoryCache.MemoryItem>;
interface Snapshot extends MemorySyncSnapshot {
readonly files: ReadonlyMap<string, string>;
index?: string;
}
Expand Down Expand Up @@ -218,6 +229,8 @@ const make = Effect.gen(function* () {
const path = yield* Path.Path;
const sql = yield* SqlClient.SqlClient;
const writerLocks = new Map<KinOrgId, Semaphore.Semaphore>();
const pinnedHeads = new Map<KinOrgId, string>();
const hooks = yield* MemorySyncHooks;
const withWriterLock = <A, E>(orgId: KinOrgId, effect: Effect.Effect<A, E>) => {
let lock = writerLocks.get(orgId);
if (!lock) {
Expand Down Expand Up @@ -281,6 +294,9 @@ const make = Effect.gen(function* () {
operation: "record",
reason: "Anchor changed",
});
// The front-matter cache follows commits to the default branch.
if (branch === (config.defaultBranch ?? "main") && pinnedHeads.get(orgId) === previous)
pinnedHeads.set(orgId, head);
}),
)
.pipe(
Expand All @@ -297,7 +313,6 @@ const make = Effect.gen(function* () {
});
const cache = yield* MemoryCache.MemoryCache;
const locks = new Map<KinOrgId, Semaphore.Semaphore>();
const pinnedHeads = new Map<KinOrgId, string>();
const initializedCheckouts = new Set<KinOrgId>();
const duplicateWarnings = new Map<KinOrgId, ReadonlySet<string>>();
const snapshots = new Map<KinOrgId, Snapshot>();
Expand Down Expand Up @@ -530,7 +545,10 @@ const make = Effect.gen(function* () {
return { writer: paths.writer, read: paths.read };
});

const load = Effect.fn("Memory.load")(function* (orgId: KinOrgId, force = false) {
const loadSnapshot = Effect.fn("Memory.loadSnapshot")(function* (
orgId: KinOrgId,
force: boolean,
) {
const paths = yield* pathsFor(orgId);
const pinned = pinnedHeads.get(orgId);
if (pinned === undefined) yield* validateWriter(orgId, paths, false);
Expand Down Expand Up @@ -693,6 +711,7 @@ const make = Effect.gen(function* () {
head,
items,
files,
duplicateIds,
...(previous?.head === head && previous.index !== undefined ? { index: previous.index } : {}),
};
snapshots.set(orgId, snapshot);
Expand All @@ -702,6 +721,11 @@ const make = Effect.gen(function* () {
}
return snapshot;
});
const load = Effect.fn("Memory.load")(function* (orgId: KinOrgId, force = false) {
const snapshot = yield* loadSnapshot(orgId, force);
yield* hooks.syncItems(orgId, snapshot);
return snapshot;
});

const query = Effect.fn("Memory.query")(function* (orgId: KinOrgId, q: MemoryQuery) {
for (const value of [q.dueBefore, q.dueAfter]) {
Expand Down
78 changes: 71 additions & 7 deletions apps/server/src/kin/memory/MemoryWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { memoryGit } from "./git.ts";
import * as NodeZlib from "node:zlib";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { expect, it } from "@effect/vitest";
import { KinAgentId, KinOrgId, VcsProcessSpawnError } from "@t3tools/contracts";
import {
KinAgentId,
KinOrgId,
type MemoryItemHead,
VcsProcessSpawnError,
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
Expand Down Expand Up @@ -58,6 +63,9 @@ it.live.each([
"review-null",
"credentials",
"read-during-record",
"deferral-card-remind",
"deferral-plain-remind",
"deferral-card-other-item",
"H3-packed-ref",
"M1-unanchored",
"M2-nondescendant",
Expand Down Expand Up @@ -120,6 +128,10 @@ it.live.each([
}),
);
}
if (scenario.startsWith("deferral-")) {
yield* fs.makeDirectory(`${source}/deferrals`);
yield* fs.writeFileString(`${source}/deferrals/def-001.md`, fixtureItem());
}
if (scenario === "B5-journal" || scenario === "P4-journal") {
yield* fs.makeDirectory(`${source}/journal/2026`, { recursive: true });
yield* fs.writeFile(
Expand Down Expand Up @@ -207,9 +219,18 @@ it.live.each([
credentialHelper: "fixture-server-helper",
repositories: [{ orgId, repoUrl: remote, path: "state" }],
});
const synced: Array<ReadonlyArray<MemoryItemHead>> = [];
const memoryLayer = Memory.layer.pipe(
Layer.provide(config),
Layer.provide(Layer.succeed(Memory.MemoryRecoveryHooks, { notify: () => Effect.void })),
Layer.provide(
Layer.succeed(Memory.MemorySyncHooks, {
syncItems: (_org, snapshot) =>
Effect.sync(() => {
synced.push(snapshot.items.map(({ head }) => head));
}),
}),
),
);
const hooks = Layer.succeed(Writer.MemoryWriterHooks, {
notify: Effect.fn("Test.notice")((notice) =>
Expand All @@ -233,7 +254,8 @@ it.live.each([
level:
scenario === "conflict" ||
scenario === "B5-journal" ||
scenario === "read-during-record"
scenario === "read-during-record" ||
scenario.startsWith("deferral-")
? "L1"
: "L0",
timeZone: "Asia/Kolkata",
Expand All @@ -258,11 +280,22 @@ it.live.each([
itemId: scenario === "owner-other-item-keep" ? "other-item" : "def-001",
action: scenario === "owner-drop-keep" ? "drop" : "keep",
}
: {
saidBy: "fixture-owner",
trust: "owner",
source: "fixture-owner-message",
},
: scenario === "deferral-card-remind" ||
scenario === "deferral-card-other-item"
? {
saidBy: "fixture-owner",
trust: "owner",
source: "fixture-owner-message",
kind: "deferral_card",
itemId:
scenario === "deferral-card-other-item" ? "other-item" : "def-001",
action: "remind",
}
: {
saidBy: "fixture-owner",
trust: "owner",
source: "fixture-owner-message",
},
],
[
"member",
Expand Down Expand Up @@ -609,6 +642,37 @@ it.live.each([
).toBe("Success");
return;
}
if (scenario === "deferral-plain-remind" || scenario === "deferral-card-other-item") {
const result = yield* writer
.stage(orgId, "wake1", {
path: "deferrals/def-001.md",
content: fixtureItem({ on_due: "remind" }),
sourceEventId: "owner",
})
.pipe(Effect.result);
expect(result._tag === "Failure" && result.failure._tag).toBe("MemoryConflict");
return;
}
if (scenario === "deferral-card-remind") {
const stage = (onDue: string) =>
writer
.stage(orgId, "wake1", {
path: "deferrals/def-001.md",
content: fixtureItem({ on_due: onDue }),
sourceEventId: "owner",
})
.pipe(Effect.result);
const forged = yield* stage("ask");
expect(forged._tag === "Failure" && forged.failure._tag).toBe("MemoryConflict");
expect((yield* stage("remind"))._tag).toBe("Success");
const committed = yield* writer.commitWakeup(orgId, "wake1", "completed");
expect(
yield* git(paths.writer, ["show", `${committed.commit}:deferrals/def-001.md`]),
).toContain("on_due: remind");
const last = synced.at(-1)?.find((item) => item.id === "def-001");
expect(last?.on_due).toBe("remind");
return;
}
if (scenario.endsWith("-keep")) {
const content = fixtureItem({
kind: "claim",
Expand Down
25 changes: 22 additions & 3 deletions apps/server/src/kin/memory/MemoryWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface WakeupContext extends JournalContext {
readonly itemId: string;
readonly action: "keep" | "update" | "drop";
}
| { readonly kind: "deferral_card"; readonly itemId: string; readonly action: "remind" }
)
>;
}
Expand Down Expand Up @@ -532,10 +533,18 @@ const make = Effect.gen(function* () {
)
mutable.add("review_by");
if (
Object.entries(item).some(
([key, value]) =>
source.kind === "deferral_card" &&
source.itemId === item.id &&
item.kind === "deferral" &&
fm.on_due === "remind"
)
mutable.add("on_due");
const existing = new Map(Object.entries(item));
if (
[...new Set([...existing.keys(), ...proposed.keys()])].some(
(key) =>
!mutable.has(key) &&
JSON.stringify(value) !== JSON.stringify(proposed.get(key)),
JSON.stringify(existing.get(key)) !== JSON.stringify(proposed.get(key)),
)
)
return yield* new MemoryConflict({ existingId: item.id });
Expand Down Expand Up @@ -804,6 +813,16 @@ const make = Effect.gen(function* () {
recorded,
]);
yield* memory.record(orgId, branch, recorded, commit);
// Arms deferrals from the commit now, not at the next wake-up.
if (branch === (config.defaultBranch ?? "main"))
yield* memory.rebuildCache(orgId).pipe(
Effect.catch((error) =>
Effect.logWarning("Could not refresh memory cache after commit", {
orgId,
errorTag: error._tag,
}),
),
);
yield* push(commit);
return { commit };
}),
Expand Down
Loading