antigravity 0.1.5: sessionId from the host's session header, past injected preambles (#1017)
sessionOf took the first text of the first user message only, so every
Codex thread (and anything else opening with AGENTS.md or
<environment_context>) sent Antigravity one constant sessionId. It now
hashes the x-magpie-session header magpie's gateway sends plugins
(sessionOf: X-Magpie-Session, else the agent's own session header), with
x-magpie-subagent (Claude Code's agent id) and the first real prompt
added when a subagent is named. With no header it hashes the first user
text that isn't an injected preamble (AGENTS.md, environment_context,
permissions instructions, system-reminder, and the rest of
INJECTED_USER_TEXT). A synthetic magpie-<24hex> id is ignored. A
chat.headers hook passes OpenCode's sessionID on as x-magpie-session.
The header never goes upstream: the plugin builds its own request
headers, and host.js's unsaid strips it too. Version 0.1.5 (dist has
0.1.4).
Run at 7709ff67 on mica (mote was at load 24), temp HOME:
bun test packages/antigravity: 88 pass, 0 fail. bun scripts/check.mjs:
exit 0. Merges cleanly with origin/main edc6dbcd (it is the merge base).
Broken by hand, each red: header ignored (2 tests), preamble skip
removed (1), subagent ignored (1), chat.headers not setting the header
(1), synthetic magpie-<hex> id not filtered (1).
Not tried: a real Antigravity request (fake fetch only); whether Google
treats sessionId as more than cache affinity. Known edges, not
blocking: a first turn whose user messages are all preamble hashes the
preamble, and the next turn with a real prompt gets a new id; inside
magpie, chat.headers sets x-magpie-session to the conversation id (the
agent's own session header first), so a caller's own X-Magpie-Session
loses to the agent's header, as in magpie's affinityID.
Co-authored-by: JJERRYY <17349077+JJERRYY@users.noreply.github.com>
This commit is contained in:
@@ -74,8 +74,10 @@ requestType: "agent", requestId, request}`):
|
||||
|
||||
- The User-Agent is `antigravity/hub/<version> <os>/<arch>`. The version is
|
||||
read from the app's update manifest every 6 hours.
|
||||
- The session id is a number made from the conversation's first user
|
||||
message, so every turn of one conversation is one session.
|
||||
- The session id is a number made from the host's session header when named,
|
||||
else the conversation's first real user prompt (skipping injected
|
||||
`AGENTS.md` and environment preambles), so unrelated conversations that
|
||||
share a preamble don't collide on one session.
|
||||
- Each reply chunk (`{response: …}`) is unwrapped back into Gemini's own.
|
||||
- A `generateContent` request gets the stream read whole.
|
||||
|
||||
|
||||
@@ -63,6 +63,8 @@ const VERSION_TTL = 6 * 60 * 60 * 1000
|
||||
|
||||
// the header magpie names a request's effort in
|
||||
const EFFORT = "x-magpie-effort"
|
||||
const SESSION = "x-magpie-session"
|
||||
const SUBAGENT = "x-magpie-subagent"
|
||||
|
||||
// ---- small helpers ------------------------------------------------------------------
|
||||
|
||||
@@ -1081,27 +1083,37 @@ function shape(req, sent, at, effort) {
|
||||
return req
|
||||
}
|
||||
|
||||
// sessionOf is the session id the app gives a conversation: a number
|
||||
// made of its first thing said
|
||||
function sessionOf(req) {
|
||||
let text = ""
|
||||
const INJECTED_USER_TEXT = /^(?:# AGENTS\.md\b|<(?:environment_context|permissions instructions|system-reminder|external_codex_apps_|subagent[-_]notification|turn_aborted)\b|\{"page_id":|Companion Page snapshot\b)/
|
||||
|
||||
function firstUserText(req) {
|
||||
let first = ""
|
||||
for (const c of req.contents ?? []) {
|
||||
if (c?.role !== "user") continue
|
||||
for (const p of c.parts ?? []) {
|
||||
if (typeof p?.text === "string" && p.text !== "") {
|
||||
text = p.text
|
||||
break
|
||||
}
|
||||
if (typeof p?.text !== "string" || p.text === "") continue
|
||||
const trimmed = p.text.trimStart()
|
||||
if (!trimmed) continue
|
||||
if (!first) first = p.text
|
||||
if (!INJECTED_USER_TEXT.test(trimmed)) return p.text
|
||||
}
|
||||
break
|
||||
}
|
||||
return first
|
||||
}
|
||||
|
||||
// sessionOf is the session id the app gives a conversation: from the host's
|
||||
// session header when named, else the conversation's first real user prompt
|
||||
function sessionOf(req, headers) {
|
||||
const rawSession = headerOf(headers, SESSION).trim()
|
||||
const session = /^magpie-[0-9a-f]{24}$/.test(rawSession) ? "" : rawSession.slice(0, 128)
|
||||
const subagent = headerOf(headers, SUBAGENT).trim().slice(0, 64)
|
||||
let text = session && !subagent ? session : [session, subagent, firstUserText(req)].filter(Boolean).join("\0")
|
||||
if (!text) text = crypto.randomBytes(16).toString("hex")
|
||||
const h = crypto.createHash("sha256").update(text).digest()
|
||||
const n = h.readBigUInt64BE(0) & 0x7fffffffffffffffn
|
||||
return "-" + n.toString()
|
||||
}
|
||||
|
||||
function envelope(req, sent, projectID, credits = false) {
|
||||
function envelope(req, sent, projectID, credits = false, headers = undefined) {
|
||||
return {
|
||||
model: sent,
|
||||
project: projectID,
|
||||
@@ -1109,7 +1121,7 @@ function envelope(req, sent, projectID, credits = false) {
|
||||
userAgent: "antigravity",
|
||||
requestType: "agent",
|
||||
requestId: "agent-" + crypto.randomBytes(6).toString("hex"),
|
||||
request: { ...req, sessionId: sessionOf(req) },
|
||||
request: { ...req, sessionId: sessionOf(req, headers) },
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1208,7 +1220,7 @@ async function complete(s, url, init, magpie) {
|
||||
fetch(`${DAILY}/v1internal:streamGenerateContent?alt=sse`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", Accept: "text/event-stream", Authorization: "Bearer " + tok, "User-Agent": ua },
|
||||
body: JSON.stringify(envelope(req, sent, l.project.id, credits)),
|
||||
body: JSON.stringify(envelope(req, sent, l.project.id, credits, init?.headers)),
|
||||
signal: init?.signal,
|
||||
})
|
||||
let res = await send(false)
|
||||
@@ -1641,6 +1653,11 @@ async function importAccount(inputs) {
|
||||
|
||||
export async function AntigravityAuthPlugin({ client, magpie } = {}) {
|
||||
return {
|
||||
async "chat.headers"(input, output) {
|
||||
if (input?.model?.providerID !== PROVIDER && input?.provider?.info?.id !== PROVIDER) return
|
||||
const s = String(input?.sessionID ?? "").trim()
|
||||
if (s && !/^magpie-[0-9a-f]{24}$/.test(s)) output.headers[SESSION] = s.slice(0, 128)
|
||||
},
|
||||
config: async (cfg) => {
|
||||
cfg.provider ??= {}
|
||||
const was = cfg.provider[PROVIDER] ?? {}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-antigravity-auth",
|
||||
"version": "0.1.4",
|
||||
"version": "0.1.5",
|
||||
"description": "Sign in to Antigravity with a Google account from OpenCode or magpie: Gemini, and the Claude and GPT-OSS models Google hosts for it",
|
||||
"type": "module",
|
||||
"main": "./opencode.mjs",
|
||||
|
||||
@@ -242,3 +242,19 @@ test("the plugin offers the switch, off unless set", async () => {
|
||||
expect(_internal.creditsOf({ paidTier: { availableCredits: [{ creditType: "GOOGLE_ONE_AI", creditAmount: "10", minimumCreditAmountForUsage: "50" }] } })).toBe(false)
|
||||
expect(_internal.creditsOf({ paidTier: { availableCredits: [{ creditType: "GOOGLE_ONE_AI", minimumCreditAmountForUsage: "50" }] } })).toBe(true)
|
||||
})
|
||||
|
||||
test("host session headers reach the envelope's sessionId without leaking upstream", async () => {
|
||||
const seen = fake({ fetchAvailableModels: models, streamGenerateContent: () => sse({ candidates: [] }) })
|
||||
const p = await AntigravityAuthPlugin({})
|
||||
const outA = { headers: {} }
|
||||
const outB = { headers: {} }
|
||||
await p["chat.headers"]({ sessionID: "sess-alpha", model: { providerID: "antigravity" } }, outA)
|
||||
await p["chat.headers"]({ sessionID: "sess-beta", model: { providerID: "antigravity" } }, outB)
|
||||
const l = await p.auth.loader(async () => auth())
|
||||
await l.fetch(URL_("gemini-3-flash"), { method: "POST", headers: outA.headers, body: ask("# AGENTS.md instructions\nshared") })
|
||||
await l.fetch(URL_("gemini-3-flash"), { method: "POST", headers: outB.headers, body: ask("# AGENTS.md instructions\nshared") })
|
||||
const calls = seen.filter((s) => s.name === "streamGenerateContent")
|
||||
expect(calls.length).toBe(2)
|
||||
expect(calls[0].json.request.sessionId).not.toBe(calls[1].json.request.sessionId)
|
||||
expect(calls[0].headers["x-magpie-session"]).toBeUndefined()
|
||||
})
|
||||
|
||||
@@ -136,6 +136,28 @@ test("a conversation keeps its session", () => {
|
||||
expect(env.request.sessionId).toBe(sessionOf(a))
|
||||
})
|
||||
|
||||
test("sessions with shared AGENTS.md preambles or distinct host headers don't collide", () => {
|
||||
const preamble = [
|
||||
{ text: "# AGENTS.md instructions\n\n<INSTRUCTIONS>global</INSTRUCTIONS>" },
|
||||
{ text: "<environment_context>\n <cwd>/tmp</cwd>\n</environment_context>" },
|
||||
]
|
||||
const chatA = { contents: [{ role: "user", parts: preamble }, { role: "user", parts: [{ text: "fix login bug" }] }] }
|
||||
const chatA2 = { contents: [...chatA.contents, { role: "model", parts: [{ text: "looking" }] }] }
|
||||
const chatB = { contents: [{ role: "user", parts: preamble }, { role: "user", parts: [{ text: "add export csv" }] }] }
|
||||
expect(sessionOf(chatA)).toBe(sessionOf(chatA2))
|
||||
expect(sessionOf(chatA)).not.toBe(sessionOf(chatB))
|
||||
|
||||
const s1 = sessionOf(chatA, { "x-magpie-session": "01a1cb56-5f3e-74d1-b7a9-b40be82a2ba4" })
|
||||
const s2 = sessionOf(chatA, { "x-magpie-session": "01a1c9cc-0757-70e3-990f-5a818d52b0cd" })
|
||||
expect(s1).not.toBe(s2)
|
||||
expect(sessionOf(chatA, { "x-magpie-session": "magpie-0123456789abcdef01234567" })).toBe(sessionOf(chatA))
|
||||
|
||||
const subA = sessionOf(chatA, { "x-magpie-session": "01a1cb56-5f3e-74d1-b7a9-b40be82a2ba4", "x-magpie-subagent": "collab_spawn" })
|
||||
const subB = sessionOf(chatB, { "x-magpie-session": "01a1cb56-5f3e-74d1-b7a9-b40be82a2ba4", "x-magpie-subagent": "collab_spawn" })
|
||||
expect(subA).not.toBe(s1)
|
||||
expect(subA).not.toBe(subB)
|
||||
})
|
||||
|
||||
test("an empty text a stream ended on isn't sent back (Claude: text Field required)", () => {
|
||||
const g = { contents: [
|
||||
{ role: "user", parts: [{ text: "read it" }] },
|
||||
|
||||
Reference in New Issue
Block a user