Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
65f0551
feat(kin): APNs push sender and device registration (MOB-10)
Surajsm60720 Nov 8, 2026
4e1403f
fix(kin): close push security review findings (MOB-10)
Surajsm60720 Nov 8, 2026
95ba3e9
chore(kin): merge kin-appr-5-approver-keys into kin-mob-10-push
Surajsm60720 Nov 8, 2026
6bb228f
Merge remote-tracking branch 'origin/kin' into kin-mob-10-push
Surajsm60720 Nov 8, 2026
4a6da7e
fix(kin): presence from acked watch beats, APNs retries, token takeov…
Surajsm60720 Nov 8, 2026
c78527a
Merge branch 'kin-appr-5-approver-keys' into kin-mob-10-push
Surajsm60720 Nov 8, 2026
b3c6c51
Merge branch 'kin-appr-5-approver-keys' into kin-mob-10-push
Surajsm60720 Nov 8, 2026
b8597ec
fix(kin): drop push devices of revoked relay phones
Surajsm60720 Nov 8, 2026
e9692e4
Merge branch 'kin-appr-5-approver-keys' into kin-mob-10-push
Surajsm60720 Nov 8, 2026
db2d49a
fix(kin): follow the repair-aware revocation helper in push devices
Surajsm60720 Nov 8, 2026
c19e4f2
chore: merge kin-appr-5-approver-keys into kin-mob-10-push
Surajsm60720 Nov 8, 2026
6230571
fix(kin): push a confirmed deferral card that carries no phase
Surajsm60720 Nov 8, 2026
a018b77
fix(kin): name the host environment in push deep links
Surajsm60720 Nov 8, 2026
8952b78
chore: merge kin-appr-5-approver-keys into kin-mob-10-push
Surajsm60720 Nov 8, 2026
74f5427
chore: merge kin-appr-5-approver-keys into kin-mob-10-push
Surajsm60720 Nov 8, 2026
74589b0
chore: merge kin-appr-5-approver-keys into kin-mob-10-push
Surajsm60720 Nov 8, 2026
75b6715
Merge branch 'kin-appr-5-approver-keys' into kin-mob-10-push
Surajsm60720 Nov 8, 2026
e5e8334
fix(server): report kin push RPCs as disabled when Kin is off
Surajsm60720 Nov 8, 2026
735c5c9
Merge branch 'kin-appr-5-approver-keys' into kin-mob-10-push
Surajsm60720 Nov 8, 2026
4b15969
Merge branch 'kin-appr-5-approver-keys' into kin-mob-10-push
Surajsm60720 Nov 8, 2026
786563a
fix(kin): push review fixes
GLDRoger Nov 8, 2026
002f07f
test(kin): two sends hitting an expired APNs token renew it once
GLDRoger Nov 8, 2026
6b882e2
Merge kin-appr-5-approver-keys and origin/kin into kin-mob-10-push
GLDRoger Nov 9, 2026
aa0403f
Merge remote-tracking branch 'origin/kin' into kin-mob-10-push
GLDRoger Nov 9, 2026
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
50 changes: 50 additions & 0 deletions apps/server/scripts/kin-push-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env node
// Dev only: one test push through the server's own APNs transport. From apps/server, with the
// KIN_APNS_* variables set: node scripts/kin-push-test.ts <device token hex> [development|production]
// The mobile spec's "Test push" step has the full runbook.
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
import { KinApsEnvironment, KinPushData, KinPushToken } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";

import { KinApnsTransport, layer } from "../src/kin/push/Apns.ts";
import { kinPushDeviceId } from "../src/kin/push/KinPushDevices.ts";

class KinPushTestError extends Schema.TaggedError<KinPushTestError>()("KinPushTestError", {
message: Schema.String,
}) {}

const decodeToken = Schema.decodeUnknownEffect(KinPushToken);
const decodeEnvironment = Schema.decodeUnknownEffect(KinApsEnvironment);
const decodeData = Schema.decodeEffect(KinPushData);

