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
4 changes: 2 additions & 2 deletions .claude/commands/convex-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ convex/[feature]/
```typescript
import { query } from "../_generated/server";
import { v } from "convex/values";
import { getAuthUserId } from "@convex-dev/auth/server";
import { getAuthUserId } from "../_utils/auth";

export const myQuery = query({
args: {
Expand All @@ -45,7 +45,7 @@ export const myQuery = query({
```typescript
import { mutation } from "../_generated/server";
import { v } from "convex/values";
import { getAuthUserId } from "@convex-dev/auth/server";
import { getAuthUserId } from "../_utils/auth";

export const myMutation = mutation({
args: {
Expand Down
2 changes: 1 addition & 1 deletion .claude/guides/convex-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const enrichedLogs = logs.map((log) => {

```typescript
// convex/admin/utils.ts
import { getAuthUserId } from "@convex-dev/auth/server";
import { getAuthUserId } from "../_utils/auth";
import type { Id } from "../_generated/dataModel";
import type { MutationCtx, QueryCtx } from "../_generated/server";

Expand Down
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Gitdog is an AI-powered GitHub assistant for repository automation. It is built
- Do not create barrel export files in the `convex/` root.
- Organize functions in subdirectories with `query.ts`, `mutation.ts`, and `action.ts`.
- Use the new Convex function syntax with validators.
- Authenticate with `getAuthUserId(ctx)` from `@convex-dev/auth/server`.
- Authenticate with `getAuthUserId(ctx)` from `convex/_utils/auth.ts`; it works in queries, mutations and actions.
- Use dot-notation API references such as `api.repositories.query.list`.
- Use `.withIndex()` instead of `.filter()` for database queries.
- Avoid unbounded `.collect()`; use `paginatedCollect()` for large reads.
Expand Down
5 changes: 4 additions & 1 deletion convex/_ai/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ export type UserKeyModel = {
// USD per 1M tokens, so owners can compare models before choosing one.
inputPer1M: number;
outputPer1M: number;
// Requests with more input tokens than this are billed at a higher rate.
longContextInputTokens?: number;
};

const EFFORTS: readonly ModelEffort[] = ["low", "medium", "high"];
Expand All @@ -193,14 +195,15 @@ function userKeyOption(
modelId: string,
defaultEffort: ModelEffort,
): UserKeyModel {
const { name, inputPer1M, outputPer1M } = CHAT_MODELS[modelId];
const { name, inputPer1M, outputPer1M, longContext } = CHAT_MODELS[modelId];
return {
modelId,
name,
efforts: EFFORTS,
defaultEffort,
inputPer1M,
outputPer1M,
...(longContext && { longContextInputTokens: longContext.inputTokens }),
};
}

Expand Down
5 changes: 3 additions & 2 deletions convex/github/accessVerification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export const ACCESS_VERIFICATION_TTL_MS = 10 * 60 * 1_000;
const DEFAULT_RETURN_PATH = "/repositories/connect";

/**
* Where GitHub sends the user back: the same site as sign-in's OAuth
* callback, so one GitHub App lists both under one host.
* Where GitHub sends the user back. Sign-in's callback always uses
* CONVEX_SITE_URL; a CUSTOM_AUTH_SITE_URL moves only this one, and the
* GitHub App must list both.
*/
export function accessVerificationRedirectUri(): string | null {
const site =
Expand Down
13 changes: 10 additions & 3 deletions convex/tokenUsage/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,9 @@ export const getUserKeyUsage = query({
return {
...usage,
actionType: action.actionType,
// Deleted tickets keep their cost but no longer link anywhere.
ticketId: ticket.deletedAt ? null : ticket._id,
// Hidden tickets keep their cost but no longer link anywhere.
ticketId:
ticket.deletedAt || ticket.githubLifecycleOnly ? null : ticket._id,
ticketType: ticket.ticketType,
githubRepo: ticket.githubRepo,
githubNumber: ticket.githubNumber,
Expand Down Expand Up @@ -402,7 +403,13 @@ export const getTicketUsage = query({
const userId = await getAuthUserId(ctx);
if (!userId) return [];
const ticket = await ctx.db.get(args.ticketId);
if (!ticket || ticket.userId !== userId || ticket.deletedAt) return [];
if (
!ticket ||
ticket.userId !== userId ||
ticket.deletedAt ||
ticket.githubLifecycleOnly === true
)
return [];

const actions = await ctx.db
.query("ticketActions")
Expand Down
15 changes: 2 additions & 13 deletions convex/tokenUsage/usageSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ export function summarizeUserKeyUsage(rows: UsageRow[]) {
const models = new Map<string, ModelUsage & { actionIds: Set<string> }>();
const repositories = new Map<
string,
{
repositoryId?: Id<"repositories">;
costCents: number;
actionIds: Set<string>;
}
{ repositoryId?: Id<"repositories">; costCents: number }
>();
const actions = new Map<string, ActionUsage>();

Expand All @@ -83,10 +79,8 @@ export function summarizeUserKeyUsage(rows: UsageRow[]) {
const repository = repositories.get(repositoryKey) ?? {
...(row.repositoryId ? { repositoryId: row.repositoryId } : {}),
costCents: 0,
actionIds: new Set<string>(),
};
repository.costCents += row.baseCostCents;
if (row.ticketActionId) repository.actionIds.add(row.ticketActionId);
repositories.set(repositoryKey, repository);

if (!row.ticketActionId) continue;
Expand All @@ -108,12 +102,7 @@ export function summarizeUserKeyUsage(rows: UsageRow[]) {
models: [...models.values()]
.map(({ actionIds, ...model }) => ({ ...model, actions: actionIds.size }))
.sort((a, b) => b.costCents - a.costCents),
repositories: [...repositories.values()].map(
({ actionIds, ...repository }) => ({
...repository,
actions: actionIds.size,
}),
),
repositories: [...repositories.values()],
actions: [...actions.values()].sort((a, b) => b.lastAt - a.lastAt),
};
}
Expand Down
4 changes: 2 additions & 2 deletions docs/github-app-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ For production deployment:

```text
User Login Flow:
GitHub App → convex/auth.ts → User session created
GitHub OAuth → convex/auth.ts (@convex-dev/auth 2.0) → User session created

Repository Access Flow:
GitHub OAuth login → Short-lived `/user/installations` attestation → Recover personal installations
Verify GitHub access (`/github/access/callback`) → Short-lived `/user/installations` attestation → Recover personal installations
Organization installation/update → Signed webhook → Save installation
Setup callback → Coordinate verification and repository reconnect UI
User requests repos → convex/github/action.ts → Generate JWT → Get installation token → Fetch repos
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ For example: `npx gitdog@latest login`. An optional global installation with

`gitdog login` opens GitHub in your browser. Use a browser on the same computer,
or pass `--no-browser` to open the printed URL yourself. Ctrl+C cancels the entire
sign-in flow. The browser callback times out after five minutes. When sign-in
sign-in flow. The browser callback listens on the first free loopback port from
41721 to 41725 and times out after five minutes. When sign-in
finishes, your browser shows the result on Gitdog and the tab can be closed.

## Projects, like Firebase
Expand Down Expand Up @@ -311,8 +312,9 @@ gitdog login --api-url https://your-deployment.convex.cloud --site-url http://lo
gitdog github install --app gitdogbot-dev
```

Custom URLs require HTTPS except for loopback development. The sign-in URL must
use the selected backend's origin or its matching `convex.site` origin.
Custom URLs require HTTPS except for loopback development. The CLI opens only
GitHub's `https://github.com/login/oauth/authorize` page for sign-in and
`gitdog github verify`.

## Development and verification

Expand Down
9 changes: 6 additions & 3 deletions packages/cli/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,20 @@ export async function createLoginListener(
};
}

/** GitHub's authorization page is the only place sign-in may send the user. */
/** GitHub's authorization page is the only place the CLI may send the user. */
export function validateAuthRedirect(value: string): void {
const url = new URL(value);
const url = URL.canParse(value) ? new URL(value) : undefined;
if (
!url ||
url.origin !== "https://github.com" ||
url.pathname !== "/login/oauth/authorize" ||
url.username ||
url.password ||
url.hash
) {
throw new Error("The server returned an unexpected sign-in URL.");
throw new Error(
"The server returned an unexpected GitHub authorization URL.",
);
}
}

Expand Down
9 changes: 4 additions & 5 deletions packages/cli/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join, resolve } from "node:path";
import { rm } from "node:fs/promises";
import type { Value } from "convex/values";
import { GitdogApi, type Api, type Args, type Kind } from "./client.js";
import { login, logout, openBrowser } from "./auth.js";
import { login, logout, openBrowser, validateAuthRedirect } from "./auth.js";
import { API_CATALOG, publicOperation } from "./catalog.js";
import {
BOOLEAN_SETTINGS,
Expand Down Expand Up @@ -1429,7 +1429,7 @@ export function createProgram(
if (!ui.json && !flag(opts.print)) await openBrowser(url);
ui.output({
url,
next: "After GitHub authorization, run npx gitdog@latest github recover, then npx gitdog@latest projects available.",
next: "After installing, run npx gitdog@latest github verify, then npx gitdog@latest github recover and npx gitdog@latest projects available.",
});
},
)
Expand All @@ -1449,9 +1449,8 @@ export function createProgram(
returnPath: "/repositories/connect",
}),
);
const url = typeof result?.url === "string" ? result.url : undefined;
if (!url?.startsWith("https://github.com/"))
throw new Error("Gitdog returned an invalid GitHub authorization URL.");
const url = typeof result?.url === "string" ? result.url : "";
validateAuthRedirect(url);
if (!ui.json && !flag(opts.print)) await openBrowser(url);
ui.output({
url,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class ProjectContext {
);
if (matches.length !== 1)
throw new Error(
`Repository ${name} is unavailable. Run npx gitdog@latest github install, authorize it, then npx gitdog@latest github recover. Check projects available.`,
`Repository ${name} is unavailable. Run npx gitdog@latest github install, then npx gitdog@latest github verify and npx gitdog@latest github recover. Check projects available.`,
);
const item = matches[0];
if (typeof item.installationId !== "number" || typeof item.id !== "number")
Expand Down
37 changes: 37 additions & 0 deletions packages/cli/test/auth.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdir, readFile, rmdir, stat, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { createServer } from "node:net";
import {
createLoginListener,
LOGIN_CALLBACK_PORTS,
login,
logout,
validateAuthRedirect,
Expand Down Expand Up @@ -84,6 +86,41 @@ test("loopback listener times out and supports cancellation", async () => {
await assert.rejects(cancelled.code, /cancelled/);
});

// Holds each port another process is not already using.
async function occupy(ports) {
const servers = [];
for (const port of ports) {
const server = createServer();
const bound = await new Promise((resolve, reject) => {
server.once("error", (error) =>
error.code === "EADDRINUSE" ? resolve(false) : reject(error),
);
server.listen(port, "127.0.0.1", () => resolve(true));
});
if (bound) servers.push(server);
}
return async () => {
await Promise.all(
servers.map((server) => new Promise((resolve) => server.close(resolve))),
);
};
}

test("loopback listener falls back to the next fixed port", async (t) => {
const release = await occupy(LOGIN_CALLBACK_PORTS.slice(0, 1));
t.after(release);
const listener = await createLoginListener(5000);
t.after(listener.close);
const port = Number(new URL(listener.redirectTo).port);
assert.ok(LOGIN_CALLBACK_PORTS.slice(1).includes(port));
});

test("loopback listener fails clearly when every fixed port is busy", async (t) => {
const release = await occupy(LOGIN_CALLBACK_PORTS);
t.after(release);
await assert.rejects(createLoginListener(5000), /are all in use/);
});

test("Convex OAuth cancellation without a code fails immediately", async (t) => {
const listener = await createLoginListener(5000);
t.after(listener.close);
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/test/commands.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,13 @@ test("github verify starts access verification and accepts only GitHub URLs", as
);
await assert.rejects(
run("https://evil.example/login"),
/invalid GitHub authorization URL/,
/unexpected GitHub authorization URL/,
);
await assert.rejects(
run("https://github.com/evil/login/oauth/authorize"),
/unexpected GitHub authorization URL/,
);
await assert.rejects(run(undefined), /unexpected GitHub authorization URL/);
});

test("mutation confirmation happens before a GitHub action executes", async () => {
Expand Down
23 changes: 15 additions & 8 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@
"signOut": "Sign out instead",
"policyChangedError": "The Terms or Privacy Policy changed while this page was open. Reloading the current policies…",
"saveError": "We could not save your confirmation. Please try again.",
"signInError": "We could not start GitHub sign-in. Please try again."
"signInError": "We could not start GitHub sign-in. Please try again.",
"signInCancelled": "GitHub sign-in was cancelled.",
"signInExpired": "GitHub sign-in took too long. Please try again.",
"signInFailed": "GitHub sign-in did not finish. Please try again."
}
},
"home": {
Expand Down Expand Up @@ -220,16 +223,17 @@
"title": "Install Gitdog GitHub App",
"description": "To access your repositories (including private and organization repos), you need to install the Gitdog GitHub App.",
"maybeLater": "Maybe later",
"installButton": "Install GitHub App"
"installButton": "Install GitHub App",
"alreadyInstalled": "Already installed? Verify GitHub access"
},
"verificationPending": {
"title": "Finishing GitHub connection",
"description": "Gitdog is waiting for GitHub to confirm this installation. Keep this page open and retry verification shortly.",
"organizationDescription": "For security, GitHub must confirm organization access through a signed installation update before Gitdog can connect it.",
"reauthDescription": "Gitdog needs a fresh GitHub authorization to verify this existing installation.",
"recoveryInstructions": "Refresh GitHub authorization, or use Manage GitHub access and make an actual repository selection change. If access is already correct, uninstall and reinstall the GitHub App before retrying.",
"reauthorize": "Refresh GitHub authorization",
"reauthorizing": "Opening GitHub...",
"reauthDescription": "Gitdog needs to confirm your GitHub access to this installation.",
"recoveryInstructions": "Verify GitHub access, or use Manage GitHub access and make an actual repository selection change. If access is already correct, uninstall and reinstall the GitHub App before retrying.",
"verifyAccess": "Verify GitHub access",
"verifyingAccess": "Opening GitHub...",
"retry": "Retry verification",
"retrying": "Checking...",
"manageAccess": "Manage GitHub access",
Expand Down Expand Up @@ -566,7 +570,9 @@
"runsOn": "Reviews run on your {{provider}} key ••••{{last4}}",
"effort": "{{effort}} effort",
"backup": "If it runs out of credit, your {{provider}} key takes over with {{model}}.",
"noActiveKey": "No key is working right now, so reviews run on Gitdog tokens.",
"noKey": "No key is saved, so reviews run on Gitdog tokens.",
"noWorkingKey": "None of your saved keys works right now, so reviews stop until you fix or replace one.",
"keyChecksUnavailable": "Your saved keys can't be used right now because of a temporary Gitdog problem. Reviews resume once it is fixed.",
"changeModel": "Change model",
"manageKeys": "Manage keys",
"costLabel": "Provider cost this period",
Expand Down Expand Up @@ -662,13 +668,14 @@
},
"defaultModel": "Default",
"price": "{{input}} in · {{output}} out per 1M tokens",
"longContextPrice": "Higher rate above {{tokens}} input tokens",
"efforts": {
"low": "Low",
"medium": "Medium",
"high": "High"
},
"effortHint": "Higher effort reads more and costs more per run.",
"thisPeriod": "This period: {{cost}} on this key"
"thisPeriod": "{{provider}} cost this period: {{cost}}"
}
},
"footer": {
Expand Down
Loading