You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maka.apache.org by xx...@apache.org on 2026/09/09 07:21:55 UTC

(maka) branch feat/plugin-system-prompt-service updated: fix(plugins): enforce scoped prompt and agent context

This is an automated email from the ASF dual-hosted git repository.

xxhZs pushed a commit to branch feat/plugin-system-prompt-service
in repository https://gitbox.apache.org/repos/asf/maka.git


The following commit(s) were added to refs/heads/feat/plugin-system-prompt-service by this push:
     new 29b2690c92 fix(plugins): enforce scoped prompt and agent context
29b2690c92 is described below

commit 29b2690c926fc23116923c0fb397da3493c74e8a
Author: xxhZs <84...@users.noreply.github.com>
AuthorDate: Wed Sep 9 15:21:47 2026 +0800

    fix(plugins): enforce scoped prompt and agent context
---
 .../src/__tests__/interactive-run-composer.test.ts | 19 +++++
 .../src/server/execution-composition.ts            | 11 ++-
 .../src/server/interactive-run-composer.ts         |  1 +
 .../src/__tests__/plugin-agent-service.test.ts     | 39 ++++++++-
 packages/runtime/src/plugin-agent-service.ts       | 98 +++++++++++-----------
 5 files changed, 112 insertions(+), 56 deletions(-)

diff --git a/packages/runtime-host/src/__tests__/interactive-run-composer.test.ts b/packages/runtime-host/src/__tests__/interactive-run-composer.test.ts
index c9ed55aae3..f2cfe2512d 100644
--- a/packages/runtime-host/src/__tests__/interactive-run-composer.test.ts
+++ b/packages/runtime-host/src/__tests__/interactive-run-composer.test.ts
@@ -154,6 +154,25 @@ test('the composer caches the Host base but reassembles scoped Plugin prompts ea
   );
 });
 
