agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Feature]: Add framework-level feedback context and memory calibration extension points

Open wahllllll opened this issue 2 months ago • 3 comments

Feature request

Problem

We are building a production application on AgentScope Java 2.0.3 with HarnessAgent. We need to collect thumbs up or thumbs down feedback for individual assistant messages. A dislike may include a reason and an explicit correction supplied by the user. The goal is to improve future responses and eventually calibrate long-term memory without allowing an incorrect response to become a durable memory.

There is currently no product-level API that models message feedback and feeds approved feedback back into runtime context. The training FeedbackRequest type is not a suitable online product abstraction for this use case.

Constraints

Feedback must not be sent as a synthetic user message. It must not start another HarnessAgent run or acquire the current session generation lock. The application should persist feedback asynchronously and make approved, bounded feedback context available during a later invocation. The normal memory flush and consolidation lifecycle should remain owned by AgentScope.

Directly editing memory files or automatically overwriting memory after one dislike would be unsafe. Feedback text is untrusted user input and needs scope isolation, size limits, expiry, conflict handling, approval, and revocation.

Proposed extension

Could AgentScope provide an optional generic extension point similar to the following conceptual API?

public interface FeedbackContextProvider {
    List<FeedbackContext> getContexts(FeedbackContextRequest request);
}

public record FeedbackContextRequest(
        String userId,
        String tenantId,
        String agentId,
        String sessionId,
        int maxItems,
        int maxTokens) {}

A framework-supplied middleware could call the provider during the system prompt phase, append a bounded structured context, and degrade to a no-op when the provider fails. The application would own the database, HTTP endpoint, audit workflow, approval policy, and storage implementation. Only application-approved contexts would be returned.

The extension should ideally support:

  • user and agent scoped context
  • maximum item and token budgets
  • expiration and versioning
  • no-op behavior when no provider is configured
  • no blocking of the main agent invocation
  • clear placement in core, harness, or extensions

Alternatives considered

  1. Send feedback as a synthetic user message. This pollutes the transcript and can corrupt memory extraction.
  2. Start an isolated agent thread for every feedback action. This adds unnecessary sessions, loses the original execution semantics, and still does not define safe memory governance.
  3. Keep everything application-specific. This works locally, but a small generic provider and middleware contract could be useful for other AgentScope applications without imposing a database or UI model.

Relationship to existing memory work

This proposal does not replace MemoryConsolidator or the HarnessAgent memory cleanup work discussed in #2930. It is an upstream extension point for external feedback context. The application can use the official memory configuration and maintenance lifecycle while keeping feedback candidates in an application-owned review store.

Questions

  1. Is feedback context and memory calibration an appropriate framework-level extension point?
  2. Should the provider and middleware live in agentscope-core, agentscope-harness, or an extension module?
  3. Is RuntimeContext the right place to obtain user, tenant, agent, and session scope?
  4. Should this be related to the existing training feedback DTO, or remain a separate online runtime concept?

We would be happy to implement a small test-driven PR after maintainers confirm the API boundary and module placement. The proposed PR would contain only generic interfaces, a default no-op implementation, middleware behavior, tests, and documentation. It would not include application database tables, REST APIs, UI, or tenant-specific policy.

Environment

  • AgentScope Java 2.0.3
  • HarnessAgent
  • Java 17

wahllllll avatar Sep 21 '26 04:09 wahllllll

Issue Evaluation

Category: enhancement | Status: Evaluated — no new framework hook needed; module placement is a maintainer call (verified against main @ 8ca0080)

