Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(tool-execution): pass the run id to device commands (#22046)

Arvin Xu4bd3b9ec793a
diff --git a/apps/server/src/services/toolExecution/__tests__/preprocessLhCommand.test.ts b/apps/server/src/services/toolExecution/__tests__/preprocessLhCommand.test.ts
index 2d72db98c17..abbe70271a4 100644
--- a/apps/server/src/services/toolExecution/__tests__/preprocessLhCommand.test.ts
+++ b/apps/server/src/services/toolExecution/__tests__/preprocessLhCommand.test.ts
@@ -430,15 +430,26 @@ describe('isDirectLhInvocation', () => {
describe('buildDeviceLhEnv', () => {
it('scopes the run to its workspace', () => {
- expect(buildDeviceLhEnv('ws-1')).toEqual({ LOBEHUB_WORKSPACE_ID: 'ws-1' });
+ expect(buildDeviceLhEnv({ workspaceId: 'ws-1' })).toEqual({ LOBEHUB_WORKSPACE_ID: 'ws-1' });
});
it('never ships the caller JWT onto the device', () => {
- expect(buildDeviceLhEnv('ws-1')).not.toHaveProperty('LOBEHUB_JWT');
+ expect(buildDeviceLhEnv({ workspaceId: 'ws-1' })).not.toHaveProperty('LOBEHUB_JWT');
});
- it('returns undefined for personal runs', () => {
- expect(buildDeviceLhEnv(undefined)).toBeUndefined();
+ it('returns undefined when there is neither a workspace nor a run id', () => {
+ expect(buildDeviceLhEnv({})).toBeUndefined();
+ });
+
+ // Regression: only the workspace scope travelled to the device, so `lh goal
+ // plan` there failed with "Current manager operation ID required" and a goal
+ // whose manager ran on a device never left planning.
+ it('passes the run id, in personal runs too', () => {
+ expect(buildDeviceLhEnv({ operationId: 'op-1' })).toEqual({ LOBEHUB_OPERATION_ID: 'op-1' });
+ expect(buildDeviceLhEnv({ operationId: 'op-1', workspaceId: 'ws-1' })).toEqual({
+ LOBEHUB_OPERATION_ID: 'op-1',
+ LOBEHUB_WORKSPACE_ID: 'ws-1',
+ });
});
// Regression: this used to be gated on `isLhCommand(command)`, so an `lh`
@@ -448,7 +459,7 @@ describe('buildDeviceLhEnv', () => {
it('scopes commands that reach lh indirectly, which no detector could match', () => {
for (const command of ['make deploy', './sync.sh', 'npm run sync']) {
expect(isLhCommand(command)).toBe(false);
- expect(buildDeviceLhEnv('ws-1')).toEqual({ LOBEHUB_WORKSPACE_ID: 'ws-1' });
+ expect(buildDeviceLhEnv({ workspaceId: 'ws-1' })).toEqual({ LOBEHUB_WORKSPACE_ID: 'ws-1' });
}
});
});
diff --git a/apps/server/src/services/toolExecution/preprocessLhCommand.ts b/apps/server/src/services/toolExecution/preprocessLhCommand.ts
index 604a5b5198d..0dbb3ee2f55 100644
--- a/apps/server/src/services/toolExecution/preprocessLhCommand.ts
+++ b/apps/server/src/services/toolExecution/preprocessLhCommand.ts
@@ -57,34 +57,52 @@ export const isDirectLhInvocation = (command: string): boolean =>
LH_DIRECT_INVOCATION_PATTERN.test(command);
/**
- * Env overrides for a workspace run executing ON THE USER'S DEVICE rather than
- * in the sandbox.
+ * Env overrides for a run executing ON THE USER'S DEVICE rather than in the
+ * sandbox.
*
* A device shell has its own `lh` and its own stored credentials, so nothing
* needs rewriting there β€” but without `LOBEHUB_WORKSPACE_ID` the CLI resolves
* to personal scope, and a workspace agent asked to edit itself silently reads
* and writes the wrong tenancy instead of failing.
*
- * Applied to EVERY command of a workspace run, deliberately not gated on
- * `isLhCommand`. The device merges this into the spawned process environment,
- * so every descendant inherits it β€” which is the only way to reach an `lh` the
- * command invokes indirectly: `bash -lc 'lh whoami'`, a shell script, a
- * Makefile target, an npm script. Command-position detection sees none of
- * those, and unlike the sandbox path there is nothing here worth gating: this
- * mints no credential, it exports a non-secret scope id that only the LobeHub
- * CLI reads, so setting it on a command that never calls `lh` costs nothing.
+ * `LOBEHUB_OPERATION_ID` names the run the command belongs to, as every other
+ * runtime already does. Without it the commands that act for the current run
+ * cannot work on a device at all: `lh goal plan` / `lh goal report` refuse
+ * outright ("Current manager operation ID required"), so a goal whose manager
+ * runs on a device never leaves planning, and `lh doc`, `lh notify`,
+ * `lh topic send` and `lh agent run` lose their attribution.
+ *
+ * Applied to EVERY command, deliberately not gated on `isLhCommand`. The device
+ * merges this into the spawned process environment, so every descendant
+ * inherits it β€” which is the only way to reach an `lh` the command invokes
+ * indirectly: `bash -lc 'lh whoami'`, a shell script, a Makefile target, an npm
+ * script. Command-position detection sees none of those, and unlike the sandbox
+ * path there is nothing here worth gating: this mints no credential, it exports
+ * non-secret ids that only the LobeHub CLI reads, so setting them on a command
+ * that never calls `lh` costs nothing.
*
* `LOBEHUB_JWT` is deliberately NOT sent: on a personal device the stored
* credentials already are the caller's, so it buys nothing, while a workspace
* device belongs to another member and shipping the caller's token onto their
* machine would be a real credential leak. Auth stays with the device; only the
- * scope travels. A device owner who is not a member of that workspace now gets
- * an explicit error rather than a silent personal-scope write.
+ * scope and the run id travel. A device owner who is not a member of that
+ * workspace now gets an explicit error rather than a silent personal-scope
+ * write.
*/
-export const buildDeviceLhEnv = (
- workspaceId: string | undefined,
-): Record<string, string> | undefined =>
- workspaceId ? { LOBEHUB_WORKSPACE_ID: workspaceId } : undefined;
+export const buildDeviceLhEnv = ({
+ operationId,
+ workspaceId,
+}: {
+ operationId?: string;
+ workspaceId?: string;
+}): Record<string, string> | undefined => {
+ const env: Record<string, string> = {
+ ...(workspaceId && { LOBEHUB_WORKSPACE_ID: workspaceId }),
+ ...(operationId && { LOBEHUB_OPERATION_ID: operationId }),
+ };
+
+ return Object.keys(env).length > 0 ? env : undefined;
+};
/** Shell variable holding the per-command directory the `lh` wrapper is written to. */
const LH_SHIM_DIR_VAR = '__lobehub_lh_bin';
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/localSystem.test.ts b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/localSystem.test.ts
index 7c15bb6e18d..b7caef7e13f 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/localSystem.test.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/localSystem.test.ts
@@ -558,6 +558,28 @@ describe('localSystemRuntime', () => {
expect(parseArgs()).toEqual({ command: 'lh agent list' });
});
+ // Regression: only the workspace scope travelled, so `lh goal plan` on the
+ // device failed with "Current manager operation ID required" and a goal
+ // managed from a device never left planning.
+ it('passes the run id to the device, in personal runs too', async () => {
+ const proxy = buildProxy({ operationId: 'op-1' });
+ await proxy[LocalSystemApiName.runCommand]({
+ command: 'lh goal plan goal_1 --file plan.json',
+ });
+
+ expect(parseArgs().env).toEqual({ LOBEHUB_OPERATION_ID: 'op-1' });
+ });
+
+ it('passes the run id alongside the workspace scope', async () => {
+ const proxy = buildProxy({ operationId: 'op-1', workspaceId: 'ws-42' });
+ await proxy[LocalSystemApiName.runCommand]({ command: 'npm run sync' });
+
+ expect(parseArgs().env).toEqual({
+ LOBEHUB_OPERATION_ID: 'op-1',
+ LOBEHUB_WORKSPACE_ID: 'ws-42',
+ });
+ });
+
it('lets a model-supplied env win', async () => {
const proxy = buildProxy({ workspaceId: 'ws-42' });
await proxy[LocalSystemApiName.runCommand]({
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/skills.test.ts b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/skills.test.ts
index bf2eeb0736c..d5b19a02bae 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/skills.test.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/skills.test.ts
@@ -590,7 +590,7 @@ describe('skillsRuntime', () => {
});
const [, toolCall] = mocks.executeToolCall.mock.calls.at(-1)!;
- expect(JSON.parse(toolCall.arguments).env).toEqual({ LOBEHUB_WORKSPACE_ID: 'workspace-1' });
+ expect(mocks.buildDeviceLhEnv.mock.calls[0][0]).toMatchObject({ operationId: 'op-1' });
// Auth stays with the device β€” the caller's token must not travel there.
expect(JSON.parse(toolCall.arguments).env).not.toHaveProperty('LOBEHUB_JWT');
});
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/localSystem.ts b/apps/server/src/services/toolExecution/serverRuntimes/localSystem.ts
index 0ad091607f8..e76ff81ef43 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/localSystem.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/localSystem.ts
@@ -128,11 +128,15 @@ export const localSystemRuntime: ServerRuntimeRegistration = {
// CLI would resolve to the device credentials' PERSONAL scope, which is
// how a workspace agent ends up unable to find (or edit) itself. Set on
// every command, so an `lh` reached indirectly (`bash -lc 'lh …'`, a
- // script, a Makefile) inherits the scope too. The model's own `env`
- // wins: it may be deliberately overriding the scope.
+ // script, a Makefile) inherits the scope too. The run id travels with
+ // it: `lh goal plan` and the other run-attributed commands need it. The
+ // model's own `env` wins: it may be deliberately overriding the scope.
if (api.name === LocalSystemApiName.runCommand && typeof finalArgs?.command === 'string') {
finalArgs = { ...finalArgs, topicId: context.topicId, agentId: context.agentId };
- const lhEnv = buildDeviceLhEnv(await getContentWorkspaceId());
+ const lhEnv = buildDeviceLhEnv({
+ operationId: context.operationId,
+ workspaceId: await getContentWorkspaceId(),
+ });
if (lhEnv) finalArgs = { ...finalArgs, env: { ...lhEnv, ...finalArgs.env } };
// The sandbox decision belongs to the run's owner, not to the model:
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/skills.ts b/apps/server/src/services/toolExecution/serverRuntimes/skills.ts
index 6c08f427173..2f99664455f 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/skills.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/skills.ts
@@ -577,7 +577,10 @@ class SkillServerRuntimeService implements SkillRuntimeService {
// Content scope, NOT the gateway-addressing scope resolved above: a
// workspace agent routed to the caller's own machine is still editing
// workspace content.
- const deviceLhEnv = buildDeviceLhEnv(await this.resolveWorkspaceId());
+ const deviceLhEnv = buildDeviceLhEnv({
+ operationId: device.operationId,
+ workspaceId: await this.resolveWorkspaceId(),
+ });
const response = await executeAuthorizedDeviceToolCall(
this.serverDB,
{
@@ -595,7 +598,7 @@ class SkillServerRuntimeService implements SkillRuntimeService {
command,
...(cwd && { cwd }),
// Keep `lh` on the device in this run's workspace instead of the
- // device credentials' personal scope.
+ // device credentials' personal scope, and tell it which run it acts for.
...(deviceLhEnv && { env: deviceLhEnv }),
...(device.executionTimeoutMs && { timeout: device.executionTimeoutMs }),
}),