Slide 1

Slide 1 text

Kotlin × グラフ理論: Unstable APIで挑む関数レベル Affected Test Selectionライブラリ Haruto Kato 1

Slide 2

Slide 2 text

⾃⼰紹介 加藤 遥⽃ (@HalunoYo) Android Engineer @ BizReach 低レイヤー & セキュリティ好き ※本発表は所属企業とは関係ありません Haruto Kato 2

Slide 3

Slide 3 text

悩みの紹介 単体テスト時間⻑すぎ問題 Haruto Kato 3

Slide 4

Slide 4 text

悩みの紹介 func A() func B() @Test func A() +9 func A() func C() Haruto Kato @Test func B() @Test func C() 4

Slide 5

Slide 5 text

悩みの紹介 func A() func B() @Test func A() +9 func A() func C() Haruto Kato @Test func B() @Test func C() 5

Slide 6

Slide 6 text

転期 競技プログラミング Haruto Kato 6

Slide 7

Slide 7 text

Rotting Oranges(腐っていく蜜柑) Haruto Kato 7

Slide 8

Slide 8 text

Rotting Oranges(腐っていく蜜柑) Haruto Kato 8

Slide 9

Slide 9 text

Rotting Oranges(腐っていく蜜柑) Haruto Kato 9

Slide 10

Slide 10 text

Rotting Oranges(腐っていく蜜柑) Haruto Kato 10

Slide 11

Slide 11 text

問題の抽象化 ある要素が変わった時、 影響を受ける他の要素を全列挙する Haruto Kato 11

Slide 12

Slide 12 text

問題の再具体化 ある関数が変わった時、 影響を受ける他の関数を全列挙する Haruto Kato 12

Slide 13

Slide 13 text

グラフ理論とは グラフ理論 (Graph Theory) Haruto Kato 13

Slide 14

Slide 14 text

グラフ理論とは 2 4 1 6 5 3 ノード(頂点) エッジ(辺) Haruto Kato 14

Slide 15

Slide 15 text

グラフ理論とは B D A F E C ノード(頂点) エッジ(辺) Haruto Kato 15

Slide 16

Slide 16 text

グラフ理論とは B D A F E C ノード(頂点) エッジ(辺) Haruto Kato 16

Slide 17

Slide 17 text

強み B D A F E C Haruto Kato 17

Slide 18

Slide 18 text

強み B D A F E C Haruto Kato 18

Slide 19

Slide 19 text

強み B D A F E C Haruto Kato 19

Slide 20

Slide 20 text

強み B D A F E C Haruto Kato 20

Slide 21

Slide 21 text

ノードB,Dの関係性 B A F C Haruto Kato fun B() { ?/ Dを呼ぶ val result = D() } D E 21

Slide 22

Slide 22 text

ノードD,Fの関係性 B A F C Haruto Kato fun D() { ?/ Fを呼ぶ val result = F() } D E 22

Slide 23

Slide 23 text

確認すべき⽅向 A D V テストすべき関数たち Haruto Kato 23 F

Slide 24

Slide 24 text

グラフ問題 変更された関数ノードが与えられます 当該ノードに対し到達可能なノードを全列挙せよ Haruto Kato 24

Slide 25

Slide 25 text

OSS紹介とお願い sazanami Kotlinプロジェクト向け。 差分から関数単位でテスト範囲を絞る。 Haruto Kato 25

Slide 26

Slide 26 text

処理全体像 入力 Haruto Kato 加工 26 出力

Slide 27

Slide 27 text

処理全体像 入力 Haruto Kato 加工 27 出力

Slide 28

Slide 28 text

⼊⼒フェーズの責務 git差分を受け取る 変更された関数モデルを作成 Haruto Kato 28

Slide 29

Slide 29 text

変更関数ドメインモデル data class ChangedFunction { val fqn: CallableFqn, val moduleName: ModuleName, … } Haruto Kato 29

Slide 30

Slide 30 text

