Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions chatbot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="google" content="notranslate" />
<meta name="color-scheme" content="light dark" />
<title>LoopForge 工作台</title>
<script src="/boot.js"></script>
<script src="/i18n-en.js"></script>
<script src="/i18n-ko.js"></script>
<script src="/i18n.js"></script>
</head>
<body>
<div id="root"></div>
Expand Down
228 changes: 228 additions & 0 deletions internal/engine/decision_i18n_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package engine

import (
"agentflow/internal/domain"
"strings"
"testing"
"time"
)

func TestDecisionAndHumanContextLocalization(t *testing.T) {
// 1. Korean run and node
koRun := domain.Run{
ID: "ko-run",
Input: "도메인 모델 크레이트 구현",
Nodes: map[string]*domain.NodeRun{
"node-design": {
NodeID: "node-design",
Status: "waiting_approval",
Attempt: 1,
},
},
WorkflowSnapshot: domain.WorkflowDefinition{
Name: "러스트 백엔드 파이프라인",
Nodes: []domain.WorkflowNode{
{
ID: "node-design",
Name: "아키텍처 및 인터페이스 설계",
},
},
},
}

// Verify language detection
if lang := runNodeLanguage(&koRun, "node-design"); lang != "ko" {
t.Fatalf("expected lang 'ko', got %q", lang)
}

// Test Approve on Korean run
note := recordHumanContextWithLang(&koRun, "node-design", "수동 승인으로 계속 진행", "", "ko")
if note != "수동 승인으로 계속 진행" {
t.Fatalf("unexpected note: %q", note)
}
if strings.Contains(note, "人工") {
t.Fatalf("note contains Chinese characters: %q", note)
}

// Simulate decision.md generation in Approve
decisionKO := "# 승인\n\n수동 승인 게이트를 통과했습니다.\n\n" + note + "\n"
appendArtifact(&koRun, "node-design", 1, "decision", "decision.md", "text/markdown", []byte(decisionKO))

var decisionArtifact *domain.Artifact
for i := range koRun.Artifacts {
if koRun.Artifacts[i].Name == "decision" {
decisionArtifact = &koRun.Artifacts[i]
break
}
}
if decisionArtifact == nil {
t.Fatal("decision artifact not found")
}
if strings.Contains(decisionArtifact.Content, "人工批准继续") {
t.Fatalf("decision.md still contains '人工批准继续':\n%s", decisionArtifact.Content)
}
if !strings.Contains(decisionArtifact.Content, "# 승인") || !strings.Contains(decisionArtifact.Content, "수동 승인으로 계속 진행") {
t.Fatalf("decision.md does not contain expected Korean text:\n%s", decisionArtifact.Content)
}

// 2. English run
enRun := domain.Run{
ID: "en-run",
Input: "<!-- lang: en --> Implement payment gateway",
Nodes: map[string]*domain.NodeRun{
"node-1": {
NodeID: "node-1",
Status: "waiting_approval",
Attempt: 1,
},
},
}
if lang := runNodeLanguage(&enRun, "node-1"); lang != "en" {
t.Fatalf("expected lang 'en', got %q", lang)
}
enNote := recordHumanContextWithLang(&enRun, "node-1", "Manually approved by human operator to continue", "Priority check", "en")
if !strings.Contains(enNote, "Manual Context:\nPriority check") {
t.Fatalf("unexpected English note: %q", enNote)
}
if strings.Contains(enNote, "人工") {
t.Fatalf("English note contains Chinese characters: %q", enNote)
}

// 3. Human context artifact localization
var humanCtxArtifact *domain.Artifact
for i := range enRun.Artifacts {
if enRun.Artifacts[i].Name == "human-context" {
humanCtxArtifact = &enRun.Artifacts[i]
break
}
}
if humanCtxArtifact == nil {
t.Fatal("human-context artifact not found")
}
if !strings.Contains(humanCtxArtifact.Content, "# Manual Context") {
t.Fatalf("human-context artifact does not contain English header:\n%s", humanCtxArtifact.Content)
}

// 4. Korean loop retro
retroKO := buildLoopRetro(koRun, "node-design", 2, nil, "loop")
if !strings.Contains(retroKO, "# Loop 복기 — `node-design`") || !strings.Contains(retroKO, "- 자체 수정 라운드: 2") || !strings.Contains(retroKO, "- 최종 결과: 통과") {
t.Fatalf("Korean loop retro does not match expected format:\n%s", retroKO)
}
if strings.Contains(retroKO, "复盘") {
t.Fatalf("Korean loop retro contains Chinese characters: %s", retroKO)
}

// 5. English loop retro
retroEN := buildLoopRetro(enRun, "node-1", 1, nil, "loop")
if !strings.Contains(retroEN, "# Loop Retrospective — `node-1`") || !strings.Contains(retroEN, "- Final result: Passed") {
t.Fatalf("English loop retro does not match expected format:\n%s", retroEN)
}
}

