mercury-agent 0.8.3 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/agent/container-runner.ts +6 -3
- package/src/cli/mercury.ts +63 -10
- package/src/storage/pi-auth.ts +1 -1
package/package.json
CHANGED
|
@@ -803,9 +803,11 @@ export class AgentContainerRunner {
|
|
|
803
803
|
input.extraEnv?.ANTHROPIC_OAUTH_TOKEN,
|
|
804
804
|
);
|
|
805
805
|
if (!hasAnthropicKey && this.config.modelProvider === "anthropic") {
|
|
806
|
+
const resolvedAuthPath =
|
|
807
|
+
this.config.authPath ?? path.join(globalDir, "auth.json");
|
|
806
808
|
const piAuth = await getPiAuthCredential({
|
|
807
809
|
provider: this.config.modelProvider,
|
|
808
|
-
authPath:
|
|
810
|
+
authPath: resolvedAuthPath,
|
|
809
811
|
});
|
|
810
812
|
if (piAuth.status === "ok") {
|
|
811
813
|
passthroughEnvPairs.push({
|
|
@@ -819,10 +821,11 @@ export class AgentContainerRunner {
|
|
|
819
821
|
const detail = oauthBlobCorrupt
|
|
820
822
|
? "MERCURY_ANTHROPIC_OAUTH_TOKEN holds a corrupt credential blob — re-provision it or set MERCURY_ANTHROPIC_API_KEY"
|
|
821
823
|
: piAuth.status === "refresh-failed"
|
|
822
|
-
?
|
|
823
|
-
:
|
|
824
|
+
? `Anthropic OAuth refresh failed for ${resolvedAuthPath} — re-authenticate on the host (run mercury auth login from the project directory that owns that file) or set MERCURY_ANTHROPIC_API_KEY`
|
|
825
|
+
: `no Anthropic credential configured (checked ${resolvedAuthPath}) — run mercury auth login or set MERCURY_ANTHROPIC_API_KEY / MERCURY_ANTHROPIC_OAUTH_TOKEN`;
|
|
824
826
|
logger.error(`Refusing to start container: ${detail}`, {
|
|
825
827
|
spaceId: input.spaceId,
|
|
828
|
+
authPath: resolvedAuthPath,
|
|
826
829
|
});
|
|
827
830
|
throw ContainerError.noCredentials(input.spaceId, detail);
|
|
828
831
|
}
|
package/src/cli/mercury.ts
CHANGED
|
@@ -18,6 +18,7 @@ import { basename, dirname, join, resolve } from "node:path";
|
|
|
18
18
|
import { fileURLToPath } from "node:url";
|
|
19
19
|
import { Command } from "commander";
|
|
20
20
|
import { loadConfig, resolveProjectPath } from "../config.js";
|
|
21
|
+
import { mergeRawMercuryConfig } from "../config-file.js";
|
|
21
22
|
import { getCatalogEntryByName } from "../extensions/catalog.js";
|
|
22
23
|
import {
|
|
23
24
|
checkExtensionIndexLoads,
|
|
@@ -100,6 +101,39 @@ function loadEnvFile(envPath: string): Record<string, string> {
|
|
|
100
101
|
return vars;
|
|
101
102
|
}
|
|
102
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Resolve the auth.json path the way the running service does: `.env` values
|
|
106
|
+
* win over process env (mirroring runAction's Object.assign), mercury.yaml
|
|
107
|
+
* sits under both, and MERCURY_AUTH_PATH / runtime.auth_path override the
|
|
108
|
+
* default `<dataDir>/global/auth.json`. Falls back to the defaults if the
|
|
109
|
+
* project config is malformed — auth commands must stay usable to fix it.
|
|
110
|
+
*/
|
|
111
|
+
function resolveAuthJsonPath(): { authPath: string; dataDir: string } {
|
|
112
|
+
const envPath = join(CWD, ".env");
|
|
113
|
+
const envVars = existsSync(envPath) ? loadEnvFile(envPath) : {};
|
|
114
|
+
let authOverride: string | undefined;
|
|
115
|
+
let dataDir = ".mercury";
|
|
116
|
+
try {
|
|
117
|
+
const raw = mergeRawMercuryConfig({ ...process.env, ...envVars }, CWD);
|
|
118
|
+
if (typeof raw.authPath === "string" && raw.authPath) {
|
|
119
|
+
authOverride = raw.authPath;
|
|
120
|
+
}
|
|
121
|
+
if (typeof raw.dataDir === "string" && raw.dataDir) {
|
|
122
|
+
dataDir = raw.dataDir;
|
|
123
|
+
}
|
|
124
|
+
} catch (e) {
|
|
125
|
+
console.warn(
|
|
126
|
+
`[WARN] ${e instanceof Error ? e.message : String(e)} — using default auth path`,
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
authPath: resolveProjectPath(
|
|
131
|
+
authOverride ?? join(dataDir, "global", "auth.json"),
|
|
132
|
+
),
|
|
133
|
+
dataDir,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
103
137
|
function withProjectDb<T>(fn: (db: Db) => T): T {
|
|
104
138
|
const dbPath = join(CWD, getProjectDataDir(CWD), "state.db");
|
|
105
139
|
const db = new Db(dbPath);
|
|
@@ -474,8 +508,7 @@ function doctorAction(): void {
|
|
|
474
508
|
|
|
475
509
|
// 4. AI credentials
|
|
476
510
|
console.log("\nAI Credentials:");
|
|
477
|
-
const
|
|
478
|
-
const authPath = join(CWD, dataDir, "global", "auth.json");
|
|
511
|
+
const { authPath } = resolveAuthJsonPath();
|
|
479
512
|
const hasOAuth = existsSync(authPath);
|
|
480
513
|
const hasApiKey = !!(
|
|
481
514
|
process.env.MERCURY_ANTHROPIC_API_KEY ||
|
|
@@ -601,7 +634,7 @@ function doctorAction(): void {
|
|
|
601
634
|
|
|
602
635
|
// 8. Spaces exist
|
|
603
636
|
console.log("\nSpaces:");
|
|
604
|
-
const dbPath = join(CWD,
|
|
637
|
+
const dbPath = join(CWD, getProjectDataDir(CWD), "state.db");
|
|
605
638
|
if (existsSync(dbPath)) {
|
|
606
639
|
try {
|
|
607
640
|
const db = new Db(dbPath);
|
|
@@ -963,9 +996,31 @@ authCommand
|
|
|
963
996
|
if (!provider) throw new Error(`Unknown provider: ${providerId}`);
|
|
964
997
|
console.log(`\nLogging in to ${provider.name}...`);
|
|
965
998
|
|
|
966
|
-
// Resolve auth.json path
|
|
967
|
-
|
|
968
|
-
const authPath
|
|
999
|
+
// Resolve auth.json path exactly like the running service does
|
|
1000
|
+
// (.env + mercury.yaml, MERCURY_AUTH_PATH / runtime.auth_path override).
|
|
1001
|
+
const { authPath, dataDir } = resolveAuthJsonPath();
|
|
1002
|
+
|
|
1003
|
+
// Credentials are CWD-scoped: a service running from another directory
|
|
1004
|
+
// reads its own <project>/<dataDir>/global/auth.json and will never see
|
|
1005
|
+
// tokens saved here. Warn before silently creating a stray .mercury tree.
|
|
1006
|
+
// A bare data dir doesn't count as a project marker — a prior misplaced
|
|
1007
|
+
// login creates exactly that; state.db only exists where Mercury has run.
|
|
1008
|
+
const looksLikeMercuryProject =
|
|
1009
|
+
existsSync(join(CWD, "mercury.yaml")) ||
|
|
1010
|
+
existsSync(join(CWD, ".env")) ||
|
|
1011
|
+
existsSync(join(CWD, dataDir, "state.db"));
|
|
1012
|
+
if (!looksLikeMercuryProject) {
|
|
1013
|
+
console.warn(
|
|
1014
|
+
`\n⚠ ${CWD} does not look like a Mercury project (no mercury.yaml, .env, or ${dataDir}/state.db).`,
|
|
1015
|
+
);
|
|
1016
|
+
console.warn(
|
|
1017
|
+
` Credentials will be saved to ${authPath} — a Mercury service running from a different directory will NOT read them.`,
|
|
1018
|
+
);
|
|
1019
|
+
console.warn(
|
|
1020
|
+
" If you meant to re-authenticate a running service, re-run this command from that project's directory.",
|
|
1021
|
+
);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
969
1024
|
const authDir = dirname(authPath);
|
|
970
1025
|
if (!existsSync(authDir)) {
|
|
971
1026
|
mkdirSync(authDir, { recursive: true });
|
|
@@ -1069,8 +1124,7 @@ authCommand
|
|
|
1069
1124
|
.command("logout [provider]")
|
|
1070
1125
|
.description("Remove saved OAuth credentials for a provider")
|
|
1071
1126
|
.action(async (providerArg?: string) => {
|
|
1072
|
-
const
|
|
1073
|
-
const authPath = join(CWD, dataDir, "global", "auth.json");
|
|
1127
|
+
const { authPath } = resolveAuthJsonPath();
|
|
1074
1128
|
|
|
1075
1129
|
if (!existsSync(authPath)) {
|
|
1076
1130
|
console.log("No credentials found.");
|
|
@@ -1113,8 +1167,7 @@ authCommand
|
|
|
1113
1167
|
.action(async () => {
|
|
1114
1168
|
const { getOAuthProviders } = await import("@earendil-works/pi-ai/oauth");
|
|
1115
1169
|
|
|
1116
|
-
const
|
|
1117
|
-
const authPath = join(CWD, dataDir, "global", "auth.json");
|
|
1170
|
+
const { authPath } = resolveAuthJsonPath();
|
|
1118
1171
|
|
|
1119
1172
|
let authData: Record<string, { type?: string; expires?: number }> = {};
|
|
1120
1173
|
if (existsSync(authPath)) {
|
package/src/storage/pi-auth.ts
CHANGED
|
@@ -96,7 +96,7 @@ export async function getPiAuthCredential(options: {
|
|
|
96
96
|
return { status: "ok", apiKey: result.apiKey };
|
|
97
97
|
} catch (error) {
|
|
98
98
|
logger.warn(
|
|
99
|
-
|
|
99
|
+
`Failed to load anthropic oauth token from pi auth.json at ${authPath}`,
|
|
100
100
|
error instanceof Error ? error : undefined,
|
|
101
101
|
);
|
|
102
102
|
return {
|