処理内容 fun B() { val result = D() + } Haruto Kato ChangedFunction( fqn = “com.hogehoge.B”, moduleName = “core”, ) 30

Slide 31

Slide 31 text

処理全体像 入力 Haruto Kato 加工 31 出力

Slide 32

Slide 32 text

加⼯フェーズの責務 Kotlin Analysis⽤解析セッション構築 関数コールグラフ構築 影響範囲⾛査 Haruto Kato 32

Slide 33

Slide 33 text

加⼯フェーズの責務 Kotlin Analysis⽤解析セッション構築 関数コールグラフ構築 影響範囲⾛査 Haruto Kato 33

Slide 34

Slide 34 text

Kotlin Analysis⽤解析セッション構築 1. Kotlin Analysis API 2. 解析環境構築 Haruto Kato 34

Slide 35

Slide 35 text

Kotlin Analysis⽤解析セッション構築 1. Kotlin Analysis API 2. 解析環境構築 Haruto Kato 35

Slide 36

Slide 36 text

Kotlin Analysis API Kotlin Analysis API Kotlin Analysis API Standalone Haruto Kato 36

Slide 37

Slide 37 text

Kotlin Analysis API Kotlin Analysis API Kotlin Analysis API Standalone Haruto Kato 37

Slide 38

Slide 38 text

違い モジュール ファイル 全て⾃前で提供 Haruto Kato 38 Etc

Slide 39

Slide 39 text

Kotlin Analysis API PSIからは分からない”意味”を教えてくれる Haruto Kato 39

Slide 40

Slide 40 text

PSI(Program Structure Interface) ファイル クラス 関数A Haruto Kato 関数B 40 関数C

Slide 41

Slide 41 text

意味解析の不明点 class KotlinLove { fun love(){ return hoge.funcA() } } funcAとは...? Haruto Kato 41

Slide 42

Slide 42 text

意味解析の不明点 class KotlinLove { fun love(){ return hoge.funcA() } } io.github.sazanami.hoge.funcA Haruto Kato 42

Slide 43

Slide 43 text

Kotlin Analysis⽤解析セッション構築 1. Kotlin Analysis API 2. 解析環境構築 Haruto Kato 43

Slide 44

Slide 44 text