func TestEngineApproveLocalizationEndToEnd(t *testing.T) {
e, _ := cvEEngine(t)
runID := "e2e-ko-approve-run"
r := domain.Run{
ID: runID,
Status: "waiting",
Input: "회원가입 기능 개발",
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
Nodes: map[string]*domain.NodeRun{
"node-gate": {
NodeID: "node-gate",
Status: "waiting_approval",
Attempt: 1,
},
},
WorkflowSnapshot: domain.WorkflowDefinition{
Name: "스프린트 워크플로우",
Nodes: []domain.WorkflowNode{
{ID: "node-gate", Name: "기능 검증 게이트", Type: "approval"},
},
},
}
_ = e.repo.PutRun(r)

if err := e.Approve(runID, "node-gate", ""); err != nil {
t.Fatalf("Approve failed: %v", err)
}

updated, ok := e.repo.RunState(runID)
if !ok {
t.Fatal("run not found after approve")
}
var decisionArt *domain.Artifact
for i := range updated.Artifacts {
if updated.Artifacts[i].Name == "decision" && updated.Artifacts[i].NodeID == "node-gate" {
decisionArt = &updated.Artifacts[i]
break
}
}
if decisionArt == nil {
t.Fatal("decision.md artifact not found in updated run")
}
if strings.Contains(decisionArt.Content, "人工批准继续") {
t.Fatalf("decision.md still contains '人工批准继续':\n%s", decisionArt.Content)
}
if !strings.Contains(decisionArt.Content, "수동 승인으로 계속 진행") {
t.Fatalf("decision.md does not contain Korean note:\n%s", decisionArt.Content)
}
}

func TestEnginePassLocalizationEndToEnd(t *testing.T) {
e, _ := cvEEngine(t)
runID := "e2e-ko-pass-run"
r := domain.Run{
ID: runID,
Status: "failed",
Input: "결제 모듈 개발",
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
Nodes: map[string]*domain.NodeRun{
"node-script": {
NodeID: "node-script",
Status: "failed",
Attempt: 1,
},
},
WorkflowSnapshot: domain.WorkflowDefinition{
ID: "wf-pay",
Name: "결제 워크플로우",
Nodes: []domain.WorkflowNode{
{ID: "node-script", Name: "스크립트 검증", Type: "script"},
},
},
}
_ = e.repo.PutRun(r)

if err := e.Pass(runID, "node-script", ""); err != nil {
t.Fatalf("Pass failed: %v", err)
}

updated, ok := e.repo.RunState(runID)
if !ok {
t.Fatal("run not found after pass")
}
nr := updated.Nodes["node-script"]
if nr.Activity != "수동 통과 (재실행되지 않음)" {
t.Fatalf("unexpected activity: %q", nr.Activity)
}
var decisionArt *domain.Artifact
for i := range updated.Artifacts {
if updated.Artifacts[i].Name == "decision" && updated.Artifacts[i].NodeID == "node-script" {
decisionArt = &updated.Artifacts[i]
break
}
}
if decisionArt == nil {
t.Fatal("decision.md artifact not found in updated run")
}
if strings.Contains(decisionArt.Content, "人工通过该节点,继续下游") {
t.Fatalf("decision.md still contains Chinese text:\n%s", decisionArt.Content)
}
if !strings.Contains(decisionArt.Content, "이 단계를 수동으로 통과하고 후속 단계 진행") {
t.Fatalf("decision.md does not contain Korean note:\n%s", decisionArt.Content)
}
}

113 changes: 105 additions & 8 deletions internal/engine/gate_submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
"unicode"

"agentflow/internal/domain"
)
Expand Down Expand Up @@ -104,17 +105,112 @@ func applyGateResult(result *domain.GateResult, rejectTarget string) (error, str
}
}