+test('the composer preserves scoped dynamic contexts for each model step', async () => {
+  const contexts = [{ name: 'plugin:context', text: 'EPHEMERAL_CONTEXT' }];
+  const composer = createFixtureComposer({
+    resolveAdditionalSystemPrompt: async (_context, baseText) => ({
+      text: baseText,
+      contexts,
+      sourceRevisions: [],
+    }),
+  });
+
+  const prompt = await composer.resolveSystemPrompt({
+    sessionId: 'session',
+    turnId: 'turn',
+    cwd: '/workspace',
+  });
+
+  assert.deepEqual(prompt.contexts, contexts);
+});
+
 test('scoped Plugin Skill contributions join the canonical model inventory', async () => {
   const composer = createFixtureComposer({
     skills: {
diff --git a/packages/runtime-host/src/server/execution-composition.ts b/packages/runtime-host/src/server/execution-composition.ts
index 698bc5c48e..c3fb8254fd 100644
--- a/packages/runtime-host/src/server/execution-composition.ts
+++ b/packages/runtime-host/src/server/execution-composition.ts
@@ -1468,7 +1468,7 @@ export async function createExecutionRuntimeHostComposition(
         | undefined,
     ) => {
       const sessions = await manager!.listSessions();
-      if (!initiator) return sessions;
+      if (!initiator) return [];
       const visible = new Set([initiator.sessionId]);
       let changed = true;
       while (changed) {
@@ -1562,7 +1562,7 @@ export async function createExecutionRuntimeHostComposition(
       id: string,
       message: unknown,
       placement: 'current_turn' | 'next_turn',
-      initiator: import('@maka/runtime/plugin-agent-service').PluginAgentInvocation | undefined,
+      initiator: import('@maka/runtime/plugin-agent-service').PluginAgentInvocation,
     ) => {
       if (!(await visibleAgentSessions(initiator)).some((session) => session.id === id)) {
         throw new Error('Agent is outside the current ownership tree');
@@ -1590,7 +1590,7 @@ export async function createExecutionRuntimeHostComposition(
     };
     pluginAgents.bindRuntime({
       create: async (options, initiator) => {
-        const spawn = initiator?.toolContext?.spawnChildSession;
+        const spawn = initiator.toolContext?.spawnChildSession;
         if (!spawn) throw new Error('Agent creation requires an active Tool invocation');
         if (!options.prompt?.trim()) throw new Error('Agent creation requires a prompt');
         return new Promise((resolve, reject) => {
@@ -1637,7 +1637,10 @@ export async function createExecutionRuntimeHostComposition(
         }
         await coordinator.stopSession(id, { source: 'stop_button' });
       },
-      whenIdle: async (id, signal) => {
+      whenIdle: async (id, signal, initiator) => {
+        if (!(await visibleAgentSessions(initiator)).some((session) => session.id === id)) {
+          throw new Error('Agent is outside the current ownership tree');
+        }
         const wait = coordinator.whenIdle(id);
         if (!wait) return;
         if (!signal) return wait;
diff --git a/packages/runtime-host/src/server/interactive-run-composer.ts b/packages/runtime-host/src/server/interactive-run-composer.ts
index cf41ccd9e9..61303839f8 100644
--- a/packages/runtime-host/src/server/interactive-run-composer.ts
+++ b/packages/runtime-host/src/server/interactive-run-composer.ts
@@ -283,6 +283,7 @@ export function createInteractiveRunComposer(input: InteractiveRunComposerInput)
     const plugin = await input.resolveAdditionalSystemPrompt(context, base.text);
     return Object.freeze({
       text: plugin.text,
+      ...(plugin.contexts ? { contexts: plugin.contexts } : {}),
       sourceRevisions: mergeSourceRevisions(base.sourceRevisions, plugin.sourceRevisions),
     });
   };
diff --git a/packages/runtime/src/__tests__/plugin-agent-service.test.ts b/packages/runtime/src/__tests__/plugin-agent-service.test.ts
index 7738826554..64bfb691b0 100644
--- a/packages/runtime/src/__tests__/plugin-agent-service.test.ts
+++ b/packages/runtime/src/__tests__/plugin-agent-service.test.ts
@@ -66,7 +66,8 @@ test('Agent handles expose the complete control and query surface', async () =>
     },
   };
   agents.bindRuntime(runtime);
-  const agent = await agents.create();
+  const invocation = toolContext('session-a');
+  const agent = await agents.withInvocation(invocation, () => agents.create());
   await agent.followup('next');
   await agent.steer('now');
   await agent.inject('context');
@@ -94,6 +95,42 @@ test('Agent handles expose the complete control and query surface', async () =>
   await root.fiber.dispose();
 });
 
+test('Agent access fails closed without an invocation and handles retain their authority', async () => {
+  const root = new Context();
+  const agents = new PluginAgentService(root);
+  const observed: string[] = [];
+  const descriptor = { id: 'child', sessionId: 'child', root: false };
+  agents.bindRuntime({
+    create: async (_options, initiator) => {
+      observed.push(`create:${initiator.sessionId}`);
+      return descriptor;
+    },
+    resume: async () => descriptor,
+    get: async () => descriptor,
+    list: async () => [descriptor],
+    roots: async () => [],
+    followup: async (_id, _message, initiator) => {
+      observed.push(`followup:${initiator.sessionId}`);
+    },
+    steer: async () => undefined,
+    inject: async () => undefined,
+    cancel: async () => undefined,
+    whenIdle: async () => undefined,
+    snapshot: async () => undefined,
+    inbox: async () => undefined,
+    result: async () => undefined,
+    artifacts: async () => undefined,
+    transcript: async () => undefined,
+    dispose: async () => undefined,
+  });
+
+  await assert.rejects(() => agents.list(), /requires an active Agent invocation/u);
+  const handle = await agents.withInvocation(toolContext('session-a'), () => agents.create());
+  await agents.withInvocation(toolContext('session-b'), () => handle.followup('next'));
+  assert.deepEqual(observed, ['create:session-a', 'followup:session-a']);
+  await root.fiber.dispose();
+});
+
 function toolContext(sessionId: string): MakaToolContext {
   return {
     sessionId,
diff --git a/packages/runtime/src/plugin-agent-service.ts b/packages/runtime/src/plugin-agent-service.ts
index ed5787bcb1..68d2316c8d 100644
--- a/packages/runtime/src/plugin-agent-service.ts
+++ b/packages/runtime/src/plugin-agent-service.ts
@@ -70,41 +70,30 @@ export interface PluginAgentResumeOptions {
 export interface PluginAgentRuntime {
   create(
     options: PluginAgentCreateOptions,
-    initiator: PluginAgentInvocation | undefined,
+    initiator: PluginAgentInvocation,
   ): Promise<PluginAgentDescriptor>;
   resume(
     options: PluginAgentResumeOptions,
-    initiator: PluginAgentInvocation | undefined,
+    initiator: PluginAgentInvocation,
   ): Promise<PluginAgentDescriptor>;
-  get(
+  get(id: string, initiator: PluginAgentInvocation): Promise<PluginAgentDescriptor | undefined>;
+  list(initiator: PluginAgentInvocation): Promise<readonly PluginAgentDescriptor[]>;
+  roots(initiator: PluginAgentInvocation): Promise<readonly PluginAgentDescriptor[]>;
+  followup(id: string, message: unknown, initiator: PluginAgentInvocation): Promise<unknown>;
+  steer(id: string, message: unknown, initiator: PluginAgentInvocation): Promise<unknown>;
+  inject(id: string, message: unknown, initiator: PluginAgentInvocation): Promise<unknown>;
+  cancel(id: string, initiator: PluginAgentInvocation): Promise<unknown>;
+  whenIdle(
     id: string,
-    initiator: PluginAgentInvocation | undefined,
-  ): Promise<PluginAgentDescriptor | undefined>;
-  list(initiator: PluginAgentInvocation | undefined): Promise<readonly PluginAgentDescriptor[]>;
-  roots(initiator: PluginAgentInvocation | undefined): Promise<readonly PluginAgentDescriptor[]>;
-  followup(
-    id: string,
-    message: unknown,
-    initiator: PluginAgentInvocation | undefined,
-  ): Promise<unknown>;
-  steer(
-    id: string,
-    message: unknown,
-    initiator: PluginAgentInvocation | undefined,
-  ): Promise<unknown>;
-  inject(
-    id: string,
-    message: unknown,
-    initiator: PluginAgentInvocation | undefined,
-  ): Promise<unknown>;
-  cancel(id: string, initiator: PluginAgentInvocation | undefined): Promise<unknown>;
-  whenIdle(id: string, signal: AbortSignal | undefined): Promise<void>;
-  snapshot(id: string, initiator: PluginAgentInvocation | undefined): Promise<unknown>;
-  inbox(id: string, initiator: PluginAgentInvocation | undefined): Promise<unknown>;
-  result(id: string, initiator: PluginAgentInvocation | undefined): Promise<unknown>;
-  artifacts(id: string, initiator: PluginAgentInvocation | undefined): Promise<unknown>;
-  transcript(id: string, initiator: PluginAgentInvocation | undefined): Promise<unknown>;
-  dispose(id: string, initiator: PluginAgentInvocation | undefined): Promise<void>;
+    signal: AbortSignal | undefined,
+    initiator: PluginAgentInvocation,
+  ): Promise<void>;
+  snapshot(id: string, initiator: PluginAgentInvocation): Promise<unknown>;
+  inbox(id: string, initiator: PluginAgentInvocation): Promise<unknown>;
+  result(id: string, initiator: PluginAgentInvocation): Promise<unknown>;
+  artifacts(id: string, initiator: PluginAgentInvocation): Promise<unknown>;
+  transcript(id: string, initiator: PluginAgentInvocation): Promise<unknown>;
+  dispose(id: string, initiator: PluginAgentInvocation): Promise<void>;
 }
 
 export interface PluginAgent {
@@ -194,30 +183,35 @@ export class PluginAgentService extends Service {
   }
 
   async create(options: PluginAgentCreateOptions = {}): Promise<PluginAgent> {
-    return this.handle(await this.runtime().create(options, this.currentInvocation()));
+    const invocation = this.requireInvocation();
+    return this.handle(await this.runtime().create(options, invocation), invocation);
   }
 
   async resume(options: PluginAgentResumeOptions): Promise<PluginAgent> {
-    return this.handle(await this.runtime().resume(options, this.currentInvocation()));
+    const invocation = this.requireInvocation();
+    return this.handle(await this.runtime().resume(options, invocation), invocation);
   }
 
   async get(id: string): Promise<PluginAgent | undefined> {
-    const descriptor = await this.runtime().get(assertId(id), this.currentInvocation());
-    return descriptor ? this.handle(descriptor) : undefined;
+    const invocation = this.requireInvocation();
+    const descriptor = await this.runtime().get(assertId(id), invocation);
+    return descriptor ? this.handle(descriptor, invocation) : undefined;
   }
 
   async list(): Promise<readonly PluginAgent[]> {
+    const invocation = this.requireInvocation();
     return Object.freeze(
-      (await this.runtime().list(this.currentInvocation())).map((descriptor) =>
-        this.handle(descriptor),
+      (await this.runtime().list(invocation)).map((descriptor) =>
+        this.handle(descriptor, invocation),
       ),
     );
   }
 
   async roots(): Promise<readonly PluginAgent[]> {
+    const invocation = this.requireInvocation();
     return Object.freeze(
-      (await this.runtime().roots(this.currentInvocation())).map((descriptor) =>
-        this.handle(descriptor),
+      (await this.runtime().roots(invocation)).map((descriptor) =>
+        this.handle(descriptor, invocation),
       ),
     );
   }
@@ -227,25 +221,27 @@ export class PluginAgentService extends Service {
     return this.agentRuntime;
   }
 
-  private handle(descriptor: PluginAgentDescriptor): PluginAgent {
+  private handle(
+    descriptor: PluginAgentDescriptor,
+    invocation: PluginAgentInvocation = this.requireInvocation(),
+  ): PluginAgent {
     const service = this;
     const id = assertId(descriptor.id);
-    const invoke = () => service.currentInvocation();
     return Object.freeze({
       ...descriptor,
       id,
-      followup: (message: unknown) => service.runtime().followup(id, message, invoke()),
-      steer: (message: unknown) => service.runtime().steer(id, message, invoke()),
-      inject: (message: unknown) => service.runtime().inject(id, message, invoke()),
-      cancel: () => service.runtime().cancel(id, invoke()),
+      followup: (message: unknown) => service.runtime().followup(id, message, invocation),
+      steer: (message: unknown) => service.runtime().steer(id, message, invocation),
+      inject: (message: unknown) => service.runtime().inject(id, message, invocation),
+      cancel: () => service.runtime().cancel(id, invocation),
       whenIdle: (signal?: AbortSignal) =>
-        service.runtime().whenIdle(id, signal ?? invoke()?.abortSignal),
-      snapshot: () => service.runtime().snapshot(id, invoke()),
-      inbox: () => service.runtime().inbox(id, invoke()),
-      result: () => service.runtime().result(id, invoke()),
-      artifacts: () => service.runtime().artifacts(id, invoke()),
-      transcript: () => service.runtime().transcript(id, invoke()),
-      dispose: () => service.runtime().dispose(id, invoke()),
+        service.runtime().whenIdle(id, signal ?? invocation.abortSignal, invocation),
+      snapshot: () => service.runtime().snapshot(id, invocation),
+      inbox: () => service.runtime().inbox(id, invocation),
+      result: () => service.runtime().result(id, invocation),
+      artifacts: () => service.runtime().artifacts(id, invocation),
+      transcript: () => service.runtime().transcript(id, invocation),
+      dispose: () => service.runtime().dispose(id, invocation),
     });
   }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@maka.apache.org
For additional commands, e-mail: commits-help@maka.apache.org