Skip to content
Draft
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
17 changes: 16 additions & 1 deletion apps/server/src/auth/EnvironmentAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Redacted from "effect/Redacted";
import * as SqlClient from "effect/sql/SqlClient";
import * as TestClock from "effect/testing/TestClock";

import * as ServerConfig from "../config.ts";
Expand Down Expand Up @@ -533,10 +534,11 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodePlatformCrypto.layer))(
it.effect("renews a peer link's session while it is used, and no other MCP client's", () =>
Effect.gen(function* () {
const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;
const sql = yield* SqlClient.SqlClient;
const day = 24 * 60 * 60 * 1000;
const issue = (peerLink: boolean) =>
serverAuth.issueMcpClientSession({
label: peerLink ? "T3 Code · A" : "ChatGPT",
label: "T3 Code · A",
access: "auto",
peerLink,
client: requestMetadata,
Expand All @@ -563,11 +565,24 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodePlatformCrypto.layer))(
const idle = yield* issue(true);
const revoked = yield* issue(true);
const outside = yield* issue(false);
const listed = yield* serverAuth.listClientSessions(outside.sessionId);
expect(listed.find((session) => session.sessionId === used.sessionId)).toMatchObject({
peerLink: true,
});
expect(
listed.find((session) => session.sessionId === outside.sessionId),
).not.toHaveProperty("peerLink");
yield* sql`UPDATE auth_sessions SET peer_link = 0 WHERE session_id = ${used.sessionId}`;

// Within a day of its full lifetime, a call leaves the session row alone.
yield* TestClock.adjust("12 hours");
expect(yield* use(used)).toBe(issuedAt + 30 * day);
expect(yield* listedExpiry(used)).toBe(issuedAt + 30 * day);
expect(
(yield* serverAuth.listClientSessions(outside.sessionId)).find(
(session) => session.sessionId === used.sessionId,
),
).toMatchObject({ peerLink: true });

yield* TestClock.adjust("468 hours");
expect(yield* use(used)).toBe(issuedAt + 50 * day);
Expand Down
17 changes: 17 additions & 0 deletions apps/server/src/auth/SessionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ const make = Effect.gen(function* () {
return Option.some(
toAuthClientSession({
sessionId: row.value.sessionId,
...(row.value.peerLink ? { peerLink: true } : {}),
subject: row.value.subject,
scopes: row.value.scopes,
method: row.value.method,
Expand Down Expand Up @@ -771,6 +772,7 @@ const make = Effect.gen(function* () {
issuedAt,
expiresAt: sessionExpiresAt,
...(claims.jkt ? { proofKeyThumbprint: claims.jkt } : {}),
...(claims.plk === 1 ? { peerLink: true } : {}),
} satisfies AuthSessions.CreateAuthSessionInput;
const replacedSessionIds = yield* (
input?.replaceSessionId !== undefined || input?.replaceActiveForSubjectAndMethod
Expand Down Expand Up @@ -800,6 +802,7 @@ const make = Effect.gen(function* () {
yield* emitUpsert(
toAuthClientSession({
sessionId,
...(claims.plk === 1 ? { peerLink: true } : {}),
subject: claims.sub,
scopes: claims.scopes,
method: claims.method,
Expand Down Expand Up @@ -920,6 +923,19 @@ const make = Effect.gen(function* () {
});
}

// Older sessions carried the peer-link marker only in the signed token.
if (rowHoldsDeadline && !row.value.peerLink) {
yield* authSessions.markPeerLink(claims.sid).pipe(
Effect.flatMap((changed) =>
changed ? loadActiveSession(claims.sid) : Effect.succeedNone,
),
Effect.flatMap(Option.match({ onNone: () => Effect.void, onSome: emitUpsert })),
Effect.catch((cause) =>
Effect.logWarning("Could not classify a peer link session.", cause),
),
);
}

return {
sessionId: claims.sid,
token,
Expand Down Expand Up @@ -1167,6 +1183,7 @@ const make = Effect.gen(function* () {
return rows.map((row) =>
toAuthClientSession({
sessionId: row.sessionId,
...(row.peerLink ? { peerLink: true } : {}),
subject: row.subject,
scopes: row.scopes,
method: row.method,
Expand Down
37 changes: 35 additions & 2 deletions apps/server/src/persistence/AuthSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type AuthSessionClientMetadataRecord = typeof AuthSessionClientMetadataRe

export const AuthSessionRecord = Schema.Struct({
sessionId: AuthSessionId,
peerLink: Schema.Boolean,
subject: Schema.String,
scopes: AuthEnvironmentScopes,
method: ServerAuthSessionMethod,
Expand All @@ -53,6 +54,7 @@ export const CreateAuthSessionInput = Schema.Struct({
issuedAt: Schema.DateTimeUtcFromString,
expiresAt: Schema.DateTimeUtcFromString,
proofKeyThumbprint: Schema.optionalKey(Schema.String),
peerLink: Schema.optionalKey(Schema.Boolean),
});
export type CreateAuthSessionInput = typeof CreateAuthSessionInput.Type;

Expand Down Expand Up @@ -126,6 +128,9 @@ export class AuthSessionRepository extends Context.Service<
readonly listActive: (
input: ListActiveAuthSessionsInput,
) => Effect.Effect<ReadonlyArray<AuthSessionRecord>, AuthSessionRepositoryError>;
readonly markPeerLink: (
sessionId: AuthSessionId,
) => Effect.Effect<boolean, AuthSessionRepositoryError>;
readonly revoke: (
input: RevokeAuthSessionInput,
) => Effect.Effect<boolean, AuthSessionRepositoryError>;
Expand All @@ -150,6 +155,7 @@ export class AuthSessionRepository extends Context.Service<

const AuthSessionDbRow = Schema.Struct({
sessionId: AuthSessionId,
peerLink: Schema.Literals([0, 1]),
subject: Schema.String,
scopes: Schema.fromJsonString(AuthEnvironmentScopes),
method: ServerAuthSessionMethod,
Expand All @@ -167,6 +173,7 @@ const AuthSessionDbRow = Schema.Struct({

const AuthSessionRawDbRow = Schema.Struct({
sessionId: Schema.String,
peerLink: Schema.Unknown,
subject: Schema.Unknown,
scopes: Schema.Unknown,
method: Schema.Unknown,
Expand All @@ -187,6 +194,7 @@ const decodeAuthSessionDbRow = Schema.decodeUnknownEffect(AuthSessionDbRow);
function toAuthSessionRecord(row: typeof AuthSessionDbRow.Type): AuthSessionRecord {
return {
sessionId: row.sessionId,
peerLink: row.peerLink === 1,
subject: row.subject,
scopes: row.scopes,
method: row.method,
Expand Down Expand Up @@ -243,7 +251,8 @@ export const make = Effect.gen(function* () {
issued_at,
expires_at,
revoked_at,
proof_key_thumbprint
proof_key_thumbprint,
peer_link
)
VALUES (
${input.sessionId},
Expand All @@ -259,7 +268,8 @@ export const make = Effect.gen(function* () {
${input.issuedAt},
${input.expiresAt},
NULL,
${input.proofKeyThumbprint ?? null}
${input.proofKeyThumbprint ?? null},
${input.peerLink === true ? 1 : 0}
)
${ignoreExisting ? sql`ON CONFLICT(session_id) DO NOTHING` : sql``}
`,
Expand All @@ -274,6 +284,7 @@ export const make = Effect.gen(function* () {
sql`
SELECT
session_id AS "sessionId",
peer_link AS "peerLink",
subject AS "subject",
scopes AS "scopes",
method AS "method",
Expand Down Expand Up @@ -318,6 +329,7 @@ export const make = Effect.gen(function* () {
sql`
SELECT
session_id AS "sessionId",
peer_link AS "peerLink",
subject AS "subject",
scopes AS "scopes",
method AS "method",
Expand Down Expand Up @@ -513,6 +525,26 @@ export const make = Effect.gen(function* () {
),
);

const markPeerLink: AuthSessionRepository["Service"]["markPeerLink"] = (sessionId) =>
sql`
UPDATE auth_sessions
SET peer_link = 1
WHERE session_id = ${sessionId}
AND peer_link = 0
AND revoked_at IS NULL
RETURNING session_id
`.pipe(
Effect.map((rows) => rows.length > 0),
Effect.mapError(
(cause) =>
new PersistenceSqlError({
operation: "AuthSessionRepository.markPeerLink:query",
correlation: { sessionId },
cause,
}),
),
);

const revoke: AuthSessionRepository["Service"]["revoke"] = (input) =>
revokeSessionRows(input).pipe(
Effect.mapError(
Expand Down Expand Up @@ -592,6 +624,7 @@ export const make = Effect.gen(function* () {
createIfAbsent,
getById,
listActive,
markPeerLink,
revoke,
extend,
revokeAllExcept,
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/persistence/ForkMigrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe("fork migration ledger", () => {
{ migration_id: 34, name: "UsageBudget" },
{ migration_id: 35, name: "RequeueQueueNotices" },
{ migration_id: 36, name: "UpstreamUsageLimitIndex" },
{ migration_id: 37, name: "AuthSessionPeerLink" },
],
);
const columns = yield* sql<{ name: string }>`PRAGMA table_info(owner_decisions)`;
Expand Down Expand Up @@ -333,6 +334,7 @@ describe("fork migration ledger", () => {
{ migration_id: 34, name: "UsageBudget" },
{ migration_id: 35, name: "RequeueQueueNotices" },
{ migration_id: 36, name: "UpstreamUsageLimitIndex" },
{ migration_id: 37, name: "AuthSessionPeerLink" },
],
);
assert.ok((yield* tableNames).has("orchestration_v2_projection_thread_usage"));
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/persistence/ForkMigrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import ProjectArtifactDescriptions from "./ForkMigrations/025_ProjectArtifactDes
import ClosedIssueWorkIndexes from "./ForkMigrations/018_ClosedIssueWorkIndexes.ts";
import UsageLimitRecoveryIndex from "./ForkMigrations/019_UsageLimitRecoveryIndex.ts";
import UpstreamUsageLimitIndex from "./ForkMigrations/036_UpstreamUsageLimitIndex.ts";
import AuthSessionPeerLink from "./ForkMigrations/037_AuthSessionPeerLink.ts";
import IssueQueue from "./ForkMigrations/016_IssueQueue.ts";
import IssueInbox from "./ForkMigrations/015_IssueInbox.ts";
import MarkedIssueInbox from "./ForkMigrations/020_MarkedIssueInbox.ts";
Expand Down Expand Up @@ -84,6 +85,7 @@ export const forkMigrationEntries = [
[34, "UsageBudget", UsageBudget],
[35, "RequeueQueueNotices", RequeueQueueNotices],
[36, "UpstreamUsageLimitIndex", UpstreamUsageLimitIndex],
[37, "AuthSessionPeerLink", AuthSessionPeerLink],
] as const;

// Fork builds before v2.11 recorded these in upstream's ledger as 57–62.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

export default Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`ALTER TABLE auth_sessions ADD COLUMN peer_link INTEGER NOT NULL DEFAULT 0`;
});
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ const ConnectedClientListRow = memo(function ConnectedClientListRow({
</div>
<div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">
{environmentId !== null &&
!clientSession.peerLink &&
clientSession.subject === MCP_CLIENT_SESSION_SUBJECT &&
clientSession.scopes.includes(AuthOrchestrationOperateScope) ? (
<ApprovalRelaySwitch
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/environments/primary/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export interface ServerPairingLinkRecord {

export interface ServerClientSessionRecord {
readonly sessionId: AuthSessionId;
readonly peerLink?: true;
readonly subject: string;
readonly scopes: ReadonlyArray<AuthEnvironmentScope>;
readonly method: ServerAuthSessionMethod;
Expand Down
6 changes: 6 additions & 0 deletions docs/user/outside-agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ keep its threads together and wait on them without a shell on your computer.
later with `t3_thread_read`. A wait returns after about 45 seconds, so a
long job is read on a schedule rather than waited on in a loop.

For Grok Bot, use the scheduled read for long jobs. T3's MCP Events webhook
requires a receiver that echoes a verification challenge and accepts Standard
Webhooks signatures. [Grok Bot's routine webhook](https://docs.x.ai/grok-bot/skills-routines-and-automations)
expects a bearer key to start a run, so direct delivery would require the
routine endpoint to support MCP Events verification and signing as well.

An outside agent can answer a job's questions with `t3_pending_request`. It
cannot accept or decline a command approval unless you let it: in
**Settings → Sharing & access**, turn on **Answers approvals** for that agent.
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ export type AuthConnectDevice = typeof AuthConnectDevice.Type;

export const AuthClientSession = Schema.Struct({
sessionId: AuthSessionId,
peerLink: Schema.optionalKey(Schema.Literal(true)),
subject: TrimmedNonEmptyString,
...authScopeResponseFields,
method: ServerAuthSessionMethod,
Expand Down