// detectPromptLanguage inspects prompt texts to determine whether the prompt
// is primarily Korean ("ko"), English ("en"), or Chinese ("zh").
// If an explicit language directive (e.g. <!-- lang: ko -->, lang: ko) is found, it is respected.
// Otherwise, character analysis is used:
// - If Hangul characters are present and outnumber or equal Han characters, returns "ko".
// - If Han characters are present and outnumber Hangul, returns "zh".
// Defaults to "zh" to preserve backwards compatibility.
func detectPromptLanguage(texts ...string) string {
for _, text := range texts {
lower := strings.ToLower(text)
if strings.Contains(lower, "<!-- lang: ko -->") || strings.Contains(lower, "lang: ko") || strings.Contains(lower, "language: ko") || strings.Contains(lower, "locale: ko") {
return "ko"
}
if strings.Contains(lower, "<!-- lang: en -->") || strings.Contains(lower, "lang: en") || strings.Contains(lower, "language: en") || strings.Contains(lower, "locale: en") {
return "en"
}
if strings.Contains(lower, "<!-- lang: zh -->") || strings.Contains(lower, "lang: zh") || strings.Contains(lower, "language: zh") || strings.Contains(lower, "locale: zh") {
return "zh"
}
}

hangulCount := 0
hanCount := 0
for _, text := range texts {
for _, r := range text {
if unicode.Is(unicode.Hangul, r) {
hangulCount++
} else if unicode.Is(unicode.Han, r) {
hanCount++
}
}
}

if hangulCount > 0 && hangulCount*2 >= hanCount {
return "ko"
}
if hanCount > 0 && hanCount > hangulCount {
return "zh"
}
return "zh"
}