const program = Effect.gen(function* () {
const [rawToken, rawEnvironment = "development"] = process.argv.slice(2);
const token = yield* decodeToken(rawToken?.toLowerCase());
const environment = yield* decodeEnvironment(rawEnvironment);
const apns = yield* KinApnsTransport;
if (apns.status === "unconfigured")
return yield* new KinPushTestError({ message: "APNs isn't configured; see the log above." });
const body = yield* decodeData({
kin: 1,
orgId: "kin-default",
deepLink: "/kin/kin-default/chats/test",
});
const result = yield* apns.send({
token,
environment,
payload: {
aps: {
alert: { title: "Kin", body: "Test push from the Kin host" },
category: "kin.message",
sound: "default",
},
body,
},
});
yield* Effect.log("APNs answered.", { deviceId: kinPushDeviceId(token), ...result });
if (result.status !== 200)
return yield* new KinPushTestError({ message: "APNs refused the push." });
});

program.pipe(Effect.provide(layer), Effect.scoped, NodeRuntime.runMain);
8 changes: 7 additions & 1 deletion apps/server/src/kin/KinServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { ApproverKeys } from "./approvals/ApproverKeys.ts";
import { GrantVerifier } from "./approvals/GrantVerifier.ts";
import { KinStop } from "./lead/KinStop.ts";
import type { KinGithubWebhookLock } from "./triggers/githubWebhooks.ts";
import type { KinPushDevices } from "./push/KinPushDevices.ts";
import type { KinPushPresence } from "./push/KinPushPresence.ts";
import type { KinPushSender } from "./push/KinPushSender.ts";

export type Services =
| KinIdentity
Expand All @@ -21,7 +24,10 @@ export type Services =
| KinStop
| KinGithubWebhookLock
| ApproverKeys
| GrantVerifier;
| GrantVerifier
| KinPushDevices
| KinPushPresence
| KinPushSender;

export class KinServer extends Context.Service<
KinServer,
Expand Down
16 changes: 16 additions & 0 deletions apps/server/src/kin/approvals/ApproverKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
type KinApprovalsRegisterKeyResult,
type KinKeyThumbprint,
type KinOrgId,
type KinPersonId,
} from "@t3tools/contracts";
import { computeDpopJwkThumbprint } from "@t3tools/shared/dpop";
import { isLocalLoopbackHost } from "@t3tools/shared/hostClassification";
Expand Down Expand Up @@ -179,6 +180,10 @@ export class ApproverKeys extends Context.Service<
readonly reconcileSessions: (
orgId: KinOrgId | undefined,
) => Effect.Effect<number, KinApprovalError>;
/** People holding an `active` key in the org: the only ones approval pushes reach. */
readonly activeApprovers: (
orgId: KinOrgId,
) => Effect.Effect<ReadonlyArray<KinPersonId>, KinApprovalError>;
}
>()("t3/kin/approvals/ApproverKeys") {}

Expand Down Expand Up @@ -530,6 +535,16 @@ const make = Effect.gen(function* () {
Effect.catch(internal("ApproverKeys.activateByGrant")),
);

const activeApprovers = Effect.fn("ApproverKeys.activeApprovers")(
function* (orgId: KinOrgId) {
yield* reconcileSessions(orgId);
const rows = yield* sql<{ human_id: KinPersonId }>`SELECT DISTINCT human_id
FROM kin_approver_keys WHERE org_id = ${orgId} AND state = 'active' ORDER BY human_id`;
return rows.map((row) => row.human_id);
},
Effect.catch(internal("ApproverKeys.activeApprovers")),
);

yield* reconcileSessions(undefined).pipe(
Effect.catch((error) => Effect.logError("Kin approver key reconciliation failed.", error)),
);
Expand All @@ -544,6 +559,7 @@ const make = Effect.gen(function* () {
requireActive,
activateByGrant,
reconcileSessions,
activeApprovers,
});
});

