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/kin/lead/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Scheduler from "../../scheduling/Scheduler.ts";
import * as Routines from "../triggers/routines.ts";
import * as DeferralFlow from "../triggers/deferralFlow.ts";
import * as GithubEvents from "../triggers/githubEvents.ts";
import * as Watchdogs from "../triggers/watchdogs.ts";
import * as OwnerCheck from "../triggers/ownerCheck.ts";
import * as TriggerScheduler from "../triggers/KinTriggerScheduler.ts";
import * as TriggerStore from "../triggers/KinTriggerStore.ts";
Expand Down Expand Up @@ -64,6 +65,7 @@ const routineRuntime = Layer.mergeAll(
Routines.layer,
DeferralFlow.layer,
GithubEvents.consumerLayer,
Watchdogs.runtimeLayer,
).pipe(
Layer.provideMerge(TriggerScheduler.layer),
Layer.provideMerge(Layer.mergeAll(TriggerStore.layer, TriggerInbox.layer)),
Expand Down
13 changes: 2 additions & 11 deletions apps/server/src/kin/memory/Memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { memoryGit as runMemoryGit } from "./git.ts";
import { VcsProcess } from "../../vcs/VcsProcess.ts";
import * as MemoryCache from "./MemoryCache.ts";
import { mergeMemoryFiles, uniqueMergeBase } from "./merge.ts";
import { buildIndex } from "./buildIndex.ts";
import { buildIndex, indexIsStale } from "./buildIndex.ts";
import { MemoryConfig } from "./MemoryConfig.ts";
import {
MemoryItemError,
Expand Down Expand Up @@ -1319,16 +1319,7 @@ const make = Effect.gen(function* () {
const index = yield* readFile(orgId, "INDEX.md").pipe(
Effect.catchTags({ MemoryError: () => Effect.succeed("") }),
);
const asOf = /^As of ([^\r\n]+)(?:\r?\n|$)/.exec(index)?.[1];
const now = yield* Clock.currentTimeMillis;
if (
!asOf ||
!Number.isFinite(dateMillis(asOf)) ||
!/(?:Z|[+-]\d{2}:\d{2})$/.test(asOf) ||
now - dateMillis(asOf) >= 36 * 60 * 60 * 1000 ||
dateMillis(asOf) > now
)
yield* rebuildIndex(orgId);
if (indexIsStale(index, yield* Clock.currentTimeMillis)) yield* rebuildIndex(orgId);
const rebuilt = yield* readFile(orgId, "INDEX.md");
const indexAsOf = yield* Effect.fromOption(
DateTime.make(/^As of ([^\r\n]+)(?:\r?\n|$)/.exec(rebuilt)?.[1] ?? ""),
Expand Down
8 changes: 8 additions & 0 deletions apps/server/src/kin/memory/buildIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const dateOrder = (value: string | null) => {

type IndexItem = MemoryItemHead & { readonly misleading?: boolean };

/** Needs a rebuild: no zoned "As of" first line, one in the future, or one 36 h old. */
export function indexIsStale(index: string, now: number): boolean {
const asOf = /^As of ([^\r\n]+)(?:\r?\n|$)/.exec(index)?.[1];
if (asOf === undefined || !/(?:Z|[+-]\d{2}:\d{2})$/.test(asOf)) return true;
const at = memoryDateMillis(asOf);
return !Number.isFinite(at) || at > now || now - at >= 36 * 60 * 60 * 1000;
}

export function buildIndex(
items: ReadonlyArray<IndexItem>,
readHistory: Readonly<Record<string, number>>,
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/kin/triggers/KinTriggerSink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ const make = Effect.gen(function* () {
return;
}
const prefix = `scheduled-task:${taskId}:webhook:`;
if (!input.commandId.startsWith(prefix) || source.status !== "active")
// `status` is what trigger_reconcile last saw; the task shape and secret below are the gate.
if (!input.commandId.startsWith(prefix))
return yield* new KinTriggerInbox.KinTriggerInboxConflict({
orgId: source.orgId,
reason: "unknown_source",
Expand Down
47 changes: 47 additions & 0 deletions apps/server/src/kin/triggers/KinTriggerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ export class KinTriggerStore extends Context.Service<
readonly sourceByTaskId: (
id: ScheduledTaskId,
) => Effect.Effect<KinWebhookSource | undefined, StoreError>;
readonly listSources: (
orgId: KinOrgId,
) => Effect.Effect<ReadonlyArray<KinWebhookSource>, StoreError>;
/** Records what `trigger_reconcile` saw; false when the status was no longer `from`. */
readonly setSourceStatus: (
orgId: KinOrgId,
id: KinWebhookSourceId,
from: KinWebhookSource["status"],
to: KinWebhookSource["status"],
) => Effect.Effect<boolean, StoreError>;
/** True when the task's current secret is the one bound to the source. */
readonly secretMatchesTask: (
orgId: KinOrgId,
id: KinWebhookSourceId,
) => Effect.Effect<boolean, StoreError>;
readonly put: (trigger: Omit<KinTrigger, "lastFireId">) => Effect.Effect<void, StoreError>;
readonly get: (
orgId: KinOrgId,
Expand Down Expand Up @@ -395,6 +410,38 @@ const make = Effect.gen(function* () {
},
Effect.mapError(toPersistenceOrDecodeError("KinTriggerStore.sourceByTaskId")),
),
listSources: Effect.fn("KinTriggerStore.listSources")(
function* (orgId) {
const rows = yield* sql`SELECT ${sql.literal(sourceColumns)} FROM kin_webhook_sources
WHERE org_id = ${orgId} ORDER BY source_id`.pipe(NodeSqliteClient.readOnly);
return yield* Effect.forEach(rows, (row) => decodeSource(row));
},
Effect.mapError(toPersistenceOrDecodeError("KinTriggerStore.listSources")),
),
setSourceStatus: Effect.fn("KinTriggerStore.setSourceStatus")(
function* (orgId, id, from, to) {
const rows = yield* sql`UPDATE kin_webhook_sources SET status = ${to}
WHERE org_id = ${orgId} AND source_id = ${id} AND status = ${from}
RETURNING source_id`;
return rows.length === 1;
},
Effect.mapError(toPersistenceSqlError("KinTriggerStore.setSourceStatus")),
),
secretMatchesTask: Effect.fn("KinTriggerStore.secretMatchesTask")(
function* (orgId, id) {
const [row] = yield* sql<{
readonly hash: string | null;
readonly secret: string | null;
}>`SELECT s.secret_sha256 AS hash, t.webhook_secret AS secret FROM kin_webhook_sources s
JOIN scheduled_tasks t ON t.task_id = s.scheduled_task_id
WHERE s.org_id = ${orgId} AND s.source_id = ${id}`.pipe(NodeSqliteClient.readOnly);
if (row?.hash == null || row.secret == null) return false;
const bound = Buffer.from(row.hash, "hex");
const given = secretDigest(Redacted.make(row.secret));
return bound.length === given.length && NodeCrypto.timingSafeEqual(bound, given);
},
Effect.mapError(toPersistenceSqlError("KinTriggerStore.secretMatchesTask")),
),
put: Effect.fn("KinTriggerStore.put")(
function* (trigger) {
const value = yield* decodePutTrigger({ ...trigger, lastFireId: null });
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/kin/triggers/githubEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const repoScopeLayer = Layer.effect(
),
);

const inRepoScope = (repos: ReadonlyArray<string>, repo: string) =>
export const inRepoScope = (repos: ReadonlyArray<string>, repo: string) =>
repos.some((scoped) => scoped.toLowerCase() === repo.toLowerCase());

const header = (headers: Readonly<Record<string, string>>, name: string) =>
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/kin/triggers/pings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function isQuiet(now: number, zone: string, hours: { readonly from: string; read
: hours.from > hours.to && (time >= hours.from || time < hours.to);
}

function quoted(value: string) {
export function quoted(value: string) {
const content = Array.from(value.replace(/[\p{Cc}\p{Cf}\p{Cs}\p{Zl}\p{Zp}]/gu, " "))
.slice(0, 500)
.join("");
Expand Down
Loading