You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2026/09/20 14:21:52 UTC

(skywalking-ai-sessionizer) 22/23: Carry the file-change half end to end in a test, not by hand

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

wu-sheng pushed a commit to branch langchain-harness
in repository https://gitbox.apache.org/repos/asf/skywalking-ai-sessionizer.git

commit a935d6eb9c288ed1f38f2a759a8ea10ccad34617
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Sun Sep 20 22:01:51 2026 +0800

    Carry the file-change half end to end in a test, not by hand
    
    Every wall in that path was found by running the pieces together and none of
    them by a unit test: the recorder skipped a tool with no shell command, its
    name could not be listed, its data directory could only be named by a Claude
    Code variable, and discovery read only Claude Code's directory layout. I found
    the last one by writing documentation, after two live checks had passed — they
    only passed because I happened to nest the two paths.
    
    So the test does what a deployment does. It builds the recorder, drives it with
    the neutral vocabulary around a real file write, collects with the changes
    adapter, and asserts the records landed under the session name the receiver
    derives for that same conversation — the one place two derivations must agree,
    where drift means records nobody assembles and nothing saying so.
    
    It also asserts an excluded tool leaves no record, which the test that existed
    could not: that one gave both tools the same call id and then used the id as
    proof nothing was written.
    
    Checked against the bug it was written for: reverting the discovery change
    fails it with '0 sessions discovered'.
---
 tests/changes_join_test.go | 162 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 162 insertions(+)

diff --git a/tests/changes_join_test.go b/tests/changes_join_test.go
new file mode 100644
index 0000000..2f3ecc4
--- /dev/null
+++ b/tests/changes_join_test.go
@@ -0,0 +1,162 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package tests_test
+
+import (
+	"encoding/json"
+	"os"
+	"os/exec"
+	"path/filepath"
+	"strings"
+	"testing"
+
+	"github.com/apache/skywalking-ai-sessionizer/internal/adapters/claudecodechanges"
+	"github.com/apache/skywalking-ai-sessionizer/internal/adapters/langsmith"
+	"github.com/apache/skywalking-ai-sessionizer/internal/storage"
+	"github.com/apache/skywalking-ai-sessionizer/pkg/sessiondata"
+)
+
+// TestAToolCallsChangesReachTheSameConversation carries the file-change half
+// the whole way, for a runtime that is not Claude Code.
+//
+// Everything in it was a wall at some point, and none of them showed up in a
+// unit test: the recorder skipped a tool with no shell command, its name could
+// not be listed, its data directory could only be named by a Claude Code
+// variable, and discovery read only Claude Code's directory layout. Each was
+// found by running the pieces together, which is what this does.
+//
+// The join is the point. The receiver lands the conversation under a session
+// name it derives, the recorder writes change records under a session name the
+// integration derives, and if those two ever differ the records go somewhere
+// nobody assembles and nothing says so.
+func TestAToolCallsChangesReachTheSameConversation(t *testing.T) {
+	recorder := build(t)
+	root := t.TempDir()
+	data := filepath.Join(root, "changes")   // what ASZ_CHANGES_DATA would be
+	work := filepath.Join(root, "workspace") // what ASZ_WATCH would be
+	for _, d := range []string{data, work} {
+		if err := os.MkdirAll(d, 0o755); err != nil {
+			t.Fatal(err)
+		}
+	}
+	// A tool the application named, which nobody could have listed in advance.
+	if err := os.WriteFile(filepath.Join(data, "settings.yaml"),
+		[]byte("tools:\n  scope: [\"*\"]\n  exclude: [\"re:^read_\"]\n"), 0o644); err != nil {
+		t.Fatal(err)
+	}
+
+	// The session the receiver would land this conversation under, derived the
+	// way the shim derives it.
+	owner, ok := langsmith.DefaultOwnership().Owner("tsb-advisor",
+		map[string]any{"thread_id": "the-conversation"})
+	if !ok {
+		t.Fatal("no owner")
+	}
+	session := langsmith.StorageID(owner)
+
+	event := func(name, tool string) string {
+		payload, _ := json.Marshal(map[string]any{
+			"hook_event_name": name, "session_id": session, "cwd": work,
+			"tool_name": tool, "tool_use_id": "call_write_report_5",
+			"tool_input": map[string]any{"cluster": "prod-1"},
+		})
+		return string(payload)
+	}
+	run := func(payload string) {
+		t.Helper()
+		cmd := exec.Command(recorder, "hook")
+		cmd.Stdin = strings.NewReader(payload)
+		cmd.Env = append(os.Environ(), "ASZ_CHANGES_DATA="+data)
+		if out, err := cmd.CombinedOutput(); err != nil {
+			t.Fatalf("recorder: %v: %s", err, out)
+		}
+	}
+
+	run(event("tool.begin", "write_report"))
+	if err := os.WriteFile(filepath.Join(work, "findings.md"),
+		[]byte("# Findings\n"), 0o644); err != nil {
+		t.Fatal(err)
+	}
+	run(event("tool.end", "write_report"))
+
+	// A tool the exclusions name changes a file and must leave no record.
+	run(event("tool.begin", "read_status"))
+	if err := os.WriteFile(filepath.Join(work, "ignored.txt"), []byte("x\n"), 0o644); err != nil {
+		t.Fatal(err)
+	}
+	run(event("tool.end", "read_status"))
+
+	zone := storage.NewZone(filepath.Join(root, "data"))
+	stats, err := claudecodechanges.New(data, zone, 2<<20).CollectAll(nil)
+	if err != nil {
+		t.Fatalf("collect: %v", err)
+	}
+	if stats.Sessions != 1 {
+		t.Fatalf("%d sessions discovered, want 1 — the records went somewhere nobody reads",
+			stats.Sessions)
+	}
+
+	// The records landed under the session the conversation would use, and
+	// name the call they belong to.
+	found, tools := 0, map[string]bool{}
+	files, err := storage.LandedFiles(zone, session)
+	if err != nil {
+		t.Fatal(err)
+	}
+	for _, f := range files {
+		file, err := os.Open(f.Path)
+		if err != nil {
+			t.Fatal(err)
+		}
+		reader, err := sessiondata.NewReader(file)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if reader.Header().Kind != sessiondata.KindChanges {
+			_ = file.Close()
+			continue
+		}
+		for {
+			record, err := reader.Next()
+			if err != nil {
+				break
+			}
+			found++
+			tools[record.Tool] = true
+		}
+		_ = file.Close()
+	}
+	if found == 0 {
+		t.Fatalf("no change record landed under %s", session)
+	}
+	if !tools["call_write_report_5"] {
+		t.Errorf("no record names the call it belongs to; got %v", tools)
+	}
+	t.Logf("%d change record(s) landed under %s, naming %v", found, session, tools)
+}
+
+// build compiles the recorder once for this test.
+func build(t *testing.T) string {
+	t.Helper()
+	out := filepath.Join(t.TempDir(), "asz-changes")
+	cmd := exec.Command("go", "build", "-o", out, "../plugins/claude-code")
+	if output, err := cmd.CombinedOutput(); err != nil {
+		t.Skipf("cannot build the recorder: %v: %s", err, output)
+	}
+	return out
+}