Expand Down
8 changes: 8 additions & 0 deletions apps/server/src/kin/approvals/Audit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assert, describe, it } from "@effect/vitest";
import {
AuthSessionId,
AuthStandardClientScopes,
EnvironmentId,
KinActionId,
KinEventId,
KinKeyThumbprint,
Expand All @@ -25,6 +26,7 @@ import * as TestClock from "effect/testing/TestClock";
import type { AuthenticatedSession } from "../../auth/EnvironmentAuth.ts";
import * as Sqlite from "../../persistence/Sqlite.ts";
import { KinIdentity } from "../chat/KinIdentity.ts";
import * as ServerEnvironment from "../../environment/ServerEnvironment.ts";
import * as Kin from "../layer.ts";
import * as KinPersistence from "../persistence/KinPersistence.ts";
import { KinAudit, kinAuditHash, layer as auditLayer } from "./Audit.ts";
Expand Down Expand Up @@ -528,6 +530,12 @@ describe("kin.audit RPCs", () => {
Effect.provide(
Kin.layer.pipe(
Layer.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ KIN_ENABLED: true }))),
// Push deep links name the host environment.
Layer.provide(
Layer.mock(ServerEnvironment.ServerEnvironment)({
getEnvironmentId: Effect.succeed(EnvironmentId.make("env_kin")),
}),
),
),
),
);
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/kin/approvals/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import * as GrantPorts from "./GrantPorts.ts";
import * as GrantVerifier from "./GrantVerifier.ts";
import * as PendingActions from "./PendingActions.ts";

// Push routing reads pending actions, so they stay in the layer's output.
export const layer = GrantVerifier.layer.pipe(
Layer.provideMerge(ApproverKeys.layer),
Layer.provideMerge(PendingActions.layer),
Layer.provide(
Layer.mergeAll(
PendingActions.layer,
AttestationTrust.layer.pipe(Layer.provide(FetchHttpClient.layer)),
GrantPorts.orgRolesLayer,
GrantPorts.liveTargetsUnconfiguredLayer,
Expand Down
7 changes: 7 additions & 0 deletions apps/server/src/kin/approvals/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { PullRequestService } from "../../pullRequest/PullRequestService.ts";
import { liveThreadShell } from "../../mcp/McpToolAccess.testkit.ts";
import * as McpHttpServer from "../../mcp/McpHttpServer.ts";
import { McpInvocationContext, type McpInvocationScope } from "../../mcp/McpInvocationContext.ts";
import * as ServerEnvironment from "../../environment/ServerEnvironment.ts";
import * as Kin from "../layer.ts";
import { KinIdentity } from "../chat/KinIdentity.ts";
import { KinChatService } from "../chat/KinChatService.ts";
Expand Down Expand Up @@ -153,6 +154,12 @@ const layerWith = <E, R>(pullRequestFacts: Layer.Layer<KinPullRequestFacts, E, R
Layer.provideMerge(
Kin.layer.pipe(
Layer.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ KIN_ENABLED: true }))),
// Push deep links name the host environment.
Layer.provide(
Layer.mock(ServerEnvironment.ServerEnvironment)({
getEnvironmentId: Effect.succeed(EnvironmentId.make("env_kin")),
}),
),
),
),
Layer.provideMerge(Layer.mergeAll(KinChatRepository.layer, KinEventQueue.layer)),
Expand Down
10 changes: 10 additions & 0 deletions apps/server/src/kin/layer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
ScheduledTaskId,
AuthStandardClientScopes,
KinChatRpcs,
KinOrgId,
type KinPushToken,
RpcScopeAuthorization,
WsRpcGroup,
WS_METHODS,
Expand All @@ -30,6 +32,7 @@ import * as Kin from "./layer.ts";
import { runKinMigrations } from "./persistence/KinMigrations.ts";
import * as Logger from "effect/Logger";
import { makeKinChatHandlers } from "./chat/rpcHandlers.ts";
import { makeKinPushHandlers } from "./push/rpcHandlers.ts";

const session = {
sessionId: AuthSessionId.make("owner"),
Expand Down Expand Up @@ -105,6 +108,13 @@ it.effect.each(["identity", "defect", "unconfigured"] as const)(
reason: "disabled",
message: "Kin is not running on this host.",
});
const push = yield* makeKinPushHandlers(session).pipe(
Effect.provideService(Kin.KinServer, kin),
);
const device = { orgId: KinOrgId.make("org"), token: "a".repeat(64) as KinPushToken };
assert.include(yield* push["kin.push.unregisterDevice"](device).pipe(Effect.flip), {
reason: "disabled",
});
}).pipe(Effect.provide(Sqlite.layerMemory)),
);

Expand Down
11 changes: 10 additions & 1 deletion apps/server/src/kin/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as HttpRouter from "effect/http/HttpRouter";
import * as Option from "effect/Option";
import * as KinChat from "./chat/layer.ts";
import * as KinApprovals from "./approvals/layer.ts";
import * as KinPush from "./push/layer.ts";
import { KinChatHub } from "./chat/KinChatHub.ts";

