Files
plugins/packages/kimi-work/no-magpie.test.mjs
T
yetone 8d687ad519 kimi-work 0.1.10: a menu the web sign-in can't get is asked again without it, so K3.1 Pro stays listed
泠夜Soul (Discord) is on 0.1.9 and still has no K3.1 Pro (k3d1-agent),
with no error and nothing in the log. 0.1.9 was checked only against
the menu signed out.

What was found: provider.models asks the menu (DescribeKimiWorkConfig,
with credentials.kimiWeb.accessToken from Kimi's file) and agent-gw's
/models together, and a failed menu ask was dropped without a word:
the list was then /models' alone, k3-agent and k2d8-preview, which is
exactly the report, on 0.1.7 to 0.1.9 alike. www.kimi.com answers a web
sign-in it doesn't take with 401 "unauthenticated", "invalid user
token" (REASON_INVALID_AUTH_TOKEN), seen on 10-24 with a malformed and
a badly signed token; signed out it answers 200 with the desktop menu.
So a stale kimiWeb token in Kimi's file (the desktop closed, or it
renewed it elsewhere) takes K3.1 Pro away silently. That this is the
reporter's case is not proven: no signed-in account here.

What changed:
- The menu, when the signed-in ask fails (any HTTP status, a timeout,
  or a reply with no models), is asked again without Authorization, as
  the desktop asks it before signing in, with the same x-msh-* headers.
  That reply is the desktop menu, K3.1 Pro first. agent-gw requests and
  usage are unchanged; nothing is retried there.
- Each failure is written to the log (console.warn, so magpie's plugin
  stderr): which ask, and its status or reason, never the token.
- jsonRequest's errors carry the HTTP status, and leaves Authorization
  out when given no token.
- no-magpie.test's fake menu is now the captured desktop menu: an
  empty one is asked twice now, which its request count would miss.

Run at this tree (origin/main df55294 + this change), macOS, bun 1.4.2,
load ~360:
- bun test packages/kimi-work: 51 pass, 0 fail. With index.mjs put back
  to df55294's: 50 pass, 1 fail, the new test (401 / 503 / empty menu
  signed in, desktop menu signed out: expects k3d1-agent "K3.1 Pro" and
  a logged line without the token).
- Live, createPlugin's provider.models with a stale-looking web token
  and a bogus Work token, against the real www.kimi.com and agent-gw,
  darwin and win32: lists k3d1-agent=K3.1 Pro, k2d8-preview, k3-agent
  and logs "DescribeKimiWorkConfig with the web sign-in failed (HTTP
  401); asking it without one". df55294's index.mjs, same script:
  throws the sign-in error (both lists failed, nothing listed).
- HOME=$(mktemp -d) bun test: 2447 pass, 1 fail, layerfs's afterAll
  timing out (#362, layerfs untouched). (A first run with HOME and
  TMPDIR under a path containing "magpie" failed four no-magpie tests
  on that path alone.)
- bun scripts/check.mjs: every plugin ok. git diff --check clean.

Magpie side, read at origin/main 137187a40: a plugin update goes
through Restart, which retires the host and calls changed(), so
forgetProviders drops the cached list and a new host lists again;
keepListed keeps an old list only for a provider that fell back, which
this one never reports. No stale-cache cause there.

Not tried: a signed-in Work account, so whether the reporter's menu
ask fails (401 or otherwise) or succeeds with a menu lacking
k3d1-agent is not known. This release logs which. Not changed: the
desktop interceptor (www.kimi.com's own web bundle, the isDesktop
branch) also sends X-Traffic-Id, x-msh-device-id, x-msh-session-id and
X-Language, and the User-Agent is still Kimi Code's (#363).
2026-10-24 20:09:47 +08:00

59 lines
3.2 KiB
JavaScript

// magpie-community/plugins#353: every request to Kimi went out with the
// User-Agent "Magpie Kimi Work provider/<version>". Nothing sent names the
// app now: agent-gw gets the headers Kimi's own Work client (the daimon)
// sends, and the membership read the same User-Agent.
import {test,expect} from 'bun:test';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {_internal as I} from './index.mjs';
const account={type:'oauth',source:'kimi-work-desktop',uid:'fixture-account'};
const session={uid:'fixture-account',workToken:'synthetic-work-token',webToken:'synthetic-web-token',deviceId:'0f8fad5b-d9cb-469f-a165-70867728950e'};
function record() {
const sent=[];
const fetchImpl=async(url,init={})=>{
const headers=Object.fromEntries(new Headers(init.headers));
sent.push({url:String(url),headers,body:typeof init.body==='string'?init.body:''});
if(String(url).endsWith('/models'))return Response.json({data:[{id:'k3-agent',context_length:262144}]});
// a menu with models: an empty one is asked again without the sign-in
if(String(url).includes('DescribeKimiWorkConfig'))return new Response(fs.readFileSync(new URL('./tests/fixtures/work-config.mac.json',import.meta.url)),{headers:{'Content-Type':'application/json'}});
if(String(url).includes('GetSubscriptionStats'))return Response.json({subscriptionBalance:{amountUsedRatio:0.25}});
return new Response('data: [DONE]\n\n',{headers:{'Content-Type':'text/event-stream'}});
};
return {sent,fetchImpl};
}
test('no request to Kimi names magpie: models, the Work menu, chat and membership',async()=>{
const {sent,fetchImpl}=record();
const p=I.createPlugin({readSession:()=>session,fetchImpl});
const loader=await p.auth.loader(async()=>account);
await p.provider.models({models:{}},{auth:account});
await (await loader.fetch(I.BASE+'/chat/completions',{method:'POST',
headers:{'User-Agent':'magpie/1.0','x-magpie-work-dir':'/w'},
body:JSON.stringify({model:'k3-agent',stream:true,messages:[{role:'user',content:'hi'}]})})).text();
await p.auth.usage(async()=>account);
expect(sent.length).toBe(4);
for(const s of sent)expect(JSON.stringify(s)).not.toMatch(/magpie/i);
for(const s of sent.filter(s=>s.url.startsWith(I.BASE))){
expect(s.headers['user-agent']).toBe('kimi-code-cli/0.36.0');
expect(s.headers['x-msh-platform']).toBe('kimi-code-cli');
expect(s.headers['x-msh-version']).toBe('0.36.0');
expect(s.headers['x-msh-device-id']).toBe(session.deviceId);
}
});
test("the device id is the daimon's own, read and never written",()=>{
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'kimi-work-device-'));
try{
const file=path.join(dir,'config.json');
expect(I.readDeviceId(file)).toBeUndefined();
expect(fs.existsSync(path.join(dir,'runtime'))).toBe(false);
expect(I.workHeaders(undefined)).not.toHaveProperty('X-Msh-Device-Id');
fs.mkdirSync(path.join(dir,'runtime','kimi-code','home'),{recursive:true});
fs.writeFileSync(path.join(dir,'runtime','kimi-code','home','device_id'),'0f8fad5b-d9cb-469f-a165-70867728950e\n');
expect(I.readDeviceId(file)).toBe('0f8fad5b-d9cb-469f-a165-70867728950e');
}finally{fs.rmSync(dir,{recursive:true,force:true});}
});