func appendSubmitGateInstruction(prompt, rejectTarget string, retry bool) string {
lang := detectPromptLanguage(prompt)
if retry {
return "刚才没有收到有效的节点终态。不要重复执行任务,也不要只输出文字;请立刻调用 submit_gate_result,提交 pass、rework、fail 或 human 之一以及具体 reason。选择 human 时还必须填写 actionRequired,明确需要人工确认什么。只有工具调用成功才算完成本节点。"
}
reworkInstruction := "- rework:存在应由上游节点返工的问题;平台会自动打回该节点;"
if strings.TrimSpace(rejectTarget) == "" {
reworkInstruction = "- rework:本节点没有配置返工目标,不可选择;需要直接终止时请选择 fail;"
} else {
reworkInstruction = fmt.Sprintf("- rework:存在应由上游节点「%s」返工的问题;平台会自动打回;", rejectTarget)
switch lang {
case "ko":
return "방금 유효한 노드 최종 상태(submit_gate_result)를 수신하지 못했습니다. 작업을 중복 실행하거나 텍스트로만 출력하지 마십시오. 즉시 submit_gate_result 도구를 호출하여 pass, rework, fail, human 중 하나와 구체적인 사유(reason)를 제출하십시오. human을 선택할 경우 actionRequired에 수동 확인이 필요한 내용을 반드시 명시하십시오. 도구 호출이 성공해야 본 노드가 완료됩니다.\n\n- 모든 사고 과정과 최종 노드 출력은 반드시 한국어로 작성하십시오."
case "en":
return "Did not receive a valid node terminal status just now. Do not re-execute the task from scratch, and do not output text only; please immediately call the tool submit_gate_result to submit one of pass, rework, fail, or human along with a specific reason. When choosing human, actionRequired is required to specify what needs human confirmation. Only a successful tool call completes this node.\n\n- Please write all reasoning and final node output in English."
default:
return "刚才没有收到有效的节点终态。不要重复执行任务,也不要只输出文字;请立刻调用 submit_gate_result,提交 pass、rework、fail 或 human 之一以及具体 reason。选择 human 时还必须填写 actionRequired,明确需要人工确认什么。只有工具调用成功才算完成本节点。"
}
}
return strings.TrimRight(prompt, "\n") + fmt.Sprintf(`

switch lang {
case "ko":
reworkInstruction := "- rework: 상위(업스트림) 노드에서 수정해야 하는 문제가 있습니다. 플랫폼이 해당 노드로 자동 반려합니다."
if strings.TrimSpace(rejectTarget) == "" {
reworkInstruction = "- rework: 본 노드에는 반려 대상이 설정되어 있지 않아 선택할 수 없습니다. 즉시 중단해야 하는 경우 fail을 선택하십시오."
} else {
reworkInstruction = fmt.Sprintf("- rework: 상위 노드 「%s」에서 수정해야 하는 문제가 있습니다. 플랫폼이 해당 노드로 자동 반려합니다.", rejectTarget)
}
return strings.TrimRight(prompt, "\n") + fmt.Sprintf(`

# 노드 최종 상태 필수 제출

작업을 완료한 후 반드시 MCP 도구 submit_gate_result를 호출하여 최종 상태를 제출해야 합니다:
- pass: 작업이 정상 완료되었습니다. 플랫폼이 다음 하위 노드로 자동 진행합니다.
%s
- fail: 본 노드를 완료할 수 없거나 복구 불가능한 오류가 발생했습니다. 상위 노드로 반려하지 않고 즉시 실패 처리합니다.
- human: 자동으로 신뢰성 있게 판단할 수 없거나 사람의 결정이 필요합니다. 플랫폼이 일시 중지되고 수동 승인을 대기합니다. 이때 actionRequired는 필수이며 사람이 확인하거나 결정해야 할 사항을 구체적으로 작성해야 합니다.

현재 실제 상황에 맞춰 선택하십시오. fail 상황을 rework로 처리하거나, 명백한 실패를 human으로 회피하지 마십시오. reason에는 해당 최종 상태를 결정한 객관적 근거를 설명해야 합니다. rework 시 actionRequired에 재작업 요구사항을 명시할 수 있으며, findings 및 evidence를 첨부할 수 있습니다. 보고서 텍스트나 일반 응답으로 도구 호출을 대신할 수 없습니다. 이 도구 호출이 성공해야 본 노드가 완료됩니다.

- 모든 사고 과정과 최종 노드 출력은 반드시 한국어로 작성하십시오.`, reworkInstruction)

case "en":
reworkInstruction := "- rework: there are issues that should be reworked by an upstream node; the platform will automatically send it back;"
if strings.TrimSpace(rejectTarget) == "" {
reworkInstruction = "- rework: this node has no rework target configured and cannot be selected; choose fail if direct termination is needed;"
} else {
reworkInstruction = fmt.Sprintf("- rework: there are issues that should be reworked by the upstream node \"%s\"; the platform will automatically send it back;", rejectTarget)
}
return strings.TrimRight(prompt, "\n") + fmt.Sprintf(`

# Must Submit Node Terminal Status

After completing the task, you must call the MCP tool submit_gate_result to submit a single terminal status:
- pass: task completed normally; platform will advance to downstream nodes automatically;
%s
- fail: this node cannot be completed or encountered an unrecoverable error; platform will mark failure directly without sending back upstream;
- human: cannot be reliably judged automatically or requires human decision; platform will pause and wait for manual confirmation, in which case actionRequired is mandatory and must specify what needs human confirmation or decision.

Choose according to the real situation. Do not treat fail as rework, nor use human to mask a definite failure. reason must explain the factual basis for the decision; rework can use actionRequired to state rework requirements; findings and evidence may be attached. Do not substitute tool calls with report text or final chat replies. Only a successful call to this tool completes this node.

- Please write all reasoning and final node output in English.`, reworkInstruction)

default:
reworkInstruction := "- rework:存在应由上游节点返工的问题;平台会自动打回该节点;"
if strings.TrimSpace(rejectTarget) == "" {
reworkInstruction = "- rework:本节点没有配置返工目标,不可选择;需要直接终止时请选择 fail;"
} else {
reworkInstruction = fmt.Sprintf("- rework:存在应由上游节点「%s」返工的问题;平台会自动打回;", rejectTarget)
}
return strings.TrimRight(prompt, "\n") + fmt.Sprintf(`

# 必须提交节点终态

Expand All @@ -125,4 +221,5 @@ func appendSubmitGateInstruction(prompt, rejectTarget string, retry bool) string
- human:无法可靠自动判断,或必须由人决策;平台会暂停并等待人工确认,此时 actionRequired 必填,必须明确说明需要人确认或决定什么。

必须根据当前真实情况选择,不要把 fail 当成 rework,也不要用 human 掩盖明确失败。reason 必须解释作出该终态的事实依据;rework 可用 actionRequired 写清返工要求;可附带 findings 和 evidence。不要用报告文本或最终回复代替工具调用。只有该工具调用成功,本节点才算完成。`, reworkInstruction)
}
}
Loading