Copilot SDK for Java 1.0.7: Session Lifecycle Hooks and Enhanced Observability

Published: (February 5, 2026 at 05:49 PM EST)
3 min read
Source: Dev.to

Source: Dev.to

What’s New in 1.0.7

Session Lifecycle Hooks

The most significant addition in this release is the extended hooks system. While v1.0.6 introduced pre‑tool and post‑tool hooks, v1.0.7 adds three new lifecycle hooks that give you fine‑grained control over session behavior.

onSessionStart

Called when a session starts (whether new or resumed). Use it for initialization, logging, or validation before the session begins processing:

var config = new SessionConfig()
    .setOnSessionStart((input, invocation) -> {
        System.out.println("Session started: " + input.getSessionId());
        // Initialize session‑specific resources
        return CompletableFuture.completedFuture(
            new SessionStartHookOutput()
        );
    });

onSessionEnd

Called when a session ends, enabling cleanup operations, metrics collection, or audit logging:

.setOnSessionEnd((input, invocation) -> {
    Duration sessionDuration = calculateDuration(input);
    metricsCollector.recordSessionDuration(sessionDuration);
    return CompletableFuture.completedFuture(
        new SessionEndHookOutput()
    );
})

onUserPromptSubmitted

Called when the user submits a prompt, allowing you to enrich, validate, or transform prompts before they’re processed:

.setOnUserPromptSubmitted((input, invocation) -> {
    String enrichedPrompt = addProjectContext(input.getPrompt());
    return CompletableFuture.completedFuture(
        new UserPromptSubmittedHookOutput()
            .setUpdatedPrompt(enrichedPrompt)
    );
})

These hooks open up powerful use cases, including:

  • Security gates – Validate tool calls against allow‑lists before execution.
  • Audit logging – Record all tool invocations for compliance.
  • Result enrichment – Add metadata or transform tool outputs.
  • Session analytics – Track session patterns and user behavior.

Client‑Level Lifecycle Events

Beyond session‑scoped hooks, v1.0.7 introduces client‑level lifecycle event subscriptions—useful for applications managing multiple concurrent sessions:

// Subscribe to all lifecycle events
client.onLifecycle(event -> {
    System.out.println("Session " + event.getSessionId() +
        " status: " + event.getType());
});

// Subscribe to specific event types
client.onLifecycle(SessionLifecycleEventTypes.CREATED, event -> {
    initializeSessionResources(event.getSessionId());
});

client.onLifecycle(SessionLifecycleEventTypes.DELETED, event -> {
    cleanupSessionResources(event.getSessionId());
});

Available event types

TypeDescription
CREATEDA new session was created
DELETEDA session was deleted
UPDATEDSession state was updated
FOREGROUNDSession moved to foreground (TUI mode)
BACKGROUNDSession moved to background (TUI mode)

Foreground Session Control

For applications running in TUI + Server mode (copilot --ui-server), v1.0.7 adds APIs to control which session is displayed in the terminal UI:

// Get the currently displayed session
String currentSession = client.getForegroundSessionId();

// Switch the TUI to display a different session
client.setForegroundSessionId(anotherSessionId);

This enables building sophisticated multi‑session orchestrators that can programmatically switch the TUI focus based on user activity or priority.

New Event Types

Two new event types enhance observability:

SessionShutdownEvent

Emitted when a session is shutting down; includes the reason and exit code:

session.on(SessionShutdownEvent.class, event -> {
    System.out.println("Session shutting down: " +
        event.getData().getReason());
});

SkillInvokedEvent

Emitted when a skill is invoked; includes the skill name and invocation context:

session.on(SkillInvokedEvent.class, event -> {
    System.out.println("Skill invoked: " +
        event.getData().getSkillName());
});

Extended Event Data

Several existing events now include additional fields:

EventNew Fields
AssistantMessageEventid, isLastReply, thinkingContent
AssistantUsageEventoutputReasoningTokens
SessionCompactionCompleteEventsuccess, messagesRemoved, tokensRemoved
SessionErrorEventExtended error context

JaCoCo Test Coverage

This release integrates JaCoCo 0.8.14 for test‑coverage reporting. Coverage reports are now:

  • Generated automatically during builds at target/site/jacoco-coverage/.
  • Summarized in GitHub Actions workflow summaries.
  • Uploaded as CI artifacts for detailed analysis.

Documentation Improvements

We’ve significantly expanded the documentation:

  • Session Hooks Guide – Comprehensive guide covering all five hook types with practical examples.
  • Events Reference – Complete documentation of all 33 event types.
  • Advanced Usage – Enhanced with lifecycle events and foreground session control.

Breaking Changes

Details of breaking changes will be listed here. (Add the specific items as needed.)

# Copilot CLI
**Minimum required version updated from 0.0.400 to 0.0.404**

Getting Started

Add the dependency to your pom.xml:

<dependency>
    <groupId>com.github.copilot-community-sdk</groupId>
    <artifactId>copilot-sdk-java</artifactId>
    <version>1.0.7</version>
</dependency>

Or try it instantly with JBang:

jbang https://github.com/copilot-community-sdk/copilot-sdk-java/blob/main/jbang-example.java

What’s Next

We continue to track the official .NET SDK and port new features as they become available. Upcoming priorities include:

  • Additional MCP server integrations
  • Enhanced error recovery patterns
  • Performance optimizations for high‑throughput scenarios

Contributing

The Copilot SDK for Java is a community‑driven project, and we welcome contributions!
Explore the repository and read the contribution guidelines:

Published: February 5, 2026

Back to Blog

Related posts

Read more »

Switch case

Overview - A switch case is a control statement that lets you run different blocks of code based on the value of a variable or expression. - It is often cleane...