import { KinServer, type Services } from "./KinServer.ts";
Expand All @@ -35,7 +36,15 @@ const enabled = Layer.effect(
subscribeForViewer: Option.some(hub.subscribeForViewer),
};
}),
).pipe(Layer.provide(Layer.mergeAll(KinChat.layer, KinApprovals.layer, GithubWebhooks.lockLayer)));
).pipe(
Layer.provide(
Layer.mergeAll(
KinChat.layer,
KinPush.layer.pipe(Layer.provideMerge(KinApprovals.layer)),
GithubWebhooks.lockLayer,
),
),
);
const disabled = Layer.succeed(KinServer, {
taskCardUpdater: Layer.empty,
services: Option.none(),
Expand Down
7 changes: 7 additions & 0 deletions apps/server/src/kin/lead/layer.testkit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { EnvironmentId } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as ServerConfig from "../../config.ts";
import * as ServerEnvironment from "../../environment/ServerEnvironment.ts";
import * as ThreadLaunch from "../../orchestration-v2/ThreadLaunchService.ts";
import * as ThreadManagement from "../../orchestration-v2/ThreadManagementService.ts";
import * as ProjectService from "../../project/ProjectService.ts";
Expand All @@ -13,4 +16,8 @@ export const dormantLeadDependencies = Layer.mergeAll(
Layer.mock(ProjectService.ProjectService)({}),
Layer.mock(VcsProcess.VcsProcess)({}),
ServerConfig.layerTest(process.cwd(), { prefix: "kin-dormant-lead-" }),
// Not a lead dependency: push deep links name the host environment.
Layer.mock(ServerEnvironment.ServerEnvironment)({
getEnvironmentId: Effect.succeed(EnvironmentId.make("env_kin")),
}),
).pipe(Layer.provideMerge(NodeServices.layer));
19 changes: 14 additions & 5 deletions apps/server/src/kin/persistence/KinMigrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const tableNames = [
"kin_ping_escalations",
"kin_ping_items",
"kin_ping_ledger",
"kin_push_devices",
"kin_read_state",
"kin_routine_folds",
"kin_routine_preferences",
Expand Down Expand Up @@ -121,6 +122,7 @@ describe("Kin migrations", () => {
{ migration_id: 18, name: "Dreams" },
{ migration_id: 19, name: "WebhookSecretHash" },
{ migration_id: 20, name: "Approvals" },
{ migration_id: 21, name: "PushDevices" },
]);
assert.deepStrictEqual(
upstreamBefore.map((row) => [row.migration_id, row.name] as const),
Expand All @@ -140,6 +142,7 @@ describe("Kin migrations", () => {
["kin_pending_actions", ["created_at", "updated_at", "expires_at"]],
["kin_grants", ["accepted_at", "consumed_at"]],
["kin_executions", ["started_at", "finished_at"]],
["kin_push_devices", ["created_at", "updated_at"]],
] as const) {
const columns = yield* sql<{ name: string; type: string }>`
SELECT name, type FROM pragma_table_info(${table})
Expand Down Expand Up @@ -191,6 +194,7 @@ describe("Kin migrations", () => {
{ migration_id: 18, name: "Dreams" },
{ migration_id: 19, name: "WebhookSecretHash" },
{ migration_id: 20, name: "Approvals" },
{ migration_id: 21, name: "PushDevices" },
]);
assert.deepStrictEqual(yield* upstreamHistory, upstreamBefore);
assert.deepStrictEqual(yield* sql`SELECT * FROM kin_orgs`, [
Expand Down Expand Up @@ -228,6 +232,7 @@ describe("Kin migrations", () => {
{ name: "idx_kin_people_org_person" },
{ name: "idx_kin_ping_items_item" },
{ name: "idx_kin_ping_items_pending" },
{ name: "idx_kin_push_devices_person" },
{ name: "idx_kin_shadow_entries_promotion" },
{ name: "idx_kin_shadow_entries_review" },
{ name: "idx_kin_trigger_fires_retention" },
Expand Down Expand Up @@ -287,6 +292,7 @@ describe("Kin migrations", () => {
[18, "Dreams"],
[19, "WebhookSecretHash"],
[20, "Approvals"],
[21, "PushDevices"],
]);
const before = yield* kinSchema;
assert.deepStrictEqual(
Expand Down Expand Up @@ -341,6 +347,7 @@ describe("Kin migrations", () => {
yield* sql`DROP TABLE kin_budget_charges`;
yield* sql`ALTER TABLE kin_events DROP COLUMN budget_deferred`;
for (const table of [
"kin_push_devices",
"kin_executions",
"kin_grants",
"kin_pending_actions",
Expand Down Expand Up @@ -370,6 +377,7 @@ describe("Kin migrations", () => {
[18, "Dreams"],
[19, "WebhookSecretHash"],
[20, "Approvals"],
[21, "PushDevices"],
]);
assert.deepStrictEqual(
yield* sql`SELECT member_id FROM kin_chat_members WHERE org_id = 'org' ORDER BY member_id`,
Expand Down Expand Up @@ -415,6 +423,7 @@ describe("Kin migrations", () => {
{ migration_id: 18, name: "Dreams" },
{ migration_id: 19, name: "WebhookSecretHash" },
{ migration_id: 20, name: "Approvals" },
{ migration_id: 21, name: "PushDevices" },
]);
assert.deepStrictEqual(yield* runKinMigrations(), []);
assert.deepStrictEqual(yield* runMigrations(), []);
Expand All @@ -426,13 +435,13 @@ describe("Kin migrations", () => {
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* runKinMigrations();
// What a database looks like when K019 merged before a lower-numbered migration.
// What a database looks like when K021 merged before a lower-numbered migration.
yield* sql`DELETE FROM kin_sql_migrations WHERE migration_id = 5`;
const history = yield* kinHistory;
const exit = yield* Effect.exit(runKinMigrations());
assert.isTrue(Exit.isFailure(exit));
assert.include(String(exit), "5_OneChatOwner");
assert.include(String(exit), "below the latest applied migration 20");
assert.include(String(exit), "below the latest applied migration 21");
assert.deepStrictEqual(yield* kinHistory, history);
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
);
Expand All @@ -441,11 +450,11 @@ describe("Kin migrations", () => {
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* runKinMigrations();
// What a database looks like after running another branch's K020.
yield* sql`UPDATE kin_sql_migrations SET name = 'Budget' WHERE migration_id = 20`;
// What a database looks like after running another branch's K021.
yield* sql`UPDATE kin_sql_migrations SET name = 'Budget' WHERE migration_id = 21`;
const exit = yield* Effect.exit(runKinMigrations());
assert.isTrue(Exit.isFailure(exit));
assert.include(String(exit), "20_Approvals");
assert.include(String(exit), "21_PushDevices");
assert.include(String(exit), "share a number");
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
);
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/kin/persistence/KinMigrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Workers from "./migrations/K017_Workers.ts";
import Dreams from "./migrations/K018_Dreams.ts";
import WebhookSecretHash from "./migrations/K019_WebhookSecretHash.ts";
import Approvals from "./migrations/K020_Approvals.ts";
import PushDevices from "./migrations/K021_PushDevices.ts";

const table = "kin_sql_migrations";
const run = Migrator.make({});
Expand All @@ -46,6 +47,7 @@ const loader = Migrator.fromRecord({
"18_Dreams": Dreams,
"19_WebhookSecretHash": WebhookSecretHash,
"20_Approvals": Approvals,
"21_PushDevices": PushDevices,
});

// The Migrator skips every id at or below the latest applied one, so a migration that
Expand Down
23 changes: 23 additions & 0 deletions apps/server/src/kin/persistence/migrations/K021_PushDevices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

export default Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
// One row per device token per org; the latest person and session to register it own it.
yield* sql`
CREATE TABLE kin_push_devices (
org_id TEXT NOT NULL REFERENCES kin_orgs(org_id),
token TEXT NOT NULL,
person_id TEXT NOT NULL REFERENCES kin_people(person_id),
platform TEXT NOT NULL CHECK (platform IN ('ios')),
aps_environment TEXT NOT NULL CHECK (aps_environment IN ('development', 'production')),
device_label TEXT,
session_id TEXT NOT NULL,
proof_key_thumbprint TEXT,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
PRIMARY KEY (org_id, token)
)
`;
yield* sql`CREATE INDEX idx_kin_push_devices_person ON kin_push_devices(org_id, person_id)`;
});
Loading