Fix plugin job company scope propagation

This commit is contained in:
Paperclip Bot 2026-06-05 08:33:23 +00:00
parent fad3237595
commit 0a56b88fa7
2 changed files with 95 additions and 1 deletions

View file

@ -1502,7 +1502,10 @@ export function startWorkerRpcHost(options: WorkerRpcHostOptions): WorkerRpcHost
if (!handler) { if (!handler) {
throw new Error(`No handler registered for job "${params.job.jobKey}"`); throw new Error(`No handler registered for job "${params.job.jobKey}"`);
} }
await handler(params.job); await runtimeCompanyContext.run(
{ companyId: params.job.companyId ?? null },
() => handler(params.job),
);
} }
async function handleWebhook(params: PluginWebhookInput): Promise<void> { async function handleWebhook(params: PluginWebhookInput): Promise<void> {

View file

@ -514,4 +514,95 @@ describe("startWorkerRpcHost runtime company context", () => {
stdout.destroy(); stdout.destroy();
} }
}); });
it("passes runJob company context into config host calls", async () => {
const stdin = new PassThrough();
const stdout = new PassThrough();
const nextMessage = collectJsonLines(stdout);
const plugin = definePlugin({
async setup(ctx) {
ctx.jobs.register("check-job-context", async () => {
const config = await ctx.config.get();
await ctx.state.set(
{
scopeKind: "instance",
namespace: "job-context",
stateKey: "mode",
},
config.mode,
);
});
},
});
const host = startWorkerRpcHost({ plugin, stdin, stdout });
try {
writeMessage(stdin, {
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {
manifest: { id: "test-plugin", name: "test-plugin", version: "1.0.0" },
config: {},
instanceInfo: { instanceId: "inst-1", hostVersion: "0.0.0-test" },
apiVersion: 1,
},
});
await expect(nextMessage()).resolves.toMatchObject({ id: 1, result: { ok: true } });
writeMessage(stdin, {
jsonrpc: "2.0",
id: 2,
method: "runJob",
params: {
job: {
id: "job-1",
jobKey: "check-job-context",
companyId: "company-1",
payload: {},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
},
},
});
const configRequest = await nextMessage();
expect(configRequest).toMatchObject({
method: "config.get",
params: { companyId: "company-1" },
});
writeMessage(stdin, {
jsonrpc: "2.0",
id: configRequest.id,
result: { mode: "company-config" },
});
const stateRequest = await nextMessage();
expect(stateRequest).toMatchObject({
method: "state.set",
params: {
scopeKind: "instance",
namespace: "job-context",
stateKey: "mode",
value: "company-config",
},
});
writeMessage(stdin, {
jsonrpc: "2.0",
id: stateRequest.id,
result: null,
});
await expect(nextMessage()).resolves.toMatchObject({
id: 2,
result: null,
});
} finally {
host.stop();
stdin.destroy();
stdout.destroy();
}
});
}); });