build.gradle.kts dependencies { implementation("kotlin:analysis-api-for-ide:$ktVersion") { isTransitive = false } // 他にも⾊々インポート } implementation("kotlin:symbol-light-classes-for-ide:$ktVersion"){ isTransitive = false } Haruto Kato 44

Slide 45

Slide 45 text

推移的依存を使⽤しない理由 推移的依存の場合: ⾮公開の内部ライブラリ(analysis-api-fir等)を参照する。 そのため、公開済artifactのみを使うよう明⽰ Haruto Kato 45

Slide 46

Slide 46 text

モジュール情報収集 fun collectModuleInfo() mapOf( ":core" to listOf("/project/core/src/main/kotlin"), ":app" to listOf("/project/app/src/main/kotlin") ) Haruto Kato 46

Slide 47

Slide 47 text

モジュール依存関係情報収集 fun collectModuleDependencies() mapOf( ":app" to setOf(":core", “:data”) ) Haruto Kato 47

Slide 48

Slide 48 text

マルチモジュールの壁 マルチモジュール対応 Haruto Kato 48

Slide 49

Slide 49 text

マルチモジュールの壁 ModuleB ModuleA Haruto Kato ModuleC 49

Slide 50

Slide 50 text

マルチモジュールの壁 Developer Kotlin Analysis ModuleAの依存先はこれ ModuleB?? ModuleB Haruto Kato ModuleC 50 ModuleC??

Slide 51

Slide 51 text

トポロジカルソート fun topologicalSort() Haruto Kato 51

Slide 52

Slide 52 text

トポロジカルソート適⽤ Before A B C B C A After Haruto Kato 52

Slide 53

Slide 53 text

コンテキスト登録 fun buildStandaloneAnalysisSession() { val session = buildStandaloneAnalysisAPISession(disposable) { for (moduleName in sortedModules) { … val module = buildKtSourceModule { this.moduleName = moduleName sourceRoots.forEach { addSourceRoot(it) } for (dep in allDependencies) { … addRegularDependency(dep) } } addModule(module) } } } Haruto Kato 53

Slide 54

Slide 54 text

コンテキスト登録 fun buildStandaloneAnalysisSession() { val session = buildStandaloneAnalysisAPISession(disposable) { for (moduleName in sortedModules) { … val module = buildKtSourceModule { this.moduleName = moduleName sourceRoots.forEach { addSourceRoot(it) } for (dep in allDependencies) { … addRegularDependency(dep) } } addModule(module) } } } Haruto Kato 54

Slide 55

Slide 55 text

セッションとは Application Project Module KtFile A KtFile B Haruto Kato 55

Slide 56

Slide 56 text

コンテキスト登録 fun buildStandaloneAnalysisSession() { val session = buildStandaloneAnalysisAPISession(disposable) { for (moduleName in sortedModules) { … val module = buildKtSourceModule { this.moduleName = moduleName sourceRoots.forEach { addSourceRoot(it) } for (dep in allDependencies) { … addRegularDependency(dep) } } addModule(module) } } } Haruto Kato 56

Slide 57

Slide 57 text

コンテキスト登録 fun buildStandaloneAnalysisSession() { val session = buildStandaloneAnalysisAPISession(disposable) { for (moduleName in sortedModules) { … val module = buildKtSourceModule { this.moduleName = moduleName sourceRoots.forEach { addSourceRoot(it) } for (dep in allDependencies) { … addRegularDependency(dep) } } addModule(module) } } } Haruto Kato 57

Slide 58

Slide 58 text

コンテキスト登録 fun buildStandaloneAnalysisSession() { val session = buildStandaloneAnalysisAPISession(disposable) { for (moduleName in sortedModules) { … val module = buildKtSourceModule { this.moduleName = moduleName sourceRoots.forEach { addSourceRoot(it) } for (dep in allDependencies) { … addRegularDependency(dep) } } addModule(module) } } } Haruto Kato 58

Slide 59

Slide 59 text

コンテキスト登録 fun buildStandaloneAnalysisSession() { val session = buildStandaloneAnalysisAPISession(disposable) { for (moduleName in sortedModules) { … val module = buildKtSourceModule { this.moduleName = moduleName sourceRoots.forEach { addSourceRoot(it) } for (dep in allDependencies) { … addRegularDependency(dep) } } addModule(module) } } } Haruto Kato 59

Slide 60

Slide 60 text

コンテキスト登録 fun buildStandaloneAnalysisSession() { val session = buildStandaloneAnalysisAPISession(disposable) { for (moduleName in sortedModules) { … val module = buildKtSourceModule { this.moduleName = moduleName sourceRoots.forEach { addSourceRoot(it) } for (dep in allDependencies) { … addRegularDependency(dep) } } addModule(module) } } } Haruto Kato 60

Slide 61

Slide 61 text

加⼯フェーズの責務 Kotlin Analysis⽤解析セッション構築 関数コールグラフ構築 影響範囲⾛査 Haruto Kato 61

Slide 62

Slide 62 text

コールグラフ構築の全体像 1. KtFile内の1⾏⽬から最終⾏まで全⾛査 2. 関数呼び出しを検知 3. 呼び出し元関数を特定 4. ⼿順2及び3の関数同⼠の関係性をグラフ化 Haruto Kato 62

Slide 63

Slide 63 text

コールグラフ構築の全体像 1. KtFile内の1⾏⽬から最終⾏まで全⾛査 2. 関数呼び出しを検知 3. 呼び出し元関数を特定 4. ⼿順2及び3の関数同⼠の関係性をグラフ化 Haruto Kato 63

Slide 64

Slide 64 text

KtFile全⾛査 1 2 3 4 5 6 7 8 Haruto Kato package io.github.kotlin_love import … class KotlinLove { fun love(){ return } } 64

Slide 65

Slide 65 text

KtFile全⾛査 1 2 3 4 5 6 7 8 Haruto Kato package io.github.kotlin_love import … class KotlinLove { fun love(){ return } } 65

Slide 66

Slide 66 text

KtFile全⾛査 1 2 3 4 5 6 7 8 Haruto Kato package io.github.kotlin_love import … class KotlinLove { fun love(){ return } } 66

Slide 67

Slide 67 text

KtFile全⾛査 1 2 3 4 5 6 7 8 Haruto Kato package io.github.kotlin_love import … class KotlinLove { fun love(){ return } } 67

Slide 68

Slide 68 text

KtFile全⾛査 改⾏無しパターン 改⾏有りパターン class KotlinLove { fun love(){ return } } class KotlinLove { fun love() { return } } Haruto Kato 68

Slide 69

Slide 69 text

KtFile全⾛査 with PSI PSI Haruto Kato 69

Slide 70

Slide 70 text

PSIの表現モデル KtElement https://kotlin.github.io/analysis-api/fundamentals.html#kotlin-psi Haruto Kato 70

Slide 71

Slide 71 text

PSI Tree化 KtClass KtClassBody class KotlinLove { fun love(): Unit { return } } KtNamedFunction KtTypeReference KtBlockExpression KtReturnExpression Haruto Kato 71

Slide 72

Slide 72 text

PSI Tree化 KtClass KtClassBody class KotlinLove { fun love(): Unit { return } } KtNamedFunction KtTypeReference KtBlockExpression KtReturnExpression Haruto Kato 72

Slide 73

Slide 73 text

PSI Tree化 KtClass KtClassBody class KotlinLove { fun love(): Unit { return } } KtNamedFunction KtTypeReference KtBlockExpression KtReturnExpression Haruto Kato 73

Slide 74

Slide 74 text

PSI Tree化 KtClass KtClassBody class KotlinLove { fun love(): Unit { return } } KtNamedFunction KtTypeReference KtBlockExpression KtReturnExpression Haruto Kato 74

Slide 75

Slide 75 text

PSI Tree化 KtClass KtClassBody class KotlinLove { fun love(): Unit { return } } KtNamedFunction KtTypeReference KtBlockExpression KtReturnExpression Haruto Kato 75

Slide 76

Slide 76 text

PSI Tree化 KtClass KtClassBody class KotlinLove { fun love(): Unit { return } } KtNamedFunction KtTypeReference KtBlockExpression KtReturnExpression Haruto Kato 76

Slide 77

Slide 77 text

PSI Tree化 KtClass KtClassBody class KotlinLove { fun love(): Unit { return } } KtNamedFunction KtTypeReference KtBlockExpression KtReturnExpression Haruto Kato 77

Slide 78

Slide 78 text

コールグラフ構築の全体像 1. KtFile内の1⾏⽬から最終⾏まで全⾛査 2. 関数呼び出しを検知 3. 呼び出し元関数を特定 4. ⼿順2及び3の関数同⼠の関係性をグラフ化 Haruto Kato 78

Slide 79

Slide 79 text

関数呼び出しの検出 KtCallExpression Haruto Kato 79

Slide 80

Slide 80 text

関数呼び出しの検出 KtCallExpressionだ! Haruto Kato 80

Slide 81

Slide 81 text

関数呼び出しの検出 fun processFile(file: KtFile) { file.accept(object : KtTreeVisitorVoid() { override fun visitCallExpression(expression: KtCallExpression){ processCallExpression(expression) super.visitCallExpression(expression) } } } Haruto Kato 81

Slide 82

Slide 82 text

関数呼び出しの検出 fun processFile(file: KtFile) { file.accept(object : KtTreeVisitorVoid() { override fun visitCallExpression(expression: KtCallExpression){ processCallExpression(expression) super.visitCallExpression(expression) } } } Haruto Kato 82

Slide 83

Slide 83 text

関数呼び出しの検出 fun processFile(file: KtFile) { file.accept(object : KtTreeVisitorVoid() { override fun visitCallExpression(expression: KtCallExpression){ processCallExpression(expression) super.visitCallExpression(expression) } } } Haruto Kato 83

Slide 84

Slide 84 text

コールグラフ構築の全体像 1. KtFile内の1⾏⽬から最終⾏まで全⾛査 2. 関数呼び出しを検知 3. 呼び出し元関数を特定 4. ⼿順2及び3の関数同⼠の関係性をグラフ化 Haruto Kato 84

Slide 85

Slide 85 text

呼び出し元関数の検出 KtCallExpressionだ! Haruto Kato 85

Slide 86

Slide 86 text

呼び出し元関数の検出 KtNamedFunction Haruto Kato 86

Slide 87

Slide 87 text

呼び出し元関数の検出 KtCallExpressionだ! Haruto Kato 87

Slide 88

Slide 88 text

呼び出し元関数の検出 KtNamedFunctionだ! KtCallExpressionだ! Haruto Kato 88

Slide 89

Slide 89 text

呼び出し元関数の検出 fun resolveOwnerNode(expression: KtExpression) { var current: PsiElement = expression.parent while (current ?= null) { when (current) { is KtNamedFunction ?> { … return current.fqName } … } current = current.parent } } Haruto Kato 89

Slide 90

Slide 90 text

呼び出し元関数の検出 fun resolveOwnerNode(expression: KtExpression) { var current: PsiElement = expression.parent while (current ?= null) { when (current) { is KtNamedFunction ?> { … return current.fqName } … } current = current.parent } } Haruto Kato 90

Slide 91

Slide 91 text

呼び出し元関数の検出 fun resolveOwnerNode(expression: KtExpression) { var current: PsiElement = expression.parent while (current ?= null) { when (current) { is KtNamedFunction ?> { … return current.fqName } … } current = current.parent } } Haruto Kato 91

Slide 92

Slide 92 text

呼び出し元関数の検出 fun resolveOwnerNode(expression: KtExpression) { var current: PsiElement = expression.parent while (current ?= null) { when (current) { is KtNamedFunction ?> { … return current.fqName } … } current = current.parent } } Haruto Kato 92

Slide 93

Slide 93 text

呼び出し元関数の検出 fun resolveOwnerNode(expression: KtExpression) { var current: PsiElement = expression.parent while (current ?= null) { when (current) { is KtNamedFunction ?> { … return current.fqName } … } current = current.parent } } Haruto Kato 93

Slide 94

Slide 94 text

コールグラフ構築の全体像 1. KtFile内の1⾏⽬から最終⾏まで全⾛査 2. 関数呼び出しを検知 3. 呼び出し元関数を特定 4. ⼿順2及び3の関数同⼠の関係性をグラフ化 Haruto Kato 94

Slide 95

Slide 95 text

グラフモデルの考え Haruto Kato Caller Callee A B 95

Slide 96

Slide 96 text

ノードに対するエッジは1以上存在 B 複数の関数を呼ぶ A C B 複数の関数から呼ばれる A C Haruto Kato 96

Slide 97

Slide 97 text

コールグラフにはテスト関数も存在 B テスト関数 A 通常関数 C Haruto Kato 97

Slide 98

Slide 98 text

グラフデータの持ち⽅ - NG B map(A to listOf(B,C)) A C Haruto Kato 98

Slide 99

Slide 99 text

グラフデータの持ち⽅ - NG 知りたいこと: ある関数が、どの関数たちから呼ばれているか? Haruto Kato 99

Slide 100

Slide 100 text

グラフデータの持ち⽅ - NG B map(A to listOf(B,C)) A C ある関数が、どの関数たちを呼んでいるか? Haruto Kato 100

Slide 101

Slide 101 text

グラフデータの持ち⽅ - OK B A map(listOf(B,C) to A) C ある関数が、どの関数たちから呼ばれているか? Haruto Kato 101

Slide 102

Slide 102 text

CallGraph実装 class CallGraph { private val edges: MutableMap } fun addEdge(caller: Node, callee: Node) { … } … Haruto Kato 102

Slide 103

Slide 103 text

グラフ構築 fun processCallExpression(expression: KtExpression) { val caller = resolveOwnerNode(expression) ?: return analyze(expression) { val call = expression.resolveToCall() val symbol = call?.singleFunctionCallOrNull()?.symbol symbol?.let { val callee = Node(symbol) graph.addEdge(caller, callee) } } } Haruto Kato 103

Slide 104

Slide 104 text

関数呼び出しの検出 fun processFile(file: KtFile) { file.accept(object : KtTreeVisitorVoid() { override fun visitCallExpression(expression: KtCallExpression){ processCallExpression(expression) super.visitCallExpression(expression) } } } Haruto Kato 104

Slide 105

Slide 105 text

グラフ構築 fun processCallExpression(expression: KtExpression) { val caller = resolveOwnerNode(expression) ?: return analyze(expression) { val call = expression.resolveToCall() val symbol = call?.singleFunctionCallOrNull()?.symbol symbol?.let { val callee = Node(symbol) graph.addEdge(caller, callee) } } } Haruto Kato 105

Slide 106

Slide 106 text

グラフ構築 fun processCallExpression(expression: KtExpression) { val caller = resolveOwnerNode(expression) ?: return analyze(expression) { val call = expression.resolveToCall() val symbol = call?.singleFunctionCallOrNull()?.symbol symbol?.let { val callee = Node(symbol) graph.addEdge(caller, callee) } } } Haruto Kato 106

Slide 107

Slide 107 text

グラフ構築 fun processCallExpression(expression: KtExpression) { val caller = resolveOwnerNode(expression) ?: return analyze(expression) { val call = expression.resolveToCall() val symbol = call?.singleFunctionCallOrNull()?.symbol symbol?.let { val callee = Node(symbol) graph.addEdge(caller, callee) } } } Haruto Kato 107

Slide 108

Slide 108 text

グラフ構築 fun processCallExpression(expression: KtExpression) { val caller = resolveOwnerNode(expression) ?: return analyze(expression) { val call = expression.resolveToCall() val symbol = call?.singleFunctionCallOrNull()?.symbol symbol?.let { val callee = Node(symbol) graph.addEdge(caller, callee) } } } Haruto Kato 108

Slide 109

Slide 109 text

resolveToCall val x = hoge.kotlinLove() resolveToCall io.github.sazanami.core.KotlinLove.kotlinLove Haruto Kato 109

Slide 110

Slide 110 text

グラフ構築 fun processCallExpression(expression: KtExpression) { val caller = resolveOwnerNode(expression) ?: return analyze(expression) { val call = expression.resolveToCall() val symbol = call?.singleFunctionCallOrNull()?.symbol symbol?.let { val callee = Node(symbol) graph.addEdge(caller, callee) } } } Haruto Kato 110

Slide 111

Slide 111 text

加⼯フェーズの責務 Kotlin Analysis⽤解析セッション構築 関数コールグラフ構築 影響範囲⾛査 Haruto Kato 111

Slide 112

Slide 112 text

BFSについて BFS (幅優先探索) Haruto Kato 112

Slide 113

Slide 113 text

BFSについて rootノードに近い順に各ノードを⾛査する⼿法 Haruto Kato 113

Slide 114

Slide 114 text

BFSのパフォーマンスは良い O(V+E) V : 総ノード数 E: 総エッジ数 Haruto Kato 114

Slide 115

Slide 115 text

BFSについて B A C Haruto Kato 115 D

Slide 116

Slide 116 text

BFSについて B A C Haruto Kato 116 D

Slide 117

Slide 117 text

BFSについて B A C Haruto Kato 117 D

Slide 118

Slide 118 text

BFSについて B A C Haruto Kato 118 D

Slide 119

Slide 119 text

影響範囲⾛査 B’ A C Haruto Kato 119 D’

Slide 120

Slide 120 text

影響範囲⾛査 B’ A C Haruto Kato 120 D’

Slide 121

Slide 121 text

影響範囲⾛査 B’ A C Haruto Kato 121 D’

Slide 122

Slide 122 text

影響範囲⾛査 B’ A C Haruto Kato 122 D’

Slide 123

Slide 123 text

影響範囲⾛査 B’ A C Haruto Kato 123 D’

Slide 124

Slide 124 text

影響範囲⾛査 B’ A C Haruto Kato 124 D’

Slide 125

Slide 125 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 125

Slide 126

Slide 126 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 126

Slide 127

Slide 127 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 127

Slide 128

Slide 128 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 128

Slide 129

Slide 129 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 129

Slide 130

Slide 130 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 130

Slide 131

Slide 131 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 131

Slide 132

Slide 132 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 132

Slide 133

Slide 133 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 133

Slide 134

Slide 134 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 134

Slide 135

Slide 135 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 135

Slide 136

Slide 136 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 136

Slide 137

Slide 137 text

影響範囲⾛査 fun findAffectedTests(changedFunctions: Set) { val affectedTests = mutableSetOf() val seen = mutableSetOf() val queue = ArrayDeque(changedFunctions) while (queue.isNotEmpty()) { val callee = queue.removeFirst() if (callee in seen) continue seen.add(callee) val callers = graph.getCallersByFqn(callee.fqn) for (caller in callers) { if (caller.isTest) { affectedTests.add(caller.fqn) } queue.add(caller) } } return affectedTests } Haruto Kato 137

Slide 138

Slide 138 text

処理全体像 入力 Haruto Kato 加工 138 出力

Slide 139

Slide 139 text

出⼒フェーズ io.github.sazanami.hoge.funcA io.github.sazanami.hoge.funcB io.github.sazanami.hoge.funcC Haruto Kato 139

Slide 140

Slide 140 text

出⼒フェーズ ファイルへ書込み 出⼒⽤ストリームへ書込み その他 Haruto Kato 140

Slide 141

Slide 141 text

限界 現在の限界 Haruto Kato 141

Slide 142

Slide 142 text

技術の破綻 B A Haruto Kato 142

Slide 143

Slide 143 text

技術の破綻 B A 到達可能性(Reachability) Haruto Kato 143

Slide 144

Slide 144 text

技術の破綻(Observerパターン) fun emitter() = dispatch(EVENT_A) fun collector() = runBlocking { EVENT_A().collect { … } } Haruto Kato 144

Slide 145

Slide 145 text

技術の破綻(Observerパターン) B A Haruto Kato 145

Slide 146

Slide 146 text

限界 静的解析で追えない情報は検出が困難 Haruto Kato 146

Slide 147

Slide 147 text

限界 CIなど最終防衛ラインでは使えない Haruto Kato 147

Slide 148

Slide 148 text

限界との向き合い⽅ ただ、カバーできる⽅法はまだある Haruto Kato 148

Slide 149

Slide 149 text

⾃分なりの限界との向き合い⽅ ariadne MCPサーバ。 sazanamiを経由してAIエージェントに 現在の差分からテストすべき関数を渡す。 Haruto Kato 149

Slide 150

Slide 150 text

ariadneの発想 AI駆動開発… 正確性 < 速度 Haruto Kato 150

Slide 151

Slide 151 text

限界との向き合い⽅ 1. 精度を改善する 2. 運⽤でカバーする 3. 課題が弱点となりにくい領域へ軸を移す Haruto Kato 151

Slide 152

Slide 152 text

まとめ 1. Kotlin Analysis APIはPSIと組み合わせることで強⼒ 2. グラフ理論を応⽤すると依存関係を表現可能 3. 静的解析のみで100%検知は不可能 Haruto Kato 152

Slide 153

Slide 153 text

Thank you for your attention ご清聴ありがとうございました! Haruto Kato 153