mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-15 14:16:16 +00:00
fix(glmt): resolve thinking block signature timing race
- guard against empty content in _createSignatureDeltaEvent() - consolidate debug flags: CCS_DEBUG_LOG, CCS_GLMT_DEBUG → CCS_DEBUG - add 6 regression tests for signature race condition - update docs with simplified debug flag usage
This commit is contained in:
1 parent
4e3592d711
commit
513e5b693f
15 files changed
+384
-46
No files matched your search
@@ -152,12 +152,12 @@ console.log('\n=== Test 2: Debug Mode ON (via config) ===');
|
||||
console.log('\n✓ All checks passed for debug mode ON');
|
||||
}
|
||||
|
||||
console.log('\n=== Test 3: Debug Mode via CCS_DEBUG_LOG=1 ===');
|
||||
console.log('\n=== Test 3: Debug Mode via CCS_DEBUG=1 ===');
|
||||
{
|
||||
// Clean up
|
||||
fs.rmSync(logDir, { recursive: true, force: true });
|
||||
|
||||
process.env.CCS_DEBUG_LOG = '1';
|
||||
process.env.CCS_DEBUG = '1';
|
||||
const transformer = new GlmtTransformer({ verbose: false });
|
||||
console.log(`Debug logging: ${transformer.debugLog}`);
|
||||
|
||||
@@ -188,13 +188,13 @@ console.log('\n=== Test 3: Debug Mode via CCS_DEBUG_LOG=1 ===');
|
||||
console.log(`Files created: ${files.length}`);
|
||||
|
||||
if (files.length === 4) {
|
||||
console.log('✓ Debug mode enabled via CCS_DEBUG_LOG=1');
|
||||
console.log('✓ Debug mode enabled via CCS_DEBUG=1');
|
||||
} else {
|
||||
console.log(`ERROR: Expected 4 files, got ${files.length}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
delete process.env.CCS_DEBUG_LOG;
|
||||
delete process.env.CCS_DEBUG;
|
||||
}
|
||||
|
||||
console.log('\n=== Test 4: Error Handling (No Write Permission) ===');
|
||||
|
||||
@@ -288,11 +288,11 @@ runner.test('debug mode enabled via config', () => {
|
||||
});
|
||||
|
||||
// Test 15: Debug mode enabled via env var
|
||||
runner.test('debug mode enabled via CCS_DEBUG_LOG=1', () => {
|
||||
process.env.CCS_DEBUG_LOG = '1';
|
||||
runner.test('debug mode enabled via CCS_DEBUG=1', () => {
|
||||
process.env.CCS_DEBUG = '1';
|
||||
const transformer = new GlmtTransformer();
|
||||
assertEqual(transformer.debugLog, true, 'debugLog should be true when CCS_DEBUG_LOG=1');
|
||||
delete process.env.CCS_DEBUG_LOG;
|
||||
assertEqual(transformer.debugLog, true, 'debugLog should be true when CCS_DEBUG=1');
|
||||
delete process.env.CCS_DEBUG;
|
||||
});
|
||||
|
||||
// Test 16: Debug log directory path
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* Simulates 3 consecutive messages to test if thinking blocks
|
||||
* appear in all messages or only the first one.
|
||||
*
|
||||
* Usage: CCS_DEBUG_LOG=1 node test-thinking-multi-message.js
|
||||
* Usage: CCS_DEBUG=1 node test-thinking-multi-message.js
|
||||
*/
|
||||
|
||||
const { spawn } = require('child_process');
|
||||
@@ -67,7 +67,7 @@ async function runMessage(messageIndex) {
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env: {
|
||||
...process.env,
|
||||
CCS_DEBUG_LOG: '1'
|
||||
CCS_DEBUG: '1'
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+250
@@ -0,0 +1,250 @@
|
||||
#!/usr/bin/env node
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Regression test for thinking signature race condition
|
||||
* Ensures signature not generated before content accumulated
|
||||
*
|
||||
* Issue: In streaming mode, thinking blocks appeared in UI but were blank
|
||||
* Root cause: _createSignatureDeltaEvent called before block.content accumulated
|
||||
* Fix: Guard against empty content, return null if block.content is empty
|
||||
*/
|
||||
|
||||
const GlmtTransformer = require('../../../bin/glmt/glmt-transformer');
|
||||
const DeltaAccumulator = require('../../../bin/glmt/delta-accumulator');
|
||||
|
||||
// Test runner
|
||||
class TestRunner {
|
||||
constructor() {
|
||||
this.tests = [];
|
||||
this.passed = 0;
|
||||
this.failed = 0;
|
||||
}
|
||||
|
||||
test(name, fn) {
|
||||
this.tests.push({ name, fn });
|
||||
}
|
||||
|
||||
async run() {
|
||||
console.log('\n=== Thinking Signature Race Condition Tests ===\n');
|
||||
|
||||
for (const { name, fn } of this.tests) {
|
||||
try {
|
||||
await fn();
|
||||
console.log(`✓ ${name}`);
|
||||
this.passed++;
|
||||
} catch (error) {
|
||||
console.log(`✗ ${name}`);
|
||||
console.log(` Error: ${error.message}`);
|
||||
this.failed++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n=== Results ===');
|
||||
console.log(`Passed: ${this.passed}/${this.tests.length}`);
|
||||
console.log(`Failed: ${this.failed}/${this.tests.length}`);
|
||||
|
||||
process.exit(this.failed > 0 ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
const runner = new TestRunner();
|
||||
|
||||
// Helper function for assertions
|
||||
function assert(condition, message) {
|
||||
if (!condition) {
|
||||
throw new Error(message || 'Assertion failed');
|
||||
}
|
||||
}
|
||||
|
||||
// Test 1: Signature not generated for empty thinking block
|
||||
runner.test('Signature not generated for empty thinking block', () => {
|
||||
const transformer = new GlmtTransformer({ verbose: false });
|
||||
const accumulator = new DeltaAccumulator({ thinking: true });
|
||||
|
||||
// Start thinking block but add no content
|
||||
accumulator.messageStarted = true;
|
||||
const block = accumulator.startBlock('thinking');
|
||||
|
||||
// Try to generate signature for empty block
|
||||
const signatureEvent = transformer._createSignatureDeltaEvent(block);
|
||||
|
||||
// Should return null for empty block (fix for race condition)
|
||||
assert(signatureEvent === null, 'Expected null for empty thinking block');
|
||||
});
|
||||
|
||||
// Test 2: Signature generated correctly after content accumulated
|
||||
runner.test('Signature generated correctly after content accumulated', () => {
|
||||
const transformer = new GlmtTransformer({ verbose: false });
|
||||
const accumulator = new DeltaAccumulator({ thinking: true });
|
||||
|
||||
// Start thinking block and add content
|
||||
accumulator.messageStarted = true;
|
||||
const block = accumulator.startBlock('thinking');
|
||||
accumulator.addDelta('First thinking delta. ');
|
||||
accumulator.addDelta('Second thinking delta.');
|
||||
|
||||
// Generate signature
|
||||
const signatureEvent = transformer._createSignatureDeltaEvent(block);
|
||||
|
||||
// Should return valid signature event
|
||||
assert(signatureEvent !== null, 'Expected signature event for non-empty block');
|
||||
assert(signatureEvent.event === 'content_block_delta', 'Expected content_block_delta event');
|
||||
assert(signatureEvent.data.delta.type === 'thinking_signature_delta', 'Expected thinking_signature_delta type');
|
||||
assert(signatureEvent.data.delta.signature.length > 0, 'Expected signature length > 0');
|
||||
assert(signatureEvent.data.delta.signature.hash, 'Expected signature hash');
|
||||
assert(signatureEvent.data.delta.signature.hash.length === 16, 'Expected 16-char hash');
|
||||
});
|
||||
|
||||
// Test 3: transformDelta skips signature for empty thinking blocks
|
||||
runner.test('transformDelta skips signature for empty thinking blocks (thinking→text transition)', () => {
|
||||
const transformer = new GlmtTransformer({ verbose: false });
|
||||
const accumulator = new DeltaAccumulator({ thinking: true });
|
||||
|
||||
// Simulate thinking block with no content
|
||||
accumulator.messageStarted = true;
|
||||
const block = accumulator.startBlock('thinking');
|
||||
|
||||
// Transition to text (would normally generate signature)
|
||||
const openaiEvent = {
|
||||
event: 'message',
|
||||
data: {
|
||||
choices: [{
|
||||
delta: { content: 'Text response after empty thinking' }
|
||||
}]
|
||||
}
|
||||
};
|
||||
|
||||
const events = transformer.transformDelta(openaiEvent, accumulator);
|
||||
|
||||
// Signature event should not be present
|
||||
const signatureEvents = events.filter(e =>
|
||||
e.data?.delta?.type === 'thinking_signature_delta'
|
||||
);
|
||||
assert(signatureEvents.length === 0, 'Expected 0 signature events for empty thinking block');
|
||||
|
||||
// Should still have content_block_stop event
|
||||
const stopEvents = events.filter(e => e.event === 'content_block_stop');
|
||||
assert(stopEvents.length > 0, 'Expected content_block_stop event');
|
||||
});
|
||||
|
||||
// Test 4: transformDelta generates signature for non-empty thinking blocks
|
||||
runner.test('transformDelta generates signature for non-empty thinking blocks', () => {
|
||||
const transformer = new GlmtTransformer({ verbose: false });
|
||||
const accumulator = new DeltaAccumulator({ thinking: true });
|
||||
|
||||
// Start thinking block with content
|
||||
accumulator.messageStarted = true;
|
||||
const block = accumulator.startBlock('thinking');
|
||||
|
||||
// Add reasoning content
|
||||
const reasoningEvent = {
|
||||
event: 'message',
|
||||
data: {
|
||||
choices: [{
|
||||
delta: { reasoning_content: 'This is actual thinking content. ' }
|
||||
}]
|
||||
}
|
||||
};
|
||||
transformer.transformDelta(reasoningEvent, accumulator);
|
||||
|
||||
// Add more content
|
||||
const moreReasoningEvent = {
|
||||
event: 'message',
|
||||
data: {
|
||||
choices: [{
|
||||
delta: { reasoning_content: 'More thinking here.' }
|
||||
}]
|
||||
}
|
||||
};
|
||||
transformer.transformDelta(moreReasoningEvent, accumulator);
|
||||
|
||||
// Now transition to text (should generate signature)
|
||||
const textEvent = {
|
||||
event: 'message',
|
||||
data: {
|
||||
choices: [{
|
||||
delta: { content: 'Final text response' }
|
||||
}]
|
||||
}
|
||||
};
|
||||
|
||||
const events = transformer.transformDelta(textEvent, accumulator);
|
||||
|
||||
// Signature event should be present
|
||||
const signatureEvents = events.filter(e =>
|
||||
e.data?.delta?.type === 'thinking_signature_delta'
|
||||
);
|
||||
assert(signatureEvents.length === 1, 'Expected 1 signature event for non-empty thinking block');
|
||||
|
||||
// Verify signature structure
|
||||
const sig = signatureEvents[0].data.delta.signature;
|
||||
assert(sig.hash && sig.hash.length === 16, 'Expected valid 16-char hash');
|
||||
assert(sig.length > 0, 'Expected content length > 0');
|
||||
});
|
||||
|
||||
// Test 5: Loop detection handles empty thinking blocks
|
||||
runner.test('Loop detection handles empty thinking blocks without signature', () => {
|
||||
const transformer = new GlmtTransformer({ verbose: false });
|
||||
const accumulator = new DeltaAccumulator({ thinking: true });
|
||||
|
||||
// Create scenario where loop is detected with empty thinking block
|
||||
accumulator.messageStarted = true;
|
||||
|
||||
// First thinking block (empty)
|
||||
accumulator.startBlock('thinking');
|
||||
accumulator.stopCurrentBlock();
|
||||
|
||||
// Second thinking block (empty)
|
||||
accumulator.startBlock('thinking');
|
||||
accumulator.stopCurrentBlock();
|
||||
|
||||
// Third thinking block (empty) - triggers loop
|
||||
accumulator.startBlock('thinking');
|
||||
|
||||
// Simulate loop detection in transformDelta
|
||||
const openaiEvent = {
|
||||
event: 'message',
|
||||
data: {
|
||||
choices: [{
|
||||
delta: { reasoning_content: '' }
|
||||
}]
|
||||
}
|
||||
};
|
||||
|
||||
// This would trigger loop detection logic
|
||||
accumulator.checkForLoop(); // Returns true after 3 blocks
|
||||
|
||||
const currentBlock = accumulator.getCurrentBlock();
|
||||
if (currentBlock && currentBlock.type === 'thinking') {
|
||||
const signatureEvent = transformer._createSignatureDeltaEvent(currentBlock);
|
||||
// Should return null for empty block
|
||||
assert(signatureEvent === null, 'Expected null signature for empty thinking block during loop detection');
|
||||
}
|
||||
});
|
||||
|
||||
// Test 6: finalizeDelta handles empty thinking blocks
|
||||
runner.test('finalizeDelta handles empty thinking blocks without signature', () => {
|
||||
const transformer = new GlmtTransformer({ verbose: false });
|
||||
const accumulator = new DeltaAccumulator({ thinking: true });
|
||||
|
||||
// Start thinking block with no content
|
||||
accumulator.messageStarted = true;
|
||||
accumulator.startBlock('thinking');
|
||||
|
||||
// Finalize (end of message)
|
||||
const events = transformer.finalizeDelta(accumulator);
|
||||
|
||||
// Should not include signature event for empty block
|
||||
const signatureEvents = events.filter(e =>
|
||||
e.data?.delta?.type === 'thinking_signature_delta'
|
||||
);
|
||||
assert(signatureEvents.length === 0, 'Expected 0 signature events for empty thinking block in finalizeDelta');
|
||||
|
||||
// Should still include content_block_stop
|
||||
const stopEvents = events.filter(e => e.event === 'content_block_stop');
|
||||
assert(stopEvents.length === 1, 'Expected content_block_stop event in finalizeDelta');
|
||||
});
|
||||
|
||||
// Run all tests
|
||||
runner.run();
|
||||
Reference in new issue
Block a user