Your four questions, answered from the code.

  1. Is a new extension point needed? No — the seam you describe already exists. MiddlewareBase exposes a dedicated system-prompt pipeline hook, onSystemPrompt(Agent, RuntimeContext, String) -> Mono<String> (agentscope-core/src/main/java/io/agentscope/core/middleware/MiddlewareBase.java:158), applied once per call() via ReActAgent.seedSystemMsg → applySystemPromptMiddlewares (agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java:761-808). It is registered with ReActAgent.Builder.middleware(...) (ReActAgent.java:4963) / HarnessAgent.Builder.middleware(...) (HarnessAgent.java:1602), ordered by order() descending. So an application-owned FeedbackContextMiddleware implements MiddlewareBase needs zero framework changes today — that is the "Alternatives considered #3" path, and it is fully supported rather than a workaround. Existing first-party precedents for exactly this shape: TaskReminderMiddleware, DynamicSkillMiddleware (core) and WorkspaceContextMiddleware, PlanModeMiddleware (harness).

  2. Where would a first-party version live? Determinable from the current layering: the interface + default no-op belongs in agentscope-core (io.agentscope.core.middleware) only if the project wants to own it, while concrete provider implementations follow the established pattern of agentscope-extensions-mem/* (Mem0 / Bailian / ReMe live outside core and implement the core LongTermMemory SPI). If the intent is to compose with MEMORY.md and workspace budgeting, agentscope-harness is the natural host, since the memory lifecycle you explicitly want to stay framework-owned (MemoryFlushMiddleware → MemoryFlushManager → MemoryConsolidator) is harness-only and is not a prompt-injection point.

  3. Is RuntimeContext the right scope source? Partially. userId and sessionId are first-class (RuntimeContext.getUserId() / getSessionId()); agentId is reachable from the Agent argument passed into the hook, not from the context. tenantId is absent — it only exists if an application puts it in the attribute bag (put/get) or via an extension customizer (e.g. the agent-protocol RuntimeContextCustomizer). As written, FeedbackContextRequest therefore over-promises on one of its four fields; a first-party record should either drop tenantId or define how it is resolved.

  4. Relationship to the training FeedbackRequest? Keep it separate. io.agentscope.core.training.backend.dto.FeedbackRequest (msgIds, reward, taskId, runId, in agentscope-extensions-training) is consumed only by TrinityClient.feedback(...) in the offline RL pipeline — it has no online prompt path and does not carry corrections or approval state. The nearest online analogue is the core LongTermMemory SPI (record(List<Msg>) / retrieve(Msg)), which your proposal deliberately is not, because you need "reviewed, bounded, expiring context" rather than "durable memory write".

Design blockers a first-party SPI would have to settle (all concrete, none fatal):

  • Failure isolation. applySystemPromptMiddlewares composes with flatMap and has no onErrorResume, so a throwing provider currently fails the whole agent call. Your "degrade to a no-op when the provider fails" requirement has to be implemented inside the middleware, not assumed from the pipeline.
  • Budget enforcement. The hook receives no token budget; maxItems / maxTokens must be enforced by the middleware itself. WorkspaceContextMiddleware.onSystemPrompt (estimateTokens + maxContextTokens trimming) is the only in-repo precedent to copy.
  • Blocking I/O. The hook runs on the call path once per call(); a provider doing DB/HTTP calls must subscribeOn(Schedulers.boundedElastic()) the same way WorkspaceContextMiddleware and MemoryFlushMiddleware do.
  • Untrusted text. Approval/revocation is your side's job (correctly), but the framework would also need a delimiter/escaping convention for correction text entering the system prompt — prompt-injection surface is not addressed anywhere today.

Feasibility: feasible, low-risk, additive. Compatibility: non-breaking as long as the default is "no provider configured → no-op". Priority: low-medium for the framework — the capability gap is real, but there is already a supported path for it, so the question is whether the project wants to standardize the contract, not whether users are blocked.

No bot-generated PR here: the only framework-visible decision is whether to adopt a first-party SPI at all (and where), which is a maintainer call. If maintainers want a starter patch (interface + no-op default + middleware + tests + docs, exactly as you scoped it), a maintainer can reply /approve on this issue and it will be generated against the points above.

@wahllllll — you offered to send a test-driven PR; the shortest unblocked route today is the app-owned middleware, so you can prototype without waiting on this decision.


Automated evaluation by github-manager-bot

oss-maintainer avatar Sep 21 '26 06:09 oss-maintainer

Status: 72 h feedback window expired — no automated PR will be generated, the proposal window here is closed

The evaluation CTA above (5756146304, posted 2026-09-21T06:11:46Z) reached its 72-hour feedback limit at 2026-09-24T06:11:46Z with no own-line /approve, /revise, or /reject command in the thread, so per our workflow we skip PR generation and no competing PR will be filed from this proposal window. No action is needed from anyone.

This is a timing stand-down, not a verdict on the idea. The evaluation stands as recorded: the seam you asked about already exists today — MiddlewareBase.onSystemPrompt(Agent, RuntimeContext, String) (agentscope-core/src/main/java/io/agentscope/core/middleware/MiddlewareBase.java:158), applied per call() through ReActAgent.seedSystemMsg → applySystemPromptMiddlewares and registered via ReActAgent.Builder.middleware(...) / HarnessAgent.Builder.middleware(...). An application-owned FeedbackContextMiddleware therefore needs zero framework changes, with TaskReminderMiddleware / WorkspaceContextMiddleware / PlanModeMiddleware as first-party shapes to copy.

What remains genuinely undecided is the framework-level question this issue posed — whether the project wants to own a first-party contract for "reviewed, bounded, expiring" context injection, and in which module. That is a maintainer call, not a gap we can close by filing a PR: a starter SPI (interface + no-op default + middleware + tests + docs) would still have to settle the four points named in the evaluation, none of which are answered by the existing hook alone:

  • Failure isolation — applySystemPromptMiddlewares composes with flatMap and has no onErrorResume, so a throwing provider fails the whole agent call; "degrade to a no-op" must live inside the middleware.
  • Budget enforcement — the hook receives no token budget; maxItems / maxTokens must be enforced by the middleware, with WorkspaceContextMiddleware (estimateTokens + maxContextTokens) the only in-repo precedent.
  • Blocking I/O — a provider doing DB/HTTP work must subscribeOn(Schedulers.boundedElastic()) the way WorkspaceContextMiddleware and MemoryFlushMiddleware do.
  • Untrusted text — there is no delimiter/escaping convention for correction text entering the system prompt; the prompt-injection surface is unaddressed today.

If a maintainer decides the project should own the contract after all, a one-line approve reply on this issue is enough to have the spec regenerated and a PR drafted against the four points above; the window closing now only means the bot stops waiting. In the meantime the app-owned middleware path is unblocked and can be prototyped without waiting on this decision.

Automated by github-manager-bot

oss-maintainer avatar Sep 24 '26 06:09 oss-maintainer

Closing this: the evaluation above concluded that no new framework hook is needed, and the 72-hour proposal window has expired. Happy to reopen if a maintainer wants to revisit the module-placement question.

wahllllll avatar Oct 27 '26 01:10 wahllllll