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
42 changes: 36 additions & 6 deletions packages/core/src/resources/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ export interface ResourceMeta {

let _initialized = false;

const DEFAULT_LEARNINGS_MD = `# Learnings

Record user preferences, corrections, and patterns here. The agent reads this at the start of every conversation.

## Preferences

## Corrections

## Patterns
`;

const DEFAULT_AGENTS_MD = `# Agent Instructions

This file customizes how the AI agent behaves in this app. Edit it to add your own instructions, preferences, and context.
Expand Down Expand Up @@ -81,20 +92,39 @@ async function ensureTable(): Promise<void> {
)
`);

// Seed default shared AGENTS.md if it doesn't exist (INSERT OR IGNORE to avoid race conditions)
// Seed default shared resources if they don't exist (INSERT OR IGNORE to avoid race conditions)
const now = Date.now();
const size = Buffer.byteLength(DEFAULT_AGENTS_MD, "utf8");
const seedSql = isPostgres()
? `INSERT INTO resources (id, path, owner, content, mime_type, size, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (path, owner) DO NOTHING`
: `INSERT OR IGNORE INTO resources (id, path, owner, content, mime_type, size, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;

// AGENTS.md β€” shared agent instructions
const agentsSize = Buffer.byteLength(DEFAULT_AGENTS_MD, "utf8");
await client.execute({
sql: isPostgres()
? `INSERT INTO resources (id, path, owner, content, mime_type, size, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (path, owner) DO NOTHING`
: `INSERT OR IGNORE INTO resources (id, path, owner, content, mime_type, size, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
sql: seedSql,
args: [
crypto.randomUUID(),
"AGENTS.md",
SHARED_OWNER,
DEFAULT_AGENTS_MD,
"text/markdown",
size,
agentsSize,
now,
now,
],
});

// LEARNINGS.md β€” shared learnings (preferences, corrections, patterns)
const learningsSize = Buffer.byteLength(DEFAULT_LEARNINGS_MD, "utf8");
await client.execute({
sql: seedSql,
Comment on lines +117 to +120
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟑 LEARNINGS.md seeded with uppercase path but all readers use lowercase learnings.md

The agent prompt, resource-read script, and migration all reference learnings.md (lowercase), but this seeds LEARNINGS.md. SQLite and Postgres path lookups are exact-match, so the seeded default is never found β€” and fresh databases can end up with two separate learnings files.


How did I do? React with πŸ‘ or πŸ‘Ž to help me improve.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different agent's code. Low priority β€” will address separately.

args: [
crypto.randomUUID(),
"LEARNINGS.md",
SHARED_OWNER,
DEFAULT_LEARNINGS_MD,
"text/markdown",
learningsSize,
now,
now,
],
Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,23 @@ const DEV_SESSION: AuthSession = { email: "local@localhost" };
/**
* Get the current auth session for a request.
*
* - In dev mode: always returns { email: "local@localhost" }
* - In dev mode: checks for a session cookie first (e.g. from Google OAuth),
* so the real email is used when sharing a DB with production.
* Falls back to { email: "local@localhost" } if no session cookie.
* - In production with built-in auth: returns session if cookie is valid
* - With custom auth (BYOA): delegates to the custom getSession
*/
export async function getSession(event: H3Event): Promise<AuthSession | null> {
if (isDevMode()) return DEV_SESSION;
if (authDisabledMode) return DEV_SESSION;
if (isDevMode() || authDisabledMode) {
// Check for a real session cookie (created by Google OAuth callback)
// so dev and prod share the same identity on the same DB
Comment on lines +206 to +208
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟑 Missing error handling around DB call newly introduced in dev mode

Previously getSession() in dev mode was DB-free; now getSessionEmail(cookie) executes SQL on every request when a cookie is present. A transient DB error at startup will throw an unhandled exception instead of falling back to DEV_SESSION. Wrap in try/catch and return DEV_SESSION on error.


How did I do? React with πŸ‘ or πŸ‘Ž to help me improve.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed β€” wrapped getSessionEmail() in try/catch, falls back to DEV_SESSION on error.

const cookie = getCookie(event, COOKIE_NAME);
if (cookie) {
const email = await getSessionEmail(cookie);
if (email) return { email, token: cookie };
}
return DEV_SESSION;
Comment on lines 203 to +214
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ”΄ /api/auth/session endpoint still returns DEV_SESSION β€” client/server identity split

The autoMountAuth() dev-mode and authDisabledMode handlers for /api/auth/session still unconditionally return DEV_SESSION, so useSession() on the client always shows local@localhost while getSession() on the server returns the real Google email. Fix by having those handlers call await getSession(event) instead of returning DEV_SESSION directly.


How did I do? React with πŸ‘ or πŸ‘Ž to help me improve.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed β€” /api/auth/session endpoints now call getSession(event) instead of returning DEV_SESSION directly.

}

if (customGetSession) return customGetSession(event);

Expand Down
8 changes: 3 additions & 5 deletions templates/mail/server/handlers/emails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,9 @@ async function getAccessToken(accountEmail: string): Promise<string | null> {
async function getAccountTokens(
forEmail?: string,
): Promise<Array<{ email: string; accessToken: string }>> {
// In dev mode (local@localhost), show all accounts regardless of owner
const accounts =
forEmail && forEmail !== "local@localhost"
? await listOAuthAccountsByOwner("google", forEmail)
: await listOAuthAccounts("google");
const accounts = forEmail
? await listOAuthAccountsByOwner("google", forEmail)
: await listOAuthAccounts("google");

const results: Array<{ email: string; accessToken: string }> = [];

Expand Down
6 changes: 2 additions & 4 deletions templates/mail/server/lib/google-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ export async function getClients(
* checks only that specific account.
*/
export async function isConnected(forEmail?: string): Promise<boolean> {
// In dev mode, check all accounts regardless of owner
if (forEmail && forEmail !== "local@localhost") {
if (forEmail) {
const accounts = await listOAuthAccountsByOwner("google", forEmail);
return accounts.length > 0;
Comment on lines 237 to 240
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟑 Mail OAuth callback still stores tokens under local@localhost β€” reads now miss them

The local@localhost bypass was removed from isConnected()/getAuthStatus(), but the OAuth callback still stores newly connected accounts under local@localhost when getSession() returns that identity (no cookie yet). After the connect, isConnected(realEmail) returns false because the token is stored under the wrong owner.


How did I do? React with πŸ‘ or πŸ‘Ž to help me improve.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer an issue β€” getSession() returns real email when cookie exists, so OAuth callback stores tokens under the real email. On first connect there's no cookie yet, but the callback creates the session and sets the cookie. Subsequent requests use the real identity.

}
Expand Down Expand Up @@ -264,8 +263,7 @@ export async function getAuthStatus(
accountId: string;
tokens: Record<string, unknown>;
}>;
// In dev mode (local@localhost), show all accounts regardless of owner
if (forEmail && forEmail !== "local@localhost") {
if (forEmail) {
oauthAccounts = await listOAuthAccountsByOwner("google", forEmail);
} else {
oauthAccounts = await listOAuthAccounts("google");
Expand Down
Loading